-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathbox_test.go
More file actions
114 lines (108 loc) · 3.01 KB
/
box_test.go
File metadata and controls
114 lines (108 loc) · 3.01 KB
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
package mp4_test
import (
"bytes"
"encoding/hex"
"testing"
"github.com/Eyevinn/mp4ff/bits"
"github.com/Eyevinn/mp4ff/mp4"
)
// TestBadBoxAndRemoveBoxDecoder checks that we can avoid decoder error by removing a BoxDecode.
//
// The box is then interpreted as an UnknownBox and its data is not further processed with decoded.
func TestBadBoxAndRemoveBoxDecoder(t *testing.T) {
badMetaBox := (`000000416d6574610000002168646c7300000000000000006d64746100000000` +
`000000000000000000000000106b657973000000000000000000000008696c7374`)
data, err := hex.DecodeString(badMetaBox)
if err != nil {
t.Error(err)
}
sr := bits.NewFixedSliceReader(data)
_, err = mp4.DecodeBoxSR(0, sr)
if err == nil {
t.Errorf("reading bad meta box should have failed")
}
sr = bits.NewFixedSliceReader(data)
mp4.RemoveBoxDecoder("meta")
defer mp4.SetBoxDecoder("meta", mp4.DecodeMeta, mp4.DecodeMetaSR)
box, err := mp4.DecodeBoxSR(0, sr)
if err != nil {
t.Error(err)
}
_, ok := box.(*mp4.MetaBox)
if ok {
t.Errorf("box should not be MetaBox")
}
unknown, ok := box.(*mp4.UnknownBox)
if !ok {
t.Errorf("box should be unknown")
}
if unknown.Type() != "meta" {
t.Errorf("unknown type %q instead of meta", unknown.Type())
}
b := bytes.Buffer{}
err = unknown.Encode(&b)
if err != nil {
t.Error(err)
}
if !bytes.Equal(data, b.Bytes()) {
t.Errorf("written unknown differs")
}
}
// TestDecodeHeader tests DecodeHeader with sufficient and insufficient bytes
func TestDecodeHeader(t *testing.T) {
tests := []struct {
name string
data []byte
wantErr bool
}{
{
name: "7 bytes (one less than boxHeaderSize)",
data: make([]byte, 7),
wantErr: true,
},
{
name: "8 bytes (exactly boxHeaderSize)",
data: []byte{0x00, 0x00, 0x00, 0x10, 't', 'e', 's', 't'}, // size=16, type="test"
wantErr: false,
},
{
name: "extended size with insufficient bytes",
data: []byte{0x00, 0x00, 0x00, 0x01, 't', 'e', 's', 't', 0x00, 0x00, 0x00},
wantErr: true,
},
{
name: "extended size with sufficient bytes for mdat",
data: []byte{0x00, 0x00, 0x00, 0x01, 'm', 'd', 'a', 't', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20},
wantErr: false,
},
{
name: "extended size rejected for non-mdat",
data: []byte{0x00, 0x00, 0x00, 0x01, 't', 'e', 's', 't', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20},
wantErr: true,
},
{
name: "zero size not supported",
data: []byte{0x00, 0x00, 0x00, 0x00, 't', 'e', 's', 't'},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := bytes.NewReader(tt.data)
_, err := mp4.DecodeHeader(r)
if (err != nil) != tt.wantErr {
t.Errorf("DecodeHeader() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestFixed16and32(t *testing.T) {
f16 := mp4.Fixed16(256)
if f16.String() != "1.0" {
t.Errorf("Fixed16(256) should be 1.0, not %s", f16.String())
}
f32 := mp4.Fixed32(65536)
if f16.String() != "1.0" {
t.Errorf("Fixed32(65536) should be 1.0, not %s", f32.String())
}
}