| 1 |
package exif |
| 2 |
|
| 3 |
//go:generate go run regen_regress.go -- regress_expected_test.go |
| 4 |
//go:generate go fmt regress_expected_test.go |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"flag" |
| 9 |
"fmt" |
| 10 |
"io/ioutil" |
| 11 |
"math" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/rwcarlsen/goexif/tiff" |
| 18 |
) |
| 19 |
|
| 20 |
var dataDir = flag.String("test_data_dir", ".", "Directory where the data files for testing are located") |
| 21 |
|
| 22 |
func TestDecode(t *testing.T) { |
| 23 |
fpath := filepath.Join(*dataDir, "samples") |
| 24 |
f, err := os.Open(fpath) |
| 25 |
if err != nil { |
| 26 |
t.Fatalf("Could not open sample directory '%s': %v", fpath, err) |
| 27 |
} |
| 28 |
|
| 29 |
names, err := f.Readdirnames(0) |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("Could not read sample directory '%s': %v", fpath, err) |
| 32 |
} |
| 33 |
|
| 34 |
cnt := 0 |
| 35 |
for _, name := range names { |
| 36 |
if !strings.HasSuffix(name, ".jpg") { |
| 37 |
t.Logf("skipping non .jpg file %v", name) |
| 38 |
continue |
| 39 |
} |
| 40 |
t.Logf("testing file %v", name) |
| 41 |
f, err := os.Open(filepath.Join(fpath, name)) |
| 42 |
if err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
|
| 46 |
x, err := Decode(f) |
| 47 |
if err != nil { |
| 48 |
t.Fatal(err) |
| 49 |
} else if x == nil { |
| 50 |
t.Fatalf("No error and yet %v was not decoded", name) |
| 51 |
} |
| 52 |
|
| 53 |
t.Logf("checking pic %v", name) |
| 54 |
x.Walk(&walker{name, t}) |
| 55 |
cnt++ |
| 56 |
} |
| 57 |
if cnt != len(regressExpected) { |
| 58 |
t.Errorf("Did not process enough samples, got %d, want %d", cnt, len(regressExpected)) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestDecodeRawEXIF(t *testing.T) { |
| 63 |
rawFile := filepath.Join(*dataDir, "samples", "raw.exif") |
| 64 |
raw, err := ioutil.ReadFile(rawFile) |
| 65 |
if err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
x, err := Decode(bytes.NewReader(raw)) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("Decode: %v", err) |
| 71 |
} |
| 72 |
got := map[string]string{} |
| 73 |
err = x.Walk(walkFunc(func(name FieldName, tag *tiff.Tag) error { |
| 74 |
got[fmt.Sprint(name)] = fmt.Sprint(tag) |
| 75 |
return nil |
| 76 |
})) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("Walk: %v", err) |
| 79 |
} |
| 80 |
// The main part of this test is that Decode can parse an |
| 81 |
// "Exif\x00\x00" body, but also briefly sanity check the |
| 82 |
// parsed results: |
| 83 |
if len(got) < 42 { |
| 84 |
t.Errorf("Got %d tags. Want at least 42. Got: %v", len(got), err) |
| 85 |
} |
| 86 |
if g, w := got["DateTime"], `"2018:04:03 07:54:33"`; g != w { |
| 87 |
t.Errorf("DateTime value = %q; want %q", g, w) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
type walkFunc func(FieldName, *tiff.Tag) error |
| 92 |
|
| 93 |
func (f walkFunc) Walk(name FieldName, tag *tiff.Tag) error { |
| 94 |
return f(name, tag) |
| 95 |
} |
| 96 |
|
| 97 |
type walker struct { |
| 98 |
picName string |
| 99 |
t *testing.T |
| 100 |
} |
| 101 |
|
| 102 |
func (w *walker) Walk(field FieldName, tag *tiff.Tag) error { |
| 103 |
// this needs to be commented out when regenerating regress expected vals |
| 104 |
pic := regressExpected[w.picName] |
| 105 |
if pic == nil { |
| 106 |
w.t.Errorf(" regression data not found") |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
exp, ok := pic[field] |
| 111 |
if !ok { |
| 112 |
w.t.Errorf(" regression data does not have field %v", field) |
| 113 |
return nil |
| 114 |
} |
| 115 |
|
| 116 |
s := tag.String() |
| 117 |
if tag.Count == 1 && s != "\"\"" { |
| 118 |
s = fmt.Sprintf("[%s]", s) |
| 119 |
} |
| 120 |
got := tag.String() |
| 121 |
|
| 122 |
if exp != got { |
| 123 |
fmt.Println("s: ", s) |
| 124 |
fmt.Printf("len(s)=%v\n", len(s)) |
| 125 |
w.t.Errorf(" field %v bad tag: expected '%s', got '%s'", field, exp, got) |
| 126 |
} |
| 127 |
return nil |
| 128 |
} |
| 129 |
|
| 130 |
func TestMarshal(t *testing.T) { |
| 131 |
name := filepath.Join(*dataDir, "sample1.jpg") |
| 132 |
f, err := os.Open(name) |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("%v\n", err) |
| 135 |
} |
| 136 |
defer f.Close() |
| 137 |
|
| 138 |
x, err := Decode(f) |
| 139 |
if err != nil { |
| 140 |
t.Fatal(err) |
| 141 |
} |
| 142 |
if x == nil { |
| 143 |
t.Fatal("bad err") |
| 144 |
} |
| 145 |
|
| 146 |
b, err := x.MarshalJSON() |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
|
| 151 |
t.Logf("%s", b) |
| 152 |
} |
| 153 |
|
| 154 |
func testSingleParseDegreesString(t *testing.T, s string, w float64) { |
| 155 |
g, err := parseTagDegreesString(s) |
| 156 |
if err != nil { |
| 157 |
t.Fatal(err) |
| 158 |
} |
| 159 |
if math.Abs(w-g) > 1e-10 { |
| 160 |
t.Errorf("Wrong parsing result %s: Want %.12f, got %.12f", s, w, g) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestParseTagDegreesString(t *testing.T) { |
| 165 |
// semicolon as decimal mark |
| 166 |
testSingleParseDegreesString(t, "52,00000,50,00000,34,01180", 52.842781055556) // comma as separator |
| 167 |
testSingleParseDegreesString(t, "52,00000;50,00000;34,01180", 52.842781055556) // semicolon as separator |
| 168 |
|
| 169 |
// point as decimal mark |
| 170 |
testSingleParseDegreesString(t, "14.00000,44.00000,34.01180", 14.742781055556) // comma as separator |
| 171 |
testSingleParseDegreesString(t, "14.00000;44.00000;34.01180", 14.742781055556) // semicolon as separator |
| 172 |
testSingleParseDegreesString(t, "14.00000;44.00000,34.01180", 14.742781055556) // mixed separators |
| 173 |
|
| 174 |
testSingleParseDegreesString(t, "-008.0,30.0,03.6", -8.501) // leading zeros |
| 175 |
|
| 176 |
// no decimal places |
| 177 |
testSingleParseDegreesString(t, "-10,15,54", -10.265) |
| 178 |
testSingleParseDegreesString(t, "-10;15;54", -10.265) |
| 179 |
|
| 180 |
// incorrect mix of comma and point as decimal mark |
| 181 |
s := "-17,00000,15.00000,04.80000" |
| 182 |
if _, err := parseTagDegreesString(s); err == nil { |
| 183 |
t.Error("parseTagDegreesString: false positive for " + s) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// Make sure we error out early when a tag had a count of MaxUint32 |
| 188 |
func TestMaxUint32CountError(t *testing.T) { |
| 189 |
name := filepath.Join(*dataDir, "corrupt/max_uint32_exif.jpg") |
| 190 |
f, err := os.Open(name) |
| 191 |
if err != nil { |
| 192 |
t.Fatalf("%v\n", err) |
| 193 |
} |
| 194 |
defer f.Close() |
| 195 |
|
| 196 |
_, err = Decode(f) |
| 197 |
if err == nil { |
| 198 |
t.Fatal("no error on bad exif data") |
| 199 |
} |
| 200 |
if !strings.Contains(err.Error(), "invalid Count offset") { |
| 201 |
t.Fatal("wrong error:", err.Error()) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// Make sure we error out early with tag data sizes larger than the image file |
| 206 |
func TestHugeTagError(t *testing.T) { |
| 207 |
name := filepath.Join(*dataDir, "corrupt/huge_tag_exif.jpg") |
| 208 |
f, err := os.Open(name) |
| 209 |
if err != nil { |
| 210 |
t.Fatalf("%v\n", err) |
| 211 |
} |
| 212 |
defer f.Close() |
| 213 |
|
| 214 |
_, err = Decode(f) |
| 215 |
if err == nil { |
| 216 |
t.Fatal("no error on bad exif data") |
| 217 |
} |
| 218 |
if !strings.Contains(err.Error(), "short read") { |
| 219 |
t.Fatal("wrong error:", err.Error()) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// Check for a 0-length tag value |
| 224 |
func TestZeroLengthTagError(t *testing.T) { |
| 225 |
name := filepath.Join(*dataDir, "corrupt/infinite_loop_exif.jpg") |
| 226 |
f, err := os.Open(name) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("%v\n", err) |
| 229 |
} |
| 230 |
defer f.Close() |
| 231 |
|
| 232 |
_, err = Decode(f) |
| 233 |
if err == nil { |
| 234 |
t.Fatal("no error on bad exif data") |
| 235 |
} |
| 236 |
if !strings.Contains(err.Error(), "zero length tag value") { |
| 237 |
t.Fatal("wrong error:", err.Error()) |
| 238 |
} |
| 239 |
} |