github.com/jackc/pgx/v5@v5.5.5/pgproto3/json_test.go (about) 1 package pgproto3 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 "reflect" 7 "testing" 8 ) 9 10 func TestJSONUnmarshalAuthenticationMD5Password(t *testing.T) { 11 data := []byte(`{"Type":"AuthenticationMD5Password", "Salt":[97,98,99,100]}`) 12 want := AuthenticationMD5Password{ 13 Salt: [4]byte{'a', 'b', 'c', 'd'}, 14 } 15 16 var got AuthenticationMD5Password 17 if err := json.Unmarshal(data, &got); err != nil { 18 t.Errorf("cannot JSON unmarshal %v", err) 19 } 20 if !reflect.DeepEqual(got, want) { 21 t.Error("unmarshaled AuthenticationMD5Password struct doesn't match expected value") 22 } 23 } 24 25 func TestJSONUnmarshalAuthenticationSASL(t *testing.T) { 26 data := []byte(`{"Type":"AuthenticationSASL","AuthMechanisms":["SCRAM-SHA-256"]}`) 27 want := AuthenticationSASL{ 28 []string{"SCRAM-SHA-256"}, 29 } 30 31 var got AuthenticationSASL 32 if err := json.Unmarshal(data, &got); err != nil { 33 t.Errorf("cannot JSON unmarshal %v", err) 34 } 35 if !reflect.DeepEqual(got, want) { 36 t.Error("unmarshaled AuthenticationSASL struct doesn't match expected value") 37 } 38 } 39 40 func TestJSONUnmarshalAuthenticationGSS(t *testing.T) { 41 data := []byte(`{"Type":"AuthenticationGSS"}`) 42 want := AuthenticationGSS{} 43 44 var got AuthenticationGSS 45 if err := json.Unmarshal(data, &got); err != nil { 46 t.Errorf("cannot JSON unmarshal %v", err) 47 } 48 if !reflect.DeepEqual(got, want) { 49 t.Error("unmarshaled AuthenticationGSS struct doesn't match expected value") 50 } 51 } 52 53 func TestJSONUnmarshalAuthenticationGSSContinue(t *testing.T) { 54 data := []byte(`{"Type":"AuthenticationGSSContinue","Data":[1,2,3,4]}`) 55 want := AuthenticationGSSContinue{Data: []byte{1, 2, 3, 4}} 56 57 var got AuthenticationGSSContinue 58 if err := json.Unmarshal(data, &got); err != nil { 59 t.Errorf("cannot JSON unmarshal %v", err) 60 } 61 if !reflect.DeepEqual(got, want) { 62 t.Error("unmarshaled AuthenticationGSSContinue struct doesn't match expected value") 63 } 64 } 65 66 func TestJSONUnmarshalAuthenticationSASLContinue(t *testing.T) { 67 data := []byte(`{"Type":"AuthenticationSASLContinue", "Data":"1"}`) 68 want := AuthenticationSASLContinue{ 69 Data: []byte{'1'}, 70 } 71 72 var got AuthenticationSASLContinue 73 if err := json.Unmarshal(data, &got); err != nil { 74 t.Errorf("cannot JSON unmarshal %v", err) 75 } 76 if !reflect.DeepEqual(got, want) { 77 t.Error("unmarshaled AuthenticationSASLContinue struct doesn't match expected value") 78 } 79 } 80 81 func TestJSONUnmarshalAuthenticationSASLFinal(t *testing.T) { 82 data := []byte(`{"Type":"AuthenticationSASLFinal", "Data":"1"}`) 83 want := AuthenticationSASLFinal{ 84 Data: []byte{'1'}, 85 } 86 87 var got AuthenticationSASLFinal 88 if err := json.Unmarshal(data, &got); err != nil { 89 t.Errorf("cannot JSON unmarshal %v", err) 90 } 91 if !reflect.DeepEqual(got, want) { 92 t.Error("unmarshaled AuthenticationSASLFinal struct doesn't match expected value") 93 } 94 } 95 96 func TestJSONUnmarshalBackendKeyData(t *testing.T) { 97 data := []byte(`{"Type":"BackendKeyData","ProcessID":8864,"SecretKey":3641487067}`) 98 want := BackendKeyData{ 99 ProcessID: 8864, 100 SecretKey: 3641487067, 101 } 102 103 var got BackendKeyData 104 if err := json.Unmarshal(data, &got); err != nil { 105 t.Errorf("cannot JSON unmarshal %v", err) 106 } 107 if !reflect.DeepEqual(got, want) { 108 t.Error("unmarshaled BackendKeyData struct doesn't match expected value") 109 } 110 } 111 112 func TestJSONUnmarshalCommandComplete(t *testing.T) { 113 data := []byte(`{"Type":"CommandComplete","CommandTag":"SELECT 1"}`) 114 want := CommandComplete{ 115 CommandTag: []byte("SELECT 1"), 116 } 117 118 var got CommandComplete 119 if err := json.Unmarshal(data, &got); err != nil { 120 t.Errorf("cannot JSON unmarshal %v", err) 121 } 122 if !reflect.DeepEqual(got, want) { 123 t.Error("unmarshaled CommandComplete struct doesn't match expected value") 124 } 125 } 126 127 func TestJSONUnmarshalCopyBothResponse(t *testing.T) { 128 data := []byte(`{"Type":"CopyBothResponse", "OverallFormat": "W"}`) 129 want := CopyBothResponse{ 130 OverallFormat: 'W', 131 } 132 133 var got CopyBothResponse 134 if err := json.Unmarshal(data, &got); err != nil { 135 t.Errorf("cannot JSON unmarshal %v", err) 136 } 137 if !reflect.DeepEqual(got, want) { 138 t.Error("unmarshaled CopyBothResponse struct doesn't match expected value") 139 } 140 } 141 142 func TestJSONUnmarshalCopyData(t *testing.T) { 143 data := []byte(`{"Type":"CopyData"}`) 144 want := CopyData{ 145 Data: []byte{}, 146 } 147 148 var got CopyData 149 if err := json.Unmarshal(data, &got); err != nil { 150 t.Errorf("cannot JSON unmarshal %v", err) 151 } 152 if !reflect.DeepEqual(got, want) { 153 t.Error("unmarshaled CopyData struct doesn't match expected value") 154 } 155 } 156 157 func TestJSONUnmarshalCopyInResponse(t *testing.T) { 158 data := []byte(`{"Type":"CopyBothResponse", "OverallFormat": "W"}`) 159 want := CopyBothResponse{ 160 OverallFormat: 'W', 161 } 162 163 var got CopyBothResponse 164 if err := json.Unmarshal(data, &got); err != nil { 165 t.Errorf("cannot JSON unmarshal %v", err) 166 } 167 if !reflect.DeepEqual(got, want) { 168 t.Error("unmarshaled CopyBothResponse struct doesn't match expected value") 169 } 170 } 171 172 func TestJSONUnmarshalCopyOutResponse(t *testing.T) { 173 data := []byte(`{"Type":"CopyOutResponse", "OverallFormat": "W"}`) 174 want := CopyOutResponse{ 175 OverallFormat: 'W', 176 } 177 178 var got CopyOutResponse 179 if err := json.Unmarshal(data, &got); err != nil { 180 t.Errorf("cannot JSON unmarshal %v", err) 181 } 182 if !reflect.DeepEqual(got, want) { 183 t.Error("unmarshaled CopyOutResponse struct doesn't match expected value") 184 } 185 } 186 187 func TestJSONUnmarshalDataRow(t *testing.T) { 188 data := []byte(`{"Type":"DataRow","Values":[{"text":"abc"},{"text":"this is a test"},{"binary":"000263d3114d2e34"}]}`) 189 want := DataRow{ 190 Values: [][]byte{ 191 []byte("abc"), 192 []byte("this is a test"), 193 {0, 2, 99, 211, 17, 77, 46, 52}, 194 }, 195 } 196 197 var got DataRow 198 if err := json.Unmarshal(data, &got); err != nil { 199 t.Errorf("cannot JSON unmarshal %v", err) 200 } 201 if !reflect.DeepEqual(got, want) { 202 t.Error("unmarshaled DataRow struct doesn't match expected value") 203 } 204 } 205 206 func TestJSONUnmarshalErrorResponse(t *testing.T) { 207 data := []byte(`{"Type":"ErrorResponse", "UnknownFields": {"97": "foo"}}`) 208 want := ErrorResponse{ 209 UnknownFields: map[byte]string{ 210 'a': "foo", 211 }, 212 } 213 214 var got ErrorResponse 215 if err := json.Unmarshal(data, &got); err != nil { 216 t.Errorf("cannot JSON unmarshal %v", err) 217 } 218 if !reflect.DeepEqual(got, want) { 219 t.Error("unmarshaled ErrorResponse struct doesn't match expected value") 220 } 221 } 222 223 func TestJSONUnmarshalFunctionCallResponse(t *testing.T) { 224 data := []byte(`{"Type":"FunctionCallResponse"}`) 225 want := FunctionCallResponse{} 226 227 var got FunctionCallResponse 228 if err := json.Unmarshal(data, &got); err != nil { 229 t.Errorf("cannot JSON unmarshal %v", err) 230 } 231 if !reflect.DeepEqual(got, want) { 232 t.Error("unmarshaled FunctionCallResponse struct doesn't match expected value") 233 } 234 } 235 236 func TestJSONUnmarshalNoticeResponse(t *testing.T) { 237 data := []byte(`{"Type":"NoticeResponse", "UnknownFields": {"97": "foo"}}`) 238 want := NoticeResponse{ 239 UnknownFields: map[byte]string{ 240 'a': "foo", 241 }, 242 } 243 244 var got NoticeResponse 245 if err := json.Unmarshal(data, &got); err != nil { 246 t.Errorf("cannot JSON unmarshal %v", err) 247 } 248 if !reflect.DeepEqual(got, want) { 249 t.Error("unmarshaled NoticeResponse struct doesn't match expected value") 250 } 251 } 252 253 func TestJSONUnmarshalNotificationResponse(t *testing.T) { 254 data := []byte(`{"Type":"NotificationResponse"}`) 255 want := NotificationResponse{} 256 257 var got NotificationResponse 258 if err := json.Unmarshal(data, &got); err != nil { 259 t.Errorf("cannot JSON unmarshal %v", err) 260 } 261 if !reflect.DeepEqual(got, want) { 262 t.Error("unmarshaled NotificationResponse struct doesn't match expected value") 263 } 264 } 265 266 func TestJSONUnmarshalParameterDescription(t *testing.T) { 267 data := []byte(`{"Type":"ParameterDescription", "ParameterOIDs": [25]}`) 268 want := ParameterDescription{ 269 ParameterOIDs: []uint32{25}, 270 } 271 272 var got ParameterDescription 273 if err := json.Unmarshal(data, &got); err != nil { 274 t.Errorf("cannot JSON unmarshal %v", err) 275 } 276 if !reflect.DeepEqual(got, want) { 277 t.Error("unmarshaled ParameterDescription struct doesn't match expected value") 278 } 279 } 280 281 func TestJSONUnmarshalParameterStatus(t *testing.T) { 282 data := []byte(`{"Type":"ParameterStatus","Name":"TimeZone","Value":"Europe/Amsterdam"}`) 283 want := ParameterStatus{ 284 Name: "TimeZone", 285 Value: "Europe/Amsterdam", 286 } 287 288 var got ParameterStatus 289 if err := json.Unmarshal(data, &got); err != nil { 290 t.Errorf("cannot JSON unmarshal %v", err) 291 } 292 if !reflect.DeepEqual(got, want) { 293 t.Error("unmarshaled ParameterDescription struct doesn't match expected value") 294 } 295 } 296 297 func TestJSONUnmarshalReadyForQuery(t *testing.T) { 298 data := []byte(`{"Type":"ReadyForQuery","TxStatus":"I"}`) 299 want := ReadyForQuery{ 300 TxStatus: 'I', 301 } 302 303 var got ReadyForQuery 304 if err := json.Unmarshal(data, &got); err != nil { 305 t.Errorf("cannot JSON unmarshal %v", err) 306 } 307 if !reflect.DeepEqual(got, want) { 308 t.Error("unmarshaled ParameterDescription struct doesn't match expected value") 309 } 310 } 311 312 func TestJSONUnmarshalRowDescription(t *testing.T) { 313 data := []byte(`{"Type":"RowDescription","Fields":[{"Name":"generate_series","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0}]}`) 314 want := RowDescription{ 315 Fields: []FieldDescription{ 316 { 317 Name: []byte("generate_series"), 318 DataTypeOID: 23, 319 DataTypeSize: 4, 320 TypeModifier: -1, 321 }, 322 }, 323 } 324 325 var got RowDescription 326 if err := json.Unmarshal(data, &got); err != nil { 327 t.Errorf("cannot JSON unmarshal %v", err) 328 } 329 if !reflect.DeepEqual(got, want) { 330 t.Error("unmarshaled RowDescription struct doesn't match expected value") 331 } 332 } 333 334 func TestJSONUnmarshalBind(t *testing.T) { 335 var testCases = []struct { 336 desc string 337 data []byte 338 }{ 339 { 340 "textual", 341 []byte(`{"Type":"Bind","DestinationPortal":"","PreparedStatement":"lrupsc_1_0","ParameterFormatCodes":[0],"Parameters":[{"text":"ABC-123"}],"ResultFormatCodes":[0,0,0,0,0,1,1]}`), 342 }, 343 { 344 "binary", 345 []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]}`), 346 }, 347 } 348 349 for _, tc := range testCases { 350 t.Run(tc.desc, func(t *testing.T) { 351 var want = Bind{ 352 PreparedStatement: "lrupsc_1_0", 353 ParameterFormatCodes: []int16{0}, 354 Parameters: [][]byte{[]byte("ABC-123")}, 355 ResultFormatCodes: []int16{0, 0, 0, 0, 0, 1, 1}, 356 } 357 358 var got Bind 359 if err := json.Unmarshal(tc.data, &got); err != nil { 360 t.Errorf("cannot JSON unmarshal %v", err) 361 } 362 if !reflect.DeepEqual(got, want) { 363 t.Error("unmarshaled Bind struct doesn't match expected value") 364 } 365 }) 366 } 367 } 368 369 func TestJSONUnmarshalCancelRequest(t *testing.T) { 370 data := []byte(`{"Type":"CancelRequest","ProcessID":8864,"SecretKey":3641487067}`) 371 want := CancelRequest{ 372 ProcessID: 8864, 373 SecretKey: 3641487067, 374 } 375 376 var got CancelRequest 377 if err := json.Unmarshal(data, &got); err != nil { 378 t.Errorf("cannot JSON unmarshal %v", err) 379 } 380 if !reflect.DeepEqual(got, want) { 381 t.Error("unmarshaled CancelRequest struct doesn't match expected value") 382 } 383 } 384 385 func TestJSONUnmarshalClose(t *testing.T) { 386 data := []byte(`{"Type":"Close","ObjectType":"S","Name":"abc"}`) 387 want := Close{ 388 ObjectType: 'S', 389 Name: "abc", 390 } 391 392 var got Close 393 if err := json.Unmarshal(data, &got); err != nil { 394 t.Errorf("cannot JSON unmarshal %v", err) 395 } 396 if !reflect.DeepEqual(got, want) { 397 t.Error("unmarshaled Close struct doesn't match expected value") 398 } 399 } 400 401 func TestJSONUnmarshalCopyFail(t *testing.T) { 402 data := []byte(`{"Type":"CopyFail","Message":"abc"}`) 403 want := CopyFail{ 404 Message: "abc", 405 } 406 407 var got CopyFail 408 if err := json.Unmarshal(data, &got); err != nil { 409 t.Errorf("cannot JSON unmarshal %v", err) 410 } 411 if !reflect.DeepEqual(got, want) { 412 t.Error("unmarshaled CopyFail struct doesn't match expected value") 413 } 414 } 415 416 func TestJSONUnmarshalDescribe(t *testing.T) { 417 data := []byte(`{"Type":"Describe","ObjectType":"S","Name":"abc"}`) 418 want := Describe{ 419 ObjectType: 'S', 420 Name: "abc", 421 } 422 423 var got Describe 424 if err := json.Unmarshal(data, &got); err != nil { 425 t.Errorf("cannot JSON unmarshal %v", err) 426 } 427 if !reflect.DeepEqual(got, want) { 428 t.Error("unmarshaled Describe struct doesn't match expected value") 429 } 430 } 431 432 func TestJSONUnmarshalExecute(t *testing.T) { 433 data := []byte(`{"Type":"Execute","Portal":"","MaxRows":0}`) 434 want := Execute{} 435 436 var got Execute 437 if err := json.Unmarshal(data, &got); err != nil { 438 t.Errorf("cannot JSON unmarshal %v", err) 439 } 440 if !reflect.DeepEqual(got, want) { 441 t.Error("unmarshaled Execute struct doesn't match expected value") 442 } 443 } 444 445 func TestJSONUnmarshalParse(t *testing.T) { 446 data := []byte(`{"Type":"Parse","Name":"lrupsc_1_0","Query":"SELECT id, name FROM t WHERE id = $1","ParameterOIDs":null}`) 447 want := Parse{ 448 Name: "lrupsc_1_0", 449 Query: "SELECT id, name FROM t WHERE id = $1", 450 } 451 452 var got Parse 453 if err := json.Unmarshal(data, &got); err != nil { 454 t.Errorf("cannot JSON unmarshal %v", err) 455 } 456 if !reflect.DeepEqual(got, want) { 457 t.Error("unmarshaled Parse struct doesn't match expected value") 458 } 459 } 460 461 func TestJSONUnmarshalPasswordMessage(t *testing.T) { 462 data := []byte(`{"Type":"PasswordMessage","Password":"abcdef"}`) 463 want := PasswordMessage{ 464 Password: "abcdef", 465 } 466 467 var got PasswordMessage 468 if err := json.Unmarshal(data, &got); err != nil { 469 t.Errorf("cannot JSON unmarshal %v", err) 470 } 471 if !reflect.DeepEqual(got, want) { 472 t.Error("unmarshaled PasswordMessage struct doesn't match expected value") 473 } 474 } 475 476 func TestJSONUnmarshalQuery(t *testing.T) { 477 data := []byte(`{"Type":"Query","String":"SELECT 1"}`) 478 want := Query{ 479 String: "SELECT 1", 480 } 481 482 var got Query 483 if err := json.Unmarshal(data, &got); err != nil { 484 t.Errorf("cannot JSON unmarshal %v", err) 485 } 486 if !reflect.DeepEqual(got, want) { 487 t.Error("unmarshaled Query struct doesn't match expected value") 488 } 489 } 490 491 func TestJSONUnmarshalSASLInitialResponse(t *testing.T) { 492 data := []byte(`{"Type":"SASLInitialResponse", "AuthMechanism":"SCRAM-SHA-256", "Data": "6D"}`) 493 want := SASLInitialResponse{ 494 AuthMechanism: "SCRAM-SHA-256", 495 Data: []byte{109}, 496 } 497 498 var got SASLInitialResponse 499 if err := json.Unmarshal(data, &got); err != nil { 500 t.Errorf("cannot JSON unmarshal %v", err) 501 } 502 if !reflect.DeepEqual(got, want) { 503 t.Error("unmarshaled SASLInitialResponse struct doesn't match expected value") 504 } 505 } 506 507 func TestJSONUnmarshalSASLResponse(t *testing.T) { 508 data := []byte(`{"Type":"SASLResponse","Message":"abc"}`) 509 want := SASLResponse{} 510 511 var got SASLResponse 512 if err := json.Unmarshal(data, &got); err != nil { 513 t.Errorf("cannot JSON unmarshal %v", err) 514 } 515 if !reflect.DeepEqual(got, want) { 516 t.Error("unmarshaled SASLResponse struct doesn't match expected value") 517 } 518 } 519 520 func TestJSONUnmarshalStartupMessage(t *testing.T) { 521 data := []byte(`{"Type":"StartupMessage","ProtocolVersion":196608,"Parameters":{"database":"testing","user":"postgres"}}`) 522 want := StartupMessage{ 523 ProtocolVersion: 196608, 524 Parameters: map[string]string{ 525 "database": "testing", 526 "user": "postgres", 527 }, 528 } 529 530 var got StartupMessage 531 if err := json.Unmarshal(data, &got); err != nil { 532 t.Errorf("cannot JSON unmarshal %v", err) 533 } 534 if !reflect.DeepEqual(got, want) { 535 t.Error("unmarshaled StartupMessage struct doesn't match expected value") 536 } 537 } 538 539 func TestAuthenticationOK(t *testing.T) { 540 data := []byte(`{"Type":"AuthenticationOK"}`) 541 want := AuthenticationOk{} 542 543 var got AuthenticationOk 544 if err := json.Unmarshal(data, &got); err != nil { 545 t.Errorf("cannot JSON unmarshal %v", err) 546 } 547 if !reflect.DeepEqual(got, want) { 548 t.Error("unmarshaled AuthenticationOK struct doesn't match expected value") 549 } 550 } 551 552 func TestAuthenticationCleartextPassword(t *testing.T) { 553 data := []byte(`{"Type":"AuthenticationCleartextPassword"}`) 554 want := AuthenticationCleartextPassword{} 555 556 var got AuthenticationCleartextPassword 557 if err := json.Unmarshal(data, &got); err != nil { 558 t.Errorf("cannot JSON unmarshal %v", err) 559 } 560 if !reflect.DeepEqual(got, want) { 561 t.Error("unmarshaled AuthenticationCleartextPassword struct doesn't match expected value") 562 } 563 } 564 565 func TestAuthenticationMD5Password(t *testing.T) { 566 data := []byte(`{"Type":"AuthenticationMD5Password","Salt":[1,2,3,4]}`) 567 want := AuthenticationMD5Password{ 568 Salt: [4]byte{1, 2, 3, 4}, 569 } 570 571 var got AuthenticationMD5Password 572 if err := json.Unmarshal(data, &got); err != nil { 573 t.Errorf("cannot JSON unmarshal %v", err) 574 } 575 if !reflect.DeepEqual(got, want) { 576 t.Error("unmarshaled AuthenticationMD5Password struct doesn't match expected value") 577 } 578 } 579 580 func TestJSONUnmarshalGSSResponse(t *testing.T) { 581 data := []byte(`{"Type":"GSSResponse","Data":[10,20,30,40]}`) 582 want := GSSResponse{Data: []byte{10, 20, 30, 40}} 583 584 var got GSSResponse 585 if err := json.Unmarshal(data, &got); err != nil { 586 t.Errorf("cannot JSON unmarshal %v", err) 587 } 588 if !reflect.DeepEqual(got, want) { 589 t.Error("unmarshaled GSSResponse struct doesn't match expected value") 590 } 591 } 592 593 func TestErrorResponse(t *testing.T) { 594 data := []byte(`{"Type":"ErrorResponse","UnknownFields":{"112":"foo"},"Code": "Fail","Position":1,"Message":"this is an error"}`) 595 want := ErrorResponse{ 596 UnknownFields: map[byte]string{ 597 'p': "foo", 598 }, 599 Code: "Fail", 600 Position: 1, 601 Message: "this is an error", 602 } 603 604 var got ErrorResponse 605 if err := json.Unmarshal(data, &got); err != nil { 606 t.Errorf("cannot JSON unmarshal %v", err) 607 } 608 if !reflect.DeepEqual(got, want) { 609 t.Error("unmarshaled ErrorResponse struct doesn't match expected value") 610 } 611 }