conformance_test.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/descriptor.h>
  40. #include <google/protobuf/stubs/common.h>
  41. #include <google/protobuf/util/type_resolver.h>
  42. #include <google/protobuf/wire_format_lite.h>
  43. #include "conformance.pb.h"
  44. namespace conformance {
  45. class ConformanceRequest;
  46. class ConformanceResponse;
  47. } // namespace conformance
  48. namespace protobuf_test_messages {
  49. namespace proto3 {
  50. class TestAllTypesProto3;
  51. } // namespace proto3
  52. } // namespace protobuf_test_messages
  53. namespace google {
  54. namespace protobuf {
  55. class ConformanceTestRunner {
  56. public:
  57. virtual ~ConformanceTestRunner() {}
  58. // Call to run a single conformance test.
  59. //
  60. // "input" is a serialized conformance.ConformanceRequest.
  61. // "output" should be set to a serialized conformance.ConformanceResponse.
  62. //
  63. // If there is any error in running the test itself, set "runtime_error" in
  64. // the response.
  65. virtual void RunTest(const std::string& test_name,
  66. const std::string& input,
  67. std::string* output) = 0;
  68. };
  69. // Class representing the test suite itself. To run it, implement your own
  70. // class derived from ConformanceTestRunner, class derived from
  71. // ConformanceTestSuite and then write code like:
  72. //
  73. // class MyConformanceTestSuite : public ConformanceTestSuite {
  74. // public:
  75. // void RunSuiteImpl() {
  76. // // INSERT ACTURAL TESTS.
  77. // }
  78. // };
  79. //
  80. // // Force MyConformanceTestSuite to be added at dynamic initialization
  81. // // time.
  82. // struct StaticTestSuiteInitializer {
  83. // StaticTestSuiteInitializer() {
  84. // AddTestSuite(new MyConformanceTestSuite());
  85. // }
  86. // } static_test_suite_initializer;
  87. //
  88. // class MyConformanceTestRunner : public ConformanceTestRunner {
  89. // public:
  90. // virtual void RunTest(...) {
  91. // // INSERT YOUR FRAMEWORK-SPECIFIC CODE HERE.
  92. // }
  93. // };
  94. //
  95. // int main() {
  96. // MyConformanceTestRunner runner;
  97. // const std::set<ConformanceTestSuite*>& test_suite_set =
  98. // ::google::protobuf::GetTestSuiteSet();
  99. // for (auto suite : test_suite_set) {
  100. // suite->RunSuite(&runner, &output);
  101. // }
  102. // }
  103. //
  104. class ConformanceTestSuite {
  105. public:
  106. ConformanceTestSuite() : verbose_(false), enforce_recommended_(false) {}
  107. virtual ~ConformanceTestSuite() {}
  108. void SetVerbose(bool verbose) { verbose_ = verbose; }
  109. // Sets the list of tests that are expected to fail when RunSuite() is called.
  110. // RunSuite() will fail unless the set of failing tests is exactly the same
  111. // as this list.
  112. //
  113. // The filename here is *only* used to create/format useful error messages for
  114. // how to update the failure list. We do NOT read this file at all.
  115. void SetFailureList(const std::string& filename,
  116. const std::vector<std::string>& failure_list);
  117. // Whether to require the testee to pass RECOMMENDED tests. By default failing
  118. // a RECOMMENDED test case will not fail the entire suite but will only
  119. // generated a warning. If this flag is set to true, RECOMMENDED tests will
  120. // be treated the same way as REQUIRED tests and failing a RECOMMENDED test
  121. // case will cause the entire test suite to fail as well. An implementation
  122. // can enable this if it wants to be strictly conforming to protobuf spec.
  123. // See the comments about ConformanceLevel below to learn more about the
  124. // difference between REQUIRED and RECOMMENDED test cases.
  125. void SetEnforceRecommended(bool value) {
  126. enforce_recommended_ = value;
  127. }
  128. // Run all the conformance tests against the given test runner.
  129. // Test output will be stored in "output".
  130. //
  131. // Returns true if the set of failing tests was exactly the same as the
  132. // failure list. If SetFailureList() was not called, returns true if all
  133. // tests passed.
  134. bool RunSuite(ConformanceTestRunner* runner, std::string* output);
  135. protected:
  136. // Test cases are classified into a few categories:
  137. // REQUIRED: the test case must be passed for an implementation to be
  138. // interoperable with other implementations. For example, a
  139. // parser implementaiton must accept both packed and unpacked
  140. // form of repeated primitive fields.
  141. // RECOMMENDED: the test case is not required for the implementation to
  142. // be interoperable with other implementations, but is
  143. // recommended for best performance and compatibility. For
  144. // example, a proto3 serializer should serialize repeated
  145. // primitive fields in packed form, but an implementation
  146. // failing to do so will still be able to communicate with
  147. // other implementations.
  148. enum ConformanceLevel {
  149. REQUIRED = 0,
  150. RECOMMENDED = 1,
  151. };
  152. class ConformanceRequestSetting {
  153. public:
  154. ConformanceRequestSetting(
  155. ConformanceLevel level,
  156. conformance::WireFormat input_format,
  157. conformance::WireFormat output_format,
  158. conformance::TestCategory test_category,
  159. const Message& prototype_message,
  160. const string& test_name, const string& input);
  161. virtual ~ConformanceRequestSetting() {}
  162. Message* GetTestMessage() const;
  163. string GetTestName() const;
  164. const conformance::ConformanceRequest& GetRequest() const {
  165. return request_;
  166. }
  167. const ConformanceLevel GetLevel() const {
  168. return level_;
  169. }
  170. string ConformanceLevelToString(ConformanceLevel level) const;
  171. protected:
  172. virtual string InputFormatString(conformance::WireFormat format) const;
  173. virtual string OutputFormatString(conformance::WireFormat format) const;
  174. private:
  175. ConformanceLevel level_;
  176. ::conformance::WireFormat input_format_;
  177. ::conformance::WireFormat output_format_;
  178. const Message& prototype_message_;
  179. string test_name_;
  180. conformance::ConformanceRequest request_;
  181. };
  182. bool CheckSetEmpty(const std::set<string>& set_to_check,
  183. const std::string& write_to_file, const std::string& msg);
  184. void ReportSuccess(const std::string& test_name);
  185. void ReportFailure(const string& test_name,
  186. ConformanceLevel level,
  187. const conformance::ConformanceRequest& request,
  188. const conformance::ConformanceResponse& response,
  189. const char* fmt, ...);
  190. void ReportSkip(const string& test_name,
  191. const conformance::ConformanceRequest& request,
  192. const conformance::ConformanceResponse& response);
  193. void RunValidInputTest(const ConformanceRequestSetting& setting,
  194. const string& equivalent_text_format);
  195. void RunValidBinaryInputTest(const ConformanceRequestSetting& setting,
  196. const string& equivalent_wire_format);
  197. void RunTest(const std::string& test_name,
  198. const conformance::ConformanceRequest& request,
  199. conformance::ConformanceResponse* response);
  200. virtual void RunSuiteImpl() = 0;
  201. ConformanceTestRunner* runner_;
  202. int successes_;
  203. int expected_failures_;
  204. bool verbose_;
  205. bool enforce_recommended_;
  206. std::string output_;
  207. std::string failure_list_filename_;
  208. // The set of test names that are expected to fail in this run, but haven't
  209. // failed yet.
  210. std::set<std::string> expected_to_fail_;
  211. // The set of test names that have been run. Used to ensure that there are no
  212. // duplicate names in the suite.
  213. std::set<std::string> test_names_;
  214. // The set of tests that failed, but weren't expected to.
  215. std::set<std::string> unexpected_failing_tests_;
  216. // The set of tests that succeeded, but weren't expected to.
  217. std::set<std::string> unexpected_succeeding_tests_;
  218. // The set of tests that the testee opted out of;
  219. std::set<std::string> skipped_;
  220. std::unique_ptr<google::protobuf::util::TypeResolver>
  221. type_resolver_;
  222. std::string type_url_;
  223. };
  224. void AddTestSuite(ConformanceTestSuite* suite);
  225. const std::set<ConformanceTestSuite*>& GetTestSuiteSet();
  226. } // namespace protobuf
  227. } // namespace google
  228. #endif // CONFORMANCE_CONFORMANCE_TEST_H