conformance_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include <set>
  31. #include <stdarg.h>
  32. #include <string>
  33. #include <fstream>
  34. #include "conformance.pb.h"
  35. #include "conformance_test.h"
  36. #include <google/protobuf/stubs/stringprintf.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. #include <google/protobuf/message.h>
  39. #include <google/protobuf/text_format.h>
  40. #include <google/protobuf/util/field_comparator.h>
  41. #include <google/protobuf/util/json_util.h>
  42. #include <google/protobuf/util/message_differencer.h>
  43. using conformance::ConformanceRequest;
  44. using conformance::ConformanceResponse;
  45. using conformance::WireFormat;
  46. using google::protobuf::TextFormat;
  47. using google::protobuf::util::DefaultFieldComparator;
  48. using google::protobuf::util::JsonToBinaryString;
  49. using google::protobuf::util::MessageDifferencer;
  50. using google::protobuf::util::Status;
  51. using std::string;
  52. namespace google {
  53. namespace protobuf {
  54. ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting(
  55. ConformanceLevel level,
  56. conformance::WireFormat input_format,
  57. conformance::WireFormat output_format,
  58. conformance::TestCategory test_category,
  59. const Message& prototype_message,
  60. const string& test_name, const string& input)
  61. : level_(level),
  62. input_format_(input_format),
  63. output_format_(output_format),
  64. prototype_message_(prototype_message),
  65. prototype_message_for_compare_(prototype_message.New()),
  66. test_name_(test_name) {
  67. switch (input_format) {
  68. case conformance::PROTOBUF: {
  69. request_.set_protobuf_payload(input);
  70. break;
  71. }
  72. case conformance::JSON: {
  73. request_.set_json_payload(input);
  74. break;
  75. }
  76. case conformance::JSPB: {
  77. request_.set_jspb_payload(input);
  78. break;
  79. }
  80. case conformance::TEXT_FORMAT: {
  81. request_.set_text_payload(input);
  82. break;
  83. }
  84. default:
  85. GOOGLE_LOG(FATAL) << "Unspecified input format";
  86. }
  87. request_.set_test_category(test_category);
  88. request_.set_message_type(prototype_message.GetDescriptor()->full_name());
  89. request_.set_requested_output_format(output_format);
  90. }
  91. Message* ConformanceTestSuite::ConformanceRequestSetting::
  92. GetTestMessage() const {
  93. return prototype_message_for_compare_->New();
  94. }
  95. string ConformanceTestSuite::ConformanceRequestSetting::
  96. GetTestName() const {
  97. string rname =
  98. prototype_message_.GetDescriptor()->file()->syntax() ==
  99. FileDescriptor::SYNTAX_PROTO3 ? "Proto3" : "Proto2";
  100. return StrCat(ConformanceLevelToString(level_), ".",
  101. rname, ".",
  102. InputFormatString(input_format_),
  103. ".", test_name_, ".",
  104. OutputFormatString(output_format_));
  105. }
  106. string ConformanceTestSuite::ConformanceRequestSetting::
  107. ConformanceLevelToString(
  108. ConformanceLevel level) const {
  109. switch (level) {
  110. case REQUIRED: return "Required";
  111. case RECOMMENDED: return "Recommended";
  112. }
  113. GOOGLE_LOG(FATAL) << "Unknown value: " << level;
  114. return "";
  115. }
  116. string ConformanceTestSuite::ConformanceRequestSetting::
  117. InputFormatString(conformance::WireFormat format) const {
  118. switch (format) {
  119. case conformance::PROTOBUF:
  120. return "ProtobufInput";
  121. case conformance::JSON:
  122. return "JsonInput";
  123. case conformance::TEXT_FORMAT:
  124. return "TextFormatInput";
  125. default:
  126. GOOGLE_LOG(FATAL) << "Unspecified output format";
  127. }
  128. return "";
  129. }
  130. string ConformanceTestSuite::ConformanceRequestSetting::
  131. OutputFormatString(conformance::WireFormat format) const {
  132. switch (format) {
  133. case conformance::PROTOBUF:
  134. return "ProtobufOutput";
  135. case conformance::JSON:
  136. return "JsonOutput";
  137. case conformance::TEXT_FORMAT:
  138. return "TextFormatOutput";
  139. default:
  140. GOOGLE_LOG(FATAL) << "Unspecified output format";
  141. }
  142. return "";
  143. }
  144. void ConformanceTestSuite::ReportSuccess(const string& test_name) {
  145. if (expected_to_fail_.erase(test_name) != 0) {
  146. StringAppendF(&output_,
  147. "ERROR: test %s is in the failure list, but test succeeded. "
  148. "Remove it from the failure list.\n",
  149. test_name.c_str());
  150. unexpected_succeeding_tests_.insert(test_name);
  151. }
  152. successes_++;
  153. }
  154. void ConformanceTestSuite::ReportFailure(const string& test_name,
  155. ConformanceLevel level,
  156. const ConformanceRequest& request,
  157. const ConformanceResponse& response,
  158. const char* fmt, ...) {
  159. if (expected_to_fail_.erase(test_name) == 1) {
  160. expected_failures_++;
  161. if (!verbose_)
  162. return;
  163. } else if (level == RECOMMENDED && !enforce_recommended_) {
  164. StringAppendF(&output_, "WARNING, test=%s: ", test_name.c_str());
  165. } else {
  166. StringAppendF(&output_, "ERROR, test=%s: ", test_name.c_str());
  167. unexpected_failing_tests_.insert(test_name);
  168. }
  169. va_list args;
  170. va_start(args, fmt);
  171. StringAppendV(&output_, fmt, args);
  172. va_end(args);
  173. StringAppendF(&output_, " request=%s, response=%s\n",
  174. request.ShortDebugString().c_str(),
  175. response.ShortDebugString().c_str());
  176. }
  177. void ConformanceTestSuite::ReportSkip(const string& test_name,
  178. const ConformanceRequest& request,
  179. const ConformanceResponse& response) {
  180. if (verbose_) {
  181. StringAppendF(&output_, "SKIPPED, test=%s request=%s, response=%s\n",
  182. test_name.c_str(), request.ShortDebugString().c_str(),
  183. response.ShortDebugString().c_str());
  184. }
  185. skipped_.insert(test_name);
  186. }
  187. void ConformanceTestSuite::RunValidInputTest(
  188. const ConformanceRequestSetting& setting,
  189. const string& equivalent_text_format) {
  190. Message* reference_message = setting.GetTestMessage();
  191. GOOGLE_CHECK(
  192. TextFormat::ParseFromString(equivalent_text_format, reference_message))
  193. << "Failed to parse data for test case: " << setting.GetTestName()
  194. << ", data: " << equivalent_text_format;
  195. const string equivalent_wire_format = reference_message->SerializeAsString();
  196. RunValidBinaryInputTest(setting, equivalent_wire_format);
  197. }
  198. void ConformanceTestSuite::RunValidBinaryInputTest(
  199. const ConformanceRequestSetting& setting,
  200. const string& equivalent_wire_format) {
  201. const ConformanceRequest& request = setting.GetRequest();
  202. ConformanceResponse response;
  203. RunTest(setting.GetTestName(), request, &response);
  204. VerifyResponse(setting, equivalent_wire_format, response, true);
  205. }
  206. void ConformanceTestSuite::VerifyResponse(
  207. const ConformanceRequestSetting& setting,
  208. const string& equivalent_wire_format,
  209. const ConformanceResponse& response,
  210. bool need_report_success) {
  211. Message* test_message = setting.GetTestMessage();
  212. const ConformanceRequest& request = setting.GetRequest();
  213. const string& test_name = setting.GetTestName();
  214. ConformanceLevel level = setting.GetLevel();
  215. Message* reference_message = setting.GetTestMessage();
  216. GOOGLE_CHECK(
  217. reference_message->ParseFromString(equivalent_wire_format))
  218. << "Failed to parse wire data for test case: " << test_name;
  219. switch (response.result_case()) {
  220. case ConformanceResponse::RESULT_NOT_SET:
  221. ReportFailure(test_name, level, request, response,
  222. "Response didn't have any field in the Response.");
  223. return;
  224. case ConformanceResponse::kParseError:
  225. case ConformanceResponse::kRuntimeError:
  226. case ConformanceResponse::kSerializeError:
  227. ReportFailure(test_name, level, request, response,
  228. "Failed to parse input or produce output.");
  229. return;
  230. case ConformanceResponse::kSkipped:
  231. ReportSkip(test_name, request, response);
  232. return;
  233. default:
  234. if (!ParseResponse(response, setting, test_message)) return;
  235. }
  236. MessageDifferencer differencer;
  237. DefaultFieldComparator field_comparator;
  238. field_comparator.set_treat_nan_as_equal(true);
  239. differencer.set_field_comparator(&field_comparator);
  240. string differences;
  241. differencer.ReportDifferencesToString(&differences);
  242. bool check;
  243. check = differencer.Compare(*reference_message, *test_message);
  244. if (check) {
  245. if (need_report_success) {
  246. ReportSuccess(test_name);
  247. }
  248. } else {
  249. ReportFailure(test_name, level, request, response,
  250. "Output was not equivalent to reference message: %s.",
  251. differences.c_str());
  252. }
  253. }
  254. void ConformanceTestSuite::RunTest(const string& test_name,
  255. const ConformanceRequest& request,
  256. ConformanceResponse* response) {
  257. if (test_names_.insert(test_name).second == false) {
  258. GOOGLE_LOG(FATAL) << "Duplicated test name: " << test_name;
  259. }
  260. string serialized_request;
  261. string serialized_response;
  262. request.SerializeToString(&serialized_request);
  263. runner_->RunTest(test_name, serialized_request, &serialized_response);
  264. if (!response->ParseFromString(serialized_response)) {
  265. response->Clear();
  266. response->set_runtime_error("response proto could not be parsed.");
  267. }
  268. if (verbose_) {
  269. StringAppendF(&output_,
  270. "conformance test: name=%s, request=%s, response=%s\n",
  271. test_name.c_str(),
  272. request.ShortDebugString().c_str(),
  273. response->ShortDebugString().c_str());
  274. }
  275. }
  276. bool ConformanceTestSuite::CheckSetEmpty(
  277. const std::set<string>& set_to_check,
  278. const std::string& write_to_file,
  279. const std::string& msg) {
  280. if (set_to_check.empty()) {
  281. return true;
  282. } else {
  283. StringAppendF(&output_, "\n");
  284. StringAppendF(&output_, "%s\n\n", msg.c_str());
  285. for (std::set<string>::const_iterator iter = set_to_check.begin();
  286. iter != set_to_check.end(); ++iter) {
  287. StringAppendF(&output_, " %s\n", iter->c_str());
  288. }
  289. StringAppendF(&output_, "\n");
  290. if (!write_to_file.empty()) {
  291. std::ofstream os(write_to_file);
  292. if (os) {
  293. for (std::set<string>::const_iterator iter = set_to_check.begin();
  294. iter != set_to_check.end(); ++iter) {
  295. os << *iter << "\n";
  296. }
  297. } else {
  298. StringAppendF(&output_, "Failed to open file: %s\n",
  299. write_to_file.c_str());
  300. }
  301. }
  302. return false;
  303. }
  304. }
  305. string ConformanceTestSuite::WireFormatToString(
  306. WireFormat wire_format) {
  307. switch (wire_format) {
  308. case conformance::PROTOBUF:
  309. return "PROTOBUF";
  310. case conformance::JSON:
  311. return "JSON";
  312. case conformance::JSPB:
  313. return "JSPB";
  314. case conformance::TEXT_FORMAT:
  315. return "TEXT_FORMAT";
  316. case conformance::UNSPECIFIED:
  317. return "UNSPECIFIED";
  318. default:
  319. GOOGLE_LOG(FATAL) << "unknown wire type: "
  320. << wire_format;
  321. }
  322. return "";
  323. }
  324. void ConformanceTestSuite::AddExpectedFailedTest(const std::string& test_name) {
  325. expected_to_fail_.insert(test_name);
  326. }
  327. bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner,
  328. std::string* output, const string& filename,
  329. conformance::FailureSet* failure_list) {
  330. runner_ = runner;
  331. successes_ = 0;
  332. expected_failures_ = 0;
  333. skipped_.clear();
  334. test_names_.clear();
  335. unexpected_failing_tests_.clear();
  336. unexpected_succeeding_tests_.clear();
  337. output_ = "\nCONFORMANCE TEST BEGIN ====================================\n\n";
  338. failure_list_filename_ = filename;
  339. expected_to_fail_.clear();
  340. for (const string& failure : failure_list->failure()) {
  341. AddExpectedFailedTest(failure);
  342. }
  343. RunSuiteImpl();
  344. bool ok = true;
  345. if (!CheckSetEmpty(expected_to_fail_, "nonexistent_tests.txt",
  346. "These tests were listed in the failure list, but they "
  347. "don't exist. Remove them from the failure list by "
  348. "running:\n"
  349. " ./update_failure_list.py " + failure_list_filename_ +
  350. " --remove nonexistent_tests.txt")) {
  351. ok = false;
  352. }
  353. if (!CheckSetEmpty(unexpected_failing_tests_, "failing_tests.txt",
  354. "These tests failed. If they can't be fixed right now, "
  355. "you can add them to the failure list so the overall "
  356. "suite can succeed. Add them to the failure list by "
  357. "running:\n"
  358. " ./update_failure_list.py " + failure_list_filename_ +
  359. " --add failing_tests.txt")) {
  360. ok = false;
  361. }
  362. if (!CheckSetEmpty(unexpected_succeeding_tests_, "succeeding_tests.txt",
  363. "These tests succeeded, even though they were listed in "
  364. "the failure list. Remove them from the failure list "
  365. "by running:\n"
  366. " ./update_failure_list.py " + failure_list_filename_ +
  367. " --remove succeeding_tests.txt")) {
  368. ok = false;
  369. }
  370. if (verbose_) {
  371. CheckSetEmpty(skipped_, "",
  372. "These tests were skipped (probably because support for some "
  373. "features is not implemented)");
  374. }
  375. StringAppendF(&output_,
  376. "CONFORMANCE SUITE %s: %d successes, %d skipped, "
  377. "%d expected failures, %d unexpected failures.\n",
  378. ok ? "PASSED" : "FAILED", successes_, skipped_.size(),
  379. expected_failures_, unexpected_failing_tests_.size());
  380. StringAppendF(&output_, "\n");
  381. output->assign(output_);
  382. return ok;
  383. }
  384. } // namespace protobuf
  385. } // namespace google