absorb: playlist/m3u (M3U/M3U8 + HLS) and playlist/pls from playlist-luau 3fc7fd3, + a PLS writer; shared codecs/Url (RFC 3986, locate) and codecs/Text (BOM, CP1252, lines); tests vs urllib.parse.urljoin, Python cp1252, Python-written playlists

Two shared codecs added outside the assigned folders (a format never requires another format): codecs/Url and codecs/Text, used by the three playlist formats.
This commit is contained in:
4DBug committed 2026-10-31 08:55:44 -05:00
1 parent 17778cff1b
commit e3fba1c2ae
11 files changed
+1504

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
-- codecs/Url + codecs/Text tests: every example of RFC 3986 section 5.4 (normal and abnormal), dot-segment removal,
-- splitting, encoding and the location helpers (ported from playlist-luau tests/url.luau); random references against
-- Python's urllib.parse.urljoin; Url.locate / Url.base; Windows-1252 decoding against Python's cp1252 codec; lines,
-- BOM, trim; the fuzz pass. Fixtures: tools/fixtures/playlist.py.
local T = require("@tests/T")
local Url = require("@formats/codecs/Url")
local Text = require("@formats/codecs/Text")
local Ref = require("@tests/gen/playlist_url")
local Cp = require("@tests/gen/playlist_cp1252")
local t = T.suite("url")
local function eq(got: any, want: any, label: string)
t:eq(got, want, label)
end
local BASE = "http://a/b/c/d;p?q"
local cases = {
-- 5.4.1 normal
{ "g:h", "g:h" }, { "g", "http://a/b/c/g" }, { "./g", "http://a/b/c/g" }, { "g/", "http://a/b/c/g/" },
{ "/g", "http://a/g" }, { "//g", "http://g" }, { "?y", "http://a/b/c/d;p?y" }, { "g?y", "http://a/b/c/g?y" },
{ "#s", "http://a/b/c/d;p?q#s" }, { "g#s", "http://a/b/c/g#s" }, { "g?y#s", "http://a/b/c/g?y#s" },
{ ";x", "http://a/b/c/;x" }, { "g;x", "http://a/b/c/g;x" }, { "g;x?y#s", "http://a/b/c/g;x?y#s" },
{ "", "http://a/b/c/d;p?q" }, { ".", "http://a/b/c/" }, { "./", "http://a/b/c/" }, { "..", "http://a/b/" },
{ "../", "http://a/b/" }, { "../g", "http://a/b/g" }, { "../..", "http://a/" }, { "../../", "http://a/" },
{ "../../g", "http://a/g" },
-- 5.4.2 abnormal
{ "../../../g", "http://a/g" }, { "../../../../g", "http://a/g" }, { "/./g", "http://a/g" },
{ "/../g", "http://a/g" }, { "g.", "http://a/b/c/g." }, { ".g", "http://a/b/c/.g" }, { "g..", "http://a/b/c/g.." },
{ "..g", "http://a/b/c/..g" }, { "./../g", "http://a/b/g" }, { "./g/.", "http://a/b/c/g/" },
{ "g/./h", "http://a/b/c/g/h" }, { "g/../h", "http://a/b/c/h" }, { "g;x=1/./y", "http://a/b/c/g;x=1/y" },
{ "g;x=1/../y", "http://a/b/c/y" }, { "g?y/./x", "http://a/b/c/g?y/./x" }, { "g?y/../x", "http://a/b/c/g?y/../x" },
{ "g#s/./x", "http://a/b/c/g#s/./x" }, { "g#s/../x", "http://a/b/c/g#s/../x" }, { "http:g", "http:g" },
}
for _, c in cases do
eq(Url.resolve(c[1], BASE), c[2], "resolve " .. c[1])
end
-- base with an empty path
eq(Url.resolve("g", "http://a"), "http://a/g", "empty base path")
eq(Url.resolve("g", "http://a?x"), "http://a/g", "empty base path with query")
eq(Url.removeDotSegments("/a/b/c/./../../g"), "/a/g", "remove_dot_segments 1")
eq(Url.removeDotSegments("mid/content=5/../6"), "mid/6", "remove_dot_segments 2")
local p = Url.split("https://user@host:8080/p/a.mp3?x=1&y#frag")
eq(p.scheme, "https", "split scheme")
eq(p.authority, "user@host:8080", "split authority")
eq(p.path, "/p/a.mp3", "split path")
eq(p.query, "x=1&y", "split query")
eq(p.fragment, "frag", "split fragment")
eq(Url.join(p), "https://user@host:8080/p/a.mp3?x=1&y#frag", "join")
local q = Url.split("a/b")
eq(q.scheme, nil, "relative has no scheme")
eq(q.authority, nil, "relative has no authority")
eq(Url.encodeUnsafe("a b/ü%20?x=\"1\"#f"), "a%20b/%C3%BC%20?x=%221%22#f", "encodeUnsafe")
eq(Url.isWindowsPath("C:\\Music\\a.mp3"), true, "windows drive")
eq(Url.isWindowsPath("d:/x.mp3"), true, "windows drive with slash")
eq(Url.isWindowsPath("\\\\server\\share\\a.mp3"), true, "UNC")
eq(Url.isWindowsPath("http://x/a"), false, "http is not windows")
eq(Url.scheme("C:\\a.mp3"), nil, "a drive letter is not a scheme")
eq(Url.scheme("HTTPS://x"), "https", "scheme lower-cased")
eq(Url.scheme("music/a.mp3"), nil, "relative has no scheme")
-- Python's urljoin
for _, c in Ref do
eq(Url.resolve(c[1], c[2]), c[3], "urljoin " .. c[1] .. " against " .. c[2])
end
t:note("%d random references resolve as urllib.parse.urljoin", #Ref)
-- locate / base
do
local w = {}
local base, scheme = Url.base("https://h/a/list.m3u", w)
t:check(base == "https://h/a/list.m3u" and scheme == "https", "base")
local l = Url.locate(" b c.mp3 ", base, scheme)
t:check(l.url == "https://h/a/b%20c.mp3" and l.playable, "locate relative")
t:check(Url.locate("C:\\x.mp3", base, scheme).reason == "local path", "locate windows")
t:check(Url.locate("ftp://x/y", base, scheme).reason == "scheme ftp", "locate ftp")
local nb, ns = Url.base("lists/x.m3u", w)
t:check(nb == nil and ns == nil and #w == 1, "a relative base is ignored with a warning")
t:check(select(2, Url.base("C:\\Music\\l.m3u")) == "windows", "windows base")
end
-- Text
do
for _, c in Cp do
eq(Text.toUtf8("x" .. string.char(c[1]) .. "y"), "x" .. c[2] .. "y", string.format("cp1252 0x%02x", c[1]))
end
local w = {}
Text.toUtf8("\150", w)
t:check(#w == 1, "cp1252 warning")
eq(Text.toUtf8("\239\187\191abc"), "abc", "BOM stripped")
eq(Text.toUtf8("h\u{e9}"), "h\u{e9}", "UTF-8 kept")
local ls = Text.lines("a\r\nb\rc\nd")
t:check(#ls == 4 and ls[1] == "a" and ls[2] == "b" and ls[3] == "c" and ls[4] == "d", "lines")
eq(Text.trim(" x y \t"), "x y", "trim")
end
t:fuzz("resolve + locate", function(b: buffer)
local s = buffer.tostring(b)
Url.resolve(s, "http://a/b/c/d;p?q")
Url.resolve("g/../h", s)
Url.locate(s, "http://h/", "http")
return Text.lines(Text.toUtf8(s))
end, { "http://a/b/../c?x#y", "C:\\a\\b.mp3", "\239\187\191#EXTM3U\r\n" }, { mutations = 30 })
t:done()
+357
View File
@@ -0,0 +1,357 @@
-- playlist/m3u tests (+ the PLS parts of the ported suite): M3U / M3U8 (EXTINF, comments, BOM, CRLF / CR,
-- Windows-1252, relative / absolute / local locations), PLS, HLS master and media playlists, detection, serialize round
-- trips, bad input; Python-written playlists (tools/fixtures/playlist.py) decode to their entries, and our M3U8 output
-- decodes back; the api; the fuzz pass. Ported from playlist-luau tests/playlist.luau. Fixtures:
-- tools/fixtures/playlist.py.
local T = require("@tests/T")
local M3u = require("@formats/playlist/m3u")
local Pls = require("@formats/playlist/pls")
local Bytes = require("@formats/core/Bytes")
local F = require("@formats")
local Cases = require("@tests/gen/playlist_cases")
local t = T.suite("m3u")
local function eq(got: any, want: any, label: string)
t:eq(got, want, label)
end
local function has(list: { string }, pattern: string): boolean
for _, w in list do
if string.find(w, pattern) then
return true
end
end
return false
end
-- the playlist-luau API on top of the two formats (the ported tests below use it)
local Playlist = {}
function Playlist.detect(text: string): string
if type(text) ~= "string" then
return "m3u"
end
local r = Bytes.reader(buffer.fromstring(text))
if Pls.sniff(r) > 0 then
return "pls"
end
return if string.find(text, "#EXT%-X%-") then "hls" else "m3u"
end
function Playlist.parse(text: any, baseUrl: string?): any
if type(text) ~= "string" then
return { kind = "m3u", entries = {}, warnings = { "expected a string" } }
end
local kind = Playlist.detect(text)
local mod = if kind == "pls" then Pls else M3u
local pl = mod.decode(Bytes.reader(buffer.fromstring(text)), { baseUrl = baseUrl }) :: any
pl.kind = pl.format -- the old field
return pl
end
function Playlist.serialize(entries: any, opts: any?): string
return buffer.tostring(M3u.encode(entries, opts) :: buffer)
end
-- M3U8 with EXTINF, comments, blank lines, BOM, CRLF
do
local text = "\239\187\191#EXTM3U\r\n#PLAYLIST:My Mix\r\n\r\n# a comment\r\n#EXTINF:123,Daft Punk - One More Time\r\n"
.. "music/one more time.mp3\r\n#EXTINF:-1,Radio\r\nhttp://radio.example/stream\r\n"
.. "#EXTINF:61.5 tvg-id=\"x\" group-title=\"a,b\",Grouped, with comma\r\n../other/b.mp3\r\n"
.. "#EXTGRP:ignored\r\n/abs/c.mp3\r\n https://cdn.example/d.mp3?x=1 \r\n"
local pl = Playlist.parse(text, "https://host.example/lists/m.m3u8")
eq(pl.kind, "m3u", "kind")
eq(pl.title, "My Mix", "#PLAYLIST")
eq(#pl.entries, 5, "entry count")
local e = pl.entries
eq(e[1].url, "https://host.example/lists/music/one%20more%20time.mp3", "relative resolved + encoded")
eq(e[1].title, "Daft Punk - One More Time", "EXTINF title")
eq(e[1].duration, 123, "EXTINF duration")
eq(e[1].playable, true, "https playable")
eq(e[2].url, "http://radio.example/stream", "absolute kept")
eq(e[2].duration, nil, "duration -1 = unknown")
eq(e[2].title, "Radio", "title 2")
eq(e[3].url, "https://host.example/other/b.mp3", "dot-dot resolved")
eq(e[3].duration, 61.5, "fractional duration with attributes")
eq(e[3].title, "Grouped, with comma", "title after the attributes keeps its commas")
eq(e[4].url, "https://host.example/abs/c.mp3", "absolute path")
eq(e[4].title, nil, "EXTINF does not leak to the next entry")
eq(e[5].url, "https://cdn.example/d.mp3?x=1", "trimmed absolute")
eq(#pl.warnings, 0, "no warnings")
end
-- plain M3U (no header), CR line endings, Windows paths, relative without base, other schemes
do
local text = "C:\\Music\\Song A.mp3\rD:/Other/b.mp3\r\\\\server\\share\\c.mp3\rsongs\\d.mp3\rftp://x/e.mp3\rfile:///home/u/f.mp3"
local pl = Playlist.parse(text)
eq(#pl.entries, 6, "plain count")
eq(pl.entries[1].url, "C:\\Music\\Song A.mp3", "windows path kept as is")
eq(pl.entries[1].playable, false, "windows path unplayable")
eq(pl.entries[1].reason, "local path", "windows path reason")
eq(pl.entries[2].reason, "local path", "drive with slash")
eq(pl.entries[3].reason, "local path", "UNC")
eq(pl.entries[4].url, "songs\\d.mp3", "relative without base kept")
eq(pl.entries[4].reason, "relative path without a base URL", "relative without base reason")
eq(pl.entries[5].reason, "scheme ftp", "ftp unplayable")
eq(pl.entries[6].reason, "local path", "file: is local")
eq(has(pl.warnings, "relative path without a base URL"), true, "warning for relative")
eq(has(pl.warnings, "4 local paths"), true, "local paths summary")
-- with a URL base, Windows separators in relative entries become slashes; drive paths stay local
local pl2 = Playlist.parse(text, "http://h/x/list.m3u")
eq(pl2.entries[4].url, "http://h/x/songs/d.mp3", "backslash relative resolved")
eq(pl2.entries[4].playable, true, "backslash relative playable")
eq(pl2.entries[1].url, "C:\\Music\\Song A.mp3", "windows path untouched with a base")
-- with a Windows base, relative entries are paths next to the playlist
local pl3 = Playlist.parse("a.mp3\nsub\\b.mp3", "C:\\Music\\list.m3u")
eq(pl3.entries[1].url, "C:\\Music\\a.mp3", "windows base")
eq(pl3.entries[2].url, "C:\\Music\\sub\\b.mp3", "windows base sub")
eq(pl3.entries[1].playable, false, "windows base unplayable")
-- file: base
local pl4 = Playlist.parse("a.mp3", "file:///C:/Music/list.m3u")
eq(pl4.entries[1].url, "file:///C:/Music/a.mp3", "file base")
eq(pl4.entries[1].reason, "local path", "file base local")
-- a relative base is not used
local pl5 = Playlist.parse("a.mp3", "lists/x.m3u")
eq(has(pl5.warnings, "not absolute"), true, "relative base warning")
end
-- Windows-1252 text (a classic .m3u), and UTF-8 kept
do
local pl = Playlist.parse("#EXTM3U\n#EXTINF:10,Bj\246rk \150 J\243ga\nhttp://x/j.mp3\n")
eq(pl.entries[1].title, "Björk – Jóga", "cp1252 transcoded")
eq(has(pl.warnings, "Windows%-1252"), true, "cp1252 warning")
local pl2 = Playlist.parse("#EXTINF:10,Björk – Jóga\nhttp://x/j.mp3")
eq(pl2.entries[1].title, "Björk – Jóga", "utf-8 kept")
eq(#pl2.warnings, 0, "utf-8 no warning")
local pl3 = Playlist.parse("http://x/Björk.mp3")
eq(pl3.entries[1].url, "http://x/Bj%C3%B6rk.mp3", "utf-8 url encoded")
end
-- PLS
do
local text = "[playlist]\r\nNumberOfEntries=3\r\nFile2=http://s.example:8000/live\r\nTitle2=(#1) Some Radio\r\nLength2=-1\r\n"
.. "File1=music/a.mp3\r\nTitle1=Artist - Song\r\nLength1=200\r\n; comment\r\nfile10 = http://x/z.mp3\r\nVersion=2\r\n"
local pl = Playlist.parse(text, "http://h/p/list.pls")
eq(pl.kind, "pls", "pls kind")
eq(#pl.entries, 3, "pls count")
eq(pl.entries[1].url, "http://h/p/music/a.mp3", "pls order by index + resolved")
eq(pl.entries[1].title, "Artist - Song", "pls title")
eq(pl.entries[1].duration, 200, "pls length")
eq(pl.entries[2].url, "http://s.example:8000/live", "pls 2")
eq(pl.entries[2].duration, nil, "pls length -1")
eq(pl.entries[3].url, "http://x/z.mp3", "pls key case / spaces / index 10")
eq(pl.entries[3].title, nil, "pls no title")
eq(#pl.warnings, 0, "pls no warnings")
local bad = Playlist.parse("[Playlist]\nNumberOfEntries=2\nFile1=http://a/1.mp3\n")
eq(bad.kind, "pls", "pls case-insensitive header")
eq(has(bad.warnings, "NumberOfEntries=2 but 1"), true, "pls count mismatch warning")
end
-- HLS master playlist: variants with attributes (quoted commas), EXT-X-MEDIA audio renditions
do
local text = [[#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aud",NAME="English",LANGUAGE="en",URI="audio/en.m3u8"
#EXT-X-STREAM-INF:BANDWIDTH=128000,CODECS="mp4a.40.2,avc1.4d401e",RESOLUTION=640x360,AUDIO="aud"
low/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=320000,CODECS="mp4a.40.34"
http://other.example/hi/index.m3u8
]]
local pl = Playlist.parse(text, "https://cdn.example/show/master.m3u8")
eq(pl.kind, "hls", "hls kind")
assert(pl.hls)
eq(pl.hls.master, true, "master")
eq(#pl.entries, 3, "variants + rendition")
eq(pl.entries[1].url, "https://cdn.example/show/audio/en.m3u8", "rendition url")
eq(pl.entries[1].title, "English", "rendition name")
eq(pl.entries[2].url, "https://cdn.example/show/low/index.m3u8", "variant 1 url")
eq(pl.entries[2].bandwidth, 128000, "variant 1 bandwidth")
eq(pl.entries[2].codecs, "mp4a.40.2,avc1.4d401e", "variant codecs keeps its comma")
eq((pl.entries[2].attributes :: any).RESOLUTION, "640x360", "variant attributes")
eq(pl.entries[3].bandwidth, 320000, "variant 2 bandwidth")
eq(pl.entries[3].url, "http://other.example/hi/index.m3u8", "variant 2 absolute")
eq(#pl.warnings, 0, "master no warnings")
end
-- HLS media playlist with mp3 segments (VOD)
do
local text = [[#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:7
#EXTINF:10.0,
seg7.mp3
#EXTINF:9.5,Part two
seg8.mp3?token=a
#EXT-X-BYTERANGE:1000@0
#EXTINF:4,
all.mp3
#EXT-X-BYTERANGE:500
#EXTINF:2,
all.mp3
#EXT-X-ENDLIST
]]
local pl = Playlist.parse(text, "https://h/radio/list.m3u8")
assert(pl.hls)
eq(pl.kind, "hls", "media kind")
eq(pl.hls.master, false, "media not master")
eq(pl.hls.targetDuration, 10, "target duration")
eq(pl.hls.mediaSequence, 7, "media sequence")
eq(pl.hls.endList, true, "endlist")
eq(pl.hls.totalDuration, 25.5, "total duration")
eq(pl.hls.segmentFormat, "mp3", "segment format")
eq(#pl.entries, 4, "segments")
eq(pl.entries[1].url, "https://h/radio/seg7.mp3", "segment url")
eq(pl.entries[1].duration, 10, "segment duration")
eq(pl.entries[1].title, nil, "empty title is nil")
eq(pl.entries[2].title, "Part two", "segment title")
eq(pl.entries[2].url, "https://h/radio/seg8.mp3?token=a", "segment query")
eq(pl.entries[2].playable, true, "mp3 segments playable")
eq((pl.entries[3].byteRange :: any).length, 1000, "byterange length")
eq((pl.entries[3].byteRange :: any).offset, 0, "byterange offset")
eq((pl.entries[4].byteRange :: any).offset, 1000, "byterange continues")
eq(#pl.warnings, 0, "mp3 VOD no warnings")
end
-- HLS media playlists with unsupported segments, live, encrypted, fMP4
do
local ts = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXTINF:6,\na.ts\n#EXTINF:6,\nb.ts\n", "http://h/x.m3u8")
assert(ts.hls)
eq(ts.entries[1].playable, false, "ts unplayable")
eq(ts.entries[1].reason, "segment format ts", "ts reason")
eq(ts.hls.segmentFormat, "ts", "ts format")
eq(has(ts.warnings, "HLS segments are %.ts"), true, "ts warning")
eq(has(ts.warnings, "live HLS"), true, "live warning")
local aac = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXTINF:6,\na.aac\n#EXTINF:6,\nb.mp3\n#EXT-X-ENDLIST\n", "http://h/x.m3u8")
assert(aac.hls)
eq(aac.hls.segmentFormat, "mixed", "mixed format")
eq(has(aac.warnings, "%.aac"), true, "aac warning")
eq(aac.entries[2].playable, true, "mp3 among aac still playable")
local m4s = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXT-X-MAP:URI=\"init.mp4\"\n#EXTINF:4,\n1.m4s\n#EXT-X-ENDLIST\n", "http://h/x.m3u8")
eq(m4s.entries[1].playable, false, "m4s unplayable")
eq(has(m4s.warnings, "%.m4s") and has(m4s.warnings, "fMP4"), true, "m4s + fmp4 warning")
local enc = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXT-X-KEY:METHOD=AES-128,URI=\"k\"\n#EXTINF:4,\n1.mp3\n#EXT-X-ENDLIST\n", "http://h/x.m3u8")
assert(enc.hls)
eq(enc.hls.encrypted, true, "encrypted")
eq(enc.entries[1].playable, false, "encrypted unplayable")
eq(has(enc.warnings, "encrypted"), true, "encrypted warning")
local none = Playlist.parse("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXT-X-KEY:METHOD=NONE\n#EXTINF:4,\n1.mp3\n#EXT-X-ENDLIST\n", "http://h/x.m3u8")
eq(none.entries[1].playable, true, "METHOD=NONE is clear")
end
-- detection
do
eq(Playlist.detect("[playlist]\nFile1=a"), "pls", "detect pls")
eq(Playlist.detect("\239\187\191 [PLAYLIST]\r\n"), "pls", "detect pls with BOM / spaces")
eq(Playlist.detect("#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1\na.m3u8"), "hls", "detect hls master")
eq(Playlist.detect("#EXTM3U\n#EXT-X-TARGETDURATION:5\n"), "hls", "detect hls media")
eq(Playlist.detect("#EXTM3U\n#EXTINF:1,a\na.mp3"), "m3u", "detect m3u")
eq(Playlist.detect("a.mp3"), "m3u", "detect plain")
end
-- serialize: M3U8, round trip
do
local entries = {
{ url = "http://x/a.mp3", title = "A - B", duration = 61.6 },
{ url = "http://x/b.mp3" },
{ url = "http://x/c.mp3", title = "multi\nline" },
{ url = "http://x/d.mp3", duration = 5 },
{ title = "no url" },
}
local text = Playlist.serialize(entries :: any, { title = "Mix" })
eq(text, "#EXTM3U\n#PLAYLIST:Mix\n#EXTINF:62,A - B\nhttp://x/a.mp3\nhttp://x/b.mp3\n#EXTINF:-1,multi line\nhttp://x/c.mp3\n"
.. "#EXTINF:5,\nhttp://x/d.mp3\n", "serialize text")
local back = Playlist.parse(text)
eq(#back.entries, 4, "round trip count")
eq(back.title, "Mix", "round trip title")
eq(back.entries[1].title, "A - B", "round trip title 1")
eq(back.entries[1].duration, 62, "round trip duration")
eq(back.entries[2].title, nil, "round trip no title")
eq(back.entries[3].duration, nil, "round trip unknown duration")
eq(Playlist.serialize({}), "#EXTM3U\n", "serialize empty")
-- parse -> serialize -> parse keeps everything of a real playlist
local pl = Playlist.parse("#EXTM3U\n#EXTINF:100,X - Y\nhttp://h/1.mp3\n#EXTINF:-1,Live\nhttp://h/live\n")
local again = Playlist.parse(Playlist.serialize(pl.entries))
for i, e in pl.entries do
eq(again.entries[i].url, e.url, "rt url " .. i)
eq(again.entries[i].title, e.title, "rt title " .. i)
eq(again.entries[i].duration, e.duration, "rt duration " .. i)
end
end
-- bad input never throws
do
local inputs: { any } = { "", "\n\n\n", "#EXTM3U", "#EXTINF:abc,\n", "[playlist]\n=\nFile=\nFilex1=y", "#EXT-X-STREAM-INF:\n",
"#EXT-X-STREAM-INF:BANDWIDTH=\"unterminated\n", "#EXT-X-BYTERANGE:x\n#EXT-X-TARGETDURATION:\n", "\255\254\0\0", 42, nil }
for i = 1, 11 do
local ok, pl = pcall(Playlist.parse, inputs[i], "http://h/")
eq(ok, true, "bad input " .. i .. " threw " .. tostring(pl))
eq(type(pl.entries), "table", "bad input " .. i .. " entries")
end
eq(#Playlist.parse("").entries, 0, "empty text")
eq(has(Playlist.parse(42 :: any).warnings, "expected a string"), true, "non-string warning")
local ok = pcall(Playlist.serialize, { 1, "x", { url = 5 } } :: any)
eq(ok, true, "serialize skips junk")
-- random bytes
local state = 7
for _ = 1, 300 do
local t = table.create(80)
for k = 1, 80 do
state = (state * 1103515245 + 12345) % 2147483648
t[k] = string.char(({ 10, 13, 35, 44, 58, 61, 34, 92, 47, 46, 65, 120, 200 })[state % 13 + 1])
end
local s = "#EXTM3U\n#EXT-X-" .. table.concat(t)
local ok2, err = pcall(Playlist.parse, s, "http://h/a/")
eq(ok2, true, "fuzz threw " .. tostring(err))
local ok3, err3 = pcall(Playlist.parse, "[playlist]\n" .. table.concat(t), "http://h/a/")
eq(ok3, true, "fuzz pls threw " .. tostring(err3))
end
end
-- Python-written playlists
for _, c in Cases do
local pl = M3u.decode(Bytes.reader(buffer.fromstring(c.m3u))) :: any
eq(#pl.entries, #c.entries, c.name .. ": entries")
for i, e in c.entries do
local g = pl.entries[i]
eq(g.url, e.url, c.name .. ": url " .. i)
eq(g.duration, e.duration, c.name .. ": duration " .. i)
if e.title then
eq(g.title, e.title, c.name .. ": title " .. i)
end
end
local back = M3u.decode(Bytes.reader(M3u.encode(pl, { title = c.title }) :: buffer)) :: any
eq(back.title, c.title, c.name .. ": #PLAYLIST round trip")
for i, e in pl.entries do
eq(back.entries[i].url, e.url, c.name .. ": round trip url " .. i)
eq(back.entries[i].title, e.title, c.name .. ": round trip title " .. i)
end
end
t:note("%d Python-written M3U8 files decode to their entries", #Cases)
-- the contract and the api
do
eq(M3u.name, "m3u", "name")
eq(M3u.category, "playlist", "category")
local c = Cases[1]
eq(F.detect(c.m3u), "m3u", "F.detect")
local pl = F.playlist.load(c.m3u, { baseUrl = "http://x/" })
t:check(pl and pl.kind == "playlist" and pl.format == "m3u" and #pl.entries == #c.entries, "F.playlist.load")
local saved = F.save(pl, nil, { format = "m3u8" })
t:check(saved and string.sub(buffer.tostring(saved), 1, 7) == "#EXTM3U", "F.save m3u8")
local hls = F.playlist.load("#EXTM3U\n#EXT-X-TARGETDURATION:4\n#EXTINF:4,\n1.mp3\n#EXT-X-ENDLIST\n", { baseUrl = "http://h/x.m3u8" })
t:check(hls and hls.format == "hls" and hls.hls.endList, "F.playlist.load HLS")
t:eq(M3u.sniff(Bytes.reader(buffer.fromstring("\239\187\191 #EXTM3U\n"))), 0.95, "sniff with BOM")
t:eq(M3u.sniff(Bytes.reader(buffer.fromstring("a.mp3\n"))), 0, "plain lists are found by extension")
local _, e = M3u.encode(5 :: any)
t:check(e ~= nil, "encode a number")
end
local samples = {}
for _, c in Cases do
table.insert(samples, c.m3u)
end
table.insert(samples, "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=128000,CODECS=\"mp4a.40.2\"\nlo.m3u8\n#EXT-X-MEDIA:TYPE=AUDIO,URI=\"a.m3u8\",NAME=\"x\"\n")
table.insert(samples, "#EXTM3U\n#EXT-X-TARGETDURATION:6\n#EXT-X-BYTERANGE:100@0\n#EXTINF:6,\na.mp3\n#EXT-X-KEY:METHOD=AES-128\n#EXT-X-ENDLIST\n")
t:fuzz("decode", function(b: buffer)
return M3u.decode(Bytes.reader(b), { baseUrl = "http://h/a/" })
end, samples, { mutations = 20 })
t:done()
+69
View File
@@ -0,0 +1,69 @@
-- playlist/pls tests: Python-written PLS files (tools/fixtures/playlist.py) decode to their entries; keys in any case
-- and order, gaps in the numbering, comments, NumberOfEntries mismatches, Windows-1252, relative locations; the
-- writer round-trips (titles, unknown lengths, line breaks); the api; the fuzz pass. (The playlist-luau PLS cases run
-- in tests/playlist/m3u.luau.) Fixtures: tools/fixtures/playlist.py.
local T = require("@tests/T")
local Pls = require("@formats/playlist/pls")
local Bytes = require("@formats/core/Bytes")
local F = require("@formats")
local Cases = require("@tests/gen/playlist_cases")
local t = T.suite("pls")
local function decode(s: string, base: string?): any
return Pls.decode(Bytes.reader(buffer.fromstring(s)), { baseUrl = base })
end
for _, c in Cases do
local pl = decode(c.pls)
t:eq(#pl.entries, #c.entries, c.name .. ": entries")
t:eq(#pl.warnings, 0, c.name .. ": no warnings")
for i, e in c.entries do
local g = pl.entries[i]
t:eq(g.url, e.url, c.name .. ": url " .. i)
t:eq(g.title, e.title, c.name .. ": title " .. i)
t:eq(g.duration, e.duration, c.name .. ": duration " .. i)
t:check(g.playable, c.name .. ": playable " .. i)
end
local back = decode(buffer.tostring(Pls.encode(pl) :: buffer))
t:eq(#back.entries, #pl.entries, c.name .. ": round trip count")
for i, e in pl.entries do
local g = back.entries[i]
t:check(g.url == e.url and g.title == e.title and g.duration == e.duration, c.name .. ": round trip " .. i)
end
end
t:note("%d Python-written PLS files decode to their entries and round-trip", #Cases)
do
local pl = decode("; comment\n[PlayList]\nversion=2\nTITLE2=Second\nfile2=b.mp3\nFile10=http://x/ten.mp3\nLength2=7\nFile1=a.mp3\nnumberofentries=4\n", "http://h/dir/list.pls")
t:eq(#pl.entries, 3, "entries sorted by number")
t:eq(pl.entries[1].url, "http://h/dir/a.mp3", "relative resolved")
t:check(pl.entries[2].title == "Second" and pl.entries[2].duration == 7, "case-insensitive keys")
t:eq(pl.entries[3].url, "http://x/ten.mp3", "gap in numbering")
t:check(string.find(pl.warnings[1], "NumberOfEntries=4", 1, true) ~= nil, "count mismatch warning")
local cp = decode("[playlist]\nFile1=http://x/a.mp3\nTitle1=Bj\246rk\nNumberOfEntries=1\n")
t:eq(cp.entries[1].title, "Bj\u{f6}rk", "Windows-1252 text")
local loc = decode("[playlist]\nFile1=C:\\Music\\a.mp3\nNumberOfEntries=1\n")
t:check(not loc.entries[1].playable and loc.entries[1].reason == "local path", "local path")
-- the writer
local out = buffer.tostring(Pls.encode({ { url = "http://a/1.mp3", title = "One\nTwo", duration = 61.6 }, { url = "http://a/2.mp3" }, { url = 5 } } :: any) :: buffer)
t:eq(out, "[playlist]\nFile1=http://a/1.mp3\nTitle1=One Two\nLength1=62\nFile2=http://a/2.mp3\nLength2=-1\nNumberOfEntries=2\nVersion=2\n", "writer output")
t:check(select(2, Pls.encode(5 :: any)) ~= nil, "encode a number")
t:eq(Pls.sniff(Bytes.reader(buffer.fromstring("\239\187\191 [playlist]\r\nFile1=a"))), 0.95, "sniff")
t:eq(Pls.sniff(Bytes.reader(buffer.fromstring("#EXTM3U"))), 0, "sniff m3u")
-- the api
t:eq(F.detect(Cases[1].pls), "pls", "F.detect")
local a = F.playlist.load(Cases[1].pls)
t:check(a and a.format == "pls", "F.playlist.load")
local m = F.convert(Cases[1].pls, "m3u")
t:check(m and string.sub(buffer.tostring(m), 1, 7) == "#EXTM3U", "F.convert pls -> m3u")
end
local samples = {}
for _, c in Cases do
table.insert(samples, c.pls)
end
t:fuzz("decode", function(b: buffer)
return Pls.decode(Bytes.reader(b), { baseUrl = "http://h/a/" })
end, samples, { mutations = 20 })
t:done()