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// Based on original Protocol Buffers design by 33// Sanjay Ghemawat, Jeff Dean, and others. 34// 35// To test the code generator, we actually use it to generate code for 36// net/proto2/internal/unittest.proto, then test that. This means that we 37// are actually testing the parser and other parts of the system at the same 38// time, and that problems in the generator may show up as compile-time errors 39// rather than unittest failures, which may be surprising. However, testing 40// the output of the C++ generator directly would be very hard. We can't very 41// well just check it against golden files since those files would have to be 42// updated for any small change; such a test would be very brittle and probably 43// not very helpful. What we really want to test is that the code compiles 44// correctly and produces the interfaces we expect, which is why this test 45// is written this way. 46 47#include <cstdint> 48#include <limits> 49#include <memory> 50#include <vector> 51 52#include <google/protobuf/compiler/cpp/unittest.h> 53#include <google/protobuf/stubs/strutil.h> 54#ifndef _MSC_VER 55// We exclude this large proto because it's too large for 56// visual studio to compile (report internal errors). 57#include <google/protobuf/unittest_enormous_descriptor.pb.h> 58#endif 59#include <google/protobuf/compiler/cpp/helpers.h> 60#include <google/protobuf/compiler/cpp/test_bad_identifiers.pb.h> 61#include <google/protobuf/unittest_no_generic_services.pb.h> 62#include <google/protobuf/descriptor.pb.h> 63#include <google/protobuf/arena.h> 64#include <google/protobuf/descriptor.h> 65#include <google/protobuf/dynamic_message.h> 66#include <google/protobuf/compiler/scc.h> 67#include <google/protobuf/compiler/importer.h> 68#include <google/protobuf/io/coded_stream.h> 69#include <google/protobuf/io/zero_copy_stream_impl.h> 70#include <google/protobuf/test_util2.h> 71 72#include <google/protobuf/stubs/callback.h> 73#include <google/protobuf/stubs/common.h> 74#include <google/protobuf/stubs/logging.h> 75#include <google/protobuf/testing/googletest.h> 76#include <gtest/gtest.h> 77#include <google/protobuf/stubs/casts.h> 78#include <google/protobuf/stubs/substitute.h> 79#include <google/protobuf/stubs/stl_util.h> 80 81// Must be included last. 82#include <google/protobuf/port_def.inc> 83 84namespace google { 85namespace protobuf { 86namespace compiler { 87namespace cpp { 88 89// Can't use an anonymous namespace here due to brokenness of Tru64 compiler. 90namespace cpp_unittest { 91 92 93void DoNothing() {} 94 95class MockErrorCollector : public MultiFileErrorCollector { 96 public: 97 MockErrorCollector() {} 98 ~MockErrorCollector() override {} 99 100 std::string text_; 101 102 // implements ErrorCollector --------------------------------------- 103 void AddError(const std::string& filename, int line, int column, 104 const std::string& message) override { 105 strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, column, 106 message); 107 } 108}; 109 110#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 111 112// Test that generated code has proper descriptors: 113// Parse a descriptor directly (using google::protobuf::compiler::Importer) and 114// compare it to the one that was produced by generated code. 115TEST(GENERATED_DESCRIPTOR_TEST_NAME, IdenticalDescriptors) { 116 const FileDescriptor* generated_descriptor = 117 UNITTEST::TestAllTypes::descriptor()->file(); 118 119 // Set up the Importer. 120 MockErrorCollector error_collector; 121 DiskSourceTree source_tree; 122 source_tree.MapPath("", TestUtil::TestSourceDir()); 123 Importer importer(&source_tree, &error_collector); 124 125 // Import (parse) unittest.proto. 126 const FileDescriptor* parsed_descriptor = 127 importer.Import(TestUtil::MaybeTranslatePath(UNITTEST_PROTO_PATH)); 128 EXPECT_EQ("", error_collector.text_); 129 ASSERT_TRUE(parsed_descriptor != nullptr); 130 131 // Test that descriptors are generated correctly by converting them to 132 // FileDescriptorProtos and comparing. 133 FileDescriptorProto generated_descriptor_proto, parsed_descriptor_proto; 134 generated_descriptor->CopyTo(&generated_descriptor_proto); 135 parsed_descriptor->CopyTo(&parsed_descriptor_proto); 136 137 EXPECT_EQ(parsed_descriptor_proto.DebugString(), 138 generated_descriptor_proto.DebugString()); 139} 140 141#ifndef _MSC_VER 142// Test that generated code has proper descriptors: 143// Touch a descriptor generated from an enormous message to validate special 144// handling for descriptors exceeding the C++ standard's recommended minimum 145// limit for string literal size 146TEST(GENERATED_DESCRIPTOR_TEST_NAME, EnormousDescriptor) { 147 const Descriptor* generated_descriptor = 148 ::protobuf_unittest::TestEnormousDescriptor::descriptor(); 149 150 EXPECT_TRUE(generated_descriptor != nullptr); 151} 152#endif 153 154#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 155 156// =================================================================== 157 158TEST(GENERATED_MESSAGE_TEST_NAME, Defaults) { 159 // Check that all default values are set correctly in the initial message. 160 UNITTEST::TestAllTypes message; 161 162 TestUtil::ExpectClear(message); 163 164 // Messages should return pointers to default instances until first use. 165 // (This is not checked by ExpectClear() since it is not actually true after 166 // the fields have been set and then cleared.) 167 EXPECT_EQ(&UNITTEST::TestAllTypes::OptionalGroup::default_instance(), 168 &message.optionalgroup()); 169 EXPECT_EQ(&UNITTEST::TestAllTypes::NestedMessage::default_instance(), 170 &message.optional_nested_message()); 171 EXPECT_EQ(&UNITTEST::ForeignMessage::default_instance(), 172 &message.optional_foreign_message()); 173 EXPECT_EQ(&UNITTEST_IMPORT::ImportMessage::default_instance(), 174 &message.optional_import_message()); 175} 176 177TEST(GENERATED_MESSAGE_TEST_NAME, FloatingPointDefaults) { 178 const UNITTEST::TestExtremeDefaultValues& extreme_default = 179 UNITTEST::TestExtremeDefaultValues::default_instance(); 180 181 EXPECT_EQ(0.0f, extreme_default.zero_float()); 182 EXPECT_EQ(1.0f, extreme_default.one_float()); 183 EXPECT_EQ(1.5f, extreme_default.small_float()); 184 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); 185 EXPECT_EQ(-1.5f, extreme_default.negative_float()); 186 EXPECT_EQ(2.0e8f, extreme_default.large_float()); 187 EXPECT_EQ(-8e-28f, extreme_default.small_negative_float()); 188 EXPECT_EQ(std::numeric_limits<double>::infinity(), 189 extreme_default.inf_double()); 190 EXPECT_EQ(-std::numeric_limits<double>::infinity(), 191 extreme_default.neg_inf_double()); 192 EXPECT_TRUE(extreme_default.nan_double() != extreme_default.nan_double()); 193 EXPECT_EQ(std::numeric_limits<float>::infinity(), 194 extreme_default.inf_float()); 195 EXPECT_EQ(-std::numeric_limits<float>::infinity(), 196 extreme_default.neg_inf_float()); 197 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); 198} 199 200TEST(GENERATED_MESSAGE_TEST_NAME, Trigraph) { 201 const UNITTEST::TestExtremeDefaultValues& extreme_default = 202 UNITTEST::TestExtremeDefaultValues::default_instance(); 203 204 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); 205} 206 207TEST(GENERATED_MESSAGE_TEST_NAME, ExtremeSmallIntegerDefault) { 208 const UNITTEST::TestExtremeDefaultValues& extreme_default = 209 UNITTEST::TestExtremeDefaultValues::default_instance(); 210 EXPECT_EQ(std::numeric_limits<int32_t>::min(), 211 extreme_default.really_small_int32()); 212 EXPECT_EQ(std::numeric_limits<int64_t>::min(), 213 extreme_default.really_small_int64()); 214} 215 216TEST(GENERATED_MESSAGE_TEST_NAME, Accessors) { 217 // Set every field to a unique value then go back and check all those 218 // values. 219 UNITTEST::TestAllTypes message; 220 221 TestUtil::SetAllFields(&message); 222 TestUtil::ExpectAllFieldsSet(message); 223 224 TestUtil::ModifyRepeatedFields(&message); 225 TestUtil::ExpectRepeatedFieldsModified(message); 226} 227 228TEST(GENERATED_MESSAGE_TEST_NAME, MutableStringDefault) { 229 // mutable_foo() for a string should return a string initialized to its 230 // default value. 231 UNITTEST::TestAllTypes message; 232 233 EXPECT_EQ("hello", *message.mutable_default_string()); 234 235 // Note that the first time we call mutable_foo(), we get a newly-allocated 236 // string, but if we clear it and call it again, we get the same object again. 237 // We should verify that it has its default value in both cases. 238 message.set_default_string("blah"); 239 message.Clear(); 240 241 EXPECT_EQ("hello", *message.mutable_default_string()); 242} 243 244TEST(GENERATED_MESSAGE_TEST_NAME, StringDefaults) { 245 UNITTEST::TestExtremeDefaultValues message; 246 // Check if '\000' can be used in default string value. 247 EXPECT_EQ(std::string("hel\000lo", 6), message.string_with_zero()); 248 EXPECT_EQ(std::string("wor\000ld", 6), message.bytes_with_zero()); 249} 250 251TEST(GENERATED_MESSAGE_TEST_NAME, ReleaseString) { 252 // Check that release_foo() starts out nullptr, and gives us a value 253 // that we can delete after it's been set. 254 UNITTEST::TestAllTypes message; 255 256 EXPECT_EQ(nullptr, message.release_default_string()); 257 EXPECT_FALSE(message.has_default_string()); 258 EXPECT_EQ("hello", message.default_string()); 259 260 message.set_default_string("blah"); 261 EXPECT_TRUE(message.has_default_string()); 262 std::unique_ptr<std::string> str(message.release_default_string()); 263 EXPECT_FALSE(message.has_default_string()); 264 ASSERT_TRUE(str != nullptr); 265 EXPECT_EQ("blah", *str); 266 267 EXPECT_EQ(nullptr, message.release_default_string()); 268 EXPECT_FALSE(message.has_default_string()); 269 EXPECT_EQ("hello", message.default_string()); 270} 271 272TEST(GENERATED_MESSAGE_TEST_NAME, ReleaseMessage) { 273 // Check that release_foo() starts out nullptr, and gives us a value 274 // that we can delete after it's been set. 275 UNITTEST::TestAllTypes message; 276 277 EXPECT_EQ(nullptr, message.release_optional_nested_message()); 278 EXPECT_FALSE(message.has_optional_nested_message()); 279 280 message.mutable_optional_nested_message()->set_bb(1); 281 std::unique_ptr<UNITTEST::TestAllTypes::NestedMessage> nest( 282 message.release_optional_nested_message()); 283 EXPECT_FALSE(message.has_optional_nested_message()); 284 ASSERT_TRUE(nest != nullptr); 285 EXPECT_EQ(1, nest->bb()); 286 287 EXPECT_EQ(nullptr, message.release_optional_nested_message()); 288 EXPECT_FALSE(message.has_optional_nested_message()); 289} 290 291TEST(GENERATED_MESSAGE_TEST_NAME, SetAllocatedString) { 292 // Check that set_allocated_foo() works for strings. 293 UNITTEST::TestAllTypes message; 294 295 EXPECT_FALSE(message.has_optional_string()); 296 const std::string kHello("hello"); 297 message.set_optional_string(kHello); 298 EXPECT_TRUE(message.has_optional_string()); 299 300 message.set_allocated_optional_string(nullptr); 301 EXPECT_FALSE(message.has_optional_string()); 302 EXPECT_EQ("", message.optional_string()); 303 304 message.set_allocated_optional_string(new std::string(kHello)); 305 EXPECT_TRUE(message.has_optional_string()); 306 EXPECT_EQ(kHello, message.optional_string()); 307} 308 309TEST(GENERATED_MESSAGE_TEST_NAME, SetAllocatedMessage) { 310 // Check that set_allocated_foo() can be called in all cases. 311 UNITTEST::TestAllTypes message; 312 313 EXPECT_FALSE(message.has_optional_nested_message()); 314 315 message.mutable_optional_nested_message()->set_bb(1); 316 EXPECT_TRUE(message.has_optional_nested_message()); 317 318 message.set_allocated_optional_nested_message(nullptr); 319 EXPECT_FALSE(message.has_optional_nested_message()); 320 EXPECT_EQ(&UNITTEST::TestAllTypes::NestedMessage::default_instance(), 321 &message.optional_nested_message()); 322 323 message.mutable_optional_nested_message()->set_bb(1); 324 UNITTEST::TestAllTypes::NestedMessage* nest = 325 message.release_optional_nested_message(); 326 ASSERT_TRUE(nest != nullptr); 327 EXPECT_FALSE(message.has_optional_nested_message()); 328 329 message.set_allocated_optional_nested_message(nest); 330 EXPECT_TRUE(message.has_optional_nested_message()); 331 EXPECT_EQ(1, message.optional_nested_message().bb()); 332} 333 334TEST(GENERATED_MESSAGE_TEST_NAME, Clear) { 335 // Set every field to a unique value, clear the message, then check that 336 // it is cleared. 337 UNITTEST::TestAllTypes message; 338 339 TestUtil::SetAllFields(&message); 340 message.Clear(); 341 TestUtil::ExpectClear(message); 342} 343 344TEST(GENERATED_MESSAGE_TEST_NAME, EmbeddedNullsInBytesCharStar) { 345 UNITTEST::TestAllTypes message; 346 347 const char* value = "\0lalala\0\0"; 348 message.set_optional_bytes(value, 9); 349 ASSERT_EQ(9, message.optional_bytes().size()); 350 EXPECT_EQ(0, memcmp(value, message.optional_bytes().data(), 9)); 351 352 message.add_repeated_bytes(value, 9); 353 ASSERT_EQ(9, message.repeated_bytes(0).size()); 354 EXPECT_EQ(0, memcmp(value, message.repeated_bytes(0).data(), 9)); 355} 356 357TEST(GENERATED_MESSAGE_TEST_NAME, ClearOneField) { 358 // Set every field to a unique value, then clear one value and insure that 359 // only that one value is cleared. 360 UNITTEST::TestAllTypes message; 361 362 TestUtil::SetAllFields(&message); 363 int64_t original_value = message.optional_int64(); 364 365 // Clear the field and make sure it shows up as cleared. 366 message.clear_optional_int64(); 367 EXPECT_FALSE(message.has_optional_int64()); 368 EXPECT_EQ(0, message.optional_int64()); 369 370 // Other adjacent fields should not be cleared. 371 EXPECT_TRUE(message.has_optional_int32()); 372 EXPECT_TRUE(message.has_optional_uint32()); 373 374 // Make sure if we set it again, then all fields are set. 375 message.set_optional_int64(original_value); 376 TestUtil::ExpectAllFieldsSet(message); 377} 378 379TEST(GENERATED_MESSAGE_TEST_NAME, StringCharStarLength) { 380 // Verify that we can use a char*,length to set one of the string fields. 381 UNITTEST::TestAllTypes message; 382 message.set_optional_string("abcdef", 3); 383 EXPECT_EQ("abc", message.optional_string()); 384 385 // Verify that we can use a char*,length to add to a repeated string field. 386 message.add_repeated_string("abcdef", 3); 387 EXPECT_EQ(1, message.repeated_string_size()); 388 EXPECT_EQ("abc", message.repeated_string(0)); 389 390 // Verify that we can use a char*,length to set a repeated string field. 391 message.set_repeated_string(0, "wxyz", 2); 392 EXPECT_EQ("wx", message.repeated_string(0)); 393} 394 395 396#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || defined(NDEBUG) 397TEST(GENERATED_MESSAGE_TEST_NAME, CopyFrom) { 398 UNITTEST::TestAllTypes message1, message2; 399 400 TestUtil::SetAllFields(&message1); 401 message2.CopyFrom(message1); 402 TestUtil::ExpectAllFieldsSet(message2); 403 404 // Copying from self should be a no-op. 405 message2.CopyFrom(message2); 406 TestUtil::ExpectAllFieldsSet(message2); 407} 408 409TEST(GENERATED_MESSAGE_TEST_NAME, CopyAssignmentOperator) { 410 UNITTEST::TestAllTypes message1; 411 TestUtil::SetAllFields(&message1); 412 413 UNITTEST::TestAllTypes message2; 414 message2 = message1; 415 TestUtil::ExpectAllFieldsSet(message2); 416 417 // Make sure that self-assignment does something sane. 418 message2.operator=(message2); 419 TestUtil::ExpectAllFieldsSet(message2); 420} 421#endif // !PROTOBUF_TEST_NO_DESCRIPTORS || NDEBUG 422 423 424TEST(GENERATED_MESSAGE_TEST_NAME, SwapWithEmpty) { 425 UNITTEST::TestAllTypes message1, message2; 426 TestUtil::SetAllFields(&message1); 427 428 TestUtil::ExpectAllFieldsSet(message1); 429 TestUtil::ExpectClear(message2); 430 message1.Swap(&message2); 431 TestUtil::ExpectAllFieldsSet(message2); 432 TestUtil::ExpectClear(message1); 433} 434 435TEST(GENERATED_MESSAGE_TEST_NAME, SwapWithSelf) { 436 UNITTEST::TestAllTypes message; 437 TestUtil::SetAllFields(&message); 438 TestUtil::ExpectAllFieldsSet(message); 439 message.Swap(&message); 440 TestUtil::ExpectAllFieldsSet(message); 441} 442 443TEST(GENERATED_MESSAGE_TEST_NAME, SwapWithOther) { 444 UNITTEST::TestAllTypes message1, message2; 445 446 message1.set_optional_int32(123); 447 message1.set_optional_string("abc"); 448 message1.mutable_optional_nested_message()->set_bb(1); 449 message1.set_optional_nested_enum(UNITTEST::TestAllTypes::FOO); 450 message1.add_repeated_int32(1); 451 message1.add_repeated_int32(2); 452 message1.add_repeated_string("a"); 453 message1.add_repeated_string("b"); 454 message1.add_repeated_nested_message()->set_bb(7); 455 message1.add_repeated_nested_message()->set_bb(8); 456 message1.add_repeated_nested_enum(UNITTEST::TestAllTypes::FOO); 457 message1.add_repeated_nested_enum(UNITTEST::TestAllTypes::BAR); 458 459 message2.set_optional_int32(456); 460 message2.set_optional_string("def"); 461 message2.mutable_optional_nested_message()->set_bb(2); 462 message2.set_optional_nested_enum(UNITTEST::TestAllTypes::BAR); 463 message2.add_repeated_int32(3); 464 message2.add_repeated_string("c"); 465 message2.add_repeated_nested_message()->set_bb(9); 466 message2.add_repeated_nested_enum(UNITTEST::TestAllTypes::BAZ); 467 468 message1.Swap(&message2); 469 470 EXPECT_EQ(456, message1.optional_int32()); 471 EXPECT_EQ("def", message1.optional_string()); 472 EXPECT_EQ(2, message1.optional_nested_message().bb()); 473 EXPECT_EQ(UNITTEST::TestAllTypes::BAR, message1.optional_nested_enum()); 474 ASSERT_EQ(1, message1.repeated_int32_size()); 475 EXPECT_EQ(3, message1.repeated_int32(0)); 476 ASSERT_EQ(1, message1.repeated_string_size()); 477 EXPECT_EQ("c", message1.repeated_string(0)); 478 ASSERT_EQ(1, message1.repeated_nested_message_size()); 479 EXPECT_EQ(9, message1.repeated_nested_message(0).bb()); 480 ASSERT_EQ(1, message1.repeated_nested_enum_size()); 481 EXPECT_EQ(UNITTEST::TestAllTypes::BAZ, message1.repeated_nested_enum(0)); 482 483 EXPECT_EQ(123, message2.optional_int32()); 484 EXPECT_EQ("abc", message2.optional_string()); 485 EXPECT_EQ(1, message2.optional_nested_message().bb()); 486 EXPECT_EQ(UNITTEST::TestAllTypes::FOO, message2.optional_nested_enum()); 487 ASSERT_EQ(2, message2.repeated_int32_size()); 488 EXPECT_EQ(1, message2.repeated_int32(0)); 489 EXPECT_EQ(2, message2.repeated_int32(1)); 490 ASSERT_EQ(2, message2.repeated_string_size()); 491 EXPECT_EQ("a", message2.repeated_string(0)); 492 EXPECT_EQ("b", message2.repeated_string(1)); 493 ASSERT_EQ(2, message2.repeated_nested_message_size()); 494 EXPECT_EQ(7, message2.repeated_nested_message(0).bb()); 495 EXPECT_EQ(8, message2.repeated_nested_message(1).bb()); 496 ASSERT_EQ(2, message2.repeated_nested_enum_size()); 497 EXPECT_EQ(UNITTEST::TestAllTypes::FOO, message2.repeated_nested_enum(0)); 498 EXPECT_EQ(UNITTEST::TestAllTypes::BAR, message2.repeated_nested_enum(1)); 499} 500 501TEST(GENERATED_MESSAGE_TEST_NAME, ADLSwap) { 502 UNITTEST::TestAllTypes message1, message2; 503 TestUtil::SetAllFields(&message1); 504 505 // Note the address of one of the repeated fields, to verify it was swapped 506 // rather than copied. 507 const int32_t* addr = &message1.repeated_int32().Get(0); 508#ifdef PROTOBUF_FORCE_COPY_IN_SWAP 509 const int32_t value = *addr; 510#endif 511 512 using std::swap; 513 swap(message1, message2); 514 515 TestUtil::ExpectAllFieldsSet(message2); 516 TestUtil::ExpectClear(message1); 517 518#ifdef PROTOBUF_FORCE_COPY_IN_SWAP 519 EXPECT_NE(addr, &message2.repeated_int32().Get(0)); 520 EXPECT_EQ(value, message2.repeated_int32().Get(0)); 521#else 522 EXPECT_EQ(addr, &message2.repeated_int32().Get(0)); 523#endif 524} 525 526TEST(GENERATED_MESSAGE_TEST_NAME, CopyConstructor) { 527 // All set. 528 { 529 UNITTEST::TestAllTypes message1; 530 TestUtil::SetAllFields(&message1); 531 532 UNITTEST::TestAllTypes message2(message1); 533 TestUtil::ExpectAllFieldsSet(message2); 534 } 535 536 // None set. 537 { 538 UNITTEST::TestAllTypes message1; 539 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization) 540 UNITTEST::TestAllTypes message2(message1); 541 542 EXPECT_FALSE(message1.has_optional_string()); 543 EXPECT_FALSE(message2.has_optional_string()); 544 EXPECT_EQ(message1.optional_string(), message2.optional_string()); 545 546 EXPECT_FALSE(message1.has_optional_bytes()); 547 EXPECT_FALSE(message2.has_optional_bytes()); 548 EXPECT_EQ(message1.optional_bytes(), message2.optional_bytes()); 549 550 EXPECT_FALSE(message1.has_optional_nested_message()); 551 EXPECT_FALSE(message2.has_optional_nested_message()); 552 EXPECT_EQ(&message1.optional_nested_message(), 553 &message2.optional_nested_message()); 554 555 EXPECT_FALSE(message1.has_optional_foreign_message()); 556 EXPECT_FALSE(message2.has_optional_foreign_message()); 557 EXPECT_EQ(&message1.optional_foreign_message(), 558 &message2.optional_foreign_message()); 559 560 EXPECT_FALSE(message1.has_optional_import_message()); 561 EXPECT_FALSE(message2.has_optional_import_message()); 562 EXPECT_EQ(&message1.optional_import_message(), 563 &message2.optional_import_message()); 564 565 EXPECT_FALSE(message1.has_optional_public_import_message()); 566 EXPECT_FALSE(message2.has_optional_public_import_message()); 567 EXPECT_EQ(&message1.optional_public_import_message(), 568 &message2.optional_public_import_message()); 569 570 EXPECT_FALSE(message1.has_optional_lazy_message()); 571 EXPECT_FALSE(message2.has_optional_lazy_message()); 572 EXPECT_EQ(&message1.optional_lazy_message(), 573 &message2.optional_lazy_message()); 574 } 575} 576 577TEST(GENERATED_MESSAGE_TEST_NAME, CopyConstructorWithArenas) { 578 Arena arena; 579 UNITTEST::TestAllTypes* message1 = 580 Arena::CreateMessage<UNITTEST::TestAllTypes>(&arena); 581 TestUtil::SetAllFields(message1); 582 583 UNITTEST::TestAllTypes message2_stack(*message1); 584 TestUtil::ExpectAllFieldsSet(message2_stack); 585 586 std::unique_ptr<UNITTEST::TestAllTypes> message2_heap( 587 new UNITTEST::TestAllTypes(*message1)); 588 TestUtil::ExpectAllFieldsSet(*message2_heap); 589 590 arena.Reset(); 591 592 // Verify that the copies are still intact. 593 TestUtil::ExpectAllFieldsSet(message2_stack); 594 TestUtil::ExpectAllFieldsSet(*message2_heap); 595} 596 597#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 598TEST(GENERATED_MESSAGE_TEST_NAME, UpcastCopyFrom) { 599 // Test the CopyFrom method that takes in the generic const Message& 600 // parameter. 601 UNITTEST::TestAllTypes message1, message2; 602 603 TestUtil::SetAllFields(&message1); 604 605 const Message* source = implicit_cast<const Message*>(&message1); 606 message2.CopyFrom(*source); 607 608 TestUtil::ExpectAllFieldsSet(message2); 609} 610 611TEST(GENERATED_MESSAGE_TEST_NAME, DynamicMessageCopyFrom) { 612 // Test copying from a DynamicMessage, which must fall back to using 613 // reflection. 614 UNITTEST::TestAllTypes message2; 615 616 // Construct a new version of the dynamic message via the factory. 617 DynamicMessageFactory factory; 618 std::unique_ptr<Message> message1; 619 message1.reset(factory.GetPrototype( 620 UNITTEST::TestAllTypes::descriptor())->New()); 621 622 TestUtil::ReflectionTester reflection_tester( 623 UNITTEST::TestAllTypes::descriptor()); 624 reflection_tester.SetAllFieldsViaReflection(message1.get()); 625 626 message2.CopyFrom(*message1); 627 628 TestUtil::ExpectAllFieldsSet(message2); 629} 630 631#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 632 633TEST(GENERATED_MESSAGE_TEST_NAME, NonEmptyMergeFrom) { 634 // Test merging with a non-empty message. Code is a modified form 635 // of that found in net/proto2/internal/reflection_ops_unittest.cc. 636 UNITTEST::TestAllTypes message1, message2; 637 638 TestUtil::SetAllFields(&message1); 639 640 // This field will test merging into an empty spot. 641 message2.set_optional_int32(message1.optional_int32()); 642 message1.clear_optional_int32(); 643 644 // This tests overwriting. 645 message2.set_optional_string(message1.optional_string()); 646 message1.set_optional_string("something else"); 647 648 // This tests concatenating. 649 message2.add_repeated_int32(message1.repeated_int32(1)); 650 int32_t i = message1.repeated_int32(0); 651 message1.clear_repeated_int32(); 652 message1.add_repeated_int32(i); 653 654 message1.MergeFrom(message2); 655 656 TestUtil::ExpectAllFieldsSet(message1); 657} 658 659 660// Test the generated SerializeWithCachedSizesToArray(), 661TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToArray) { 662 UNITTEST::TestAllTypes message1, message2; 663 std::string data; 664 TestUtil::SetAllFields(&message1); 665 int size = message1.ByteSizeLong(); 666 data.resize(size); 667 uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 668 uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 669 EXPECT_EQ(size, end - start); 670 EXPECT_TRUE(message2.ParseFromString(data)); 671 TestUtil::ExpectAllFieldsSet(message2); 672 673} 674 675TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToArray) { 676 UNITTEST::TestPackedTypes packed_message1, packed_message2; 677 std::string packed_data; 678 TestUtil::SetPackedFields(&packed_message1); 679 int packed_size = packed_message1.ByteSizeLong(); 680 packed_data.resize(packed_size); 681 uint8_t* start = 682 reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&packed_data)); 683 uint8_t* end = packed_message1.SerializeWithCachedSizesToArray(start); 684 EXPECT_EQ(packed_size, end - start); 685 EXPECT_TRUE(packed_message2.ParseFromString(packed_data)); 686 TestUtil::ExpectPackedFieldsSet(packed_message2); 687} 688 689// Test the generated SerializeWithCachedSizes() by forcing the buffer to write 690// one byte at a time. 691TEST(GENERATED_MESSAGE_TEST_NAME, SerializationToStream) { 692 UNITTEST::TestAllTypes message1, message2; 693 TestUtil::SetAllFields(&message1); 694 int size = message1.ByteSizeLong(); 695 std::string data; 696 data.resize(size); 697 { 698 // Allow the output stream to buffer only one byte at a time. 699 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 700 io::CodedOutputStream output_stream(&array_stream); 701 message1.SerializeWithCachedSizes(&output_stream); 702 EXPECT_FALSE(output_stream.HadError()); 703 EXPECT_EQ(size, output_stream.ByteCount()); 704 } 705 EXPECT_TRUE(message2.ParseFromString(data)); 706 TestUtil::ExpectAllFieldsSet(message2); 707 708} 709 710TEST(GENERATED_MESSAGE_TEST_NAME, PackedFieldsSerializationToStream) { 711 UNITTEST::TestPackedTypes message1, message2; 712 TestUtil::SetPackedFields(&message1); 713 int size = message1.ByteSizeLong(); 714 std::string data; 715 data.resize(size); 716 { 717 // Allow the output stream to buffer only one byte at a time. 718 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 719 io::CodedOutputStream output_stream(&array_stream); 720 message1.SerializeWithCachedSizes(&output_stream); 721 EXPECT_FALSE(output_stream.HadError()); 722 EXPECT_EQ(size, output_stream.ByteCount()); 723 } 724 EXPECT_TRUE(message2.ParseFromString(data)); 725 TestUtil::ExpectPackedFieldsSet(message2); 726} 727 728 729TEST(GENERATED_MESSAGE_TEST_NAME, Required) { 730 // Test that IsInitialized() returns false if required fields are missing. 731 UNITTEST::TestRequired message; 732 733 EXPECT_FALSE(message.IsInitialized()); 734 message.set_a(1); 735 EXPECT_FALSE(message.IsInitialized()); 736 message.set_b(2); 737 EXPECT_FALSE(message.IsInitialized()); 738 message.set_c(3); 739 EXPECT_TRUE(message.IsInitialized()); 740} 741 742TEST(GENERATED_MESSAGE_TEST_NAME, RequiredForeign) { 743 // Test that IsInitialized() returns false if required fields in nested 744 // messages are missing. 745 UNITTEST::TestRequiredForeign message; 746 747 EXPECT_TRUE(message.IsInitialized()); 748 749 message.mutable_optional_message(); 750 EXPECT_FALSE(message.IsInitialized()); 751 752 message.mutable_optional_message()->set_a(1); 753 message.mutable_optional_message()->set_b(2); 754 message.mutable_optional_message()->set_c(3); 755 EXPECT_TRUE(message.IsInitialized()); 756 757 message.add_repeated_message(); 758 EXPECT_FALSE(message.IsInitialized()); 759 760 message.mutable_repeated_message(0)->set_a(1); 761 message.mutable_repeated_message(0)->set_b(2); 762 message.mutable_repeated_message(0)->set_c(3); 763 EXPECT_TRUE(message.IsInitialized()); 764} 765 766TEST(GENERATED_MESSAGE_TEST_NAME, ForeignNested) { 767 // Test that TestAllTypes::NestedMessage can be embedded directly into 768 // another message. 769 UNITTEST::TestForeignNested message; 770 771 // If this compiles and runs without crashing, it must work. We have 772 // nothing more to test. 773 UNITTEST::TestAllTypes::NestedMessage* nested = 774 message.mutable_foreign_nested(); 775 nested->set_bb(1); 776} 777 778TEST(GENERATED_MESSAGE_TEST_NAME, ReallyLargeTagNumber) { 779 // Test that really large tag numbers don't break anything. 780 UNITTEST::TestReallyLargeTagNumber message1, message2; 781 std::string data; 782 783 // For the most part, if this compiles and runs then we're probably good. 784 // (The most likely cause for failure would be if something were attempting 785 // to allocate a lookup table of some sort using tag numbers as the index.) 786 // We'll try serializing just for fun. 787 message1.set_a(1234); 788 message1.set_bb(5678); 789 message1.SerializeToString(&data); 790 EXPECT_TRUE(message2.ParseFromString(data)); 791 EXPECT_EQ(1234, message2.a()); 792 EXPECT_EQ(5678, message2.bb()); 793} 794 795TEST(GENERATED_MESSAGE_TEST_NAME, MutualRecursion) { 796 // Test that mutually-recursive message types work. 797 UNITTEST::TestMutualRecursionA message; 798 UNITTEST::TestMutualRecursionA* nested = message.mutable_bb()->mutable_a(); 799 UNITTEST::TestMutualRecursionA* nested2 = nested->mutable_bb()->mutable_a(); 800 801 // Again, if the above compiles and runs, that's all we really have to 802 // test, but just for run we'll check that the system didn't somehow come 803 // up with a pointer loop... 804 EXPECT_NE(&message, nested); 805 EXPECT_NE(&message, nested2); 806 EXPECT_NE(nested, nested2); 807} 808 809TEST(GENERATED_MESSAGE_TEST_NAME, CamelCaseFieldNames) { 810 // This test is mainly checking that the following compiles, which verifies 811 // that the field names were coerced to lower-case. 812 // 813 // Protocol buffers standard style is to use lowercase-with-underscores for 814 // field names. Some old proto1 .protos unfortunately used camel-case field 815 // names. In proto1, these names were forced to lower-case. So, we do the 816 // same thing in proto2. 817 818 UNITTEST::TestCamelCaseFieldNames message; 819 820 message.set_primitivefield(2); 821 message.set_stringfield("foo"); 822 message.set_enumfield(UNITTEST::FOREIGN_FOO); 823 message.mutable_messagefield()->set_c(6); 824 825 message.add_repeatedprimitivefield(8); 826 message.add_repeatedstringfield("moo"); 827 message.add_repeatedenumfield(UNITTEST::FOREIGN_BAR); 828 message.add_repeatedmessagefield()->set_c(15); 829 830 EXPECT_EQ(2, message.primitivefield()); 831 EXPECT_EQ("foo", message.stringfield()); 832 EXPECT_EQ(UNITTEST::FOREIGN_FOO, message.enumfield()); 833 EXPECT_EQ(6, message.messagefield().c()); 834 835 EXPECT_EQ(8, message.repeatedprimitivefield(0)); 836 EXPECT_EQ("moo", message.repeatedstringfield(0)); 837 EXPECT_EQ(UNITTEST::FOREIGN_BAR, message.repeatedenumfield(0)); 838 EXPECT_EQ(15, message.repeatedmessagefield(0).c()); 839} 840 841#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 842 843TEST(GENERATED_MESSAGE_TEST_NAME, TestOptimizedForSize) { 844 // We rely on the tests in reflection_ops_unittest and wire_format_unittest 845 // to really test that reflection-based methods work. Here we are mostly 846 // just making sure that TestOptimizedForSize actually builds and seems to 847 // function. 848 849 UNITTEST::TestOptimizedForSize message, message2; 850 message.set_i(1); 851 message.mutable_msg()->set_c(2); 852 message2.CopyFrom(message); 853 EXPECT_EQ(1, message2.i()); 854 EXPECT_EQ(2, message2.msg().c()); 855} 856 857TEST(GENERATED_MESSAGE_TEST_NAME, TestEmbedOptimizedForSize) { 858 // Verifies that something optimized for speed can contain something optimized 859 // for size. 860 861 UNITTEST::TestEmbedOptimizedForSize message, message2; 862 message.mutable_optional_message()->set_i(1); 863 message.add_repeated_message()->mutable_msg()->set_c(2); 864 std::string data; 865 message.SerializeToString(&data); 866 ASSERT_TRUE(message2.ParseFromString(data)); 867 EXPECT_EQ(1, message2.optional_message().i()); 868 EXPECT_EQ(2, message2.repeated_message(0).msg().c()); 869} 870 871TEST(GENERATED_MESSAGE_TEST_NAME, TestSpaceUsed) { 872 // Allocate explicitly on the heap to prevent arena creation. 873 std::unique_ptr<UNITTEST::TestAllTypes> message1( 874 Arena::CreateMessage<UNITTEST::TestAllTypes>(nullptr)); 875 // sizeof provides a lower bound on SpaceUsedLong(). 876 EXPECT_LE(sizeof(UNITTEST::TestAllTypes), message1->SpaceUsedLong()); 877 const size_t empty_message_size = message1->SpaceUsedLong(); 878 879 // Setting primitive types shouldn't affect the space used. 880 message1->set_optional_int32(123); 881 message1->set_optional_int64(12345); 882 message1->set_optional_uint32(123); 883 message1->set_optional_uint64(12345); 884 EXPECT_EQ(empty_message_size, message1->SpaceUsedLong()); 885 886 // On some STL implementations, setting the string to a small value should 887 // only increase SpaceUsedLong() by the size of a string object, though this 888 // is not true everywhere. 889 message1->set_optional_string("abc"); 890 EXPECT_LE(empty_message_size + message1->optional_string().size(), 891 message1->SpaceUsedLong()); 892 893 // Setting a string to a value larger than the string object itself should 894 // increase SpaceUsedLong(), because it cannot store the value internally. 895 message1->set_optional_string(std::string(sizeof(std::string) + 1, 'x')); 896 int min_expected_increase = message1->optional_string().capacity(); 897 EXPECT_LE(empty_message_size + min_expected_increase, 898 message1->SpaceUsedLong()); 899 900 size_t previous_size = message1->SpaceUsedLong(); 901 // Adding an optional message should increase the size by the size of the 902 // nested message type. NestedMessage is simple enough (1 int field) that it 903 // is equal to sizeof(NestedMessage) 904 message1->mutable_optional_nested_message(); 905 ASSERT_EQ(sizeof(UNITTEST::TestAllTypes::NestedMessage), 906 message1->optional_nested_message().SpaceUsedLong()); 907 EXPECT_EQ(previous_size + 908 sizeof(UNITTEST::TestAllTypes::NestedMessage), 909 message1->SpaceUsedLong()); 910} 911 912TEST(GENERATED_MESSAGE_TEST_NAME, TestOneofSpaceUsed) { 913 // Allocate explicitly on the heap to prevent arena creation. 914 std::unique_ptr<UNITTEST::TestOneof2> message1( 915 Arena::CreateMessage<UNITTEST::TestOneof2>(nullptr)); 916 EXPECT_LE(sizeof(UNITTEST::TestOneof2), message1->SpaceUsedLong()); 917 918 const size_t empty_message_size = message1->SpaceUsedLong(); 919 // Setting primitive types shouldn't affect the space used. 920 message1->set_foo_int(123); 921 message1->set_bar_int(12345); 922 EXPECT_EQ(empty_message_size, message1->SpaceUsedLong()); 923 924 // Setting a string in oneof to a small value should only increase 925 // SpaceUsedLong() by the size of a string object. 926 message1->set_foo_string("abc"); 927 EXPECT_LE(empty_message_size + sizeof(std::string), message1->SpaceUsedLong()); 928 929 // Setting a string in oneof to a value larger than the string object itself 930 // should increase SpaceUsedLong(), because it cannot store the value 931 // internally. 932 message1->set_foo_string(std::string(sizeof(std::string) + 1, 'x')); 933 int min_expected_increase = 934 message1->foo_string().capacity() + sizeof(std::string); 935 EXPECT_LE(empty_message_size + min_expected_increase, 936 message1->SpaceUsedLong()); 937 938 // Setting a message in oneof should delete the other fields and increase the 939 // size by the size of the nested message type. NestedMessage is simple enough 940 // that it is equal to sizeof(NestedMessage). It may be backed by LazyField, 941 // increasing space used by LazyField and backing Cord. 942 message1->mutable_foo_message(); 943 ASSERT_EQ(sizeof(UNITTEST::TestOneof2::NestedMessage), 944 message1->foo_message().SpaceUsedLong()); 945 EXPECT_LE(empty_message_size + sizeof(UNITTEST::TestOneof2::NestedMessage), 946 message1->SpaceUsedLong()); 947} 948 949#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 950 951TEST(GENERATED_MESSAGE_TEST_NAME, FieldConstantValues) { 952 UNITTEST::TestRequired message; 953 EXPECT_EQ(UNITTEST::TestAllTypes_NestedMessage::kBbFieldNumber, 1); 954 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalInt32FieldNumber, 1); 955 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalgroupFieldNumber, 16); 956 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalNestedMessageFieldNumber, 18); 957 EXPECT_EQ(UNITTEST::TestAllTypes::kOptionalNestedEnumFieldNumber, 21); 958 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedInt32FieldNumber, 31); 959 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedgroupFieldNumber, 46); 960 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedNestedMessageFieldNumber, 48); 961 EXPECT_EQ(UNITTEST::TestAllTypes::kRepeatedNestedEnumFieldNumber, 51); 962} 963 964TEST(GENERATED_MESSAGE_TEST_NAME, ExtensionConstantValues) { 965 EXPECT_EQ(UNITTEST::TestRequired::kSingleFieldNumber, 1000); 966 EXPECT_EQ(UNITTEST::TestRequired::kMultiFieldNumber, 1001); 967 EXPECT_EQ(UNITTEST::kOptionalInt32ExtensionFieldNumber, 1); 968 EXPECT_EQ(UNITTEST::kOptionalgroupExtensionFieldNumber, 16); 969 EXPECT_EQ(UNITTEST::kOptionalNestedMessageExtensionFieldNumber, 18); 970 EXPECT_EQ(UNITTEST::kOptionalNestedEnumExtensionFieldNumber, 21); 971 EXPECT_EQ(UNITTEST::kRepeatedInt32ExtensionFieldNumber, 31); 972 EXPECT_EQ(UNITTEST::kRepeatedgroupExtensionFieldNumber, 46); 973 EXPECT_EQ(UNITTEST::kRepeatedNestedMessageExtensionFieldNumber, 48); 974 EXPECT_EQ(UNITTEST::kRepeatedNestedEnumExtensionFieldNumber, 51); 975} 976 977TEST(GENERATED_MESSAGE_TEST_NAME, ParseFromTruncated) { 978 const std::string long_string = std::string(128, 'q'); 979 FileDescriptorProto p; 980 p.add_extension()->set_name(long_string); 981 const std::string msg = p.SerializeAsString(); 982 int successful_count = 0; 983 for (int i = 0; i <= msg.size(); i++) { 984 if (p.ParseFromArray(msg.c_str(), i)) { 985 ++successful_count; 986 } 987 } 988 // We don't really care about how often we succeeded. 989 // As long as we didn't crash, we're happy. 990 EXPECT_GE(successful_count, 1); 991} 992 993// =================================================================== 994 995TEST(GENERATED_ENUM_TEST_NAME, EnumValuesAsSwitchCases) { 996 // Test that our nested enum values can be used as switch cases. This test 997 // doesn't actually do anything, the proof that it works is that it 998 // compiles. 999 int i =0; 1000 UNITTEST::TestAllTypes::NestedEnum a = UNITTEST::TestAllTypes::BAR; 1001 switch (a) { 1002 case UNITTEST::TestAllTypes::FOO: 1003 i = 1; 1004 break; 1005 case UNITTEST::TestAllTypes::BAR: 1006 i = 2; 1007 break; 1008 case UNITTEST::TestAllTypes::BAZ: 1009 i = 3; 1010 break; 1011 case UNITTEST::TestAllTypes::NEG: 1012 i = -1; 1013 break; 1014 // no default case: We want to make sure the compiler recognizes that 1015 // all cases are covered. (GCC warns if you do not cover all cases of 1016 // an enum in a switch.) 1017 } 1018 1019 // Token check just for fun. 1020 EXPECT_EQ(2, i); 1021} 1022 1023TEST(GENERATED_ENUM_TEST_NAME, IsValidValue) { 1024 // Test enum IsValidValue. 1025 EXPECT_TRUE(UNITTEST::TestAllTypes::NestedEnum_IsValid(1)); 1026 EXPECT_TRUE(UNITTEST::TestAllTypes::NestedEnum_IsValid(2)); 1027 EXPECT_TRUE(UNITTEST::TestAllTypes::NestedEnum_IsValid(3)); 1028 1029 EXPECT_FALSE(UNITTEST::TestAllTypes::NestedEnum_IsValid(0)); 1030 EXPECT_FALSE(UNITTEST::TestAllTypes::NestedEnum_IsValid(4)); 1031 1032 // Make sure it also works when there are dups. 1033 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_IsValid(1)); 1034 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_IsValid(2)); 1035 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_IsValid(3)); 1036 1037 EXPECT_FALSE(UNITTEST::TestEnumWithDupValue_IsValid(0)); 1038 EXPECT_FALSE(UNITTEST::TestEnumWithDupValue_IsValid(4)); 1039} 1040 1041TEST(GENERATED_ENUM_TEST_NAME, MinAndMax) { 1042 EXPECT_EQ(UNITTEST::TestAllTypes::NEG, 1043 UNITTEST::TestAllTypes::NestedEnum_MIN); 1044 EXPECT_EQ(UNITTEST::TestAllTypes::BAZ, 1045 UNITTEST::TestAllTypes::NestedEnum_MAX); 1046 EXPECT_EQ(4, UNITTEST::TestAllTypes::NestedEnum_ARRAYSIZE); 1047 1048 EXPECT_EQ(UNITTEST::FOREIGN_FOO, UNITTEST::ForeignEnum_MIN); 1049 EXPECT_EQ(UNITTEST::FOREIGN_BAZ, UNITTEST::ForeignEnum_MAX); 1050 EXPECT_EQ(7, UNITTEST::ForeignEnum_ARRAYSIZE); 1051 1052 EXPECT_EQ(1, UNITTEST::TestEnumWithDupValue_MIN); 1053 EXPECT_EQ(3, UNITTEST::TestEnumWithDupValue_MAX); 1054 EXPECT_EQ(4, UNITTEST::TestEnumWithDupValue_ARRAYSIZE); 1055 1056 EXPECT_EQ(UNITTEST::SPARSE_E, UNITTEST::TestSparseEnum_MIN); 1057 EXPECT_EQ(UNITTEST::SPARSE_C, UNITTEST::TestSparseEnum_MAX); 1058 EXPECT_EQ(12589235, UNITTEST::TestSparseEnum_ARRAYSIZE); 1059 1060 // Make sure we can take the address of _MIN, _MAX and _ARRAYSIZE. 1061 EXPECT_NE(nullptr, &UNITTEST::TestAllTypes::NestedEnum_MIN); 1062 EXPECT_NE(nullptr, &UNITTEST::TestAllTypes::NestedEnum_MAX); 1063 EXPECT_NE(nullptr, &UNITTEST::TestAllTypes::NestedEnum_ARRAYSIZE); 1064 1065 EXPECT_NE(nullptr, &UNITTEST::ForeignEnum_MIN); 1066 EXPECT_NE(nullptr, &UNITTEST::ForeignEnum_MAX); 1067 EXPECT_NE(nullptr, &UNITTEST::ForeignEnum_ARRAYSIZE); 1068 1069 // Make sure we can use _MIN and _MAX as switch cases. 1070 switch (UNITTEST::SPARSE_A) { 1071 case UNITTEST::TestSparseEnum_MIN: 1072 case UNITTEST::TestSparseEnum_MAX: 1073 break; 1074 default: 1075 break; 1076 } 1077} 1078 1079#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 1080 1081TEST(GENERATED_ENUM_TEST_NAME, Name) { 1082 // "Names" in the presence of dup values map to the first alias. 1083 EXPECT_EQ("FOO1", UNITTEST::TestEnumWithDupValue_Name(UNITTEST::FOO1)); 1084 EXPECT_EQ("FOO1", UNITTEST::TestEnumWithDupValue_Name(UNITTEST::FOO2)); 1085 1086 EXPECT_EQ("SPARSE_A", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_A)); 1087 EXPECT_EQ("SPARSE_B", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_B)); 1088 EXPECT_EQ("SPARSE_C", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_C)); 1089 EXPECT_EQ("SPARSE_D", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_D)); 1090 EXPECT_EQ("SPARSE_E", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_E)); 1091 EXPECT_EQ("SPARSE_F", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_F)); 1092 EXPECT_EQ("SPARSE_G", UNITTEST::TestSparseEnum_Name(UNITTEST::SPARSE_G)); 1093} 1094 1095TEST(GENERATED_ENUM_TEST_NAME, Parse) { 1096 UNITTEST::TestEnumWithDupValue dup_value = UNITTEST::FOO1; 1097 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_Parse("FOO1", &dup_value)); 1098 EXPECT_EQ(UNITTEST::FOO1, dup_value); 1099 EXPECT_TRUE(UNITTEST::TestEnumWithDupValue_Parse("FOO2", &dup_value)); 1100 EXPECT_EQ(UNITTEST::FOO2, dup_value); 1101 EXPECT_FALSE(UNITTEST::TestEnumWithDupValue_Parse("FOO", &dup_value)); 1102} 1103 1104TEST(GENERATED_ENUM_TEST_NAME, GetEnumDescriptor) { 1105 EXPECT_EQ(UNITTEST::TestAllTypes::NestedEnum_descriptor(), 1106 GetEnumDescriptor<UNITTEST::TestAllTypes::NestedEnum>()); 1107 EXPECT_EQ(UNITTEST::ForeignEnum_descriptor(), 1108 GetEnumDescriptor<UNITTEST::ForeignEnum>()); 1109 EXPECT_EQ(UNITTEST::TestEnumWithDupValue_descriptor(), 1110 GetEnumDescriptor<UNITTEST::TestEnumWithDupValue>()); 1111 EXPECT_EQ(UNITTEST::TestSparseEnum_descriptor(), 1112 GetEnumDescriptor<UNITTEST::TestSparseEnum>()); 1113} 1114 1115enum NonProtoEnum { 1116 kFoo = 1, 1117}; 1118 1119TEST(GENERATED_ENUM_TEST_NAME, IsProtoEnumTypeTrait) { 1120 EXPECT_TRUE(is_proto_enum<UNITTEST::TestAllTypes::NestedEnum>::value); 1121 EXPECT_TRUE(is_proto_enum<UNITTEST::ForeignEnum>::value); 1122 EXPECT_TRUE(is_proto_enum<UNITTEST::TestEnumWithDupValue>::value); 1123 EXPECT_TRUE(is_proto_enum<UNITTEST::TestSparseEnum>::value); 1124 1125 EXPECT_FALSE(is_proto_enum<int>::value); 1126 EXPECT_FALSE(is_proto_enum<NonProtoEnum>::value); 1127} 1128 1129#endif // PROTOBUF_TEST_NO_DESCRIPTORS 1130 1131// =================================================================== 1132 1133#ifndef PROTOBUF_TEST_NO_DESCRIPTORS 1134 1135// Support code for testing services. 1136class GENERATED_SERVICE_TEST_NAME : public testing::Test { 1137 protected: 1138 class MockTestService : public UNITTEST::TestService { 1139 public: 1140 MockTestService() 1141 : called_(false), 1142 method_(""), 1143 controller_(nullptr), 1144 request_(nullptr), 1145 response_(nullptr), 1146 done_(nullptr) {} 1147 1148 ~MockTestService() override {} 1149 1150 void Reset() { called_ = false; } 1151 1152 // implements TestService ---------------------------------------- 1153 1154 void Foo(RpcController* controller, const UNITTEST::FooRequest* request, 1155 UNITTEST::FooResponse* response, Closure* done) override { 1156 ASSERT_FALSE(called_); 1157 called_ = true; 1158 method_ = "Foo"; 1159 controller_ = controller; 1160 request_ = request; 1161 response_ = response; 1162 done_ = done; 1163 } 1164 1165 void Bar(RpcController* controller, const UNITTEST::BarRequest* request, 1166 UNITTEST::BarResponse* response, Closure* done) override { 1167 ASSERT_FALSE(called_); 1168 called_ = true; 1169 method_ = "Bar"; 1170 controller_ = controller; 1171 request_ = request; 1172 response_ = response; 1173 done_ = done; 1174 } 1175 1176 // --------------------------------------------------------------- 1177 1178 bool called_; 1179 std::string method_; 1180 RpcController* controller_; 1181 const Message* request_; 1182 Message* response_; 1183 Closure* done_; 1184 }; 1185 1186 class MockRpcChannel : public RpcChannel { 1187 public: 1188 MockRpcChannel() 1189 : called_(false), 1190 method_(nullptr), 1191 controller_(nullptr), 1192 request_(nullptr), 1193 response_(nullptr), 1194 done_(nullptr), 1195 destroyed_(nullptr) {} 1196 1197 ~MockRpcChannel() override { 1198 if (destroyed_ != nullptr) *destroyed_ = true; 1199 } 1200 1201 void Reset() { called_ = false; } 1202 1203 // implements TestService ---------------------------------------- 1204 1205 void CallMethod(const MethodDescriptor* method, RpcController* controller, 1206 const Message* request, Message* response, 1207 Closure* done) override { 1208 ASSERT_FALSE(called_); 1209 called_ = true; 1210 method_ = method; 1211 controller_ = controller; 1212 request_ = request; 1213 response_ = response; 1214 done_ = done; 1215 } 1216 1217 // --------------------------------------------------------------- 1218 1219 bool called_; 1220 const MethodDescriptor* method_; 1221 RpcController* controller_; 1222 const Message* request_; 1223 Message* response_; 1224 Closure* done_; 1225 bool* destroyed_; 1226 }; 1227 1228 class MockController : public RpcController { 1229 public: 1230 void Reset() override { 1231 ADD_FAILURE() << "Reset() not expected during this test."; 1232 } 1233 bool Failed() const override { 1234 ADD_FAILURE() << "Failed() not expected during this test."; 1235 return false; 1236 } 1237 std::string ErrorText() const override { 1238 ADD_FAILURE() << "ErrorText() not expected during this test."; 1239 return ""; 1240 } 1241 void StartCancel() override { 1242 ADD_FAILURE() << "StartCancel() not expected during this test."; 1243 } 1244 void SetFailed(const std::string& reason) override { 1245 ADD_FAILURE() << "SetFailed() not expected during this test."; 1246 } 1247 bool IsCanceled() const override { 1248 ADD_FAILURE() << "IsCanceled() not expected during this test."; 1249 return false; 1250 } 1251 void NotifyOnCancel(Closure* callback) override { 1252 ADD_FAILURE() << "NotifyOnCancel() not expected during this test."; 1253 } 1254 }; 1255 1256 GENERATED_SERVICE_TEST_NAME() 1257 : descriptor_(UNITTEST::TestService::descriptor()), 1258 foo_(descriptor_->FindMethodByName("Foo")), 1259 bar_(descriptor_->FindMethodByName("Bar")), 1260 stub_(&mock_channel_), 1261 done_(::google::protobuf::NewPermanentCallback(&DoNothing)) {} 1262 1263 void SetUp() override { 1264 ASSERT_TRUE(foo_ != nullptr); 1265 ASSERT_TRUE(bar_ != nullptr); 1266 } 1267 1268 const ServiceDescriptor* descriptor_; 1269 const MethodDescriptor* foo_; 1270 const MethodDescriptor* bar_; 1271 1272 MockTestService mock_service_; 1273 MockController mock_controller_; 1274 1275 MockRpcChannel mock_channel_; 1276 UNITTEST::TestService::Stub stub_; 1277 1278 // Just so we don't have to re-define these with every test. 1279 UNITTEST::FooRequest foo_request_; 1280 UNITTEST::FooResponse foo_response_; 1281 UNITTEST::BarRequest bar_request_; 1282 UNITTEST::BarResponse bar_response_; 1283 std::unique_ptr<Closure> done_; 1284}; 1285 1286TEST_F(GENERATED_SERVICE_TEST_NAME, GetDescriptor) { 1287 // Test that GetDescriptor() works. 1288 1289 EXPECT_EQ(descriptor_, mock_service_.GetDescriptor()); 1290} 1291 1292TEST_F(GENERATED_SERVICE_TEST_NAME, GetChannel) { 1293 EXPECT_EQ(&mock_channel_, stub_.channel()); 1294} 1295 1296TEST_F(GENERATED_SERVICE_TEST_NAME, OwnsChannel) { 1297 MockRpcChannel* channel = new MockRpcChannel; 1298 bool destroyed = false; 1299 channel->destroyed_ = &destroyed; 1300 1301 { 1302 UNITTEST::TestService::Stub owning_stub(channel, 1303 Service::STUB_OWNS_CHANNEL); 1304 EXPECT_FALSE(destroyed); 1305 } 1306 1307 EXPECT_TRUE(destroyed); 1308} 1309 1310TEST_F(GENERATED_SERVICE_TEST_NAME, CallMethod) { 1311 // Test that CallMethod() works. 1312 1313 // Call Foo() via CallMethod(). 1314 mock_service_.CallMethod(foo_, &mock_controller_, 1315 &foo_request_, &foo_response_, done_.get()); 1316 1317 ASSERT_TRUE(mock_service_.called_); 1318 1319 EXPECT_EQ("Foo" , mock_service_.method_ ); 1320 EXPECT_EQ(&mock_controller_, mock_service_.controller_); 1321 EXPECT_EQ(&foo_request_ , mock_service_.request_ ); 1322 EXPECT_EQ(&foo_response_ , mock_service_.response_ ); 1323 EXPECT_EQ(done_.get() , mock_service_.done_ ); 1324 1325 // Try again, but call Bar() instead. 1326 mock_service_.Reset(); 1327 mock_service_.CallMethod(bar_, &mock_controller_, 1328 &bar_request_, &bar_response_, done_.get()); 1329 1330 ASSERT_TRUE(mock_service_.called_); 1331 EXPECT_EQ("Bar", mock_service_.method_); 1332} 1333 1334TEST_F(GENERATED_SERVICE_TEST_NAME, CallMethodTypeFailure) { 1335 // Verify death if we call Foo() with Bar's message types. 1336 1337#ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet 1338 EXPECT_DEBUG_DEATH( 1339 mock_service_.CallMethod(foo_, &mock_controller_, 1340 &foo_request_, &bar_response_, done_.get()), 1341 "dynamic_cast"); 1342 1343 mock_service_.Reset(); 1344 EXPECT_DEBUG_DEATH( 1345 mock_service_.CallMethod(foo_, &mock_controller_, 1346 &bar_request_, &foo_response_, done_.get()), 1347 "dynamic_cast"); 1348#endif // PROTOBUF_HAS_DEATH_TEST 1349} 1350 1351TEST_F(GENERATED_SERVICE_TEST_NAME, GetPrototypes) { 1352 // Test Get{Request,Response}Prototype() methods. 1353 1354 EXPECT_EQ(&UNITTEST::FooRequest::default_instance(), 1355 &mock_service_.GetRequestPrototype(foo_)); 1356 EXPECT_EQ(&UNITTEST::BarRequest::default_instance(), 1357 &mock_service_.GetRequestPrototype(bar_)); 1358 1359 EXPECT_EQ(&UNITTEST::FooResponse::default_instance(), 1360 &mock_service_.GetResponsePrototype(foo_)); 1361 EXPECT_EQ(&UNITTEST::BarResponse::default_instance(), 1362 &mock_service_.GetResponsePrototype(bar_)); 1363} 1364 1365TEST_F(GENERATED_SERVICE_TEST_NAME, Stub) { 1366 // Test that the stub class works. 1367 1368 // Call Foo() via the stub. 1369 stub_.Foo(&mock_controller_, &foo_request_, &foo_response_, done_.get()); 1370 1371 ASSERT_TRUE(mock_channel_.called_); 1372 1373 EXPECT_EQ(foo_ , mock_channel_.method_ ); 1374 EXPECT_EQ(&mock_controller_, mock_channel_.controller_); 1375 EXPECT_EQ(&foo_request_ , mock_channel_.request_ ); 1376 EXPECT_EQ(&foo_response_ , mock_channel_.response_ ); 1377 EXPECT_EQ(done_.get() , mock_channel_.done_ ); 1378 1379 // Call Bar() via the stub. 1380 mock_channel_.Reset(); 1381 stub_.Bar(&mock_controller_, &bar_request_, &bar_response_, done_.get()); 1382 1383 ASSERT_TRUE(mock_channel_.called_); 1384 EXPECT_EQ(bar_, mock_channel_.method_); 1385} 1386 1387TEST_F(GENERATED_SERVICE_TEST_NAME, NotImplemented) { 1388 // Test that failing to implement a method of a service causes it to fail 1389 // with a "not implemented" error message. 1390 1391 // A service which doesn't implement any methods. 1392 class UnimplementedService : public UNITTEST::TestService { 1393 public: 1394 UnimplementedService() {} 1395 }; 1396 1397 UnimplementedService unimplemented_service; 1398 1399 // And a controller which expects to get a "not implemented" error. 1400 class ExpectUnimplementedController : public MockController { 1401 public: 1402 ExpectUnimplementedController() : called_(false) {} 1403 1404 void SetFailed(const std::string& reason) override { 1405 EXPECT_FALSE(called_); 1406 called_ = true; 1407 EXPECT_EQ("Method Foo() not implemented.", reason); 1408 } 1409 1410 bool called_; 1411 }; 1412 1413 ExpectUnimplementedController controller; 1414 1415 // Call Foo. 1416 unimplemented_service.Foo(&controller, &foo_request_, &foo_response_, 1417 done_.get()); 1418 1419 EXPECT_TRUE(controller.called_); 1420} 1421 1422// =================================================================== 1423 1424class OneofTest : public testing::Test { 1425 protected: 1426 void SetUp() override {} 1427 1428 void ExpectEnumCasesWork(const UNITTEST::TestOneof2 &message) { 1429 switch (message.foo_case()) { 1430 case UNITTEST::TestOneof2::kFooInt: 1431 EXPECT_TRUE(message.has_foo_int()); 1432 break; 1433 case UNITTEST::TestOneof2::kFooString: 1434 EXPECT_TRUE(message.has_foo_string()); 1435 break; 1436 case UNITTEST::TestOneof2::kFooCord: 1437 EXPECT_TRUE(message.has_foo_cord()); 1438 break; 1439 case UNITTEST::TestOneof2::kFooStringPiece: 1440 EXPECT_TRUE(message.has_foo_string_piece()); 1441 break; 1442 case UNITTEST::TestOneof2::kFooBytes: 1443 EXPECT_TRUE(message.has_foo_bytes()); 1444 break; 1445 case UNITTEST::TestOneof2::kFooEnum: 1446 EXPECT_TRUE(message.has_foo_enum()); 1447 break; 1448 case UNITTEST::TestOneof2::kFooMessage: 1449 EXPECT_TRUE(message.has_foo_message()); 1450 break; 1451 case UNITTEST::TestOneof2::kFoogroup: 1452 EXPECT_TRUE(message.has_foogroup()); 1453 break; 1454 case UNITTEST::TestOneof2::kFooLazyMessage: 1455 EXPECT_TRUE(message.has_foo_lazy_message()); 1456 break; 1457 case UNITTEST::TestOneof2::FOO_NOT_SET: 1458 break; 1459 } 1460 } 1461}; 1462 1463TEST_F(OneofTest, SettingOneFieldClearsOthers) { 1464 UNITTEST::TestOneof2 message; 1465 1466 message.set_foo_int(123); 1467 EXPECT_TRUE(message.has_foo_int()); 1468 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1469 1470 message.set_foo_string("foo"); 1471 EXPECT_TRUE(message.has_foo_string()); 1472 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1473 1474 1475 message.set_foo_bytes("moo"); 1476 EXPECT_TRUE(message.has_foo_bytes()); 1477 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1478 1479 message.set_foo_enum(UNITTEST::TestOneof2::FOO); 1480 EXPECT_TRUE(message.has_foo_enum()); 1481 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1482 1483 message.mutable_foo_message()->set_moo_int(234); 1484 EXPECT_TRUE(message.has_foo_message()); 1485 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1486 1487 message.mutable_foogroup()->set_a(345); 1488 EXPECT_TRUE(message.has_foogroup()); 1489 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1490 1491 1492 // we repeat this because we didn't test if this properly clears other fields 1493 // at the beginning. 1494 message.set_foo_int(123); 1495 EXPECT_TRUE(message.has_foo_int()); 1496 TestUtil::ExpectAtMostOneFieldSetInOneof(message); 1497} 1498 1499TEST_F(OneofTest, EnumCases) { 1500 UNITTEST::TestOneof2 message; 1501 1502 message.set_foo_int(123); 1503 ExpectEnumCasesWork(message); 1504 message.set_foo_string("foo"); 1505 ExpectEnumCasesWork(message); 1506 message.set_foo_bytes("moo"); 1507 ExpectEnumCasesWork(message); 1508 message.set_foo_enum(UNITTEST::TestOneof2::FOO); 1509 ExpectEnumCasesWork(message); 1510 message.mutable_foo_message()->set_moo_int(234); 1511 ExpectEnumCasesWork(message); 1512 message.mutable_foogroup()->set_a(345); 1513 ExpectEnumCasesWork(message); 1514} 1515 1516TEST_F(OneofTest, PrimitiveType) { 1517 UNITTEST::TestOneof2 message; 1518 // Unset field returns default value 1519 EXPECT_EQ(message.foo_int(), 0); 1520 1521 message.set_foo_int(123); 1522 EXPECT_TRUE(message.has_foo_int()); 1523 EXPECT_EQ(message.foo_int(), 123); 1524 message.clear_foo_int(); 1525 EXPECT_FALSE(message.has_foo_int()); 1526} 1527 1528TEST_F(OneofTest, EnumType) { 1529 UNITTEST::TestOneof2 message; 1530 // Unset field returns default value 1531 EXPECT_EQ(message.foo_enum(), 1); 1532 1533 message.set_foo_enum(UNITTEST::TestOneof2::FOO); 1534 EXPECT_TRUE(message.has_foo_enum()); 1535 EXPECT_EQ(message.foo_enum(), UNITTEST::TestOneof2::FOO); 1536 message.clear_foo_enum(); 1537 EXPECT_FALSE(message.has_foo_enum()); 1538} 1539 1540TEST_F(OneofTest, SetString) { 1541 // Check that setting a string field in various ways works 1542 UNITTEST::TestOneof2 message; 1543 1544 // Unset field returns default value 1545 EXPECT_EQ(message.foo_string(), ""); 1546 1547 message.set_foo_string("foo"); 1548 EXPECT_TRUE(message.has_foo_string()); 1549 EXPECT_EQ(message.foo_string(), "foo"); 1550 message.clear_foo_string(); 1551 EXPECT_FALSE(message.has_foo_string()); 1552 1553 message.set_foo_string(std::string("bar")); 1554 EXPECT_TRUE(message.has_foo_string()); 1555 EXPECT_EQ(message.foo_string(), "bar"); 1556 message.clear_foo_string(); 1557 EXPECT_FALSE(message.has_foo_string()); 1558 1559 1560 message.set_foo_string("moo", 3); 1561 EXPECT_TRUE(message.has_foo_string()); 1562 EXPECT_EQ(message.foo_string(), "moo"); 1563 message.clear_foo_string(); 1564 EXPECT_FALSE(message.has_foo_string()); 1565 1566 message.mutable_foo_string()->assign("mooo"); 1567 EXPECT_TRUE(message.has_foo_string()); 1568 EXPECT_EQ(message.foo_string(), "mooo"); 1569 message.clear_foo_string(); 1570 EXPECT_FALSE(message.has_foo_string()); 1571 1572 message.set_foo_string("corge"); 1573 EXPECT_TRUE(message.has_foo_string()); 1574 EXPECT_EQ(message.foo_string(), "corge"); 1575 message.clear_foo_string(); 1576 EXPECT_FALSE(message.has_foo_string()); 1577} 1578 1579TEST_F(OneofTest, ReleaseString) { 1580 // Check that release_foo() starts out nullptr, and gives us a value 1581 // that we can delete after it's been set. 1582 UNITTEST::TestOneof2 message; 1583 1584 EXPECT_EQ(nullptr, message.release_foo_string()); 1585 EXPECT_FALSE(message.has_foo_string()); 1586 1587 message.set_foo_string("blah"); 1588 EXPECT_TRUE(message.has_foo_string()); 1589 std::unique_ptr<std::string> str(message.release_foo_string()); 1590 EXPECT_FALSE(message.has_foo_string()); 1591 ASSERT_TRUE(str != nullptr); 1592 EXPECT_EQ("blah", *str); 1593 1594 EXPECT_EQ(nullptr, message.release_foo_string()); 1595 EXPECT_FALSE(message.has_foo_string()); 1596} 1597 1598TEST_F(OneofTest, SetAllocatedString) { 1599 // Check that set_allocated_foo() works for strings. 1600 UNITTEST::TestOneof2 message; 1601 1602 EXPECT_FALSE(message.has_foo_string()); 1603 const std::string kHello("hello"); 1604 message.set_foo_string(kHello); 1605 EXPECT_TRUE(message.has_foo_string()); 1606 1607 message.set_allocated_foo_string(nullptr); 1608 EXPECT_FALSE(message.has_foo_string()); 1609 EXPECT_EQ("", message.foo_string()); 1610 1611 message.set_allocated_foo_string(new std::string(kHello)); 1612 EXPECT_TRUE(message.has_foo_string()); 1613 EXPECT_EQ(kHello, message.foo_string()); 1614} 1615 1616TEST_F(OneofTest, ArenaSetAllocatedString) { 1617 // Check that set_allocated_foo() works for strings. 1618 Arena arena; 1619 UNITTEST::TestOneof2* message = 1620 Arena::CreateMessage<UNITTEST::TestOneof2>(&arena); 1621 1622 EXPECT_FALSE(message->has_foo_string()); 1623 const std::string kHello("hello"); 1624 message->set_foo_string(kHello); 1625 EXPECT_TRUE(message->has_foo_string()); 1626 1627 message->set_allocated_foo_string(nullptr); 1628 EXPECT_FALSE(message->has_foo_string()); 1629 EXPECT_EQ("", message->foo_string()); 1630 1631 message->set_allocated_foo_string(new std::string(kHello)); 1632 EXPECT_TRUE(message->has_foo_string()); 1633 EXPECT_EQ(kHello, message->foo_string()); 1634} 1635 1636 1637TEST_F(OneofTest, SetMessage) { 1638 // Check that setting a message field works 1639 UNITTEST::TestOneof2 message; 1640 1641 // Unset field returns default instance 1642 EXPECT_EQ(&message.foo_message(), 1643 &UNITTEST::TestOneof2_NestedMessage::default_instance()); 1644 EXPECT_EQ(message.foo_message().moo_int(), 0); 1645 1646 message.mutable_foo_message()->set_moo_int(234); 1647 EXPECT_TRUE(message.has_foo_message()); 1648 EXPECT_EQ(message.foo_message().moo_int(), 234); 1649 message.clear_foo_message(); 1650 EXPECT_FALSE(message.has_foo_message()); 1651} 1652 1653TEST_F(OneofTest, ReleaseMessage) { 1654 // Check that release_foo() starts out nullptr, and gives us a value 1655 // that we can delete after it's been set. 1656 UNITTEST::TestOneof2 message; 1657 1658 EXPECT_EQ(nullptr, message.release_foo_message()); 1659 EXPECT_FALSE(message.has_foo_message()); 1660 1661 message.mutable_foo_message()->set_moo_int(1); 1662 EXPECT_TRUE(message.has_foo_message()); 1663 std::unique_ptr<UNITTEST::TestOneof2_NestedMessage> mes( 1664 message.release_foo_message()); 1665 EXPECT_FALSE(message.has_foo_message()); 1666 ASSERT_TRUE(mes != nullptr); 1667 EXPECT_EQ(1, mes->moo_int()); 1668 1669 EXPECT_EQ(nullptr, message.release_foo_message()); 1670 EXPECT_FALSE(message.has_foo_message()); 1671} 1672 1673TEST_F(OneofTest, SetAllocatedMessage) { 1674 // Check that set_allocated_foo() works for messages. 1675 UNITTEST::TestOneof2 message; 1676 1677 EXPECT_FALSE(message.has_foo_message()); 1678 1679 message.mutable_foo_message()->set_moo_int(1); 1680 EXPECT_TRUE(message.has_foo_message()); 1681 1682 message.set_allocated_foo_message(nullptr); 1683 EXPECT_FALSE(message.has_foo_message()); 1684 EXPECT_EQ(&message.foo_message(), 1685 &UNITTEST::TestOneof2_NestedMessage::default_instance()); 1686 1687 message.mutable_foo_message()->set_moo_int(1); 1688 UNITTEST::TestOneof2_NestedMessage* mes = message.release_foo_message(); 1689 ASSERT_TRUE(mes != nullptr); 1690 EXPECT_FALSE(message.has_foo_message()); 1691 1692 message.set_allocated_foo_message(mes); 1693 EXPECT_TRUE(message.has_foo_message()); 1694 EXPECT_EQ(1, message.foo_message().moo_int()); 1695} 1696 1697 1698TEST_F(OneofTest, Clear) { 1699 UNITTEST::TestOneof2 message; 1700 1701 message.set_foo_int(1); 1702 EXPECT_TRUE(message.has_foo_int()); 1703 message.clear_foo_int(); 1704 EXPECT_FALSE(message.has_foo_int()); 1705} 1706 1707TEST_F(OneofTest, Defaults) { 1708 UNITTEST::TestOneof2 message; 1709 1710 EXPECT_FALSE(message.has_foo_int()); 1711 EXPECT_EQ(message.foo_int(), 0); 1712 1713 EXPECT_FALSE(message.has_foo_string()); 1714 EXPECT_EQ(message.foo_string(), ""); 1715 1716 1717 EXPECT_FALSE(message.has_foo_bytes()); 1718 EXPECT_EQ(message.foo_bytes(), ""); 1719 1720 EXPECT_FALSE(message.has_foo_enum()); 1721 EXPECT_EQ(message.foo_enum(), 1); 1722 1723 EXPECT_FALSE(message.has_foo_message()); 1724 EXPECT_EQ(message.foo_message().moo_int(), 0); 1725 1726 EXPECT_FALSE(message.has_foogroup()); 1727 EXPECT_EQ(message.foogroup().a(), 0); 1728 1729 1730 EXPECT_FALSE(message.has_bar_int()); 1731 EXPECT_EQ(message.bar_int(), 5); 1732 1733 EXPECT_FALSE(message.has_bar_string()); 1734 EXPECT_EQ(message.bar_string(), "STRING"); 1735 1736 1737 EXPECT_FALSE(message.has_bar_bytes()); 1738 EXPECT_EQ(message.bar_bytes(), "BYTES"); 1739 1740 EXPECT_FALSE(message.has_bar_enum()); 1741 EXPECT_EQ(message.bar_enum(), 2); 1742} 1743 1744TEST_F(OneofTest, SwapWithEmpty) { 1745 UNITTEST::TestOneof2 message1, message2; 1746 message1.set_foo_string("FOO"); 1747 EXPECT_TRUE(message1.has_foo_string()); 1748 message1.Swap(&message2); 1749 EXPECT_FALSE(message1.has_foo_string()); 1750 EXPECT_TRUE(message2.has_foo_string()); 1751 EXPECT_EQ(message2.foo_string(), "FOO"); 1752} 1753 1754TEST_F(OneofTest, SwapWithSelf) { 1755 UNITTEST::TestOneof2 message; 1756 message.set_foo_string("FOO"); 1757 EXPECT_TRUE(message.has_foo_string()); 1758 message.Swap(&message); 1759 EXPECT_TRUE(message.has_foo_string()); 1760 EXPECT_EQ(message.foo_string(), "FOO"); 1761} 1762 1763TEST_F(OneofTest, SwapBothHasFields) { 1764 UNITTEST::TestOneof2 message1, message2; 1765 1766 message1.set_foo_string("FOO"); 1767 EXPECT_TRUE(message1.has_foo_string()); 1768 message2.mutable_foo_message()->set_moo_int(1); 1769 EXPECT_TRUE(message2.has_foo_message()); 1770 1771 message1.Swap(&message2); 1772 EXPECT_FALSE(message1.has_foo_string()); 1773 EXPECT_FALSE(message2.has_foo_message()); 1774 EXPECT_TRUE(message1.has_foo_message()); 1775 EXPECT_EQ(message1.foo_message().moo_int(), 1); 1776 EXPECT_TRUE(message2.has_foo_string()); 1777 EXPECT_EQ(message2.foo_string(), "FOO"); 1778} 1779 1780TEST_F(OneofTest, CopyConstructor) { 1781 UNITTEST::TestOneof2 message1; 1782 message1.set_foo_bytes("FOO"); 1783 1784 UNITTEST::TestOneof2 message2(message1); 1785 EXPECT_TRUE(message2.has_foo_bytes()); 1786 EXPECT_EQ(message2.foo_bytes(), "FOO"); 1787} 1788 1789TEST_F(OneofTest, CopyFrom) { 1790 UNITTEST::TestOneof2 message1, message2; 1791 message1.set_foo_enum(UNITTEST::TestOneof2::BAR); 1792 EXPECT_TRUE(message1.has_foo_enum()); 1793 1794 message2.CopyFrom(message1); 1795 EXPECT_TRUE(message2.has_foo_enum()); 1796 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::BAR); 1797 1798 // Copying from self should be a no-op. 1799 message2.CopyFrom(message2); 1800 EXPECT_TRUE(message2.has_foo_enum()); 1801 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::BAR); 1802} 1803 1804TEST_F(OneofTest, CopyAssignmentOperator) { 1805 UNITTEST::TestOneof2 message1; 1806 message1.mutable_foo_message()->set_moo_int(123); 1807 EXPECT_TRUE(message1.has_foo_message()); 1808 1809 UNITTEST::TestOneof2 message2; 1810 message2 = message1; 1811 EXPECT_EQ(message2.foo_message().moo_int(), 123); 1812 1813 // Make sure that self-assignment does something sane. 1814 message2 = *&message2; // Avoid -Wself-assign. 1815 EXPECT_EQ(message2.foo_message().moo_int(), 123); 1816} 1817 1818TEST_F(OneofTest, UpcastCopyFrom) { 1819 // Test the CopyFrom method that takes in the generic const Message& 1820 // parameter. 1821 UNITTEST::TestOneof2 message1, message2; 1822 message1.mutable_foogroup()->set_a(123); 1823 EXPECT_TRUE(message1.has_foogroup()); 1824 1825 const Message* source = implicit_cast<const Message*>(&message1); 1826 message2.CopyFrom(*source); 1827 1828 EXPECT_TRUE(message2.has_foogroup()); 1829 EXPECT_EQ(message2.foogroup().a(), 123); 1830} 1831 1832// Test the generated SerializeWithCachedSizesToArray(), 1833// This indirectly tests MergePartialFromCodedStream() 1834// We have to test each field type separately because we cannot set them at the 1835// same time 1836TEST_F(OneofTest, SerializationToArray) { 1837 // Primitive type 1838 { 1839 UNITTEST::TestOneof2 message1, message2; 1840std::string data; 1841message1.set_foo_int(123); 1842int size = message1.ByteSizeLong(); 1843data.resize(size); 1844uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 1845uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 1846EXPECT_EQ(size, end - start); 1847EXPECT_TRUE(message2.ParseFromString(data)); 1848EXPECT_EQ(message2.foo_int(), 123); 1849 } 1850 1851 // String 1852 { 1853 UNITTEST::TestOneof2 message1, message2; 1854 std::string data; 1855 message1.set_foo_string("foo"); 1856 int size = message1.ByteSizeLong(); 1857 data.resize(size); 1858 uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 1859 uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 1860 EXPECT_EQ(size, end - start); 1861 EXPECT_TRUE(message2.ParseFromString(data)); 1862 EXPECT_EQ(message2.foo_string(), "foo"); 1863 } 1864 1865 1866 // Bytes 1867 { 1868 UNITTEST::TestOneof2 message1, message2; 1869 std::string data; 1870 message1.set_foo_bytes("moo"); 1871 int size = message1.ByteSizeLong(); 1872 data.resize(size); 1873 uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 1874 uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 1875 EXPECT_EQ(size, end - start); 1876 EXPECT_TRUE(message2.ParseFromString(data)); 1877 EXPECT_EQ(message2.foo_bytes(), "moo"); 1878 } 1879 1880 // Enum 1881 { 1882 UNITTEST::TestOneof2 message1, message2; 1883 std::string data; 1884 message1.set_foo_enum(UNITTEST::TestOneof2::FOO); 1885 int size = message1.ByteSizeLong(); 1886 data.resize(size); 1887 uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 1888 uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 1889 EXPECT_EQ(size, end - start); 1890 EXPECT_TRUE(message2.ParseFromString(data)); 1891 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO); 1892 } 1893 1894 // Message 1895 { 1896 UNITTEST::TestOneof2 message1, message2; 1897 std::string data; 1898 message1.mutable_foo_message()->set_moo_int(234); 1899 int size = message1.ByteSizeLong(); 1900 data.resize(size); 1901 uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 1902 uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 1903 EXPECT_EQ(size, end - start); 1904 EXPECT_TRUE(message2.ParseFromString(data)); 1905 EXPECT_EQ(message2.foo_message().moo_int(), 234); 1906 } 1907 1908 // Group 1909 { 1910 UNITTEST::TestOneof2 message1, message2; 1911 std::string data; 1912 message1.mutable_foogroup()->set_a(345); 1913 int size = message1.ByteSizeLong(); 1914 data.resize(size); 1915 uint8_t* start = reinterpret_cast<uint8_t*>(::google::protobuf::string_as_array(&data)); 1916 uint8_t* end = message1.SerializeWithCachedSizesToArray(start); 1917 EXPECT_EQ(size, end - start); 1918 EXPECT_TRUE(message2.ParseFromString(data)); 1919 EXPECT_EQ(message2.foogroup().a(), 345); 1920 } 1921 1922} 1923 1924// Test the generated SerializeWithCachedSizes() by forcing the buffer to write 1925// one byte at a time. 1926// This indirectly tests MergePartialFromCodedStream() 1927// We have to test each field type separately because we cannot set them at the 1928// same time 1929TEST_F(OneofTest, SerializationToStream) { 1930 // Primitive type 1931 { 1932 UNITTEST::TestOneof2 message1, message2; 1933std::string data; 1934message1.set_foo_int(123); 1935int size = message1.ByteSizeLong(); 1936data.resize(size); 1937 1938{ 1939 // Allow the output stream to buffer only one byte at a time. 1940 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 1941 io::CodedOutputStream output_stream(&array_stream); 1942 message1.SerializeWithCachedSizes(&output_stream); 1943 EXPECT_FALSE(output_stream.HadError()); 1944 EXPECT_EQ(size, output_stream.ByteCount()); 1945} 1946 1947 EXPECT_TRUE(message2.ParseFromString(data)); 1948 EXPECT_EQ(message2.foo_int(), 123); 1949 } 1950 1951 // String 1952 { 1953 UNITTEST::TestOneof2 message1, message2; 1954 std::string data; 1955 message1.set_foo_string("foo"); 1956 int size = message1.ByteSizeLong(); 1957 data.resize(size); 1958 1959 { 1960 // Allow the output stream to buffer only one byte at a time. 1961 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 1962 io::CodedOutputStream output_stream(&array_stream); 1963 message1.SerializeWithCachedSizes(&output_stream); 1964 EXPECT_FALSE(output_stream.HadError()); 1965 EXPECT_EQ(size, output_stream.ByteCount()); 1966 } 1967 1968 EXPECT_TRUE(message2.ParseFromString(data)); 1969 EXPECT_EQ(message2.foo_string(), "foo"); 1970 } 1971 1972 1973 // Bytes 1974 { 1975 UNITTEST::TestOneof2 message1, message2; 1976 std::string data; 1977 message1.set_foo_bytes("moo"); 1978 int size = message1.ByteSizeLong(); 1979 data.resize(size); 1980 1981 { 1982 // Allow the output stream to buffer only one byte at a time. 1983 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 1984 io::CodedOutputStream output_stream(&array_stream); 1985 message1.SerializeWithCachedSizes(&output_stream); 1986 EXPECT_FALSE(output_stream.HadError()); 1987 EXPECT_EQ(size, output_stream.ByteCount()); 1988 } 1989 1990 EXPECT_TRUE(message2.ParseFromString(data)); 1991 EXPECT_EQ(message2.foo_bytes(), "moo"); 1992 } 1993 1994 // Enum 1995 { 1996 UNITTEST::TestOneof2 message1, message2; 1997 std::string data; 1998 message1.set_foo_enum(UNITTEST::TestOneof2::FOO); 1999 int size = message1.ByteSizeLong(); 2000 data.resize(size); 2001 2002 { 2003 // Allow the output stream to buffer only one byte at a time. 2004 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 2005 io::CodedOutputStream output_stream(&array_stream); 2006 message1.SerializeWithCachedSizes(&output_stream); 2007 EXPECT_FALSE(output_stream.HadError()); 2008 EXPECT_EQ(size, output_stream.ByteCount()); 2009 } 2010 2011 EXPECT_TRUE(message2.ParseFromString(data)); 2012 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO); 2013 } 2014 2015 // Message 2016 { 2017 UNITTEST::TestOneof2 message1, message2; 2018 std::string data; 2019 message1.mutable_foo_message()->set_moo_int(234); 2020 int size = message1.ByteSizeLong(); 2021 data.resize(size); 2022 2023 { 2024 // Allow the output stream to buffer only one byte at a time. 2025 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 2026 io::CodedOutputStream output_stream(&array_stream); 2027 message1.SerializeWithCachedSizes(&output_stream); 2028 EXPECT_FALSE(output_stream.HadError()); 2029 EXPECT_EQ(size, output_stream.ByteCount()); 2030 } 2031 2032 EXPECT_TRUE(message2.ParseFromString(data)); 2033 EXPECT_EQ(message2.foo_message().moo_int(), 234); 2034 } 2035 2036 // Group 2037 { 2038 UNITTEST::TestOneof2 message1, message2; 2039 std::string data; 2040 message1.mutable_foogroup()->set_a(345); 2041 int size = message1.ByteSizeLong(); 2042 data.resize(size); 2043 2044 { 2045 // Allow the output stream to buffer only one byte at a time. 2046 io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1); 2047 io::CodedOutputStream output_stream(&array_stream); 2048 message1.SerializeWithCachedSizes(&output_stream); 2049 EXPECT_FALSE(output_stream.HadError()); 2050 EXPECT_EQ(size, output_stream.ByteCount()); 2051 } 2052 2053 EXPECT_TRUE(message2.ParseFromString(data)); 2054 EXPECT_EQ(message2.foogroup().a(), 345); 2055 } 2056 2057} 2058 2059TEST_F(OneofTest, MergeFrom) { 2060 UNITTEST::TestOneof2 message1, message2; 2061 2062 message1.set_foo_int(123); 2063 message2.MergeFrom(message1); 2064 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2065 EXPECT_TRUE(message2.has_foo_int()); 2066 EXPECT_EQ(message2.foo_int(), 123); 2067 2068 message1.set_foo_string("foo"); 2069 message2.MergeFrom(message1); 2070 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2071 EXPECT_TRUE(message2.has_foo_string()); 2072 EXPECT_EQ(message2.foo_string(), "foo"); 2073 2074 2075 message1.set_foo_bytes("moo"); 2076 message2.MergeFrom(message1); 2077 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2078 EXPECT_TRUE(message2.has_foo_bytes()); 2079 EXPECT_EQ(message2.foo_bytes(), "moo"); 2080 2081 message1.set_foo_enum(UNITTEST::TestOneof2::FOO); 2082 message2.MergeFrom(message1); 2083 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2084 EXPECT_TRUE(message2.has_foo_enum()); 2085 EXPECT_EQ(message2.foo_enum(), UNITTEST::TestOneof2::FOO); 2086 2087 message1.mutable_foo_message()->set_moo_int(234); 2088 message2.MergeFrom(message1); 2089 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2090 EXPECT_TRUE(message2.has_foo_message()); 2091 EXPECT_EQ(message2.foo_message().moo_int(), 234); 2092 2093 message1.mutable_foogroup()->set_a(345); 2094 message2.MergeFrom(message1); 2095 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); 2096 EXPECT_TRUE(message2.has_foogroup()); 2097 EXPECT_EQ(message2.foogroup().a(), 345); 2098 2099} 2100 2101TEST(HELPERS_TEST_NAME, TestSCC) { 2102 UNITTEST::TestMutualRecursionA a; 2103 MessageSCCAnalyzer scc_analyzer((Options())); 2104 const SCC* scc = scc_analyzer.GetSCC(a.GetDescriptor()); 2105 std::vector<std::string> names; 2106 names.reserve(scc->descriptors.size()); 2107 for (int i = 0; i < scc->descriptors.size(); i++) { 2108 names.push_back(scc->descriptors[i]->full_name()); 2109 } 2110 std::string package = a.GetDescriptor()->file()->package(); 2111 ASSERT_EQ(names.size(), 4); 2112 std::sort(names.begin(), names.end()); 2113 EXPECT_EQ(names[0], package + ".TestMutualRecursionA"); 2114 EXPECT_EQ(names[1], package + ".TestMutualRecursionA.SubGroup"); 2115 EXPECT_EQ(names[2], package + ".TestMutualRecursionA.SubMessage"); 2116 EXPECT_EQ(names[3], package + ".TestMutualRecursionB"); 2117 2118 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2119 EXPECT_EQ(result.is_recursive, true); 2120 EXPECT_EQ(result.contains_required, false); 2121 EXPECT_EQ(result.contains_cord, true); // TestAllTypes 2122 EXPECT_EQ(result.contains_extension, false); // TestAllTypes 2123} 2124 2125TEST(HELPERS_TEST_NAME, TestSCCAnalysis) { 2126 { 2127 UNITTEST::TestRecursiveMessage msg; 2128 MessageSCCAnalyzer scc_analyzer((Options())); 2129 const SCC* scc = scc_analyzer.GetSCC(msg.GetDescriptor()); 2130 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2131 EXPECT_EQ(result.is_recursive, true); 2132 EXPECT_EQ(result.contains_required, false); 2133 EXPECT_EQ(result.contains_cord, false); 2134 EXPECT_EQ(result.contains_extension, false); 2135 } 2136 { 2137 UNITTEST::TestAllExtensions msg; 2138 MessageSCCAnalyzer scc_analyzer((Options())); 2139 const SCC* scc = scc_analyzer.GetSCC(msg.GetDescriptor()); 2140 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2141 EXPECT_EQ(result.is_recursive, false); 2142 EXPECT_EQ(result.contains_required, false); 2143 EXPECT_EQ(result.contains_cord, false); 2144 EXPECT_EQ(result.contains_extension, true); 2145 } 2146 { 2147 UNITTEST::TestRequired msg; 2148 MessageSCCAnalyzer scc_analyzer((Options())); 2149 const SCC* scc = scc_analyzer.GetSCC(msg.GetDescriptor()); 2150 MessageAnalysis result = scc_analyzer.GetSCCAnalysis(scc); 2151 EXPECT_EQ(result.is_recursive, false); 2152 EXPECT_EQ(result.contains_required, true); 2153 EXPECT_EQ(result.contains_cord, false); 2154 EXPECT_EQ(result.contains_extension, false); 2155 } 2156} 2157 2158} // namespace cpp_unittest 2159} // namespace cpp 2160} // namespace compiler 2161 2162namespace no_generic_services_test { 2163 // Verify that no class called "TestService" was defined in 2164 // unittest_no_generic_services.pb.h by defining a different type by the same 2165 // name. If such a service was generated, this will not compile. 2166 struct TestService { 2167 int i; 2168 }; 2169} 2170 2171namespace compiler { 2172namespace cpp { 2173namespace cpp_unittest { 2174 2175TEST_F(GENERATED_SERVICE_TEST_NAME, NoGenericServices) { 2176 // Verify that non-services in unittest_no_generic_services.proto were 2177 // generated. 2178 ::protobuf_unittest::no_generic_services_test::TestMessage message; 2179 message.set_a(1); 2180 message.SetExtension( 2181 ::protobuf_unittest::no_generic_services_test::test_extension, 123); 2182 ::protobuf_unittest::no_generic_services_test::TestEnum e = 2183 ::protobuf_unittest::no_generic_services_test::FOO; 2184 EXPECT_EQ(e, 1); 2185 2186 // Verify that a ServiceDescriptor is generated for the service even if the 2187 // class itself is not. 2188 const FileDescriptor* file = 2189 ::google::protobuf::unittest::no_generic_services_test::TestMessage::descriptor() 2190 ->file(); 2191 2192 ASSERT_EQ(1, file->service_count()); 2193 EXPECT_EQ("TestService", file->service(0)->name()); 2194 ASSERT_EQ(1, file->service(0)->method_count()); 2195 EXPECT_EQ("Foo", file->service(0)->method(0)->name()); 2196} 2197 2198#endif // !PROTOBUF_TEST_NO_DESCRIPTORS 2199 2200// =================================================================== 2201 2202// This test must run last. It verifies that descriptors were or were not 2203// initialized depending on whether PROTOBUF_TEST_NO_DESCRIPTORS was defined. 2204// When this is defined, we skip all tests which are expected to trigger 2205// descriptor initialization. This verifies that everything else still works 2206// if descriptors are not initialized. 2207TEST(DESCRIPTOR_INIT_TEST_NAME, Initialized) { 2208#ifdef PROTOBUF_TEST_NO_DESCRIPTORS 2209 bool should_have_descriptors = false; 2210#else 2211 bool should_have_descriptors = true; 2212#endif 2213 2214 EXPECT_EQ(should_have_descriptors, 2215 DescriptorPool::generated_pool()->InternalIsFileLoaded( 2216 TestUtil::MaybeTranslatePath(UNITTEST_PROTO_PATH))); 2217} 2218 2219} // namespace cpp_unittest 2220 2221} // namespace cpp 2222} // namespace compiler 2223} // namespace protobuf 2224} // namespace google 2225 2226#include <google/protobuf/port_undef.inc> 2227