From 80a8719253180efc68ee1d63c23580700f702d60 Mon Sep 17 00:00:00 2001 From: hermes Date: Wed, 30 Sep 2026 21:59:32 +0200 Subject: [PATCH] add internal/infra/m3u_test.go --- internal/infra/m3u_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 internal/infra/m3u_test.go diff --git a/internal/infra/m3u_test.go b/internal/infra/m3u_test.go new file mode 100644 index 0000000..2eb99d3 --- /dev/null +++ b/internal/infra/m3u_test.go @@ -0,0 +1,35 @@ +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) + } +}