-
Notifications
You must be signed in to change notification settings - Fork 43
/
json_test.go
613 lines (535 loc) · 17.1 KB
/
json_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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package pgproto3
import (
"encoding/hex"
"encoding/json"
"reflect"
"testing"
)
func TestJSONUnmarshalAuthenticationMD5Password(t *testing.T) {
data := []byte(`{"Type":"AuthenticationMD5Password", "Salt":[97,98,99,100]}`)
want := AuthenticationMD5Password{
Salt: [4]byte{'a', 'b', 'c', 'd'},
}
var got AuthenticationMD5Password
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationMD5Password struct doesn't match expected value")
}
}
func TestJSONUnmarshalAuthenticationSASL(t *testing.T) {
data := []byte(`{"Type":"AuthenticationSASL","AuthMechanisms":["SCRAM-SHA-256"]}`)
want := AuthenticationSASL{
[]string{"SCRAM-SHA-256"},
}
var got AuthenticationSASL
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationSASL struct doesn't match expected value")
}
}
func TestJSONUnmarshalAuthenticationGSS(t *testing.T) {
data := []byte(`{"Type":"AuthenticationGSS"}`)
want := AuthenticationGSS{}
var got AuthenticationGSS
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationGSS struct doesn't match expected value")
}
}
func TestJSONUnmarshalAuthenticationGSSContinue(t *testing.T) {
data := []byte(`{"Type":"AuthenticationGSSContinue","Data":[1,2,3,4]}`)
want := AuthenticationGSSContinue{Data: []byte{1, 2, 3, 4}}
var got AuthenticationGSSContinue
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationGSSContinue struct doesn't match expected value")
}
}
func TestJSONUnmarshalAuthenticationSASLContinue(t *testing.T) {
data := []byte(`{"Type":"AuthenticationSASLContinue", "Data":"1"}`)
want := AuthenticationSASLContinue{
Data: []byte{'1'},
}
var got AuthenticationSASLContinue
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationSASLContinue struct doesn't match expected value")
}
}
func TestJSONUnmarshalAuthenticationSASLFinal(t *testing.T) {
data := []byte(`{"Type":"AuthenticationSASLFinal", "Data":"1"}`)
want := AuthenticationSASLFinal{
Data: []byte{'1'},
}
var got AuthenticationSASLFinal
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationSASLFinal struct doesn't match expected value")
}
}
func TestJSONUnmarshalBackendKeyData(t *testing.T) {
data := []byte(`{"Type":"BackendKeyData","ProcessID":8864,"SecretKey":3641487067}`)
want := BackendKeyData{
ProcessID: 8864,
SecretKey: 3641487067,
}
var got BackendKeyData
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled BackendKeyData struct doesn't match expected value")
}
}
func TestJSONUnmarshalCommandComplete(t *testing.T) {
data := []byte(`{"Type":"CommandComplete","CommandTag":"SELECT 1"}`)
want := CommandComplete{
CommandTag: []byte("SELECT 1"),
}
var got CommandComplete
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CommandComplete struct doesn't match expected value")
}
}
func TestJSONUnmarshalCopyBothResponse(t *testing.T) {
data := []byte(`{"Type":"CopyBothResponse", "OverallFormat": "W"}`)
want := CopyBothResponse{
OverallFormat: 'W',
}
var got CopyBothResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CopyBothResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalCopyData(t *testing.T) {
data := []byte(`{"Type":"CopyData"}`)
want := CopyData{
Data: []byte{},
}
var got CopyData
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CopyData struct doesn't match expected value")
}
}
func TestJSONUnmarshalCopyInResponse(t *testing.T) {
data := []byte(`{"Type":"CopyBothResponse", "OverallFormat": "W"}`)
want := CopyBothResponse{
OverallFormat: 'W',
}
var got CopyBothResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CopyBothResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalCopyOutResponse(t *testing.T) {
data := []byte(`{"Type":"CopyOutResponse", "OverallFormat": "W"}`)
want := CopyOutResponse{
OverallFormat: 'W',
}
var got CopyOutResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CopyOutResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalDataRow(t *testing.T) {
data := []byte(`{"Type":"DataRow","Values":[{"text":"abc"},{"text":"this is a test"},{"binary":"000263d3114d2e34"}]}`)
want := DataRow{
Values: [][]byte{
[]byte("abc"),
[]byte("this is a test"),
{0, 2, 99, 211, 17, 77, 46, 52},
},
}
var got DataRow
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled DataRow struct doesn't match expected value")
}
}
func TestJSONUnmarshalErrorResponse(t *testing.T) {
data := []byte(`{"Type":"ErrorResponse", "UnknownFields": {"97": "foo"}}`)
want := ErrorResponse{
UnknownFields: map[byte]string{
'a': "foo",
},
}
var got ErrorResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled ErrorResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalFunctionCallResponse(t *testing.T) {
data := []byte(`{"Type":"FunctionCallResponse"}`)
want := FunctionCallResponse{}
var got FunctionCallResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled FunctionCallResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalNoticeResponse(t *testing.T) {
data := []byte(`{"Type":"NoticeResponse", "UnknownFields": {"97": "foo"}}`)
want := NoticeResponse{
UnknownFields: map[byte]string{
'a': "foo",
},
}
var got NoticeResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled NoticeResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalNotificationResponse(t *testing.T) {
data := []byte(`{"Type":"NotificationResponse"}`)
want := NotificationResponse{}
var got NotificationResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled NotificationResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalParameterDescription(t *testing.T) {
data := []byte(`{"Type":"ParameterDescription", "ParameterOIDs": [25]}`)
want := ParameterDescription{
ParameterOIDs: []uint32{25},
}
var got ParameterDescription
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled ParameterDescription struct doesn't match expected value")
}
}
func TestJSONUnmarshalParameterStatus(t *testing.T) {
data := []byte(`{"Type":"ParameterStatus","Name":"TimeZone","Value":"Europe/Amsterdam"}`)
want := ParameterStatus{
Name: "TimeZone",
Value: "Europe/Amsterdam",
}
var got ParameterStatus
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled ParameterDescription struct doesn't match expected value")
}
}
func TestJSONUnmarshalReadyForQuery(t *testing.T) {
data := []byte(`{"Type":"ReadyForQuery","TxStatus":"I"}`)
want := ReadyForQuery{
TxStatus: 'I',
}
var got ReadyForQuery
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled ParameterDescription struct doesn't match expected value")
}
}
func TestJSONUnmarshalRowDescription(t *testing.T) {
data := []byte(`{"Type":"RowDescription","Fields":[{"Name":"generate_series","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0}]}`)
want := RowDescription{
Fields: []FieldDescription{
{
Name: []byte("generate_series"),
DataTypeOID: 23,
DataTypeSize: 4,
TypeModifier: -1,
},
},
}
var got RowDescription
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled RowDescription struct doesn't match expected value")
}
}
func TestJSONUnmarshalBind(t *testing.T) {
var testCases = []struct {
desc string
data []byte
}{
{
"textual",
[]byte(`{"Type":"Bind","DestinationPortal":"","PreparedStatement":"lrupsc_1_0","ParameterFormatCodes":[0],"Parameters":[{"text":"ABC-123"}],"ResultFormatCodes":[0,0,0,0,0,1,1]}`),
},
{
"binary",
[]byte(`{"Type":"Bind","DestinationPortal":"","PreparedStatement":"lrupsc_1_0","ParameterFormatCodes":[0],"Parameters":[{"binary":"` + hex.EncodeToString([]byte("ABC-123")) + `"}],"ResultFormatCodes":[0,0,0,0,0,1,1]}`),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
var want = Bind{
PreparedStatement: "lrupsc_1_0",
ParameterFormatCodes: []int16{0},
Parameters: [][]byte{[]byte("ABC-123")},
ResultFormatCodes: []int16{0, 0, 0, 0, 0, 1, 1},
}
var got Bind
if err := json.Unmarshal(tc.data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled Bind struct doesn't match expected value")
}
})
}
}
func TestJSONUnmarshalCancelRequest(t *testing.T) {
data := []byte(`{"Type":"CancelRequest","ProcessID":8864,"SecretKey":3641487067}`)
want := CancelRequest{
ProcessID: 8864,
SecretKey: 3641487067,
}
var got CancelRequest
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CancelRequest struct doesn't match expected value")
}
}
func TestJSONUnmarshalClose(t *testing.T) {
data := []byte(`{"Type":"Close","ObjectType":"S","Name":"abc"}`)
want := Close{
ObjectType: 'S',
Name: "abc",
}
var got Close
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled Close struct doesn't match expected value")
}
}
func TestJSONUnmarshalCopyFail(t *testing.T) {
data := []byte(`{"Type":"CopyFail","Message":"abc"}`)
want := CopyFail{
Message: "abc",
}
var got CopyFail
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled CopyFail struct doesn't match expected value")
}
}
func TestJSONUnmarshalDescribe(t *testing.T) {
data := []byte(`{"Type":"Describe","ObjectType":"S","Name":"abc"}`)
want := Describe{
ObjectType: 'S',
Name: "abc",
}
var got Describe
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled Describe struct doesn't match expected value")
}
}
func TestJSONUnmarshalExecute(t *testing.T) {
data := []byte(`{"Type":"Execute","Portal":"","MaxRows":0}`)
want := Execute{}
var got Execute
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled Execute struct doesn't match expected value")
}
}
func TestJSONUnmarshalParse(t *testing.T) {
data := []byte(`{"Type":"Parse","Name":"lrupsc_1_0","Query":"SELECT id, name FROM t WHERE id = $1","ParameterOIDs":null}`)
want := Parse{
Name: "lrupsc_1_0",
Query: "SELECT id, name FROM t WHERE id = $1",
}
var got Parse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled Parse struct doesn't match expected value")
}
}
func TestJSONUnmarshalPasswordMessage(t *testing.T) {
data := []byte(`{"Type":"PasswordMessage","Password":"abcdef"}`)
want := PasswordMessage{
Password: "abcdef",
}
var got PasswordMessage
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled PasswordMessage struct doesn't match expected value")
}
}
func TestJSONUnmarshalQuery(t *testing.T) {
data := []byte(`{"Type":"Query","String":"SELECT 1"}`)
want := Query{
String: "SELECT 1",
}
var got Query
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled Query struct doesn't match expected value")
}
}
func TestJSONUnmarshalSASLInitialResponse(t *testing.T) {
data := []byte(`{"Type":"SASLInitialResponse", "AuthMechanism":"SCRAM-SHA-256", "Data": "abc"}`)
want := SASLInitialResponse{
AuthMechanism: "SCRAM-SHA-256",
Data: []byte("abc"),
}
var got SASLInitialResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled SASLInitialResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalSASLResponse(t *testing.T) {
data := []byte(`{"Type":"SASLResponse","Data":"abc"}`)
want := SASLResponse{
Data: []byte("abc"),
}
var got SASLResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled SASLResponse struct doesn't match expected value")
}
}
func TestJSONUnmarshalStartupMessage(t *testing.T) {
data := []byte(`{"Type":"StartupMessage","ProtocolVersion":196608,"Parameters":{"database":"testing","user":"postgres"}}`)
want := StartupMessage{
ProtocolVersion: 196608,
Parameters: map[string]string{
"database": "testing",
"user": "postgres",
},
}
var got StartupMessage
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled StartupMessage struct doesn't match expected value")
}
}
func TestAuthenticationOK(t *testing.T) {
data := []byte(`{"Type":"AuthenticationOK"}`)
want := AuthenticationOk{}
var got AuthenticationOk
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationOK struct doesn't match expected value")
}
}
func TestAuthenticationCleartextPassword(t *testing.T) {
data := []byte(`{"Type":"AuthenticationCleartextPassword"}`)
want := AuthenticationCleartextPassword{}
var got AuthenticationCleartextPassword
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationCleartextPassword struct doesn't match expected value")
}
}
func TestAuthenticationMD5Password(t *testing.T) {
data := []byte(`{"Type":"AuthenticationMD5Password","Salt":[1,2,3,4]}`)
want := AuthenticationMD5Password{
Salt: [4]byte{1, 2, 3, 4},
}
var got AuthenticationMD5Password
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled AuthenticationMD5Password struct doesn't match expected value")
}
}
func TestJSONUnmarshalGSSResponse(t *testing.T) {
data := []byte(`{"Type":"GSSResponse","Data":[10,20,30,40]}`)
want := GSSResponse{Data: []byte{10, 20, 30, 40}}
var got GSSResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled GSSResponse struct doesn't match expected value")
}
}
func TestErrorResponse(t *testing.T) {
data := []byte(`{"Type":"ErrorResponse","UnknownFields":{"112":"foo"},"Code": "Fail","Position":1,"Message":"this is an error"}`)
want := ErrorResponse{
UnknownFields: map[byte]string{
'p': "foo",
},
Code: "Fail",
Position: 1,
Message: "this is an error",
}
var got ErrorResponse
if err := json.Unmarshal(data, &got); err != nil {
t.Errorf("cannot JSON unmarshal %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Error("unmarshaled ErrorResponse struct doesn't match expected value")
}
}