blob: 15a18fb2d2b8242bf11956acf8b26ee643dd4c64 [file] [log] [blame]
Damien Neil6e40b322019-10-01 13:05:16 -07001// 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
5package proto_test
6
7import (
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 Neilb0c26f12019-12-16 09:37:59 -080018func init() {
19 if flags.ProtoLegacy {
20 testValidMessages = append(testValidMessages, testWeakValidMessages...)
21 testInvalidMessages = append(testInvalidMessages, testWeakInvalidMessages...)
Damien Neil6e40b322019-10-01 13:05:16 -070022 }
Damien Neilb0c26f12019-12-16 09:37:59 -080023}
Damien Neil6e40b322019-10-01 13:05:16 -070024
Damien Neilb0c26f12019-12-16 09:37:59 -080025var 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 Neil6e40b322019-10-01 13:05:16 -070055
Damien Neilb0c26f12019-12-16 09:37:59 -080056var 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 Neil6e40b322019-10-01 13:05:16 -070066}
67
68func 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
79func 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}