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