conformance_test.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <google/protobuf/descriptor.h>
  38. #include <google/protobuf/stubs/common.h>
  39. #include <google/protobuf/util/type_resolver.h>
  40. #include <google/protobuf/wire_format_lite.h>
  41. #include <functional>
  42. #include <string>
  43. #include <vector>
  44. #include "conformance.pb.h"
  45. namespace conformance {
  46. class ConformanceRequest;
  47. class ConformanceResponse;
  48. } // namespace conformance
  49. namespace protobuf_test_messages {
  50. namespace proto3 {
  51. class TestAllTypesProto3;
  52. } // namespace proto3
  53. } // namespace protobuf_test_messages
  54. namespace google {
  55. namespace protobuf {
  56. class ConformanceTestSuite;
  57. class ConformanceTestRunner {
  58. public:
  59. virtual ~ConformanceTestRunner() {}
  60. // Call to run a single conformance test.
  61. //
  62. // "input" is a serialized conformance.ConformanceRequest.
  63. // "output" should be set to a serialized conformance.ConformanceResponse.
  64. //
  65. // If there is any error in running the test itself, set "runtime_error" in
  66. // the response.
  67. virtual void RunTest(const std::string& test_name,
  68. const std::string& input,
  69. std::string* output) = 0;
  70. };
  71. // Test runner that spawns the process being tested and communicates with it
  72. // over a pipe.
  73. class ForkPipeRunner : public ConformanceTestRunner {
  74. public:
  75. // Note: Run() doesn't take ownership of the pointers inside suites.
  76. static int Run(int argc, char *argv[],
  77. const std::vector<ConformanceTestSuite*>& suites);
  78. ForkPipeRunner(const std::string& executable,
  79. const std::vector<string>& executable_args)
  80. : child_pid_(-1),
  81. executable_(executable),
  82. executable_args_(executable_args) {}
  83. explicit ForkPipeRunner(const std::string& executable)
  84. : child_pid_(-1), executable_(executable) {}
  85. virtual ~ForkPipeRunner() {}
  86. void RunTest(const std::string& test_name,
  87. const std::string& request,
  88. std::string* response);
  89. private:
  90. void SpawnTestProgram();
  91. void CheckedWrite(int fd, const void *buf, size_t len);
  92. bool TryRead(int fd, void *buf, size_t len);
  93. void CheckedRead(int fd, void *buf, size_t len);
  94. int write_fd_;
  95. int read_fd_;
  96. pid_t child_pid_;
  97. std::string executable_;
  98. const std::vector<string> executable_args_;
  99. std::string current_test_name_;
  100. };
  101. // Class representing the test suite itself. To run it, implement your own
  102. // class derived from ConformanceTestRunner, class derived from
  103. // ConformanceTestSuite and then write code like:
  104. //
  105. // class MyConformanceTestSuite : public ConformanceTestSuite {
  106. // public:
  107. // void RunSuiteImpl() {
  108. // // INSERT ACTURAL TESTS.
  109. // }
  110. // };
  111. //
  112. // class MyConformanceTestRunner : public ConformanceTestRunner {
  113. // public:
  114. // static int Run(int argc, char *argv[],
  115. // ConformanceTestSuite* suite);
  116. //
  117. // private:
  118. // virtual void RunTest(...) {
  119. // // INSERT YOUR FRAMEWORK-SPECIFIC CODE HERE.
  120. // }
  121. // };
  122. //
  123. // int main() {
  124. // MyConformanceTestSuite suite;
  125. // MyConformanceTestRunner::Run(argc, argv, &suite);
  126. // }
  127. //
  128. class ConformanceTestSuite {
  129. public:
  130. ConformanceTestSuite()
  131. : verbose_(false),
  132. enforce_recommended_(false),
  133. failure_list_flag_name_("--failure_list") {}
  134. virtual ~ConformanceTestSuite() {}
  135. void SetVerbose(bool verbose) { verbose_ = verbose; }
  136. // Whether to require the testee to pass RECOMMENDED tests. By default failing
  137. // a RECOMMENDED test case will not fail the entire suite but will only
  138. // generated a warning. If this flag is set to true, RECOMMENDED tests will
  139. // be treated the same way as REQUIRED tests and failing a RECOMMENDED test
  140. // case will cause the entire test suite to fail as well. An implementation
  141. // can enable this if it wants to be strictly conforming to protobuf spec.
  142. // See the comments about ConformanceLevel below to learn more about the
  143. // difference between REQUIRED and RECOMMENDED test cases.
  144. void SetEnforceRecommended(bool value) {
  145. enforce_recommended_ = value;
  146. }
  147. // Gets the flag name to the failure list file.
  148. // By default, this would return --failure_list
  149. string GetFailureListFlagName() {
  150. return failure_list_flag_name_;
  151. }
  152. void SetFailureListFlagName(const std::string& failure_list_flag_name) {
  153. failure_list_flag_name_ = failure_list_flag_name;
  154. }
  155. // Run all the conformance tests against the given test runner.
  156. // Test output will be stored in "output".
  157. //
  158. // Returns true if the set of failing tests was exactly the same as the
  159. // failure list.
  160. // The filename here is *only* used to create/format useful error messages for
  161. // how to update the failure list. We do NOT read this file at all.
  162. bool RunSuite(ConformanceTestRunner* runner, std::string* output,
  163. const std::string& filename,
  164. conformance::FailureSet* failure_list);
  165. protected:
  166. // Test cases are classified into a few categories:
  167. // REQUIRED: the test case must be passed for an implementation to be
  168. // interoperable with other implementations. For example, a
  169. // parser implementaiton must accept both packed and unpacked
  170. // form of repeated primitive fields.
  171. // RECOMMENDED: the test case is not required for the implementation to
  172. // be interoperable with other implementations, but is
  173. // recommended for best performance and compatibility. For
  174. // example, a proto3 serializer should serialize repeated
  175. // primitive fields in packed form, but an implementation
  176. // failing to do so will still be able to communicate with
  177. // other implementations.
  178. enum ConformanceLevel {
  179. REQUIRED = 0,
  180. RECOMMENDED = 1,
  181. };
  182. class ConformanceRequestSetting {
  183. public:
  184. ConformanceRequestSetting(
  185. ConformanceLevel level,
  186. conformance::WireFormat input_format,
  187. conformance::WireFormat output_format,
  188. conformance::TestCategory test_category,
  189. const Message& prototype_message,
  190. const string& test_name, const string& input);
  191. virtual ~ConformanceRequestSetting() {}
  192. Message* GetTestMessage() const;
  193. string GetTestName() const;
  194. const conformance::ConformanceRequest& GetRequest() const {
  195. return request_;
  196. }
  197. const ConformanceLevel GetLevel() const {
  198. return level_;
  199. }
  200. string ConformanceLevelToString(ConformanceLevel level) const;
  201. void SetPrintUnknownFields(bool print_unknown_fields) {
  202. request_.set_print_unknown_fields(true);
  203. }
  204. void SetPrototypeMessageForCompare(const Message& message) {
  205. prototype_message_for_compare_.reset(message.New());
  206. }
  207. protected:
  208. virtual string InputFormatString(conformance::WireFormat format) const;
  209. virtual string OutputFormatString(conformance::WireFormat format) const;
  210. conformance::ConformanceRequest request_;
  211. private:
  212. ConformanceLevel level_;
  213. ::conformance::WireFormat input_format_;
  214. ::conformance::WireFormat output_format_;
  215. const Message& prototype_message_;
  216. std::unique_ptr<Message> prototype_message_for_compare_;
  217. string test_name_;
  218. };
  219. bool CheckSetEmpty(const std::set<string>& set_to_check,
  220. const std::string& write_to_file, const std::string& msg);
  221. string WireFormatToString(conformance::WireFormat wire_format);
  222. // Parse payload in the response to the given message. Returns true on
  223. // success.
  224. virtual bool ParseResponse(
  225. const conformance::ConformanceResponse& response,
  226. const ConformanceRequestSetting& setting,
  227. Message* test_message) = 0;
  228. void VerifyResponse(const ConformanceRequestSetting& setting,
  229. const string& equivalent_wire_format,
  230. const conformance::ConformanceResponse& response,
  231. bool need_report_success, bool require_same_wire_format);
  232. void ReportSuccess(const std::string& test_name);
  233. void ReportFailure(const string& test_name,
  234. ConformanceLevel level,
  235. const conformance::ConformanceRequest& request,
  236. const conformance::ConformanceResponse& response,
  237. const char* fmt, ...);
  238. void ReportSkip(const string& test_name,
  239. const conformance::ConformanceRequest& request,
  240. const conformance::ConformanceResponse& response);
  241. void RunValidInputTest(const ConformanceRequestSetting& setting,
  242. const string& equivalent_text_format);
  243. void RunValidBinaryInputTest(const ConformanceRequestSetting& setting,
  244. const string& equivalent_wire_format,
  245. bool require_same_wire_format = false);
  246. void RunTest(const std::string& test_name,
  247. const conformance::ConformanceRequest& request,
  248. conformance::ConformanceResponse* response);
  249. void AddExpectedFailedTest(const std::string& test_name);
  250. virtual void RunSuiteImpl() = 0;
  251. ConformanceTestRunner* runner_;
  252. int successes_;
  253. int expected_failures_;
  254. bool verbose_;
  255. bool enforce_recommended_;
  256. std::string output_;
  257. std::string failure_list_flag_name_;
  258. std::string failure_list_filename_;
  259. // The set of test names that are expected to fail in this run, but haven't
  260. // failed yet.
  261. std::set<std::string> expected_to_fail_;
  262. // The set of test names that have been run. Used to ensure that there are no
  263. // duplicate names in the suite.
  264. std::set<std::string> test_names_;
  265. // The set of tests that failed, but weren't expected to.
  266. std::set<std::string> unexpected_failing_tests_;
  267. // The set of tests that succeeded, but weren't expected to.
  268. std::set<std::string> unexpected_succeeding_tests_;
  269. // The set of tests that the testee opted out of;
  270. std::set<std::string> skipped_;
  271. };
  272. } // namespace protobuf
  273. } // namespace google
  274. #endif // CONFORMANCE_CONFORMANCE_TEST_H