forked from discord/lilliput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv_test.go
219 lines (194 loc) · 5.32 KB
/
opencv_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package lilliput
import (
"bytes"
"testing"
)
func TestAPNG(t *testing.T) {
pngNoMagic := []byte{
0, 0, 0, 0, // size
byte('I'), byte('H'), byte('D'), byte('R'), // type
0, 0, 0, 0, // crc
}
png := append(pngMagic[:], pngNoMagic...)
if detectAPNG(png) {
t.Fatalf(`Incorrectly detected APNG in %v`, png)
}
apngChunks := [][]byte{
{0x61, 0x63, 0x54, 0x4c}, // acTL
{0x66, 0x63, 0x54, 0x4c}, // fcTL
{0x66, 0x64, 0x41, 0x54}, // fdAT
}
for i, chunk := range apngChunks {
apng := append(png, 0, 0, 0, 0) // size
apng = append(apng, chunk...) // type
apng = append(apng, 0, 0, 0, 0) // crc
if !detectAPNG(apng) {
t.Fatalf(`Failed to detect APNG at idx %d in %v`, i, apng)
}
}
}
func TestContentLength_PNG_ExtraData(t *testing.T) {
pngNoMagic := []byte{
0, 0, 0, 0, // size
byte('I'), byte('H'), byte('D'), byte('R'), // type
0, 0, 0, 0, // crc
0, 0, 0, 4, // size
1, 2, 3, 4, // type (not real)
8, 9, 8, 9, // data
0, 0, 0, 0, // crc
0, 0, 0, 0, // size
7, 7, 7, 7, // type
0, 0, 0, 0, // crc
}
png := append(pngMagic[:], pngNoMagic...)
end := detectContentLength(png)
if end != len(png) {
t.Fatalf(`end = "%d", expected "%d"`, end, len(png))
}
png = append(png, 56, 56)
end = detectContentLength(png)
if end != len(png) {
t.Fatalf(`end = "%d", expected "%d"`, end, len(png))
}
}
func TestContentLength_PNG_IEND(t *testing.T) {
pngNoMagic := []byte{
0, 0, 0, 0, // size
byte('I'), byte('H'), byte('D'), byte('R'), // type
0, 0, 0, 0, // crc
0, 0, 0, 0, // size
byte('I'), byte('E'), byte('N'), byte('D'), // type
0, 0, 0, 0, // crc
}
pngExtraData := []byte{
0, 0, 0, 4, // size
1, 2, 3, 4, // type
8, 9, 8, 9, // data
0, 0, 0, 0, // crc
}
png := append(pngMagic[:], pngNoMagic...)
expectedLength := len(png)
png = append(png, pngExtraData...)
end := detectContentLength(png)
if end != expectedLength {
t.Fatalf(`end = "%d", expected "%d"`, end, expectedLength)
}
}
func TestContentLength_JPEG_ExtraData(t *testing.T) {
jpeg := []byte{
0xFF, 0xD8, // SOI
0xFF, 0xE7, 0x00, 0x04, 0xFF, 0xD9, // made up segment
0xFF, 0xDA, 0x00, 0x04, 0x00, 0x00, // SOS
0x00, 0x01, 0xD9, 0xFF, 0xD5, 0xD5, // ECS data
0xFF, 0xD9, // EOI
}
result := detectContentLength(jpeg)
if result != len(jpeg) {
t.Fatalf(`Expected jpeg content length %d, got %d`, len(jpeg), result)
}
extraStuff := []byte{0xFF, 0xC2, 0x00, 0x02}
jpeg = append(jpeg, extraStuff...)
result = detectContentLength(jpeg)
if result != len(jpeg)-len(extraStuff) {
t.Fatalf(`Expected jpeg content length %d, got %d`, len(jpeg)-len(extraStuff), result)
}
}
func TestContentLength_JPEG_EntropyCoding(t *testing.T) {
jpeg := []byte{
0xFF, 0xD8, // SOI
0xFF, 0xE7, 0x00, 0x04, 0xFF, 0xD9, // made up data
0xFF, 0xDA, 0x00, 0x02, // SOS
0x02, 0x01, 0xFF, 0x00, 0xD9, // ECS data
0xFF, 0xFF, // padding
0xFF, 0xD9, // EOI
0x01, // extra
}
result := detectContentLength(jpeg)
if result != len(jpeg)-1 {
t.Fatalf(`Expected jpeg content length %d, got %d`, len(jpeg)-1, result)
}
}
func TestContentLength_Unrecognized(t *testing.T) {
data := make([]byte, 128)
result := detectContentLength(data)
if result != len(data) {
t.Fatalf(`Expected data content length %d, got %d`, len(data), result)
}
}
func expectChunks(t *testing.T, png []byte, chunks [][]byte) {
chunkIter, err := makePngChunkIter(png)
if err != nil {
t.Fatalf(`makePngChunkIter failed with error %v`, err)
}
chunkIdx := 0
for chunkIter.next() {
if chunkIdx >= len(chunks) {
t.Fatalf(`Found %d chunks, expected only %d`, chunkIdx+1, len(chunks))
}
if !bytes.Equal(chunkIter.chunkType(), chunks[chunkIdx]) {
t.Fatalf(`chunkType = "%v", expected "%v"`, chunkIter.chunkType(), chunks[chunkIdx])
}
chunkIdx++
}
if chunkIdx < len(chunks) {
t.Fatalf(`Found %d chunks, expected %d`, chunkIdx, len(chunks))
}
}
func TestPNGWalk_ExtraData(t *testing.T) {
pngNoMagic := []byte{
0, 0, 0, 0, // size
byte('I'), byte('H'), byte('D'), byte('R'), // type
0, 0, 0, 0, // crc
0, 0, 0, 4, // size
1, 2, 3, 4, // type (not real)
8, 9, 8, 9, // data
0, 0, 0, 0, // crc
}
png := append(pngMagic[:], pngNoMagic...)
chunkTypes := [][]byte{
{byte('I'), byte('H'), byte('D'), byte('R')},
{1, 2, 3, 4},
}
// min chunk size is 12, try extra data up to that amount
for i := 0; i < 11; i++ {
png = append(png, 0)
expectChunks(t, png, chunkTypes)
}
}
func TestPNGWalk_BadSize(t *testing.T) {
pngNoMagic := []byte{
0, 0, 0, 0, // size
byte('I'), byte('H'), byte('D'), byte('R'), // type
0, 0, 0, 0, // crc
0, 128, 0, 4, // size (massive)
1, 2, 3, 4, // type (not real)
8, 9, 8, 9, // data
0, 0, 0, 0, // crc
}
png := append(pngMagic[:], pngNoMagic...)
chunkTypes := [][]byte{
{byte('I'), byte('H'), byte('D'), byte('R')},
{1, 2, 3, 4},
}
expectChunks(t, png, chunkTypes)
}
func TestPNGWalk_NotPNG(t *testing.T) {
pngNoMagic := []byte{
0, 0, 0, 0, // size
byte('I'), byte('H'), byte('D'), byte('R'), // type
0, 0, 0, 0, // crc
}
_, err := makePngChunkIter(pngNoMagic)
if err == nil {
t.Fatalf(`Expected makePngChunkIter to fail, but it did not`)
}
}
func TestPNGWalk_NoChunks(t *testing.T) {
pngNoMagic := []byte{}
png := append(pngMagic[:], pngNoMagic...)
// min chunk size is 12, try extra data up to that amount
for i := 0; i < 12; i++ {
expectChunks(t, png, [][]byte{})
png = append(png, 0)
}
}