Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for playlist version check #32

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions m3u8/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,9 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, state *decodingState, line stri
case strings.HasPrefix(line, "#EXT-X-KEY:"):
state.listType = MEDIA
state.xkey = parseKeyParams(line[11:])
state.versionCheck = ValidIVInEXTXKEY{
ActualVersion: p.ver,
IV: state.xkey.IV}
state.tagKey = true
case strings.HasPrefix(line, "#EXT-X-MAP:"):
state.listType = MEDIA
Expand Down Expand Up @@ -1025,6 +1028,13 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, state *decodingState, line stri
val := strings.TrimPrefix(line, "#EXT-X-ALLOW-CACHE:") == "YES"
p.AllowCache = &val
}

if strict {
if valid, err := state.versionCheck.Validate(); !valid {
return err
}
}

return err
}

Expand Down
1 change: 1 addition & 0 deletions m3u8/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ type decodingState struct {
scte *SCTE
scte35DateRanges []*DateRange
custom CustomMap
versionCheck VersionMatchingRule
}

// DateRange corresponds to EXT-X-DATERANGE tag.
Expand Down
41 changes: 41 additions & 0 deletions m3u8/version_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package m3u8

import "fmt"

type VersionMismatchError struct {
ActualVersion uint8
ExpectedVersion uint8
Description string
}

// Error implements error.
func (m VersionMismatchError) Error() string {
return fmt.Sprintf("Playlist version mismatch %s. Actual version is %d, expected version is %d", m.Description, m.ActualVersion, m.ExpectedVersion)

Check failure on line 13 in m3u8/version_check.go

View workflow job for this annotation

GitHub Actions / lint

The line is 151 characters long, which exceeds the maximum of 120 characters. (lll)
}

type VersionMatchingRule interface {
Validate() (bool, VersionMismatchError)
}

type DefaultMatchingRule struct{}

func (d *DefaultMatchingRule) Validate() (bool, VersionMismatchError) {
return true, VersionMismatchError{}
}

type ValidIVInEXTXKEY struct {
ActualVersion uint8
IV string
}

func (v ValidIVInEXTXKEY) Validate() (bool, VersionMismatchError) {
if v.IV == "" || v.ActualVersion >= 2 {
return true, VersionMismatchError{}
}

return false, VersionMismatchError{
ActualVersion: v.ActualVersion,
ExpectedVersion: 2,
Description: "Protocol version needs to be at least 2 if you have IV in EXT-X-KEY.",
}
}
68 changes: 68 additions & 0 deletions m3u8/version_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package m3u8

import (
"testing"
)

func TestDefaultMatchingRule_Validate(t *testing.T) {
rule := DefaultMatchingRule{}
valid, err := rule.Validate()
if !valid {
t.Errorf("expected true, got false")
}
if err != (VersionMismatchError{}) {
t.Errorf("expected empty error, got %v", err)
}
}

func TestValidIVInEXTXKEY(t *testing.T) {
tests := []struct {
name string
actualVersion uint8
iv string
expectedValid bool
expectedError VersionMismatchError
}{
{
name: "Valid case with empty IV",
actualVersion: 1,
iv: "",
expectedValid: true,
expectedError: VersionMismatchError{},
},
{
name: "Invalid case with non-empty IV",
actualVersion: 1,
iv: "someIV",
expectedValid: false,
expectedError: VersionMismatchError{
ActualVersion: 1,
ExpectedVersion: 2,
Description: "Protocol version needs to be at least 2 if you have IV in EXT-X-KEY.",
},
},
{
name: "Valid case with non-empty IV",
actualVersion: 2,
iv: "someIV",
expectedValid: true,
expectedError: VersionMismatchError{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rule := ValidIVInEXTXKEY{
ActualVersion: tt.actualVersion,
IV: tt.iv,
}
valid, err := rule.Validate()
if valid != tt.expectedValid {
t.Errorf("expected %v, got %v", tt.expectedValid, valid)
}
if err != tt.expectedError {
t.Errorf("expected %v, got %v", tt.expectedError, err)
}
})
}
}
Loading