conformance_test.h 12 KB

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