From b7a497a30b5f3120ae131b597e8587db432e64a4 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 29 Apr 2021 10:26:53 -0700 Subject: [PATCH] ipn/ipnlocal: make FileTargets check IPN state first Signed-off-by: Brad Fitzpatrick --- ipn/ipnlocal/local.go | 6 +++--- ipn/ipnlocal/local_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index b04061b7f..07f563d56 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -2425,13 +2425,13 @@ func (b *LocalBackend) FileTargets() ([]*apitype.FileTarget, error) { b.mu.Lock() defer b.mu.Unlock() - if !b.capFileSharing { - return nil, errors.New("file sharing not enabled by Tailscale admin") - } nm := b.netMap if b.state != ipn.Running || nm == nil { return nil, errors.New("not connected") } + if !b.capFileSharing { + return nil, errors.New("file sharing not enabled by Tailscale admin") + } for _, p := range nm.Peers { if p.User != nm.User { continue diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 24a248f03..48dee16af 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -504,3 +504,33 @@ func TestLazyMachineKeyGeneration(t *testing.T) { // hit panicOnUseTransport). time.Sleep(500 * time.Millisecond) } + +func TestFileTargets(t *testing.T) { + b := new(LocalBackend) + _, err := b.FileTargets() + if got, want := fmt.Sprint(err), "not connected"; got != want { + t.Errorf("before connect: got %q; want %q", got, want) + } + + b.netMap = new(netmap.NetworkMap) + _, err = b.FileTargets() + if got, want := fmt.Sprint(err), "not connected"; got != want { + t.Errorf("non-running netmap: got %q; want %q", got, want) + } + + b.state = ipn.Running + _, err = b.FileTargets() + if got, want := fmt.Sprint(err), "file sharing not enabled by Tailscale admin"; got != want { + t.Errorf("without cap: got %q; want %q", got, want) + } + + b.capFileSharing = true + got, err := b.FileTargets() + if err != nil { + t.Fatal(err) + } + if len(got) != 0 { + t.Fatalf("unexpected %d peers", len(got)) + } + // (other cases handled by TestPeerAPIBase above) +}