forked from at-wat/ebml-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matroska_official_test.go
120 lines (109 loc) · 2.46 KB
/
matroska_official_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//go:build matroska_official
// +build matroska_official
package ebml
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"
)
const (
testDataBaseURL = "https://raw.githubusercontent.com/Matroska-Org/matroska-test-files/master/test_files/"
cacheDir = "ebml-go-matroska-official-test-data"
)
func loadTestData(t *testing.T, file string) ([]byte, error) {
var r io.ReadCloser
var hasCache bool
cacheFile := filepath.Join(os.TempDir(), cacheDir, file)
if f, err := os.Open(cacheFile); err == nil {
t.Logf("Using cache: %s", cacheFile)
r = f
hasCache = true
} else {
mkvResp, err := http.Get(testDataBaseURL + file)
if err != nil {
return nil, err
}
r = mkvResp.Body
}
b, err := ioutil.ReadAll(r)
if err != nil {
r.Close()
return nil, err
}
r.Close()
if !hasCache {
os.MkdirAll(filepath.Join(os.TempDir(), cacheDir), 0755)
if f, err := os.Create(cacheFile); err == nil {
n, err := f.Write(b)
f.Close()
if err != nil || n != len(b) {
os.Remove(cacheFile)
} else {
t.Logf("Saved cache: %s", cacheFile)
}
}
}
return b, nil
}
func TestMatroskaOfficial(t *testing.T) {
testData := map[string]struct {
filename string
opts []UnmarshalOption
}{
"Basic": {
filename: "test1.mkv",
},
"NonDefaultTimecodeScaleAndAspectRatio": {
filename: "test2.mkv",
},
"HeaderStrippingAndStandardBlock": {
filename: "test3.mkv",
},
"LiveStreamRecording": {
filename: "test4.mkv",
opts: []UnmarshalOption{WithIgnoreUnknown(true)},
},
"MultipleAudioSubtitles": {
filename: "test5.mkv",
},
"DifferentEBMLHeadSizesAndCueLessSeeking": {
filename: "test6.mkv",
},
"ExtraUnknownJunkElementsDamaged": {
filename: "test7.mkv",
opts: []UnmarshalOption{WithIgnoreUnknown(true)},
},
"AudioGap": {
filename: "test8.mkv",
},
}
for name, tt := range testData {
tt := tt
t.Run(name, func(t *testing.T) {
mkvRaw, err := loadTestData(t, tt.filename)
if err != nil {
t.Fatal(err)
}
var dump string
for i := 0; i < 16 && i < len(mkvRaw); i++ {
dump += fmt.Sprintf("%02x ", mkvRaw[i])
}
t.Logf("dump: %s", dump)
var mkv map[string]interface{}
if err := Unmarshal(bytes.NewReader(mkvRaw), &mkv, tt.opts...); err != nil {
t.Fatalf("Failed to unmarshal: '%v'", err)
}
txt := fmt.Sprintf("%+v", mkv)
if len(txt) > 512 {
t.Logf("result: %s...", txt[:512])
} else {
t.Logf("result: %s", txt)
}
})
}
}