encode_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. protoV1 "github.com/golang/protobuf/proto"
  10. "github.com/google/go-cmp/cmp"
  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, protoV1.MarshalTextString(want.(protoV1.Message)))
  35. return
  36. }
  37. if test.invalidExtensions {
  38. // Equal doesn't work on messages containing invalid extension data.
  39. return
  40. }
  41. if !proto.Equal(got, want) {
  42. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
  43. }
  44. })
  45. }
  46. }
  47. }
  48. func TestEncodeDeterministic(t *testing.T) {
  49. for _, test := range testProtos {
  50. for _, want := range test.decodeTo {
  51. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  52. opts := proto.MarshalOptions{
  53. Deterministic: true,
  54. AllowPartial: test.partial,
  55. }
  56. wire, err := opts.Marshal(want)
  57. if err != nil {
  58. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  59. }
  60. wire2, err := opts.Marshal(want)
  61. if err != nil {
  62. t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
  63. }
  64. if !bytes.Equal(wire, wire2) {
  65. t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
  66. }
  67. got := want.ProtoReflect().New().Interface()
  68. uopts := proto.UnmarshalOptions{
  69. AllowPartial: test.partial,
  70. }
  71. if err := uopts.Unmarshal(wire, got); err != nil {
  72. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  73. return
  74. }
  75. if test.invalidExtensions {
  76. // Equal doesn't work on messages containing invalid extension data.
  77. return
  78. }
  79. if !proto.Equal(got, want) {
  80. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  81. }
  82. })
  83. }
  84. }
  85. }
  86. func TestEncodeInvalidUTF8(t *testing.T) {
  87. for _, test := range invalidUTF8TestProtos {
  88. for _, want := range test.decodeTo {
  89. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  90. _, err := proto.Marshal(want)
  91. if err == nil {
  92. t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  93. }
  94. })
  95. }
  96. }
  97. }
  98. func TestEncodeRequiredFieldChecks(t *testing.T) {
  99. for _, test := range testProtos {
  100. if !test.partial {
  101. continue
  102. }
  103. for _, m := range test.decodeTo {
  104. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  105. _, err := proto.Marshal(m)
  106. if err == nil {
  107. t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
  108. }
  109. })
  110. }
  111. }
  112. }
  113. func TestMarshalAppend(t *testing.T) {
  114. want := []byte("prefix")
  115. got := append([]byte(nil), want...)
  116. got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
  117. OptionalString: "value",
  118. })
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. if !bytes.HasPrefix(got, want) {
  123. t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
  124. }
  125. }