-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif_test.go
157 lines (151 loc) · 3.62 KB
/
exif_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
package imaging
import (
"os"
"strings"
"testing"
)
func TestReadOrientation(t *testing.T) {
t.Parallel()
testCases := []struct {
path string
orient Orientation
}{
{"testdata/orientation_0.jpg", 0},
{"testdata/orientation_1.jpg", 1},
{"testdata/orientation_2.jpg", 2},
{"testdata/orientation_3.jpg", 3},
{"testdata/orientation_4.jpg", 4},
{"testdata/orientation_5.jpg", 5},
{"testdata/orientation_6.jpg", 6},
{"testdata/orientation_7.jpg", 7},
{"testdata/orientation_8.jpg", 8},
}
for _, tc := range testCases {
tc := tc
f, err := os.Open(tc.path)
if err != nil {
t.Fatalf("%q: failed to open: %v", tc.path, err)
}
orient := ReadOrientation(f)
if orient != tc.orient {
t.Fatalf("%q: got orientation %d want %d", tc.path, orient, tc.orient)
}
}
}
func TestReadOrientationFails(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
data string
}{
{
"empty",
"",
},
{
"missing SOI marker",
"\xff\xe1",
},
{
"missing APP1 marker",
"\xff\xd8",
},
{
"short read marker",
"\xff\xd8\xff",
},
{
"short read block size",
"\xff\xd8\xff\xe1\x00",
},
{
"invalid marker",
"\xff\xd8\x00\xe1\x00\x00",
},
{
"block size too small",
"\xff\xd8\xff\xe0\x00\x01",
},
{
"short read block",
"\xff\xd8\xff\xe0\x00\x08\x00",
},
{
"missing EXIF header",
"\xff\xd8\xff\xe1\x00\xff",
},
{
"invalid EXIF header",
"\xff\xd8\xff\xe1\x00\xff\x00\x00\x00\x00",
},
{
"missing EXIF header tail",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66",
},
{
"missing byte order tag",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00",
},
{
"invalid byte order tag",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x00\x00",
},
{
"missing byte order tail",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x49\x49",
},
{
"missing exif offset",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x49\x49\x00\x2a",
},
{
"invalid exif offset",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x07",
},
{
"read exif offset error",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x09",
},
{
"missing number of tags",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08",
},
{
"zero number of tags",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x00",
},
{
"missing tag",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x01",
},
{
"missing tag offset",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x01\x00\x00",
},
{
"missing orientation tag",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
},
{
"missing orientation tag value offset",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x01\x01\x12",
},
{
"missing orientation value",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x01\x01\x12\x00\x03\x00\x00\x00\x01",
},
{
"invalid orientation value",
"\xff\xd8\xff\xe1\x00\xff\x45\x78\x69\x66\x00\x00\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00\x01\x01\x12\x00\x03\x00\x00\x00\x01\x00\x09",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if o := ReadOrientation(strings.NewReader(tc.data)); o != OrientationUnspecified {
t.Fatalf("got orientation %d want %d", o, OrientationUnspecified)
}
})
}
}