package httpapi import ( "bytes" "crypto/ed25519" "crypto/rand" "encoding/base64" "encoding/json" "image" "image/png" "net/http" "net/http/httptest" "os" "path/filepath" "strconv" "strings" "sync/atomic" "testing" "time" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/federation" "git.kmalloc.org/squid/sonda/internal/library" "git.kmalloc.org/squid/sonda/internal/storage" ) // peerFixture is a server with two libraries, one that can be shared and one that cannot, and a friend that holds // a key of its own. type peerFixture struct { h http.Handler db *bun.DB admin string fed *federation.Service pub ed25519.PublicKey priv ed25519.PrivateKey shared int64 // library ids private int64 track map[string]int64 friendID string bytes map[string][]byte // what each file holds root string // the folder of the libraries: one folder for each, with song.mp3 in it nonce atomic.Int64 } func peerParsed(rel, title, album, artist string) library.ParsedFile { return library.ParsedFile{ FileInfo: library.FileInfo{RelPath: rel, Size: 400, MTimeUnix: 1_700_000_000}, Metadata: library.Metadata{ Tags: library.Tags{Title: title, Album: album, AlbumArtist: artist, Artists: []string{artist}, TrackNo: 1, DiscNo: 1}, Properties: library.Properties{Format: "mpeg", DurationMs: 60000, SampleRate: 44100}, }, } } func newPeerFixture(t *testing.T) *peerFixture { t.Helper() f := &peerFixture{track: map[string]int64{}, bytes: map[string][]byte{}} h, db := newTestEnvDB(t, func(d *Deps, db *bun.DB) { f.fed = federation.NewService(storage.NewFederationRepo(db), federation.NewClient()) d.Federation = f.fed }) f.h, f.db = h, db f.admin = registerAndLogin(t, h) root := t.TempDir() f.root = root repo := storage.NewLibraryRepo(db) for _, l := range []struct { name, title, album, artist string fill byte id *int64 }{ {"shared", "Shared Song", "Shared Album", "Shared Band", 0xA1, &f.shared}, {"private", "Private Song", "Private Album", "Private Band", 0xB2, &f.private}, } { dir := filepath.Join(root, l.name) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } data := bytes.Repeat([]byte{l.fill}, 400) for i := range data { data[i] ^= byte(i) } f.bytes[l.title] = data if err := os.WriteFile(filepath.Join(dir, "song.mp3"), data, 0o644); err != nil { t.Fatal(err) } // A picture next to the audio: the cover of the album, once somebody asks for it. var pic bytes.Buffer if err := png.Encode(&pic, image.NewRGBA(image.Rect(0, 0, 40, 30))); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "cover.png"), pic.Bytes(), 0o644); err != nil { t.Fatal(err) } lib, err := repo.CreateLibrary(t.Context(), l.name, dir) if err != nil { t.Fatal(err) } *l.id = lib.ID if _, err := repo.SaveFile(t.Context(), lib.ID, peerParsed("song.mp3", l.title, l.album, l.artist), 0); err != nil { t.Fatal(err) } } rows, err := db.Query("SELECT id, title FROM tracks WHERE title <> 'song'") if err != nil { t.Fatal(err) } for rows.Next() { var id int64 var title string _ = rows.Scan(&id, &title) f.track[title] = id } _ = rows.Close() return f } func (f *peerFixture) admin1(t *testing.T, method, path string, body any) resp { t.Helper() return do(t, f.h, method, "/api/v1"+path, f.admin, body) } // befriend turns federation on and makes the friend (its key is in f.priv) an active friend, the way a real one // would: with an invite, and then the admin's approval. func (f *peerFixture) befriend(t *testing.T) { t.Helper() if r := f.admin1(t, "PUT", "/federation", map[string]any{"enabled": true, "name": "Anna", "address": "https://anna.example.org"}); r.Code != 200 { t.Fatalf("turn on: %d %v", r.Code, r.Body) } inv := f.admin1(t, "POST", "/federation/invites", nil) f.pub, f.priv, _ = ed25519.GenerateKey(rand.Reader) body := map[string]any{ "code": inviteCode(t, str(inv.Body, "invite")), "name": "Bruno", "address": "https://bruno.example.org", "public_key": base64.RawURLEncoding.EncodeToString(f.pub), } if r := do(t, f.h, "POST", "/api/v1/peer/pair", "", body); r.Code != 200 { t.Fatalf("pair: %d %v", r.Code, r.Body) } list := f.admin1(t, "GET", "/federation", nil).Body["friends"].([]any) f.friendID = strconv.Itoa(int(list[0].(map[string]any)["id"].(float64))) if r := f.admin1(t, "POST", "/federation/friends/"+f.friendID+"/approve", nil); r.Code != 200 { t.Fatalf("approve: %d %v", r.Code, r.Body) } } func (f *peerFixture) share(t *testing.T, id int64, on bool) { t.Helper() r := f.admin1(t, "PATCH", "/libraries/"+strconv.FormatInt(id, 10), map[string]any{"shared": on}) if r.Code != 200 || r.Body["shared"] != on { t.Fatalf("share %d: %d %v", id, r.Code, r.Body) } } // signed makes a request a friend would make, signed with its key. func (f *peerFixture) signed(method, path string, mutate ...func(*http.Request)) *http.Request { uri := "/api/v1/peer" + path n := f.nonce.Add(1) h := federation.Sign(f.priv, method, uri, time.Now(), "nonce-"+strconv.FormatInt(n, 10)+"-0123456789abcdef") req := httptest.NewRequest(method, uri, nil) req.Header.Set(federation.HeaderKeyID, federation.KeyID(f.pub)) req.Header.Set(federation.HeaderDate, h.Date) req.Header.Set(federation.HeaderNonce, h.Nonce) req.Header.Set(federation.HeaderSignature, h.Signature) for _, m := range mutate { m(req) } return req } func (f *peerFixture) serve(req *http.Request) *httptest.ResponseRecorder { rec := httptest.NewRecorder() f.h.ServeHTTP(rec, req) return rec } func (f *peerFixture) get(t *testing.T, path string) resp { t.Helper() rec := f.serve(f.signed("GET", path)) out := resp{Code: rec.Code, Body: map[string]any{}} _ = json.Unmarshal(rec.Body.Bytes(), &out.Body) return out } func titlesIn(r resp) []string { var out []string for _, it := range r.Body["items"].([]any) { m := it.(map[string]any) if v, ok := m["title"].(string); ok { out = append(out, v) } else { out = append(out, m["name"].(string)) } } return out } func TestAFriendMustSignItsRequests(t *testing.T) { f := newPeerFixture(t) f.share(t, f.shared, true) // Federation is off: the routes are as good as missing, whoever asks. if rec := f.serve(httptest.NewRequest("GET", "/api/v1/peer/info", nil)); rec.Code != 404 { t.Errorf("while off: %d", rec.Code) } f.befriend(t) for name, req := range map[string]*http.Request{ "no signature": httptest.NewRequest("GET", "/api/v1/peer/info", nil), "another path": f.signed("GET", "/info", func(r *http.Request) { r.URL.Path = "/api/v1/peer/artists" }), "a bad signature": f.signed("GET", "/info", func(r *http.Request) { r.Header.Set(federation.HeaderSignature, "AAAA") }), "an old date": f.signed("GET", "/info", func(r *http.Request) { r.Header.Set(federation.HeaderDate, strconv.FormatInt(time.Now().Add(-time.Hour).Unix(), 10)) }), "another key": f.signed("GET", "/info", func(r *http.Request) { r.Header.Set(federation.HeaderKeyID, "0123456789abcdef") }), } { rec := f.serve(req) var body struct{ Error struct{ Code string } } _ = json.Unmarshal(rec.Body.Bytes(), &body) if rec.Code != 401 || body.Error.Code != "unauthorized" { t.Errorf("%s: %d %s", name, rec.Code, rec.Body.String()) } } // A request is good once. req := f.signed("GET", "/info") if rec := f.serve(req); rec.Code != 200 { t.Fatalf("the good request: %d %s", rec.Code, rec.Body.String()) } if rec := f.serve(req.Clone(req.Context())); rec.Code != 401 { t.Errorf("the same request again: %d, want 401", rec.Code) } // Removing the friend ends it at once. f.admin1(t, "DELETE", "/federation/friends/"+f.friendID, nil) if rec := f.serve(f.signed("GET", "/info")); rec.Code != 401 { t.Errorf("after removal: %d, want 401", rec.Code) } } func TestAFriendWaitingForApprovalIsToldSo(t *testing.T) { f := newPeerFixture(t) f.admin1(t, "PUT", "/federation", map[string]any{"enabled": true, "address": "https://anna.example.org"}) inv := f.admin1(t, "POST", "/federation/invites", nil) f.pub, f.priv, _ = ed25519.GenerateKey(rand.Reader) do(t, f.h, "POST", "/api/v1/peer/pair", "", map[string]any{ "code": inviteCode(t, str(inv.Body, "invite")), "name": "Bruno", "address": "https://bruno.example.org", "public_key": base64.RawURLEncoding.EncodeToString(f.pub), }) rec := f.serve(f.signed("GET", "/info")) if rec.Code != 403 || !strings.Contains(rec.Body.String(), `"not_approved"`) { t.Errorf("unapproved friend: %d %s", rec.Code, rec.Body.String()) } } func TestAFriendSeesWhoWeAreAndHowManyLibrariesAreShared(t *testing.T) { f := newPeerFixture(t) f.befriend(t) r := f.get(t, "/info") sameKeys(t, "info", r.Body, "name", "shared_libraries") if r.Code != 200 || r.Body["name"] != "Anna" || r.Body["shared_libraries"] != float64(0) { t.Errorf("info = %d %v", r.Code, r.Body) } f.share(t, f.shared, true) if r := f.get(t, "/info"); r.Body["shared_libraries"] != float64(1) { t.Errorf("info = %v", r.Body) } } func TestAFriendReadsOnlyTheSharedLibraries(t *testing.T) { f := newPeerFixture(t) f.befriend(t) // Nothing is shared: nothing is there, by list, by id or by search. for _, p := range []string{"/artists", "/albums", "/tracks"} { if r := f.get(t, p); r.Code != 200 || r.Body["total"] != float64(0) { t.Errorf("%s with nothing shared = %d %v", p, r.Code, r.Body) } } for _, p := range []string{"/tracks/" + strconv.FormatInt(f.track["Shared Song"], 10), "/tracks/" + strconv.FormatInt(f.track["Private Song"], 10)} { if r := f.get(t, p); r.Code != 404 { t.Errorf("%s with nothing shared = %d", p, r.Code) } } f.share(t, f.shared, true) if got := titlesIn(f.get(t, "/tracks")); len(got) != 1 || got[0] != "Shared Song" { t.Errorf("tracks = %v", got) } if got := titlesIn(f.get(t, "/albums")); len(got) != 1 || got[0] != "Shared Album" { t.Errorf("albums = %v", got) } if got := titlesIn(f.get(t, "/artists")); len(got) != 1 || got[0] != "Shared Band" { t.Errorf("artists = %v", got) } if got := titlesIn(f.get(t, "/tracks?q=private")); len(got) != 0 { t.Errorf("a search found %v in the library that is not shared", got) } if got := titlesIn(f.get(t, "/tracks?library_id="+strconv.FormatInt(f.private, 10))); len(got) != 0 { t.Errorf("asking for the other library found %v", got) } one := f.get(t, "/tracks/"+strconv.FormatInt(f.track["Shared Song"], 10)) sameKeys(t, "track", one.Body, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format", "replay_gain") if one.Code != 200 || one.Body["title"] != "Shared Song" { t.Errorf("track = %d %v", one.Code, one.Body) } if r := f.get(t, "/tracks/"+strconv.FormatInt(f.track["Private Song"], 10)); r.Code != 404 || errCode(r) != "track_not_found" { t.Errorf("the track of the other library: %d %v", r.Code, r.Body) } for _, p := range []string{"/artists/", "/albums/"} { if r := f.get(t, p+"999999"); r.Code != 404 { t.Errorf("%s999999 = %d", p, r.Code) } } // Whatever the answer says, it is never a path. rec := f.serve(f.signed("GET", "/tracks")) if strings.Contains(rec.Body.String(), "song.mp3") || strings.Contains(rec.Body.String(), "/shared") { t.Errorf("the answer names a path: %s", rec.Body.String()) } // Unsharing takes it away at once. f.share(t, f.shared, false) if r := f.get(t, "/tracks"); r.Body["total"] != float64(0) { t.Errorf("after unsharing: %v", r.Body) } } func TestAFriendStreamsSharedMusicWithRanges(t *testing.T) { f := newPeerFixture(t) f.befriend(t) f.share(t, f.shared, true) want := f.bytes["Shared Song"] id := strconv.FormatInt(f.track["Shared Song"], 10) rec := f.serve(f.signed("GET", "/tracks/"+id+"/stream")) if rec.Code != 200 || !bytes.Equal(rec.Body.Bytes(), want) { t.Fatalf("stream: %d, %d bytes", rec.Code, rec.Body.Len()) } if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "audio/") { t.Errorf("Content-Type = %q", ct) } rec = f.serve(f.signed("GET", "/tracks/"+id+"/stream", func(r *http.Request) { r.Header.Set("Range", "bytes=100-199") })) if rec.Code != 206 || !bytes.Equal(rec.Body.Bytes(), want[100:200]) || rec.Header().Get("Content-Range") != "bytes 100-199/400" { t.Errorf("range: %d %q %d bytes", rec.Code, rec.Header().Get("Content-Range"), rec.Body.Len()) } // The other library is out of reach, by its id too. other := strconv.FormatInt(f.track["Private Song"], 10) if rec := f.serve(f.signed("GET", "/tracks/"+other+"/stream")); rec.Code != 404 { t.Errorf("stream of a track that is not shared: %d", rec.Code) } if rec := f.serve(f.signed("GET", "/tracks/999999/stream")); rec.Code != 404 { t.Errorf("stream of no track: %d", rec.Code) } } func TestAFriendCannotHaveMoreStreamsThanAllowed(t *testing.T) { f := newPeerFixture(t) f.befriend(t) f.share(t, f.shared, true) one := 1 if _, err := f.fed.Update(t.Context(), federation.Patch{MaxStreams: &one}); err != nil { t.Fatal(err) } fid, _ := strconv.ParseInt(f.friendID, 10, 64) open, err := f.fed.StartStream(t.Context(), fid) if err != nil { t.Fatal(err) } id := strconv.FormatInt(f.track["Shared Song"], 10) rec := f.serve(f.signed("GET", "/tracks/"+id+"/stream")) if rec.Code != 429 || !strings.Contains(rec.Body.String(), `"too_many_streams"`) || rec.Header().Get("Retry-After") == "" { t.Errorf("one stream too many: %d %s (Retry-After %q)", rec.Code, rec.Body.String(), rec.Header().Get("Retry-After")) } open.Close() if rec := f.serve(f.signed("GET", "/tracks/"+id+"/stream")); rec.Code != 200 { t.Errorf("after the first ended: %d", rec.Code) } } func TestACoverIsOnlyServedOnceBuiltAndOnlyForSharedAlbums(t *testing.T) { f := newPeerFixture(t) f.befriend(t) f.share(t, f.shared, true) var shared, private int64 _ = f.db.QueryRow("SELECT id FROM albums WHERE title = 'Shared Album'").Scan(&shared) _ = f.db.QueryRow("SELECT id FROM albums WHERE title = 'Private Album'").Scan(&private) sid, pid := strconv.FormatInt(shared, 10), strconv.FormatInt(private, 10) r := f.get(t, "/albums/"+sid+"/cover") if r.Code != 404 || errCode(r) != "cover_not_found" { t.Errorf("a cover that was not built: %d %v: a friend must not make this server build one", r.Code, r.Body) } if r := f.get(t, "/albums/"+pid+"/cover"); r.Code != 404 || errCode(r) != "album_not_found" { t.Errorf("the cover of an album that is not shared: %d %v", r.Code, r.Body) } if r := f.get(t, "/albums/"+sid+"/cover?size=huge"); r.Code != 400 { t.Errorf("a bad size: %d", r.Code) } } func TestSharingALibraryIsAnAdminsChoice(t *testing.T) { f := newPeerFixture(t) id := strconv.FormatInt(f.shared, 10) list := f.admin1(t, "GET", "/libraries", nil).Body["libraries"].([]any) for _, l := range list { if l.(map[string]any)["shared"] != false { t.Errorf("library %v starts shared", l) } } _, bob := addUser(t, f.h, f.admin, "bob", false) if r := do(t, f.h, "PATCH", "/api/v1/libraries/"+id, bob, map[string]any{"shared": true}); r.Code != 403 { t.Errorf("a user who is not an admin shares a library: %d", r.Code) } r := f.admin1(t, "PATCH", "/libraries/"+id, map[string]any{"shared": true}) if r.Code != 200 || r.Body["shared"] != true { t.Fatalf("share: %d %v", r.Code, r.Body) } // Name and sharing can be changed together or apart. r = f.admin1(t, "PATCH", "/libraries/"+id, map[string]any{"name": "renamed"}) if r.Code != 200 || r.Body["shared"] != true || r.Body["name"] != "renamed" { t.Errorf("rename keeps sharing: %d %v", r.Code, r.Body) } r = f.admin1(t, "PATCH", "/libraries/"+id, map[string]any{"name": "again", "shared": false}) if r.Code != 200 || r.Body["shared"] != false || r.Body["name"] != "again" { t.Errorf("both at once: %d %v", r.Code, r.Body) } if r := f.admin1(t, "PATCH", "/libraries/"+id, map[string]any{}); r.Code != 400 { t.Errorf("nothing to change: %d", r.Code) } if r := f.admin1(t, "PATCH", "/libraries/99999", map[string]any{"shared": true}); r.Code != 404 { t.Errorf("unknown library: %d", r.Code) } } func TestTheAdminSeesWhatFriendsAsked(t *testing.T) { f := newPeerFixture(t) f.befriend(t) f.share(t, f.shared, true) f.get(t, "/info") f.get(t, "/tracks?q=secret+words&limit=5") f.serve(f.signed("GET", "/tracks/"+strconv.FormatInt(f.track["Shared Song"], 10)+"/stream")) r := f.admin1(t, "GET", "/federation/requests", nil) if r.Code != 200 { t.Fatalf("requests: %d %v", r.Code, r.Body) } list := r.Body["requests"].([]any) if len(list) != 3 { t.Fatalf("requests = %v", list) } newest := list[0].(map[string]any) sameKeys(t, "request", newest, "at", "friend_id", "friend", "route", "status", "bytes") if newest["route"] != "/api/v1/peer/tracks/:id/stream" || newest["status"] != float64(200) || newest["bytes"] != float64(400) || newest["friend"] != "Bruno" { t.Errorf("newest = %v", newest) } if body := strings.ToLower(jsonOf(t, r.Body)); strings.Contains(body, "secret") { t.Errorf("the log keeps what was searched for: %s", body) } _, bob := addUser(t, f.h, f.admin, "bob", false) if r := do(t, f.h, "GET", "/api/v1/federation/requests", bob, nil); r.Code != 403 { t.Errorf("a user who is not an admin reads the log: %d", r.Code) } } func jsonOf(t *testing.T, v any) string { t.Helper() b, err := json.Marshal(v) if err != nil { t.Fatal(err) } return string(b) } func TestTheLimitsAreSetWithTheOtherSettings(t *testing.T) { f := newPeerFixture(t) r := f.admin1(t, "GET", "/federation", nil) if r.Body["max_streams"] != float64(3) || r.Body["max_kib_per_sec"] != float64(4096) { t.Errorf("defaults = %v %v", r.Body["max_streams"], r.Body["max_kib_per_sec"]) } r = f.admin1(t, "PUT", "/federation", map[string]any{"max_streams": 2, "max_kib_per_sec": 0}) if r.Code != 200 || r.Body["max_streams"] != float64(2) || r.Body["max_kib_per_sec"] != float64(0) { t.Errorf("update = %d %v", r.Code, r.Body) } for _, body := range []map[string]any{{"max_streams": 0}, {"max_streams": 99}, {"max_kib_per_sec": 5}} { if r := f.admin1(t, "PUT", "/federation", body); r.Code != 400 || errCode(r) != "invalid_input" { t.Errorf("%v: %d %v", body, r.Code, r.Body) } } } func TestAServerChecksWhetherTheOtherApproved(t *testing.T) { a := newFriendServer(t, true) b := newFriendServer(t, false) a.setUp(t, "Anna", a.ts.URL) b.setUp(t, "Bruno", "https://bruno.example.org") inv := a.call(t, "POST", "/federation/invites", nil) mine := b.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}) bID := strconv.Itoa(int(mine.Body["id"].(float64))) // Anna has not approved yet. r := b.call(t, "POST", "/federation/friends/"+bID+"/check", nil) if r.Code != http.StatusConflict || errCode(r) != "not_approved" { t.Fatalf("before approval: %d %v", r.Code, r.Body) } if got := b.call(t, "GET", "/federation", nil).Body["friends"].([]any)[0].(map[string]any)["state"]; got != "outgoing" { t.Errorf("state = %v", got) } list := a.call(t, "GET", "/federation", nil).Body["friends"].([]any) aID := strconv.Itoa(int(list[0].(map[string]any)["id"].(float64))) a.call(t, "POST", "/federation/friends/"+aID+"/approve", nil) r = b.call(t, "POST", "/federation/friends/"+bID+"/check", nil) if r.Code != 200 || r.Body["state"] != "active" || r.Body["approved_at"] == nil { t.Fatalf("after approval: %d %v", r.Code, r.Body) } // Anna logged that call. if reqs := a.call(t, "GET", "/federation/requests", nil).Body["requests"].([]any); len(reqs) != 1 || reqs[0].(map[string]any)["route"] != "/api/v1/peer/info" { t.Errorf("Anna's log = %v", reqs) } // Anna removes Bruno: Bruno is told it is no friend any more, and stays as it was. a.call(t, "DELETE", "/federation/friends/"+aID, nil) r = b.call(t, "POST", "/federation/friends/"+bID+"/check", nil) if r.Code != http.StatusConflict || errCode(r) != "friend_gone" { t.Errorf("after removal: %d %v", r.Code, r.Body) } // And a server that is gone is "unreachable", not a verdict. a.ts.Close() if r := b.call(t, "POST", "/federation/friends/"+bID+"/check", nil); r.Code != http.StatusBadGateway || errCode(r) != "friend_unreachable" { t.Errorf("server gone: %d %v", r.Code, r.Body) } if r := b.call(t, "POST", "/federation/friends/99999/check", nil); r.Code != 404 { t.Errorf("unknown friend: %d", r.Code) } } // serveAs asks the server as its admin and returns the whole answer. func (f *peerFixture) serveAs(t *testing.T, method, path string) *httptest.ResponseRecorder { t.Helper() req := httptest.NewRequest(method, path, nil) req.Header.Set("Authorization", "Bearer "+f.admin) rec := httptest.NewRecorder() f.h.ServeHTTP(rec, req) return rec }