| 1 |
package tiff |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"encoding/hex" |
| 7 |
"flag" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
var dataDir = flag.String("test_data_dir", ".", "Directory where the data files for testing are located") |
| 14 |
|
| 15 |
type input struct { |
| 16 |
tgId string |
| 17 |
tpe string |
| 18 |
nVals string |
| 19 |
offset string |
| 20 |
val string |
| 21 |
} |
| 22 |
|
| 23 |
type output struct { |
| 24 |
id uint16 |
| 25 |
typ DataType |
| 26 |
count uint32 |
| 27 |
val []byte |
| 28 |
} |
| 29 |
|
| 30 |
type tagTest struct { |
| 31 |
big input // big endian |
| 32 |
little input // little endian |
| 33 |
out output |
| 34 |
} |
| 35 |
|
| 36 |
/////////////////////////////////////////////// |
| 37 |
//// Big endian Tests ///////////////////////// |
| 38 |
/////////////////////////////////////////////// |
| 39 |
var set1 = []tagTest{ |
| 40 |
//////////// string type ////////////// |
| 41 |
tagTest{ |
| 42 |
// {"TgId", "TYPE", "N-VALUES", "OFFSET--", "VAL..."}, |
| 43 |
input{"0003", "0002", "00000002", "11000000", ""}, |
| 44 |
input{"0300", "0200", "02000000", "11000000", ""}, |
| 45 |
output{0x0003, DataType(0x0002), 0x0002, []byte{0x11, 0x00}}, |
| 46 |
}, |
| 47 |
tagTest{ |
| 48 |
input{"0001", "0002", "00000006", "00000012", "111213141516"}, |
| 49 |
input{"0100", "0200", "06000000", "12000000", "111213141516"}, |
| 50 |
output{0x0001, DataType(0x0002), 0x0006, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16}}, |
| 51 |
}, |
| 52 |
// broken Count, because it includes all trailing NULL bytes, as is the case in |
| 53 |
// https://user-images.githubusercontent.com/2621/39392188-71a66fc4-4a66-11e8-9e3b-694163efa643.jpg |
| 54 |
tagTest{ |
| 55 |
input{"0001", "0002", "00000009", "00000012", "111213141516000000"}, |
| 56 |
input{"0100", "0200", "09000000", "12000000", "111213141516000000"}, |
| 57 |
output{0x0001, DataType(0x0002), 0x0009, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16}}, |
| 58 |
}, |
| 59 |
//////////// int (1-byte) type //////////////// |
| 60 |
tagTest{ |
| 61 |
input{"0001", "0001", "00000001", "11000000", ""}, |
| 62 |
input{"0100", "0100", "01000000", "11000000", ""}, |
| 63 |
output{0x0001, DataType(0x0001), 0x0001, []byte{0x11}}, |
| 64 |
}, |
| 65 |
tagTest{ |
| 66 |
input{"0001", "0001", "00000005", "00000010", "1112131415"}, |
| 67 |
input{"0100", "0100", "05000000", "10000000", "1112131415"}, |
| 68 |
output{0x0001, DataType(0x0001), 0x0005, []byte{0x11, 0x12, 0x13, 0x14, 0x15}}, |
| 69 |
}, |
| 70 |
tagTest{ |
| 71 |
input{"0001", "0006", "00000001", "11000000", ""}, |
| 72 |
input{"0100", "0600", "01000000", "11000000", ""}, |
| 73 |
output{0x0001, DataType(0x0006), 0x0001, []byte{0x11}}, |
| 74 |
}, |
| 75 |
tagTest{ |
| 76 |
input{"0001", "0006", "00000005", "00000010", "1112131415"}, |
| 77 |
input{"0100", "0600", "05000000", "10000000", "1112131415"}, |
| 78 |
output{0x0001, DataType(0x0006), 0x0005, []byte{0x11, 0x12, 0x13, 0x14, 0x15}}, |
| 79 |
}, |
| 80 |
//////////// int (2-byte) types //////////////// |
| 81 |
tagTest{ |
| 82 |
input{"0001", "0003", "00000002", "11111212", ""}, |
| 83 |
input{"0100", "0300", "02000000", "11111212", ""}, |
| 84 |
output{0x0001, DataType(0x0003), 0x0002, []byte{0x11, 0x11, 0x12, 0x12}}, |
| 85 |
}, |
| 86 |
tagTest{ |
| 87 |
input{"0001", "0003", "00000003", "00000010", "111213141516"}, |
| 88 |
input{"0100", "0300", "03000000", "10000000", "111213141516"}, |
| 89 |
output{0x0001, DataType(0x0003), 0x0003, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16}}, |
| 90 |
}, |
| 91 |
tagTest{ |
| 92 |
input{"0001", "0008", "00000001", "11120000", ""}, |
| 93 |
input{"0100", "0800", "01000000", "11120000", ""}, |
| 94 |
output{0x0001, DataType(0x0008), 0x0001, []byte{0x11, 0x12}}, |
| 95 |
}, |
| 96 |
tagTest{ |
| 97 |
input{"0001", "0008", "00000003", "00000100", "111213141516"}, |
| 98 |
input{"0100", "0800", "03000000", "00100000", "111213141516"}, |
| 99 |
output{0x0001, DataType(0x0008), 0x0003, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16}}, |
| 100 |
}, |
| 101 |
//////////// int (4-byte) types //////////////// |
| 102 |
tagTest{ |
| 103 |
input{"0001", "0004", "00000001", "11121314", ""}, |
| 104 |
input{"0100", "0400", "01000000", "11121314", ""}, |
| 105 |
output{0x0001, DataType(0x0004), 0x0001, []byte{0x11, 0x12, 0x13, 0x14}}, |
| 106 |
}, |
| 107 |
tagTest{ |
| 108 |
input{"0001", "0004", "00000002", "00000010", "1112131415161718"}, |
| 109 |
input{"0100", "0400", "02000000", "10000000", "1112131415161718"}, |
| 110 |
output{0x0001, DataType(0x0004), 0x0002, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}}, |
| 111 |
}, |
| 112 |
tagTest{ |
| 113 |
input{"0001", "0009", "00000001", "11121314", ""}, |
| 114 |
input{"0100", "0900", "01000000", "11121314", ""}, |
| 115 |
output{0x0001, DataType(0x0009), 0x0001, []byte{0x11, 0x12, 0x13, 0x14}}, |
| 116 |
}, |
| 117 |
tagTest{ |
| 118 |
input{"0001", "0009", "00000002", "00000011", "1112131415161819"}, |
| 119 |
input{"0100", "0900", "02000000", "11000000", "1112131415161819"}, |
| 120 |
output{0x0001, DataType(0x0009), 0x0002, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18, 0x19}}, |
| 121 |
}, |
| 122 |
//////////// rational types //////////////////// |
| 123 |
tagTest{ |
| 124 |
input{"0001", "0005", "00000001", "00000010", "1112131415161718"}, |
| 125 |
input{"0100", "0500", "01000000", "10000000", "1112131415161718"}, |
| 126 |
output{0x0001, DataType(0x0005), 0x0001, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}}, |
| 127 |
}, |
| 128 |
tagTest{ |
| 129 |
input{"0001", "000A", "00000001", "00000011", "1112131415161819"}, |
| 130 |
input{"0100", "0A00", "01000000", "11000000", "1112131415161819"}, |
| 131 |
output{0x0001, DataType(0x000A), 0x0001, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18, 0x19}}, |
| 132 |
}, |
| 133 |
//////////// float types /////////////////////// |
| 134 |
tagTest{ |
| 135 |
input{"0001", "0005", "00000001", "00000010", "1112131415161718"}, |
| 136 |
input{"0100", "0500", "01000000", "10000000", "1112131415161718"}, |
| 137 |
output{0x0001, DataType(0x0005), 0x0001, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}}, |
| 138 |
}, |
| 139 |
tagTest{ |
| 140 |
input{"0101", "000A", "00000001", "00000011", "1112131415161819"}, |
| 141 |
input{"0101", "0A00", "01000000", "11000000", "1112131415161819"}, |
| 142 |
output{0x0101, DataType(0x000A), 0x0001, []byte{0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18, 0x19}}, |
| 143 |
}, |
| 144 |
} |
| 145 |
|
| 146 |
func TestDecodeTag(t *testing.T) { |
| 147 |
for i, tst := range set1 { |
| 148 |
testSingle(t, binary.BigEndian, tst.big, tst.out, i) |
| 149 |
testSingle(t, binary.LittleEndian, tst.little, tst.out, i) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func testSingle(t *testing.T, order binary.ByteOrder, in input, out output, i int) { |
| 154 |
data := buildInput(in, order) |
| 155 |
buf := bytes.NewReader(data) |
| 156 |
tg, err := DecodeTag(buf, order) |
| 157 |
if err != nil { |
| 158 |
t.Errorf("(%v) tag %v%+v decode failed: %v", order, i, in, err) |
| 159 |
return |
| 160 |
} |
| 161 |
|
| 162 |
if tg.Id != out.id { |
| 163 |
t.Errorf("(%v) tag %v id decode: expected %v, got %v", order, i, out.id, tg.Id) |
| 164 |
} |
| 165 |
if tg.Type != out.typ { |
| 166 |
t.Errorf("(%v) tag %v type decode: expected %v, got %v", order, i, out.typ, tg.Type) |
| 167 |
} |
| 168 |
if tg.Count != out.count { |
| 169 |
t.Errorf("(%v) tag %v component count decode: expected %v, got %v", order, i, out.count, tg.Count) |
| 170 |
} |
| 171 |
if tg.Type == DTAscii && in.val != "" { |
| 172 |
strOut := string(out.val) |
| 173 |
if tg.strVal != strOut { |
| 174 |
t.Errorf("(%v) tag %v string value decode: expected %q, got %q", order, i, strOut, tg.strVal) |
| 175 |
} |
| 176 |
} else { |
| 177 |
if !bytes.Equal(tg.Val, out.val) { |
| 178 |
t.Errorf("(%v) tag %v value decode: expected %q, got %q", order, i, out.val, tg.Val) |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// buildInputBig creates a byte-slice based on big-endian ordered input |
| 184 |
func buildInput(in input, order binary.ByteOrder) []byte { |
| 185 |
data := make([]byte, 0) |
| 186 |
d, _ := hex.DecodeString(in.tgId) |
| 187 |
data = append(data, d...) |
| 188 |
d, _ = hex.DecodeString(in.tpe) |
| 189 |
data = append(data, d...) |
| 190 |
d, _ = hex.DecodeString(in.nVals) |
| 191 |
data = append(data, d...) |
| 192 |
d, _ = hex.DecodeString(in.offset) |
| 193 |
data = append(data, d...) |
| 194 |
|
| 195 |
if in.val != "" { |
| 196 |
off := order.Uint32(d) |
| 197 |
for i := 0; i < int(off)-12; i++ { |
| 198 |
data = append(data, 0xFF) |
| 199 |
} |
| 200 |
|
| 201 |
d, _ = hex.DecodeString(in.val) |
| 202 |
data = append(data, d...) |
| 203 |
} |
| 204 |
|
| 205 |
return data |
| 206 |
} |
| 207 |
|
| 208 |
func TestDecode(t *testing.T) { |
| 209 |
name := filepath.Join(*dataDir, "sample1.tif") |
| 210 |
f, err := os.Open(name) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("%v\n", err) |
| 213 |
} |
| 214 |
|
| 215 |
tif, err := Decode(f) |
| 216 |
if err != nil { |
| 217 |
t.Fatal(err) |
| 218 |
} |
| 219 |
|
| 220 |
t.Log(tif) |
| 221 |
} |
| 222 |
|
| 223 |
func TestDecodeTag_blob(t *testing.T) { |
| 224 |
buf := bytes.NewReader(data()) |
| 225 |
buf.Seek(10, 1) |
| 226 |
tg, err := DecodeTag(buf, binary.LittleEndian) |
| 227 |
if err != nil { |
| 228 |
t.Fatalf("tag decode failed: %v", err) |
| 229 |
} |
| 230 |
|
| 231 |
t.Logf("tag: %v+\n", tg) |
| 232 |
n, d, err := tg.Rat2(0) |
| 233 |
if err != nil { |
| 234 |
t.Fatalf("tag decoded wrong type: %v", err) |
| 235 |
} |
| 236 |
t.Logf("tag rat val: %v/%v\n", n, d) |
| 237 |
} |
| 238 |
|
| 239 |
func data() []byte { |
| 240 |
s1 := "49492A000800000002001A0105000100" |
| 241 |
s1 += "00002600000069870400010000001102" |
| 242 |
s1 += "0000000000004800000001000000" |
| 243 |
|
| 244 |
dat, err := hex.DecodeString(s1) |
| 245 |
if err != nil { |
| 246 |
panic("invalid string fixture") |
| 247 |
} |
| 248 |
return dat |
| 249 |
} |