Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 <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 Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 39 | #include <google/protobuf/map_lite_test_util.h> |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 40 | #include <google/protobuf/map_lite_unittest.pb.h> |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 <google/protobuf/wire_format_lite_inl.h> |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 47 | #include <gtest/gtest.h> |
| 48 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 49 | #include <google/protobuf/stubs/strutil.h> |
| 50 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 51 | using std::string; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 52 | |
| 53 | namespace { |
| 54 | // Helper methods to test parsing merge behavior. |
| 55 | void ExpectMessageMerged(const google::protobuf::unittest::TestAllTypesLite& message) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 56 | EXPECT_EQ(message.optional_int32(), 3); |
| 57 | EXPECT_EQ(message.optional_int64(), 2); |
| 58 | EXPECT_EQ(message.optional_string(), "hello"); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void 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 | |
| 71 | void 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 | |
| 80 | void 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 94 | TEST(Lite, AllLite1) { |
| 95 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 96 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 112 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 113 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 114 | TEST(Lite, AllLite2) { |
| 115 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 116 | { |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 131 | } |
| 132 | |
| 133 | TEST(Lite, AllLite3) { |
| 134 | string data, packed_data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 135 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 158 | EXPECT_EQ(packed_extensions_data, packed_data); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 159 | 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 168 | } |
| 169 | |
| 170 | TEST(Lite, AllLite5) { |
| 171 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 172 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 219 | 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 Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 224 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 225 | } |
| 226 | |
| 227 | TEST(Lite, AllLite6) { |
| 228 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 229 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 246 | } |
| 247 | |
| 248 | TEST(Lite, AllLite7) { |
| 249 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 250 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 266 | } |
| 267 | |
| 268 | TEST(Lite, AllLite8) { |
| 269 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 270 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 286 | } |
| 287 | |
| 288 | TEST(Lite, AllLite9) { |
| 289 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 290 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 306 | } |
| 307 | |
| 308 | TEST(Lite, AllLite10) { |
| 309 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 310 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 317 | string data2 = empty_message2.SerializeAsString(); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 318 | empty_message.Swap(&empty_message2); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 319 | EXPECT_EQ(data, empty_message2.SerializeAsString()); |
| 320 | EXPECT_EQ(data2, empty_message.SerializeAsString()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 321 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 322 | } |
| 323 | |
| 324 | TEST(Lite, AllLite11) { |
| 325 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 326 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 333 | EXPECT_EQ(data, empty_message.SerializeAsString()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 334 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 335 | } |
| 336 | |
| 337 | TEST(Lite, AllLite12) { |
| 338 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 339 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 363 | EXPECT_EQ(message.SerializeAsString(), message2.SerializeAsString()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 364 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 365 | } |
| 366 | |
| 367 | TEST(Lite, AllLite13) { |
| 368 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 369 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 388 | EXPECT_EQ(data, buffer); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 389 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 390 | } |
| 391 | |
| 392 | TEST(Lite, AllLite14) { |
| 393 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 394 | |
| 395 | { |
| 396 | // Test Clear with unknown fields |
| 397 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 398 | SetAllTypesInEmptyMessageUnknownFields(&empty_message); |
| 399 | empty_message.Clear(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 400 | EXPECT_EQ(0, empty_message.unknown_fields().size()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 401 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 402 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 403 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 404 | // Tests for map lite ============================================= |
| 405 | |
| 406 | TEST(Lite, AllLite15) { |
| 407 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 408 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 419 | } |
| 420 | |
| 421 | TEST(Lite, AllLite16) { |
| 422 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 423 | |
| 424 | { |
| 425 | // SetMapFieldsInitialized |
| 426 | protobuf_unittest::TestMapLite message; |
| 427 | |
| 428 | google::protobuf::MapLiteTestUtil::SetMapFieldsInitialized(&message); |
| 429 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSetInitialized(message); |
| 430 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 431 | } |
| 432 | |
| 433 | TEST(Lite, AllLite17) { |
| 434 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 435 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 444 | } |
| 445 | |
| 446 | TEST(Lite, AllLite18) { |
| 447 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 448 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 457 | } |
| 458 | |
| 459 | TEST(Lite, AllLite19) { |
| 460 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 461 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 474 | } |
| 475 | |
| 476 | TEST(Lite, AllLite20) { |
| 477 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 478 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 492 | } |
| 493 | |
| 494 | TEST(Lite, AllLite21) { |
| 495 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 496 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 509 | } |
| 510 | |
| 511 | TEST(Lite, AllLite22) { |
| 512 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 513 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 524 | } |
| 525 | |
| 526 | TEST(Lite, AllLite23) { |
| 527 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 528 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 541 | } |
| 542 | |
| 543 | TEST(Lite, AllLite24) { |
| 544 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 545 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 554 | } |
| 555 | |
| 556 | TEST(Lite, AllLite25) { |
| 557 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 558 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 572 | } |
| 573 | |
| 574 | TEST(Lite, AllLite26) { |
| 575 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 576 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 594 | } |
| 595 | |
| 596 | TEST(Lite, AllLite27) { |
| 597 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 598 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 612 | } |
| 613 | |
| 614 | TEST(Lite, AllLite28) { |
| 615 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 616 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 624 | ::google::protobuf::uint8* start = reinterpret_cast<::google::protobuf::uint8*>(::google::protobuf::string_as_array(&data)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 625 | ::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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 630 | } |
| 631 | |
| 632 | TEST(Lite, AllLite29) { |
| 633 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 634 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 654 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 655 | |
| 656 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 657 | TEST(Lite, AllLite32) { |
| 658 | string data; |
| 659 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 660 | { |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 689 | } |
| 690 | |
| 691 | TEST(Lite, AllLite33) { |
| 692 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 693 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 703 | } |
| 704 | |
| 705 | TEST(Lite, AllLite34) { |
| 706 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 707 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 719 | } |
| 720 | |
| 721 | TEST(Lite, AllLite35) { |
| 722 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 723 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 735 | } |
| 736 | |
| 737 | TEST(Lite, AllLite36) { |
| 738 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 739 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 751 | } |
| 752 | |
| 753 | TEST(Lite, AllLite37) { |
| 754 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 755 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 767 | } |
| 768 | |
| 769 | TEST(Lite, AllLite38) { |
| 770 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 771 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 783 | } |
| 784 | |
| 785 | TEST(Lite, AllLite39) { |
| 786 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 787 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 799 | } |
| 800 | |
| 801 | TEST(Lite, AllLite40) { |
| 802 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 803 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 813 | } |
| 814 | |
| 815 | TEST(Lite, AllLite41) { |
| 816 | string data; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 817 | |
| 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 Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 832 | } |
| 833 | |
| 834 | TEST(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. |
| 867 | TEST(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. |
| 908 | TEST(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 Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 988 | |
| 989 | std::cout << "PASS" << std::endl; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 990 | } |
| 991 | |
| 992 | TEST(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. |
| 1014 | TEST(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 | |
| 1026 | TEST(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 Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1036 | } |