-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors_test.go
219 lines (186 loc) · 4.89 KB
/
errors_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package ikea
import (
"bytes"
"errors"
"math"
"testing"
)
func TestReadPointer(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestReadPointer should have panicked due to an invalid argument, it didn't")
}
}()
var i int
_ = Unpack(nil, i)
}
func TestUseInt(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestUseInt should have panicked due to an unsupported type, it didn't")
}
}()
var i int
_ = Unpack(nil, &i)
}
func TestUseUint(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestUseUint should have panicked due to an unsupported type, it didn't")
}
}()
var ui uint
_ = Unpack(nil, &ui) // uint is not supported
}
func TestUnsupportedType(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestUseUint should have panicked due to an unsupported type, it didn't")
}
}()
var ui complex64
_ = Unpack(nil, &ui) // uint is not supported
}
func TestVariableLengthOverflow(t *testing.T) {
overflow := new(bytes.Buffer)
_ = Pack(overflow, uint32(math.MaxInt32+1))
var t1 struct {
Data struct{} `ikea:"compress:9"`
}
if err := Unpack(overflow, &t1); err == nil {
t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32")
}
overflow.Reset()
_ = Pack(overflow, uint32(math.MaxInt32+1))
var t2 map[string]struct{}
if err := Unpack(overflow, &t2); err == nil {
t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32")
}
overflow.Reset()
_ = Pack(overflow, uint32(math.MaxInt32+1))
var t3 []struct{}
if err := Unpack(overflow, &t3); err == nil {
t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32")
}
overflow.Reset()
_ = Pack(overflow, uint32(math.MaxInt32+1))
var t4 string
if err := Unpack(overflow, &t4); err == nil {
t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32")
}
}
func TestCompressionInitError(t *testing.T) {
s1 := struct {
Data []byte `ikea:"compress:10"`
}{make([]byte, 10)}
if err := Pack(new(bytes.Buffer), &s1); err == nil {
t.Error("TestCompressionInitError should have failed because of an illegal compression level")
}
s2 := struct {
Data []byte `ikea:"compress:a"`
}{make([]byte, 10)}
defer func() {
if recover() == nil {
t.Error("TestCompressionInitError should have failed because of an non-numerical compression level")
}
}()
_ = Pack(new(bytes.Buffer), &s2)
}
func TestInvalidUTF8(t *testing.T) {
var invalid string
b := bytes.NewBuffer([]byte{0x00, 0x00, 0x00, 0x01, 0xF1})
if Unpack(b, &invalid) == nil {
t.Error("TestInvalidUTF8 should have failed because of an invalid utf-8 string")
}
}
func TestFixedNilPointerPacking(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestFixedNilPointerPacking should panic because of an attempt to write an uninitialized variable, it didn't")
}
}()
s := struct {
A *uint32
}{}
_ = Pack(new(bytes.Buffer), &s)
}
func TestVariableNilPointerPacking(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestVariableNilPointerPacking should panic because of an attempt to write an uninitialized variable, it didn't")
}
}()
s := struct {
A *string
}{}
_ = Pack(new(bytes.Buffer), &s)
}
func TestFixedNilPointerLength(t *testing.T) {
s := struct {
A *uint32
}{}
Len(&s) // Unlike all other nil values, this should succeed
}
func TestVariableNilPointerLength(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("TestVariableNilPointerLength should panic because of an attempt to write an uninitialized variable, it didn't")
}
}()
s := struct {
A *string
}{}
Len(&s)
}
func TestReadErrors(t *testing.T) {
e := new(errorStream)
// Reading error tests
tst := new(testStruct)
err := errors.New("start of errors")
for err != nil {
err = Unpack(e, tst)
if err == nil && e.pass != len(testData) {
t.Error("TestReadErrors should have failed because of simulated IO errors, it didn't")
return
}
e.Reset()
}
}
func TestWriteErrors(t *testing.T) {
e := new(errorStream)
// Reading error tests
err := errors.New("start of errors")
for err != nil {
err = Pack(e, source)
if err == nil && e.pass != len(testData) {
t.Error("TestWriteErrors should have failed because of simulated IO errors, it didn't")
return
}
e.Reset()
}
}
type errorStream struct {
pointer int
pass int
}
func (s *errorStream) Read(p []byte) (n int, err error) {
if s.pointer+len(p) > s.pass {
s.pointer += len(p)
return 0, errors.New("test error")
}
copy(p, testData[s.pointer:s.pointer+len(p)])
s.pointer += len(p)
return len(p), nil
}
func (s *errorStream) Write(p []byte) (n int, err error) {
if s.pointer+len(p) > s.pass {
s.pointer += len(p)
return 0, errors.New("test error")
}
s.pointer += len(p)
return len(p), nil
}
func (s *errorStream) Reset() {
s.pass = s.pointer
s.pointer = 0
}