1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package types
33
34 import (
35 "testing"
36
37 "github.com/gogo/protobuf/proto"
38 pb "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
39 )
40
41 func TestMarshalUnmarshal(t *testing.T) {
42 orig := &Any{Value: []byte("test")}
43
44 packed, err := MarshalAny(orig)
45 if err != nil {
46 t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err)
47 }
48
49 unpacked := &Any{}
50 err = UnmarshalAny(packed, unpacked)
51 if err != nil || !proto.Equal(unpacked, orig) {
52 t.Errorf("got: %v, %+v; want nil, %+v", err, unpacked, orig)
53 }
54 }
55
56 func TestIs(t *testing.T) {
57 a, err := MarshalAny(&pb.FileDescriptorProto{})
58 if err != nil {
59 t.Fatal(err)
60 }
61 if Is(a, &pb.DescriptorProto{}) {
62
63 t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
64 }
65 if Is(a, &pb.EnumDescriptorProto{}) {
66
67 t.Error("FileDescriptorProto is not an EnumDescriptorProto, but Is says it is")
68 }
69 if !Is(a, &pb.FileDescriptorProto{}) {
70 t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
71 }
72 }
73
74 func TestIsDifferentUrlPrefixes(t *testing.T) {
75 m := &pb.FileDescriptorProto{}
76 a := &Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
77 if !Is(a, m) {
78 t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
79 }
80 }
81
82 func TestIsCornerCases(t *testing.T) {
83 m := &pb.FileDescriptorProto{}
84 if Is(nil, m) {
85 t.Errorf("message with nil type url incorrectly claimed to be %q", proto.MessageName(m))
86 }
87 noPrefix := &Any{TypeUrl: proto.MessageName(m)}
88 if Is(noPrefix, m) {
89 t.Errorf("message with type url %q incorrectly claimed to be %q", noPrefix.TypeUrl, proto.MessageName(m))
90 }
91 shortPrefix := &Any{TypeUrl: "/" + proto.MessageName(m)}
92 if !Is(shortPrefix, m) {
93 t.Errorf("message with type url %q didn't satisfy Is for type %q", shortPrefix.TypeUrl, proto.MessageName(m))
94 }
95 }
96
97 func TestUnmarshalDynamic(t *testing.T) {
98 want := &pb.FileDescriptorProto{Name: proto.String("foo")}
99 a, err := MarshalAny(want)
100 if err != nil {
101 t.Fatal(err)
102 }
103 var got DynamicAny
104 if err := UnmarshalAny(a, &got); err != nil {
105 t.Fatal(err)
106 }
107 if !proto.Equal(got.Message, want) {
108 t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want)
109 }
110 }
111
112 func TestEmpty(t *testing.T) {
113 want := &pb.FileDescriptorProto{}
114 a, err := MarshalAny(want)
115 if err != nil {
116 t.Fatal(err)
117 }
118 got, err := EmptyAny(a)
119 if err != nil {
120 t.Fatal(err)
121 }
122 if !proto.Equal(got, want) {
123 t.Errorf("unequal empty message, got %q, want %q", got, want)
124 }
125
126
127
128 a.TypeUrl = "type.googleapis.com/google.protobuf.TestAny"
129 if _, err := EmptyAny(a); err == nil {
130 t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)
131 }
132 }
133
134 func TestEmptyCornerCases(t *testing.T) {
135 _, err := EmptyAny(nil)
136 if err == nil {
137 t.Error("expected Empty for nil to fail")
138 }
139 want := &pb.FileDescriptorProto{}
140 noPrefix := &Any{TypeUrl: proto.MessageName(want)}
141 _, err = EmptyAny(noPrefix)
142 if err == nil {
143 t.Errorf("expected Empty for any type %q to fail", noPrefix.TypeUrl)
144 }
145 shortPrefix := &Any{TypeUrl: "/" + proto.MessageName(want)}
146 got, err := EmptyAny(shortPrefix)
147 if err != nil {
148 t.Errorf("Empty for any type %q failed: %s", shortPrefix.TypeUrl, err)
149 }
150 if !proto.Equal(got, want) {
151 t.Errorf("Empty for any type %q differs, got %q, want %q", shortPrefix.TypeUrl, got, want)
152 }
153 }
154
View as plain text