encode_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package proto_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. protoV1 "github.com/golang/protobuf/proto"
  8. "github.com/golang/protobuf/v2/proto"
  9. "github.com/google/go-cmp/cmp"
  10. test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
  11. )
  12. func TestEncode(t *testing.T) {
  13. for _, test := range testProtos {
  14. for _, want := range test.decodeTo {
  15. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  16. opts := proto.MarshalOptions{
  17. AllowPartial: test.partial,
  18. }
  19. wire, err := opts.Marshal(want)
  20. if err != nil {
  21. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  22. }
  23. size := proto.Size(want)
  24. if size != len(wire) {
  25. t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
  26. }
  27. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  28. uopts := proto.UnmarshalOptions{
  29. AllowPartial: test.partial,
  30. }
  31. if err := uopts.Unmarshal(wire, got); err != nil {
  32. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
  33. return
  34. }
  35. if test.invalidExtensions {
  36. // Equal doesn't work on messages containing invalid extension data.
  37. return
  38. }
  39. if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
  40. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
  41. }
  42. })
  43. }
  44. }
  45. }
  46. func TestEncodeDeterministic(t *testing.T) {
  47. for _, test := range testProtos {
  48. for _, want := range test.decodeTo {
  49. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  50. opts := proto.MarshalOptions{
  51. Deterministic: true,
  52. AllowPartial: test.partial,
  53. }
  54. wire, err := opts.Marshal(want)
  55. if err != nil {
  56. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  57. }
  58. wire2, err := opts.Marshal(want)
  59. if err != nil {
  60. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  61. }
  62. if !bytes.Equal(wire, wire2) {
  63. t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
  64. }
  65. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  66. uopts := proto.UnmarshalOptions{
  67. AllowPartial: test.partial,
  68. }
  69. if err := uopts.Unmarshal(wire, got); err != nil {
  70. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  71. return
  72. }
  73. if test.invalidExtensions {
  74. // Equal doesn't work on messages containing invalid extension data.
  75. return
  76. }
  77. if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
  78. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  79. }
  80. })
  81. }
  82. }
  83. }
  84. func TestEncodeRequiredFieldChecks(t *testing.T) {
  85. for _, test := range testProtos {
  86. if !test.partial {
  87. continue
  88. }
  89. for _, m := range test.decodeTo {
  90. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  91. _, err := proto.Marshal(m)
  92. if err == nil {
  93. t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
  94. }
  95. })
  96. }
  97. }
  98. }
  99. func TestMarshalAppend(t *testing.T) {
  100. want := []byte("prefix")
  101. got := append([]byte(nil), want...)
  102. got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
  103. OptionalString: "value",
  104. })
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. if !bytes.HasPrefix(got, want) {
  109. t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
  110. }
  111. }