54 lines
2.1 KiB
Go
54 lines
2.1 KiB
Go
package media
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestNormalizeManifestIsDeterministic(t *testing.T) {
|
||
|
|
first := []SegmentDeclaration{
|
||
|
|
{Track: "far", SequenceNo: 2, Codec: "wav_pcm_s16le", ContentType: "audio/wav", DurationMS: 5000, ExpectedSize: 1200, ExpectedDigest: strings.Repeat("b", 64)},
|
||
|
|
{Track: "near", SequenceNo: 1, Codec: "wav_pcm_s16le", ContentType: "audio/wav", DurationMS: 5000, ExpectedSize: 2400, ExpectedDigest: strings.Repeat("a", 64)},
|
||
|
|
}
|
||
|
|
second := []SegmentDeclaration{first[1], first[0]}
|
||
|
|
digestA, normalized, total, err := normalizeManifest(first)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
digestB, _, _, err := normalizeManifest(second)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if digestA != digestB {
|
||
|
|
t.Fatalf("manifest digest changed with input order: %s != %s", digestA, digestB)
|
||
|
|
}
|
||
|
|
if normalized[0].Track != "far" || normalized[1].Track != "near" {
|
||
|
|
t.Fatalf("manifest was not sorted: %#v", normalized)
|
||
|
|
}
|
||
|
|
if total != 3600 {
|
||
|
|
t.Fatalf("total = %d, want 3600", total)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNormalizeManifestRejectsInvalidDeclarations(t *testing.T) {
|
||
|
|
base := SegmentDeclaration{Track: "near", SequenceNo: 1, Codec: "wav_pcm_s16le", ContentType: "audio/wav", DurationMS: 5000, ExpectedSize: 1200, ExpectedDigest: strings.Repeat("a", 64)}
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
items []SegmentDeclaration
|
||
|
|
}{
|
||
|
|
{name: "empty"},
|
||
|
|
{name: "duplicate", items: []SegmentDeclaration{base, base}},
|
||
|
|
{name: "track", items: []SegmentDeclaration{func() SegmentDeclaration { item := base; item.Track = "mixed"; return item }()}},
|
||
|
|
{name: "codec content type mismatch", items: []SegmentDeclaration{func() SegmentDeclaration { item := base; item.ContentType = "audio/opus"; return item }()}},
|
||
|
|
{name: "digest", items: []SegmentDeclaration{func() SegmentDeclaration { item := base; item.ExpectedDigest = "no"; return item }()}},
|
||
|
|
{name: "size", items: []SegmentDeclaration{func() SegmentDeclaration { item := base; item.ExpectedSize = 0; return item }()}},
|
||
|
|
}
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
if _, _, _, err := normalizeManifest(tt.items); err == nil {
|
||
|
|
t.Fatal("normalizeManifest accepted invalid input")
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|