Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 1 | package proto_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
| 9 | protoV1 "github.com/golang/protobuf/proto" |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 10 | "github.com/golang/protobuf/v2/proto" |
| 11 | "github.com/google/go-cmp/cmp" |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 12 | |
| 13 | test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3" |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | func TestEncode(t *testing.T) { |
| 17 | for _, test := range testProtos { |
| 18 | for _, want := range test.decodeTo { |
| 19 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 20 | opts := proto.MarshalOptions{ |
| 21 | AllowPartial: test.partial, |
| 22 | } |
| 23 | wire, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 24 | if err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 25 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want)) |
| 26 | } |
| 27 | |
| 28 | size := proto.Size(want) |
| 29 | if size != len(wire) { |
| 30 | t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 34 | uopts := proto.UnmarshalOptions{ |
| 35 | AllowPartial: test.partial, |
| 36 | } |
| 37 | if err := uopts.Unmarshal(wire, got); err != nil { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 38 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message))) |
| 39 | return |
| 40 | } |
| 41 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 42 | if test.invalidExtensions { |
| 43 | // Equal doesn't work on messages containing invalid extension data. |
| 44 | return |
| 45 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 46 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
| 47 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message))) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestEncodeDeterministic(t *testing.T) { |
| 55 | for _, test := range testProtos { |
| 56 | for _, want := range test.decodeTo { |
| 57 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 58 | opts := proto.MarshalOptions{ |
| 59 | Deterministic: true, |
| 60 | AllowPartial: test.partial, |
| 61 | } |
| 62 | wire, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 63 | if err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 64 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 65 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 66 | wire2, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 67 | if err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 68 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 69 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 70 | if !bytes.Equal(wire, wire2) { |
| 71 | t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2)) |
| 72 | } |
| 73 | |
| 74 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 75 | uopts := proto.UnmarshalOptions{ |
| 76 | AllowPartial: test.partial, |
| 77 | } |
| 78 | if err := uopts.Unmarshal(wire, got); err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 79 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 80 | return |
| 81 | } |
| 82 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 83 | if test.invalidExtensions { |
| 84 | // Equal doesn't work on messages containing invalid extension data. |
| 85 | return |
| 86 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 87 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 88 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 94 | |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame^] | 95 | func TestEncodeInvalidUTF8(t *testing.T) { |
| 96 | for _, test := range invalidUTF8TestProtos { |
| 97 | for _, want := range test.decodeTo { |
| 98 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
| 99 | wire, err := proto.Marshal(want) |
| 100 | if !isErrInvalidUTF8(err) { |
| 101 | t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want)) |
| 102 | } |
| 103 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
| 104 | if err := proto.Unmarshal(wire, got); !isErrInvalidUTF8(err) { |
| 105 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) |
| 106 | return |
| 107 | } |
| 108 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
| 109 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 116 | func TestEncodeRequiredFieldChecks(t *testing.T) { |
| 117 | for _, test := range testProtos { |
| 118 | if !test.partial { |
| 119 | continue |
| 120 | } |
| 121 | for _, m := range test.decodeTo { |
| 122 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
| 123 | _, err := proto.Marshal(m) |
| 124 | if err == nil { |
| 125 | t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m)) |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |
| 130 | } |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 131 | |
| 132 | func TestMarshalAppend(t *testing.T) { |
| 133 | want := []byte("prefix") |
| 134 | got := append([]byte(nil), want...) |
| 135 | got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{ |
| 136 | OptionalString: "value", |
| 137 | }) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if !bytes.HasPrefix(got, want) { |
| 142 | t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want) |
| 143 | } |
| 144 | } |