encode_test.go 3.5 KB

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