36 lines
969 B
Go
36 lines
969 B
Go
package infra
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"radio-go/internal/domain"
|
|
)
|
|
|
|
func TestWriteM3U(t *testing.T) {
|
|
pl := []domain.Track{
|
|
{ID: "a", Title: "Alpha", Duration: 123.7},
|
|
{ID: "b", Title: "", Style: "beta style", Duration: 45},
|
|
}
|
|
got := WriteM3U("My Station", pl)
|
|
want := "#EXTM3U\n" +
|
|
"#EXTINF:123,My Station - Alpha\na.mp3\n" +
|
|
"#EXTINF:45,My Station - beta style\nb.mp3\n"
|
|
if got != want {
|
|
t.Fatalf("m3u mismatch:\n got:\n%s\nwant:\n%s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestM3ULineSafe(t *testing.T) {
|
|
pl := []domain.Track{{ID: "x", Title: "evil\n#EXTINF:99,hack", Duration: 1}}
|
|
got := WriteM3U("st", pl)
|
|
lines := strings.Split(strings.TrimSpace(got), "\n")
|
|
// header + 1 extinf + 1 file = 3 lines; the injected newline must be neutralized
|
|
if len(lines) != 3 {
|
|
t.Fatalf("newline injection not sanitized: %q", got)
|
|
}
|
|
if strings.Contains(lines[1], "\n") || strings.Count(got, "#EXTINF") != 1 {
|
|
t.Fatalf("injected EXTINF survived: %q", got)
|
|
}
|
|
}
|