Damien Neil | 6e40b32 | 2019-10-01 13:05:16 -0700 | [diff] [blame] | 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 | |
| 5 | package proto_test |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | |
| 10 | "google.golang.org/protobuf/internal/encoding/pack" |
| 11 | "google.golang.org/protobuf/internal/flags" |
| 12 | "google.golang.org/protobuf/proto" |
| 13 | |
| 14 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 15 | weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1" |
| 16 | ) |
| 17 | |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 18 | func init() { |
| 19 | if flags.ProtoLegacy { |
| 20 | testValidMessages = append(testValidMessages, testWeakValidMessages...) |
| 21 | testInvalidMessages = append(testInvalidMessages, testWeakInvalidMessages...) |
Damien Neil | 6e40b32 | 2019-10-01 13:05:16 -0700 | [diff] [blame] | 22 | } |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 23 | } |
Damien Neil | 6e40b32 | 2019-10-01 13:05:16 -0700 | [diff] [blame] | 24 | |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 25 | var testWeakValidMessages = []testProto{ |
| 26 | { |
| 27 | desc: "weak message", |
| 28 | decodeTo: []proto.Message{ |
| 29 | func() proto.Message { |
| 30 | if !flags.ProtoLegacy { |
| 31 | return nil |
| 32 | } |
| 33 | m := &testpb.TestWeak{} |
| 34 | m.SetWeakMessage1(&weakpb.WeakImportMessage1{ |
| 35 | A: proto.Int32(1000), |
| 36 | }) |
| 37 | m.ProtoReflect().SetUnknown(pack.Message{ |
| 38 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 39 | pack.Tag{1, pack.VarintType}, pack.Varint(2000), |
| 40 | }), |
| 41 | }.Marshal()) |
| 42 | return m |
| 43 | }(), |
| 44 | }, |
| 45 | wire: pack.Message{ |
| 46 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 47 | pack.Tag{1, pack.VarintType}, pack.Varint(1000), |
| 48 | }), |
| 49 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 50 | pack.Tag{1, pack.VarintType}, pack.Varint(2000), |
| 51 | }), |
| 52 | }.Marshal(), |
| 53 | }, |
| 54 | } |
Damien Neil | 6e40b32 | 2019-10-01 13:05:16 -0700 | [diff] [blame] | 55 | |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 56 | var testWeakInvalidMessages = []testProto{ |
| 57 | { |
| 58 | desc: "invalid field number 0 in weak message", |
| 59 | decodeTo: []proto.Message{(*testpb.TestWeak)(nil)}, |
| 60 | wire: pack.Message{ |
| 61 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 62 | pack.Tag{0, pack.VarintType}, pack.Varint(1000), |
| 63 | }), |
| 64 | }.Marshal(), |
| 65 | }, |
Damien Neil | 6e40b32 | 2019-10-01 13:05:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func TestWeakNil(t *testing.T) { |
| 69 | if !flags.ProtoLegacy { |
| 70 | t.SkipNow() |
| 71 | } |
| 72 | |
| 73 | m := new(testpb.TestWeak) |
| 74 | if v, ok := m.GetWeakMessage1().(*weakpb.WeakImportMessage1); !ok || v != nil { |
| 75 | t.Errorf("m.GetWeakMessage1() = type %[1]T(%[1]v), want (*weakpb.WeakImportMessage1)", v) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestWeakMarshalNil(t *testing.T) { |
| 80 | if !flags.ProtoLegacy { |
| 81 | t.SkipNow() |
| 82 | } |
| 83 | |
| 84 | m := new(testpb.TestWeak) |
| 85 | m.SetWeakMessage1(nil) |
| 86 | if b, err := proto.Marshal(m); err != nil || len(b) != 0 { |
| 87 | t.Errorf("Marshal(weak field set to nil) = [%x], %v; want [], nil", b, err) |
| 88 | } |
| 89 | m.SetWeakMessage1((*weakpb.WeakImportMessage1)(nil)) |
| 90 | if b, err := proto.Marshal(m); err != nil || len(b) != 0 { |
| 91 | t.Errorf("Marshal(weak field set to typed nil) = [%x], %v; want [], nil", b, err) |
| 92 | } |
| 93 | } |