encode_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "bytes"
  7. "fmt"
  8. "testing"
  9. "github.com/google/go-cmp/cmp"
  10. "google.golang.org/protobuf/internal/flags"
  11. "google.golang.org/protobuf/proto"
  12. testpb "google.golang.org/protobuf/internal/testprotos/test"
  13. test3pb "google.golang.org/protobuf/internal/testprotos/test3"
  14. )
  15. func TestEncode(t *testing.T) {
  16. for _, test := range testProtos {
  17. for _, want := range test.decodeTo {
  18. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  19. opts := proto.MarshalOptions{
  20. AllowPartial: test.partial,
  21. }
  22. wire, err := opts.Marshal(want)
  23. if err != nil {
  24. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  25. }
  26. size := proto.Size(want)
  27. if size != len(wire) {
  28. t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
  29. }
  30. got := want.ProtoReflect().New().Interface()
  31. uopts := proto.UnmarshalOptions{
  32. AllowPartial: test.partial,
  33. }
  34. if err := uopts.Unmarshal(wire, got); err != nil {
  35. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  36. return
  37. }
  38. if !proto.Equal(got, want) {
  39. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  40. }
  41. })
  42. }
  43. }
  44. }
  45. func TestEncodeDeterministic(t *testing.T) {
  46. for _, test := range testProtos {
  47. for _, want := range test.decodeTo {
  48. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  49. opts := proto.MarshalOptions{
  50. Deterministic: true,
  51. AllowPartial: test.partial,
  52. }
  53. wire, err := opts.Marshal(want)
  54. if err != nil {
  55. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  56. }
  57. wire2, err := opts.Marshal(want)
  58. if err != nil {
  59. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  60. }
  61. if !bytes.Equal(wire, wire2) {
  62. t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
  63. }
  64. got := want.ProtoReflect().New().Interface()
  65. uopts := proto.UnmarshalOptions{
  66. AllowPartial: test.partial,
  67. }
  68. if err := uopts.Unmarshal(wire, got); err != nil {
  69. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  70. return
  71. }
  72. if !proto.Equal(got, want) {
  73. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  74. }
  75. })
  76. }
  77. }
  78. }
  79. func TestEncodeInvalidUTF8(t *testing.T) {
  80. for _, test := range invalidUTF8TestProtos {
  81. for _, want := range test.decodeTo {
  82. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  83. _, err := proto.Marshal(want)
  84. if err == nil {
  85. t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  86. }
  87. })
  88. }
  89. }
  90. }
  91. func TestEncodeNoEnforceUTF8(t *testing.T) {
  92. for _, test := range noEnforceUTF8TestProtos {
  93. for _, want := range test.decodeTo {
  94. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  95. _, err := proto.Marshal(want)
  96. switch {
  97. case flags.ProtoLegacy && err != nil:
  98. t.Errorf("Marshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
  99. case !flags.ProtoLegacy && err == nil:
  100. t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  101. }
  102. })
  103. }
  104. }
  105. }
  106. func TestEncodeRequiredFieldChecks(t *testing.T) {
  107. for _, test := range testProtos {
  108. if !test.partial {
  109. continue
  110. }
  111. for _, m := range test.decodeTo {
  112. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  113. _, err := proto.Marshal(m)
  114. if err == nil {
  115. t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
  116. }
  117. })
  118. }
  119. }
  120. }
  121. func TestEncodeAppend(t *testing.T) {
  122. want := []byte("prefix")
  123. got := append([]byte(nil), want...)
  124. got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
  125. OptionalString: "value",
  126. })
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if !bytes.HasPrefix(got, want) {
  131. t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
  132. }
  133. }
  134. func TestEncodeOneofNilWrapper(t *testing.T) {
  135. m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
  136. b, err := proto.Marshal(m)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. if len(b) > 0 {
  141. t.Errorf("Marshal return non-empty, want empty")
  142. }
  143. }
  144. func TestMarshalAppendAllocations(t *testing.T) {
  145. m := &test3pb.TestAllTypes{OptionalInt32: 1}
  146. size := proto.Size(m)
  147. const count = 1000
  148. b := make([]byte, size)
  149. // AllocsPerRun returns an integral value.
  150. marshalAllocs := testing.AllocsPerRun(count, func() {
  151. _, err := proto.MarshalOptions{}.MarshalAppend(b[:0], m)
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. })
  156. b = nil
  157. marshalAppendAllocs := testing.AllocsPerRun(count, func() {
  158. var err error
  159. b, err = proto.MarshalOptions{}.MarshalAppend(b, m)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. })
  164. if marshalAllocs != marshalAppendAllocs {
  165. t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
  166. t.Errorf("%v allocs/op when repeatedly appending to a slice", marshalAppendAllocs)
  167. t.Errorf("expect amortized allocs/op to be identical")
  168. }
  169. }