A friend's server can now read what an admin chose to share.
- A library has a shared flag, off by default (migration 0024), set with
PATCH /libraries/{id}. Changing it never waits for a scan.
- Requests between friends are signed with each server's Ed25519 key: the
method, the path and query from /api/v1/peer on, a date, a nonce and the
hash of the empty body. A date more than two minutes off, a nonce used
before, an unknown or removed key and a bad signature all get the same
401. A friend still waiting for approval gets 403, and while federation is
off the routes are missing.
- Routes under /api/v1/peer: info, artists, albums, tracks, covers and
streaming with Range. The catalogue, the counts and the format come from
the shared libraries only, and the file served is the one in a shared
library. No path, library id, listen, taste, user or provider data is part
of an answer.
- A friend never makes this server build a cover, read an audio file or ask
a provider: only covers already built from the audio files are served.
- Per-friend limits, set by the admin: parallel streams (default 3) and KiB
a second (default 4096, 0 for none), shared by all the streams of a friend.
- A log of the last requests friends made (route, status, bytes; never the
query), kept for 7 days, in GET /federation/requests.
- POST /federation/friends/{id}/check lets the side that pasted an invite
find out that the other admin approved; a signed call from a friend this
server is waiting for makes it active too.
- Admin: a Shared with friends switch on each library, which asks first, and
limits, recent requests and a Check button in the Federation section, in
English and Italian.
- OpenAPI paths and schemas, contract test, two real servers checked end to
end with tagged audio files.
160 lines
6.7 KiB
Go
160 lines
6.7 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/catalog"
|
|
"codeberg.org/squidd/sonda/internal/library"
|
|
"codeberg.org/squidd/sonda/internal/streaming"
|
|
)
|
|
|
|
func TestALibraryIsNotSharedUntilAnAdminSaysSo(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewLibraryRepo(db)
|
|
id := mkLib(t, repo, "music")
|
|
if lib, _ := repo.GetLibrary(ctx, id); lib.Shared {
|
|
t.Fatal("a new library must not be shared")
|
|
}
|
|
lib, err := repo.SetLibraryShared(ctx, id, true)
|
|
if err != nil || !lib.Shared {
|
|
t.Fatalf("SetLibraryShared = %+v, %v", lib, err)
|
|
}
|
|
if got, _ := repo.ListLibraries(ctx); len(got) != 1 || !got[0].Shared {
|
|
t.Errorf("ListLibraries = %+v", got)
|
|
}
|
|
// Renaming does not undo it.
|
|
if _, err := repo.UpdateLibrary(ctx, id, "renamed", lib.Path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := repo.GetLibrary(ctx, id); !got.Shared {
|
|
t.Errorf("after a rename: %+v", got)
|
|
}
|
|
if lib, err = repo.SetLibraryShared(ctx, id, false); err != nil || lib.Shared {
|
|
t.Errorf("unshare = %+v, %v", lib, err)
|
|
}
|
|
if _, err := repo.SetLibraryShared(ctx, 999, true); !errors.Is(err, library.ErrLibraryNotFound) {
|
|
t.Errorf("unknown library: %v, want ErrLibraryNotFound", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A friend sees what a listener would see if the only library were the shared ones: the same items, counts and
|
|
// formats, and nothing that lives only in a library that is not shared.
|
|
func TestFriendsSeeOnlyWhatIsInASharedLibrary(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
s := seedCatalog(t, db)
|
|
libs := NewLibraryRepo(db)
|
|
shared := NewSharedCatalogRepo(db)
|
|
|
|
// Nothing is shared: nothing is seen, however the ask is made.
|
|
for name, run := range map[string]func() (int, error){
|
|
"artists": func() (int, error) { p, err := shared.ListArtists(ctx, catalog.Filter{Limit: 50}); return p.Total, err },
|
|
"albums": func() (int, error) { p, err := shared.ListAlbums(ctx, catalog.Filter{Limit: 50}); return p.Total, err },
|
|
"tracks": func() (int, error) { p, err := shared.ListTracks(ctx, catalog.Filter{Limit: 50}); return p.Total, err },
|
|
} {
|
|
if n, err := run(); err != nil || n != 0 {
|
|
t.Errorf("%s with nothing shared: %d, %v", name, n, err)
|
|
}
|
|
}
|
|
if _, err := shared.GetTrack(ctx, s.track["So What"]); !errors.Is(err, catalog.ErrNotFound) {
|
|
t.Errorf("GetTrack with nothing shared: %v", err)
|
|
}
|
|
|
|
if _, err := libs.SetLibraryShared(ctx, s.jazz, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
all := catalog.Filter{Limit: 200}
|
|
inJazz := catalog.Filter{Limit: 200, LibraryID: s.jazz}
|
|
for _, c := range []struct {
|
|
name string
|
|
got func() (any, error)
|
|
want func() (any, error)
|
|
}{
|
|
{"artists", func() (any, error) { return shared.ListArtists(ctx, all) }, func() (any, error) { return s.repo.ListArtists(ctx, inJazz) }},
|
|
{"albums", func() (any, error) { return shared.ListAlbums(ctx, all) }, func() (any, error) { return s.repo.ListAlbums(ctx, inJazz) }},
|
|
{"tracks", func() (any, error) { return shared.ListTracks(ctx, all) }, func() (any, error) { return s.repo.ListTracks(ctx, inJazz) }},
|
|
} {
|
|
got, err1 := c.got()
|
|
want, err2 := c.want()
|
|
if err1 != nil || err2 != nil || !reflect.DeepEqual(got, want) {
|
|
t.Errorf("%s: shared view = %+v (%v), want what the jazz library alone shows = %+v (%v)", c.name, got, err1, want, err2)
|
|
}
|
|
}
|
|
|
|
// Airbag is in both libraries: it is seen through the shared file, as an mp3.
|
|
air, err := shared.GetTrack(ctx, s.track["Airbag"])
|
|
if err != nil || air.Format != "mp3" {
|
|
t.Errorf("Airbag = %+v, %v: want the format of the shared file", air, err)
|
|
}
|
|
// Something that lives only in the library that is not shared is not there, by id either.
|
|
if _, err := shared.GetTrack(ctx, s.track["Karma Police"]); !errors.Is(err, catalog.ErrNotFound) {
|
|
t.Errorf("GetTrack of an unshared track: %v", err)
|
|
}
|
|
var radiohead, okComputer int64
|
|
_ = db.QueryRow("SELECT id FROM artists WHERE name = 'Radiohead'").Scan(&radiohead)
|
|
_ = db.QueryRow("SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&okComputer)
|
|
if _, err := shared.GetArtist(ctx, radiohead); err != nil {
|
|
// Radiohead is credited on Airbag, which the shared library has: it is there, with one track.
|
|
t.Errorf("GetArtist(Radiohead) = %v", err)
|
|
}
|
|
if a, _ := shared.GetArtist(ctx, radiohead); a.TrackCount != 1 {
|
|
t.Errorf("Radiohead has %d tracks for a friend, want 1 (Airbag)", a.TrackCount)
|
|
}
|
|
// Searching and filtering cannot reach past the shared libraries either.
|
|
if p, _ := shared.ListTracks(ctx, catalog.Filter{Limit: 50, Query: "karma"}); p.Total != 0 {
|
|
t.Errorf("search found %d unshared tracks", p.Total)
|
|
}
|
|
if p, _ := shared.ListTracks(ctx, catalog.Filter{Limit: 50, LibraryID: s.rock}); p.Total != 0 {
|
|
t.Errorf("asking for the unshared library found %d tracks", p.Total)
|
|
}
|
|
if p, _ := shared.ListTracks(ctx, catalog.Filter{Limit: 50, AlbumID: okComputer}); len(p.Items) != 1 || p.Items[0].Title != "Airbag" {
|
|
t.Errorf("tracks of OK Computer for a friend = %v, want only Airbag", trackTitles(p))
|
|
}
|
|
|
|
// A server with the library unshared again shows nothing.
|
|
if _, err := libs.SetLibraryShared(ctx, s.jazz, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ := shared.ListTracks(ctx, all); p.Total != 0 {
|
|
t.Errorf("after unsharing: %d tracks", p.Total)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestAFriendIsOnlyServedFilesOfSharedLibraries(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
s := seedCatalog(t, db)
|
|
libs := NewLibraryRepo(db)
|
|
src := libs.Shared()
|
|
|
|
if _, err := src.PlayableFile(ctx, s.track["Karma Police"]); !errors.Is(err, streaming.ErrNotFound) {
|
|
t.Errorf("nothing shared: %v, want ErrNotFound", err)
|
|
}
|
|
if _, err := libs.SetLibraryShared(ctx, s.jazz, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := src.PlayableFile(ctx, s.track["Karma Police"]); !errors.Is(err, streaming.ErrNotFound) {
|
|
t.Errorf("a track of the library that is not shared: %v, want ErrNotFound", err)
|
|
}
|
|
f, err := src.PlayableFile(ctx, s.track["So What"])
|
|
if err != nil || f.Root != "/lib/jazz" || f.RelPath != "m/1.mp3" {
|
|
t.Errorf("So What = %+v, %v", f, err)
|
|
}
|
|
// Airbag has a file in each library: the friend gets the shared one, the listener the oldest.
|
|
f, err = src.PlayableFile(ctx, s.track["Airbag"])
|
|
if err != nil || f.Root != "/lib/jazz" || f.RelPath != "airbag.mp3" {
|
|
t.Errorf("Airbag for a friend = %+v, %v, want the file in the shared library", f, err)
|
|
}
|
|
if f, _ := libs.PlayableFile(ctx, s.track["Airbag"]); f.Root != "/lib/rock" {
|
|
t.Errorf("Airbag for a listener = %+v: the listener's choice must not change", f)
|
|
}
|
|
})
|
|
}
|