package streaming import ( "context" "errors" "io" "os" "path/filepath" "testing" ) type fakeSource struct { files map[int64]File err error } func (f fakeSource) PlayableFile(_ context.Context, trackID int64) (File, error) { if f.err != nil { return File{}, f.err } file, ok := f.files[trackID] if !ok { return File{}, ErrNotFound } return file, nil } func write(t *testing.T, path, content string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatal(err) } } func service(files map[int64]File) *Service { return NewService(fakeSource{files: files}) } func TestOpenServesTheFile(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "Artist/song.mp3"), "audio-bytes") s := service(map[int64]File{1: {Root: root, RelPath: "Artist/song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatal(err) } defer st.Close() data, _ := io.ReadAll(st.File) if string(data) != "audio-bytes" { t.Errorf("content = %q", data) } if st.ContentType != "audio/mpeg" || st.Name != "song.mp3" || st.ModTime.IsZero() { t.Errorf("stream = %+v", st) } } func TestContentTypeFollowsTheExtension(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "a.FLAC"), "x") write(t, filepath.Join(root, "b.mp3"), "x") write(t, filepath.Join(root, "c.xyz"), "x") s := service(map[int64]File{ 1: {Root: root, RelPath: "a.FLAC"}, 2: {Root: root, RelPath: "b.mp3"}, 3: {Root: root, RelPath: "c.xyz"}, }) want := map[int64]string{1: "audio/flac", 2: "audio/mpeg", 3: "application/octet-stream"} for id, ct := range want { st, err := s.Open(context.Background(), id) if err != nil { t.Fatal(err) } if st.ContentType != ct { t.Errorf("track %d: content type = %q, want %q", id, st.ContentType, ct) } st.Close() } } func TestOpenUnknownTrack(t *testing.T) { s := service(nil) if _, err := s.Open(context.Background(), 42); !errors.Is(err, ErrNotFound) { t.Fatalf("err = %v, want ErrNotFound", err) } } func TestOpenPropagatesSourceErrors(t *testing.T) { boom := errors.New("db down") s := NewService(fakeSource{err: boom}) if _, err := s.Open(context.Background(), 1); !errors.Is(err, boom) { t.Fatalf("err = %v, want the source error", err) } } func TestOpenMissingFileOnDisk(t *testing.T) { root := t.TempDir() s := service(map[int64]File{1: {Root: root, RelPath: "gone.mp3"}}) if _, err := s.Open(context.Background(), 1); !errors.Is(err, ErrFileMissing) { t.Fatalf("err = %v, want ErrFileMissing", err) } } func TestOpenRejectsUnsafePaths(t *testing.T) { root := t.TempDir() outside := t.TempDir() write(t, filepath.Join(outside, "secret.mp3"), "secret") write(t, filepath.Join(root, "ok.mp3"), "ok") tests := []struct { name string file File }{ {"dot dot", File{Root: root, RelPath: "../" + filepath.Base(outside) + "/secret.mp3"}}, {"deep dot dot", File{Root: root, RelPath: "a/../../x.mp3"}}, {"absolute path", File{Root: root, RelPath: filepath.Join(outside, "secret.mp3")}}, {"empty path", File{Root: root, RelPath: ""}}, {"the root itself", File{Root: root, RelPath: "."}}, {"relative root", File{Root: "relative/dir", RelPath: "ok.mp3"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := service(map[int64]File{1: tt.file}) st, err := s.Open(context.Background(), 1) if err == nil { st.Close() t.Fatal("unsafe path was opened") } if !errors.Is(err, ErrUnsafePath) && !errors.Is(err, ErrFileMissing) { t.Fatalf("err = %v, want ErrUnsafePath", err) } if errors.Is(err, ErrNotFound) { t.Fatal("must not look like a plain not-found") } }) } } func TestOpenRejectsSymlinkEscapingTheRoot(t *testing.T) { root := t.TempDir() outside := t.TempDir() write(t, filepath.Join(outside, "secret.mp3"), "secret") if err := os.Symlink(filepath.Join(outside, "secret.mp3"), filepath.Join(root, "link.mp3")); err != nil { t.Skip("symlinks not supported:", err) } if err := os.Symlink(outside, filepath.Join(root, "dirlink")); err != nil { t.Skip("symlinks not supported:", err) } for _, rel := range []string{"link.mp3", "dirlink/secret.mp3"} { s := service(map[int64]File{1: {Root: root, RelPath: rel}}) st, err := s.Open(context.Background(), 1) if err == nil { data, _ := io.ReadAll(st.File) st.Close() t.Fatalf("%s: symlink escaped the root and served %q", rel, data) } if !errors.Is(err, ErrUnsafePath) { t.Errorf("%s: err = %v, want ErrUnsafePath", rel, err) } } } func TestOpenAllowsRelativeSymlinkThatStaysInsideTheRoot(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "real/song.mp3"), "ok") if err := os.Symlink("real", filepath.Join(root, "alias")); err != nil { t.Skip("symlinks not supported:", err) } s := service(map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatalf("relative symlink inside the root rejected: %v", err) } st.Close() } // os.Root refuses absolute symlink targets even when they point back inside the // root. The scanner never indexes through a symlink, so this only costs files // that were linked in after the scan, and it keeps the rule simple and safe. func TestOpenRefusesAbsoluteSymlinksEvenInsideTheRoot(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "real/song.mp3"), "ok") if err := os.Symlink(filepath.Join(root, "real"), filepath.Join(root, "alias")); err != nil { t.Skip("symlinks not supported:", err) } s := service(map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) st, err := s.Open(context.Background(), 1) if err == nil { st.Close() t.Fatal("an absolute symlink was followed") } if !errors.Is(err, ErrUnsafePath) { t.Errorf("err = %v, want ErrUnsafePath", err) } } func TestOpenAcceptsASymlinkedRoot(t *testing.T) { real := t.TempDir() write(t, filepath.Join(real, "song.mp3"), "ok") link := filepath.Join(t.TempDir(), "music") if err := os.Symlink(real, link); err != nil { t.Skip("symlinks not supported:", err) } s := service(map[int64]File{1: {Root: link, RelPath: "song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatalf("a symlinked root is a normal setup: %v", err) } st.Close() } func TestOpenRejectsDirectories(t *testing.T) { root := t.TempDir() if err := os.MkdirAll(filepath.Join(root, "album.mp3"), 0o755); err != nil { t.Fatal(err) } s := service(map[int64]File{1: {Root: root, RelPath: "album.mp3"}}) st, err := s.Open(context.Background(), 1) if err == nil { st.Close() t.Fatal("a directory was opened as a stream") } if !errors.Is(err, ErrUnsafePath) { t.Errorf("err = %v, want ErrUnsafePath", err) } } func TestRootsAreComparedAfterCleaning(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "song.mp3"), "ok") // A stored directory with a trailing slash is still the same directory. s := service(map[int64]File{1: {Root: root + "/", RelPath: "song.mp3"}}) st, err := s.Open(context.Background(), 1) if err != nil { t.Fatalf("trailing slash on the stored root: %v", err) } st.Close() }