sonda/internal/library/libraries_test.go
tgiachi 89826fcd07 feat(federation): shared libraries and the signed read API for friends
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.
2026-11-13 15:04:07 +01:00

442 lines
13 KiB
Go

package library
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// memLibraries is an in-memory LibraryStore that enforces unique names and paths.
type memLibraries struct {
mu sync.Mutex
next int64
libs []Library
delay time.Duration // makes ListLibraries slow, to widen race windows in tests
}
func (m *memLibraries) ListLibraries(context.Context) ([]Library, error) {
m.mu.Lock()
out := append([]Library(nil), m.libs...)
m.mu.Unlock()
time.Sleep(m.delay) // the snapshot is taken before the pause, like a slow query
return out, nil
}
func (m *memLibraries) GetLibrary(_ context.Context, id int64) (Library, error) {
m.mu.Lock()
defer m.mu.Unlock()
for _, l := range m.libs {
if l.ID == id {
return l, nil
}
}
return Library{}, ErrLibraryNotFound
}
func (m *memLibraries) clash(name, path string, skip int64) bool {
for _, l := range m.libs {
if l.ID != skip && (strings.EqualFold(l.Name, name) || l.Path == path) {
return true
}
}
return false
}
func (m *memLibraries) CreateLibrary(_ context.Context, name, path string) (Library, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.clash(name, path, 0) {
return Library{}, ErrLibraryExists
}
m.next++
l := Library{ID: m.next, Name: name, Path: path, CreatedAt: time.Now()}
m.libs = append(m.libs, l)
return l, nil
}
func (m *memLibraries) UpdateLibrary(_ context.Context, id int64, name, path string) (Library, error) {
m.mu.Lock()
defer m.mu.Unlock()
for i, l := range m.libs {
if l.ID == id {
if m.clash(name, path, id) {
return Library{}, ErrLibraryExists
}
m.libs[i].Name, m.libs[i].Path = name, path
return m.libs[i], nil
}
}
return Library{}, ErrLibraryNotFound
}
func (m *memLibraries) SetLibraryShared(_ context.Context, id int64, shared bool) (Library, error) {
m.mu.Lock()
defer m.mu.Unlock()
for i := range m.libs {
if m.libs[i].ID == id {
m.libs[i].Shared = shared
return m.libs[i], nil
}
}
return Library{}, ErrLibraryNotFound
}
func (m *memLibraries) DeleteLibrary(_ context.Context, id int64) error {
m.mu.Lock()
defer m.mu.Unlock()
for i, l := range m.libs {
if l.ID == id {
m.libs = append(m.libs[:i], m.libs[i+1:]...)
return nil
}
}
return ErrLibraryNotFound
}
func newLibraries() (*Libraries, *memLibraries) {
st := &memLibraries{}
return NewLibraries(st), st
}
func mkdir(t *testing.T, path string) string {
t.Helper()
if err := os.MkdirAll(path, 0o755); err != nil {
t.Fatal(err)
}
return path
}
func TestCreateLibrary(t *testing.T) {
s, _ := newLibraries()
dir := t.TempDir()
lib, err := s.Create(context.Background(), " Jazz ", dir+"/")
if err != nil {
t.Fatal(err)
}
if lib.Name != "Jazz" || lib.Path != dir || lib.ID == 0 {
t.Errorf("library = %+v, want trimmed name and cleaned path %q", lib, dir)
}
}
func TestCreateLibraryValidation(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "file.txt")
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
tests := []struct {
name, libName, path string
}{
{"empty name", "", dir},
{"blank name", " ", dir},
{"long name", strings.Repeat("n", 101), dir},
{"control character in name", "bad\x00name", dir},
{"empty path", "x", ""},
{"relative path", "x", "music/jazz"},
{"filesystem root", "x", "/"},
{"path does not exist", "x", filepath.Join(dir, "nope")},
{"path is a file", "x", file},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, _ := newLibraries()
if _, err := s.Create(context.Background(), tt.libName, tt.path); !errors.Is(err, ErrInvalidLibrary) {
t.Fatalf("err = %v, want ErrInvalidLibrary", err)
}
})
}
}
func TestCreateLibraryDuplicates(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
a, b := mkdir(t, filepath.Join(t.TempDir(), "a")), mkdir(t, filepath.Join(t.TempDir(), "b"))
if _, err := s.Create(ctx, "Music", a); err != nil {
t.Fatal(err)
}
if _, err := s.Create(ctx, "MUSIC", b); !errors.Is(err, ErrLibraryExists) {
t.Errorf("same name, other case: err = %v", err)
}
if _, err := s.Create(ctx, "Other", a); !errors.Is(err, ErrLibraryExists) {
t.Errorf("same path: err = %v", err)
}
if _, err := s.Create(ctx, "Other", a+"/"); !errors.Is(err, ErrLibraryExists) {
t.Errorf("same path with a trailing slash: err = %v", err)
}
}
// Nested libraries would index the same files twice.
func TestCreateLibraryRejectsOverlap(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
base := t.TempDir()
parent := mkdir(t, filepath.Join(base, "music"))
child := mkdir(t, filepath.Join(parent, "jazz"))
sibling := mkdir(t, filepath.Join(base, "music2")) // shares a string prefix, not a directory
if _, err := s.Create(ctx, "Music", parent); err != nil {
t.Fatal(err)
}
if _, err := s.Create(ctx, "Jazz", child); !errors.Is(err, ErrInvalidLibrary) {
t.Errorf("inside an existing library: err = %v", err)
}
if _, err := s.Create(ctx, "Sibling", sibling); err != nil {
t.Errorf("a sibling with a common prefix must be allowed: %v", err)
}
if _, err := s.Create(ctx, "Everything", base); !errors.Is(err, ErrInvalidLibrary) {
t.Errorf("around existing libraries: err = %v", err)
}
}
func TestCreateLibraryRejectsOverlapThroughSymlinks(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
base := t.TempDir()
parent := mkdir(t, filepath.Join(base, "music"))
child := mkdir(t, filepath.Join(parent, "jazz"))
link := filepath.Join(base, "shortcut")
if err := os.Symlink(child, link); err != nil {
t.Skip("symlinks not supported:", err)
}
if _, err := s.Create(ctx, "Music", parent); err != nil {
t.Fatal(err)
}
if _, err := s.Create(ctx, "Shortcut", link); !errors.Is(err, ErrInvalidLibrary) {
t.Errorf("a symlink into an existing library: err = %v", err)
}
}
func TestUpdateLibrary(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
base := t.TempDir()
a, b := mkdir(t, filepath.Join(base, "a")), mkdir(t, filepath.Join(base, "b"))
inside := mkdir(t, filepath.Join(a, "inside"))
first, _ := s.Create(ctx, "First", a)
second, _ := s.Create(ctx, "Second", b)
str := func(v string) *string { return &v }
// Rename only.
got, err := s.Update(ctx, first.ID, str("Renamed"), nil)
if err != nil || got.Name != "Renamed" || got.Path != a {
t.Fatalf("rename: %+v err=%v", got, err)
}
// Moving to its own current path is not an overlap with itself.
if _, err := s.Update(ctx, first.ID, nil, str(a)); err != nil {
t.Errorf("same path: %v", err)
}
// A new path in an unrelated place.
c := mkdir(t, filepath.Join(base, "c"))
if got, err = s.Update(ctx, first.ID, nil, str(c)); err != nil || got.Path != c {
t.Fatalf("move: %+v err=%v", got, err)
}
// Into another library, or onto its name or path.
if _, err := s.Update(ctx, first.ID, nil, str(b)); !errors.Is(err, ErrLibraryExists) {
t.Errorf("onto another library's path: err = %v", err)
}
if _, err := s.Update(ctx, second.ID, str("renamed"), nil); !errors.Is(err, ErrLibraryExists) {
t.Errorf("onto another library's name: err = %v", err)
}
if _, err := s.Update(ctx, first.ID, nil, str(filepath.Join(b, "deeper"))); !errors.Is(err, ErrInvalidLibrary) {
t.Errorf("a missing directory: err = %v", err)
}
_ = inside
if _, err := s.Update(ctx, first.ID, str(""), nil); !errors.Is(err, ErrInvalidLibrary) {
t.Errorf("empty name: err = %v", err)
}
if _, err := s.Update(ctx, 999, str("x"), nil); !errors.Is(err, ErrLibraryNotFound) {
t.Errorf("unknown id: err = %v", err)
}
}
func TestDeleteLibrary(t *testing.T) {
s, st := newLibraries()
ctx := context.Background()
lib, _ := s.Create(ctx, "Music", t.TempDir())
if err := s.Delete(ctx, lib.ID); err != nil {
t.Fatal(err)
}
if len(st.libs) != 0 {
t.Error("library not deleted")
}
if err := s.Delete(ctx, lib.ID); !errors.Is(err, ErrLibraryNotFound) {
t.Errorf("deleting twice: err = %v", err)
}
}
func TestListLibraries(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
base := t.TempDir()
_, _ = s.Create(ctx, "A", mkdir(t, filepath.Join(base, "a")))
_, _ = s.Create(ctx, "B", mkdir(t, filepath.Join(base, "b")))
libs, err := s.List(ctx)
if err != nil || len(libs) != 2 {
t.Fatalf("libs = %+v err=%v", libs, err)
}
}
// Names must clash on accents and case the same way on every database.
func TestLibraryNamesClashCaseInsensitivelyOnUnicode(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
base := t.TempDir()
if _, err := s.Create(ctx, "Música", mkdir(t, filepath.Join(base, "a"))); err != nil {
t.Fatal(err)
}
if _, err := s.Create(ctx, "MÚSICA", mkdir(t, filepath.Join(base, "b"))); !errors.Is(err, ErrLibraryExists) {
t.Fatalf("err = %v, want ErrLibraryExists", err)
}
}
// Two admins creating libraries at the same moment must not both pass the
// overlap check.
func TestConcurrentCreatesCannotOverlap(t *testing.T) {
for round := 0; round < 5; round++ {
s, st := newLibraries()
st.delay = 5 * time.Millisecond
parent := mkdir(t, filepath.Join(t.TempDir(), "music"))
child := mkdir(t, filepath.Join(parent, "a"))
var wg sync.WaitGroup
for i, p := range []string{parent, child} {
wg.Add(1)
go func() {
defer wg.Done()
_, _ = s.Create(context.Background(), "lib"+string(rune('a'+i)), p)
}()
}
wg.Wait()
if len(st.libs) != 1 {
t.Fatalf("round %d: %d libraries created, want 1 (the two paths overlap)", round, len(st.libs))
}
}
}
// A migrated library is named after its path, which can be longer than the
// name limit. Changing only the path must still work.
func TestUpdatingThePathDoesNotRevalidateAnUntouchedName(t *testing.T) {
s, st := newLibraries()
ctx := context.Background()
base := t.TempDir()
a, b := mkdir(t, filepath.Join(base, "a")), mkdir(t, filepath.Join(base, "b"))
long := strings.Repeat("x", 150)
st.libs = []Library{{ID: 1, Name: long, Path: a}}
st.next = 1
got, err := s.Update(ctx, 1, nil, &b)
if err != nil || got.Path != b || got.Name != long {
t.Fatalf("path-only update: %+v err=%v", got, err)
}
short := "short"
if _, err := s.Update(ctx, 1, &short, nil); err != nil {
t.Fatalf("renaming to a valid name: %v", err)
}
tooLong := strings.Repeat("y", 101)
if _, err := s.Update(ctx, 1, &tooLong, nil); !errors.Is(err, ErrInvalidLibrary) {
t.Fatalf("a new name is still validated: err = %v", err)
}
}
// Changing a library's directory or deleting it while a scan runs would leave
// the scan writing under a path that no longer matches.
func TestLibraryPathChangeAndDeleteAreRefusedWhileScanning(t *testing.T) {
s, _ := newLibraries()
ctx := context.Background()
base := t.TempDir()
a, b := mkdir(t, filepath.Join(base, "a")), mkdir(t, filepath.Join(base, "b"))
lib, _ := s.Create(ctx, "Music", a)
scanning := true
s.SetBusy(func() bool { return scanning })
if _, err := s.Update(ctx, lib.ID, nil, &b); !errors.Is(err, ErrScanRunning) {
t.Errorf("moving during a scan: err = %v, want ErrScanRunning", err)
}
if err := s.Delete(ctx, lib.ID); !errors.Is(err, ErrScanRunning) {
t.Errorf("deleting during a scan: err = %v, want ErrScanRunning", err)
}
// A rename, or "moving" to the same path, changes nothing under the scan.
name := "Renamed"
if _, err := s.Update(ctx, lib.ID, &name, nil); err != nil {
t.Errorf("renaming during a scan: %v", err)
}
if _, err := s.Update(ctx, lib.ID, nil, &a); err != nil {
t.Errorf("same path during a scan: %v", err)
}
scanning = false
if _, err := s.Update(ctx, lib.ID, nil, &b); err != nil {
t.Errorf("moving after the scan: %v", err)
}
if err := s.Delete(ctx, lib.ID); err != nil {
t.Errorf("deleting after the scan: %v", err)
}
}
func TestProblemsReportsOverlapAndMissingDirectories(t *testing.T) {
s, st := newLibraries()
ctx := context.Background()
base := t.TempDir()
parent := mkdir(t, filepath.Join(base, "music"))
child := mkdir(t, filepath.Join(parent, "flac"))
gone := filepath.Join(base, "gone")
fine := mkdir(t, filepath.Join(base, "fine"))
// Rows as a migration could leave them, bypassing validation.
st.libs = []Library{
{ID: 1, Name: "music", Path: parent},
{ID: 2, Name: "flac", Path: child},
{ID: 3, Name: "gone", Path: gone},
{ID: 4, Name: "fine", Path: fine},
}
problems, err := s.Problems(ctx)
if err != nil {
t.Fatal(err)
}
joined := strings.Join(problems, "\n")
for _, want := range []string{`"gone"`, `"music"`, `"flac"`} {
if !strings.Contains(joined, want) {
t.Errorf("problems %q do not mention %s", joined, want)
}
}
if strings.Contains(joined, `"fine"`) {
t.Errorf("a healthy library was reported: %q", joined)
}
st.libs = st.libs[3:]
if problems, _ := s.Problems(ctx); len(problems) != 0 {
t.Errorf("a healthy setup reported problems: %v", problems)
}
}
func TestSharingALibraryIsSeparateFromItsNameAndPath(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
svc := NewLibraries(&memLibraries{})
lib, err := svc.Create(ctx, "music", dir)
if err != nil || lib.Shared {
t.Fatalf("Create = %+v, %v: a new library is not shared", lib, err)
}
if lib, err = svc.SetShared(ctx, lib.ID, true); err != nil || !lib.Shared {
t.Fatalf("SetShared = %+v, %v", lib, err)
}
name := "renamed"
if lib, err = svc.Update(ctx, lib.ID, &name, nil); err != nil || !lib.Shared {
t.Errorf("after a rename: %+v, %v", lib, err)
}
if _, err := svc.SetShared(ctx, 99, true); !errors.Is(err, ErrLibraryNotFound) {
t.Errorf("SetShared on an unknown library: %v", err)
}
}