encode_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. test3pb "google.golang.org/protobuf/internal/testprotos/test3"
  13. )
  14. func TestEncode(t *testing.T) {
  15. for _, test := range testProtos {
  16. for _, want := range test.decodeTo {
  17. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  18. opts := proto.MarshalOptions{
  19. AllowPartial: test.partial,
  20. }
  21. wire, err := opts.Marshal(want)
  22. if err != nil {
  23. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  24. }
  25. size := proto.Size(want)
  26. if size != len(wire) {
  27. t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
  28. }
  29. got := want.ProtoReflect().New().Interface()
  30. uopts := proto.UnmarshalOptions{
  31. AllowPartial: test.partial,
  32. }
  33. if err := uopts.Unmarshal(wire, got); err != nil {
  34. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  35. return
  36. }
  37. if !proto.Equal(got, want) {
  38. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  39. }
  40. })
  41. }
  42. }
  43. }
  44. func TestEncodeDeterministic(t *testing.T) {
  45. for _, test := range testProtos {
  46. for _, want := range test.decodeTo {
  47. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  48. opts := proto.MarshalOptions{
  49. Deterministic: true,
  50. AllowPartial: test.partial,
  51. }
  52. wire, err := opts.Marshal(want)
  53. if err != nil {
  54. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  55. }
  56. wire2, err := opts.Marshal(want)
  57. if err != nil {
  58. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  59. }
  60. if !bytes.Equal(wire, wire2) {
  61. t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
  62. }
  63. got := want.ProtoReflect().New().Interface()
  64. uopts := proto.UnmarshalOptions{
  65. AllowPartial: test.partial,
  66. }
  67. if err := uopts.Unmarshal(wire, got); err != nil {
  68. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  69. return
  70. }
  71. if !proto.Equal(got, want) {
  72. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  73. }
  74. })
  75. }
  76. }
  77. }
  78. func TestEncodeInvalidUTF8(t *testing.T) {
  79. for _, test := range invalidUTF8TestProtos {
  80. for _, want := range test.decodeTo {
  81. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  82. _, err := proto.Marshal(want)
  83. if err == nil {
  84. t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  85. }
  86. })
  87. }
  88. }
  89. }
  90. func TestEncodeNoEnforceUTF8(t *testing.T) {
  91. for _, test := range noEnforceUTF8TestProtos {
  92. for _, want := range test.decodeTo {
  93. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  94. _, err := proto.Marshal(want)
  95. switch {
  96. case flags.ProtoLegacy && err != nil:
  97. t.Errorf("Marshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
  98. case !flags.ProtoLegacy && err == nil:
  99. t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  100. }
  101. })
  102. }
  103. }
  104. }
  105. func TestEncodeRequiredFieldChecks(t *testing.T) {
  106. for _, test := range testProtos {
  107. if !test.partial {
  108. continue
  109. }
  110. for _, m := range test.decodeTo {
  111. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  112. _, err := proto.Marshal(m)
  113. if err == nil {
  114. t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
  115. }
  116. })
  117. }
  118. }
  119. }
  120. func TestMarshalAppend(t *testing.T) {
  121. want := []byte("prefix")
  122. got := append([]byte(nil), want...)
  123. got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
  124. OptionalString: "value",
  125. })
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. if !bytes.HasPrefix(got, want) {
  130. t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
  131. }
  132. }