1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Author: kenton@google.com (Kenton Varda)
32
33 #include <iostream>
34 #include <string>
35
36 #include <google/protobuf/stubs/logging.h>
37 #include <google/protobuf/stubs/common.h>
38 #include <google/protobuf/arena_test_util.h>
39 #include <google/protobuf/map_lite_test_util.h>
40 #include <google/protobuf/map_lite_unittest.pb.h>
41 #include <google/protobuf/test_util_lite.h>
42 #include <google/protobuf/unittest_lite.pb.h>
43 #include <google/protobuf/io/coded_stream.h>
44 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
45 #include <google/protobuf/wire_format_lite.h>
46 #include <gtest/gtest.h>
47 #include <google/protobuf/stubs/strutil.h>
48
49 namespace google {
50 namespace protobuf {
51
52 // Helper methods to test parsing merge behavior.
ExpectMessageMerged(const unittest::TestAllTypesLite & message)53 void ExpectMessageMerged(const unittest::TestAllTypesLite& message) {
54 EXPECT_EQ(message.optional_int32(), 3);
55 EXPECT_EQ(message.optional_int64(), 2);
56 EXPECT_EQ(message.optional_string(), "hello");
57 }
58
AssignParsingMergeMessages(unittest::TestAllTypesLite * msg1,unittest::TestAllTypesLite * msg2,unittest::TestAllTypesLite * msg3)59 void AssignParsingMergeMessages(unittest::TestAllTypesLite* msg1,
60 unittest::TestAllTypesLite* msg2,
61 unittest::TestAllTypesLite* msg3) {
62 msg1->set_optional_int32(1);
63 msg2->set_optional_int64(2);
64 msg3->set_optional_int32(3);
65 msg3->set_optional_string("hello");
66 }
67
SetAllTypesInEmptyMessageUnknownFields(unittest::TestEmptyMessageLite * empty_message)68 void SetAllTypesInEmptyMessageUnknownFields(
69 unittest::TestEmptyMessageLite* empty_message) {
70 protobuf_unittest::TestAllTypesLite message;
71 TestUtilLite::ExpectClear(message);
72 TestUtilLite::SetAllFields(&message);
73 std::string data = message.SerializeAsString();
74 empty_message->ParseFromString(data);
75 }
76
SetSomeTypesInEmptyMessageUnknownFields(unittest::TestEmptyMessageLite * empty_message)77 void SetSomeTypesInEmptyMessageUnknownFields(
78 unittest::TestEmptyMessageLite* empty_message) {
79 protobuf_unittest::TestAllTypesLite message;
80 TestUtilLite::ExpectClear(message);
81 message.set_optional_int32(101);
82 message.set_optional_int64(102);
83 message.set_optional_uint32(103);
84 message.set_optional_uint64(104);
85 std::string data = message.SerializeAsString();
86 empty_message->ParseFromString(data);
87 }
88
TEST(Lite,AllLite1)89 TEST(Lite, AllLite1) {
90 std::string data;
91
92 {
93 protobuf_unittest::TestAllTypesLite message, message2, message3;
94 TestUtilLite::ExpectClear(message);
95 TestUtilLite::SetAllFields(&message);
96 message2.CopyFrom(message);
97 data = message.SerializeAsString();
98 message3.ParseFromString(data);
99 TestUtilLite::ExpectAllFieldsSet(message);
100 TestUtilLite::ExpectAllFieldsSet(message2);
101 TestUtilLite::ExpectAllFieldsSet(message3);
102 TestUtilLite::ModifyRepeatedFields(&message);
103 TestUtilLite::ExpectRepeatedFieldsModified(message);
104 message.Clear();
105 TestUtilLite::ExpectClear(message);
106 }
107 }
108
TEST(Lite,AllLite2)109 TEST(Lite, AllLite2) {
110 std::string data;
111 {
112 protobuf_unittest::TestAllExtensionsLite message, message2, message3;
113 TestUtilLite::ExpectExtensionsClear(message);
114 TestUtilLite::SetAllExtensions(&message);
115 message2.CopyFrom(message);
116 std::string extensions_data = message.SerializeAsString();
117 message3.ParseFromString(extensions_data);
118 TestUtilLite::ExpectAllExtensionsSet(message);
119 TestUtilLite::ExpectAllExtensionsSet(message2);
120 TestUtilLite::ExpectAllExtensionsSet(message3);
121 TestUtilLite::ModifyRepeatedExtensions(&message);
122 TestUtilLite::ExpectRepeatedExtensionsModified(message);
123 message.Clear();
124 TestUtilLite::ExpectExtensionsClear(message);
125 }
126 }
127
TEST(Lite,AllLite3)128 TEST(Lite, AllLite3) {
129 std::string data, packed_data;
130
131 {
132 protobuf_unittest::TestPackedTypesLite message, message2, message3;
133 TestUtilLite::ExpectPackedClear(message);
134 TestUtilLite::SetPackedFields(&message);
135 message2.CopyFrom(message);
136 packed_data = message.SerializeAsString();
137 message3.ParseFromString(packed_data);
138 TestUtilLite::ExpectPackedFieldsSet(message);
139 TestUtilLite::ExpectPackedFieldsSet(message2);
140 TestUtilLite::ExpectPackedFieldsSet(message3);
141 TestUtilLite::ModifyPackedFields(&message);
142 TestUtilLite::ExpectPackedFieldsModified(message);
143 message.Clear();
144 TestUtilLite::ExpectPackedClear(message);
145 }
146
147 {
148 protobuf_unittest::TestPackedExtensionsLite message, message2, message3;
149 TestUtilLite::ExpectPackedExtensionsClear(message);
150 TestUtilLite::SetPackedExtensions(&message);
151 message2.CopyFrom(message);
152 std::string packed_extensions_data = message.SerializeAsString();
153 EXPECT_EQ(packed_extensions_data, packed_data);
154 message3.ParseFromString(packed_extensions_data);
155 TestUtilLite::ExpectPackedExtensionsSet(message);
156 TestUtilLite::ExpectPackedExtensionsSet(message2);
157 TestUtilLite::ExpectPackedExtensionsSet(message3);
158 TestUtilLite::ModifyPackedExtensions(&message);
159 TestUtilLite::ExpectPackedExtensionsModified(message);
160 message.Clear();
161 TestUtilLite::ExpectPackedExtensionsClear(message);
162 }
163 }
164
TEST(Lite,AllLite5)165 TEST(Lite, AllLite5) {
166 std::string data;
167
168 {
169 // Test that if an optional or required message/group field appears multiple
170 // times in the input, they need to be merged.
171 unittest::TestParsingMergeLite::RepeatedFieldsGenerator generator;
172 unittest::TestAllTypesLite* msg1;
173 unittest::TestAllTypesLite* msg2;
174 unittest::TestAllTypesLite* msg3;
175
176 #define ASSIGN_REPEATED_FIELD(FIELD) \
177 msg1 = generator.add_##FIELD(); \
178 msg2 = generator.add_##FIELD(); \
179 msg3 = generator.add_##FIELD(); \
180 AssignParsingMergeMessages(msg1, msg2, msg3)
181
182 ASSIGN_REPEATED_FIELD(field1);
183 ASSIGN_REPEATED_FIELD(field2);
184 ASSIGN_REPEATED_FIELD(field3);
185 ASSIGN_REPEATED_FIELD(ext1);
186 ASSIGN_REPEATED_FIELD(ext2);
187
188 #undef ASSIGN_REPEATED_FIELD
189 #define ASSIGN_REPEATED_GROUP(FIELD) \
190 msg1 = generator.add_##FIELD()->mutable_field1(); \
191 msg2 = generator.add_##FIELD()->mutable_field1(); \
192 msg3 = generator.add_##FIELD()->mutable_field1(); \
193 AssignParsingMergeMessages(msg1, msg2, msg3)
194
195 ASSIGN_REPEATED_GROUP(group1);
196 ASSIGN_REPEATED_GROUP(group2);
197
198 #undef ASSIGN_REPEATED_GROUP
199
200 std::string buffer;
201 generator.SerializeToString(&buffer);
202 unittest::TestParsingMergeLite parsing_merge;
203 parsing_merge.ParseFromString(buffer);
204
205 // Required and optional fields should be merged.
206 ExpectMessageMerged(parsing_merge.required_all_types());
207 ExpectMessageMerged(parsing_merge.optional_all_types());
208 ExpectMessageMerged(
209 parsing_merge.optionalgroup().optional_group_all_types());
210 ExpectMessageMerged(parsing_merge.GetExtension(
211 unittest::TestParsingMergeLite::optional_ext));
212
213 // Repeated fields should not be merged.
214 EXPECT_EQ(parsing_merge.repeated_all_types_size(), 3);
215 EXPECT_EQ(parsing_merge.repeatedgroup_size(), 3);
216 EXPECT_EQ(parsing_merge.ExtensionSize(
217 unittest::TestParsingMergeLite::repeated_ext),
218 3);
219 }
220 }
221
TEST(Lite,AllLite6)222 TEST(Lite, AllLite6) {
223 std::string data;
224
225 // Test unknown fields support for lite messages.
226 {
227 protobuf_unittest::TestAllTypesLite message, message2;
228 protobuf_unittest::TestEmptyMessageLite empty_message;
229 TestUtilLite::ExpectClear(message);
230 TestUtilLite::SetAllFields(&message);
231 data = message.SerializeAsString();
232 ASSERT_TRUE(empty_message.ParseFromString(data));
233 data.clear();
234 data = empty_message.SerializeAsString();
235 EXPECT_TRUE(message2.ParseFromString(data));
236 data = message2.SerializeAsString();
237 TestUtilLite::ExpectAllFieldsSet(message2);
238 message.Clear();
239 TestUtilLite::ExpectClear(message);
240 }
241 }
242
TEST(Lite,AllLite7)243 TEST(Lite, AllLite7) {
244 std::string data;
245
246 {
247 protobuf_unittest::TestAllExtensionsLite message, message2;
248 protobuf_unittest::TestEmptyMessageLite empty_message;
249 TestUtilLite::ExpectExtensionsClear(message);
250 TestUtilLite::SetAllExtensions(&message);
251 data = message.SerializeAsString();
252 empty_message.ParseFromString(data);
253 data.clear();
254 data = empty_message.SerializeAsString();
255 message2.ParseFromString(data);
256 data = message2.SerializeAsString();
257 TestUtilLite::ExpectAllExtensionsSet(message2);
258 message.Clear();
259 TestUtilLite::ExpectExtensionsClear(message);
260 }
261 }
262
TEST(Lite,AllLite8)263 TEST(Lite, AllLite8) {
264 std::string data;
265
266 {
267 protobuf_unittest::TestPackedTypesLite message, message2;
268 protobuf_unittest::TestEmptyMessageLite empty_message;
269 TestUtilLite::ExpectPackedClear(message);
270 TestUtilLite::SetPackedFields(&message);
271 data = message.SerializeAsString();
272 empty_message.ParseFromString(data);
273 data.clear();
274 data = empty_message.SerializeAsString();
275 message2.ParseFromString(data);
276 data = message2.SerializeAsString();
277 TestUtilLite::ExpectPackedFieldsSet(message2);
278 message.Clear();
279 TestUtilLite::ExpectPackedClear(message);
280 }
281 }
282
TEST(Lite,AllLite9)283 TEST(Lite, AllLite9) {
284 std::string data;
285
286 {
287 protobuf_unittest::TestPackedExtensionsLite message, message2;
288 protobuf_unittest::TestEmptyMessageLite empty_message;
289 TestUtilLite::ExpectPackedExtensionsClear(message);
290 TestUtilLite::SetPackedExtensions(&message);
291 data = message.SerializeAsString();
292 empty_message.ParseFromString(data);
293 data.clear();
294 data = empty_message.SerializeAsString();
295 message2.ParseFromString(data);
296 data = message2.SerializeAsString();
297 TestUtilLite::ExpectPackedExtensionsSet(message2);
298 message.Clear();
299 TestUtilLite::ExpectPackedExtensionsClear(message);
300 }
301 }
302
TEST(Lite,AllLite10)303 TEST(Lite, AllLite10) {
304 std::string data;
305
306 {
307 // Test Unknown fields swap
308 protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
309 SetAllTypesInEmptyMessageUnknownFields(&empty_message);
310 SetSomeTypesInEmptyMessageUnknownFields(&empty_message2);
311 data = empty_message.SerializeAsString();
312 std::string data2 = empty_message2.SerializeAsString();
313 empty_message.Swap(&empty_message2);
314 EXPECT_EQ(data, empty_message2.SerializeAsString());
315 EXPECT_EQ(data2, empty_message.SerializeAsString());
316 }
317 }
318
TEST(Lite,AllLite11)319 TEST(Lite, AllLite11) {
320 std::string data;
321
322 {
323 // Test unknown fields swap with self
324 protobuf_unittest::TestEmptyMessageLite empty_message;
325 SetAllTypesInEmptyMessageUnknownFields(&empty_message);
326 data = empty_message.SerializeAsString();
327 empty_message.Swap(&empty_message);
328 EXPECT_EQ(data, empty_message.SerializeAsString());
329 }
330 }
331
TEST(Lite,AllLite12)332 TEST(Lite, AllLite12) {
333 std::string data;
334
335 {
336 // Test MergeFrom with unknown fields
337 protobuf_unittest::TestAllTypesLite message, message2;
338 protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
339 message.set_optional_int32(101);
340 message.add_repeated_int32(201);
341 message.set_optional_nested_enum(unittest::TestAllTypesLite::BAZ);
342 message2.set_optional_int64(102);
343 message2.add_repeated_int64(202);
344 message2.set_optional_foreign_enum(unittest::FOREIGN_LITE_BAZ);
345
346 data = message.SerializeAsString();
347 empty_message.ParseFromString(data);
348 data = message2.SerializeAsString();
349 empty_message2.ParseFromString(data);
350 message.MergeFrom(message2);
351 empty_message.MergeFrom(empty_message2);
352
353 data = empty_message.SerializeAsString();
354 message2.ParseFromString(data);
355 // We do not compare the serialized output of a normal message and a lite
356 // message because the order of fields do not match. We convert lite message
357 // back into normal message, then compare.
358 EXPECT_EQ(message.SerializeAsString(), message2.SerializeAsString());
359 }
360 }
361
TEST(Lite,AllLite13)362 TEST(Lite, AllLite13) {
363 std::string data;
364
365 {
366 // Test unknown enum value
367 protobuf_unittest::TestAllTypesLite message;
368 std::string buffer;
369 {
370 io::StringOutputStream output_stream(&buffer);
371 io::CodedOutputStream coded_output(&output_stream);
372 internal::WireFormatLite::WriteTag(
373 protobuf_unittest::TestAllTypesLite::kOptionalNestedEnumFieldNumber,
374 internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
375 coded_output.WriteVarint32(10);
376 internal::WireFormatLite::WriteTag(
377 protobuf_unittest::TestAllTypesLite::kRepeatedNestedEnumFieldNumber,
378 internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
379 coded_output.WriteVarint32(20);
380 }
381 message.ParseFromString(buffer);
382 data = message.SerializeAsString();
383 EXPECT_EQ(data, buffer);
384 }
385 }
386
TEST(Lite,AllLite14)387 TEST(Lite, AllLite14) {
388 std::string data;
389
390 {
391 // Test Clear with unknown fields
392 protobuf_unittest::TestEmptyMessageLite empty_message;
393 SetAllTypesInEmptyMessageUnknownFields(&empty_message);
394 empty_message.Clear();
395 EXPECT_EQ(0, empty_message.unknown_fields().size());
396 }
397 }
398
399 // Tests for map lite =============================================
400
TEST(Lite,AllLite15)401 TEST(Lite, AllLite15) {
402 std::string data;
403
404 {
405 // Accessors
406 protobuf_unittest::TestMapLite message;
407
408 MapLiteTestUtil::SetMapFields(&message);
409 MapLiteTestUtil::ExpectMapFieldsSet(message);
410
411 MapLiteTestUtil::ModifyMapFields(&message);
412 MapLiteTestUtil::ExpectMapFieldsModified(message);
413 }
414 }
415
TEST(Lite,AllLite16)416 TEST(Lite, AllLite16) {
417 std::string data;
418
419 {
420 // SetMapFieldsInitialized
421 protobuf_unittest::TestMapLite message;
422
423 MapLiteTestUtil::SetMapFieldsInitialized(&message);
424 MapLiteTestUtil::ExpectMapFieldsSetInitialized(message);
425 }
426 }
427
TEST(Lite,AllLite17)428 TEST(Lite, AllLite17) {
429 std::string data;
430
431 {
432 // Clear
433 protobuf_unittest::TestMapLite message;
434
435 MapLiteTestUtil::SetMapFields(&message);
436 message.Clear();
437 MapLiteTestUtil::ExpectClear(message);
438 }
439 }
440
TEST(Lite,AllLite18)441 TEST(Lite, AllLite18) {
442 std::string data;
443
444 {
445 // ClearMessageMap
446 protobuf_unittest::TestMessageMapLite message;
447
448 // Creates a TestAllTypes with default value
449 TestUtilLite::ExpectClear((*message.mutable_map_int32_message())[0]);
450 }
451 }
452
TEST(Lite,AllLite19)453 TEST(Lite, AllLite19) {
454 std::string data;
455
456 {
457 // CopyFrom
458 protobuf_unittest::TestMapLite message1, message2;
459
460 MapLiteTestUtil::SetMapFields(&message1);
461 message2.CopyFrom(message1);
462 MapLiteTestUtil::ExpectMapFieldsSet(message2);
463
464 // Copying from self should be a no-op.
465 message2.CopyFrom(message2);
466 MapLiteTestUtil::ExpectMapFieldsSet(message2);
467 }
468 }
469
TEST(Lite,AllLite20)470 TEST(Lite, AllLite20) {
471 std::string data;
472
473 {
474 // CopyFromMessageMap
475 protobuf_unittest::TestMessageMapLite message1, message2;
476
477 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
478 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
479
480 message1.CopyFrom(message2);
481
482 // Checks repeated field is overwritten.
483 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
484 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
485 }
486 }
487
TEST(Lite,AllLite21)488 TEST(Lite, AllLite21) {
489 std::string data;
490
491 {
492 // SwapWithEmpty
493 protobuf_unittest::TestMapLite message1, message2;
494
495 MapLiteTestUtil::SetMapFields(&message1);
496 MapLiteTestUtil::ExpectMapFieldsSet(message1);
497 MapLiteTestUtil::ExpectClear(message2);
498
499 message1.Swap(&message2);
500 MapLiteTestUtil::ExpectMapFieldsSet(message2);
501 MapLiteTestUtil::ExpectClear(message1);
502 }
503 }
504
TEST(Lite,AllLite22)505 TEST(Lite, AllLite22) {
506 std::string data;
507
508 {
509 // SwapWithSelf
510 protobuf_unittest::TestMapLite message;
511
512 MapLiteTestUtil::SetMapFields(&message);
513 MapLiteTestUtil::ExpectMapFieldsSet(message);
514
515 message.Swap(&message);
516 MapLiteTestUtil::ExpectMapFieldsSet(message);
517 }
518 }
519
TEST(Lite,AllLite23)520 TEST(Lite, AllLite23) {
521 std::string data;
522
523 {
524 // SwapWithOther
525 protobuf_unittest::TestMapLite message1, message2;
526
527 MapLiteTestUtil::SetMapFields(&message1);
528 MapLiteTestUtil::SetMapFields(&message2);
529 MapLiteTestUtil::ModifyMapFields(&message2);
530
531 message1.Swap(&message2);
532 MapLiteTestUtil::ExpectMapFieldsModified(message1);
533 MapLiteTestUtil::ExpectMapFieldsSet(message2);
534 }
535 }
536
TEST(Lite,AllLite24)537 TEST(Lite, AllLite24) {
538 std::string data;
539
540 {
541 // CopyConstructor
542 protobuf_unittest::TestMapLite message1;
543 MapLiteTestUtil::SetMapFields(&message1);
544
545 protobuf_unittest::TestMapLite message2(message1);
546 MapLiteTestUtil::ExpectMapFieldsSet(message2);
547 }
548 }
549
TEST(Lite,AllLite25)550 TEST(Lite, AllLite25) {
551 std::string data;
552
553 {
554 // CopyAssignmentOperator
555 protobuf_unittest::TestMapLite message1;
556 MapLiteTestUtil::SetMapFields(&message1);
557
558 protobuf_unittest::TestMapLite message2;
559 message2 = message1;
560 MapLiteTestUtil::ExpectMapFieldsSet(message2);
561
562 // Make sure that self-assignment does something sane.
563 message2.operator=(message2);
564 MapLiteTestUtil::ExpectMapFieldsSet(message2);
565 }
566 }
567
TEST(Lite,AllLite26)568 TEST(Lite, AllLite26) {
569 std::string data;
570
571 {
572 // NonEmptyMergeFrom
573 protobuf_unittest::TestMapLite message1, message2;
574
575 MapLiteTestUtil::SetMapFields(&message1);
576
577 // This field will test merging into an empty spot.
578 (*message2.mutable_map_int32_int32())[1] = 1;
579 message1.mutable_map_int32_int32()->erase(1);
580
581 // This tests overwriting.
582 (*message2.mutable_map_int32_double())[1] = 1;
583 (*message1.mutable_map_int32_double())[1] = 2;
584
585 message1.MergeFrom(message2);
586 MapLiteTestUtil::ExpectMapFieldsSet(message1);
587 }
588 }
589
TEST(Lite,AllLite27)590 TEST(Lite, AllLite27) {
591 std::string data;
592
593 {
594 // MergeFromMessageMap
595 protobuf_unittest::TestMessageMapLite message1, message2;
596
597 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
598 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
599
600 message1.MergeFrom(message2);
601
602 // Checks repeated field is overwritten.
603 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
604 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
605 }
606 }
607
TEST(Lite,AllLite28)608 TEST(Lite, AllLite28) {
609 std::string data;
610
611 {
612 // Test the generated SerializeWithCachedSizesToArray()
613 protobuf_unittest::TestMapLite message1, message2;
614 std::string data;
615 MapLiteTestUtil::SetMapFields(&message1);
616 size_t size = message1.ByteSizeLong();
617 data.resize(size);
618 ::google::protobuf::uint8* start = reinterpret_cast<::google::protobuf::uint8*>(::google::protobuf::string_as_array(&data));
619 ::google::protobuf::uint8* end = message1.SerializeWithCachedSizesToArray(start);
620 EXPECT_EQ(size, end - start);
621 EXPECT_TRUE(message2.ParseFromString(data));
622 MapLiteTestUtil::ExpectMapFieldsSet(message2);
623 }
624 }
625
TEST(Lite,AllLite29)626 TEST(Lite, AllLite29) {
627 std::string data;
628
629 {
630 // Test the generated SerializeWithCachedSizes()
631 protobuf_unittest::TestMapLite message1, message2;
632 MapLiteTestUtil::SetMapFields(&message1);
633 size_t size = message1.ByteSizeLong();
634 std::string data;
635 data.resize(size);
636 {
637 // Allow the output stream to buffer only one byte at a time.
638 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1);
639 io::CodedOutputStream output_stream(&array_stream);
640 message1.SerializeWithCachedSizes(&output_stream);
641 EXPECT_FALSE(output_stream.HadError());
642 EXPECT_EQ(size, output_stream.ByteCount());
643 }
644 EXPECT_TRUE(message2.ParseFromString(data));
645 MapLiteTestUtil::ExpectMapFieldsSet(message2);
646 }
647 }
648
649
TEST(Lite,AllLite32)650 TEST(Lite, AllLite32) {
651 std::string data;
652
653 {
654 // Proto2UnknownEnum
655 protobuf_unittest::TestEnumMapPlusExtraLite from;
656 (*from.mutable_known_map_field())[0] =
657 protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE;
658 (*from.mutable_unknown_map_field())[0] =
659 protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE;
660 std::string data;
661 from.SerializeToString(&data);
662
663 protobuf_unittest::TestEnumMapLite to;
664 EXPECT_TRUE(to.ParseFromString(data));
665 EXPECT_EQ(0, to.unknown_map_field().size());
666 EXPECT_FALSE(to.mutable_unknown_fields()->empty());
667 ASSERT_EQ(1, to.known_map_field().size());
668 EXPECT_EQ(protobuf_unittest::PROTO2_MAP_ENUM_FOO_LITE,
669 to.known_map_field().at(0));
670
671 data.clear();
672 from.Clear();
673 to.SerializeToString(&data);
674 EXPECT_TRUE(from.ParseFromString(data));
675 ASSERT_EQ(1, from.known_map_field().size());
676 EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE,
677 from.known_map_field().at(0));
678 ASSERT_EQ(1, from.unknown_map_field().size());
679 EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE,
680 from.unknown_map_field().at(0));
681 }
682 }
683
TEST(Lite,AllLite33)684 TEST(Lite, AllLite33) {
685 std::string data;
686
687 {
688 // StandardWireFormat
689 protobuf_unittest::TestMapLite message;
690 std::string data = "\x0A\x04\x08\x01\x10\x01";
691
692 EXPECT_TRUE(message.ParseFromString(data));
693 ASSERT_EQ(1, message.map_int32_int32().size());
694 EXPECT_EQ(1, message.map_int32_int32().at(1));
695 }
696 }
697
TEST(Lite,AllLite34)698 TEST(Lite, AllLite34) {
699 std::string data;
700
701 {
702 // UnorderedWireFormat
703 protobuf_unittest::TestMapLite message;
704
705 // put value before key in wire format
706 std::string data = "\x0A\x04\x10\x01\x08\x02";
707
708 EXPECT_TRUE(message.ParseFromString(data));
709 ASSERT_EQ(1, message.map_int32_int32().size());
710 ASSERT_NE(message.map_int32_int32().find(2),
711 message.map_int32_int32().end());
712 EXPECT_EQ(1, message.map_int32_int32().at(2));
713 }
714 }
715
TEST(Lite,AllLite35)716 TEST(Lite, AllLite35) {
717 std::string data;
718
719 {
720 // DuplicatedKeyWireFormat
721 protobuf_unittest::TestMapLite message;
722
723 // Two key fields in wire format
724 std::string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
725
726 EXPECT_TRUE(message.ParseFromString(data));
727 ASSERT_EQ(1, message.map_int32_int32().size());
728 EXPECT_EQ(1, message.map_int32_int32().at(2));
729 }
730 }
731
TEST(Lite,AllLite36)732 TEST(Lite, AllLite36) {
733 std::string data;
734
735 {
736 // DuplicatedValueWireFormat
737 protobuf_unittest::TestMapLite message;
738
739 // Two value fields in wire format
740 std::string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
741
742 EXPECT_TRUE(message.ParseFromString(data));
743 ASSERT_EQ(1, message.map_int32_int32().size());
744 EXPECT_EQ(2, message.map_int32_int32().at(1));
745 }
746 }
747
TEST(Lite,AllLite37)748 TEST(Lite, AllLite37) {
749 std::string data;
750
751 {
752 // MissedKeyWireFormat
753 protobuf_unittest::TestMapLite message;
754
755 // No key field in wire format
756 std::string data = "\x0A\x02\x10\x01";
757
758 EXPECT_TRUE(message.ParseFromString(data));
759 ASSERT_EQ(1, message.map_int32_int32().size());
760 ASSERT_NE(message.map_int32_int32().find(0),
761 message.map_int32_int32().end());
762 EXPECT_EQ(1, message.map_int32_int32().at(0));
763 }
764 }
765
TEST(Lite,AllLite38)766 TEST(Lite, AllLite38) {
767 std::string data;
768
769 {
770 // MissedValueWireFormat
771 protobuf_unittest::TestMapLite message;
772
773 // No value field in wire format
774 std::string data = "\x0A\x02\x08\x01";
775
776 EXPECT_TRUE(message.ParseFromString(data));
777 ASSERT_EQ(1, message.map_int32_int32().size());
778 ASSERT_NE(message.map_int32_int32().find(1),
779 message.map_int32_int32().end());
780 EXPECT_EQ(0, message.map_int32_int32().at(1));
781 }
782 }
783
TEST(Lite,AllLite39)784 TEST(Lite, AllLite39) {
785 std::string data;
786
787 {
788 // UnknownFieldWireFormat
789 protobuf_unittest::TestMapLite message;
790
791 // Unknown field in wire format
792 std::string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
793
794 EXPECT_TRUE(message.ParseFromString(data));
795 ASSERT_EQ(1, message.map_int32_int32().size());
796 EXPECT_EQ(3, message.map_int32_int32().at(2));
797 }
798 }
799
TEST(Lite,AllLite40)800 TEST(Lite, AllLite40) {
801 std::string data;
802
803 {
804 // CorruptedWireFormat
805 protobuf_unittest::TestMapLite message;
806
807 // corrupted data in wire format
808 std::string data = "\x0A\x06\x08\x02\x11\x03";
809
810 EXPECT_FALSE(message.ParseFromString(data));
811 }
812 }
813
TEST(Lite,AllLite41)814 TEST(Lite, AllLite41) {
815 std::string data;
816
817 {
818 // IsInitialized
819 protobuf_unittest::TestRequiredMessageMapLite map_message;
820
821 // Add an uninitialized message.
822 (*map_message.mutable_map_field())[0];
823 EXPECT_FALSE(map_message.IsInitialized());
824
825 // Initialize uninitialized message
826 (*map_message.mutable_map_field())[0].set_a(0);
827 (*map_message.mutable_map_field())[0].set_b(0);
828 (*map_message.mutable_map_field())[0].set_c(0);
829 EXPECT_TRUE(map_message.IsInitialized());
830 }
831 }
832
TEST(Lite,AllLite42)833 TEST(Lite, AllLite42) {
834 std::string data;
835
836 {
837 // Check that adding more values to enum does not corrupt message
838 // when passed through an old client.
839 protobuf_unittest::V2MessageLite v2_message;
840 v2_message.set_int_field(800);
841 // Set enum field to the value not understood by the old client.
842 v2_message.set_enum_field(protobuf_unittest::V2_SECOND);
843 std::string v2_bytes = v2_message.SerializeAsString();
844
845 protobuf_unittest::V1MessageLite v1_message;
846 v1_message.ParseFromString(v2_bytes);
847 EXPECT_TRUE(v1_message.IsInitialized());
848 EXPECT_EQ(v1_message.int_field(), v2_message.int_field());
849 // V1 client does not understand V2_SECOND value, so it discards it and
850 // uses default value instead.
851 EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST);
852
853 // However, when re-serialized, it should preserve enum value.
854 std::string v1_bytes = v1_message.SerializeAsString();
855
856 protobuf_unittest::V2MessageLite same_v2_message;
857 same_v2_message.ParseFromString(v1_bytes);
858
859 EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field());
860 EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field());
861 }
862 }
863
864 // Test that when parsing a oneof, we can successfully clear whatever already
865 // happened to be stored in the oneof.
TEST(Lite,AllLite43)866 TEST(Lite, AllLite43) {
867 protobuf_unittest::TestOneofParsingLite message1;
868
869 message1.set_oneof_int32(17);
870 std::string serialized;
871 EXPECT_TRUE(message1.SerializeToString(&serialized));
872
873 // Submessage
874 {
875 protobuf_unittest::TestOneofParsingLite message2;
876 message2.mutable_oneof_submessage();
877 io::CodedInputStream input_stream(
878 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
879 EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
880 EXPECT_EQ(17, message2.oneof_int32());
881 }
882
883 // String
884 {
885 protobuf_unittest::TestOneofParsingLite message2;
886 message2.set_oneof_string("string");
887 io::CodedInputStream input_stream(
888 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
889 EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
890 EXPECT_EQ(17, message2.oneof_int32());
891 }
892
893 // Bytes
894 {
895 protobuf_unittest::TestOneofParsingLite message2;
896 message2.set_oneof_bytes("bytes");
897 io::CodedInputStream input_stream(
898 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
899 EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
900 EXPECT_EQ(17, message2.oneof_int32());
901 }
902 }
903
904 // Verify that we can successfully parse fields of various types within oneof
905 // fields. We also verify that we can parse the same data twice into the same
906 // message.
TEST(Lite,AllLite44)907 TEST(Lite, AllLite44) {
908 // Int32
909 {
910 protobuf_unittest::TestOneofParsingLite original;
911 original.set_oneof_int32(17);
912 std::string serialized;
913 EXPECT_TRUE(original.SerializeToString(&serialized));
914 protobuf_unittest::TestOneofParsingLite parsed;
915 for (int i = 0; i < 2; ++i) {
916 io::CodedInputStream input_stream(
917 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
918 serialized.size());
919 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
920 EXPECT_EQ(17, parsed.oneof_int32());
921 }
922 }
923
924 // Submessage
925 {
926 protobuf_unittest::TestOneofParsingLite original;
927 original.mutable_oneof_submessage()->set_optional_int32(5);
928 std::string serialized;
929 EXPECT_TRUE(original.SerializeToString(&serialized));
930 protobuf_unittest::TestOneofParsingLite parsed;
931 for (int i = 0; i < 2; ++i) {
932 io::CodedInputStream input_stream(
933 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
934 serialized.size());
935 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
936 EXPECT_EQ(5, parsed.oneof_submessage().optional_int32());
937 }
938 }
939
940 // String
941 {
942 protobuf_unittest::TestOneofParsingLite original;
943 original.set_oneof_string("string");
944 std::string serialized;
945 EXPECT_TRUE(original.SerializeToString(&serialized));
946 protobuf_unittest::TestOneofParsingLite parsed;
947 for (int i = 0; i < 2; ++i) {
948 io::CodedInputStream input_stream(
949 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
950 serialized.size());
951 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
952 EXPECT_EQ("string", parsed.oneof_string());
953 }
954 }
955
956 // Bytes
957 {
958 protobuf_unittest::TestOneofParsingLite original;
959 original.set_oneof_bytes("bytes");
960 std::string serialized;
961 EXPECT_TRUE(original.SerializeToString(&serialized));
962 protobuf_unittest::TestOneofParsingLite parsed;
963 for (int i = 0; i < 2; ++i) {
964 io::CodedInputStream input_stream(
965 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
966 serialized.size());
967 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
968 EXPECT_EQ("bytes", parsed.oneof_bytes());
969 }
970 }
971
972 // Enum
973 {
974 protobuf_unittest::TestOneofParsingLite original;
975 original.set_oneof_enum(protobuf_unittest::V2_SECOND);
976 std::string serialized;
977 EXPECT_TRUE(original.SerializeToString(&serialized));
978 protobuf_unittest::TestOneofParsingLite parsed;
979 for (int i = 0; i < 2; ++i) {
980 io::CodedInputStream input_stream(
981 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
982 serialized.size());
983 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
984 EXPECT_EQ(protobuf_unittest::V2_SECOND, parsed.oneof_enum());
985 }
986 }
987
988 std::cout << "PASS" << std::endl;
989 }
990
TEST(Lite,AllLite45)991 TEST(Lite, AllLite45) {
992 // Test unknown fields are not discarded upon parsing.
993 std::string data = "\20\1"; // varint 1 with field number 2
994
995 protobuf_unittest::ForeignMessageLite a;
996 EXPECT_TRUE(a.ParseFromString(data));
997 io::CodedInputStream input_stream(
998 reinterpret_cast<const ::google::protobuf::uint8*>(data.data()), data.size());
999 EXPECT_TRUE(a.MergePartialFromCodedStream(&input_stream));
1000
1001 std::string serialized = a.SerializeAsString();
1002 EXPECT_EQ(serialized.substr(0, 2), data);
1003 EXPECT_EQ(serialized.substr(2), data);
1004 }
1005
1006 // The following two tests check for wire compatibility between packed and
1007 // unpacked repeated fields. There used to be a bug in the generated parsing
1008 // code that caused us to calculate the highest possible tag number without
1009 // taking into account that a repeated field might not be in the packed (or
1010 // unpacked) state we expect. These tests specifically check for that issue by
1011 // making sure we can parse repeated fields when the tag is higher than we would
1012 // expect.
TEST(Lite,AllLite46)1013 TEST(Lite, AllLite46) {
1014 protobuf_unittest::PackedInt32 packed;
1015 packed.add_repeated_int32(42);
1016 std::string serialized;
1017 ASSERT_TRUE(packed.SerializeToString(&serialized));
1018
1019 protobuf_unittest::NonPackedInt32 non_packed;
1020 ASSERT_TRUE(non_packed.ParseFromString(serialized));
1021 ASSERT_EQ(1, non_packed.repeated_int32_size());
1022 EXPECT_EQ(42, non_packed.repeated_int32(0));
1023 }
1024
TEST(Lite,AllLite47)1025 TEST(Lite, AllLite47) {
1026 protobuf_unittest::NonPackedFixed32 non_packed;
1027 non_packed.add_repeated_fixed32(42);
1028 std::string serialized;
1029 ASSERT_TRUE(non_packed.SerializeToString(&serialized));
1030
1031 protobuf_unittest::PackedFixed32 packed;
1032 ASSERT_TRUE(packed.ParseFromString(serialized));
1033 ASSERT_EQ(1, packed.repeated_fixed32_size());
1034 EXPECT_EQ(42, packed.repeated_fixed32(0));
1035 }
1036
TEST(Lite,MapCrash)1037 TEST(Lite, MapCrash) {
1038 // See b/113635730
1039 Arena arena;
1040 auto msg = Arena::CreateMessage<protobuf_unittest::TestMapLite>(&arena);
1041 // Payload for the map<string, Enum> with a enum varint that's longer >
1042 // 10 bytes. This causes a parse fail and a subsequent delete. field 16
1043 // (map<int32, MapEnumLite>) tag = 128+2 = \202 \1
1044 // 13 long \15
1045 // int32 key = 1 (\10 \1)
1046 // MapEnumLite value = too long varint (parse error)
1047 EXPECT_FALSE(msg->ParseFromString(
1048 "\202\1\15\10\1\200\200\200\200\200\200\200\200\200\200\1"));
1049 }
1050
TEST(Lite,CorrectEnding)1051 TEST(Lite, CorrectEnding) {
1052 protobuf_unittest::TestAllTypesLite msg;
1053 {
1054 // All proto wireformat parsers should act the same on parsing data in as
1055 // much as it concerns the parsing, ie. not the interpretation of the data.
1056 // TestAllTypesLite is not a group inside another message. So in practice
1057 // will not encounter an end-group tag. However the parser should behave
1058 // like any wire format parser should.
1059 static const char kWireFormat[] = "\204\1";
1060 io::CodedInputStream cis(reinterpret_cast<const uint8*>(kWireFormat), 2);
1061 // The old CodedInputStream parser got an optimization (ReadTagNoLastTag)
1062 // for non-group messages (like TestAllTypesLite) which made it not accept
1063 // end-group. This is not a real big deal, but I think going forward its
1064 // good to have all parse loops behave 'exactly' the same.
1065 EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis));
1066 EXPECT_FALSE(cis.ConsumedEntireMessage());
1067 EXPECT_TRUE(cis.LastTagWas(132));
1068 }
1069 {
1070 // This is an incomplete end-group tag. This should be a genuine parse
1071 // failure.
1072 static const char kWireFormat[] = "\214";
1073 io::CodedInputStream cis(reinterpret_cast<const uint8*>(kWireFormat), 1);
1074 // Unfortunately the old parser detects a parse error in ReadTag and returns
1075 // 0 (as it states 0 is an invalid tag). However 0 is not an invalid tag
1076 // as it can be used to terminate the stream, so this returns true.
1077 EXPECT_FALSE(msg.MergePartialFromCodedStream(&cis));
1078 }
1079 }
1080
TEST(Lite,DebugString)1081 TEST(Lite, DebugString) {
1082 protobuf_unittest::TestAllTypesLite message1, message2;
1083 EXPECT_TRUE(HasPrefixString(message1.DebugString(), "MessageLite at 0x"));
1084 EXPECT_TRUE(HasPrefixString(message2.DebugString(), "MessageLite at 0x"));
1085
1086 // DebugString() and ShortDebugString() are the same for now.
1087 EXPECT_EQ(message1.DebugString(), message1.ShortDebugString());
1088
1089 // Even identical lite protos should have different DebugString() output. Part
1090 // of the reason for including the memory address is so that we get some
1091 // non-determinism, which should make it easier for us to change the output
1092 // later without breaking any code.
1093 EXPECT_NE(message1.DebugString(), message2.DebugString());
1094 }
1095
TEST(Lite,EnumValueToName)1096 TEST(Lite, EnumValueToName) {
1097 EXPECT_EQ("FOREIGN_LITE_FOO", protobuf_unittest::ForeignEnumLite_Name(
1098 protobuf_unittest::FOREIGN_LITE_FOO));
1099 EXPECT_EQ("FOREIGN_LITE_BAR", protobuf_unittest::ForeignEnumLite_Name(
1100 protobuf_unittest::FOREIGN_LITE_BAR));
1101 EXPECT_EQ("FOREIGN_LITE_BAZ", protobuf_unittest::ForeignEnumLite_Name(
1102 protobuf_unittest::FOREIGN_LITE_BAZ));
1103 EXPECT_EQ("", protobuf_unittest::ForeignEnumLite_Name(0));
1104 EXPECT_EQ("", protobuf_unittest::ForeignEnumLite_Name(999));
1105 }
1106
TEST(Lite,NestedEnumValueToName)1107 TEST(Lite, NestedEnumValueToName) {
1108 EXPECT_EQ("FOO", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(
1109 protobuf_unittest::TestAllTypesLite::FOO));
1110 EXPECT_EQ("BAR", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(
1111 protobuf_unittest::TestAllTypesLite::BAR));
1112 EXPECT_EQ("BAZ", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(
1113 protobuf_unittest::TestAllTypesLite::BAZ));
1114 EXPECT_EQ("", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(0));
1115 EXPECT_EQ("", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(999));
1116 }
1117
TEST(Lite,EnumNameToValue)1118 TEST(Lite, EnumNameToValue) {
1119 protobuf_unittest::ForeignEnumLite value;
1120
1121 ASSERT_TRUE(
1122 protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_FOO", &value));
1123 EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_FOO, value);
1124
1125 ASSERT_TRUE(
1126 protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_BAR", &value));
1127 EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_BAR, value);
1128
1129 ASSERT_TRUE(
1130 protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_BAZ", &value));
1131 EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_BAZ, value);
1132
1133 // Non-existent values
1134 EXPECT_FALSE(protobuf_unittest::ForeignEnumLite_Parse("E", &value));
1135 EXPECT_FALSE(
1136 protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_C", &value));
1137 EXPECT_FALSE(protobuf_unittest::ForeignEnumLite_Parse("G", &value));
1138 }
1139
TEST(Lite,NestedEnumNameToValue)1140 TEST(Lite, NestedEnumNameToValue) {
1141 protobuf_unittest::TestAllTypesLite::NestedEnum value;
1142
1143 ASSERT_TRUE(
1144 protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("FOO", &value));
1145 EXPECT_EQ(protobuf_unittest::TestAllTypesLite::FOO, value);
1146
1147 ASSERT_TRUE(
1148 protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("BAR", &value));
1149 EXPECT_EQ(protobuf_unittest::TestAllTypesLite::BAR, value);
1150
1151 ASSERT_TRUE(
1152 protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("BAZ", &value));
1153 EXPECT_EQ(protobuf_unittest::TestAllTypesLite::BAZ, value);
1154
1155 // Non-existent values
1156 EXPECT_FALSE(
1157 protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("A", &value));
1158 EXPECT_FALSE(
1159 protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("C", &value));
1160 EXPECT_FALSE(
1161 protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("G", &value));
1162 }
1163
TEST(Lite,AliasedEnum)1164 TEST(Lite, AliasedEnum) {
1165 // Enums with allow_alias = true can have multiple entries with the same
1166 // value.
1167 EXPECT_EQ("FOO1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
1168 protobuf_unittest::DupEnum::FOO1));
1169 EXPECT_EQ("FOO1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
1170 protobuf_unittest::DupEnum::FOO2));
1171 EXPECT_EQ("BAR1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
1172 protobuf_unittest::DupEnum::BAR1));
1173 EXPECT_EQ("BAR1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
1174 protobuf_unittest::DupEnum::BAR2));
1175 EXPECT_EQ("BAZ", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
1176 protobuf_unittest::DupEnum::BAZ));
1177 EXPECT_EQ("", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(999));
1178
1179 protobuf_unittest::DupEnum::TestEnumWithDupValueLite value;
1180 ASSERT_TRUE(
1181 protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Parse("FOO1", &value));
1182 EXPECT_EQ(protobuf_unittest::DupEnum::FOO1, value);
1183
1184 value = static_cast<protobuf_unittest::DupEnum::TestEnumWithDupValueLite>(0);
1185 ASSERT_TRUE(
1186 protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Parse("FOO2", &value));
1187 EXPECT_EQ(protobuf_unittest::DupEnum::FOO2, value);
1188 }
1189
1190 } // namespace protobuf
1191 } // namespace google
1192