blob: b42a7b146614defc101f4ea1a4d50015681efcfd [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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 <string>
34#include <iostream>
35
36#include <google/protobuf/stubs/logging.h>
37#include <google/protobuf/stubs/common.h>
38#include <google/protobuf/arena_test_util.h>
Brian Silverman9c614bc2016-02-15 20:20:02 -050039#include <google/protobuf/map_lite_test_util.h>
Austin Schuh40c16522018-10-28 20:27:54 -070040#include <google/protobuf/map_lite_unittest.pb.h>
Brian Silverman9c614bc2016-02-15 20:20:02 -050041#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 <google/protobuf/wire_format_lite_inl.h>
Austin Schuh40c16522018-10-28 20:27:54 -070047#include <gtest/gtest.h>
48
Brian Silverman9c614bc2016-02-15 20:20:02 -050049#include <google/protobuf/stubs/strutil.h>
50
Austin Schuh40c16522018-10-28 20:27:54 -070051using std::string;
Brian Silverman9c614bc2016-02-15 20:20:02 -050052
53namespace {
54// Helper methods to test parsing merge behavior.
55void ExpectMessageMerged(const google::protobuf::unittest::TestAllTypesLite& message) {
Austin Schuh40c16522018-10-28 20:27:54 -070056 EXPECT_EQ(message.optional_int32(), 3);
57 EXPECT_EQ(message.optional_int64(), 2);
58 EXPECT_EQ(message.optional_string(), "hello");
Brian Silverman9c614bc2016-02-15 20:20:02 -050059}
60
61void AssignParsingMergeMessages(
62 google::protobuf::unittest::TestAllTypesLite* msg1,
63 google::protobuf::unittest::TestAllTypesLite* msg2,
64 google::protobuf::unittest::TestAllTypesLite* msg3) {
65 msg1->set_optional_int32(1);
66 msg2->set_optional_int64(2);
67 msg3->set_optional_int32(3);
68 msg3->set_optional_string("hello");
69}
70
71void SetAllTypesInEmptyMessageUnknownFields(
72 google::protobuf::unittest::TestEmptyMessageLite* empty_message) {
73 protobuf_unittest::TestAllTypesLite message;
74 google::protobuf::TestUtilLite::ExpectClear(message);
75 google::protobuf::TestUtilLite::SetAllFields(&message);
76 string data = message.SerializeAsString();
77 empty_message->ParseFromString(data);
78}
79
80void SetSomeTypesInEmptyMessageUnknownFields(
81 google::protobuf::unittest::TestEmptyMessageLite* empty_message) {
82 protobuf_unittest::TestAllTypesLite message;
83 google::protobuf::TestUtilLite::ExpectClear(message);
84 message.set_optional_int32(101);
85 message.set_optional_int64(102);
86 message.set_optional_uint32(103);
87 message.set_optional_uint64(104);
88 string data = message.SerializeAsString();
89 empty_message->ParseFromString(data);
90}
91
92} // namespace
93
Austin Schuh40c16522018-10-28 20:27:54 -070094TEST(Lite, AllLite1) {
95 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -050096
97 {
98 protobuf_unittest::TestAllTypesLite message, message2, message3;
99 google::protobuf::TestUtilLite::ExpectClear(message);
100 google::protobuf::TestUtilLite::SetAllFields(&message);
101 message2.CopyFrom(message);
102 data = message.SerializeAsString();
103 message3.ParseFromString(data);
104 google::protobuf::TestUtilLite::ExpectAllFieldsSet(message);
105 google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2);
106 google::protobuf::TestUtilLite::ExpectAllFieldsSet(message3);
107 google::protobuf::TestUtilLite::ModifyRepeatedFields(&message);
108 google::protobuf::TestUtilLite::ExpectRepeatedFieldsModified(message);
109 message.Clear();
110 google::protobuf::TestUtilLite::ExpectClear(message);
111 }
Austin Schuh40c16522018-10-28 20:27:54 -0700112}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500113
Austin Schuh40c16522018-10-28 20:27:54 -0700114TEST(Lite, AllLite2) {
115 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500116 {
117 protobuf_unittest::TestAllExtensionsLite message, message2, message3;
118 google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
119 google::protobuf::TestUtilLite::SetAllExtensions(&message);
120 message2.CopyFrom(message);
121 string extensions_data = message.SerializeAsString();
122 message3.ParseFromString(extensions_data);
123 google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message);
124 google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2);
125 google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message3);
126 google::protobuf::TestUtilLite::ModifyRepeatedExtensions(&message);
127 google::protobuf::TestUtilLite::ExpectRepeatedExtensionsModified(message);
128 message.Clear();
129 google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
130 }
Austin Schuh40c16522018-10-28 20:27:54 -0700131}
132
133TEST(Lite, AllLite3) {
134 string data, packed_data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500135
136 {
137 protobuf_unittest::TestPackedTypesLite message, message2, message3;
138 google::protobuf::TestUtilLite::ExpectPackedClear(message);
139 google::protobuf::TestUtilLite::SetPackedFields(&message);
140 message2.CopyFrom(message);
141 packed_data = message.SerializeAsString();
142 message3.ParseFromString(packed_data);
143 google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message);
144 google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2);
145 google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message3);
146 google::protobuf::TestUtilLite::ModifyPackedFields(&message);
147 google::protobuf::TestUtilLite::ExpectPackedFieldsModified(message);
148 message.Clear();
149 google::protobuf::TestUtilLite::ExpectPackedClear(message);
150 }
151
152 {
153 protobuf_unittest::TestPackedExtensionsLite message, message2, message3;
154 google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
155 google::protobuf::TestUtilLite::SetPackedExtensions(&message);
156 message2.CopyFrom(message);
157 string packed_extensions_data = message.SerializeAsString();
Austin Schuh40c16522018-10-28 20:27:54 -0700158 EXPECT_EQ(packed_extensions_data, packed_data);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500159 message3.ParseFromString(packed_extensions_data);
160 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message);
161 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2);
162 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message3);
163 google::protobuf::TestUtilLite::ModifyPackedExtensions(&message);
164 google::protobuf::TestUtilLite::ExpectPackedExtensionsModified(message);
165 message.Clear();
166 google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
167 }
Austin Schuh40c16522018-10-28 20:27:54 -0700168}
169
170TEST(Lite, AllLite5) {
171 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500172
173 {
174 // Test that if an optional or required message/group field appears multiple
175 // times in the input, they need to be merged.
176 google::protobuf::unittest::TestParsingMergeLite::RepeatedFieldsGenerator generator;
177 google::protobuf::unittest::TestAllTypesLite* msg1;
178 google::protobuf::unittest::TestAllTypesLite* msg2;
179 google::protobuf::unittest::TestAllTypesLite* msg3;
180
181#define ASSIGN_REPEATED_FIELD(FIELD) \
182 msg1 = generator.add_##FIELD(); \
183 msg2 = generator.add_##FIELD(); \
184 msg3 = generator.add_##FIELD(); \
185 AssignParsingMergeMessages(msg1, msg2, msg3)
186
187 ASSIGN_REPEATED_FIELD(field1);
188 ASSIGN_REPEATED_FIELD(field2);
189 ASSIGN_REPEATED_FIELD(field3);
190 ASSIGN_REPEATED_FIELD(ext1);
191 ASSIGN_REPEATED_FIELD(ext2);
192
193#undef ASSIGN_REPEATED_FIELD
194#define ASSIGN_REPEATED_GROUP(FIELD) \
195 msg1 = generator.add_##FIELD()->mutable_field1(); \
196 msg2 = generator.add_##FIELD()->mutable_field1(); \
197 msg3 = generator.add_##FIELD()->mutable_field1(); \
198 AssignParsingMergeMessages(msg1, msg2, msg3)
199
200 ASSIGN_REPEATED_GROUP(group1);
201 ASSIGN_REPEATED_GROUP(group2);
202
203#undef ASSIGN_REPEATED_GROUP
204
205 string buffer;
206 generator.SerializeToString(&buffer);
207 google::protobuf::unittest::TestParsingMergeLite parsing_merge;
208 parsing_merge.ParseFromString(buffer);
209
210 // Required and optional fields should be merged.
211 ExpectMessageMerged(parsing_merge.required_all_types());
212 ExpectMessageMerged(parsing_merge.optional_all_types());
213 ExpectMessageMerged(
214 parsing_merge.optionalgroup().optional_group_all_types());
215 ExpectMessageMerged(parsing_merge.GetExtension(
216 google::protobuf::unittest::TestParsingMergeLite::optional_ext));
217
218 // Repeated fields should not be merged.
Austin Schuh40c16522018-10-28 20:27:54 -0700219 EXPECT_EQ(parsing_merge.repeated_all_types_size(), 3);
220 EXPECT_EQ(parsing_merge.repeatedgroup_size(), 3);
221 EXPECT_EQ(parsing_merge.ExtensionSize(
222 google::protobuf::unittest::TestParsingMergeLite::repeated_ext),
223 3);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500224 }
Austin Schuh40c16522018-10-28 20:27:54 -0700225}
226
227TEST(Lite, AllLite6) {
228 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500229
230 // Test unknown fields support for lite messages.
231 {
232 protobuf_unittest::TestAllTypesLite message, message2;
233 protobuf_unittest::TestEmptyMessageLite empty_message;
234 google::protobuf::TestUtilLite::ExpectClear(message);
235 google::protobuf::TestUtilLite::SetAllFields(&message);
236 data = message.SerializeAsString();
237 empty_message.ParseFromString(data);
238 data.clear();
239 data = empty_message.SerializeAsString();
240 message2.ParseFromString(data);
241 data = message2.SerializeAsString();
242 google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2);
243 message.Clear();
244 google::protobuf::TestUtilLite::ExpectClear(message);
245 }
Austin Schuh40c16522018-10-28 20:27:54 -0700246}
247
248TEST(Lite, AllLite7) {
249 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500250
251 {
252 protobuf_unittest::TestAllExtensionsLite message, message2;
253 protobuf_unittest::TestEmptyMessageLite empty_message;
254 google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
255 google::protobuf::TestUtilLite::SetAllExtensions(&message);
256 data = message.SerializeAsString();
257 empty_message.ParseFromString(data);
258 data.clear();
259 data = empty_message.SerializeAsString();
260 message2.ParseFromString(data);
261 data = message2.SerializeAsString();
262 google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2);
263 message.Clear();
264 google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
265 }
Austin Schuh40c16522018-10-28 20:27:54 -0700266}
267
268TEST(Lite, AllLite8) {
269 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500270
271 {
272 protobuf_unittest::TestPackedTypesLite message, message2;
273 protobuf_unittest::TestEmptyMessageLite empty_message;
274 google::protobuf::TestUtilLite::ExpectPackedClear(message);
275 google::protobuf::TestUtilLite::SetPackedFields(&message);
276 data = message.SerializeAsString();
277 empty_message.ParseFromString(data);
278 data.clear();
279 data = empty_message.SerializeAsString();
280 message2.ParseFromString(data);
281 data = message2.SerializeAsString();
282 google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2);
283 message.Clear();
284 google::protobuf::TestUtilLite::ExpectPackedClear(message);
285 }
Austin Schuh40c16522018-10-28 20:27:54 -0700286}
287
288TEST(Lite, AllLite9) {
289 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500290
291 {
292 protobuf_unittest::TestPackedExtensionsLite message, message2;
293 protobuf_unittest::TestEmptyMessageLite empty_message;
294 google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
295 google::protobuf::TestUtilLite::SetPackedExtensions(&message);
296 data = message.SerializeAsString();
297 empty_message.ParseFromString(data);
298 data.clear();
299 data = empty_message.SerializeAsString();
300 message2.ParseFromString(data);
301 data = message2.SerializeAsString();
302 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2);
303 message.Clear();
304 google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
305 }
Austin Schuh40c16522018-10-28 20:27:54 -0700306}
307
308TEST(Lite, AllLite10) {
309 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500310
311 {
312 // Test Unknown fields swap
313 protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
314 SetAllTypesInEmptyMessageUnknownFields(&empty_message);
315 SetSomeTypesInEmptyMessageUnknownFields(&empty_message2);
316 data = empty_message.SerializeAsString();
Austin Schuh40c16522018-10-28 20:27:54 -0700317 string data2 = empty_message2.SerializeAsString();
Brian Silverman9c614bc2016-02-15 20:20:02 -0500318 empty_message.Swap(&empty_message2);
Austin Schuh40c16522018-10-28 20:27:54 -0700319 EXPECT_EQ(data, empty_message2.SerializeAsString());
320 EXPECT_EQ(data2, empty_message.SerializeAsString());
Brian Silverman9c614bc2016-02-15 20:20:02 -0500321 }
Austin Schuh40c16522018-10-28 20:27:54 -0700322}
323
324TEST(Lite, AllLite11) {
325 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500326
327 {
328 // Test unknown fields swap with self
329 protobuf_unittest::TestEmptyMessageLite empty_message;
330 SetAllTypesInEmptyMessageUnknownFields(&empty_message);
331 data = empty_message.SerializeAsString();
332 empty_message.Swap(&empty_message);
Austin Schuh40c16522018-10-28 20:27:54 -0700333 EXPECT_EQ(data, empty_message.SerializeAsString());
Brian Silverman9c614bc2016-02-15 20:20:02 -0500334 }
Austin Schuh40c16522018-10-28 20:27:54 -0700335}
336
337TEST(Lite, AllLite12) {
338 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500339
340 {
341 // Test MergeFrom with unknown fields
342 protobuf_unittest::TestAllTypesLite message, message2;
343 protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
344 message.set_optional_int32(101);
345 message.add_repeated_int32(201);
346 message.set_optional_nested_enum(google::protobuf::unittest::TestAllTypesLite::BAZ);
347 message2.set_optional_int64(102);
348 message2.add_repeated_int64(202);
349 message2.set_optional_foreign_enum(google::protobuf::unittest::FOREIGN_LITE_BAZ);
350
351 data = message.SerializeAsString();
352 empty_message.ParseFromString(data);
353 data = message2.SerializeAsString();
354 empty_message2.ParseFromString(data);
355 message.MergeFrom(message2);
356 empty_message.MergeFrom(empty_message2);
357
358 data = empty_message.SerializeAsString();
359 message2.ParseFromString(data);
360 // We do not compare the serialized output of a normal message and a lite
361 // message because the order of fields do not match. We convert lite message
362 // back into normal message, then compare.
Austin Schuh40c16522018-10-28 20:27:54 -0700363 EXPECT_EQ(message.SerializeAsString(), message2.SerializeAsString());
Brian Silverman9c614bc2016-02-15 20:20:02 -0500364 }
Austin Schuh40c16522018-10-28 20:27:54 -0700365}
366
367TEST(Lite, AllLite13) {
368 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500369
370 {
371 // Test unknown enum value
372 protobuf_unittest::TestAllTypesLite message;
373 string buffer;
374 {
375 google::protobuf::io::StringOutputStream output_stream(&buffer);
376 google::protobuf::io::CodedOutputStream coded_output(&output_stream);
377 google::protobuf::internal::WireFormatLite::WriteTag(
378 protobuf_unittest::TestAllTypesLite::kOptionalNestedEnumFieldNumber,
379 google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
380 coded_output.WriteVarint32(10);
381 google::protobuf::internal::WireFormatLite::WriteTag(
382 protobuf_unittest::TestAllTypesLite::kRepeatedNestedEnumFieldNumber,
383 google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
384 coded_output.WriteVarint32(20);
385 }
386 message.ParseFromString(buffer);
387 data = message.SerializeAsString();
Austin Schuh40c16522018-10-28 20:27:54 -0700388 EXPECT_EQ(data, buffer);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500389 }
Austin Schuh40c16522018-10-28 20:27:54 -0700390}
391
392TEST(Lite, AllLite14) {
393 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500394
395 {
396 // Test Clear with unknown fields
397 protobuf_unittest::TestEmptyMessageLite empty_message;
398 SetAllTypesInEmptyMessageUnknownFields(&empty_message);
399 empty_message.Clear();
Austin Schuh40c16522018-10-28 20:27:54 -0700400 EXPECT_EQ(0, empty_message.unknown_fields().size());
Brian Silverman9c614bc2016-02-15 20:20:02 -0500401 }
Austin Schuh40c16522018-10-28 20:27:54 -0700402}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500403
Austin Schuh40c16522018-10-28 20:27:54 -0700404// Tests for map lite =============================================
405
406TEST(Lite, AllLite15) {
407 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500408
409 {
410 // Accessors
411 protobuf_unittest::TestMapLite message;
412
413 google::protobuf::MapLiteTestUtil::SetMapFields(&message);
414 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message);
415
416 google::protobuf::MapLiteTestUtil::ModifyMapFields(&message);
417 google::protobuf::MapLiteTestUtil::ExpectMapFieldsModified(message);
418 }
Austin Schuh40c16522018-10-28 20:27:54 -0700419}
420
421TEST(Lite, AllLite16) {
422 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500423
424 {
425 // SetMapFieldsInitialized
426 protobuf_unittest::TestMapLite message;
427
428 google::protobuf::MapLiteTestUtil::SetMapFieldsInitialized(&message);
429 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSetInitialized(message);
430 }
Austin Schuh40c16522018-10-28 20:27:54 -0700431}
432
433TEST(Lite, AllLite17) {
434 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500435
436 {
437 // Clear
438 protobuf_unittest::TestMapLite message;
439
440 google::protobuf::MapLiteTestUtil::SetMapFields(&message);
441 message.Clear();
442 google::protobuf::MapLiteTestUtil::ExpectClear(message);
443 }
Austin Schuh40c16522018-10-28 20:27:54 -0700444}
445
446TEST(Lite, AllLite18) {
447 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500448
449 {
450 // ClearMessageMap
451 protobuf_unittest::TestMessageMapLite message;
452
453 // Creates a TestAllTypes with default value
454 google::protobuf::TestUtilLite::ExpectClear(
455 (*message.mutable_map_int32_message())[0]);
456 }
Austin Schuh40c16522018-10-28 20:27:54 -0700457}
458
459TEST(Lite, AllLite19) {
460 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500461
462 {
463 // CopyFrom
464 protobuf_unittest::TestMapLite message1, message2;
465
466 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
467 message2.CopyFrom(message1);
468 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
469
470 // Copying from self should be a no-op.
471 message2.CopyFrom(message2);
472 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
473 }
Austin Schuh40c16522018-10-28 20:27:54 -0700474}
475
476TEST(Lite, AllLite20) {
477 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500478
479 {
480 // CopyFromMessageMap
481 protobuf_unittest::TestMessageMapLite message1, message2;
482
483 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
484 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
485
486 message1.CopyFrom(message2);
487
488 // Checks repeated field is overwritten.
489 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
490 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
491 }
Austin Schuh40c16522018-10-28 20:27:54 -0700492}
493
494TEST(Lite, AllLite21) {
495 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500496
497 {
498 // SwapWithEmpty
499 protobuf_unittest::TestMapLite message1, message2;
500
501 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
502 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message1);
503 google::protobuf::MapLiteTestUtil::ExpectClear(message2);
504
505 message1.Swap(&message2);
506 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
507 google::protobuf::MapLiteTestUtil::ExpectClear(message1);
508 }
Austin Schuh40c16522018-10-28 20:27:54 -0700509}
510
511TEST(Lite, AllLite22) {
512 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500513
514 {
515 // SwapWithSelf
516 protobuf_unittest::TestMapLite message;
517
518 google::protobuf::MapLiteTestUtil::SetMapFields(&message);
519 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message);
520
521 message.Swap(&message);
522 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message);
523 }
Austin Schuh40c16522018-10-28 20:27:54 -0700524}
525
526TEST(Lite, AllLite23) {
527 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500528
529 {
530 // SwapWithOther
531 protobuf_unittest::TestMapLite message1, message2;
532
533 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
534 google::protobuf::MapLiteTestUtil::SetMapFields(&message2);
535 google::protobuf::MapLiteTestUtil::ModifyMapFields(&message2);
536
537 message1.Swap(&message2);
538 google::protobuf::MapLiteTestUtil::ExpectMapFieldsModified(message1);
539 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
540 }
Austin Schuh40c16522018-10-28 20:27:54 -0700541}
542
543TEST(Lite, AllLite24) {
544 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500545
546 {
547 // CopyConstructor
548 protobuf_unittest::TestMapLite message1;
549 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
550
551 protobuf_unittest::TestMapLite message2(message1);
552 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
553 }
Austin Schuh40c16522018-10-28 20:27:54 -0700554}
555
556TEST(Lite, AllLite25) {
557 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500558
559 {
560 // CopyAssignmentOperator
561 protobuf_unittest::TestMapLite message1;
562 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
563
564 protobuf_unittest::TestMapLite message2;
565 message2 = message1;
566 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
567
568 // Make sure that self-assignment does something sane.
569 message2.operator=(message2);
570 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
571 }
Austin Schuh40c16522018-10-28 20:27:54 -0700572}
573
574TEST(Lite, AllLite26) {
575 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500576
577 {
578 // NonEmptyMergeFrom
579 protobuf_unittest::TestMapLite message1, message2;
580
581 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
582
583 // This field will test merging into an empty spot.
584 (*message2.mutable_map_int32_int32())[1] = 1;
585 message1.mutable_map_int32_int32()->erase(1);
586
587 // This tests overwriting.
588 (*message2.mutable_map_int32_double())[1] = 1;
589 (*message1.mutable_map_int32_double())[1] = 2;
590
591 message1.MergeFrom(message2);
592 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message1);
593 }
Austin Schuh40c16522018-10-28 20:27:54 -0700594}
595
596TEST(Lite, AllLite27) {
597 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500598
599 {
600 // MergeFromMessageMap
601 protobuf_unittest::TestMessageMapLite message1, message2;
602
603 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
604 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
605
606 message1.MergeFrom(message2);
607
608 // Checks repeated field is overwritten.
609 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
610 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
611 }
Austin Schuh40c16522018-10-28 20:27:54 -0700612}
613
614TEST(Lite, AllLite28) {
615 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500616
617 {
618 // Test the generated SerializeWithCachedSizesToArray()
619 protobuf_unittest::TestMapLite message1, message2;
620 string data;
621 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
622 int size = message1.ByteSize();
623 data.resize(size);
Austin Schuh40c16522018-10-28 20:27:54 -0700624 ::google::protobuf::uint8* start = reinterpret_cast<::google::protobuf::uint8*>(::google::protobuf::string_as_array(&data));
Brian Silverman9c614bc2016-02-15 20:20:02 -0500625 ::google::protobuf::uint8* end = message1.SerializeWithCachedSizesToArray(start);
626 EXPECT_EQ(size, end - start);
627 EXPECT_TRUE(message2.ParseFromString(data));
628 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
629 }
Austin Schuh40c16522018-10-28 20:27:54 -0700630}
631
632TEST(Lite, AllLite29) {
633 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500634
635 {
636 // Test the generated SerializeWithCachedSizes()
637 protobuf_unittest::TestMapLite message1, message2;
638 google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
639 int size = message1.ByteSize();
640 string data;
641 data.resize(size);
642 {
643 // Allow the output stream to buffer only one byte at a time.
644 google::protobuf::io::ArrayOutputStream array_stream(
645 ::google::protobuf::string_as_array(&data), size, 1);
646 google::protobuf::io::CodedOutputStream output_stream(&array_stream);
647 message1.SerializeWithCachedSizes(&output_stream);
648 EXPECT_FALSE(output_stream.HadError());
649 EXPECT_EQ(size, output_stream.ByteCount());
650 }
651 EXPECT_TRUE(message2.ParseFromString(data));
652 google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
653 }
Austin Schuh40c16522018-10-28 20:27:54 -0700654}
Brian Silverman9c614bc2016-02-15 20:20:02 -0500655
656
Austin Schuh40c16522018-10-28 20:27:54 -0700657TEST(Lite, AllLite32) {
658 string data;
659
Brian Silverman9c614bc2016-02-15 20:20:02 -0500660 {
661 // Proto2UnknownEnum
662 protobuf_unittest::TestEnumMapPlusExtraLite from;
663 (*from.mutable_known_map_field())[0] =
664 protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE;
665 (*from.mutable_unknown_map_field())[0] =
666 protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE;
667 string data;
668 from.SerializeToString(&data);
669
670 protobuf_unittest::TestEnumMapLite to;
671 EXPECT_TRUE(to.ParseFromString(data));
672 EXPECT_EQ(0, to.unknown_map_field().size());
673 EXPECT_FALSE(to.mutable_unknown_fields()->empty());
674 EXPECT_EQ(1, to.known_map_field().size());
675 EXPECT_EQ(protobuf_unittest::PROTO2_MAP_ENUM_FOO_LITE,
676 to.known_map_field().at(0));
677
678 data.clear();
679 from.Clear();
680 to.SerializeToString(&data);
681 EXPECT_TRUE(from.ParseFromString(data));
682 EXPECT_EQ(1, from.known_map_field().size());
683 EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE,
684 from.known_map_field().at(0));
685 EXPECT_EQ(1, from.unknown_map_field().size());
686 EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE,
687 from.unknown_map_field().at(0));
688 }
Austin Schuh40c16522018-10-28 20:27:54 -0700689}
690
691TEST(Lite, AllLite33) {
692 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500693
694 {
695 // StandardWireFormat
696 protobuf_unittest::TestMapLite message;
697 string data = "\x0A\x04\x08\x01\x10\x01";
698
699 EXPECT_TRUE(message.ParseFromString(data));
700 EXPECT_EQ(1, message.map_int32_int32().size());
701 EXPECT_EQ(1, message.map_int32_int32().at(1));
702 }
Austin Schuh40c16522018-10-28 20:27:54 -0700703}
704
705TEST(Lite, AllLite34) {
706 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500707
708 {
709 // UnorderedWireFormat
710 protobuf_unittest::TestMapLite message;
711
712 // put value before key in wire format
713 string data = "\x0A\x04\x10\x01\x08\x02";
714
715 EXPECT_TRUE(message.ParseFromString(data));
716 EXPECT_EQ(1, message.map_int32_int32().size());
717 EXPECT_EQ(1, message.map_int32_int32().at(2));
718 }
Austin Schuh40c16522018-10-28 20:27:54 -0700719}
720
721TEST(Lite, AllLite35) {
722 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500723
724 {
725 // DuplicatedKeyWireFormat
726 protobuf_unittest::TestMapLite message;
727
728 // Two key fields in wire format
729 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
730
731 EXPECT_TRUE(message.ParseFromString(data));
732 EXPECT_EQ(1, message.map_int32_int32().size());
733 EXPECT_EQ(1, message.map_int32_int32().at(2));
734 }
Austin Schuh40c16522018-10-28 20:27:54 -0700735}
736
737TEST(Lite, AllLite36) {
738 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500739
740 {
741 // DuplicatedValueWireFormat
742 protobuf_unittest::TestMapLite message;
743
744 // Two value fields in wire format
745 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
746
747 EXPECT_TRUE(message.ParseFromString(data));
748 EXPECT_EQ(1, message.map_int32_int32().size());
749 EXPECT_EQ(2, message.map_int32_int32().at(1));
750 }
Austin Schuh40c16522018-10-28 20:27:54 -0700751}
752
753TEST(Lite, AllLite37) {
754 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500755
756 {
757 // MissedKeyWireFormat
758 protobuf_unittest::TestMapLite message;
759
760 // No key field in wire format
761 string data = "\x0A\x02\x10\x01";
762
763 EXPECT_TRUE(message.ParseFromString(data));
764 EXPECT_EQ(1, message.map_int32_int32().size());
765 EXPECT_EQ(1, message.map_int32_int32().at(0));
766 }
Austin Schuh40c16522018-10-28 20:27:54 -0700767}
768
769TEST(Lite, AllLite38) {
770 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500771
772 {
773 // MissedValueWireFormat
774 protobuf_unittest::TestMapLite message;
775
776 // No value field in wire format
777 string data = "\x0A\x02\x08\x01";
778
779 EXPECT_TRUE(message.ParseFromString(data));
780 EXPECT_EQ(1, message.map_int32_int32().size());
781 EXPECT_EQ(0, message.map_int32_int32().at(1));
782 }
Austin Schuh40c16522018-10-28 20:27:54 -0700783}
784
785TEST(Lite, AllLite39) {
786 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500787
788 {
789 // UnknownFieldWireFormat
790 protobuf_unittest::TestMapLite message;
791
792 // Unknown field in wire format
793 string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
794
795 EXPECT_TRUE(message.ParseFromString(data));
796 EXPECT_EQ(1, message.map_int32_int32().size());
797 EXPECT_EQ(3, message.map_int32_int32().at(2));
798 }
Austin Schuh40c16522018-10-28 20:27:54 -0700799}
800
801TEST(Lite, AllLite40) {
802 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500803
804 {
805 // CorruptedWireFormat
806 protobuf_unittest::TestMapLite message;
807
808 // corrupted data in wire format
809 string data = "\x0A\x06\x08\x02\x11\x03";
810
811 EXPECT_FALSE(message.ParseFromString(data));
812 }
Austin Schuh40c16522018-10-28 20:27:54 -0700813}
814
815TEST(Lite, AllLite41) {
816 string data;
Brian Silverman9c614bc2016-02-15 20:20:02 -0500817
818 {
819 // IsInitialized
820 protobuf_unittest::TestRequiredMessageMapLite map_message;
821
822 // Add an uninitialized message.
823 (*map_message.mutable_map_field())[0];
824 EXPECT_FALSE(map_message.IsInitialized());
825
826 // Initialize uninitialized message
827 (*map_message.mutable_map_field())[0].set_a(0);
828 (*map_message.mutable_map_field())[0].set_b(0);
829 (*map_message.mutable_map_field())[0].set_c(0);
830 EXPECT_TRUE(map_message.IsInitialized());
831 }
Austin Schuh40c16522018-10-28 20:27:54 -0700832}
833
834TEST(Lite, AllLite42) {
835 string data;
836
837 {
838 // Check that adding more values to enum does not corrupt message
839 // when passed through an old client.
840 protobuf_unittest::V2MessageLite v2_message;
841 v2_message.set_int_field(800);
842 // Set enum field to the value not understood by the old client.
843 v2_message.set_enum_field(protobuf_unittest::V2_SECOND);
844 string v2_bytes = v2_message.SerializeAsString();
845
846 protobuf_unittest::V1MessageLite v1_message;
847 v1_message.ParseFromString(v2_bytes);
848 EXPECT_TRUE(v1_message.IsInitialized());
849 EXPECT_EQ(v1_message.int_field(), v2_message.int_field());
850 // V1 client does not understand V2_SECOND value, so it discards it and
851 // uses default value instead.
852 EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST);
853
854 // However, when re-serialized, it should preserve enum value.
855 string v1_bytes = v1_message.SerializeAsString();
856
857 protobuf_unittest::V2MessageLite same_v2_message;
858 same_v2_message.ParseFromString(v1_bytes);
859
860 EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field());
861 EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field());
862 }
863}
864
865// Test that when parsing a oneof, we can successfully clear whatever already
866// happened to be stored in the oneof.
867TEST(Lite, AllLite43) {
868 protobuf_unittest::TestOneofParsingLite message1;
869
870 message1.set_oneof_int32(17);
871 string serialized;
872 EXPECT_TRUE(message1.SerializeToString(&serialized));
873
874 // Submessage
875 {
876 protobuf_unittest::TestOneofParsingLite message2;
877 message2.mutable_oneof_submessage();
878 google::protobuf::io::CodedInputStream input_stream(
879 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
880 EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
881 EXPECT_EQ(17, message2.oneof_int32());
882 }
883
884 // String
885 {
886 protobuf_unittest::TestOneofParsingLite message2;
887 message2.set_oneof_string("string");
888 google::protobuf::io::CodedInputStream input_stream(
889 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
890 EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
891 EXPECT_EQ(17, message2.oneof_int32());
892 }
893
894 // Bytes
895 {
896 protobuf_unittest::TestOneofParsingLite message2;
897 message2.set_oneof_bytes("bytes");
898 google::protobuf::io::CodedInputStream input_stream(
899 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
900 EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
901 EXPECT_EQ(17, message2.oneof_int32());
902 }
903}
904
905// Verify that we can successfully parse fields of various types within oneof
906// fields. We also verify that we can parse the same data twice into the same
907// message.
908TEST(Lite, AllLite44) {
909 // Int32
910 {
911 protobuf_unittest::TestOneofParsingLite original;
912 original.set_oneof_int32(17);
913 string serialized;
914 EXPECT_TRUE(original.SerializeToString(&serialized));
915 protobuf_unittest::TestOneofParsingLite parsed;
916 for (int i = 0; i < 2; ++i) {
917 google::protobuf::io::CodedInputStream input_stream(
918 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
919 serialized.size());
920 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
921 EXPECT_EQ(17, parsed.oneof_int32());
922 }
923 }
924
925 // Submessage
926 {
927 protobuf_unittest::TestOneofParsingLite original;
928 original.mutable_oneof_submessage()->set_optional_int32(5);
929 string serialized;
930 EXPECT_TRUE(original.SerializeToString(&serialized));
931 protobuf_unittest::TestOneofParsingLite parsed;
932 for (int i = 0; i < 2; ++i) {
933 google::protobuf::io::CodedInputStream input_stream(
934 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
935 serialized.size());
936 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
937 EXPECT_EQ(5, parsed.oneof_submessage().optional_int32());
938 }
939 }
940
941 // String
942 {
943 protobuf_unittest::TestOneofParsingLite original;
944 original.set_oneof_string("string");
945 string serialized;
946 EXPECT_TRUE(original.SerializeToString(&serialized));
947 protobuf_unittest::TestOneofParsingLite parsed;
948 for (int i = 0; i < 2; ++i) {
949 google::protobuf::io::CodedInputStream input_stream(
950 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
951 serialized.size());
952 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
953 EXPECT_EQ("string", parsed.oneof_string());
954 }
955 }
956
957 // Bytes
958 {
959 protobuf_unittest::TestOneofParsingLite original;
960 original.set_oneof_bytes("bytes");
961 string serialized;
962 EXPECT_TRUE(original.SerializeToString(&serialized));
963 protobuf_unittest::TestOneofParsingLite parsed;
964 for (int i = 0; i < 2; ++i) {
965 google::protobuf::io::CodedInputStream input_stream(
966 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
967 serialized.size());
968 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
969 EXPECT_EQ("bytes", parsed.oneof_bytes());
970 }
971 }
972
973 // Enum
974 {
975 protobuf_unittest::TestOneofParsingLite original;
976 original.set_oneof_enum(protobuf_unittest::V2_SECOND);
977 string serialized;
978 EXPECT_TRUE(original.SerializeToString(&serialized));
979 protobuf_unittest::TestOneofParsingLite parsed;
980 for (int i = 0; i < 2; ++i) {
981 google::protobuf::io::CodedInputStream input_stream(
982 reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
983 serialized.size());
984 EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
985 EXPECT_EQ(protobuf_unittest::V2_SECOND, parsed.oneof_enum());
986 }
987 }
Brian Silverman9c614bc2016-02-15 20:20:02 -0500988
989 std::cout << "PASS" << std::endl;
Austin Schuh40c16522018-10-28 20:27:54 -0700990}
991
992TEST(Lite, AllLite45) {
993 // Test unknown fields are not discarded upon parsing.
994 string data = "\20\1"; // varint 1 with field number 2
995
996 protobuf_unittest::ForeignMessageLite a;
997 EXPECT_TRUE(a.ParseFromString(data));
998 google::protobuf::io::CodedInputStream input_stream(
999 reinterpret_cast<const ::google::protobuf::uint8*>(data.data()), data.size());
1000 EXPECT_TRUE(a.MergePartialFromCodedStream(&input_stream));
1001
1002 string serialized = a.SerializeAsString();
1003 EXPECT_EQ(serialized.substr(0, 2), data);
1004 EXPECT_EQ(serialized.substr(2), data);
1005}
1006
1007// The following two tests check for wire compatibility between packed and
1008// unpacked repeated fields. There used to be a bug in the generated parsing
1009// code that caused us to calculate the highest possible tag number without
1010// taking into account that a repeated field might not be in the packed (or
1011// unpacked) state we expect. These tests specifically check for that issue by
1012// making sure we can parse repeated fields when the tag is higher than we would
1013// expect.
1014TEST(Lite, AllLite46) {
1015 protobuf_unittest::PackedInt32 packed;
1016 packed.add_repeated_int32(42);
1017 string serialized;
1018 ASSERT_TRUE(packed.SerializeToString(&serialized));
1019
1020 protobuf_unittest::NonPackedInt32 non_packed;
1021 ASSERT_TRUE(non_packed.ParseFromString(serialized));
1022 ASSERT_EQ(1, non_packed.repeated_int32_size());
1023 EXPECT_EQ(42, non_packed.repeated_int32(0));
1024}
1025
1026TEST(Lite, AllLite47) {
1027 protobuf_unittest::NonPackedFixed32 non_packed;
1028 non_packed.add_repeated_fixed32(42);
1029 string serialized;
1030 ASSERT_TRUE(non_packed.SerializeToString(&serialized));
1031
1032 protobuf_unittest::PackedFixed32 packed;
1033 ASSERT_TRUE(packed.ParseFromString(serialized));
1034 ASSERT_EQ(1, packed.repeated_fixed32_size());
1035 EXPECT_EQ(42, packed.repeated_fixed32(0));
Brian Silverman9c614bc2016-02-15 20:20:02 -05001036}