package federation import ( "context" "crypto/ed25519" "errors" "net/http" "strconv" "strings" "testing" "time" ) // pairedServers makes two servers that are friends: b used an invite of a, and a approved. It returns a, its store, // b's key pair and the id of b among a's friends. func pairedServers(t *testing.T) (a *Service, aStore *memStore, ck *clock, bPub ed25519.PublicKey, bPriv ed25519.PrivateKey, bID int64) { t.Helper() ctx := context.Background() a, aStore, ck = newServer(t, "Anna", "https://anna.example.org", nil) b, bStore, _ := newServer(t, "Bruno", "https://bruno.example.org", &direct{to: a}) inv, _ := a.CreateInvite(ctx) if _, err := b.Redeem(ctx, inv.Text); err != nil { t.Fatal(err) } list, _ := aStore.Friends(ctx) if _, err := a.Approve(ctx, list[0].ID); err != nil { t.Fatal(err) } return a, aStore, ck, bStore.settings.PublicKey, bStore.settings.PrivateKey, list[0].ID } func signedBy(priv ed25519.PrivateKey, pub ed25519.PublicKey, now time.Time, uri, nonce string) Signed { h := Sign(priv, "GET", uri, now, nonce) return Signed{KeyID: KeyID(pub), Date: h.Date, Nonce: nonce, Signature: h.Signature, Method: "GET", URI: uri} } func TestAFriendThatSignsProperlyIsAuthenticated(t *testing.T) { a, _, ck, pub, priv, id := pairedServers(t) got, err := a.Authenticate(context.Background(), signedBy(priv, pub, ck.t, "/api/v1/peer/artists?limit=5", "nonce-0123456789ab")) if err != nil || got.ID != id || got.Name != "Bruno" || got.State != Active { t.Fatalf("Authenticate = %+v, %v", got, err) } } func TestAuthenticationRefusesWhatIsNotProperlySigned(t *testing.T) { ctx := context.Background() a, _, ck, pub, priv, _ := pairedServers(t) good := signedBy(priv, pub, ck.t, "/api/v1/peer/artists", "nonce-0123456789ab") stranger, strangerPriv, _ := ed25519.GenerateKey(nil) tamper := func(f func(*Signed)) Signed { s := good; f(&s); return s } for name, in := range map[string]Signed{ "nothing": {}, "another path": tamper(func(s *Signed) { s.URI = "/api/v1/peer/albums" }), "another query": tamper(func(s *Signed) { s.URI = "/api/v1/peer/artists?limit=1" }), "another method": tamper(func(s *Signed) { s.Method = "POST" }), "another nonce": tamper(func(s *Signed) { s.Nonce = "nonce-ffffffffffff" }), "a short nonce": signedBy(priv, pub, ck.t, "/api/v1/peer/artists", "short"), "a nonce with spaces": signedBy(priv, pub, ck.t, "/api/v1/peer/artists", "nonce with spaces!!"), "a damaged signature": tamper(func(s *Signed) { s.Signature = "AAAA" }), "no signature": tamper(func(s *Signed) { s.Signature = "" }), "a key that is no friend": signedBy(strangerPriv, stranger, ck.t, "/api/v1/peer/artists", "nonce-0123456789ab"), "a bad key id": tamper(func(s *Signed) { s.KeyID = "xyz" }), "a date that is no date": tamper(func(s *Signed) { s.Date = "yesterday" }), "too old": signedBy(priv, pub, ck.t.Add(-3*time.Minute), "/api/v1/peer/artists", "nonce-0123456789ab"), "from the future": signedBy(priv, pub, ck.t.Add(3*time.Minute), "/api/v1/peer/artists", "nonce-0123456789ab"), } { if _, err := a.Authenticate(ctx, in); !errors.Is(err, ErrUnauthorized) { t.Errorf("%s: err = %v, want ErrUnauthorized", name, err) } } // None of those used the nonce up: the good request still works. if _, err := a.Authenticate(ctx, good); err != nil { t.Errorf("the good request after the bad ones: %v", err) } } func TestAnAnswerToTheSameRequestIsNotGivenTwice(t *testing.T) { ctx := context.Background() a, _, ck, pub, priv, _ := pairedServers(t) req := signedBy(priv, pub, ck.t, "/api/v1/peer/artists", "nonce-0123456789ab") if _, err := a.Authenticate(ctx, req); err != nil { t.Fatal(err) } if _, err := a.Authenticate(ctx, req); !errors.Is(err, ErrUnauthorized) { t.Errorf("replay = %v, want ErrUnauthorized", err) } // Once the window has passed the request is too old anyway, and the nonce is forgotten. ck.t = ck.t.Add(10 * time.Minute) if _, err := a.Authenticate(ctx, req); !errors.Is(err, ErrUnauthorized) { t.Errorf("replay after the window = %v, want ErrUnauthorized", err) } if n := len(a.seen); n != 0 { // Cleaned the next time one is added. a.remember("k", "n", ck.t) if len(a.seen) != 1 { t.Errorf("seen nonces = %d, want the old ones forgotten", len(a.seen)) } } } func TestOnlyApprovedFriendsAreAnswered(t *testing.T) { ctx := context.Background() a, aStore, ck, pub, priv, id := pairedServers(t) // Not on, nothing is answered. off := false if _, err := a.Update(ctx, Patch{Enabled: &off}); err != nil { t.Fatal(err) } if _, err := a.Authenticate(ctx, signedBy(priv, pub, ck.t, "/x", "nonce-0123456789ab")); !errors.Is(err, ErrOff) { t.Errorf("while off: %v, want ErrOff", err) } on := true _, _ = a.Update(ctx, Patch{Enabled: &on}) // A request that is waiting for approval is told so, once its signature is known to be good. aStore.friends[0].State = Incoming if _, err := a.Authenticate(ctx, signedBy(priv, pub, ck.t, "/x", "nonce-aaaaaaaaaaaa")); !errors.Is(err, ErrNotApproved) { t.Errorf("incoming: %v, want ErrNotApproved", err) } // A friend that was removed is a stranger. if err := a.Remove(ctx, id); err != nil { t.Fatal(err) } if _, err := a.Authenticate(ctx, signedBy(priv, pub, ck.t, "/x", "nonce-bbbbbbbbbbbb")); !errors.Is(err, ErrUnauthorized) { t.Errorf("removed: %v, want ErrUnauthorized", err) } } func TestAServerThatApprovedUsMayCallBeforeWeHeardIt(t *testing.T) { // b pasted an invite: for b, a is outgoing. When a calls b it has approved (it would not call otherwise), // so b answers and the friendship is active from then on. ctx := context.Background() a, aStore, ck, _, _, _ := pairedServers(t) b, bStore, _ := newServer(t, "Carla", "https://carla.example.org", &direct{to: a}) inv, _ := a.CreateInvite(ctx) if _, err := b.Redeem(ctx, inv.Text); err != nil { t.Fatal(err) } b.now = ck.now list, _ := aStore.Friends(ctx) _, _ = a.Approve(ctx, list[len(list)-1].ID) aPub, aPriv := aStore.settings.PublicKey, aStore.settings.PrivateKey if st, _ := bStore.Friends(ctx); st[0].State != Outgoing { t.Fatalf("b's friend = %+v", st) } got, err := b.Authenticate(ctx, signedBy(aPriv, aPub, ck.t, "/x", "nonce-0123456789ab")) if err != nil || got.State != Active { t.Fatalf("Authenticate = %+v, %v", got, err) } if st, _ := bStore.Friends(ctx); st[0].State != Active || st[0].ApprovedAt.IsZero() { t.Errorf("b's friend after the call = %+v", st) } } func TestStreamsAreLimitedForEachFriend(t *testing.T) { ctx := context.Background() a, _, _, _, _, id := pairedServers(t) var open []*Stream for i := 0; i < DefaultMaxStreams; i++ { s, err := a.StartStream(ctx, id) if err != nil { t.Fatalf("stream %d: %v", i, err) } open = append(open, s) } if _, err := a.StartStream(ctx, id); !errors.Is(err, ErrTooManyStreams) { t.Fatalf("one too many = %v, want ErrTooManyStreams", err) } // Another friend has its own allowance. if s, err := a.StartStream(ctx, id+1); err != nil { t.Errorf("another friend: %v", err) } else { s.Close() } open[0].Close() open[0].Close() // closing twice frees only one place s, err := a.StartStream(ctx, id) if err != nil { t.Fatalf("after one ended: %v", err) } if _, err := a.StartStream(ctx, id); !errors.Is(err, ErrTooManyStreams) { t.Errorf("a double Close freed two places: %v", err) } s.Close() // The limit is the admin's to set. two := 2 if _, err := a.Update(ctx, Patch{MaxStreams: &two}); err != nil { t.Fatal(err) } for _, o := range open[1:] { o.Close() } x, _ := a.StartStream(ctx, id) y, _ := a.StartStream(ctx, id) if _, err := a.StartStream(ctx, id); !errors.Is(err, ErrTooManyStreams) { t.Errorf("limit 2: %v, want ErrTooManyStreams", err) } x.Close() y.Close() } func TestTheBytesOfAFriendAreSpacedOutOverTime(t *testing.T) { ctx := context.Background() a, _, ck, _, _, id := pairedServers(t) var slept time.Duration a.sleep = func(_ context.Context, d time.Duration) error { slept += d; ck.t = ck.t.Add(d); return nil } kib := 1024 // one MiB a second if _, err := a.Update(ctx, Patch{MaxKiBPerSec: &kib}); err != nil { t.Fatal(err) } s, err := a.StartStream(ctx, id) if err != nil { t.Fatal(err) } defer s.Close() for i := 0; i < 64; i++ { // 4 MiB in chunks of 64 KiB if err := s.Wait(ctx, 64<<10); err != nil { t.Fatal(err) } } // 4 MiB at 1 MiB/s is 4 s, less the short burst that is let through at once. if slept < 3500*time.Millisecond || slept > 4*time.Second { t.Errorf("slept %v for 4 MiB at 1 MiB/s", slept) } // Two streams of one friend share the allowance. slept = 0 s2, _ := a.StartStream(ctx, id) defer s2.Close() for i := 0; i < 16; i++ { _ = s.Wait(ctx, 64<<10) _ = s2.Wait(ctx, 64<<10) } if slept < 1500*time.Millisecond { t.Errorf("two streams slept %v for 2 MiB: they must share one allowance", slept) } // 0 means no limit. zero := 0 _, _ = a.Update(ctx, Patch{MaxKiBPerSec: &zero}) s3, _ := a.StartStream(ctx, id) defer s3.Close() slept = 0 for i := 0; i < 64; i++ { _ = s3.Wait(ctx, 64<<10) } if slept != 0 { t.Errorf("no limit slept %v", slept) } } func TestTheLimitsAreChecked(t *testing.T) { ctx := context.Background() s := NewService(newMemStore(), nil) for name, p := range map[string]Patch{ "no streams": {MaxStreams: ptr(0)}, "too many": {MaxStreams: ptr(21)}, "a trickle": {MaxKiBPerSec: ptr(10)}, "negative": {MaxKiBPerSec: ptr(-1)}, "a flood": {MaxKiBPerSec: ptr(200000)}, } { if _, err := s.Update(ctx, p); !errors.Is(err, ErrInvalidInput) { t.Errorf("%s: err = %v, want ErrInvalidInput", name, err) } } st, err := s.Update(ctx, Patch{MaxStreams: ptr(5), MaxKiBPerSec: ptr(512)}) if err != nil || st.MaxStreams != 5 || st.MaxKiBPerSec != 512 { t.Errorf("Update = %+v, %v", st, err) } if def, _ := NewService(newMemStore(), nil).Status(ctx); def.MaxStreams != DefaultMaxStreams || def.MaxKiBPerSec != DefaultMaxKiBPerSec { t.Errorf("defaults = %d and %d", def.MaxStreams, def.MaxKiBPerSec) } } func ptr(n int) *int { return &n } func TestTheRequestsOfFriendsAreKeptAndOldOnesDropped(t *testing.T) { ctx := context.Background() a, aStore, ck, _, _, id := pairedServers(t) for i := 0; i < 3; i++ { a.Record(ctx, FriendRequest{FriendID: id, Route: "/api/v1/peer/tracks/:id/stream", Status: 200, Bytes: int64(100 * (i + 1))}) ck.t = ck.t.Add(time.Minute) } got, err := a.Requests(ctx) if err != nil || len(got) != 3 { t.Fatalf("Requests = %+v, %v", got, err) } if got[0].Bytes != 300 || got[0].FriendName != "Bruno" || got[0].Route == "" || got[2].Bytes != 100 { t.Errorf("newest first, with the friend's name: %+v", got) } // Rows older than a week go, on a later write (every 100th). ck.t = ck.t.Add(8 * 24 * time.Hour) for i := 0; i < requestPruneEvery; i++ { a.Record(ctx, FriendRequest{FriendID: id, Route: "/api/v1/peer/info", Status: 200}) } for _, r := range aStore.requests { if ck.t.Sub(r.At) > requestKeep { t.Errorf("a request from %v is still kept", r.At) } } got, _ = a.Requests(ctx) if len(got) == 0 || len(got) > requestsShown { t.Errorf("Requests shows %d rows, want 1 to %d", len(got), requestsShown) } } // fakeRemote answers a signed call as a script says. type fakeRemote struct { info Info err error got Auth addr string } func (f *fakeRemote) Pair(context.Context, string, PairRequest) (PairReply, error) { return PairReply{}, errors.New("not used") } func (f *fakeRemote) Get(context.Context, string, string, Auth) (int, []byte, error) { return 0, nil, errors.New("not used") } func (f *fakeRemote) Open(context.Context, string, string, Auth, http.Header) (*http.Response, error) { return nil, errors.New("not used") } func (f *fakeRemote) Info(_ context.Context, addr string, auth Auth) (Info, error) { f.got, f.addr = auth, addr return f.info, f.err } func TestAServerThatPastedAnInviteLearnsWhenTheOtherApproved(t *testing.T) { ctx := context.Background() a, _, _ := newServer(t, "Anna", "https://anna.example.org", nil) remote := &fakeRemote{} b, bStore, _ := newServer(t, "Bruno", "https://bruno.example.org", &direct{to: a}) inv, _ := a.CreateInvite(ctx) mine, err := b.Redeem(ctx, inv.Text) if err != nil { t.Fatal(err) } b.remote = remote // Anna has not approved yet: the answer is "not yet", and the friend stays as it is. remote.err = ErrNotApproved got, err := b.Check(ctx, mine.ID) if !errors.Is(err, ErrNotApproved) || got.State != Outgoing { t.Fatalf("before approval: %+v, %v", got, err) } if remote.addr != "https://anna.example.org" || remote.got.KeyID != KeyID(bStore.settings.PublicKey) || len(remote.got.Key) != ed25519.PrivateKeySize { t.Errorf("the call was made to %q with %+v: it must be signed by this server's key", remote.addr, remote.got.KeyID) } // Anna approved: the first answer makes it active. remote.err, remote.info = nil, Info{Name: "Anna", SharedLibraries: 2} got, err = b.Check(ctx, mine.ID) if err != nil || got.State != Active || got.ApprovedAt.IsZero() { t.Fatalf("after approval: %+v, %v", got, err) } // And again once it is active: nothing changes, nothing breaks. if again, err := b.Check(ctx, mine.ID); err != nil || again.State != Active { t.Errorf("second check: %+v, %v", again, err) } // A server that cannot be asked is not a verdict. remote.err = ErrUnreachable if _, err := b.Check(ctx, mine.ID); !errors.Is(err, ErrUnreachable) { t.Errorf("unreachable: %v", err) } if st, _ := bStore.Friends(ctx); st[0].State != Active { t.Errorf("an unreachable server changed the state to %v", st[0].State) } if _, err := b.Check(ctx, 999); !errors.Is(err, ErrFriendMissing) { t.Errorf("unknown friend: %v", err) } } func TestAnIncomingRequestIsNotSomethingToCheck(t *testing.T) { ctx := context.Background() a, aStore, _ := newServer(t, "Anna", "https://anna.example.org", nil) a.remote = &fakeRemote{} pub, _, _ := ed25519.GenerateKey(nil) f, _ := aStore.AddFriend(ctx, Friend{Name: "B", Address: "https://b.example.org", PublicKey: pub, State: Incoming}) if _, err := a.Check(ctx, f.ID); !errors.Is(err, ErrWrongState) { t.Errorf("Check of an incoming friend = %v, want ErrWrongState", err) } } func TestSignAndVerifyAgree(t *testing.T) { pub, priv, _ := ed25519.GenerateKey(nil) now := time.Unix(1_700_000_000, 0) h := Sign(priv, "GET", "/api/v1/peer/info", now, "n-0123456789abcdef") if h.Date != strconv.FormatInt(now.Unix(), 10) || !strings.HasPrefix(h.Nonce, "n-") && h.Nonce == "" { t.Errorf("headers = %+v", h) } if !verify(pub, "GET", "/api/v1/peer/info", h.Date, "n-0123456789abcdef", h.Signature) { t.Error("a signature does not verify") } if verify(pub, "GET", "/api/v1/peer/other", h.Date, "n-0123456789abcdef", h.Signature) { t.Error("a signature verifies for another path") } }