1 package exifcommon
2
3 import (
4 "bytes"
5 "math"
6 "reflect"
7 "testing"
8
9 "github.com/dsoprea/go-logging"
10 )
11
12 func TestParser_ParseBytes(t *testing.T) {
13 p := new(Parser)
14
15 encoded := []byte("abcdefg")
16
17 value, err := p.ParseBytes(encoded, 1)
18 log.PanicIf(err)
19
20 if bytes.Equal(value, encoded[:1]) != true {
21 t.Fatalf("Encoding not correct (1): %v", value)
22 }
23
24 value, err = p.ParseBytes(encoded, 4)
25 log.PanicIf(err)
26
27 if bytes.Equal(value, encoded[:4]) != true {
28 t.Fatalf("Encoding not correct (2): %v", value)
29 }
30
31 value, err = p.ParseBytes(encoded, uint32(len(encoded)))
32 log.PanicIf(err)
33
34 if bytes.Equal(value, encoded) != true {
35 t.Fatalf("Encoding not correct (3): %v", value)
36 }
37 }
38
39 func TestParser_ParseAscii(t *testing.T) {
40 p := new(Parser)
41
42 original := "abcdefg"
43
44 encoded := []byte(original[:1])
45 encoded = append(encoded, 0)
46
47 value, err := p.ParseAscii(encoded, uint32(len(encoded)))
48 log.PanicIf(err)
49
50 if value != original[:1] {
51 t.Fatalf("Encoding not correct (1): %s", value)
52 }
53
54 encoded = []byte(original[:4])
55 encoded = append(encoded, 0)
56
57 value, err = p.ParseAscii(encoded, uint32(len(encoded)))
58 log.PanicIf(err)
59
60 if value != original[:4] {
61 t.Fatalf("Encoding not correct (2): %v", value)
62 }
63
64 encoded = []byte(original)
65 encoded = append(encoded, 0)
66
67 value, err = p.ParseAscii(encoded, uint32(len(encoded)))
68 log.PanicIf(err)
69
70 if value != original {
71 t.Fatalf("Encoding not correct (3): %v", value)
72 }
73 }
74
75 func TestParser_ParseAsciiNoNul(t *testing.T) {
76 p := new(Parser)
77
78 original := "abcdefg"
79
80 encoded := []byte(original)
81
82 value, err := p.ParseAsciiNoNul(encoded, 1)
83 log.PanicIf(err)
84
85 if value != original[:1] {
86 t.Fatalf("Encoding not correct (1): %s", value)
87 }
88
89 value, err = p.ParseAsciiNoNul(encoded, 4)
90 log.PanicIf(err)
91
92 if value != original[:4] {
93 t.Fatalf("Encoding not correct (2): %v", value)
94 }
95
96 value, err = p.ParseAsciiNoNul(encoded, uint32(len(encoded)))
97 log.PanicIf(err)
98
99 if value != original {
100 t.Fatalf("Encoding not correct (3): (%d) %v", len(value), value)
101 }
102 }
103
104 func TestParser_ParseShorts__Single(t *testing.T) {
105 p := new(Parser)
106
107 encoded := []byte{0x00, 0x01}
108
109 value, err := p.ParseShorts(encoded, 1, TestDefaultByteOrder)
110 log.PanicIf(err)
111
112 if reflect.DeepEqual(value, []uint16{1}) != true {
113 t.Fatalf("Encoding not correct (1): %v", value)
114 }
115
116 encoded = []byte{0x00, 0x01, 0x00, 0x02}
117
118 value, err = p.ParseShorts(encoded, 1, TestDefaultByteOrder)
119 log.PanicIf(err)
120
121 if reflect.DeepEqual(value, []uint16{1}) != true {
122 t.Fatalf("Encoding not correct (2): %v", value)
123 }
124 }
125
126 func TestParser_ParseShorts__Multiple(t *testing.T) {
127 p := new(Parser)
128
129 encoded := []byte{0x00, 0x01, 0x00, 0x02}
130
131 value, err := p.ParseShorts(encoded, 2, TestDefaultByteOrder)
132 log.PanicIf(err)
133
134 if reflect.DeepEqual(value, []uint16{1, 2}) != true {
135 t.Fatalf("Encoding not correct: %v", value)
136 }
137 }
138
139 func TestParser_ParseLongs__Single(t *testing.T) {
140 p := new(Parser)
141
142 encoded := []byte{0x00, 0x00, 0x00, 0x01}
143
144 value, err := p.ParseLongs(encoded, 1, TestDefaultByteOrder)
145 log.PanicIf(err)
146
147 if reflect.DeepEqual(value, []uint32{1}) != true {
148 t.Fatalf("Encoding not correct (1): %v", value)
149 }
150
151 encoded = []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}
152
153 value, err = p.ParseLongs(encoded, 1, TestDefaultByteOrder)
154 log.PanicIf(err)
155
156 if reflect.DeepEqual(value, []uint32{1}) != true {
157 t.Fatalf("Encoding not correct (2): %v", value)
158 }
159 }
160
161 func TestParser_ParseLongs__Multiple(t *testing.T) {
162 p := new(Parser)
163
164 encoded := []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}
165
166 value, err := p.ParseLongs(encoded, 2, TestDefaultByteOrder)
167 log.PanicIf(err)
168
169 if reflect.DeepEqual(value, []uint32{1, 2}) != true {
170 t.Fatalf("Encoding not correct: %v", value)
171 }
172 }
173
174 func TestParser_ParseFloats__Single(t *testing.T) {
175 p := new(Parser)
176
177 encoded := []byte{0x40, 0x49, 0x0f, 0xdb}
178
179 value, err := p.ParseFloats(encoded, 1, TestDefaultByteOrder)
180 log.PanicIf(err)
181
182 expectedResult := []float32{3.14159265}
183
184 for i, v := range value {
185 if v < expectedResult[i] ||
186 v >= math.Nextafter32(expectedResult[i], expectedResult[i]+1) {
187 t.Fatalf("Encoding not correct (1): %v", value)
188 }
189 }
190 }
191
192 func TestParser_ParseFloats__Multiple(t *testing.T) {
193 p := new(Parser)
194
195 encoded := []byte{0x40, 0x49, 0x0f, 0xdb, 0x40, 0x2d, 0xf8, 0x54}
196
197 value, err := p.ParseFloats(encoded, 2, TestDefaultByteOrder)
198 log.PanicIf(err)
199
200 expectedResult := []float32{3.14159265, 2.71828182}
201
202 for i, v := range value {
203 if v < expectedResult[i] ||
204 v >= math.Nextafter32(expectedResult[i], expectedResult[i]+1) {
205 t.Fatalf("Encoding not correct (1): %v", value)
206 }
207 }
208 }
209
210 func TestParser_ParseDoubles__Single(t *testing.T) {
211 p := new(Parser)
212
213 encoded := []byte{0x40, 0x09, 0x21, 0xfb, 0x53, 0xc8, 0xd4, 0xf1}
214
215 value, err := p.ParseDoubles(encoded, 1, TestDefaultByteOrder)
216 log.PanicIf(err)
217
218 expectedResult := []float64{3.14159265}
219 for i, v := range value {
220 if v < expectedResult[i] ||
221 v >= math.Nextafter(expectedResult[i], expectedResult[i]+1) {
222 t.Fatalf("Encoding not correct (1): %v", value)
223 }
224 }
225 }
226
227 func TestParser_ParseDoubles__Multiple(t *testing.T) {
228 p := new(Parser)
229
230 encoded := []byte{0x40, 0x09, 0x21, 0xfb, 0x53, 0xc8, 0xd4, 0xf1,
231 0x40, 0x05, 0xbf, 0x0a, 0x89, 0xf1, 0xb0, 0xdd}
232
233 value, err := p.ParseDoubles(encoded, 2, TestDefaultByteOrder)
234 log.PanicIf(err)
235
236 expectedResult := []float64{3.14159265, 2.71828182}
237
238 for i, v := range value {
239 if v < expectedResult[i] ||
240 v >= math.Nextafter(expectedResult[i], expectedResult[i]+1) {
241 t.Fatalf("Encoding not correct: %v", value)
242 }
243 }
244 }
245
246 func TestParser_ParseRationals__Single(t *testing.T) {
247 p := new(Parser)
248
249 encoded := []byte{
250 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
251 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
252 }
253
254 value, err := p.ParseRationals(encoded, 1, TestDefaultByteOrder)
255 log.PanicIf(err)
256
257 expected := []Rational{
258 {Numerator: 1, Denominator: 2},
259 }
260
261 if reflect.DeepEqual(value, expected) != true {
262 t.Fatalf("Encoding not correct (1): %v", value)
263 }
264
265 encoded = []byte{
266 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
267 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
268 }
269
270 value, err = p.ParseRationals(encoded, 1, TestDefaultByteOrder)
271 log.PanicIf(err)
272
273 expected = []Rational{
274 {Numerator: 1, Denominator: 2},
275 }
276
277 if reflect.DeepEqual(value, expected) != true {
278 t.Fatalf("Encoding not correct (2): %v", value)
279 }
280 }
281
282 func TestParser_ParseRationals__Multiple(t *testing.T) {
283 p := new(Parser)
284
285 encoded := []byte{
286 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
287 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
288 }
289
290 value, err := p.ParseRationals(encoded, 2, TestDefaultByteOrder)
291 log.PanicIf(err)
292
293 expected := []Rational{
294 {Numerator: 1, Denominator: 2},
295 {Numerator: 3, Denominator: 4},
296 }
297
298 if reflect.DeepEqual(value, expected) != true {
299 t.Fatalf("Encoding not correct (2): %v", value)
300 }
301 }
302
303 func TestParser_ParseSignedLongs__Single(t *testing.T) {
304 p := new(Parser)
305
306 encoded := []byte{0x00, 0x00, 0x00, 0x01}
307
308 value, err := p.ParseSignedLongs(encoded, 1, TestDefaultByteOrder)
309 log.PanicIf(err)
310
311 if reflect.DeepEqual(value, []int32{1}) != true {
312 t.Fatalf("Encoding not correct (1): %v", value)
313 }
314
315 encoded = []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}
316
317 value, err = p.ParseSignedLongs(encoded, 1, TestDefaultByteOrder)
318 log.PanicIf(err)
319
320 if reflect.DeepEqual(value, []int32{1}) != true {
321 t.Fatalf("Encoding not correct (2): %v", value)
322 }
323 }
324
325 func TestParser_ParseSignedLongs__Multiple(t *testing.T) {
326 p := new(Parser)
327
328 encoded := []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02}
329
330 value, err := p.ParseSignedLongs(encoded, 2, TestDefaultByteOrder)
331 log.PanicIf(err)
332
333 if reflect.DeepEqual(value, []int32{1, 2}) != true {
334 t.Fatalf("Encoding not correct: %v", value)
335 }
336 }
337
338 func TestParser_ParseSignedRationals__Single(t *testing.T) {
339 p := new(Parser)
340
341 encoded := []byte{
342 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
343 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
344 }
345
346 value, err := p.ParseSignedRationals(encoded, 1, TestDefaultByteOrder)
347 log.PanicIf(err)
348
349 expected := []SignedRational{
350 {Numerator: 1, Denominator: 2},
351 }
352
353 if reflect.DeepEqual(value, expected) != true {
354 t.Fatalf("Encoding not correct (1): %v", value)
355 }
356
357 encoded = []byte{
358 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
359 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
360 }
361
362 value, err = p.ParseSignedRationals(encoded, 1, TestDefaultByteOrder)
363 log.PanicIf(err)
364
365 expected = []SignedRational{
366 {Numerator: 1, Denominator: 2},
367 }
368
369 if reflect.DeepEqual(value, expected) != true {
370 t.Fatalf("Encoding not correct (2): %v", value)
371 }
372 }
373
374 func TestParser_ParseSignedRationals__Multiple(t *testing.T) {
375 p := new(Parser)
376
377 encoded := []byte{
378 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
379 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
380 }
381
382 value, err := p.ParseSignedRationals(encoded, 2, TestDefaultByteOrder)
383 log.PanicIf(err)
384
385 expected := []SignedRational{
386 {Numerator: 1, Denominator: 2},
387 {Numerator: 3, Denominator: 4},
388 }
389
390 if reflect.DeepEqual(value, expected) != true {
391 t.Fatalf("Encoding not correct (2): %v", value)
392 }
393 }
394
View as plain text