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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #include <string> | 42 #include <string> |
43 #include <google/protobuf/stubs/common.h> | 43 #include <google/protobuf/stubs/common.h> |
44 #include <google/protobuf/util/type_resolver.h> | 44 #include <google/protobuf/util/type_resolver.h> |
45 #include <google/protobuf/wire_format_lite.h> | 45 #include <google/protobuf/wire_format_lite.h> |
46 | 46 |
47 #include "third_party/jsoncpp/json.h" | 47 #include "third_party/jsoncpp/json.h" |
48 | 48 |
49 namespace conformance { | 49 namespace conformance { |
50 class ConformanceRequest; | 50 class ConformanceRequest; |
51 class ConformanceResponse; | 51 class ConformanceResponse; |
| 52 } // namespace conformance |
| 53 |
| 54 namespace protobuf_test_messages { |
| 55 namespace proto3 { |
52 class TestAllTypes; | 56 class TestAllTypes; |
53 } // namespace conformance | 57 } // namespace proto3 |
| 58 } // namespace protobuf_test_messages |
54 | 59 |
55 namespace google { | 60 namespace google { |
56 namespace protobuf { | 61 namespace protobuf { |
57 | 62 |
58 class ConformanceTestRunner { | 63 class ConformanceTestRunner { |
59 public: | 64 public: |
60 virtual ~ConformanceTestRunner() {} | 65 virtual ~ConformanceTestRunner() {} |
61 | 66 |
62 // Call to run a single conformance test. | 67 // Call to run a single conformance test. |
63 // | 68 // |
(...skipping 20 matching lines...) Expand all Loading... |
84 // int main() { | 89 // int main() { |
85 // MyConformanceTestRunner runner; | 90 // MyConformanceTestRunner runner; |
86 // google::protobuf::ConformanceTestSuite suite; | 91 // google::protobuf::ConformanceTestSuite suite; |
87 // | 92 // |
88 // std::string output; | 93 // std::string output; |
89 // suite.RunSuite(&runner, &output); | 94 // suite.RunSuite(&runner, &output); |
90 // } | 95 // } |
91 // | 96 // |
92 class ConformanceTestSuite { | 97 class ConformanceTestSuite { |
93 public: | 98 public: |
94 ConformanceTestSuite() : verbose_(false) {} | 99 ConformanceTestSuite() : verbose_(false), enforce_recommended_(false) {} |
95 | 100 |
96 void SetVerbose(bool verbose) { verbose_ = verbose; } | 101 void SetVerbose(bool verbose) { verbose_ = verbose; } |
97 | 102 |
98 // Sets the list of tests that are expected to fail when RunSuite() is called. | 103 // Sets the list of tests that are expected to fail when RunSuite() is called. |
99 // RunSuite() will fail unless the set of failing tests is exactly the same | 104 // RunSuite() will fail unless the set of failing tests is exactly the same |
100 // as this list. | 105 // as this list. |
101 void SetFailureList(const std::vector<std::string>& failure_list); | 106 // |
| 107 // The filename here is *only* used to create/format useful error messages for |
| 108 // how to update the failure list. We do NOT read this file at all. |
| 109 void SetFailureList(const std::string& filename, |
| 110 const std::vector<std::string>& failure_list); |
| 111 |
| 112 // Whether to require the testee to pass RECOMMENDED tests. By default failing |
| 113 // a RECOMMENDED test case will not fail the entire suite but will only |
| 114 // generated a warning. If this flag is set to true, RECOMMENDED tests will |
| 115 // be treated the same way as REQUIRED tests and failing a RECOMMENDED test |
| 116 // case will cause the entire test suite to fail as well. An implementation |
| 117 // can enable this if it wants to be strictly conforming to protobuf spec. |
| 118 // See the comments about ConformanceLevel below to learn more about the |
| 119 // difference between REQUIRED and RECOMMENDED test cases. |
| 120 void SetEnforceRecommended(bool value) { |
| 121 enforce_recommended_ = value; |
| 122 } |
102 | 123 |
103 // Run all the conformance tests against the given test runner. | 124 // Run all the conformance tests against the given test runner. |
104 // Test output will be stored in "output". | 125 // Test output will be stored in "output". |
105 // | 126 // |
106 // Returns true if the set of failing tests was exactly the same as the | 127 // Returns true if the set of failing tests was exactly the same as the |
107 // failure list. If SetFailureList() was not called, returns true if all | 128 // failure list. If SetFailureList() was not called, returns true if all |
108 // tests passed. | 129 // tests passed. |
109 bool RunSuite(ConformanceTestRunner* runner, std::string* output); | 130 bool RunSuite(ConformanceTestRunner* runner, std::string* output); |
110 | 131 |
111 private: | 132 private: |
| 133 // Test cases are classified into a few categories: |
| 134 // REQUIRED: the test case must be passed for an implementation to be |
| 135 // interoperable with other implementations. For example, a |
| 136 // parser implementaiton must accept both packed and unpacked |
| 137 // form of repeated primitive fields. |
| 138 // RECOMMENDED: the test case is not required for the implementation to |
| 139 // be interoperable with other implementations, but is |
| 140 // recommended for best performance and compatibility. For |
| 141 // example, a proto3 serializer should serialize repeated |
| 142 // primitive fields in packed form, but an implementation |
| 143 // failing to do so will still be able to communicate with |
| 144 // other implementations. |
| 145 enum ConformanceLevel { |
| 146 REQUIRED = 0, |
| 147 RECOMMENDED = 1, |
| 148 }; |
| 149 string ConformanceLevelToString(ConformanceLevel level); |
| 150 |
112 void ReportSuccess(const std::string& test_name); | 151 void ReportSuccess(const std::string& test_name); |
113 void ReportFailure(const string& test_name, | 152 void ReportFailure(const string& test_name, |
| 153 ConformanceLevel level, |
114 const conformance::ConformanceRequest& request, | 154 const conformance::ConformanceRequest& request, |
115 const conformance::ConformanceResponse& response, | 155 const conformance::ConformanceResponse& response, |
116 const char* fmt, ...); | 156 const char* fmt, ...); |
117 void ReportSkip(const string& test_name, | 157 void ReportSkip(const string& test_name, |
118 const conformance::ConformanceRequest& request, | 158 const conformance::ConformanceRequest& request, |
119 const conformance::ConformanceResponse& response); | 159 const conformance::ConformanceResponse& response); |
120 void RunTest(const std::string& test_name, | 160 void RunTest(const std::string& test_name, |
121 const conformance::ConformanceRequest& request, | 161 const conformance::ConformanceRequest& request, |
122 conformance::ConformanceResponse* response); | 162 conformance::ConformanceResponse* response); |
123 void RunValidInputTest(const string& test_name, const string& input, | 163 void RunValidInputTest(const string& test_name, |
| 164 ConformanceLevel level, |
| 165 const string& input, |
124 conformance::WireFormat input_format, | 166 conformance::WireFormat input_format, |
125 const string& equivalent_text_format, | 167 const string& equivalent_text_format, |
126 conformance::WireFormat requested_output); | 168 conformance::WireFormat requested_output); |
127 void RunValidJsonTest(const string& test_name, const string& input_json, | 169 void RunValidJsonTest(const string& test_name, |
| 170 ConformanceLevel level, |
| 171 const string& input_json, |
128 const string& equivalent_text_format); | 172 const string& equivalent_text_format); |
129 void RunValidJsonTestWithProtobufInput(const string& test_name, | 173 void RunValidJsonTestWithProtobufInput( |
130 const conformance::TestAllTypes& input, | 174 const string& test_name, |
131 const string& equivalent_text_format); | 175 ConformanceLevel level, |
| 176 const protobuf_test_messages::proto3::TestAllTypes& input, |
| 177 const string& equivalent_text_format); |
| 178 void RunValidProtobufTest(const string& test_name, ConformanceLevel level, |
| 179 const string& input_protobuf, |
| 180 const string& equivalent_text_format); |
| 181 void RunValidProtobufTestWithMessage( |
| 182 const string& test_name, ConformanceLevel level, |
| 183 const protobuf_test_messages::proto3::TestAllTypes& input, |
| 184 const string& equivalent_text_format); |
132 | 185 |
133 typedef std::function<bool(const Json::Value&)> Validator; | 186 typedef std::function<bool(const Json::Value&)> Validator; |
134 void RunValidJsonTestWithValidator(const string& test_name, | 187 void RunValidJsonTestWithValidator(const string& test_name, |
| 188 ConformanceLevel level, |
135 const string& input_json, | 189 const string& input_json, |
136 const Validator& validator); | 190 const Validator& validator); |
137 void ExpectParseFailureForJson(const string& test_name, | 191 void ExpectParseFailureForJson(const string& test_name, |
| 192 ConformanceLevel level, |
138 const string& input_json); | 193 const string& input_json); |
139 void ExpectSerializeFailureForJson(const string& test_name, | 194 void ExpectSerializeFailureForJson(const string& test_name, |
| 195 ConformanceLevel level, |
140 const string& text_format); | 196 const string& text_format); |
141 void ExpectParseFailureForProto(const std::string& proto, | 197 void ExpectParseFailureForProto(const std::string& proto, |
142 const std::string& test_name); | 198 const std::string& test_name, |
| 199 ConformanceLevel level); |
143 void ExpectHardParseFailureForProto(const std::string& proto, | 200 void ExpectHardParseFailureForProto(const std::string& proto, |
144 const std::string& test_name); | 201 const std::string& test_name, |
| 202 ConformanceLevel level); |
145 void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type); | 203 void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type); |
146 bool CheckSetEmpty(const set<string>& set_to_check, const char* msg); | 204 void TestValidDataForType( |
| 205 google::protobuf::FieldDescriptor::Type, |
| 206 std::vector<std::pair<std::string, std::string>> values); |
| 207 bool CheckSetEmpty(const set<string>& set_to_check, |
| 208 const std::string& write_to_file, const std::string& msg); |
147 ConformanceTestRunner* runner_; | 209 ConformanceTestRunner* runner_; |
148 int successes_; | 210 int successes_; |
149 int expected_failures_; | 211 int expected_failures_; |
150 bool verbose_; | 212 bool verbose_; |
| 213 bool enforce_recommended_; |
151 std::string output_; | 214 std::string output_; |
| 215 std::string failure_list_filename_; |
152 | 216 |
153 // The set of test names that are expected to fail in this run, but haven't | 217 // The set of test names that are expected to fail in this run, but haven't |
154 // failed yet. | 218 // failed yet. |
155 std::set<std::string> expected_to_fail_; | 219 std::set<std::string> expected_to_fail_; |
156 | 220 |
157 // The set of test names that have been run. Used to ensure that there are no | 221 // The set of test names that have been run. Used to ensure that there are no |
158 // duplicate names in the suite. | 222 // duplicate names in the suite. |
159 std::set<std::string> test_names_; | 223 std::set<std::string> test_names_; |
160 | 224 |
161 // The set of tests that failed, but weren't expected to. | 225 // The set of tests that failed, but weren't expected to. |
162 std::set<std::string> unexpected_failing_tests_; | 226 std::set<std::string> unexpected_failing_tests_; |
163 | 227 |
164 // The set of tests that succeeded, but weren't expected to. | 228 // The set of tests that succeeded, but weren't expected to. |
165 std::set<std::string> unexpected_succeeding_tests_; | 229 std::set<std::string> unexpected_succeeding_tests_; |
166 | 230 |
167 // The set of tests that the testee opted out of; | 231 // The set of tests that the testee opted out of; |
168 std::set<std::string> skipped_; | 232 std::set<std::string> skipped_; |
169 | 233 |
170 google::protobuf::internal::scoped_ptr<google::protobuf::util::TypeResolver> | 234 google::protobuf::internal::scoped_ptr<google::protobuf::util::TypeResolver> |
171 type_resolver_; | 235 type_resolver_; |
172 std::string type_url_; | 236 std::string type_url_; |
173 }; | 237 }; |
174 | 238 |
175 } // namespace protobuf | 239 } // namespace protobuf |
176 } // namespace google | 240 } // namespace google |
177 | 241 |
178 #endif // CONFORMANCE_CONFORMANCE_TEST_H | 242 #endif // CONFORMANCE_CONFORMANCE_TEST_H |
OLD | NEW |