conformance_test.h 11 KB

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