sonda/internal/storage/library_test.go
tgiachi 49f1051b3f feat(streaming): stream tracks with HTTP Range support
- feat(streaming): open a track's file only after checking it against the
  configured library roots: relative paths only, no "..", symlinks resolved on
  both sides so none can leave the root, regular files only
- feat(api): GET and HEAD /api/v1/tracks/{id}/stream via http.ServeContent
  (206, 416, Content-Length, conditional requests), MIME type from the
  extension, nosniff, no-referrer and private caching headers
- feat(api): the stream route also accepts the access token as access_token in
  the query string, because an audio element cannot send headers; no other
  route does, and the query string is never logged
- feat(storage): PlayableFile picks the oldest file of a track that is not
  flagged missing
- fix(api): internal errors set by handlers are now written to the request log
- test: range variants, HEAD, conditional requests, auth variants, traversal,
  symlink escape, symlinked root, missing file, directory instead of file
2026-11-11 12:56:20 +01:00

355 lines
12 KiB
Go

package storage
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/library"
"codeberg.org/squidd/sonda/internal/streaming"
)
func parsed(rel, title, album string, artists ...string) library.ParsedFile {
albumArtist := ""
if len(artists) > 0 {
albumArtist = artists[0]
}
return library.ParsedFile{
FileInfo: library.FileInfo{RelPath: rel, Size: 100, MTimeUnix: 1_700_000_000},
Metadata: library.Metadata{
Tags: library.Tags{
Title: title, Album: album, AlbumArtist: albumArtist,
Artists: artists, TrackNo: 2, DiscNo: 1,
},
Properties: library.Properties{Format: "flac", DurationMs: 180000, SampleRate: 44100, BitRate: 900},
},
}
}
func count(t *testing.T, db *bun.DB, query string, args ...any) int {
t.Helper()
var n int
if err := db.QueryRow(query, args...).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
func TestLibraryRepoSaveNewFile(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
added, err := repo.SaveFile(ctx, "/music", parsed("a/1.flac", "Song", "Album", "Radiohead"), 0)
if err != nil || !added {
t.Fatalf("added=%v err=%v", added, err)
}
// Same artist (different case) and same album are reused, not duplicated.
if _, err := repo.SaveFile(ctx, "/music", parsed("a/2.flac", "Other", "album", "RADIOHEAD"), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists"); n != 1 {
t.Errorf("artists = %d, want 1", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM albums"); n != 1 {
t.Errorf("albums = %d, want 1", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 2 {
t.Errorf("tracks = %d, want 2", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE root = ?", "/music"); n != 2 {
t.Errorf("audio_files = %d, want 2", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM track_artists"); n != 2 {
t.Errorf("track_artists = %d, want 2", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE mbid IS NOT NULL"); n != 0 {
t.Errorf("a MusicBrainz id was invented for %d artists", n)
}
var title string
var dur, trackNo int64
err = db.QueryRow("SELECT title, duration_ms, track_no FROM tracks WHERE title = 'Song'").Scan(&title, &dur, &trackNo)
if err != nil || dur != 180000 || trackNo != 2 {
t.Errorf("track row: %q %d %d err=%v", title, dur, trackNo, err)
}
})
}
// SQLite's lower() only folds ASCII, so matching is done in Go on a stored key.
func TestLibraryRepoMatchesUnicodeNamesCaseInsensitively(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
if _, err := repo.SaveFile(ctx, "/m", parsed("1.flac", "Hyperballad", "Post", "Björk"), 0); err != nil {
t.Fatal(err)
}
if _, err := repo.SaveFile(ctx, "/m", parsed("2.flac", "Army of Me", "POST", "BJÖRK"), 0); err != nil {
t.Fatal(err)
}
if _, err := repo.SaveFile(ctx, "/m", parsed("3.flac", "Isobel", "Ünïcode Album", "Åsa"), 0); err != nil {
t.Fatal(err)
}
if _, err := repo.SaveFile(ctx, "/m", parsed("4.flac", "Other", "ÜNÏCODE ALBUM", "ÅSA"), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists"); n != 2 {
t.Errorf("artists = %d, want 2 (Björk and Åsa, whatever the case)", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM albums"); n != 2 {
t.Errorf("albums = %d, want 2", n)
}
})
}
// Rows written before migration 0003 got an ASCII-only key from SQL lower().
// RefreshMatchKeys recomputes them so matching works for those rows too.
func TestLibraryRepoRefreshMatchKeysFixesOldRows(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
// Simulate what the migration backfill leaves behind on SQLite.
if _, err := db.Exec("INSERT INTO artists (name, name_key) VALUES ('BJÖRK', 'bjÖrk')"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO albums (title, title_key, artist_id) SELECT 'POST', 'post', id FROM artists"); err != nil {
t.Fatal(err)
}
if err := repo.RefreshMatchKeys(ctx); err != nil {
t.Fatal(err)
}
if err := repo.RefreshMatchKeys(ctx); err != nil { // idempotent
t.Fatal(err)
}
if _, err := repo.SaveFile(ctx, "/m", parsed("1.flac", "Hyperballad", "post", "Björk"), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM artists"); n != 1 {
t.Errorf("artists = %d, want 1: the old row was not matched", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM albums"); n != 1 {
t.Errorf("albums = %d, want 1", n)
}
})
}
func TestLibraryRepoTrackWithoutAlbum(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
repo := NewLibraryRepo(db)
if _, err := repo.SaveFile(context.Background(), "/m", parsed("x.flac", "Loose", "", "Someone"), 0); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM albums"); n != 0 {
t.Errorf("albums = %d, want 0", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks WHERE album_id IS NULL"); n != 1 {
t.Errorf("tracks without album = %d, want 1", n)
}
})
}
func TestLibraryRepoUpdateKeepsIdentityAndReplacesCredits(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
if _, err := repo.SaveFile(ctx, "/m", parsed("s.flac", "Old", "Album", "Artist A"), 0); err != nil {
t.Fatal(err)
}
existing, err := repo.ExistingFiles(ctx, "/m")
if err != nil || len(existing) != 1 {
t.Fatalf("existing = %v err=%v", existing, err)
}
id := existing[0].ID
upd := parsed("s.flac", "New", "Album", "Artist B")
upd.Size, upd.MTimeUnix = 200, 1_800_000_000
added, err := repo.SaveFile(ctx, "/m", upd, id)
if err != nil || added {
t.Fatalf("update: added=%v err=%v", added, err)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 1 {
t.Errorf("tracks = %d, want 1 (no duplicate on update)", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM audio_files"); n != 1 {
t.Errorf("audio_files = %d, want 1", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks WHERE title = 'New'"); n != 1 {
t.Error("title was not updated")
}
if n := count(t, db, `SELECT COUNT(*) FROM track_artists ta JOIN artists a ON a.id = ta.artist_id WHERE a.name = 'Artist B'`); n != 1 {
t.Error("credits were not replaced")
}
if n := count(t, db, `SELECT COUNT(*) FROM track_artists ta JOIN artists a ON a.id = ta.artist_id WHERE a.name = 'Artist A'`); n != 0 {
t.Error("old credit still present")
}
after, _ := repo.ExistingFiles(ctx, "/m")
if after[0].ID != id || after[0].Size != 200 || after[0].MTimeUnix != 1_800_000_000 {
t.Errorf("after update = %+v", after[0])
}
})
}
func TestLibraryRepoSameRelPathUnderTwoRoots(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
for _, root := range []string{"/a", "/b"} {
if _, err := repo.SaveFile(ctx, root, parsed("same.flac", "S", "", "X"), 0); err != nil {
t.Fatalf("root %s: %v", root, err)
}
}
for _, root := range []string{"/a", "/b"} {
if ex, _ := repo.ExistingFiles(ctx, root); len(ex) != 1 {
t.Errorf("root %s has %d files, want 1", root, len(ex))
}
}
})
}
func TestLibraryRepoMissingRestoreRename(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
if _, err := repo.SaveFile(ctx, "/m", parsed("a.flac", "A", "", "X"), 0); err != nil {
t.Fatal(err)
}
ex, _ := repo.ExistingFiles(ctx, "/m")
id := ex[0].ID
if err := repo.MarkMissing(ctx, []int64{id}, time.Unix(1_900_000_000, 0)); err != nil {
t.Fatal(err)
}
ex, _ = repo.ExistingFiles(ctx, "/m")
if len(ex) != 1 || !ex[0].Missing {
t.Fatalf("after MarkMissing: %+v (row must be kept, flagged)", ex)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 1 {
t.Errorf("tracks = %d: marking a file missing must keep the track", n)
}
if err := repo.Restore(ctx, []int64{id}); err != nil {
t.Fatal(err)
}
if ex, _ = repo.ExistingFiles(ctx, "/m"); ex[0].Missing {
t.Error("Restore did not clear the missing flag")
}
_ = repo.MarkMissing(ctx, []int64{id}, time.Now())
if err := repo.RenameFile(ctx, id, "sub/b.flac"); err != nil {
t.Fatal(err)
}
ex, _ = repo.ExistingFiles(ctx, "/m")
if ex[0].ID != id || ex[0].RelPath != "sub/b.flac" || ex[0].Missing {
t.Errorf("after rename: %+v", ex[0])
}
})
}
func TestLibraryRepoPlayableFile(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewLibraryRepo(db)
if _, err := repo.PlayableFile(ctx, 999); !errors.Is(err, streaming.ErrNotFound) {
t.Fatalf("unknown track: err = %v, want ErrNotFound", err)
}
if _, err := repo.SaveFile(ctx, "/music", parsed("a/1.flac", "Song", "", "X"), 0); err != nil {
t.Fatal(err)
}
var trackID, fileID int64
if err := db.QueryRow("SELECT track_id, id FROM audio_files").Scan(&trackID, &fileID); err != nil {
t.Fatal(err)
}
f, err := repo.PlayableFile(ctx, trackID)
if err != nil || f.Root != "/music" || f.RelPath != "a/1.flac" {
t.Fatalf("PlayableFile = %+v err=%v", f, err)
}
// A second encoding of the same track.
if _, err := db.Exec(`INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix)
VALUES (?, '/music', 'a/1.mp3', 'mp3', 1, 1)`, trackID); err != nil {
t.Fatal(err)
}
// The first file goes missing: the other one is served.
if err := repo.MarkMissing(ctx, []int64{fileID}, time.Now()); err != nil {
t.Fatal(err)
}
f, err = repo.PlayableFile(ctx, trackID)
if err != nil || f.RelPath != "a/1.mp3" {
t.Fatalf("with one file missing: %+v err=%v", f, err)
}
// Every file missing: nothing to play, but the track still exists.
if _, err := db.Exec("UPDATE audio_files SET missing_at = 1"); err != nil {
t.Fatal(err)
}
if _, err := repo.PlayableFile(ctx, trackID); !errors.Is(err, streaming.ErrNotFound) {
t.Fatalf("all files missing: err = %v, want ErrNotFound", err)
}
})
}
// stubReader lets the end-to-end test run without real audio.
type stubReader struct{}
func (stubReader) Read(path string) (library.Metadata, error) {
return library.Metadata{
Tags: library.Tags{Title: filepath.Base(path), Artists: []string{"Band"}, Album: "LP", AlbumArtist: "Band"},
Properties: library.Properties{Format: "mp3", DurationMs: 1000, SampleRate: 44100},
}, nil
}
func TestScannerEndToEndOnBothBackends(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
root := t.TempDir()
for _, n := range []string{"one.mp3", "two.mp3"} {
if err := os.WriteFile(filepath.Join(root, n), []byte(n), 0o644); err != nil {
t.Fatal(err)
}
}
s := library.NewScanner(NewLibraryRepo(db), stubReader{}, library.NewHub(), 4)
scan := func() library.Summary {
sum, err := s.Scan(context.Background(), []string{root})
if err != nil {
t.Fatal(err)
}
return sum
}
if sum := scan(); sum.Added != 2 {
t.Fatalf("first scan: %+v", sum)
}
if sum := scan(); sum.Unchanged != 2 || sum.Added != 0 {
t.Fatalf("rescan: %+v", sum)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 2 {
t.Errorf("tracks = %d after rescan, want 2 (no duplicates)", n)
}
if err := os.Remove(filepath.Join(root, "one.mp3")); err != nil {
t.Fatal(err)
}
if sum := scan(); sum.Missing != 1 {
t.Fatalf("after delete: %+v", sum)
}
if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE missing_at IS NOT NULL"); n != 1 {
t.Errorf("missing files = %d, want 1", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM tracks"); n != 2 {
t.Errorf("tracks = %d: history must survive a missing file", n)
}
})
}