forked from RedisLabs/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
454 lines (402 loc) · 11.4 KB
/
decoder_test.go
File metadata and controls
454 lines (402 loc) · 11.4 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package xmlrpc
import (
"errors"
"io"
"os"
"reflect"
"testing"
"time"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
)
type book struct {
Title string
Amount int
}
type bookUnexported struct {
title string //lint:ignore U1000 intentionally unexported for testing unmarshalling behavior
amount int //lint:ignore U1000 intentionally unexported for testing unmarshalling behavior
}
func testTime(year int, month time.Month, day, hour, min, sec int, loc *time.Location) time.Time {
return time.Date(year, month, day, hour, min, sec, 0, loc)
}
var unmarshalTests = []struct {
name string
value any
ptr any
xml string
}{
// int, i4, i8
{"int/empty", 0, new(*int), "<value><int></int></value>"},
{"int/positive", 100, new(*int), "<value><int>100</int></value>"},
{"i4", 389451, new(*int), "<value><i4>389451</i4></value>"},
{"i8", int64(45659074), new(*int64), "<value><i8>45659074</i8></value>"},
// string
{
"string/simple",
"Once upon a time",
new(*string),
"<value><string>Once upon a time</string></value>",
},
{
"string/escaped",
"Mike & Mick <London, UK>",
new(*string),
"<value><string>Mike & Mick <London, UK></string></value>",
},
{"string/implicit", "Once upon a time", new(*string), "<value>Once upon a time</value>"},
// base64
{
"base64",
"T25jZSB1cG9uIGEgdGltZQ==",
new(*string),
"<value><base64>T25jZSB1cG9uIGEgdGltZQ==</base64></value>",
},
// boolean
{"boolean/true", true, new(*bool), "<value><boolean>1</boolean></value>"},
{"boolean/false", false, new(*bool), "<value><boolean>0</boolean></value>"},
// double
{"double/positive", 12.134, new(*float32), "<value><double>12.134</double></value>"},
{"double/negative", -12.134, new(*float32), "<value><double>-12.134</double></value>"},
// datetime.iso8601
{
"datetime/basic",
testTime(2013, 12, 9, 21, 0, 12, time.UTC),
new(*time.Time),
"<value><dateTime.iso8601>20131209T21:00:12</dateTime.iso8601></value>",
},
{
"datetime/Z",
testTime(2013, 12, 9, 21, 0, 12, time.UTC),
new(*time.Time),
"<value><dateTime.iso8601>20131209T21:00:12Z</dateTime.iso8601></value>",
},
{
"datetime/negative_offset",
testTime(2013, 12, 9, 21, 0, 12, time.FixedZone("", -3600)),
new(*time.Time),
"<value><dateTime.iso8601>20131209T21:00:12-01:00</dateTime.iso8601></value>",
},
{
"datetime/positive_offset",
testTime(2013, 12, 9, 21, 0, 12, time.FixedZone("", 3600)),
new(*time.Time),
"<value><dateTime.iso8601>20131209T21:00:12+01:00</dateTime.iso8601></value>",
},
{
"datetime/hyphen",
testTime(2013, 12, 9, 21, 0, 12, time.UTC),
new(*time.Time),
"<value><dateTime.iso8601>2013-12-09T21:00:12</dateTime.iso8601></value>",
},
{
"datetime/hyphen_Z",
testTime(2013, 12, 9, 21, 0, 12, time.UTC),
new(*time.Time),
"<value><dateTime.iso8601>2013-12-09T21:00:12Z</dateTime.iso8601></value>",
},
{
"datetime/hyphen_negative_offset",
testTime(2013, 12, 9, 21, 0, 12, time.FixedZone("", -3600)),
new(*time.Time),
"<value><dateTime.iso8601>2013-12-09T21:00:12-01:00</dateTime.iso8601></value>",
},
{
"datetime/hyphen_positive_offset",
testTime(2013, 12, 9, 21, 0, 12, time.FixedZone("", 3600)),
new(*time.Time),
"<value><dateTime.iso8601>2013-12-09T21:00:12+01:00</dateTime.iso8601></value>",
},
// array
{
"array/int",
[]int{1, 5, 7},
new(*[]int),
"<value><array><data><value><int>1</int></value><value><int>5</int></value><value><int>7</int></value></data></array></value>",
},
{
"array/any_strings",
[]any{"A", "5"},
new(any),
"<value><array><data><value><string>A</string></value><value><string>5</string></value></data></array></value>",
},
{
"array/any_mixed",
[]any{"A", int64(5)},
new(any),
"<value><array><data><value><string>A</string></value><value><int>5</int></value></data></array></value>",
},
// struct
{
"struct/book",
book{"War and Piece", 20},
new(*book),
"<value><struct><member><name>Title</name><value><string>War and Piece</string></value></member><member><name>Amount</name><value><int>20</int></value></member></struct></value>",
},
{
"struct/unexported",
bookUnexported{},
new(*bookUnexported),
"<value><struct><member><name>title</name><value><string>War and Piece</string></value></member><member><name>amount</name><value><int>20</int></value></member></struct></value>",
},
{
"struct/to_map",
map[string]any{"Name": "John Smith"},
new(any),
"<value><struct><member><name>Name</name><value><string>John Smith</string></value></member></struct></value>",
},
{"struct/empty", map[string]any{}, new(any), "<value><struct></struct></value>"},
}
func TestUnmarshal(t *testing.T) {
t.Parallel()
for _, tt := range unmarshalTests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := reflect.New(reflect.TypeOf(tt.value))
if err := unmarshal([]byte(tt.xml), v.Interface()); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
v = v.Elem()
if v.Kind() == reflect.Slice {
vv := reflect.ValueOf(tt.value)
if vv.Len() != v.Len() {
t.Fatalf(
"unmarshal error:\nexpected: %v\n got: %v",
tt.value,
v.Interface(),
)
}
for i := 0; i < v.Len(); i++ {
if v.Index(i).Interface() != vv.Index(i).Interface() {
t.Fatalf(
"unmarshal error:\nexpected: %v\n got: %v",
tt.value,
v.Interface(),
)
}
}
} else if t1, ok := v.Interface().(time.Time); ok {
t2 := tt.value.(time.Time)
if !t1.Equal(t2) {
t.Fatalf(
"unmarshal error:\nexpected: %v\n got: %v",
tt.value,
v.Interface(),
)
}
} else {
a1 := v.Interface()
a2 := any(tt.value)
if !reflect.DeepEqual(a1, a2) {
t.Fatalf(
"unmarshal error:\nexpected: %v\n got: %v",
tt.value,
v.Interface(),
)
}
}
})
}
}
func TestUnmarshalToNil(t *testing.T) {
t.Parallel()
for _, tt := range unmarshalTests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := unmarshal([]byte(tt.xml), tt.ptr); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
})
}
}
func TestTypeMismatchError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
xml string
target any
wantErr bool
}{
{"int_to_string", "<value><int>100</int></value>", new(string), true},
{"string_to_int", "<value><string>hello</string></value>", new(int), true},
{"bool_to_string", "<value><boolean>1</boolean></value>", new(string), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := unmarshal([]byte(tt.xml), tt.target)
if tt.wantErr {
if err == nil {
t.Fatal("expected error, got nil")
}
if _, ok := err.(TypeMismatchError); !ok {
t.Fatalf("expected TypeMismatchError, got %T: %v", err, err)
}
} else if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
}
func TestUnmarshalEmptyValueTag(t *testing.T) {
t.Parallel()
var v int
if err := unmarshal([]byte("<value/>"), &v); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
}
func TestUnmarshalEmptyStruct(t *testing.T) {
t.Parallel()
const xml = `<value><struct></struct></value>`
var v any
if err := unmarshal([]byte(xml), &v); err != nil {
t.Fatal(err)
}
if v == nil {
t.Fatalf("got nil map")
}
}
func TestUnmarshalExistingArray(t *testing.T) {
t.Parallel()
const xml = `
<value>
<array>
<data>
<value><int>234</int></value>
<value><boolean>1</boolean></value>
<value><string>Hello World</string></value>
<value><string>Extra Value</string></value>
</data>
</array>
</value>`
var (
v1 int
v2 bool
v3 string
v = []any{&v1, &v2, &v3}
)
if err := unmarshal([]byte(xml), &v); err != nil {
t.Fatal(err)
}
if want := 234; v1 != want {
t.Fatalf("v1: want %d, got %d", want, v1)
}
if want := true; v2 != want {
t.Fatalf("v2: want %t, got %t", want, v2)
}
if want := "Hello World"; v3 != want {
t.Fatalf("v3: want %s, got %s", want, v3)
}
if n := len(v); n != 4 {
t.Fatalf("missing appended result, len=%d", n)
}
if got, ok := v[3].(string); !ok || got != "Extra Value" {
t.Fatalf("v[3]: got %s, want %s", got, "Extra Value")
}
}
func TestDecodeNonUTF8Response(t *testing.T) {
data, err := os.ReadFile("testdata/fixtures/cp1251.xml")
if err != nil {
t.Fatal(err)
}
CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
if charset != "cp1251" {
return nil, errors.New("unsupported charset: " + charset)
}
return transform.NewReader(input, charmap.Windows1251.NewDecoder()), nil
}
t.Cleanup(func() { CharsetReader = nil })
var s string
if err = unmarshal(data, &s); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
expected := "Л.Н. Толстой - Война и Мир"
if s != expected {
t.Fatalf("unmarshal error:\nexpected: %v\n got: %v", expected, s)
}
}
func BenchmarkUnmarshal(b *testing.B) {
benchmarks := []struct {
name string
xml string
ptr any
}{
{"int", "<value><int>12345</int></value>", new(int)},
{"string", "<value><string>Hello, World!</string></value>", new(string)},
{"bool", "<value><boolean>1</boolean></value>", new(bool)},
{
"struct",
"<value><struct><member><name>Title</name><value><string>Test</string></value></member><member><name>Amount</name><value><int>100</int></value></member></struct></value>",
new(book),
},
{
"array",
"<value><array><data><value><int>1</int></value><value><int>2</int></value><value><int>3</int></value></data></array></value>",
new([]int),
},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
data := []byte(bm.xml)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := unmarshal(data, bm.ptr); err != nil {
b.Fatal(err)
}
}
})
}
}
func FuzzUnmarshalInt(f *testing.F) {
f.Add([]byte(`<value><int>0</int></value>`))
f.Add([]byte(`<value><int>123</int></value>`))
f.Add([]byte(`<value><int>-456</int></value>`))
f.Add([]byte(`<value><i4>789</i4></value>`))
f.Add([]byte(`<value><i8>9223372036854775807</i8></value>`))
f.Fuzz(func(t *testing.T, data []byte) {
var result int64
_ = unmarshal(data, &result)
})
}
func FuzzUnmarshalString(f *testing.F) {
f.Add([]byte(`<value><string>hello</string></value>`))
f.Add([]byte(`<value><string></string></value>`))
f.Add([]byte(`<value><string>a & b</string></value>`))
f.Add([]byte(`<value>implicit string</value>`))
f.Fuzz(func(t *testing.T, data []byte) {
var result string
_ = unmarshal(data, &result)
})
}
func FuzzUnmarshalStruct(f *testing.F) {
f.Add([]byte(`<value><struct></struct></value>`))
f.Add(
[]byte(
`<value><struct><member><name>foo</name><value><string>bar</string></value></member></struct></value>`,
),
)
f.Fuzz(func(t *testing.T, data []byte) {
var result map[string]any
_ = unmarshal(data, &result)
})
}
func FuzzUnmarshalArray(f *testing.F) {
f.Add([]byte(`<value><array><data></data></array></value>`))
f.Add([]byte(`<value><array><data><value><int>1</int></value></data></array></value>`))
f.Fuzz(func(t *testing.T, data []byte) {
var result []any
_ = unmarshal(data, &result)
})
}
func FuzzUnmarshalAny(f *testing.F) {
f.Add([]byte(`<value><int>1</int></value>`))
f.Add([]byte(`<value><string>test</string></value>`))
f.Add([]byte(`<value><boolean>1</boolean></value>`))
f.Add([]byte(`<value><double>1.5</double></value>`))
f.Add([]byte(`<value><struct></struct></value>`))
f.Add([]byte(`<value><array><data></data></array></value>`))
f.Fuzz(func(t *testing.T, data []byte) {
var result any
_ = unmarshal(data, &result)
})
}