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