• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include <memory>
9 
10 #include <gtest/gtest.h>
11 #include "google/protobuf/dynamic_message.h"
12 #include "google/protobuf/message_lite.h"
13 #include "google/protobuf/unittest_drop_unknown_fields.pb.h"
14 
15 using unittest_drop_unknown_fields::Foo;
16 using unittest_drop_unknown_fields::FooWithExtraFields;
17 
18 namespace google {
19 namespace protobuf {
20 
TEST(DropUnknownFieldsTest,GeneratedMessage)21 TEST(DropUnknownFieldsTest, GeneratedMessage) {
22   FooWithExtraFields foo_with_extra_fields;
23   foo_with_extra_fields.set_int32_value(1);
24   foo_with_extra_fields.set_enum_value(FooWithExtraFields::MOO);
25   foo_with_extra_fields.set_extra_int32_value(2);
26 
27   Foo foo;
28   ASSERT_TRUE(foo.ParseFromString(foo_with_extra_fields.SerializeAsString()));
29   EXPECT_EQ(1, foo.int32_value());
30   EXPECT_EQ(static_cast<int>(FooWithExtraFields::MOO),
31             static_cast<int>(foo.enum_value()));
32   EXPECT_FALSE(foo.GetReflection()->GetUnknownFields(foo).empty());
33 
34   ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo.SerializeAsString()));
35   EXPECT_EQ(1, foo_with_extra_fields.int32_value());
36   EXPECT_EQ(FooWithExtraFields::MOO, foo_with_extra_fields.enum_value());
37   // The "extra_int32_value" field should not be lost.
38   EXPECT_EQ(2, foo_with_extra_fields.extra_int32_value());
39 }
40 
TEST(DropUnknownFieldsTest,DynamicMessage)41 TEST(DropUnknownFieldsTest, DynamicMessage) {
42   FooWithExtraFields foo_with_extra_fields;
43   foo_with_extra_fields.set_int32_value(1);
44   foo_with_extra_fields.set_enum_value(FooWithExtraFields::MOO);
45   foo_with_extra_fields.set_extra_int32_value(2);
46 
47   DynamicMessageFactory factory;
48   std::unique_ptr<Message> foo(factory.GetPrototype(Foo::descriptor())->New());
49   ASSERT_TRUE(foo->ParseFromString(foo_with_extra_fields.SerializeAsString()));
50   EXPECT_FALSE(foo->GetReflection()->GetUnknownFields(*foo).empty());
51 
52   ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo->SerializeAsString()));
53   EXPECT_EQ(1, foo_with_extra_fields.int32_value());
54   EXPECT_EQ(FooWithExtraFields::MOO, foo_with_extra_fields.enum_value());
55   // The "extra_int32_value" field should not be lost.
56   EXPECT_EQ(2, foo_with_extra_fields.extra_int32_value());
57 }
58 
59 }  // namespace protobuf
60 }  // namespace google
61