-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathhelpers_test.go
170 lines (148 loc) · 3.71 KB
/
helpers_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package mp4
import (
"bytes"
"encoding/binary"
"flag"
"os"
"testing"
"github.com/Eyevinn/mp4ff/bits"
"github.com/go-test/deep"
)
// Helpers to tests. By including t.Helper(), the right failing line in the test
// itself is reported.
var (
update = flag.Bool("update", false, "update the golden files of this test")
)
// cmpAfterDecodeEncodeBox compares bytes after a box has been decoded and encoded.
func cmpAfterDecodeEncodeBox(t *testing.T, data []byte) {
t.Helper()
// First SliceReader + SliceWriter
sr := bits.NewFixedSliceReader(data)
box, err := DecodeBoxSR(0, sr)
if err != nil {
t.Error(err)
}
sw := bits.NewFixedSliceWriter(int(box.Size()))
err = box.EncodeSW(sw)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, sw.Bytes()) {
t.Error("non-matching DecodeBoxSR/EncodeSW")
}
bufIn := bytes.NewBuffer(data)
box, err = DecodeBox(0, bufIn)
if err != nil {
t.Error(err)
}
bufOut := bytes.Buffer{}
err = box.Encode(&bufOut)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, bufOut.Bytes()) {
t.Error("non-matchin DecodeBox/Encode")
}
}
// boxDiffAfterEncodeAndDecode compares a box after encoding and decoding using deep.Equal().
// Further check that Info can be run without an error.
func boxDiffAfterEncodeAndDecode(t *testing.T, box Box) {
t.Helper()
// First do encode in a slice via SliceWriter
size := box.Size()
sw := bits.NewFixedSliceWriter(int(size))
err := box.EncodeSW(sw)
if err != nil {
t.Error(err)
}
buf := bytes.NewBuffer(sw.Bytes())
boxDec, err := DecodeBox(0, buf)
if err != nil {
t.Error(err)
}
if diff := deep.Equal(boxDec, box); diff != nil {
t.Error(diff)
}
// Then do encode using io.Writer
midBuf := bytes.Buffer{}
err = box.Encode(&midBuf)
if err != nil {
t.Error(err)
}
// and decode using SliceReader
sr := bits.NewFixedSliceReader(midBuf.Bytes())
boxDec, err = DecodeBoxSR(0, sr)
if err != nil {
t.Error(err)
}
if diff := deep.Equal(boxDec, box); diff != nil {
t.Error(diff)
}
// Finally check that the Info method does not render an error
infoBuf := bytes.Buffer{}
err = box.Info(&infoBuf, "all:1", "", " ")
if err != nil {
t.Error(err)
}
}
func boxAfterEncodeAndDecode(t *testing.T, box Box) Box {
t.Helper()
buf := bytes.Buffer{}
err := box.Encode(&buf)
if err != nil {
t.Error(err)
}
boxDec, err := DecodeBox(0, &buf)
if err != nil {
t.Error(err)
}
return boxDec
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("Got error %s but expected none", err)
}
}
func assertError(t *testing.T, err error, msg string) {
t.Helper()
if err == nil {
t.Error(msg)
}
}
func changeBoxSizeAndAssertError(t *testing.T, data []byte, pos uint64, newSize uint32, errMsg string) {
t.Helper()
raw := make([]byte, len(data))
copy(raw, data)
binary.BigEndian.PutUint32(raw[pos:pos+4], newSize)
assertBoxDecodeError(t, raw, pos, errMsg)
}
func assertBoxDecodeError(t *testing.T, data []byte, pos uint64, errMsg string) {
t.Helper()
_, err := DecodeBox(pos, bytes.NewBuffer(data))
if err == nil || err.Error() != errMsg {
t.Errorf("DecodeBox: Expected error msg: %q", errMsg)
}
_, err = DecodeBoxSR(pos, bits.NewFixedSliceReader(data))
if err == nil || err.Error() != errMsg {
t.Errorf("DecodeBoxSR: Expected error msg: %q", errMsg)
}
}
// writeGolden - write golden file that to be used for later tests
func writeGolden(t *testing.T, goldenAssetPath string, data []byte) error {
t.Helper()
fd, err := os.Create(goldenAssetPath)
if err != nil {
return err
}
_, err = fd.Write(data)
if err != nil {
return err
}
return nil
}
// TestMain is to set flags for tests. In particular, the update flag to update golden files.
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}