Debian Codegraph @

common/types/pb/equal_test.go

raw symbols SCIP
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package pb
16
17 import (
18 "math"
19 "testing"
20
21 "google.golang.org/protobuf/proto"
22
23 proto3pb "github.com/google/cel-go/test/proto3pb"
24 anypb "google.golang.org/protobuf/types/known/anypb"
25 )
26
27 func TestEqual(t *testing.T) {
28 tests := []struct {
29 name string
30 a proto.Message
31 b proto.Message
32 out bool
33 }{
34 {
35 name: "EqualEmptyInstances",
36 a: &proto3pb.TestAllTypes{},
37 b: &proto3pb.TestAllTypes{},
38 out: true,
39 },
40 {
41 name: "NotEqualEmptyInstances",
42 a: &proto3pb.TestAllTypes{},
43 b: &proto3pb.NestedTestAllTypes{},
44 out: false,
45 },
46 {
47 name: "EqualScalarFields",
48 a: &proto3pb.TestAllTypes{
49 SingleBool: true,
50 SingleBytes: []byte("world"),
51 SingleDouble: 3.0,
52 SingleFloat: 1.5,
53 SingleInt32: 1,
54 SingleUint64: 1,
55 SingleString: "hello",
56 },
57 b: &proto3pb.TestAllTypes{
58 SingleBool: true,
59 SingleBytes: []byte("world"),
60 SingleDouble: 3.0,
61 SingleFloat: 1.5,
62 SingleInt32: 1,
63 SingleUint64: 1,
64 SingleString: "hello",
65 },
66 out: true,
67 },
68 {
69 name: "NotEqualFloatNan",
70 a: &proto3pb.TestAllTypes{
71 SingleFloat: float32(math.NaN()),
72 },
73 b: &proto3pb.TestAllTypes{
74 SingleFloat: float32(math.NaN()),
75 },
76 out: false,
77 },
78 {
79 name: "NotEqualDifferentFieldsSet",
80 a: &proto3pb.TestAllTypes{
81 SingleInt32: 1,
82 },
83 b: &proto3pb.TestAllTypes{},
84 out: false,
85 },
86 {
87 name: "NotEqualDifferentFieldsSetReverse",
88 a: &proto3pb.TestAllTypes{},
89 b: &proto3pb.TestAllTypes{
90 SingleInt32: 1,
91 },
92 out: false,
93 },
94 {
95 name: "EqualListField",
96 a: &proto3pb.TestAllTypes{
97 RepeatedInt32: []int32{1, 2, 3, 4},
98 },
99 b: &proto3pb.TestAllTypes{
100 RepeatedInt32: []int32{1, 2, 3, 4},
101 },
102 out: true,
103 },
104 {
105 name: "NotEqualListFieldDifferentLength",
106 a: &proto3pb.TestAllTypes{
107 RepeatedInt32: []int32{1, 2, 3},
108 },
109 b: &proto3pb.TestAllTypes{
110 RepeatedInt32: []int32{1, 2, 3, 4},
111 },
112 out: false,
113 },
114 {
115 name: "NotEqualListFieldDifferentContent",
116 a: &proto3pb.TestAllTypes{
117 RepeatedInt32: []int32{2, 1},
118 },
119 b: &proto3pb.TestAllTypes{
120 RepeatedInt32: []int32{1, 2},
121 },
122 out: false,
123 },
124 {
125 name: "EqualMapField",
126 a: &proto3pb.TestAllTypes{
127 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{
128 1: {
129 Child: &proto3pb.NestedTestAllTypes{
130 Payload: &proto3pb.TestAllTypes{
131 StandaloneEnum: proto3pb.TestAllTypes_BAR,
132 },
133 },
134 },
135 2: {
136 Payload: &proto3pb.TestAllTypes{},
137 },
138 },
139 },
140 b: &proto3pb.TestAllTypes{
141 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{
142 1: {
143 Child: &proto3pb.NestedTestAllTypes{
144 Payload: &proto3pb.TestAllTypes{
145 StandaloneEnum: proto3pb.TestAllTypes_BAR,
146 },
147 },
148 },
149 2: {
150 Payload: &proto3pb.TestAllTypes{},
151 },
152 },
153 },
154 out: true,
155 },
156 {
157 name: "NotEqualMapFieldDifferentLength",
158 a: &proto3pb.TestAllTypes{
159 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{
160 1: {
161 Child: &proto3pb.NestedTestAllTypes{},
162 },
163 2: {
164 Payload: &proto3pb.TestAllTypes{},
165 },
166 },
167 },
168 b: &proto3pb.TestAllTypes{
169 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{
170 1: {
171 Child: &proto3pb.NestedTestAllTypes{},
172 },
173 },
174 },
175 out: false,
176 },
177 {
178 name: "EqualAnyBytes",
179 a: &proto3pb.TestAllTypes{
180 SingleAny: packAny(t, &proto3pb.TestAllTypes{
181 SingleInt32: 1,
182 SingleUint32: 2,
183 SingleString: "three",
184 RepeatedInt32: []int32{1, 2, 3},
185 }),
186 },
187 b: &proto3pb.TestAllTypes{
188 SingleAny: packAny(t, &proto3pb.TestAllTypes{
189 SingleInt32: 1,
190 SingleUint32: 2,
191 SingleString: "three",
192 RepeatedInt32: []int32{1, 2, 3},
193 }),
194 },
195 out: true,
196 },
197 {
198 name: "NotEqualDoublePackedAny",
199 a: &proto3pb.TestAllTypes{
200 SingleAny: doublePackAny(t, &proto3pb.TestAllTypes{
201 SingleInt32: 1,
202 SingleUint32: 2,
203 SingleString: "three",
204 RepeatedInt32: []int32{1, 2, 3},
205 }),
206 },
207 b: &proto3pb.TestAllTypes{
208 SingleAny: doublePackAny(t, &proto3pb.TestAllTypes{
209 SingleInt32: 1,
210 SingleUint32: 2,
211 SingleString: "three",
212 RepeatedInt32: []int32{1, 2, 3, 4},
213 }),
214 },
215 out: false,
216 },
217 {
218 name: "NotEqualAnyTypeURL",
219 a: &proto3pb.TestAllTypes{
220 SingleAny: packAny(t, &proto3pb.NestedTestAllTypes{}),
221 },
222 b: &proto3pb.TestAllTypes{
223 SingleAny: packAny(t, &proto3pb.TestAllTypes{}),
224 },
225 out: false,
226 },
227 {
228 name: "NotEqualAnyFields",
229 a: &proto3pb.TestAllTypes{
230 SingleAny: packAny(t, &proto3pb.TestAllTypes{
231 SingleInt32: 1,
232 SingleUint32: 2,
233 RepeatedInt32: []int32{1, 2, 3},
234 }),
235 },
236 b: &proto3pb.TestAllTypes{
237 SingleAny: packAny(t, &proto3pb.TestAllTypes{
238 SingleInt32: 1,
239 SingleUint32: 2,
240 SingleString: "three",
241 RepeatedInt32: []int32{1, 2, 3},
242 }),
243 },
244 out: false,
245 },
246 {
247 name: "NotEqualAnyDeserializeA",
248 a: &proto3pb.TestAllTypes{
249 SingleAny: badPackAny(t, &proto3pb.TestAllTypes{
250 SingleInt32: 1,
251 SingleUint32: 2,
252 RepeatedInt32: []int32{1, 2, 3},
253 }),
254 },
255 b: &proto3pb.TestAllTypes{
256 SingleAny: badPackAny(t, &proto3pb.TestAllTypes{
257 SingleInt32: 1,
258 SingleUint32: 2,
259 SingleString: "three",
260 RepeatedInt32: []int32{1, 2, 3},
261 }),
262 },
263 out: false,
264 },
265 {
266 name: "EqualUnknownFields",
267 a: &proto3pb.TestAllTypes{
268 SingleAny: misPackAny(t, &proto3pb.NestedTestAllTypes{
269 Child: &proto3pb.NestedTestAllTypes{
270 Payload: &proto3pb.TestAllTypes{
271 SingleInt32: 1,
272 },
273 },
274 }),
275 },
276 b: &proto3pb.TestAllTypes{
277 SingleAny: misPackAny(t, &proto3pb.NestedTestAllTypes{
278 Child: &proto3pb.NestedTestAllTypes{
279 Payload: &proto3pb.TestAllTypes{
280 SingleInt32: 1,
281 },
282 },
283 }),
284 },
285 out: true,
286 },
287 {
288 name: "NotEqualUnknownFieldsCount",
289 a: &proto3pb.TestAllTypes{
290 SingleAny: misPackAny(t, &proto3pb.NestedTestAllTypes{
291 Child: &proto3pb.NestedTestAllTypes{
292 Payload: &proto3pb.TestAllTypes{
293 SingleInt32: 1,
294 SingleFloat: 2.0,
295 },
296 },
297 }),
298 },
299 b: &proto3pb.TestAllTypes{
300 SingleAny: misPackAny(t, &proto3pb.NestedTestAllTypes{
301 Child: &proto3pb.NestedTestAllTypes{
302 Payload: &proto3pb.TestAllTypes{
303 SingleInt32: 1,
304 },
305 },
306 }),
307 },
308 out: false,
309 },
310 {
311 name: "NotEqualUnknownFields",
312 a: &proto3pb.TestAllTypes{
313 SingleAny: misPackAny(t, &proto3pb.NestedTestAllTypes{
314 Child: &proto3pb.NestedTestAllTypes{
315 Payload: &proto3pb.TestAllTypes{
316 SingleInt64: 2,
317 },
318 },
319 }),
320 },
321 b: &proto3pb.TestAllTypes{
322 SingleAny: misPackAny(t, &proto3pb.NestedTestAllTypes{
323 Child: &proto3pb.NestedTestAllTypes{
324 Payload: &proto3pb.TestAllTypes{
325 SingleInt32: 1,
326 },
327 },
328 }),
329 },
330 out: false,
331 },
332 {
333 name: "NotEqualOneNil",
334 a: nil,
335 b: &proto3pb.TestAllTypes{},
336 out: false,
337 },
338 {
339 name: "EqualBothNil",
340 a: nil,
341 b: nil,
342 out: true,
343 },
344 }
345
346 for _, tst := range tests {
347 tc := tst
348 t.Run(tc.name, func(t *testing.T) {
349 got := Equal(tc.a, tc.b)
350 if got != tc.out {
351 t.Errorf("Equal(%v, %v) got %v, wanted %v", tc.a, tc.b, got, tc.out)
352 }
353 })
354 }
355 }
356
357 func packAny(t *testing.T, m proto.Message) *anypb.Any {
358 t.Helper()
359 any, err := anypb.New(m)
360 if err != nil {
361 t.Fatalf("anypb.New(%v) failed with error: %v", m, err)
362 }
363 return any
364 }
365
366 func doublePackAny(t *testing.T, m proto.Message) *anypb.Any {
367 t.Helper()
368 any, err := anypb.New(m)
369 if err != nil {
370 t.Fatalf("anypb.New(%v) failed with error: %v", m, err)
371 }
372 any, err = anypb.New(any)
373 if err != nil {
374 t.Fatalf("anypb.New(%v) failed with error: %v", any, err)
375 }
376 return any
377 }
378
379 func badPackAny(t *testing.T, m proto.Message) *anypb.Any {
380 t.Helper()
381 any, err := anypb.New(m)
382 if err != nil {
383 t.Fatalf("anypb.New(%v) failed with error: %v", m, err)
384 }
385 any.TypeUrl = "type.googleapis.com/BadType"
386 return any
387 }
388
389 func misPackAny(t *testing.T, m proto.Message) *anypb.Any {
390 t.Helper()
391 any, err := anypb.New(m)
392 if err != nil {
393 t.Fatalf("anypb.New(%v) failed with error: %v", m, err)
394 }
395 any.TypeUrl = "type.googleapis.com/google.expr.proto3.test.TestAllTypes"
396 return any
397 }