text_format_conformance_suite.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "text_format_conformance_suite.h"
  31. #include "conformance_test.h"
  32. #include <google/protobuf/any.pb.h>
  33. #include <google/protobuf/test_messages_proto2.pb.h>
  34. #include <google/protobuf/test_messages_proto3.pb.h>
  35. #include <google/protobuf/text_format.h>
  36. using conformance::ConformanceRequest;
  37. using conformance::ConformanceResponse;
  38. using conformance::WireFormat;
  39. using google::protobuf::Message;
  40. using google::protobuf::TextFormat;
  41. using protobuf_test_messages::proto2::TestAllTypesProto2;
  42. using protobuf_test_messages::proto3::TestAllTypesProto3;
  43. using std::string;
  44. namespace google {
  45. namespace protobuf {
  46. TextFormatConformanceTestSuite::TextFormatConformanceTestSuite() {
  47. SetFailureListFlagName("--text_format_failure_list");
  48. }
  49. bool TextFormatConformanceTestSuite::ParseTextFormatResponse(
  50. const ConformanceResponse& response, Message* test_message) {
  51. if (!TextFormat::ParseFromString(response.text_payload(), test_message)) {
  52. GOOGLE_LOG(ERROR) << "INTERNAL ERROR: internal text->protobuf transcode "
  53. << "yielded unparseable proto. Text payload: "
  54. << response.text_payload();
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool TextFormatConformanceTestSuite::ParseResponse(
  60. const ConformanceResponse& response,
  61. const ConformanceRequestSetting& setting, Message* test_message) {
  62. const ConformanceRequest& request = setting.GetRequest();
  63. WireFormat requested_output = request.requested_output_format();
  64. const string& test_name = setting.GetTestName();
  65. ConformanceLevel level = setting.GetLevel();
  66. switch (response.result_case()) {
  67. case ConformanceResponse::kProtobufPayload: {
  68. if (requested_output != conformance::PROTOBUF) {
  69. ReportFailure(
  70. test_name, level, request, response,
  71. StrCat("Test was asked for ", WireFormatToString(requested_output),
  72. " output but provided PROTOBUF instead.")
  73. .c_str());
  74. return false;
  75. }
  76. if (!test_message->ParseFromString(response.protobuf_payload())) {
  77. ReportFailure(test_name, level, request, response,
  78. "Protobuf output we received from test was unparseable.");
  79. return false;
  80. }
  81. break;
  82. }
  83. case ConformanceResponse::kTextPayload: {
  84. if (requested_output != conformance::TEXT_FORMAT) {
  85. ReportFailure(
  86. test_name, level, request, response,
  87. StrCat("Test was asked for ", WireFormatToString(requested_output),
  88. " output but provided TEXT_FORMAT instead.")
  89. .c_str());
  90. return false;
  91. }
  92. if (!ParseTextFormatResponse(response, test_message)) {
  93. ReportFailure(
  94. test_name, level, request, response,
  95. "TEXT_FORMAT output we received from test was unparseable.");
  96. return false;
  97. }
  98. break;
  99. }
  100. default:
  101. GOOGLE_LOG(FATAL) << test_name
  102. << ": unknown payload type: " << response.result_case();
  103. }
  104. return true;
  105. }
  106. void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name,
  107. ConformanceLevel level,
  108. const string& input) {
  109. TestAllTypesProto3 prototype;
  110. // We don't expect output, but if the program erroneously accepts the protobuf
  111. // we let it send its response as this. We must not leave it unspecified.
  112. ConformanceRequestSetting setting(
  113. level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT,
  114. conformance::TEXT_FORMAT_TEST, prototype, test_name, input);
  115. const ConformanceRequest& request = setting.GetRequest();
  116. ConformanceResponse response;
  117. string effective_test_name = StrCat(setting.ConformanceLevelToString(level),
  118. ".Proto3.TextFormatInput.", test_name);
  119. RunTest(effective_test_name, request, &response);
  120. if (response.result_case() == ConformanceResponse::kParseError) {
  121. ReportSuccess(effective_test_name);
  122. } else if (response.result_case() == ConformanceResponse::kSkipped) {
  123. ReportSkip(effective_test_name, request, response);
  124. } else {
  125. ReportFailure(effective_test_name, level, request, response,
  126. "Should have failed to parse, but didn't.");
  127. }
  128. }
  129. void TextFormatConformanceTestSuite::RunValidTextFormatTest(
  130. const string& test_name, ConformanceLevel level, const string& input_text) {
  131. TestAllTypesProto3 prototype;
  132. RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype);
  133. }
  134. void TextFormatConformanceTestSuite::RunValidTextFormatTestProto2(
  135. const string& test_name, ConformanceLevel level, const string& input_text) {
  136. TestAllTypesProto2 prototype;
  137. RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype);
  138. }
  139. void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage(
  140. const string& test_name, ConformanceLevel level, const string& input_text,
  141. const Message& prototype) {
  142. ConformanceRequestSetting setting1(
  143. level, conformance::TEXT_FORMAT, conformance::PROTOBUF,
  144. conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text);
  145. RunValidInputTest(setting1, input_text);
  146. ConformanceRequestSetting setting2(
  147. level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT,
  148. conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text);
  149. RunValidInputTest(setting2, input_text);
  150. }
  151. void TextFormatConformanceTestSuite::RunSuiteImpl() {
  152. RunValidTextFormatTest("HelloWorld", REQUIRED,
  153. "optional_string: 'Hello, World!'");
  154. // Integer fields.
  155. RunValidTextFormatTest("Int32FieldMaxValue", REQUIRED,
  156. "optional_int32: 2147483647");
  157. RunValidTextFormatTest("Int32FieldMinValue", REQUIRED,
  158. "optional_int32: -2147483648");
  159. RunValidTextFormatTest("Uint32FieldMaxValue", REQUIRED,
  160. "optional_uint32: 4294967295");
  161. RunValidTextFormatTest("Int64FieldMaxValue", REQUIRED,
  162. "optional_int64: 9223372036854775807");
  163. RunValidTextFormatTest("Int64FieldMinValue", REQUIRED,
  164. "optional_int64: -9223372036854775808");
  165. RunValidTextFormatTest("Uint64FieldMaxValue", REQUIRED,
  166. "optional_uint64: 18446744073709551615");
  167. // Parsers reject out-of-bound integer values.
  168. ExpectParseFailure("Int32FieldTooLarge", REQUIRED,
  169. "optional_int32: 2147483648");
  170. ExpectParseFailure("Int32FieldTooSmall", REQUIRED,
  171. "optional_int32: -2147483649");
  172. ExpectParseFailure("Uint32FieldTooLarge", REQUIRED,
  173. "optional_uint32: 4294967296");
  174. ExpectParseFailure("Int64FieldTooLarge", REQUIRED,
  175. "optional_int64: 9223372036854775808");
  176. ExpectParseFailure("Int64FieldTooSmall", REQUIRED,
  177. "optional_int64: -9223372036854775809");
  178. ExpectParseFailure("Uint64FieldTooLarge", REQUIRED,
  179. "optional_uint64: 18446744073709551616");
  180. // Floating point fields
  181. RunValidTextFormatTest("FloatField", REQUIRED,
  182. "optional_float: 3.192837");
  183. RunValidTextFormatTest("FloatFieldWithVeryPreciseNumber", REQUIRED,
  184. "optional_float: 3.123456789123456789");
  185. RunValidTextFormatTest("FloatFieldMaxValue", REQUIRED,
  186. "optional_float: 3.40282e+38");
  187. RunValidTextFormatTest("FloatFieldMinValue", REQUIRED,
  188. "optional_float: 1.17549e-38");
  189. RunValidTextFormatTest("FloatFieldNaNValue", REQUIRED,
  190. "optional_float: NaN");
  191. RunValidTextFormatTest("FloatFieldPosInfValue", REQUIRED,
  192. "optional_float: inf");
  193. RunValidTextFormatTest("FloatFieldNegInfValue", REQUIRED,
  194. "optional_float: -inf");
  195. RunValidTextFormatTest("FloatFieldWithInt32Max", REQUIRED,
  196. "optional_float: 4294967296");
  197. RunValidTextFormatTest("FloatFieldLargerThanInt64", REQUIRED,
  198. "optional_float: 9223372036854775808");
  199. ExpectParseFailure("FloatFieldTooLarge", REQUIRED,
  200. "optional_int32: 3.4028235e+39");
  201. ExpectParseFailure("FloatFieldTooSmall", REQUIRED,
  202. "optional_int32: 1.17549e-39");
  203. // Group fields
  204. RunValidTextFormatTestProto2("GroupFieldNoColon", REQUIRED,
  205. "Data { group_int32: 1 }");
  206. RunValidTextFormatTestProto2("GroupFieldWithColon", REQUIRED,
  207. "Data: { group_int32: 1 }");
  208. RunValidTextFormatTestProto2("GroupFieldEmpty", REQUIRED,
  209. "Data {}");
  210. }
  211. } // namespace protobuf
  212. } // namespace google