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" |
| 12 | ) |
| 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) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 18 | opts := proto.MarshalOptions{ |
| 19 | AllowPartial: test.partial, |
| 20 | } |
| 21 | wire, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 22 | if err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 23 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want)) |
| 24 | } |
| 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)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 32 | uopts := proto.UnmarshalOptions{ |
| 33 | AllowPartial: test.partial, |
| 34 | } |
| 35 | if err := uopts.Unmarshal(wire, got); err != nil { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 36 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message))) |
| 37 | return |
| 38 | } |
| 39 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 40 | if test.invalidExtensions { |
| 41 | // Equal doesn't work on messages containing invalid extension data. |
| 42 | return |
| 43 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 44 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
| 45 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message))) |
| 46 | } |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestEncodeDeterministic(t *testing.T) { |
| 53 | for _, test := range testProtos { |
| 54 | for _, want := range test.decodeTo { |
| 55 | 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^] | 56 | opts := proto.MarshalOptions{ |
| 57 | Deterministic: true, |
| 58 | AllowPartial: test.partial, |
| 59 | } |
| 60 | wire, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 61 | if err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 62 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 63 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 64 | wire2, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 65 | if err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 66 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 67 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 68 | if !bytes.Equal(wire, wire2) { |
| 69 | t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2)) |
| 70 | } |
| 71 | |
| 72 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 73 | uopts := proto.UnmarshalOptions{ |
| 74 | AllowPartial: test.partial, |
| 75 | } |
| 76 | if err := uopts.Unmarshal(wire, got); err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 77 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 78 | return |
| 79 | } |
| 80 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 81 | if test.invalidExtensions { |
| 82 | // Equal doesn't work on messages containing invalid extension data. |
| 83 | return |
| 84 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 85 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 86 | 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] | 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame^] | 92 | |
| 93 | func TestEncodeRequiredFieldChecks(t *testing.T) { |
| 94 | for _, test := range testProtos { |
| 95 | if !test.partial { |
| 96 | continue |
| 97 | } |
| 98 | for _, m := range test.decodeTo { |
| 99 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
| 100 | _, err := proto.Marshal(m) |
| 101 | if err == nil { |
| 102 | t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m)) |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | } |