conformance_test.cc 15 KB

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