github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/util/proto/marshal_test.go (about) 1 package proto_test 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/TrueCloudLab/frostfs-api-go/v2/util/proto" 8 "github.com/TrueCloudLab/frostfs-api-go/v2/util/proto/test" 9 "github.com/stretchr/testify/require" 10 goproto "google.golang.org/protobuf/proto" 11 ) 12 13 type SomeEnum int32 14 15 type stablePrimitives struct { 16 FieldA []byte 17 FieldB string 18 FieldC bool 19 FieldD int32 20 FieldE uint32 21 FieldF int64 22 FieldG uint64 23 FieldH SomeEnum 24 FieldI uint64 // fixed64 25 FieldJ float64 26 FieldK uint32 // fixed32 27 } 28 29 type stableRepPrimitives struct { 30 FieldA [][]byte 31 FieldB []string 32 FieldC []int32 33 FieldD []uint32 34 FieldE []int64 35 FieldF []uint64 36 } 37 38 const ( 39 ENUM_UNKNOWN SomeEnum = 0 40 ENUM_POSITIVE = 1 41 ENUM_NEGATIVE = -1 42 ) 43 44 func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, error) { 45 if s == nil { 46 return []byte{}, nil 47 } 48 49 if buf == nil { 50 buf = make([]byte, s.stableSize()) 51 } 52 53 var ( 54 i, offset, fieldNum int 55 ) 56 57 fieldNum = 1 58 if wrongField { 59 fieldNum++ 60 } 61 i += proto.BytesMarshal(fieldNum, buf, s.FieldA) 62 63 fieldNum = 2 64 if wrongField { 65 fieldNum++ 66 } 67 i += proto.StringMarshal(fieldNum, buf, s.FieldB) 68 69 fieldNum = 200 70 if wrongField { 71 fieldNum++ 72 } 73 i += proto.BoolMarshal(fieldNum, buf, s.FieldC) 74 75 fieldNum = 201 76 if wrongField { 77 fieldNum++ 78 } 79 i += proto.Int32Marshal(fieldNum, buf, s.FieldD) 80 81 fieldNum = 202 82 if wrongField { 83 fieldNum++ 84 } 85 i += proto.UInt32Marshal(fieldNum, buf, s.FieldE) 86 87 fieldNum = 203 88 if wrongField { 89 fieldNum++ 90 } 91 i += proto.Int64Marshal(fieldNum, buf, s.FieldF) 92 93 fieldNum = 204 94 if wrongField { 95 fieldNum++ 96 } 97 i += proto.UInt64Marshal(fieldNum, buf, s.FieldG) 98 99 fieldNum = 205 100 if wrongField { 101 fieldNum++ 102 } 103 i += proto.Fixed64Marshal(fieldNum, buf, s.FieldI) 104 105 fieldNum = 206 106 if wrongField { 107 fieldNum++ 108 } 109 i += proto.Float64Marshal(fieldNum, buf, s.FieldJ) 110 111 fieldNum = 207 112 if wrongField { 113 fieldNum++ 114 } 115 116 offset = proto.Fixed32Marshal(fieldNum, buf, s.FieldK) 117 118 i += offset 119 120 fieldNum = 300 121 if wrongField { 122 fieldNum++ 123 } 124 i += proto.EnumMarshal(fieldNum, buf, int32(s.FieldH)) 125 126 return buf, nil 127 } 128 129 func (s *stablePrimitives) stableSize() int { 130 return proto.BytesSize(1, s.FieldA) + 131 proto.StringSize(2, s.FieldB) + 132 proto.BoolSize(200, s.FieldC) + 133 proto.Int32Size(201, s.FieldD) + 134 proto.UInt32Size(202, s.FieldE) + 135 proto.Int64Size(203, s.FieldF) + 136 proto.UInt64Size(204, s.FieldG) + 137 proto.Fixed64Size(205, s.FieldI) + 138 proto.Float64Size(206, s.FieldJ) + 139 proto.Fixed32Size(207, s.FieldK) + 140 proto.EnumSize(300, int32(s.FieldH)) 141 } 142 143 func (s *stableRepPrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, error) { 144 if s == nil { 145 return []byte{}, nil 146 } 147 148 if buf == nil { 149 buf = make([]byte, s.stableSize()) 150 } 151 152 var i, fieldNum int 153 154 fieldNum = 1 155 if wrongField { 156 fieldNum++ 157 } 158 i += proto.RepeatedBytesMarshal(fieldNum, buf, s.FieldA) 159 160 fieldNum = 2 161 if wrongField { 162 fieldNum++ 163 } 164 i += proto.RepeatedStringMarshal(fieldNum, buf, s.FieldB) 165 166 fieldNum = 3 167 if wrongField { 168 fieldNum++ 169 } 170 i += proto.RepeatedInt32Marshal(fieldNum, buf, s.FieldC) 171 172 fieldNum = 4 173 if wrongField { 174 fieldNum++ 175 } 176 i += proto.RepeatedUInt32Marshal(fieldNum, buf, s.FieldD) 177 178 fieldNum = 5 179 if wrongField { 180 fieldNum++ 181 } 182 i += proto.RepeatedInt64Marshal(fieldNum, buf, s.FieldE) 183 184 fieldNum = 6 185 if wrongField { 186 fieldNum++ 187 } 188 i += proto.RepeatedUInt64Marshal(fieldNum, buf, s.FieldF) 189 190 return buf, nil 191 } 192 193 func (s *stableRepPrimitives) stableSize() int { 194 f1 := proto.RepeatedBytesSize(1, s.FieldA) 195 f2 := proto.RepeatedStringSize(2, s.FieldB) 196 f3, _ := proto.RepeatedInt32Size(3, s.FieldC) 197 f4, _ := proto.RepeatedUInt32Size(4, s.FieldD) 198 f5, _ := proto.RepeatedInt64Size(5, s.FieldE) 199 f6, _ := proto.RepeatedUInt64Size(6, s.FieldF) 200 201 return f1 + f2 + f3 + f4 + f5 + f6 202 } 203 204 func TestBytesMarshal(t *testing.T) { 205 t.Run("not empty", func(t *testing.T) { 206 data := []byte("Hello World") 207 testBytesMarshal(t, data, false) 208 testBytesMarshal(t, data, true) 209 }) 210 211 t.Run("empty", func(t *testing.T) { 212 testBytesMarshal(t, []byte{}, false) 213 }) 214 215 t.Run("nil", func(t *testing.T) { 216 testBytesMarshal(t, nil, false) 217 }) 218 } 219 220 func TestStringMarshal(t *testing.T) { 221 t.Run("not empty", func(t *testing.T) { 222 data := "Hello World" 223 testStringMarshal(t, data, false) 224 testStringMarshal(t, data, true) 225 }) 226 227 t.Run("empty", func(t *testing.T) { 228 testStringMarshal(t, "", false) 229 }) 230 } 231 232 func TestBoolMarshal(t *testing.T) { 233 t.Run("true", func(t *testing.T) { 234 testBoolMarshal(t, true, false) 235 testBoolMarshal(t, true, true) 236 }) 237 238 t.Run("false", func(t *testing.T) { 239 testBoolMarshal(t, false, false) 240 }) 241 } 242 243 func TestInt32Marshal(t *testing.T) { 244 t.Run("zero", func(t *testing.T) { 245 testInt32Marshal(t, 0, false) 246 }) 247 248 t.Run("positive", func(t *testing.T) { 249 testInt32Marshal(t, math.MaxInt32, false) 250 testInt32Marshal(t, math.MaxInt32, true) 251 }) 252 253 t.Run("negative", func(t *testing.T) { 254 testInt32Marshal(t, math.MinInt32, false) 255 testInt32Marshal(t, math.MinInt32, true) 256 }) 257 } 258 259 func TestUInt32Marshal(t *testing.T) { 260 t.Run("zero", func(t *testing.T) { 261 testUInt32Marshal(t, 0, false) 262 }) 263 264 t.Run("non zero", func(t *testing.T) { 265 testUInt32Marshal(t, math.MaxUint32, false) 266 testUInt32Marshal(t, math.MaxUint32, true) 267 }) 268 } 269 270 func TestInt64Marshal(t *testing.T) { 271 t.Run("zero", func(t *testing.T) { 272 testInt32Marshal(t, 0, false) 273 }) 274 275 t.Run("positive", func(t *testing.T) { 276 testInt64Marshal(t, math.MaxInt64, false) 277 testInt64Marshal(t, math.MaxInt64, true) 278 }) 279 280 t.Run("negative", func(t *testing.T) { 281 testInt64Marshal(t, math.MinInt64, false) 282 testInt64Marshal(t, math.MinInt64, true) 283 }) 284 } 285 286 func TestUInt64Marshal(t *testing.T) { 287 t.Run("zero", func(t *testing.T) { 288 testUInt64Marshal(t, 0, false) 289 }) 290 291 t.Run("non zero", func(t *testing.T) { 292 testUInt64Marshal(t, math.MaxUint64, false) 293 testUInt64Marshal(t, math.MaxUint64, true) 294 }) 295 } 296 297 func TestEnumMarshal(t *testing.T) { 298 testEnumMarshal(t, ENUM_UNKNOWN, false) 299 testEnumMarshal(t, ENUM_POSITIVE, false) 300 testEnumMarshal(t, ENUM_POSITIVE, true) 301 testEnumMarshal(t, ENUM_NEGATIVE, false) 302 testEnumMarshal(t, ENUM_NEGATIVE, true) 303 } 304 305 func TestRepeatedBytesMarshal(t *testing.T) { 306 t.Run("not empty", func(t *testing.T) { 307 data := [][]byte{[]byte("One"), []byte("Two"), []byte("Three")} 308 testRepeatedBytesMarshal(t, data, false) 309 testRepeatedBytesMarshal(t, data, true) 310 }) 311 312 t.Run("empty", func(t *testing.T) { 313 testRepeatedBytesMarshal(t, [][]byte{}, false) 314 }) 315 316 t.Run("empty element", func(t *testing.T) { 317 testRepeatedBytesMarshal(t, [][]byte{{1}, {}}, false) 318 }) 319 320 t.Run("nil", func(t *testing.T) { 321 testRepeatedBytesMarshal(t, nil, false) 322 }) 323 } 324 325 func TestRepeatedStringMarshal(t *testing.T) { 326 t.Run("not empty", func(t *testing.T) { 327 data := []string{"One", "Two", "Three"} 328 testRepeatedStringMarshal(t, data, false) 329 testRepeatedStringMarshal(t, data, true) 330 }) 331 332 t.Run("empty element", func(t *testing.T) { 333 testRepeatedStringMarshal(t, []string{""}, false) 334 }) 335 336 t.Run("empty", func(t *testing.T) { 337 testRepeatedStringMarshal(t, []string{}, false) 338 }) 339 340 t.Run("nil", func(t *testing.T) { 341 testRepeatedStringMarshal(t, nil, false) 342 }) 343 } 344 345 func TestRepeatedInt32Marshal(t *testing.T) { 346 t.Run("not empty", func(t *testing.T) { 347 data := []int32{-1, 0, 1, 2, 3, 4, 5} 348 testRepeatedInt32Marshal(t, data, false) 349 testRepeatedInt32Marshal(t, data, true) 350 }) 351 352 t.Run("empty", func(t *testing.T) { 353 testRepeatedInt32Marshal(t, []int32{}, false) 354 }) 355 356 t.Run("nil", func(t *testing.T) { 357 testRepeatedInt32Marshal(t, nil, false) 358 }) 359 } 360 361 func TestRepeatedUInt32Marshal(t *testing.T) { 362 t.Run("not empty", func(t *testing.T) { 363 data := []uint32{0, 1, 2, 3, 4, 5} 364 testRepeatedUInt32Marshal(t, data, false) 365 testRepeatedUInt32Marshal(t, data, true) 366 }) 367 368 t.Run("empty", func(t *testing.T) { 369 testRepeatedUInt32Marshal(t, []uint32{}, false) 370 }) 371 372 t.Run("nil", func(t *testing.T) { 373 testRepeatedUInt32Marshal(t, nil, false) 374 }) 375 } 376 377 func TestRepeatedInt64Marshal(t *testing.T) { 378 t.Run("not empty", func(t *testing.T) { 379 data := []int64{-1, 0, 1, 2, 3, 4, 5} 380 testRepeatedInt64Marshal(t, data, false) 381 testRepeatedInt64Marshal(t, data, true) 382 }) 383 384 t.Run("empty", func(t *testing.T) { 385 testRepeatedInt64Marshal(t, []int64{}, false) 386 }) 387 388 t.Run("nil", func(t *testing.T) { 389 testRepeatedInt64Marshal(t, nil, false) 390 }) 391 } 392 393 func TestRepeatedUInt64Marshal(t *testing.T) { 394 t.Run("not empty", func(t *testing.T) { 395 data := []uint64{0, 1, 2, 3, 4, 5} 396 testRepeatedUInt64Marshal(t, data, false) 397 testRepeatedUInt64Marshal(t, data, true) 398 }) 399 400 t.Run("empty", func(t *testing.T) { 401 testRepeatedUInt64Marshal(t, []uint64{}, false) 402 }) 403 404 t.Run("nil", func(t *testing.T) { 405 testRepeatedUInt64Marshal(t, nil, false) 406 }) 407 } 408 409 func TestFixed64Marshal(t *testing.T) { 410 t.Run("zero", func(t *testing.T) { 411 testFixed64Marshal(t, 0, false) 412 }) 413 414 t.Run("non zero", func(t *testing.T) { 415 testFixed64Marshal(t, math.MaxUint64, false) 416 testFixed64Marshal(t, math.MaxUint64, true) 417 }) 418 } 419 420 func TestFloat64Marshal(t *testing.T) { 421 t.Run("zero", func(t *testing.T) { 422 testFloat64Marshal(t, 0, false) 423 }) 424 425 t.Run("non zero", func(t *testing.T) { 426 f := math.Float64frombits(12345677890) 427 428 testFloat64Marshal(t, f, false) 429 testFloat64Marshal(t, f, true) 430 }) 431 } 432 433 func TestFixed32Marshal(t *testing.T) { 434 t.Run("zero", func(t *testing.T) { 435 testFixed32Marshal(t, 0, false) 436 }) 437 438 t.Run("non zero", func(t *testing.T) { 439 testFixed32Marshal(t, math.MaxUint32, false) 440 testFixed32Marshal(t, math.MaxUint32, true) 441 }) 442 } 443 444 func testMarshal(t *testing.T, c stablePrimitives, tr test.Primitives, wrongField bool) *test.Primitives { 445 var ( 446 wire []byte 447 err error 448 ) 449 wire, err = c.stableMarshal(nil, wrongField) 450 require.NoError(t, err) 451 452 wireGen, err := goproto.Marshal(&tr) 453 require.NoError(t, err) 454 455 if !wrongField { 456 // we can check equality because single field cannot be unstable marshalled 457 require.Equal(t, wireGen, wire) 458 } else { 459 require.NotEqual(t, wireGen, wire) 460 } 461 462 result := new(test.Primitives) 463 err = goproto.Unmarshal(wire, result) 464 require.NoError(t, err) 465 466 return result 467 } 468 469 func testBytesMarshal(t *testing.T, data []byte, wrongField bool) { 470 var ( 471 custom = stablePrimitives{FieldA: data} 472 transport = test.Primitives{FieldA: data} 473 ) 474 475 result := testMarshal(t, custom, transport, wrongField) 476 477 if !wrongField { 478 require.Len(t, result.FieldA, len(data)) 479 if len(data) > 0 { 480 require.Equal(t, data, result.FieldA) 481 } 482 } else { 483 require.Len(t, result.FieldA, 0) 484 } 485 } 486 487 func testStringMarshal(t *testing.T, s string, wrongField bool) { 488 var ( 489 custom = stablePrimitives{FieldB: s} 490 transport = test.Primitives{FieldB: s} 491 ) 492 493 result := testMarshal(t, custom, transport, wrongField) 494 495 if !wrongField { 496 require.Len(t, result.FieldB, len(s)) 497 if len(s) > 0 { 498 require.Equal(t, s, result.FieldB) 499 } 500 } else { 501 require.Len(t, result.FieldB, 0) 502 } 503 } 504 505 func testBoolMarshal(t *testing.T, b bool, wrongField bool) { 506 var ( 507 custom = stablePrimitives{FieldC: b} 508 transport = test.Primitives{FieldC: b} 509 ) 510 511 result := testMarshal(t, custom, transport, wrongField) 512 513 if !wrongField { 514 require.Equal(t, b, result.FieldC) 515 } else { 516 require.False(t, false, result.FieldC) 517 } 518 } 519 520 func testInt32Marshal(t *testing.T, n int32, wrongField bool) { 521 var ( 522 custom = stablePrimitives{FieldD: n} 523 transport = test.Primitives{FieldD: n} 524 ) 525 526 result := testMarshal(t, custom, transport, wrongField) 527 528 if !wrongField { 529 require.Equal(t, n, result.FieldD) 530 } else { 531 require.EqualValues(t, 0, result.FieldD) 532 } 533 } 534 535 func testUInt32Marshal(t *testing.T, n uint32, wrongField bool) { 536 var ( 537 custom = stablePrimitives{FieldE: n} 538 transport = test.Primitives{FieldE: n} 539 ) 540 541 result := testMarshal(t, custom, transport, wrongField) 542 543 if !wrongField { 544 require.Equal(t, n, result.FieldE) 545 } else { 546 require.EqualValues(t, 0, result.FieldE) 547 } 548 } 549 550 func testInt64Marshal(t *testing.T, n int64, wrongField bool) { 551 var ( 552 custom = stablePrimitives{FieldF: n} 553 transport = test.Primitives{FieldF: n} 554 ) 555 556 result := testMarshal(t, custom, transport, wrongField) 557 558 if !wrongField { 559 require.Equal(t, n, result.FieldF) 560 } else { 561 require.EqualValues(t, 0, result.FieldF) 562 } 563 } 564 565 func testUInt64Marshal(t *testing.T, n uint64, wrongField bool) { 566 var ( 567 custom = stablePrimitives{FieldG: n} 568 transport = test.Primitives{FieldG: n} 569 ) 570 571 result := testMarshal(t, custom, transport, wrongField) 572 573 if !wrongField { 574 require.Equal(t, n, result.FieldG) 575 } else { 576 require.EqualValues(t, 0, result.FieldG) 577 } 578 } 579 580 func testFloat64Marshal(t *testing.T, n float64, wrongField bool) { 581 var ( 582 custom = stablePrimitives{FieldJ: n} 583 transport = test.Primitives{FieldJ: n} 584 ) 585 586 result := testMarshal(t, custom, transport, wrongField) 587 588 if !wrongField { 589 require.Equal(t, n, result.FieldJ) 590 } else { 591 require.EqualValues(t, 0, result.FieldJ) 592 } 593 } 594 595 func testEnumMarshal(t *testing.T, e SomeEnum, wrongField bool) { 596 var ( 597 custom = stablePrimitives{FieldH: e} 598 transport = test.Primitives{FieldH: test.Primitives_SomeEnum(e)} 599 ) 600 601 result := testMarshal(t, custom, transport, wrongField) 602 603 if !wrongField { 604 require.EqualValues(t, custom.FieldH, result.FieldH) 605 } else { 606 require.EqualValues(t, 0, result.FieldH) 607 } 608 } 609 610 func testRepMarshal(t *testing.T, c stableRepPrimitives, tr test.RepPrimitives, wrongField bool) *test.RepPrimitives { 611 var ( 612 wire []byte 613 err error 614 ) 615 wire, err = c.stableMarshal(nil, wrongField) 616 require.NoError(t, err) 617 618 wireGen, err := goproto.Marshal(&tr) 619 require.NoError(t, err) 620 621 if !wrongField { 622 // we can check equality because single field cannot be unstable marshalled 623 require.Equal(t, wireGen, wire) 624 } else { 625 require.NotEqual(t, wireGen, wire) 626 } 627 628 result := new(test.RepPrimitives) 629 err = goproto.Unmarshal(wire, result) 630 require.NoError(t, err) 631 632 return result 633 } 634 635 func testRepeatedBytesMarshal(t *testing.T, data [][]byte, wrongField bool) { 636 var ( 637 custom = stableRepPrimitives{FieldA: data} 638 transport = test.RepPrimitives{FieldA: data} 639 ) 640 641 result := testRepMarshal(t, custom, transport, wrongField) 642 643 if !wrongField { 644 require.Len(t, result.FieldA, len(data)) 645 if len(data) > 0 { 646 require.Equal(t, data, result.FieldA) 647 } 648 } else { 649 require.Len(t, result.FieldA, 0) 650 } 651 } 652 653 func testRepeatedStringMarshal(t *testing.T, s []string, wrongField bool) { 654 var ( 655 custom = stableRepPrimitives{FieldB: s} 656 transport = test.RepPrimitives{FieldB: s} 657 ) 658 659 result := testRepMarshal(t, custom, transport, wrongField) 660 661 if !wrongField { 662 require.Len(t, result.FieldB, len(s)) 663 if len(s) > 0 { 664 require.Equal(t, s, result.FieldB) 665 } 666 } else { 667 require.Len(t, result.FieldB, 0) 668 } 669 } 670 671 func testRepeatedInt32Marshal(t *testing.T, n []int32, wrongField bool) { 672 var ( 673 custom = stableRepPrimitives{FieldC: n} 674 transport = test.RepPrimitives{FieldC: n} 675 ) 676 677 result := testRepMarshal(t, custom, transport, wrongField) 678 679 if !wrongField { 680 require.Len(t, result.FieldC, len(n)) 681 if len(n) > 0 { 682 require.Equal(t, n, result.FieldC) 683 } 684 } else { 685 require.Len(t, result.FieldC, 0) 686 } 687 } 688 689 func testRepeatedUInt32Marshal(t *testing.T, n []uint32, wrongField bool) { 690 var ( 691 custom = stableRepPrimitives{FieldD: n} 692 transport = test.RepPrimitives{FieldD: n} 693 ) 694 695 result := testRepMarshal(t, custom, transport, wrongField) 696 697 if !wrongField { 698 require.Len(t, result.FieldD, len(n)) 699 if len(n) > 0 { 700 require.Equal(t, n, result.FieldD) 701 } 702 } else { 703 require.Len(t, result.FieldD, 0) 704 } 705 } 706 707 func testRepeatedInt64Marshal(t *testing.T, n []int64, wrongField bool) { 708 var ( 709 custom = stableRepPrimitives{FieldE: n} 710 transport = test.RepPrimitives{FieldE: n} 711 ) 712 713 result := testRepMarshal(t, custom, transport, wrongField) 714 715 if !wrongField { 716 require.Len(t, result.FieldE, len(n)) 717 if len(n) > 0 { 718 require.Equal(t, n, result.FieldE) 719 } 720 } else { 721 require.Len(t, result.FieldE, 0) 722 } 723 } 724 725 func testRepeatedUInt64Marshal(t *testing.T, n []uint64, wrongField bool) { 726 var ( 727 custom = stableRepPrimitives{FieldF: n} 728 transport = test.RepPrimitives{FieldF: n} 729 ) 730 731 result := testRepMarshal(t, custom, transport, wrongField) 732 733 if !wrongField { 734 require.Len(t, result.FieldF, len(n)) 735 if len(n) > 0 { 736 require.Equal(t, n, result.FieldF) 737 } 738 } else { 739 require.Len(t, result.FieldF, 0) 740 } 741 } 742 743 func testFixed64Marshal(t *testing.T, n uint64, wrongField bool) { 744 var ( 745 custom = stablePrimitives{FieldI: n} 746 transport = test.Primitives{FieldI: n} 747 ) 748 749 result := testMarshal(t, custom, transport, wrongField) 750 751 if !wrongField { 752 require.Equal(t, n, result.FieldI) 753 } else { 754 require.EqualValues(t, 0, result.FieldI) 755 } 756 } 757 758 func testFixed32Marshal(t *testing.T, n uint32, wrongField bool) { 759 var ( 760 custom = stablePrimitives{FieldK: n} 761 transport = test.Primitives{FieldK: n} 762 ) 763 764 result := testMarshal(t, custom, transport, wrongField) 765 766 if !wrongField { 767 require.Equal(t, n, result.FieldK) 768 } else { 769 require.EqualValues(t, 0, result.FieldK) 770 } 771 }