commit a2109c70b8c1180b4a6b9400e503e37ff4abdda8 from: Oliver Lowe date: Sat May 3 03:22:56 2025 UTC m3u8: test more bad resolutions We don't necessarily need to make it easier to specify Variant test cases, we can pick the already easy-to-test bits of Variant to make better. References: https://github.com/untangledco/streaming/issues/34 commit - 18185121cc63011d9f097754c13cdc4cc11fd72b commit + a2109c70b8c1180b4a6b9400e503e37ff4abdda8 blob - 20dcbafb6a363bd6b7450c989e8750e388189460 blob + 0117911d561fd529fde1dada7019c2b885925615 --- m3u8/parse.go +++ m3u8/parse.go @@ -134,9 +134,6 @@ func parseVariant(items chan item) (*Variant, error) { v.Codecs = strings.Split(strings.Trim(it.val, `"`), ",") case "RESOLUTION": it = <-items - if it.typ != itemString { - return nil, fmt.Errorf("parse resolution attribute: unexpected %s", it) - } res, err := parseResolution(it.val) if err != nil { return nil, fmt.Errorf("parse resolution: %w", err) @@ -179,7 +176,7 @@ func parseVariant(items chan item) (*Variant, error) { case "CLOSED-CAPTIONS": it = <-items if it.typ != itemString { - return nil, fmt.Errorf("parse closed-captions: unexpcted %s", it) + return nil, fmt.Errorf("parse closed-captions: unexpected %s", it) } v.ClosedCaptions = strings.Trim(it.val, `"`) default: @@ -192,7 +189,6 @@ func parseVariant(items chan item) (*Variant, error) { return &v, nil } } - fmt.Println(v) return &v, nil } @@ -209,6 +205,9 @@ func parseResolution(s string) (res [2]int, err error) if err != nil { return res, fmt.Errorf("vertical pixels: %v", err) } + if res[0] < 0 || res[1] < 0 { + return res, fmt.Errorf("negative dimensions") + } return res, nil } blob - 8614b5d3dff6b87a4b67ccf09b7e89a9543f3348 blob + 96e924816a03c74379ff972dad1afc1f44551989 --- m3u8/parse_test.go +++ m3u8/parse_test.go @@ -150,3 +150,24 @@ func TestParseSequence(t *testing.T) { t.Errorf("want %d, got %d", 91240, plist.Sequence) } } + +func TestResolution(t *testing.T) { + var tests = []struct { + name string + s string + }{ + {"double x", "1280xx720"}, + {"missing x", "1280720"}, + {"missing height", "1280x"}, + {"negative", "-1x-1"}, + {"decimal", "10.69x20"}, + } + + for _, tt := range tests { + _, err := parseResolution(tt.s) + if err == nil { + t.Errorf("parse resolution %q (%s): no error", tt.s, tt.name) + continue + } + } +}