A friend's server works out the peaks of a track in a shared library and gives
them to a signed friend (GET /peer/tracks/{id}/peaks). This server asks for them
and passes them on to the listener after checking them: 1 to 1000 levels from 0
to 255, as many as the count says (GET /friends/{friend}/tracks/{id}/peaks). The
player uses it for a friend's track, so its bar is no longer flat.
193 lines
7.2 KiB
Go
193 lines
7.2 KiB
Go
package federation
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"errors"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// getRemote is a Remote whose signed GET answers as a script says.
|
|
type getRemote struct {
|
|
fakeRemote
|
|
status int
|
|
body string
|
|
err error
|
|
uri string
|
|
auth Auth
|
|
addr string
|
|
calls int
|
|
}
|
|
|
|
func (g *getRemote) Get(_ context.Context, addr, uri string, auth Auth) (int, []byte, error) {
|
|
g.calls++
|
|
g.addr, g.uri, g.auth = addr, uri, auth
|
|
return g.status, []byte(g.body), g.err
|
|
}
|
|
|
|
// browsing returns a server that has an active friend, and the remote it asks.
|
|
func browsing(t *testing.T) (*Service, *memStore, *getRemote, int64) {
|
|
t.Helper()
|
|
s, st, _, _, _, id := pairedServers(t)
|
|
remote := &getRemote{status: 200, body: `{"items":[],"total":0,"limit":50,"offset":0}`}
|
|
s.remote = remote
|
|
return s, st, remote, id
|
|
}
|
|
|
|
func TestAListenerSeesTheFriendsThatAreActiveAndNothingElse(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, st, _, _ := browsing(t)
|
|
pub, _, _ := ed25519.GenerateKey(nil)
|
|
_, _ = st.AddFriend(ctx, Friend{Name: "Waiting", Address: "https://w.example.org", PublicKey: pub, State: Outgoing})
|
|
pub2, _, _ := ed25519.GenerateKey(nil)
|
|
_, _ = st.AddFriend(ctx, Friend{Name: "Asking", Address: "https://a.example.org", PublicKey: pub2, State: Incoming})
|
|
|
|
got, err := s.ActiveFriends(ctx)
|
|
if err != nil || len(got) != 1 || got[0].Name != "Bruno" {
|
|
t.Fatalf("ActiveFriends = %+v, %v", got, err)
|
|
}
|
|
off := false
|
|
if _, err := s.Update(ctx, Patch{Enabled: &off}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := s.ActiveFriends(ctx); err != nil || len(got) != 0 {
|
|
t.Errorf("while off: %+v, %v, want none", got, err)
|
|
}
|
|
}
|
|
|
|
func TestBrowsingAsksTheFriendWithASignedRequestForTheRightRoute(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, st, remote, id := browsing(t)
|
|
q := url.Values{"q": {"blue train"}, "limit": {"5"}, "offset": {"10"}, "artist_id": {"3"}, "album_id": {"4"}}
|
|
|
|
body, err := s.Browse(ctx, id, "tracks", 0, q)
|
|
if err != nil || string(body) != remote.body {
|
|
t.Fatalf("Browse = %q, %v", body, err)
|
|
}
|
|
if remote.uri != "/api/v1/peer/tracks?album_id=4&artist_id=3&limit=5&offset=10&q=blue+train" {
|
|
t.Errorf("uri = %q", remote.uri)
|
|
}
|
|
if remote.addr != "https://bruno.example.org" || remote.auth.KeyID != KeyID(st.settings.PublicKey) || len(remote.auth.Key) != ed25519.PrivateKeySize {
|
|
t.Errorf("asked %q with %+v", remote.addr, remote.auth.KeyID)
|
|
}
|
|
|
|
if _, err := s.Browse(ctx, id, "albums", 7, nil); err != nil || remote.uri != "/api/v1/peer/albums/7" {
|
|
t.Errorf("one album: %v, uri %q", err, remote.uri)
|
|
}
|
|
// Only what the friend's routes take is passed on, however it is asked.
|
|
if _, err := s.Browse(ctx, id, "artists", 0, url.Values{"q": {"x"}, "evil": {"1"}, "library_id": {"1"}, "access_token": {"secret"}}); err != nil || remote.uri != "/api/v1/peer/artists?q=x" {
|
|
t.Errorf("filtered: %v, uri %q", err, remote.uri)
|
|
}
|
|
// An item has no list parameters at all.
|
|
if _, err := s.Browse(ctx, id, "artists", 9, url.Values{"q": {"x"}, "limit": {"3"}}); err != nil || remote.uri != "/api/v1/peer/artists/9" {
|
|
t.Errorf("an item with parameters: %v, uri %q", err, remote.uri)
|
|
}
|
|
}
|
|
|
|
func TestBrowsingOnlyAsksForTheThreeKindsOfThing(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, _, remote, id := browsing(t)
|
|
for _, resource := range []string{"", "info", "tracks/1/stream", "../pair", "artists?x=1", "Tracks"} {
|
|
if _, err := s.Browse(ctx, id, resource, 0, nil); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("%q: err = %v, want ErrInvalidInput", resource, err)
|
|
}
|
|
}
|
|
if _, err := s.Browse(ctx, id, "tracks", -1, nil); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("a negative id: %v", err)
|
|
}
|
|
if remote.calls != 0 {
|
|
t.Errorf("the friend was asked %d times for things that cannot be asked", remote.calls)
|
|
}
|
|
}
|
|
|
|
func TestOnlyAnActiveFriendCanBeBrowsed(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, st, remote, id := browsing(t)
|
|
pub, _, _ := ed25519.GenerateKey(nil)
|
|
waiting, _ := st.AddFriend(ctx, Friend{Name: "Waiting", Address: "https://w.example.org", PublicKey: pub, State: Outgoing})
|
|
pub2, _, _ := ed25519.GenerateKey(nil)
|
|
asking, _ := st.AddFriend(ctx, Friend{Name: "Asking", Address: "https://a.example.org", PublicKey: pub2, State: Incoming})
|
|
|
|
for name, fid := range map[string]int64{"waiting": waiting.ID, "asking": asking.ID, "unknown": 999} {
|
|
if _, err := s.Browse(ctx, fid, "tracks", 0, nil); !errors.Is(err, ErrFriendMissing) {
|
|
t.Errorf("%s: err = %v, want ErrFriendMissing", name, err)
|
|
}
|
|
}
|
|
off := false
|
|
_, _ = s.Update(ctx, Patch{Enabled: &off})
|
|
if _, err := s.Browse(ctx, id, "tracks", 0, nil); !errors.Is(err, ErrFriendMissing) {
|
|
t.Errorf("while off: %v, want ErrFriendMissing", err)
|
|
}
|
|
if remote.calls != 0 {
|
|
t.Errorf("the friend was asked %d times", remote.calls)
|
|
}
|
|
}
|
|
|
|
func TestWhatAFriendAnswersIsToldInOurOwnTerms(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, _, remote, id := browsing(t)
|
|
for _, tc := range []struct {
|
|
status int
|
|
body string
|
|
err error
|
|
want error
|
|
}{
|
|
{400, `{"error":{"code":"invalid_input"}}`, nil, ErrInvalidInput},
|
|
{401, `{"error":{"code":"unauthorized"}}`, nil, ErrNotFriend},
|
|
{403, `{"error":{"code":"not_approved"}}`, nil, ErrNotApproved},
|
|
{404, `{"error":{"code":"not_found"}}`, nil, ErrNotFriend},
|
|
{404, `<html>nginx</html>`, nil, ErrNotFriend},
|
|
{404, `{"error":{"code":"track_not_found"}}`, nil, ErrNotFoundThere},
|
|
{404, `{"error":{"code":"album_not_found"}}`, nil, ErrNotFoundThere},
|
|
{429, `{"error":{"code":"too_many_requests"}}`, nil, ErrTooMany},
|
|
{500, `oops`, nil, ErrUnreachable},
|
|
{200, ``, ErrUnreachable, ErrUnreachable},
|
|
} {
|
|
remote.status, remote.body, remote.err = tc.status, tc.body, tc.err
|
|
_, err := s.Browse(ctx, id, "tracks", 1, nil)
|
|
if !errors.Is(err, tc.want) {
|
|
t.Errorf("%d %q: err = %v, want %v", tc.status, tc.body, err, tc.want)
|
|
}
|
|
if err != nil && strings.Contains(err.Error(), "bruno.example.org") && !errors.Is(err, ErrUnreachable) {
|
|
t.Errorf("the error names the friend's address: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestThePeaksOfAFriendsTrackAreAskedForSignedAndTheAnswerIsToldInOurOwnTerms(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, _, remote, id := browsing(t)
|
|
remote.body = `{"count":2,"peaks":[1,2]}`
|
|
body, err := s.Peaks(ctx, id, 7)
|
|
if err != nil || string(body) != remote.body {
|
|
t.Fatalf("Peaks = %q, %v", body, err)
|
|
}
|
|
if remote.uri != "/api/v1/peer/tracks/7/peaks" || remote.addr != "https://bruno.example.org" || remote.auth.KeyID == "" {
|
|
t.Errorf("asked %q at %q as %q", remote.uri, remote.addr, remote.auth.KeyID)
|
|
}
|
|
for _, tc := range []struct {
|
|
status int
|
|
body string
|
|
want error
|
|
}{
|
|
{202, `{"status":"working"}`, ErrWorking},
|
|
{404, `{"error":{"code":"waveform_unavailable"}}`, ErrNoWaveform},
|
|
{404, `{"error":{"code":"track_not_found"}}`, ErrNotFoundThere},
|
|
{404, `{"error":{"code":"file_missing"}}`, ErrNotFriend},
|
|
{429, ``, ErrTooMany},
|
|
{500, `oops`, ErrUnreachable},
|
|
} {
|
|
remote.status, remote.body = tc.status, tc.body
|
|
if _, err := s.Peaks(ctx, id, 7); !errors.Is(err, tc.want) {
|
|
t.Errorf("%d %q: err = %v, want %v", tc.status, tc.body, err, tc.want)
|
|
}
|
|
}
|
|
if _, err := s.Peaks(ctx, id, 0); !errors.Is(err, ErrInvalidInput) {
|
|
t.Errorf("track 0: %v, want ErrInvalidInput", err)
|
|
}
|
|
if _, err := s.Peaks(ctx, id+99, 7); !errors.Is(err, ErrFriendMissing) {
|
|
t.Errorf("a friend that is not there: %v, want ErrFriendMissing", err)
|
|
}
|
|
}
|