Repository navigation
Expand file tree
/
Copy pathproperties_test.go
More file actions
139 lines (134 loc) · 4.07 KB
/
Copy pathproperties_test.go
File metadata and controls
139 lines (134 loc) · 4.07 KB
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
package xlsreader
import (
"bytes"
"errors"
"sort"
"testing"
"time"
"unicode/utf16"
)
func propertyFixture(values map[uint32][]byte) []byte {
ids := make([]int, 0, len(values))
for id := range values {
ids = append(ids, int(id))
}
sort.Ints(ids)
section := make([]byte, 8+len(ids)*8)
le.PutUint32(section[4:], uint32(len(ids)))
for i, id := range ids {
le.PutUint32(section[8+i*8:], uint32(id))
le.PutUint32(section[12+i*8:], uint32(len(section)))
section = append(section, values[uint32(id)]...)
for len(section)%4 != 0 {
section = append(section, 0)
}
}
le.PutUint32(section, uint32(len(section)))
header := make([]byte, 48)
le.PutUint16(header, 0xFFFE)
le.PutUint32(header[24:], 1)
copy(header[28:], summaryFMTID)
le.PutUint32(header[44:], 48)
return append(header, section...)
}
func cpProperty(cp uint16) []byte {
v := make([]byte, 8)
le.PutUint32(v, 2)
le.PutUint16(v[4:], cp)
return v
}
func textProperty(kind uint32, encoded []byte, length uint32) []byte {
v := make([]byte, 8)
le.PutUint32(v, kind)
le.PutUint32(v[4:], length)
return append(v, encoded...)
}
func utf16Property(s string) []byte {
units := utf16.Encode([]rune(s + "\x00"))
b := make([]byte, len(units)*2)
for i, u := range units {
le.PutUint16(b[i*2:], u)
}
return textProperty(31, b, uint32(len(units)))
}
func timeProperty(t time.Time) []byte {
ticks := uint64(t.Unix()+11644473600)*10000000 + uint64(t.Nanosecond()/100)
b := make([]byte, 12)
le.PutUint32(b, 0x40)
le.PutUint64(b[4:], ticks)
return b
}
func TestPropertyEncodingsAndTimes(t *testing.T) {
stamp := time.Date(2024, 6, 5, 4, 3, 2, 123456700, time.UTC)
cases := []struct {
cp uint16
title []byte
want string
}{
{1252, textProperty(30, []byte{'c', 'a', 'f', 0xE9, 0}, 5), "café"},
{1251, textProperty(30, []byte{0xCF, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0}, 7), "Привет"},
{65001, textProperty(30, []byte("🐧\x00"), 5), "🐧"},
{1200, textProperty(30, []byte{'A', 0, 0, 0}, 4), "A"},
{1252, utf16Property("Hello 🐧"), "Hello 🐧"},
{9999, textProperty(30, []byte("text\x00"), 5), ""},
}
for _, tc := range cases {
data := propertyFixture(map[uint32][]byte{1: cpProperty(tc.cp), 2: tc.title, 12: timeProperty(stamp), 13: timeProperty(stamp.Add(time.Hour))})
p, err := readProperties(data)
if err != nil {
t.Fatal(err)
}
if p.Title != tc.want || !p.CreatedAt.Equal(stamp) || !p.ModifiedAt.Equal(stamp.Add(time.Hour)) {
t.Fatalf("%d: %+v", tc.cp, p)
}
if p.Subject != "" {
t.Fatal("absent field")
}
}
data := propertyFixture(map[uint32][]byte{2: {3, 0, 0, 0, 1, 0, 0, 0}, 12: {0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}})
p, err := readProperties(data)
if err != nil || p != (Properties{}) {
t.Fatal(p, err)
}
}
func TestMalformedProperties(t *testing.T) {
good := propertyFixture(map[uint32][]byte{1: cpProperty(1200), 2: utf16Property("text")})
for _, n := range []int{0, 27, 47, 49, len(good) - 1} {
if _, err := readProperties(good[:n]); !errors.Is(err, ErrInvalidFile) {
t.Fatal(n, err)
}
}
mutations := []func([]byte){
func(b []byte) { le.PutUint32(b[24:], 0xFFFFFFFF) },
func(b []byte) { le.PutUint32(b[44:], 0xFFFFFFFF) },
func(b []byte) { le.PutUint32(b[48:], 0xFFFFFFFF) },
func(b []byte) { le.PutUint32(b[52:], 0xFFFFFFFF) },
func(b []byte) { le.PutUint32(b[60:], 0) },
func(b []byte) { le.PutUint32(b[68:], le.Uint32(b[60:])) },
}
for i, mutate := range mutations {
data := bytes.Clone(good)
mutate(data)
if _, err := readProperties(data); !errors.Is(err, ErrInvalidFile) {
t.Fatal(i, err)
}
}
for _, value := range [][]byte{textProperty(31, []byte{0}, 0xFFFFFFFF), {0x40, 0, 0, 0}} {
id := uint32(2)
if value[0] == 0x40 {
id = 12
}
if _, err := readProperties(propertyFixture(map[uint32][]byte{id: value})); !errors.Is(err, ErrInvalidFile) {
t.Fatal(err)
}
}
}
func FuzzProperties(f *testing.F) {
f.Add(propertyFixture(map[uint32][]byte{1: cpProperty(1200), 2: utf16Property("title 🐧"), 12: timeProperty(time.Unix(0, 0))}))
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) > 1<<20 {
t.Skip()
}
_, _ = readProperties(data)
})
}