OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 24 matching lines...) Expand all Loading... |
35 #include <google/protobuf/stubs/shared_ptr.h> | 35 #include <google/protobuf/stubs/shared_ptr.h> |
36 #endif | 36 #endif |
37 #include <sstream> | 37 #include <sstream> |
38 | 38 |
39 #include <google/protobuf/stubs/casts.h> | 39 #include <google/protobuf/stubs/casts.h> |
40 #include <google/protobuf/any.pb.h> | 40 #include <google/protobuf/any.pb.h> |
41 #include <google/protobuf/io/coded_stream.h> | 41 #include <google/protobuf/io/coded_stream.h> |
42 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> | 42 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
43 #include <google/protobuf/descriptor.h> | 43 #include <google/protobuf/descriptor.h> |
44 #include <google/protobuf/util/internal/expecting_objectwriter.h> | 44 #include <google/protobuf/util/internal/expecting_objectwriter.h> |
| 45 #include <google/protobuf/util/internal/testdata/anys.pb.h> |
45 #include <google/protobuf/util/internal/testdata/books.pb.h> | 46 #include <google/protobuf/util/internal/testdata/books.pb.h> |
46 #include <google/protobuf/util/internal/testdata/field_mask.pb.h> | 47 #include <google/protobuf/util/internal/testdata/field_mask.pb.h> |
| 48 #include <google/protobuf/util/internal/testdata/maps.pb.h> |
| 49 #include <google/protobuf/util/internal/testdata/proto3.pb.h> |
| 50 #include <google/protobuf/util/internal/testdata/struct.pb.h> |
| 51 #include <google/protobuf/util/internal/testdata/timestamp_duration.pb.h> |
47 #include <google/protobuf/util/internal/type_info_test_helper.h> | 52 #include <google/protobuf/util/internal/type_info_test_helper.h> |
48 #include <google/protobuf/util/internal/constants.h> | 53 #include <google/protobuf/util/internal/constants.h> |
49 #include <google/protobuf/stubs/strutil.h> | 54 #include <google/protobuf/stubs/strutil.h> |
50 #include <google/protobuf/util/internal/testdata/anys.pb.h> | |
51 #include <google/protobuf/util/internal/testdata/maps.pb.h> | |
52 #include <google/protobuf/util/internal/testdata/struct.pb.h> | |
53 #include <google/protobuf/util/internal/testdata/timestamp_duration.pb.h> | |
54 #include <gtest/gtest.h> | 55 #include <gtest/gtest.h> |
55 | 56 |
56 | 57 |
57 namespace google { | 58 namespace google { |
58 namespace protobuf { | 59 namespace protobuf { |
59 namespace util { | 60 namespace util { |
60 namespace converter { | 61 namespace converter { |
61 | 62 |
62 using google::protobuf::Descriptor; | 63 using google::protobuf::Descriptor; |
63 using google::protobuf::DescriptorPool; | 64 using google::protobuf::DescriptorPool; |
64 using google::protobuf::FileDescriptorProto; | 65 using google::protobuf::FileDescriptorProto; |
65 using google::protobuf::Message; | 66 using google::protobuf::Message; |
66 using google::protobuf::io::ArrayInputStream; | 67 using google::protobuf::io::ArrayInputStream; |
67 using google::protobuf::io::CodedInputStream; | 68 using google::protobuf::io::CodedInputStream; |
68 using util::Status; | 69 using util::Status; |
| 70 using google::protobuf::testing::AnyM; |
| 71 using google::protobuf::testing::AnyOut; |
69 using google::protobuf::testing::Author; | 72 using google::protobuf::testing::Author; |
70 using google::protobuf::testing::BadAuthor; | 73 using google::protobuf::testing::BadAuthor; |
71 using google::protobuf::testing::BadNestedBook; | 74 using google::protobuf::testing::BadNestedBook; |
72 using google::protobuf::testing::Book; | 75 using google::protobuf::testing::Book; |
| 76 using google::protobuf::testing::Book_Label; |
73 using google::protobuf::testing::Cyclic; | 77 using google::protobuf::testing::Cyclic; |
74 using google::protobuf::testing::Book_Label; | 78 using google::protobuf::testing::FieldMaskTest; |
| 79 using google::protobuf::testing::MapOut; |
| 80 using google::protobuf::testing::MapOutWireFormat; |
75 using google::protobuf::testing::NestedBook; | 81 using google::protobuf::testing::NestedBook; |
| 82 using google::protobuf::testing::NestedFieldMask; |
76 using google::protobuf::testing::PackedPrimitive; | 83 using google::protobuf::testing::PackedPrimitive; |
77 using google::protobuf::testing::Primitive; | 84 using google::protobuf::testing::Primitive; |
78 using google::protobuf::testing::more_author; | 85 using google::protobuf::testing::Proto3Message; |
79 using google::protobuf::testing::maps::MapOut; | 86 using google::protobuf::testing::StructType; |
80 using google::protobuf::testing::maps::MapOutWireFormat; | 87 using google::protobuf::testing::TimestampDuration; |
81 using google::protobuf::testing::timestampduration::TimestampDuration; | |
82 using google::protobuf::testing::anys::AnyOut; | |
83 using google::protobuf::testing::anys::AnyM; | |
84 using google::protobuf::testing::FieldMaskTest; | |
85 using google::protobuf::testing::NestedFieldMask; | |
86 using google::protobuf::testing::structs::StructType; | |
87 using ::testing::_; | 88 using ::testing::_; |
88 | 89 |
89 | 90 |
90 namespace { | 91 namespace { |
91 string GetTypeUrl(const Descriptor* descriptor) { | 92 string GetTypeUrl(const Descriptor* descriptor) { |
92 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); | 93 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); |
93 } | 94 } |
94 } // namespace | 95 } // namespace |
95 | 96 |
96 class ProtostreamObjectSourceTest | 97 class ProtostreamObjectSourceTest |
97 : public ::testing::TestWithParam<testing::TypeInfoSource> { | 98 : public ::testing::TestWithParam<testing::TypeInfoSource> { |
98 protected: | 99 protected: |
99 ProtostreamObjectSourceTest() | 100 ProtostreamObjectSourceTest() |
100 : helper_(GetParam()), | 101 : helper_(GetParam()), |
101 mock_(), | 102 mock_(), |
102 ow_(&mock_), | 103 ow_(&mock_), |
103 use_lower_camel_for_enums_(false) { | 104 use_lower_camel_for_enums_(false), |
104 helper_.ResetTypeInfo(Book::descriptor()); | 105 add_trailing_zeros_(false) { |
| 106 helper_.ResetTypeInfo(Book::descriptor(), Proto3Message::descriptor()); |
105 } | 107 } |
106 | 108 |
107 virtual ~ProtostreamObjectSourceTest() {} | 109 virtual ~ProtostreamObjectSourceTest() {} |
108 | 110 |
109 void DoTest(const Message& msg, const Descriptor* descriptor) { | 111 void DoTest(const Message& msg, const Descriptor* descriptor) { |
110 Status status = ExecuteTest(msg, descriptor); | 112 Status status = ExecuteTest(msg, descriptor); |
111 EXPECT_EQ(Status::OK, status); | 113 EXPECT_EQ(Status::OK, status); |
112 } | 114 } |
113 | 115 |
114 Status ExecuteTest(const Message& msg, const Descriptor* descriptor) { | 116 Status ExecuteTest(const Message& msg, const Descriptor* descriptor) { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 primitive.add_rep_double(0.0); | 263 primitive.add_rep_double(0.0); |
262 primitive.add_rep_bool(true); | 264 primitive.add_rep_bool(true); |
263 primitive.add_rep_bool(false); | 265 primitive.add_rep_bool(false); |
264 | 266 |
265 PrepareExpectingObjectWriterForRepeatedPrimitive(); | 267 PrepareExpectingObjectWriterForRepeatedPrimitive(); |
266 return primitive; | 268 return primitive; |
267 } | 269 } |
268 | 270 |
269 void UseLowerCamelForEnums() { use_lower_camel_for_enums_ = true; } | 271 void UseLowerCamelForEnums() { use_lower_camel_for_enums_ = true; } |
270 | 272 |
| 273 void AddTrailingZeros() { add_trailing_zeros_ = true; } |
| 274 |
271 testing::TypeInfoTestHelper helper_; | 275 testing::TypeInfoTestHelper helper_; |
272 | 276 |
273 ::testing::NiceMock<MockObjectWriter> mock_; | 277 ::testing::NiceMock<MockObjectWriter> mock_; |
274 ExpectingObjectWriter ow_; | 278 ExpectingObjectWriter ow_; |
275 bool use_lower_camel_for_enums_; | 279 bool use_lower_camel_for_enums_; |
| 280 bool add_trailing_zeros_; |
276 }; | 281 }; |
277 | 282 |
278 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 283 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
279 ProtostreamObjectSourceTest, | 284 ProtostreamObjectSourceTest, |
280 ::testing::Values( | 285 ::testing::Values( |
281 testing::USE_TYPE_RESOLVER)); | 286 testing::USE_TYPE_RESOLVER)); |
282 | 287 |
283 TEST_P(ProtostreamObjectSourceTest, EmptyMessage) { | 288 TEST_P(ProtostreamObjectSourceTest, EmptyMessage) { |
284 Book empty; | 289 Book empty; |
285 ow_.StartObject("")->EndObject(); | 290 ow_.StartObject("")->EndObject(); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 | 491 |
487 TEST_P(ProtostreamObjectSourceTest, EnumCaseIsUnchangedByDefault) { | 492 TEST_P(ProtostreamObjectSourceTest, EnumCaseIsUnchangedByDefault) { |
488 Book book; | 493 Book book; |
489 book.set_type(Book::ACTION_AND_ADVENTURE); | 494 book.set_type(Book::ACTION_AND_ADVENTURE); |
490 ow_.StartObject("") | 495 ow_.StartObject("") |
491 ->RenderString("type", "ACTION_AND_ADVENTURE") | 496 ->RenderString("type", "ACTION_AND_ADVENTURE") |
492 ->EndObject(); | 497 ->EndObject(); |
493 DoTest(book, Book::descriptor()); | 498 DoTest(book, Book::descriptor()); |
494 } | 499 } |
495 | 500 |
| 501 TEST_P(ProtostreamObjectSourceTest, UnknownEnum) { |
| 502 Proto3Message message; |
| 503 message.set_enum_value(static_cast<Proto3Message::NestedEnum>(1234)); |
| 504 ow_.StartObject("") |
| 505 ->RenderInt32("enumValue", 1234) |
| 506 ->EndObject(); |
| 507 DoTest(message, Proto3Message::descriptor()); |
| 508 } |
| 509 |
496 TEST_P(ProtostreamObjectSourceTest, CyclicMessageDepthTest) { | 510 TEST_P(ProtostreamObjectSourceTest, CyclicMessageDepthTest) { |
497 Cyclic cyclic; | 511 Cyclic cyclic; |
498 cyclic.set_m_int(123); | 512 cyclic.set_m_int(123); |
499 | 513 |
500 Book* book = cyclic.mutable_m_book(); | 514 Book* book = cyclic.mutable_m_book(); |
501 book->set_title("book title"); | 515 book->set_title("book title"); |
502 Cyclic* current = cyclic.mutable_m_cyclic(); | 516 Cyclic* current = cyclic.mutable_m_cyclic(); |
503 Author* current_author = cyclic.add_m_author(); | 517 Author* current_author = cyclic.add_m_author(); |
504 for (int i = 0; i < 63; ++i) { | 518 for (int i = 0; i < 63; ++i) { |
505 Author* next = current_author->add_friend_(); | 519 Author* next = current_author->add_friend_(); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 686 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
673 ProtostreamObjectSourceAnysTest, | 687 ProtostreamObjectSourceAnysTest, |
674 ::testing::Values( | 688 ::testing::Values( |
675 testing::USE_TYPE_RESOLVER)); | 689 testing::USE_TYPE_RESOLVER)); |
676 | 690 |
677 // Tests JSON any support. | 691 // Tests JSON any support. |
678 // | 692 // |
679 // This is the example expected output. | 693 // This is the example expected output. |
680 // { | 694 // { |
681 // "any": { | 695 // "any": { |
682 // "@type": "type.googleapis.com/google.protobuf.testing.anys.AnyM" | 696 // "@type": "type.googleapis.com/google.protobuf.testing.AnyM" |
683 // "foo": "foovalue" | 697 // "foo": "foovalue" |
684 // } | 698 // } |
685 // } | 699 // } |
686 TEST_P(ProtostreamObjectSourceAnysTest, BasicAny) { | 700 TEST_P(ProtostreamObjectSourceAnysTest, BasicAny) { |
687 AnyOut out; | 701 AnyOut out; |
688 ::google::protobuf::Any* any = out.mutable_any(); | 702 ::google::protobuf::Any* any = out.mutable_any(); |
689 | 703 |
690 AnyM m; | 704 AnyM m; |
691 m.set_foo("foovalue"); | 705 m.set_foo("foovalue"); |
692 any->PackFrom(m); | 706 any->PackFrom(m); |
693 | 707 |
694 ow_.StartObject("") | 708 ow_.StartObject("") |
695 ->StartObject("any") | 709 ->StartObject("any") |
696 ->RenderString("@type", | 710 ->RenderString("@type", |
697 "type.googleapis.com/google.protobuf.testing.anys.AnyM") | 711 "type.googleapis.com/google.protobuf.testing.AnyM") |
698 ->RenderString("foo", "foovalue") | 712 ->RenderString("foo", "foovalue") |
699 ->EndObject() | 713 ->EndObject() |
700 ->EndObject(); | 714 ->EndObject(); |
701 | 715 |
702 DoTest(out, AnyOut::descriptor()); | 716 DoTest(out, AnyOut::descriptor()); |
703 } | 717 } |
704 | 718 |
705 TEST_P(ProtostreamObjectSourceAnysTest, RecursiveAny) { | 719 TEST_P(ProtostreamObjectSourceAnysTest, RecursiveAny) { |
706 AnyOut out; | 720 AnyOut out; |
707 ::google::protobuf::Any* any = out.mutable_any(); | 721 ::google::protobuf::Any* any = out.mutable_any(); |
708 any->set_type_url("type.googleapis.com/google.protobuf.Any"); | 722 any->set_type_url("type.googleapis.com/google.protobuf.Any"); |
709 | 723 |
710 ::google::protobuf::Any nested_any; | 724 ::google::protobuf::Any nested_any; |
711 nested_any.set_type_url( | 725 nested_any.set_type_url("type.googleapis.com/google.protobuf.testing.AnyM"); |
712 "type.googleapis.com/google.protobuf.testing.anys.AnyM"); | |
713 | 726 |
714 AnyM m; | 727 AnyM m; |
715 m.set_foo("foovalue"); | 728 m.set_foo("foovalue"); |
716 nested_any.set_value(m.SerializeAsString()); | 729 nested_any.set_value(m.SerializeAsString()); |
717 | 730 |
718 any->set_value(nested_any.SerializeAsString()); | 731 any->set_value(nested_any.SerializeAsString()); |
719 | 732 |
720 ow_.StartObject("") | 733 ow_.StartObject("") |
721 ->StartObject("any") | 734 ->StartObject("any") |
722 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 735 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
723 ->StartObject("value") | 736 ->StartObject("value") |
724 ->RenderString("@type", | 737 ->RenderString("@type", |
725 "type.googleapis.com/google.protobuf.testing.anys.AnyM") | 738 "type.googleapis.com/google.protobuf.testing.AnyM") |
726 ->RenderString("foo", "foovalue") | 739 ->RenderString("foo", "foovalue") |
727 ->EndObject() | 740 ->EndObject() |
728 ->EndObject() | 741 ->EndObject() |
729 ->EndObject(); | 742 ->EndObject(); |
730 | 743 |
731 DoTest(out, AnyOut::descriptor()); | 744 DoTest(out, AnyOut::descriptor()); |
732 } | 745 } |
733 | 746 |
734 TEST_P(ProtostreamObjectSourceAnysTest, DoubleRecursiveAny) { | 747 TEST_P(ProtostreamObjectSourceAnysTest, DoubleRecursiveAny) { |
735 AnyOut out; | 748 AnyOut out; |
736 ::google::protobuf::Any* any = out.mutable_any(); | 749 ::google::protobuf::Any* any = out.mutable_any(); |
737 any->set_type_url("type.googleapis.com/google.protobuf.Any"); | 750 any->set_type_url("type.googleapis.com/google.protobuf.Any"); |
738 | 751 |
739 ::google::protobuf::Any nested_any; | 752 ::google::protobuf::Any nested_any; |
740 nested_any.set_type_url("type.googleapis.com/google.protobuf.Any"); | 753 nested_any.set_type_url("type.googleapis.com/google.protobuf.Any"); |
741 | 754 |
742 ::google::protobuf::Any second_nested_any; | 755 ::google::protobuf::Any second_nested_any; |
743 second_nested_any.set_type_url( | 756 second_nested_any.set_type_url( |
744 "type.googleapis.com/google.protobuf.testing.anys.AnyM"); | 757 "type.googleapis.com/google.protobuf.testing.AnyM"); |
745 | 758 |
746 AnyM m; | 759 AnyM m; |
747 m.set_foo("foovalue"); | 760 m.set_foo("foovalue"); |
748 second_nested_any.set_value(m.SerializeAsString()); | 761 second_nested_any.set_value(m.SerializeAsString()); |
749 nested_any.set_value(second_nested_any.SerializeAsString()); | 762 nested_any.set_value(second_nested_any.SerializeAsString()); |
750 any->set_value(nested_any.SerializeAsString()); | 763 any->set_value(nested_any.SerializeAsString()); |
751 | 764 |
752 ow_.StartObject("") | 765 ow_.StartObject("") |
753 ->StartObject("any") | 766 ->StartObject("any") |
754 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 767 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
755 ->StartObject("value") | 768 ->StartObject("value") |
756 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 769 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
757 ->StartObject("value") | 770 ->StartObject("value") |
758 ->RenderString("@type", | 771 ->RenderString("@type", |
759 "type.googleapis.com/google.protobuf.testing.anys.AnyM") | 772 "type.googleapis.com/google.protobuf.testing.AnyM") |
760 ->RenderString("foo", "foovalue") | 773 ->RenderString("foo", "foovalue") |
761 ->EndObject() | 774 ->EndObject() |
762 ->EndObject() | 775 ->EndObject() |
763 ->EndObject() | 776 ->EndObject() |
764 ->EndObject(); | 777 ->EndObject(); |
765 | 778 |
766 DoTest(out, AnyOut::descriptor()); | 779 DoTest(out, AnyOut::descriptor()); |
767 } | 780 } |
768 | 781 |
769 TEST_P(ProtostreamObjectSourceAnysTest, EmptyAnyOutputsEmptyObject) { | 782 TEST_P(ProtostreamObjectSourceAnysTest, EmptyAnyOutputsEmptyObject) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 TimestampDuration out; | 1007 TimestampDuration out; |
995 google::protobuf::Duration* dur = out.mutable_dur(); | 1008 google::protobuf::Duration* dur = out.mutable_dur(); |
996 // Min allowed seconds + 1 | 1009 // Min allowed seconds + 1 |
997 dur->set_seconds(kDurationMaxSeconds + 1); | 1010 dur->set_seconds(kDurationMaxSeconds + 1); |
998 ow_.StartObject(""); | 1011 ow_.StartObject(""); |
999 | 1012 |
1000 Status status = ExecuteTest(out, TimestampDuration::descriptor()); | 1013 Status status = ExecuteTest(out, TimestampDuration::descriptor()); |
1001 EXPECT_EQ(util::error::INTERNAL, status.error_code()); | 1014 EXPECT_EQ(util::error::INTERNAL, status.error_code()); |
1002 } | 1015 } |
1003 | 1016 |
| 1017 TEST_P(ProtostreamObjectSourceTimestampTest, TimestampDurationDefaultValue) { |
| 1018 TimestampDuration out; |
| 1019 out.mutable_ts()->set_seconds(0); |
| 1020 out.mutable_dur()->set_seconds(0); |
| 1021 |
| 1022 ow_.StartObject("") |
| 1023 ->RenderString("ts", "1970-01-01T00:00:00Z") |
| 1024 ->RenderString("dur", "0s") |
| 1025 ->EndObject(); |
| 1026 |
| 1027 DoTest(out, TimestampDuration::descriptor()); |
| 1028 } |
| 1029 |
| 1030 |
1004 } // namespace converter | 1031 } // namespace converter |
1005 } // namespace util | 1032 } // namespace util |
1006 } // namespace protobuf | 1033 } // namespace protobuf |
1007 } // namespace google | 1034 } // namespace google |
OLD | NEW |