-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_test.go
270 lines (255 loc) · 5.92 KB
/
value_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package bsony
import (
"bytes"
"encoding/hex"
"strings"
"testing"
)
// 0x01 => 8,
// 0x02 => 5,
// 0x03 => 5,
// 0x04 => 5,
// 0x05 => 5,
// 0x06 => 0,
// 0x07 => 12,
// 0x08 => 1,
// 0x09 => 8,
// 0x0A => 0,
// 0x0B => 2,
// 0x0C => 17,
// 0x0D => 5,
// 0x0E => 5,
// 0x0F => 11,
// 0x10 => 4,
// 0x11 => 8,
// 0x12 => 8,
// 0x13 => 16,
// 0x7F => 0,
// 0xFF => 0,
// Every type except null, undefined, minkey, and maxkey requires a fixed or
// minimum number of bytes.
func TestNewUnsafeValue_TooShort(t *testing.T) {
fct := New()
cases := []struct {
minLen int
types []Type
}{
{
minLen: 1,
types: []Type{TypeBoolean},
},
{
minLen: 2,
types: []Type{TypeRegex},
},
{
minLen: 4,
types: []Type{TypeInt32},
},
{
minLen: 5,
types: []Type{TypeString, TypeEmbeddedDocument, TypeArray, TypeBinary, TypeSymbol, TypeJavaScript},
},
{
minLen: 8,
types: []Type{TypeDouble, TypeInt64, TypeDateTime, TypeTimestamp},
},
{
minLen: 12,
types: []Type{TypeObjectID},
},
{
minLen: 14,
types: []Type{TypeCodeWithScope},
},
{
minLen: 16,
types: []Type{TypeDecimal128},
},
{
minLen: 17,
types: []Type{TypeDBPointer},
},
}
for _, c := range cases {
for _, bt := range c.types {
// nil buffer
uv := newValueUnsafe(fct, nil, bt)
if uv.Err() != errShortDoc {
t.Errorf("for type %s with nil buffer, expected '%v', got '%v'", bt, errShortDoc, uv.Err())
}
uv.Release()
// short buffer
buf := make([]byte, c.minLen-1)
uv = newValueUnsafe(fct, buf, bt)
if uv.Err() != errShortDoc {
t.Errorf("for type %s, expected '%v', got '%v'", bt, errShortDoc, uv.Err())
}
uv.Release()
}
}
}
// Some types have a leading length, though they vary
// whether or not the length includes itself. This test covers types that have
// no other internal structure.
func TestNewUnsafeValue_BadLeadingLength(t *testing.T) {
fct := New()
cases := []struct {
bt Type
nullLocations []int
lengthIncludesSelf bool
minLen int
unTerminated bool
}{
{
bt: TypeString,
nullLocations: []int{4},
lengthIncludesSelf: false,
minLen: 5,
},
{
bt: TypeEmbeddedDocument,
nullLocations: []int{4},
lengthIncludesSelf: true,
minLen: 5,
},
{
bt: TypeArray,
nullLocations: []int{4},
lengthIncludesSelf: true,
minLen: 5,
},
{
bt: TypeBinary,
nullLocations: []int{4},
lengthIncludesSelf: false,
minLen: 5,
unTerminated: true,
},
{
bt: TypeJavaScript,
nullLocations: []int{4},
lengthIncludesSelf: false,
minLen: 5,
},
{
bt: TypeSymbol,
nullLocations: []int{4},
lengthIncludesSelf: false,
minLen: 5,
},
}
for _, c := range cases {
// First test case: a minimum length buffer with a leading length that
// exceeds that.
buf := make([]byte, c.minLen)
l := c.minLen + 1
if !c.lengthIncludesSelf {
l -= 4
}
writeInt32(buf, 0, int32(l))
uv := newValueUnsafe(fct, buf, c.bt)
if uv.Err() != errShortDoc {
t.Errorf("for type %s, expected '%v', got '%v'", c.bt, errShortDoc, uv.Err())
}
uv.Release()
// Second test case: a buffer that exceeds the minimum length, with
// non-zero sentinal values afterwards, and a leading length that
// exceeds the minimum length to catch the sentinal.
buf = bytes.Repeat([]byte{0xff}, c.minLen)
writeInt32(buf, 0, int32(l))
for _, n := range c.nullLocations {
buf[n] = 0
}
uv = newValueUnsafe(fct, buf, c.bt)
if uv.Err() != errShortDoc {
t.Errorf("for type %s, expected '%v', got '%v'", c.bt, errShortDoc, uv.Err())
}
uv.Release()
}
}
// Code with scope has several ways the internal structure can be invalid
func TestNewUnsafeValue_BadCodeWithScope(t *testing.T) {
fct := New()
cases := []struct {
label string
src string
errStr string
}{
{
label: "length exceeds buffer",
src: "0f000000 01000000 00 05000000 00",
errStr: errShortDoc.Error(),
},
{
label: "length exceeds null terminator",
src: "0f000000 01000000 00 05000000 00 ff",
errStr: "code with scope: scope missing null terminator",
},
{
label: "zero string length",
src: "0e000000 00000000 05000000 00 00",
errStr: "invalid, non-positive string length",
},
{
label: "zero string length",
src: "0e000000 0a000000 00 05000000 00",
errStr: "string length too long",
},
{
label: "zero string length",
src: "0e000000 01000000 00 06000000 00",
errStr: "scope size invalid",
},
}
for _, c := range cases {
stripped := strings.ReplaceAll(c.src, " ", "")
buf, err := hex.DecodeString(stripped)
if err != nil {
t.Fatal(err)
}
uv := newValueUnsafe(fct, buf, TypeCodeWithScope)
if uv.Err() == nil || !strings.Contains(uv.Err().Error(), c.errStr) {
t.Errorf("expected '%v', got '%v'", c.errStr, uv.Err())
}
uv.Release()
}
}
func TestValueCloning(t *testing.T) {
fct = New()
bufStr := "0e00000002610002000000620000"
buf, _ := hex.DecodeString(bufStr)
doc, err := fct.NewDocFromBytes(buf)
if err != nil {
t.Fatal(err)
}
iter := doc.Iter()
iter.Next()
v1 := iter.ValueUnsafe()
uv, ok := v1.(*unsafeValue)
if !ok {
t.Fatal("ValueUnsafe was not unsafeValue")
}
if &(uv.data[0]) != &(buf[07]) {
t.Fatal("unsafeValue was not view into document buffer")
}
v2 := v1.Clone()
ov, ok := v2.(*ownedValue)
if !ok {
t.Fatal("Cloned unsafeValue was not ownedValue")
}
if &(ov.unsafeValue.data[0]) == &(uv.data[0]) {
t.Fatal("Clone of unsafeValue did not copy data")
}
v3 := v2.Clone()
ov2, ok := v3.(*ownedValue)
if !ok {
t.Fatal("Cloned ownedValue was not ownedValue")
}
if &(ov2.unsafeValue.data[0]) == &(ov.unsafeValue.data[0]) {
t.Fatal("Clone of ownedValue did not copy data")
}
v1.Release()
v2.Release()
v3.Release()
}