conformance_test.cc 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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 <stdarg.h>
  31. #include <string>
  32. #include <fstream>
  33. #include "conformance.pb.h"
  34. #include "conformance_test.h"
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/stubs/stringprintf.h>
  37. #include <google/protobuf/text_format.h>
  38. #include <google/protobuf/util/json_util.h>
  39. #include <google/protobuf/util/field_comparator.h>
  40. #include <google/protobuf/util/message_differencer.h>
  41. #include <google/protobuf/util/type_resolver_util.h>
  42. #include <google/protobuf/wire_format_lite.h>
  43. #include "third_party/jsoncpp/json.h"
  44. using conformance::ConformanceRequest;
  45. using conformance::ConformanceResponse;
  46. using conformance::TestAllTypes;
  47. using conformance::WireFormat;
  48. using google::protobuf::Descriptor;
  49. using google::protobuf::FieldDescriptor;
  50. using google::protobuf::internal::WireFormatLite;
  51. using google::protobuf::TextFormat;
  52. using google::protobuf::util::DefaultFieldComparator;
  53. using google::protobuf::util::JsonToBinaryString;
  54. using google::protobuf::util::MessageDifferencer;
  55. using google::protobuf::util::NewTypeResolverForDescriptorPool;
  56. using google::protobuf::util::Status;
  57. using std::string;
  58. namespace {
  59. static const char kTypeUrlPrefix[] = "type.googleapis.com";
  60. static string GetTypeUrl(const Descriptor* message) {
  61. return string(kTypeUrlPrefix) + "/" + message->full_name();
  62. }
  63. /* Routines for building arbitrary protos *************************************/
  64. // We would use CodedOutputStream except that we want more freedom to build
  65. // arbitrary protos (even invalid ones).
  66. const string empty;
  67. string cat(const string& a, const string& b,
  68. const string& c = empty,
  69. const string& d = empty,
  70. const string& e = empty,
  71. const string& f = empty,
  72. const string& g = empty,
  73. const string& h = empty,
  74. const string& i = empty,
  75. const string& j = empty,
  76. const string& k = empty,
  77. const string& l = empty) {
  78. string ret;
  79. ret.reserve(a.size() + b.size() + c.size() + d.size() + e.size() + f.size() +
  80. g.size() + h.size() + i.size() + j.size() + k.size() + l.size());
  81. ret.append(a);
  82. ret.append(b);
  83. ret.append(c);
  84. ret.append(d);
  85. ret.append(e);
  86. ret.append(f);
  87. ret.append(g);
  88. ret.append(h);
  89. ret.append(i);
  90. ret.append(j);
  91. ret.append(k);
  92. ret.append(l);
  93. return ret;
  94. }
  95. // The maximum number of bytes that it takes to encode a 64-bit varint.
  96. #define VARINT_MAX_LEN 10
  97. size_t vencode64(uint64_t val, char *buf) {
  98. if (val == 0) { buf[0] = 0; return 1; }
  99. size_t i = 0;
  100. while (val) {
  101. uint8_t byte = val & 0x7fU;
  102. val >>= 7;
  103. if (val) byte |= 0x80U;
  104. buf[i++] = byte;
  105. }
  106. return i;
  107. }
  108. string varint(uint64_t x) {
  109. char buf[VARINT_MAX_LEN];
  110. size_t len = vencode64(x, buf);
  111. return string(buf, len);
  112. }
  113. // TODO: proper byte-swapping for big-endian machines.
  114. string fixed32(void *data) { return string(static_cast<char*>(data), 4); }
  115. string fixed64(void *data) { return string(static_cast<char*>(data), 8); }
  116. string delim(const string& buf) { return cat(varint(buf.size()), buf); }
  117. string uint32(uint32_t u32) { return fixed32(&u32); }
  118. string uint64(uint64_t u64) { return fixed64(&u64); }
  119. string flt(float f) { return fixed32(&f); }
  120. string dbl(double d) { return fixed64(&d); }
  121. string zz32(int32_t x) { return varint(WireFormatLite::ZigZagEncode32(x)); }
  122. string zz64(int64_t x) { return varint(WireFormatLite::ZigZagEncode64(x)); }
  123. string tag(uint32_t fieldnum, char wire_type) {
  124. return varint((fieldnum << 3) | wire_type);
  125. }
  126. string submsg(uint32_t fn, const string& buf) {
  127. return cat( tag(fn, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), delim(buf) );
  128. }
  129. #define UNKNOWN_FIELD 666
  130. uint32_t GetFieldNumberForType(FieldDescriptor::Type type, bool repeated) {
  131. const Descriptor* d = TestAllTypes().GetDescriptor();
  132. for (int i = 0; i < d->field_count(); i++) {
  133. const FieldDescriptor* f = d->field(i);
  134. if (f->type() == type && f->is_repeated() == repeated) {
  135. return f->number();
  136. }
  137. }
  138. GOOGLE_LOG(FATAL) << "Couldn't find field with type " << (int)type;
  139. return 0;
  140. }
  141. string UpperCase(string str) {
  142. for (int i = 0; i < str.size(); i++) {
  143. str[i] = toupper(str[i]);
  144. }
  145. return str;
  146. }
  147. } // anonymous namespace
  148. namespace google {
  149. namespace protobuf {
  150. void ConformanceTestSuite::ReportSuccess(const string& test_name) {
  151. if (expected_to_fail_.erase(test_name) != 0) {
  152. StringAppendF(&output_,
  153. "ERROR: test %s is in the failure list, but test succeeded. "
  154. "Remove it from the failure list.\n",
  155. test_name.c_str());
  156. unexpected_succeeding_tests_.insert(test_name);
  157. }
  158. successes_++;
  159. }
  160. void ConformanceTestSuite::ReportFailure(const string& test_name,
  161. const ConformanceRequest& request,
  162. const ConformanceResponse& response,
  163. const char* fmt, ...) {
  164. if (expected_to_fail_.erase(test_name) == 1) {
  165. expected_failures_++;
  166. if (!verbose_)
  167. return;
  168. } else {
  169. StringAppendF(&output_, "ERROR, test=%s: ", test_name.c_str());
  170. unexpected_failing_tests_.insert(test_name);
  171. }
  172. va_list args;
  173. va_start(args, fmt);
  174. StringAppendV(&output_, fmt, args);
  175. va_end(args);
  176. StringAppendF(&output_, " request=%s, response=%s\n",
  177. request.ShortDebugString().c_str(),
  178. response.ShortDebugString().c_str());
  179. }
  180. void ConformanceTestSuite::ReportSkip(const string& test_name,
  181. const ConformanceRequest& request,
  182. const ConformanceResponse& response) {
  183. if (verbose_) {
  184. StringAppendF(&output_, "SKIPPED, test=%s request=%s, response=%s\n",
  185. test_name.c_str(), request.ShortDebugString().c_str(),
  186. response.ShortDebugString().c_str());
  187. }
  188. skipped_.insert(test_name);
  189. }
  190. void ConformanceTestSuite::RunTest(const string& test_name,
  191. const ConformanceRequest& request,
  192. ConformanceResponse* response) {
  193. if (test_names_.insert(test_name).second == false) {
  194. GOOGLE_LOG(FATAL) << "Duplicated test name: " << test_name;
  195. }
  196. string serialized_request;
  197. string serialized_response;
  198. request.SerializeToString(&serialized_request);
  199. runner_->RunTest(test_name, serialized_request, &serialized_response);
  200. if (!response->ParseFromString(serialized_response)) {
  201. response->Clear();
  202. response->set_runtime_error("response proto could not be parsed.");
  203. }
  204. if (verbose_) {
  205. StringAppendF(&output_, "conformance test: name=%s, request=%s, response=%s\n",
  206. test_name.c_str(),
  207. request.ShortDebugString().c_str(),
  208. response->ShortDebugString().c_str());
  209. }
  210. }
  211. void ConformanceTestSuite::RunValidInputTest(
  212. const string& test_name, const string& input, WireFormat input_format,
  213. const string& equivalent_text_format, WireFormat requested_output) {
  214. TestAllTypes reference_message;
  215. GOOGLE_CHECK(
  216. TextFormat::ParseFromString(equivalent_text_format, &reference_message))
  217. << "Failed to parse data for test case: " << test_name
  218. << ", data: " << equivalent_text_format;
  219. ConformanceRequest request;
  220. ConformanceResponse response;
  221. switch (input_format) {
  222. case conformance::PROTOBUF:
  223. request.set_protobuf_payload(input);
  224. break;
  225. case conformance::JSON:
  226. request.set_json_payload(input);
  227. break;
  228. default:
  229. GOOGLE_LOG(FATAL) << "Unspecified input format";
  230. }
  231. request.set_requested_output_format(requested_output);
  232. RunTest(test_name, request, &response);
  233. TestAllTypes test_message;
  234. switch (response.result_case()) {
  235. case ConformanceResponse::RESULT_NOT_SET:
  236. ReportFailure(test_name, request, response,
  237. "Response didn't have any field in the Response.");
  238. return;
  239. case ConformanceResponse::kParseError:
  240. case ConformanceResponse::kRuntimeError:
  241. case ConformanceResponse::kSerializeError:
  242. ReportFailure(test_name, request, response,
  243. "Failed to parse input or produce output.");
  244. return;
  245. case ConformanceResponse::kSkipped:
  246. ReportSkip(test_name, request, response);
  247. return;
  248. case ConformanceResponse::kJsonPayload: {
  249. if (requested_output != conformance::JSON) {
  250. ReportFailure(
  251. test_name, request, response,
  252. "Test was asked for protobuf output but provided JSON instead.");
  253. return;
  254. }
  255. string binary_protobuf;
  256. Status status =
  257. JsonToBinaryString(type_resolver_.get(), type_url_,
  258. response.json_payload(), &binary_protobuf);
  259. if (!status.ok()) {
  260. ReportFailure(test_name, request, response,
  261. "JSON output we received from test was unparseable.");
  262. return;
  263. }
  264. if (!test_message.ParseFromString(binary_protobuf)) {
  265. ReportFailure(test_name, request, response,
  266. "INTERNAL ERROR: internal JSON->protobuf transcode "
  267. "yielded unparseable proto.");
  268. return;
  269. }
  270. break;
  271. }
  272. case ConformanceResponse::kProtobufPayload: {
  273. if (requested_output != conformance::PROTOBUF) {
  274. ReportFailure(
  275. test_name, request, response,
  276. "Test was asked for JSON output but provided protobuf instead.");
  277. return;
  278. }
  279. if (!test_message.ParseFromString(response.protobuf_payload())) {
  280. ReportFailure(test_name, request, response,
  281. "Protobuf output we received from test was unparseable.");
  282. return;
  283. }
  284. break;
  285. }
  286. default:
  287. GOOGLE_LOG(FATAL) << test_name << ": unknown payload type: "
  288. << response.result_case();
  289. }
  290. MessageDifferencer differencer;
  291. DefaultFieldComparator field_comparator;
  292. field_comparator.set_treat_nan_as_equal(true);
  293. differencer.set_field_comparator(&field_comparator);
  294. string differences;
  295. differencer.ReportDifferencesToString(&differences);
  296. if (differencer.Compare(reference_message, test_message)) {
  297. ReportSuccess(test_name);
  298. } else {
  299. ReportFailure(test_name, request, response,
  300. "Output was not equivalent to reference message: %s.",
  301. differences.c_str());
  302. }
  303. }
  304. // Expect that this precise protobuf will cause a parse error.
  305. void ConformanceTestSuite::ExpectParseFailureForProto(
  306. const string& proto, const string& test_name) {
  307. ConformanceRequest request;
  308. ConformanceResponse response;
  309. request.set_protobuf_payload(proto);
  310. string effective_test_name = "ProtobufInput." + test_name;
  311. // We don't expect output, but if the program erroneously accepts the protobuf
  312. // we let it send its response as this. We must not leave it unspecified.
  313. request.set_requested_output_format(conformance::PROTOBUF);
  314. RunTest(effective_test_name, request, &response);
  315. if (response.result_case() == ConformanceResponse::kParseError) {
  316. ReportSuccess(effective_test_name);
  317. } else if (response.result_case() == ConformanceResponse::kSkipped) {
  318. ReportSkip(effective_test_name, request, response);
  319. } else {
  320. ReportFailure(effective_test_name, request, response,
  321. "Should have failed to parse, but didn't.");
  322. }
  323. }
  324. // Expect that this protobuf will cause a parse error, even if it is followed
  325. // by valid protobuf data. We can try running this twice: once with this
  326. // data verbatim and once with this data followed by some valid data.
  327. //
  328. // TODO(haberman): implement the second of these.
  329. void ConformanceTestSuite::ExpectHardParseFailureForProto(
  330. const string& proto, const string& test_name) {
  331. return ExpectParseFailureForProto(proto, test_name);
  332. }
  333. void ConformanceTestSuite::RunValidJsonTest(
  334. const string& test_name, const string& input_json,
  335. const string& equivalent_text_format) {
  336. RunValidInputTest("JsonInput." + test_name + ".ProtobufOutput", input_json,
  337. conformance::JSON, equivalent_text_format,
  338. conformance::PROTOBUF);
  339. RunValidInputTest("JsonInput." + test_name + ".JsonOutput", input_json,
  340. conformance::JSON, equivalent_text_format,
  341. conformance::JSON);
  342. }
  343. void ConformanceTestSuite::RunValidJsonTestWithProtobufInput(
  344. const string& test_name, const TestAllTypes& input,
  345. const string& equivalent_text_format) {
  346. RunValidInputTest("ProtobufInput." + test_name + ".JsonOutput",
  347. input.SerializeAsString(), conformance::PROTOBUF,
  348. equivalent_text_format, conformance::JSON);
  349. }
  350. void ConformanceTestSuite::RunValidProtobufTest(
  351. const string& test_name, const TestAllTypes& input,
  352. const string& equivalent_text_format) {
  353. RunValidInputTest("ProtobufInput." + test_name + ".ProtobufOutput",
  354. input.SerializeAsString(), conformance::PROTOBUF,
  355. equivalent_text_format, conformance::PROTOBUF);
  356. RunValidInputTest("ProtobufInput." + test_name + ".JsonOutput",
  357. input.SerializeAsString(), conformance::PROTOBUF,
  358. equivalent_text_format, conformance::JSON);
  359. }
  360. // According to proto3 JSON specification, JSON serializers follow more strict
  361. // rules than parsers (e.g., a serializer must serialize int32 values as JSON
  362. // numbers while the parser is allowed to accept them as JSON strings). This
  363. // method allows strict checking on a proto3 JSON serializer by inspecting
  364. // the JSON output directly.
  365. void ConformanceTestSuite::RunValidJsonTestWithValidator(
  366. const string& test_name, const string& input_json,
  367. const Validator& validator) {
  368. ConformanceRequest request;
  369. ConformanceResponse response;
  370. request.set_json_payload(input_json);
  371. request.set_requested_output_format(conformance::JSON);
  372. string effective_test_name = "JsonInput." + test_name + ".Validator";
  373. RunTest(effective_test_name, request, &response);
  374. if (response.result_case() == ConformanceResponse::kSkipped) {
  375. ReportSkip(effective_test_name, request, response);
  376. return;
  377. }
  378. if (response.result_case() != ConformanceResponse::kJsonPayload) {
  379. ReportFailure(effective_test_name, request, response,
  380. "Expected JSON payload but got type %d.",
  381. response.result_case());
  382. return;
  383. }
  384. Json::Reader reader;
  385. Json::Value value;
  386. if (!reader.parse(response.json_payload(), value)) {
  387. ReportFailure(effective_test_name, request, response,
  388. "JSON payload cannot be parsed as valid JSON: %s",
  389. reader.getFormattedErrorMessages().c_str());
  390. return;
  391. }
  392. if (!validator(value)) {
  393. ReportFailure(effective_test_name, request, response,
  394. "JSON payload validation failed.");
  395. return;
  396. }
  397. ReportSuccess(effective_test_name);
  398. }
  399. void ConformanceTestSuite::ExpectParseFailureForJson(
  400. const string& test_name, const string& input_json) {
  401. ConformanceRequest request;
  402. ConformanceResponse response;
  403. request.set_json_payload(input_json);
  404. string effective_test_name = "JsonInput." + test_name;
  405. // We don't expect output, but if the program erroneously accepts the protobuf
  406. // we let it send its response as this. We must not leave it unspecified.
  407. request.set_requested_output_format(conformance::JSON);
  408. RunTest(effective_test_name, request, &response);
  409. if (response.result_case() == ConformanceResponse::kParseError) {
  410. ReportSuccess(effective_test_name);
  411. } else if (response.result_case() == ConformanceResponse::kSkipped) {
  412. ReportSkip(effective_test_name, request, response);
  413. } else {
  414. ReportFailure(effective_test_name, request, response,
  415. "Should have failed to parse, but didn't.");
  416. }
  417. }
  418. void ConformanceTestSuite::ExpectSerializeFailureForJson(
  419. const string& test_name, const string& text_format) {
  420. TestAllTypes payload_message;
  421. GOOGLE_CHECK(
  422. TextFormat::ParseFromString(text_format, &payload_message))
  423. << "Failed to parse: " << text_format;
  424. ConformanceRequest request;
  425. ConformanceResponse response;
  426. request.set_protobuf_payload(payload_message.SerializeAsString());
  427. string effective_test_name = test_name + ".JsonOutput";
  428. request.set_requested_output_format(conformance::JSON);
  429. RunTest(effective_test_name, request, &response);
  430. if (response.result_case() == ConformanceResponse::kSerializeError) {
  431. ReportSuccess(effective_test_name);
  432. } else if (response.result_case() == ConformanceResponse::kSkipped) {
  433. ReportSkip(effective_test_name, request, response);
  434. } else {
  435. ReportFailure(effective_test_name, request, response,
  436. "Should have failed to serialize, but didn't.");
  437. }
  438. }
  439. void ConformanceTestSuite::TestPrematureEOFForType(FieldDescriptor::Type type) {
  440. // Incomplete values for each wire type.
  441. static const string incompletes[6] = {
  442. string("\x80"), // VARINT
  443. string("abcdefg"), // 64BIT
  444. string("\x80"), // DELIMITED (partial length)
  445. string(), // START_GROUP (no value required)
  446. string(), // END_GROUP (no value required)
  447. string("abc") // 32BIT
  448. };
  449. uint32_t fieldnum = GetFieldNumberForType(type, false);
  450. uint32_t rep_fieldnum = GetFieldNumberForType(type, true);
  451. WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
  452. static_cast<WireFormatLite::FieldType>(type));
  453. const string& incomplete = incompletes[wire_type];
  454. const string type_name =
  455. UpperCase(string(".") + FieldDescriptor::TypeName(type));
  456. ExpectParseFailureForProto(
  457. tag(fieldnum, wire_type),
  458. "PrematureEofBeforeKnownNonRepeatedValue" + type_name);
  459. ExpectParseFailureForProto(
  460. tag(rep_fieldnum, wire_type),
  461. "PrematureEofBeforeKnownRepeatedValue" + type_name);
  462. ExpectParseFailureForProto(
  463. tag(UNKNOWN_FIELD, wire_type),
  464. "PrematureEofBeforeUnknownValue" + type_name);
  465. ExpectParseFailureForProto(
  466. cat( tag(fieldnum, wire_type), incomplete ),
  467. "PrematureEofInsideKnownNonRepeatedValue" + type_name);
  468. ExpectParseFailureForProto(
  469. cat( tag(rep_fieldnum, wire_type), incomplete ),
  470. "PrematureEofInsideKnownRepeatedValue" + type_name);
  471. ExpectParseFailureForProto(
  472. cat( tag(UNKNOWN_FIELD, wire_type), incomplete ),
  473. "PrematureEofInsideUnknownValue" + type_name);
  474. if (wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
  475. ExpectParseFailureForProto(
  476. cat( tag(fieldnum, wire_type), varint(1) ),
  477. "PrematureEofInDelimitedDataForKnownNonRepeatedValue" + type_name);
  478. ExpectParseFailureForProto(
  479. cat( tag(rep_fieldnum, wire_type), varint(1) ),
  480. "PrematureEofInDelimitedDataForKnownRepeatedValue" + type_name);
  481. // EOF in the middle of delimited data for unknown value.
  482. ExpectParseFailureForProto(
  483. cat( tag(UNKNOWN_FIELD, wire_type), varint(1) ),
  484. "PrematureEofInDelimitedDataForUnknownValue" + type_name);
  485. if (type == FieldDescriptor::TYPE_MESSAGE) {
  486. // Submessage ends in the middle of a value.
  487. string incomplete_submsg =
  488. cat( tag(WireFormatLite::TYPE_INT32, WireFormatLite::WIRETYPE_VARINT),
  489. incompletes[WireFormatLite::WIRETYPE_VARINT] );
  490. ExpectHardParseFailureForProto(
  491. cat( tag(fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
  492. varint(incomplete_submsg.size()),
  493. incomplete_submsg ),
  494. "PrematureEofInSubmessageValue" + type_name);
  495. }
  496. } else if (type != FieldDescriptor::TYPE_GROUP) {
  497. // Non-delimited, non-group: eligible for packing.
  498. // Packed region ends in the middle of a value.
  499. ExpectHardParseFailureForProto(
  500. cat( tag(rep_fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
  501. varint(incomplete.size()),
  502. incomplete ),
  503. "PrematureEofInPackedFieldValue" + type_name);
  504. // EOF in the middle of packed region.
  505. ExpectParseFailureForProto(
  506. cat( tag(rep_fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
  507. varint(1) ),
  508. "PrematureEofInPackedField" + type_name);
  509. }
  510. }
  511. void ConformanceTestSuite::SetFailureList(const string& filename,
  512. const vector<string>& failure_list) {
  513. failure_list_filename_ = filename;
  514. expected_to_fail_.clear();
  515. std::copy(failure_list.begin(), failure_list.end(),
  516. std::inserter(expected_to_fail_, expected_to_fail_.end()));
  517. }
  518. bool ConformanceTestSuite::CheckSetEmpty(const set<string>& set_to_check,
  519. const std::string& write_to_file,
  520. const std::string& msg) {
  521. if (set_to_check.empty()) {
  522. return true;
  523. } else {
  524. StringAppendF(&output_, "\n");
  525. StringAppendF(&output_, "%s\n\n", msg.c_str());
  526. for (set<string>::const_iterator iter = set_to_check.begin();
  527. iter != set_to_check.end(); ++iter) {
  528. StringAppendF(&output_, " %s\n", iter->c_str());
  529. }
  530. StringAppendF(&output_, "\n");
  531. if (!write_to_file.empty()) {
  532. std::ofstream os(write_to_file);
  533. if (os) {
  534. for (set<string>::const_iterator iter = set_to_check.begin();
  535. iter != set_to_check.end(); ++iter) {
  536. os << *iter << "\n";
  537. }
  538. } else {
  539. StringAppendF(&output_, "Failed to open file: %s\n",
  540. write_to_file.c_str());
  541. }
  542. }
  543. return false;
  544. }
  545. }
  546. bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner,
  547. std::string* output) {
  548. runner_ = runner;
  549. successes_ = 0;
  550. expected_failures_ = 0;
  551. skipped_.clear();
  552. test_names_.clear();
  553. unexpected_failing_tests_.clear();
  554. unexpected_succeeding_tests_.clear();
  555. type_resolver_.reset(NewTypeResolverForDescriptorPool(
  556. kTypeUrlPrefix, DescriptorPool::generated_pool()));
  557. type_url_ = GetTypeUrl(TestAllTypes::descriptor());
  558. output_ = "\nCONFORMANCE TEST BEGIN ====================================\n\n";
  559. for (int i = 1; i <= FieldDescriptor::MAX_TYPE; i++) {
  560. if (i == FieldDescriptor::TYPE_GROUP) continue;
  561. TestPrematureEOFForType(static_cast<FieldDescriptor::Type>(i));
  562. }
  563. RunValidJsonTest("HelloWorld", "{\"optionalString\":\"Hello, World!\"}",
  564. "optional_string: 'Hello, World!'");
  565. // NOTE: The spec for JSON support is still being sorted out, these may not
  566. // all be correct.
  567. // Test field name conventions.
  568. RunValidJsonTest(
  569. "FieldNameInSnakeCase",
  570. R"({
  571. "fieldname1": 1,
  572. "fieldName2": 2,
  573. "fieldName3": 3,
  574. "fieldName4": 4
  575. })",
  576. R"(
  577. fieldname1: 1
  578. field_name2: 2
  579. _field_name3: 3
  580. field__name4_: 4
  581. )");
  582. RunValidJsonTest(
  583. "FieldNameWithNumbers",
  584. R"({
  585. "field0name5": 5,
  586. "field0Name6": 6
  587. })",
  588. R"(
  589. field0name5: 5
  590. field_0_name6: 6
  591. )");
  592. RunValidJsonTest(
  593. "FieldNameWithMixedCases",
  594. R"({
  595. "fieldName7": 7,
  596. "fieldName8": 8,
  597. "fieldName9": 9,
  598. "fieldName10": 10,
  599. "fIELDNAME11": 11,
  600. "fIELDName12": 12
  601. })",
  602. R"(
  603. fieldName7: 7
  604. FieldName8: 8
  605. field_Name9: 9
  606. Field_Name10: 10
  607. FIELD_NAME11: 11
  608. FIELD_name12: 12
  609. )");
  610. RunValidJsonTest(
  611. "FieldNameWithDoubleUnderscores",
  612. R"({
  613. "fieldName13": 13,
  614. "fieldName14": 14,
  615. "fieldName15": 15,
  616. "fieldName16": 16,
  617. "fieldName17": 17,
  618. "fieldName18": 18
  619. })",
  620. R"(
  621. __field_name13: 13
  622. __Field_name14: 14
  623. field__name15: 15
  624. field__Name16: 16
  625. field_name17__: 17
  626. Field_name18__: 18
  627. )");
  628. // Using the original proto field name in JSON is also allowed.
  629. RunValidJsonTest(
  630. "OriginalProtoFieldName",
  631. R"({
  632. "fieldname1": 1,
  633. "field_name2": 2,
  634. "_field_name3": 3,
  635. "field__name4_": 4,
  636. "field0name5": 5,
  637. "field_0_name6": 6,
  638. "fieldName7": 7,
  639. "FieldName8": 8,
  640. "field_Name9": 9,
  641. "Field_Name10": 10,
  642. "FIELD_NAME11": 11,
  643. "FIELD_name12": 12,
  644. "__field_name13": 13,
  645. "__Field_name14": 14,
  646. "field__name15": 15,
  647. "field__Name16": 16,
  648. "field_name17__": 17,
  649. "Field_name18__": 18
  650. })",
  651. R"(
  652. fieldname1: 1
  653. field_name2: 2
  654. _field_name3: 3
  655. field__name4_: 4
  656. field0name5: 5
  657. field_0_name6: 6
  658. fieldName7: 7
  659. FieldName8: 8
  660. field_Name9: 9
  661. Field_Name10: 10
  662. FIELD_NAME11: 11
  663. FIELD_name12: 12
  664. __field_name13: 13
  665. __Field_name14: 14
  666. field__name15: 15
  667. field__Name16: 16
  668. field_name17__: 17
  669. Field_name18__: 18
  670. )");
  671. // Field names can be escaped.
  672. RunValidJsonTest(
  673. "FieldNameEscaped",
  674. R"({"fieldn\u0061me1": 1})",
  675. "fieldname1: 1");
  676. // String ends with escape character.
  677. ExpectParseFailureForJson(
  678. "StringEndsWithEscapeChar",
  679. "{\"optionalString\": \"abc\\");
  680. // Field names must be quoted (or it's not valid JSON).
  681. ExpectParseFailureForJson(
  682. "FieldNameNotQuoted",
  683. "{fieldname1: 1}");
  684. // Trailing comma is not allowed (not valid JSON).
  685. ExpectParseFailureForJson(
  686. "TrailingCommaInAnObject",
  687. R"({"fieldname1":1,})");
  688. ExpectParseFailureForJson(
  689. "TrailingCommaInAnObjectWithSpace",
  690. R"({"fieldname1":1 ,})");
  691. ExpectParseFailureForJson(
  692. "TrailingCommaInAnObjectWithSpaceCommaSpace",
  693. R"({"fieldname1":1 , })");
  694. ExpectParseFailureForJson(
  695. "TrailingCommaInAnObjectWithNewlines",
  696. R"({
  697. "fieldname1":1,
  698. })");
  699. // JSON doesn't support comments.
  700. ExpectParseFailureForJson(
  701. "JsonWithComments",
  702. R"({
  703. // This is a comment.
  704. "fieldname1": 1
  705. })");
  706. // JSON spec says whitespace doesn't matter, so try a few spacings to be sure.
  707. RunValidJsonTest(
  708. "OneLineNoSpaces",
  709. "{\"optionalInt32\":1,\"optionalInt64\":2}",
  710. R"(
  711. optional_int32: 1
  712. optional_int64: 2
  713. )");
  714. RunValidJsonTest(
  715. "OneLineWithSpaces",
  716. "{ \"optionalInt32\" : 1 , \"optionalInt64\" : 2 }",
  717. R"(
  718. optional_int32: 1
  719. optional_int64: 2
  720. )");
  721. RunValidJsonTest(
  722. "MultilineNoSpaces",
  723. "{\n\"optionalInt32\"\n:\n1\n,\n\"optionalInt64\"\n:\n2\n}",
  724. R"(
  725. optional_int32: 1
  726. optional_int64: 2
  727. )");
  728. RunValidJsonTest(
  729. "MultilineWithSpaces",
  730. "{\n \"optionalInt32\" : 1\n ,\n \"optionalInt64\" : 2\n}\n",
  731. R"(
  732. optional_int32: 1
  733. optional_int64: 2
  734. )");
  735. // Missing comma between key/value pairs.
  736. ExpectParseFailureForJson(
  737. "MissingCommaOneLine",
  738. "{ \"optionalInt32\": 1 \"optionalInt64\": 2 }");
  739. ExpectParseFailureForJson(
  740. "MissingCommaMultiline",
  741. "{\n \"optionalInt32\": 1\n \"optionalInt64\": 2\n}");
  742. // Duplicated field names are not allowed.
  743. ExpectParseFailureForJson(
  744. "FieldNameDuplicate",
  745. R"({
  746. "optionalNestedMessage": {a: 1},
  747. "optionalNestedMessage": {}
  748. })");
  749. ExpectParseFailureForJson(
  750. "FieldNameDuplicateDifferentCasing1",
  751. R"({
  752. "optional_nested_message": {a: 1},
  753. "optionalNestedMessage": {}
  754. })");
  755. ExpectParseFailureForJson(
  756. "FieldNameDuplicateDifferentCasing2",
  757. R"({
  758. "optionalNestedMessage": {a: 1},
  759. "optional_nested_message": {}
  760. })");
  761. // NOTE: The spec for JSON support is still being sorted out, these may not
  762. // all be correct.
  763. // Serializers should use lowerCamelCase by default.
  764. RunValidJsonTestWithValidator(
  765. "FieldNameInLowerCamelCase",
  766. R"({
  767. "fieldname1": 1,
  768. "fieldName2": 2,
  769. "fieldName3": 3,
  770. "fieldName4": 4
  771. })",
  772. [](const Json::Value& value) {
  773. return value.isMember("fieldname1") &&
  774. value.isMember("fieldName2") &&
  775. value.isMember("fieldName3") &&
  776. value.isMember("fieldName4");
  777. });
  778. RunValidJsonTestWithValidator(
  779. "FieldNameWithNumbers",
  780. R"({
  781. "field0name5": 5,
  782. "field0Name6": 6
  783. })",
  784. [](const Json::Value& value) {
  785. return value.isMember("field0name5") &&
  786. value.isMember("field0Name6");
  787. });
  788. RunValidJsonTestWithValidator(
  789. "FieldNameWithMixedCases",
  790. R"({
  791. "fieldName7": 7,
  792. "fieldName8": 8,
  793. "fieldName9": 9,
  794. "fieldName10": 10,
  795. "fIELDNAME11": 11,
  796. "fIELDName12": 12
  797. })",
  798. [](const Json::Value& value) {
  799. return value.isMember("fieldName7") &&
  800. value.isMember("fieldName8") &&
  801. value.isMember("fieldName9") &&
  802. value.isMember("fieldName10") &&
  803. value.isMember("fIELDNAME11") &&
  804. value.isMember("fIELDName12");
  805. });
  806. RunValidJsonTestWithValidator(
  807. "FieldNameWithDoubleUnderscores",
  808. R"({
  809. "fieldName13": 13,
  810. "fieldName14": 14,
  811. "fieldName15": 15,
  812. "fieldName16": 16,
  813. "fieldName17": 17,
  814. "fieldName18": 18
  815. })",
  816. [](const Json::Value& value) {
  817. return value.isMember("fieldName13") &&
  818. value.isMember("fieldName14") &&
  819. value.isMember("fieldName15") &&
  820. value.isMember("fieldName16") &&
  821. value.isMember("fieldName17") &&
  822. value.isMember("fieldName18");
  823. });
  824. // Integer fields.
  825. RunValidJsonTest(
  826. "Int32FieldMaxValue",
  827. R"({"optionalInt32": 2147483647})",
  828. "optional_int32: 2147483647");
  829. RunValidJsonTest(
  830. "Int32FieldMinValue",
  831. R"({"optionalInt32": -2147483648})",
  832. "optional_int32: -2147483648");
  833. RunValidJsonTest(
  834. "Uint32FieldMaxValue",
  835. R"({"optionalUint32": 4294967295})",
  836. "optional_uint32: 4294967295");
  837. RunValidJsonTest(
  838. "Int64FieldMaxValue",
  839. R"({"optionalInt64": "9223372036854775807"})",
  840. "optional_int64: 9223372036854775807");
  841. RunValidJsonTest(
  842. "Int64FieldMinValue",
  843. R"({"optionalInt64": "-9223372036854775808"})",
  844. "optional_int64: -9223372036854775808");
  845. RunValidJsonTest(
  846. "Uint64FieldMaxValue",
  847. R"({"optionalUint64": "18446744073709551615"})",
  848. "optional_uint64: 18446744073709551615");
  849. // While not the largest Int64, this is the largest
  850. // Int64 which can be exactly represented within an
  851. // IEEE-754 64-bit float, which is the expected level
  852. // of interoperability guarantee. Larger values may
  853. // work in some implementations, but should not be
  854. // relied upon.
  855. RunValidJsonTest(
  856. "Int64FieldMaxValueNotQuoted",
  857. R"({"optionalInt64": 9223372036854774784})",
  858. "optional_int64: 9223372036854774784");
  859. RunValidJsonTest(
  860. "Int64FieldMinValueNotQuoted",
  861. R"({"optionalInt64": -9223372036854775808})",
  862. "optional_int64: -9223372036854775808");
  863. // Largest interoperable Uint64; see comment above
  864. // for Int64FieldMaxValueNotQuoted.
  865. RunValidJsonTest(
  866. "Uint64FieldMaxValueNotQuoted",
  867. R"({"optionalUint64": 18446744073709549568})",
  868. "optional_uint64: 18446744073709549568");
  869. // Values can be represented as JSON strings.
  870. RunValidJsonTest(
  871. "Int32FieldStringValue",
  872. R"({"optionalInt32": "2147483647"})",
  873. "optional_int32: 2147483647");
  874. RunValidJsonTest(
  875. "Int32FieldStringValueEscaped",
  876. R"({"optionalInt32": "2\u003147483647"})",
  877. "optional_int32: 2147483647");
  878. // Parsers reject out-of-bound integer values.
  879. ExpectParseFailureForJson(
  880. "Int32FieldTooLarge",
  881. R"({"optionalInt32": 2147483648})");
  882. ExpectParseFailureForJson(
  883. "Int32FieldTooSmall",
  884. R"({"optionalInt32": -2147483649})");
  885. ExpectParseFailureForJson(
  886. "Uint32FieldTooLarge",
  887. R"({"optionalUint32": 4294967296})");
  888. ExpectParseFailureForJson(
  889. "Int64FieldTooLarge",
  890. R"({"optionalInt64": "9223372036854775808"})");
  891. ExpectParseFailureForJson(
  892. "Int64FieldTooSmall",
  893. R"({"optionalInt64": "-9223372036854775809"})");
  894. ExpectParseFailureForJson(
  895. "Uint64FieldTooLarge",
  896. R"({"optionalUint64": "18446744073709551616"})");
  897. // Parser reject non-integer numeric values as well.
  898. ExpectParseFailureForJson(
  899. "Int32FieldNotInteger",
  900. R"({"optionalInt32": 0.5})");
  901. ExpectParseFailureForJson(
  902. "Uint32FieldNotInteger",
  903. R"({"optionalUint32": 0.5})");
  904. ExpectParseFailureForJson(
  905. "Int64FieldNotInteger",
  906. R"({"optionalInt64": "0.5"})");
  907. ExpectParseFailureForJson(
  908. "Uint64FieldNotInteger",
  909. R"({"optionalUint64": "0.5"})");
  910. // Integers but represented as float values are accepted.
  911. RunValidJsonTest(
  912. "Int32FieldFloatTrailingZero",
  913. R"({"optionalInt32": 100000.000})",
  914. "optional_int32: 100000");
  915. RunValidJsonTest(
  916. "Int32FieldExponentialFormat",
  917. R"({"optionalInt32": 1e5})",
  918. "optional_int32: 100000");
  919. RunValidJsonTest(
  920. "Int32FieldMaxFloatValue",
  921. R"({"optionalInt32": 2.147483647e9})",
  922. "optional_int32: 2147483647");
  923. RunValidJsonTest(
  924. "Int32FieldMinFloatValue",
  925. R"({"optionalInt32": -2.147483648e9})",
  926. "optional_int32: -2147483648");
  927. RunValidJsonTest(
  928. "Uint32FieldMaxFloatValue",
  929. R"({"optionalUint32": 4.294967295e9})",
  930. "optional_uint32: 4294967295");
  931. // Parser reject non-numeric values.
  932. ExpectParseFailureForJson(
  933. "Int32FieldNotNumber",
  934. R"({"optionalInt32": "3x3"})");
  935. ExpectParseFailureForJson(
  936. "Uint32FieldNotNumber",
  937. R"({"optionalUint32": "3x3"})");
  938. ExpectParseFailureForJson(
  939. "Int64FieldNotNumber",
  940. R"({"optionalInt64": "3x3"})");
  941. ExpectParseFailureForJson(
  942. "Uint64FieldNotNumber",
  943. R"({"optionalUint64": "3x3"})");
  944. // JSON does not allow "+" on numric values.
  945. ExpectParseFailureForJson(
  946. "Int32FieldPlusSign",
  947. R"({"optionalInt32": +1})");
  948. // JSON doesn't allow leading 0s.
  949. ExpectParseFailureForJson(
  950. "Int32FieldLeadingZero",
  951. R"({"optionalInt32": 01})");
  952. ExpectParseFailureForJson(
  953. "Int32FieldNegativeWithLeadingZero",
  954. R"({"optionalInt32": -01})");
  955. // String values must follow the same syntax rule. Specifically leading
  956. // or traling spaces are not allowed.
  957. ExpectParseFailureForJson(
  958. "Int32FieldLeadingSpace",
  959. R"({"optionalInt32": " 1"})");
  960. ExpectParseFailureForJson(
  961. "Int32FieldTrailingSpace",
  962. R"({"optionalInt32": "1 "})");
  963. // 64-bit values are serialized as strings.
  964. RunValidJsonTestWithValidator(
  965. "Int64FieldBeString",
  966. R"({"optionalInt64": 1})",
  967. [](const Json::Value& value) {
  968. return value["optionalInt64"].type() == Json::stringValue &&
  969. value["optionalInt64"].asString() == "1";
  970. });
  971. RunValidJsonTestWithValidator(
  972. "Uint64FieldBeString",
  973. R"({"optionalUint64": 1})",
  974. [](const Json::Value& value) {
  975. return value["optionalUint64"].type() == Json::stringValue &&
  976. value["optionalUint64"].asString() == "1";
  977. });
  978. // Bool fields.
  979. RunValidJsonTest(
  980. "BoolFieldTrue",
  981. R"({"optionalBool":true})",
  982. "optional_bool: true");
  983. RunValidJsonTest(
  984. "BoolFieldFalse",
  985. R"({"optionalBool":false})",
  986. "optional_bool: false");
  987. // Other forms are not allowed.
  988. ExpectParseFailureForJson(
  989. "BoolFieldIntegerZero",
  990. R"({"optionalBool":0})");
  991. ExpectParseFailureForJson(
  992. "BoolFieldIntegerOne",
  993. R"({"optionalBool":1})");
  994. ExpectParseFailureForJson(
  995. "BoolFieldCamelCaseTrue",
  996. R"({"optionalBool":True})");
  997. ExpectParseFailureForJson(
  998. "BoolFieldCamelCaseFalse",
  999. R"({"optionalBool":False})");
  1000. ExpectParseFailureForJson(
  1001. "BoolFieldAllCapitalTrue",
  1002. R"({"optionalBool":TRUE})");
  1003. ExpectParseFailureForJson(
  1004. "BoolFieldAllCapitalFalse",
  1005. R"({"optionalBool":FALSE})");
  1006. ExpectParseFailureForJson(
  1007. "BoolFieldDoubleQuotedTrue",
  1008. R"({"optionalBool":"true"})");
  1009. ExpectParseFailureForJson(
  1010. "BoolFieldDoubleQuotedFalse",
  1011. R"({"optionalBool":"false"})");
  1012. // Float fields.
  1013. RunValidJsonTest(
  1014. "FloatFieldMinPositiveValue",
  1015. R"({"optionalFloat": 1.175494e-38})",
  1016. "optional_float: 1.175494e-38");
  1017. RunValidJsonTest(
  1018. "FloatFieldMaxNegativeValue",
  1019. R"({"optionalFloat": -1.175494e-38})",
  1020. "optional_float: -1.175494e-38");
  1021. RunValidJsonTest(
  1022. "FloatFieldMaxPositiveValue",
  1023. R"({"optionalFloat": 3.402823e+38})",
  1024. "optional_float: 3.402823e+38");
  1025. RunValidJsonTest(
  1026. "FloatFieldMinNegativeValue",
  1027. R"({"optionalFloat": 3.402823e+38})",
  1028. "optional_float: 3.402823e+38");
  1029. // Values can be quoted.
  1030. RunValidJsonTest(
  1031. "FloatFieldQuotedValue",
  1032. R"({"optionalFloat": "1"})",
  1033. "optional_float: 1");
  1034. // Special values.
  1035. RunValidJsonTest(
  1036. "FloatFieldNan",
  1037. R"({"optionalFloat": "NaN"})",
  1038. "optional_float: nan");
  1039. RunValidJsonTest(
  1040. "FloatFieldInfinity",
  1041. R"({"optionalFloat": "Infinity"})",
  1042. "optional_float: inf");
  1043. RunValidJsonTest(
  1044. "FloatFieldNegativeInfinity",
  1045. R"({"optionalFloat": "-Infinity"})",
  1046. "optional_float: -inf");
  1047. // Non-cannonical Nan will be correctly normalized.
  1048. {
  1049. TestAllTypes message;
  1050. // IEEE floating-point standard 32-bit quiet NaN:
  1051. // 0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  1052. message.set_optional_float(
  1053. WireFormatLite::DecodeFloat(0x7FA12345));
  1054. RunValidJsonTestWithProtobufInput(
  1055. "FloatFieldNormalizeQuietNan", message,
  1056. "optional_float: nan");
  1057. // IEEE floating-point standard 64-bit signaling NaN:
  1058. // 1111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  1059. message.set_optional_float(
  1060. WireFormatLite::DecodeFloat(0xFFB54321));
  1061. RunValidJsonTestWithProtobufInput(
  1062. "FloatFieldNormalizeSignalingNan", message,
  1063. "optional_float: nan");
  1064. }
  1065. // Special values must be quoted.
  1066. ExpectParseFailureForJson(
  1067. "FloatFieldNanNotQuoted",
  1068. R"({"optionalFloat": NaN})");
  1069. ExpectParseFailureForJson(
  1070. "FloatFieldInfinityNotQuoted",
  1071. R"({"optionalFloat": Infinity})");
  1072. ExpectParseFailureForJson(
  1073. "FloatFieldNegativeInfinityNotQuoted",
  1074. R"({"optionalFloat": -Infinity})");
  1075. // Parsers should reject out-of-bound values.
  1076. ExpectParseFailureForJson(
  1077. "FloatFieldTooSmall",
  1078. R"({"optionalFloat": -3.502823e+38})");
  1079. ExpectParseFailureForJson(
  1080. "FloatFieldTooLarge",
  1081. R"({"optionalFloat": 3.502823e+38})");
  1082. // Double fields.
  1083. RunValidJsonTest(
  1084. "DoubleFieldMinPositiveValue",
  1085. R"({"optionalDouble": 2.22507e-308})",
  1086. "optional_double: 2.22507e-308");
  1087. RunValidJsonTest(
  1088. "DoubleFieldMaxNegativeValue",
  1089. R"({"optionalDouble": -2.22507e-308})",
  1090. "optional_double: -2.22507e-308");
  1091. RunValidJsonTest(
  1092. "DoubleFieldMaxPositiveValue",
  1093. R"({"optionalDouble": 1.79769e+308})",
  1094. "optional_double: 1.79769e+308");
  1095. RunValidJsonTest(
  1096. "DoubleFieldMinNegativeValue",
  1097. R"({"optionalDouble": -1.79769e+308})",
  1098. "optional_double: -1.79769e+308");
  1099. // Values can be quoted.
  1100. RunValidJsonTest(
  1101. "DoubleFieldQuotedValue",
  1102. R"({"optionalDouble": "1"})",
  1103. "optional_double: 1");
  1104. // Speical values.
  1105. RunValidJsonTest(
  1106. "DoubleFieldNan",
  1107. R"({"optionalDouble": "NaN"})",
  1108. "optional_double: nan");
  1109. RunValidJsonTest(
  1110. "DoubleFieldInfinity",
  1111. R"({"optionalDouble": "Infinity"})",
  1112. "optional_double: inf");
  1113. RunValidJsonTest(
  1114. "DoubleFieldNegativeInfinity",
  1115. R"({"optionalDouble": "-Infinity"})",
  1116. "optional_double: -inf");
  1117. // Non-cannonical Nan will be correctly normalized.
  1118. {
  1119. TestAllTypes message;
  1120. message.set_optional_double(
  1121. WireFormatLite::DecodeDouble(0x7FFA123456789ABCLL));
  1122. RunValidJsonTestWithProtobufInput(
  1123. "DoubleFieldNormalizeQuietNan", message,
  1124. "optional_double: nan");
  1125. message.set_optional_double(
  1126. WireFormatLite::DecodeDouble(0xFFFBCBA987654321LL));
  1127. RunValidJsonTestWithProtobufInput(
  1128. "DoubleFieldNormalizeSignalingNan", message,
  1129. "optional_double: nan");
  1130. }
  1131. // Special values must be quoted.
  1132. ExpectParseFailureForJson(
  1133. "DoubleFieldNanNotQuoted",
  1134. R"({"optionalDouble": NaN})");
  1135. ExpectParseFailureForJson(
  1136. "DoubleFieldInfinityNotQuoted",
  1137. R"({"optionalDouble": Infinity})");
  1138. ExpectParseFailureForJson(
  1139. "DoubleFieldNegativeInfinityNotQuoted",
  1140. R"({"optionalDouble": -Infinity})");
  1141. // Parsers should reject out-of-bound values.
  1142. ExpectParseFailureForJson(
  1143. "DoubleFieldTooSmall",
  1144. R"({"optionalDouble": -1.89769e+308})");
  1145. ExpectParseFailureForJson(
  1146. "DoubleFieldTooLarge",
  1147. R"({"optionalDouble": +1.89769e+308})");
  1148. // Enum fields.
  1149. RunValidJsonTest(
  1150. "EnumField",
  1151. R"({"optionalNestedEnum": "FOO"})",
  1152. "optional_nested_enum: FOO");
  1153. // Enum values must be represented as strings.
  1154. ExpectParseFailureForJson(
  1155. "EnumFieldNotQuoted",
  1156. R"({"optionalNestedEnum": FOO})");
  1157. // Numeric values are allowed.
  1158. RunValidJsonTest(
  1159. "EnumFieldNumericValueZero",
  1160. R"({"optionalNestedEnum": 0})",
  1161. "optional_nested_enum: FOO");
  1162. RunValidJsonTest(
  1163. "EnumFieldNumericValueNonZero",
  1164. R"({"optionalNestedEnum": 1})",
  1165. "optional_nested_enum: BAR");
  1166. // Unknown enum values are represented as numeric values.
  1167. RunValidJsonTestWithValidator(
  1168. "EnumFieldUnknownValue",
  1169. R"({"optionalNestedEnum": 123})",
  1170. [](const Json::Value& value) {
  1171. return value["optionalNestedEnum"].type() == Json::intValue &&
  1172. value["optionalNestedEnum"].asInt() == 123;
  1173. });
  1174. // String fields.
  1175. RunValidJsonTest(
  1176. "StringField",
  1177. R"({"optionalString": "Hello world!"})",
  1178. "optional_string: \"Hello world!\"");
  1179. RunValidJsonTest(
  1180. "StringFieldUnicode",
  1181. // Google in Chinese.
  1182. R"({"optionalString": "谷歌"})",
  1183. R"(optional_string: "谷歌")");
  1184. RunValidJsonTest(
  1185. "StringFieldEscape",
  1186. R"({"optionalString": "\"\\\/\b\f\n\r\t"})",
  1187. R"(optional_string: "\"\\/\b\f\n\r\t")");
  1188. RunValidJsonTest(
  1189. "StringFieldUnicodeEscape",
  1190. R"({"optionalString": "\u8C37\u6B4C"})",
  1191. R"(optional_string: "谷歌")");
  1192. RunValidJsonTest(
  1193. "StringFieldUnicodeEscapeWithLowercaseHexLetters",
  1194. R"({"optionalString": "\u8c37\u6b4c"})",
  1195. R"(optional_string: "谷歌")");
  1196. RunValidJsonTest(
  1197. "StringFieldSurrogatePair",
  1198. // The character is an emoji: grinning face with smiling eyes. 😁
  1199. R"({"optionalString": "\uD83D\uDE01"})",
  1200. R"(optional_string: "\xF0\x9F\x98\x81")");
  1201. // Unicode escapes must start with "\u" (lowercase u).
  1202. ExpectParseFailureForJson(
  1203. "StringFieldUppercaseEscapeLetter",
  1204. R"({"optionalString": "\U8C37\U6b4C"})");
  1205. ExpectParseFailureForJson(
  1206. "StringFieldInvalidEscape",
  1207. R"({"optionalString": "\uXXXX\u6B4C"})");
  1208. ExpectParseFailureForJson(
  1209. "StringFieldUnterminatedEscape",
  1210. R"({"optionalString": "\u8C3"})");
  1211. ExpectParseFailureForJson(
  1212. "StringFieldUnpairedHighSurrogate",
  1213. R"({"optionalString": "\uD800"})");
  1214. ExpectParseFailureForJson(
  1215. "StringFieldUnpairedLowSurrogate",
  1216. R"({"optionalString": "\uDC00"})");
  1217. ExpectParseFailureForJson(
  1218. "StringFieldSurrogateInWrongOrder",
  1219. R"({"optionalString": "\uDE01\uD83D"})");
  1220. ExpectParseFailureForJson(
  1221. "StringFieldNotAString",
  1222. R"({"optionalString": 12345})");
  1223. // Bytes fields.
  1224. RunValidJsonTest(
  1225. "BytesField",
  1226. R"({"optionalBytes": "AQI="})",
  1227. R"(optional_bytes: "\x01\x02")");
  1228. ExpectParseFailureForJson(
  1229. "BytesFieldNoPadding",
  1230. R"({"optionalBytes": "AQI"})");
  1231. ExpectParseFailureForJson(
  1232. "BytesFieldInvalidBase64Characters",
  1233. R"({"optionalBytes": "-_=="})");
  1234. // Message fields.
  1235. RunValidJsonTest(
  1236. "MessageField",
  1237. R"({"optionalNestedMessage": {"a": 1234}})",
  1238. "optional_nested_message: {a: 1234}");
  1239. // Oneof fields.
  1240. ExpectParseFailureForJson(
  1241. "OneofFieldDuplicate",
  1242. R"({"oneofUint32": 1, "oneofString": "test"})");
  1243. // Ensure zero values for oneof make it out/backs.
  1244. {
  1245. TestAllTypes message;
  1246. message.set_oneof_uint32(0);
  1247. RunValidProtobufTest(
  1248. "OneofZeroUint32", message, "oneof_uint32: 0");
  1249. message.mutable_oneof_nested_message()->set_a(0);
  1250. RunValidProtobufTest(
  1251. "OneofZeroMessage", message, "oneof_nested_message: {}");
  1252. message.set_oneof_string("");
  1253. RunValidProtobufTest(
  1254. "OneofZeroString", message, "oneof_string: \"\"");
  1255. message.set_oneof_bytes("");
  1256. RunValidProtobufTest(
  1257. "OneofZeroBytes", message, "oneof_bytes: \"\"");
  1258. message.set_oneof_bool(false);
  1259. RunValidProtobufTest(
  1260. "OneofZeroBool", message, "oneof_bool: false");
  1261. message.set_oneof_uint64(0);
  1262. RunValidProtobufTest(
  1263. "OneofZeroUint64", message, "oneof_uint64: 0");
  1264. message.set_oneof_float(0.0f);
  1265. RunValidProtobufTest(
  1266. "OneofZeroFloat", message, "oneof_float: 0");
  1267. message.set_oneof_double(0.0);
  1268. RunValidProtobufTest(
  1269. "OneofZeroDouble", message, "oneof_double: 0");
  1270. message.set_oneof_enum(TestAllTypes::FOO);
  1271. RunValidProtobufTest(
  1272. "OneofZeroEnum", message, "oneof_enum: FOO");
  1273. }
  1274. RunValidJsonTest(
  1275. "OneofZeroUint32",
  1276. R"({"oneofUint32": 0})", "oneof_uint32: 0");
  1277. RunValidJsonTest(
  1278. "OneofZeroMessage",
  1279. R"({"oneofNestedMessage": {}})", "oneof_nested_message: {}");
  1280. RunValidJsonTest(
  1281. "OneofZeroString",
  1282. R"({"oneofString": ""})", "oneof_string: \"\"");
  1283. RunValidJsonTest(
  1284. "OneofZeroBytes",
  1285. R"({"oneofBytes": ""})", "oneof_bytes: \"\"");
  1286. RunValidJsonTest(
  1287. "OneofZeroBool",
  1288. R"({"oneofBool": false})", "oneof_bool: false");
  1289. RunValidJsonTest(
  1290. "OneofZeroUint64",
  1291. R"({"oneofUint64": 0})", "oneof_uint64: 0");
  1292. RunValidJsonTest(
  1293. "OneofZeroFloat",
  1294. R"({"oneofFloat": 0.0})", "oneof_float: 0");
  1295. RunValidJsonTest(
  1296. "OneofZeroDouble",
  1297. R"({"oneofDouble": 0.0})", "oneof_double: 0");
  1298. RunValidJsonTest(
  1299. "OneofZeroEnum",
  1300. R"({"oneofEnum":"FOO"})", "oneof_enum: FOO");
  1301. // Repeated fields.
  1302. RunValidJsonTest(
  1303. "PrimitiveRepeatedField",
  1304. R"({"repeatedInt32": [1, 2, 3, 4]})",
  1305. "repeated_int32: [1, 2, 3, 4]");
  1306. RunValidJsonTest(
  1307. "EnumRepeatedField",
  1308. R"({"repeatedNestedEnum": ["FOO", "BAR", "BAZ"]})",
  1309. "repeated_nested_enum: [FOO, BAR, BAZ]");
  1310. RunValidJsonTest(
  1311. "StringRepeatedField",
  1312. R"({"repeatedString": ["Hello", "world"]})",
  1313. R"(repeated_string: ["Hello", "world"])");
  1314. RunValidJsonTest(
  1315. "BytesRepeatedField",
  1316. R"({"repeatedBytes": ["AAEC", "AQI="]})",
  1317. R"(repeated_bytes: ["\x00\x01\x02", "\x01\x02"])");
  1318. RunValidJsonTest(
  1319. "MessageRepeatedField",
  1320. R"({"repeatedNestedMessage": [{"a": 1234}, {"a": 5678}]})",
  1321. "repeated_nested_message: {a: 1234}"
  1322. "repeated_nested_message: {a: 5678}");
  1323. // Repeated field elements are of incorrect type.
  1324. ExpectParseFailureForJson(
  1325. "RepeatedFieldWrongElementTypeExpectingIntegersGotBool",
  1326. R"({"repeatedInt32": [1, false, 3, 4]})");
  1327. ExpectParseFailureForJson(
  1328. "RepeatedFieldWrongElementTypeExpectingIntegersGotString",
  1329. R"({"repeatedInt32": [1, 2, "name", 4]})");
  1330. ExpectParseFailureForJson(
  1331. "RepeatedFieldWrongElementTypeExpectingIntegersGotMessage",
  1332. R"({"repeatedInt32": [1, 2, 3, {"a": 4}]})");
  1333. ExpectParseFailureForJson(
  1334. "RepeatedFieldWrongElementTypeExpectingStringsGotInt",
  1335. R"({"repeatedString": ["1", 2, "3", "4"]})");
  1336. ExpectParseFailureForJson(
  1337. "RepeatedFieldWrongElementTypeExpectingStringsGotBool",
  1338. R"({"repeatedString": ["1", "2", false, "4"]})");
  1339. ExpectParseFailureForJson(
  1340. "RepeatedFieldWrongElementTypeExpectingStringsGotMessage",
  1341. R"({"repeatedString": ["1", 2, "3", {"a": 4}]})");
  1342. ExpectParseFailureForJson(
  1343. "RepeatedFieldWrongElementTypeExpectingMessagesGotInt",
  1344. R"({"repeatedNestedMessage": [{"a": 1}, 2]})");
  1345. ExpectParseFailureForJson(
  1346. "RepeatedFieldWrongElementTypeExpectingMessagesGotBool",
  1347. R"({"repeatedNestedMessage": [{"a": 1}, false]})");
  1348. ExpectParseFailureForJson(
  1349. "RepeatedFieldWrongElementTypeExpectingMessagesGotString",
  1350. R"({"repeatedNestedMessage": [{"a": 1}, "2"]})");
  1351. // Trailing comma in the repeated field is not allowed.
  1352. ExpectParseFailureForJson(
  1353. "RepeatedFieldTrailingComma",
  1354. R"({"repeatedInt32": [1, 2, 3, 4,]})");
  1355. ExpectParseFailureForJson(
  1356. "RepeatedFieldTrailingCommaWithSpace",
  1357. "{\"repeatedInt32\": [1, 2, 3, 4 ,]}");
  1358. ExpectParseFailureForJson(
  1359. "RepeatedFieldTrailingCommaWithSpaceCommaSpace",
  1360. "{\"repeatedInt32\": [1, 2, 3, 4 , ]}");
  1361. ExpectParseFailureForJson(
  1362. "RepeatedFieldTrailingCommaWithNewlines",
  1363. "{\"repeatedInt32\": [\n 1,\n 2,\n 3,\n 4,\n]}");
  1364. // Map fields.
  1365. RunValidJsonTest(
  1366. "Int32MapField",
  1367. R"({"mapInt32Int32": {"1": 2, "3": 4}})",
  1368. "map_int32_int32: {key: 1 value: 2}"
  1369. "map_int32_int32: {key: 3 value: 4}");
  1370. ExpectParseFailureForJson(
  1371. "Int32MapFieldKeyNotQuoted",
  1372. R"({"mapInt32Int32": {1: 2, 3: 4}})");
  1373. RunValidJsonTest(
  1374. "Uint32MapField",
  1375. R"({"mapUint32Uint32": {"1": 2, "3": 4}})",
  1376. "map_uint32_uint32: {key: 1 value: 2}"
  1377. "map_uint32_uint32: {key: 3 value: 4}");
  1378. ExpectParseFailureForJson(
  1379. "Uint32MapFieldKeyNotQuoted",
  1380. R"({"mapUint32Uint32": {1: 2, 3: 4}})");
  1381. RunValidJsonTest(
  1382. "Int64MapField",
  1383. R"({"mapInt64Int64": {"1": 2, "3": 4}})",
  1384. "map_int64_int64: {key: 1 value: 2}"
  1385. "map_int64_int64: {key: 3 value: 4}");
  1386. ExpectParseFailureForJson(
  1387. "Int64MapFieldKeyNotQuoted",
  1388. R"({"mapInt64Int64": {1: 2, 3: 4}})");
  1389. RunValidJsonTest(
  1390. "Uint64MapField",
  1391. R"({"mapUint64Uint64": {"1": 2, "3": 4}})",
  1392. "map_uint64_uint64: {key: 1 value: 2}"
  1393. "map_uint64_uint64: {key: 3 value: 4}");
  1394. ExpectParseFailureForJson(
  1395. "Uint64MapFieldKeyNotQuoted",
  1396. R"({"mapUint64Uint64": {1: 2, 3: 4}})");
  1397. RunValidJsonTest(
  1398. "BoolMapField",
  1399. R"({"mapBoolBool": {"true": true, "false": false}})",
  1400. "map_bool_bool: {key: true value: true}"
  1401. "map_bool_bool: {key: false value: false}");
  1402. ExpectParseFailureForJson(
  1403. "BoolMapFieldKeyNotQuoted",
  1404. R"({"mapBoolBool": {true: true, false: false}})");
  1405. RunValidJsonTest(
  1406. "MessageMapField",
  1407. R"({
  1408. "mapStringNestedMessage": {
  1409. "hello": {"a": 1234},
  1410. "world": {"a": 5678}
  1411. }
  1412. })",
  1413. R"(
  1414. map_string_nested_message: {
  1415. key: "hello"
  1416. value: {a: 1234}
  1417. }
  1418. map_string_nested_message: {
  1419. key: "world"
  1420. value: {a: 5678}
  1421. }
  1422. )");
  1423. // Since Map keys are represented as JSON strings, escaping should be allowed.
  1424. RunValidJsonTest(
  1425. "Int32MapEscapedKey",
  1426. R"({"mapInt32Int32": {"\u0031": 2}})",
  1427. "map_int32_int32: {key: 1 value: 2}");
  1428. RunValidJsonTest(
  1429. "Int64MapEscapedKey",
  1430. R"({"mapInt64Int64": {"\u0031": 2}})",
  1431. "map_int64_int64: {key: 1 value: 2}");
  1432. RunValidJsonTest(
  1433. "BoolMapEscapedKey",
  1434. R"({"mapBoolBool": {"tr\u0075e": true}})",
  1435. "map_bool_bool: {key: true value: true}");
  1436. // "null" is accepted for all fields types.
  1437. RunValidJsonTest(
  1438. "AllFieldAcceptNull",
  1439. R"({
  1440. "optionalInt32": null,
  1441. "optionalInt64": null,
  1442. "optionalUint32": null,
  1443. "optionalUint64": null,
  1444. "optionalBool": null,
  1445. "optionalString": null,
  1446. "optionalBytes": null,
  1447. "optionalNestedEnum": null,
  1448. "optionalNestedMessage": null,
  1449. "repeatedInt32": null,
  1450. "repeatedInt64": null,
  1451. "repeatedUint32": null,
  1452. "repeatedUint64": null,
  1453. "repeatedBool": null,
  1454. "repeatedString": null,
  1455. "repeatedBytes": null,
  1456. "repeatedNestedEnum": null,
  1457. "repeatedNestedMessage": null,
  1458. "mapInt32Int32": null,
  1459. "mapBoolBool": null,
  1460. "mapStringNestedMessage": null
  1461. })",
  1462. "");
  1463. // Repeated field elements cannot be null.
  1464. ExpectParseFailureForJson(
  1465. "RepeatedFieldPrimitiveElementIsNull",
  1466. R"({"repeatedInt32": [1, null, 2]})");
  1467. ExpectParseFailureForJson(
  1468. "RepeatedFieldMessageElementIsNull",
  1469. R"({"repeatedNestedMessage": [{"a":1}, null, {"a":2}]})");
  1470. // Map field keys cannot be null.
  1471. ExpectParseFailureForJson(
  1472. "MapFieldKeyIsNull",
  1473. R"({"mapInt32Int32": {null: 1}})");
  1474. // Map field values cannot be null.
  1475. ExpectParseFailureForJson(
  1476. "MapFieldValueIsNull",
  1477. R"({"mapInt32Int32": {"0": null}})");
  1478. // http://www.rfc-editor.org/rfc/rfc7159.txt says strings have to use double
  1479. // quotes.
  1480. ExpectParseFailureForJson(
  1481. "StringFieldSingleQuoteKey",
  1482. R"({'optionalString': "Hello world!"})");
  1483. ExpectParseFailureForJson(
  1484. "StringFieldSingleQuoteValue",
  1485. R"({"optionalString": 'Hello world!'})");
  1486. ExpectParseFailureForJson(
  1487. "StringFieldSingleQuoteBoth",
  1488. R"({'optionalString': 'Hello world!'})");
  1489. // Wrapper types.
  1490. RunValidJsonTest(
  1491. "OptionalBoolWrapper",
  1492. R"({"optionalBoolWrapper": false})",
  1493. "optional_bool_wrapper: {value: false}");
  1494. RunValidJsonTest(
  1495. "OptionalInt32Wrapper",
  1496. R"({"optionalInt32Wrapper": 0})",
  1497. "optional_int32_wrapper: {value: 0}");
  1498. RunValidJsonTest(
  1499. "OptionalUint32Wrapper",
  1500. R"({"optionalUint32Wrapper": 0})",
  1501. "optional_uint32_wrapper: {value: 0}");
  1502. RunValidJsonTest(
  1503. "OptionalInt64Wrapper",
  1504. R"({"optionalInt64Wrapper": 0})",
  1505. "optional_int64_wrapper: {value: 0}");
  1506. RunValidJsonTest(
  1507. "OptionalUint64Wrapper",
  1508. R"({"optionalUint64Wrapper": 0})",
  1509. "optional_uint64_wrapper: {value: 0}");
  1510. RunValidJsonTest(
  1511. "OptionalFloatWrapper",
  1512. R"({"optionalFloatWrapper": 0})",
  1513. "optional_float_wrapper: {value: 0}");
  1514. RunValidJsonTest(
  1515. "OptionalDoubleWrapper",
  1516. R"({"optionalDoubleWrapper": 0})",
  1517. "optional_double_wrapper: {value: 0}");
  1518. RunValidJsonTest(
  1519. "OptionalStringWrapper",
  1520. R"({"optionalStringWrapper": ""})",
  1521. R"(optional_string_wrapper: {value: ""})");
  1522. RunValidJsonTest(
  1523. "OptionalBytesWrapper",
  1524. R"({"optionalBytesWrapper": ""})",
  1525. R"(optional_bytes_wrapper: {value: ""})");
  1526. RunValidJsonTest(
  1527. "OptionalWrapperTypesWithNonDefaultValue",
  1528. R"({
  1529. "optionalBoolWrapper": true,
  1530. "optionalInt32Wrapper": 1,
  1531. "optionalUint32Wrapper": 1,
  1532. "optionalInt64Wrapper": "1",
  1533. "optionalUint64Wrapper": "1",
  1534. "optionalFloatWrapper": 1,
  1535. "optionalDoubleWrapper": 1,
  1536. "optionalStringWrapper": "1",
  1537. "optionalBytesWrapper": "AQI="
  1538. })",
  1539. R"(
  1540. optional_bool_wrapper: {value: true}
  1541. optional_int32_wrapper: {value: 1}
  1542. optional_uint32_wrapper: {value: 1}
  1543. optional_int64_wrapper: {value: 1}
  1544. optional_uint64_wrapper: {value: 1}
  1545. optional_float_wrapper: {value: 1}
  1546. optional_double_wrapper: {value: 1}
  1547. optional_string_wrapper: {value: "1"}
  1548. optional_bytes_wrapper: {value: "\x01\x02"}
  1549. )");
  1550. RunValidJsonTest(
  1551. "RepeatedBoolWrapper",
  1552. R"({"repeatedBoolWrapper": [true, false]})",
  1553. "repeated_bool_wrapper: {value: true}"
  1554. "repeated_bool_wrapper: {value: false}");
  1555. RunValidJsonTest(
  1556. "RepeatedInt32Wrapper",
  1557. R"({"repeatedInt32Wrapper": [0, 1]})",
  1558. "repeated_int32_wrapper: {value: 0}"
  1559. "repeated_int32_wrapper: {value: 1}");
  1560. RunValidJsonTest(
  1561. "RepeatedUint32Wrapper",
  1562. R"({"repeatedUint32Wrapper": [0, 1]})",
  1563. "repeated_uint32_wrapper: {value: 0}"
  1564. "repeated_uint32_wrapper: {value: 1}");
  1565. RunValidJsonTest(
  1566. "RepeatedInt64Wrapper",
  1567. R"({"repeatedInt64Wrapper": [0, 1]})",
  1568. "repeated_int64_wrapper: {value: 0}"
  1569. "repeated_int64_wrapper: {value: 1}");
  1570. RunValidJsonTest(
  1571. "RepeatedUint64Wrapper",
  1572. R"({"repeatedUint64Wrapper": [0, 1]})",
  1573. "repeated_uint64_wrapper: {value: 0}"
  1574. "repeated_uint64_wrapper: {value: 1}");
  1575. RunValidJsonTest(
  1576. "RepeatedFloatWrapper",
  1577. R"({"repeatedFloatWrapper": [0, 1]})",
  1578. "repeated_float_wrapper: {value: 0}"
  1579. "repeated_float_wrapper: {value: 1}");
  1580. RunValidJsonTest(
  1581. "RepeatedDoubleWrapper",
  1582. R"({"repeatedDoubleWrapper": [0, 1]})",
  1583. "repeated_double_wrapper: {value: 0}"
  1584. "repeated_double_wrapper: {value: 1}");
  1585. RunValidJsonTest(
  1586. "RepeatedStringWrapper",
  1587. R"({"repeatedStringWrapper": ["", "AQI="]})",
  1588. R"(
  1589. repeated_string_wrapper: {value: ""}
  1590. repeated_string_wrapper: {value: "AQI="}
  1591. )");
  1592. RunValidJsonTest(
  1593. "RepeatedBytesWrapper",
  1594. R"({"repeatedBytesWrapper": ["", "AQI="]})",
  1595. R"(
  1596. repeated_bytes_wrapper: {value: ""}
  1597. repeated_bytes_wrapper: {value: "\x01\x02"}
  1598. )");
  1599. RunValidJsonTest(
  1600. "WrapperTypesWithNullValue",
  1601. R"({
  1602. "optionalBoolWrapper": null,
  1603. "optionalInt32Wrapper": null,
  1604. "optionalUint32Wrapper": null,
  1605. "optionalInt64Wrapper": null,
  1606. "optionalUint64Wrapper": null,
  1607. "optionalFloatWrapper": null,
  1608. "optionalDoubleWrapper": null,
  1609. "optionalStringWrapper": null,
  1610. "optionalBytesWrapper": null,
  1611. "repeatedBoolWrapper": null,
  1612. "repeatedInt32Wrapper": null,
  1613. "repeatedUint32Wrapper": null,
  1614. "repeatedInt64Wrapper": null,
  1615. "repeatedUint64Wrapper": null,
  1616. "repeatedFloatWrapper": null,
  1617. "repeatedDoubleWrapper": null,
  1618. "repeatedStringWrapper": null,
  1619. "repeatedBytesWrapper": null
  1620. })",
  1621. "");
  1622. // Duration
  1623. RunValidJsonTest(
  1624. "DurationMinValue",
  1625. R"({"optionalDuration": "-315576000000.999999999s"})",
  1626. "optional_duration: {seconds: -315576000000 nanos: -999999999}");
  1627. RunValidJsonTest(
  1628. "DurationMaxValue",
  1629. R"({"optionalDuration": "315576000000.999999999s"})",
  1630. "optional_duration: {seconds: 315576000000 nanos: 999999999}");
  1631. RunValidJsonTest(
  1632. "DurationRepeatedValue",
  1633. R"({"repeatedDuration": ["1.5s", "-1.5s"]})",
  1634. "repeated_duration: {seconds: 1 nanos: 500000000}"
  1635. "repeated_duration: {seconds: -1 nanos: -500000000}");
  1636. ExpectParseFailureForJson(
  1637. "DurationMissingS",
  1638. R"({"optionalDuration": "1"})");
  1639. ExpectParseFailureForJson(
  1640. "DurationJsonInputTooSmall",
  1641. R"({"optionalDuration": "-315576000001.000000000s"})");
  1642. ExpectParseFailureForJson(
  1643. "DurationJsonInputTooLarge",
  1644. R"({"optionalDuration": "315576000001.000000000s"})");
  1645. ExpectSerializeFailureForJson(
  1646. "DurationProtoInputTooSmall",
  1647. "optional_duration: {seconds: -315576000001 nanos: 0}");
  1648. ExpectSerializeFailureForJson(
  1649. "DurationProtoInputTooLarge",
  1650. "optional_duration: {seconds: 315576000001 nanos: 0}");
  1651. RunValidJsonTestWithValidator(
  1652. "DurationHasZeroFractionalDigit",
  1653. R"({"optionalDuration": "1.000000000s"})",
  1654. [](const Json::Value& value) {
  1655. return value["optionalDuration"].asString() == "1s";
  1656. });
  1657. RunValidJsonTestWithValidator(
  1658. "DurationHas3FractionalDigits",
  1659. R"({"optionalDuration": "1.010000000s"})",
  1660. [](const Json::Value& value) {
  1661. return value["optionalDuration"].asString() == "1.010s";
  1662. });
  1663. RunValidJsonTestWithValidator(
  1664. "DurationHas6FractionalDigits",
  1665. R"({"optionalDuration": "1.000010000s"})",
  1666. [](const Json::Value& value) {
  1667. return value["optionalDuration"].asString() == "1.000010s";
  1668. });
  1669. RunValidJsonTestWithValidator(
  1670. "DurationHas9FractionalDigits",
  1671. R"({"optionalDuration": "1.000000010s"})",
  1672. [](const Json::Value& value) {
  1673. return value["optionalDuration"].asString() == "1.000000010s";
  1674. });
  1675. // Timestamp
  1676. RunValidJsonTest(
  1677. "TimestampMinValue",
  1678. R"({"optionalTimestamp": "0001-01-01T00:00:00Z"})",
  1679. "optional_timestamp: {seconds: -62135596800}");
  1680. RunValidJsonTest(
  1681. "TimestampMaxValue",
  1682. R"({"optionalTimestamp": "9999-12-31T23:59:59.999999999Z"})",
  1683. "optional_timestamp: {seconds: 253402300799 nanos: 999999999}");
  1684. RunValidJsonTest(
  1685. "TimestampRepeatedValue",
  1686. R"({
  1687. "repeatedTimestamp": [
  1688. "0001-01-01T00:00:00Z",
  1689. "9999-12-31T23:59:59.999999999Z"
  1690. ]
  1691. })",
  1692. "repeated_timestamp: {seconds: -62135596800}"
  1693. "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}");
  1694. RunValidJsonTest(
  1695. "TimestampWithPositiveOffset",
  1696. R"({"optionalTimestamp": "1970-01-01T08:00:00+08:00"})",
  1697. "optional_timestamp: {seconds: 0}");
  1698. RunValidJsonTest(
  1699. "TimestampWithNegativeOffset",
  1700. R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
  1701. "optional_timestamp: {seconds: 0}");
  1702. ExpectParseFailureForJson(
  1703. "TimestampJsonInputTooSmall",
  1704. R"({"optionalTimestamp": "0000-01-01T00:00:00Z"})");
  1705. ExpectParseFailureForJson(
  1706. "TimestampJsonInputTooLarge",
  1707. R"({"optionalTimestamp": "10000-01-01T00:00:00Z"})");
  1708. ExpectParseFailureForJson(
  1709. "TimestampJsonInputMissingZ",
  1710. R"({"optionalTimestamp": "0001-01-01T00:00:00"})");
  1711. ExpectParseFailureForJson(
  1712. "TimestampJsonInputMissingT",
  1713. R"({"optionalTimestamp": "0001-01-01 00:00:00Z"})");
  1714. ExpectParseFailureForJson(
  1715. "TimestampJsonInputLowercaseZ",
  1716. R"({"optionalTimestamp": "0001-01-01T00:00:00z"})");
  1717. ExpectParseFailureForJson(
  1718. "TimestampJsonInputLowercaseT",
  1719. R"({"optionalTimestamp": "0001-01-01t00:00:00Z"})");
  1720. ExpectSerializeFailureForJson(
  1721. "TimestampProtoInputTooSmall",
  1722. "optional_timestamp: {seconds: -62135596801}");
  1723. ExpectSerializeFailureForJson(
  1724. "TimestampProtoInputTooLarge",
  1725. "optional_timestamp: {seconds: 253402300800}");
  1726. RunValidJsonTestWithValidator(
  1727. "TimestampZeroNormalized",
  1728. R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
  1729. [](const Json::Value& value) {
  1730. return value["optionalTimestamp"].asString() ==
  1731. "1970-01-01T00:00:00Z";
  1732. });
  1733. RunValidJsonTestWithValidator(
  1734. "TimestampHasZeroFractionalDigit",
  1735. R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
  1736. [](const Json::Value& value) {
  1737. return value["optionalTimestamp"].asString() ==
  1738. "1970-01-01T00:00:00Z";
  1739. });
  1740. RunValidJsonTestWithValidator(
  1741. "TimestampHas3FractionalDigits",
  1742. R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
  1743. [](const Json::Value& value) {
  1744. return value["optionalTimestamp"].asString() ==
  1745. "1970-01-01T00:00:00.010Z";
  1746. });
  1747. RunValidJsonTestWithValidator(
  1748. "TimestampHas6FractionalDigits",
  1749. R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
  1750. [](const Json::Value& value) {
  1751. return value["optionalTimestamp"].asString() ==
  1752. "1970-01-01T00:00:00.000010Z";
  1753. });
  1754. RunValidJsonTestWithValidator(
  1755. "TimestampHas9FractionalDigits",
  1756. R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
  1757. [](const Json::Value& value) {
  1758. return value["optionalTimestamp"].asString() ==
  1759. "1970-01-01T00:00:00.000000010Z";
  1760. });
  1761. // FieldMask
  1762. RunValidJsonTest(
  1763. "FieldMask",
  1764. R"({"optionalFieldMask": "foo,barBaz"})",
  1765. R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
  1766. ExpectParseFailureForJson(
  1767. "FieldMaskInvalidCharacter",
  1768. R"({"optionalFieldMask": "foo,bar_bar"})");
  1769. ExpectSerializeFailureForJson(
  1770. "FieldMaskPathsDontRoundTrip",
  1771. R"(optional_field_mask: {paths: "fooBar"})");
  1772. ExpectSerializeFailureForJson(
  1773. "FieldMaskNumbersDontRoundTrip",
  1774. R"(optional_field_mask: {paths: "foo_3_bar"})");
  1775. ExpectSerializeFailureForJson(
  1776. "FieldMaskTooManyUnderscore",
  1777. R"(optional_field_mask: {paths: "foo__bar"})");
  1778. // Struct
  1779. RunValidJsonTest(
  1780. "Struct",
  1781. R"({
  1782. "optionalStruct": {
  1783. "nullValue": null,
  1784. "intValue": 1234,
  1785. "boolValue": true,
  1786. "doubleValue": 1234.5678,
  1787. "stringValue": "Hello world!",
  1788. "listValue": [1234, "5678"],
  1789. "objectValue": {
  1790. "value": 0
  1791. }
  1792. }
  1793. })",
  1794. R"(
  1795. optional_struct: {
  1796. fields: {
  1797. key: "nullValue"
  1798. value: {null_value: NULL_VALUE}
  1799. }
  1800. fields: {
  1801. key: "intValue"
  1802. value: {number_value: 1234}
  1803. }
  1804. fields: {
  1805. key: "boolValue"
  1806. value: {bool_value: true}
  1807. }
  1808. fields: {
  1809. key: "doubleValue"
  1810. value: {number_value: 1234.5678}
  1811. }
  1812. fields: {
  1813. key: "stringValue"
  1814. value: {string_value: "Hello world!"}
  1815. }
  1816. fields: {
  1817. key: "listValue"
  1818. value: {
  1819. list_value: {
  1820. values: {
  1821. number_value: 1234
  1822. }
  1823. values: {
  1824. string_value: "5678"
  1825. }
  1826. }
  1827. }
  1828. }
  1829. fields: {
  1830. key: "objectValue"
  1831. value: {
  1832. struct_value: {
  1833. fields: {
  1834. key: "value"
  1835. value: {
  1836. number_value: 0
  1837. }
  1838. }
  1839. }
  1840. }
  1841. }
  1842. }
  1843. )");
  1844. // Value
  1845. RunValidJsonTest(
  1846. "ValueAcceptInteger",
  1847. R"({"optionalValue": 1})",
  1848. "optional_value: { number_value: 1}");
  1849. RunValidJsonTest(
  1850. "ValueAcceptFloat",
  1851. R"({"optionalValue": 1.5})",
  1852. "optional_value: { number_value: 1.5}");
  1853. RunValidJsonTest(
  1854. "ValueAcceptBool",
  1855. R"({"optionalValue": false})",
  1856. "optional_value: { bool_value: false}");
  1857. RunValidJsonTest(
  1858. "ValueAcceptNull",
  1859. R"({"optionalValue": null})",
  1860. "optional_value: { null_value: NULL_VALUE}");
  1861. RunValidJsonTest(
  1862. "ValueAcceptString",
  1863. R"({"optionalValue": "hello"})",
  1864. R"(optional_value: { string_value: "hello"})");
  1865. RunValidJsonTest(
  1866. "ValueAcceptList",
  1867. R"({"optionalValue": [0, "hello"]})",
  1868. R"(
  1869. optional_value: {
  1870. list_value: {
  1871. values: {
  1872. number_value: 0
  1873. }
  1874. values: {
  1875. string_value: "hello"
  1876. }
  1877. }
  1878. }
  1879. )");
  1880. RunValidJsonTest(
  1881. "ValueAcceptObject",
  1882. R"({"optionalValue": {"value": 1}})",
  1883. R"(
  1884. optional_value: {
  1885. struct_value: {
  1886. fields: {
  1887. key: "value"
  1888. value: {
  1889. number_value: 1
  1890. }
  1891. }
  1892. }
  1893. }
  1894. )");
  1895. // Any
  1896. RunValidJsonTest(
  1897. "Any",
  1898. R"({
  1899. "optionalAny": {
  1900. "@type": "type.googleapis.com/conformance.TestAllTypes",
  1901. "optionalInt32": 12345
  1902. }
  1903. })",
  1904. R"(
  1905. optional_any: {
  1906. [type.googleapis.com/conformance.TestAllTypes] {
  1907. optional_int32: 12345
  1908. }
  1909. }
  1910. )");
  1911. RunValidJsonTest(
  1912. "AnyNested",
  1913. R"({
  1914. "optionalAny": {
  1915. "@type": "type.googleapis.com/google.protobuf.Any",
  1916. "value": {
  1917. "@type": "type.googleapis.com/conformance.TestAllTypes",
  1918. "optionalInt32": 12345
  1919. }
  1920. }
  1921. })",
  1922. R"(
  1923. optional_any: {
  1924. [type.googleapis.com/google.protobuf.Any] {
  1925. [type.googleapis.com/conformance.TestAllTypes] {
  1926. optional_int32: 12345
  1927. }
  1928. }
  1929. }
  1930. )");
  1931. // The special "@type" tag is not required to appear first.
  1932. RunValidJsonTest(
  1933. "AnyUnorderedTypeTag",
  1934. R"({
  1935. "optionalAny": {
  1936. "optionalInt32": 12345,
  1937. "@type": "type.googleapis.com/conformance.TestAllTypes"
  1938. }
  1939. })",
  1940. R"(
  1941. optional_any: {
  1942. [type.googleapis.com/conformance.TestAllTypes] {
  1943. optional_int32: 12345
  1944. }
  1945. }
  1946. )");
  1947. // Well-known types in Any.
  1948. RunValidJsonTest(
  1949. "AnyWithInt32ValueWrapper",
  1950. R"({
  1951. "optionalAny": {
  1952. "@type": "type.googleapis.com/google.protobuf.Int32Value",
  1953. "value": 12345
  1954. }
  1955. })",
  1956. R"(
  1957. optional_any: {
  1958. [type.googleapis.com/google.protobuf.Int32Value] {
  1959. value: 12345
  1960. }
  1961. }
  1962. )");
  1963. RunValidJsonTest(
  1964. "AnyWithDuration",
  1965. R"({
  1966. "optionalAny": {
  1967. "@type": "type.googleapis.com/google.protobuf.Duration",
  1968. "value": "1.5s"
  1969. }
  1970. })",
  1971. R"(
  1972. optional_any: {
  1973. [type.googleapis.com/google.protobuf.Duration] {
  1974. seconds: 1
  1975. nanos: 500000000
  1976. }
  1977. }
  1978. )");
  1979. RunValidJsonTest(
  1980. "AnyWithTimestamp",
  1981. R"({
  1982. "optionalAny": {
  1983. "@type": "type.googleapis.com/google.protobuf.Timestamp",
  1984. "value": "1970-01-01T00:00:00Z"
  1985. }
  1986. })",
  1987. R"(
  1988. optional_any: {
  1989. [type.googleapis.com/google.protobuf.Timestamp] {
  1990. seconds: 0
  1991. nanos: 0
  1992. }
  1993. }
  1994. )");
  1995. RunValidJsonTest(
  1996. "AnyWithFieldMask",
  1997. R"({
  1998. "optionalAny": {
  1999. "@type": "type.googleapis.com/google.protobuf.FieldMask",
  2000. "value": "foo,barBaz"
  2001. }
  2002. })",
  2003. R"(
  2004. optional_any: {
  2005. [type.googleapis.com/google.protobuf.FieldMask] {
  2006. paths: ["foo", "bar_baz"]
  2007. }
  2008. }
  2009. )");
  2010. RunValidJsonTest(
  2011. "AnyWithStruct",
  2012. R"({
  2013. "optionalAny": {
  2014. "@type": "type.googleapis.com/google.protobuf.Struct",
  2015. "value": {
  2016. "foo": 1
  2017. }
  2018. }
  2019. })",
  2020. R"(
  2021. optional_any: {
  2022. [type.googleapis.com/google.protobuf.Struct] {
  2023. fields: {
  2024. key: "foo"
  2025. value: {
  2026. number_value: 1
  2027. }
  2028. }
  2029. }
  2030. }
  2031. )");
  2032. RunValidJsonTest(
  2033. "AnyWithValueForJsonObject",
  2034. R"({
  2035. "optionalAny": {
  2036. "@type": "type.googleapis.com/google.protobuf.Value",
  2037. "value": {
  2038. "foo": 1
  2039. }
  2040. }
  2041. })",
  2042. R"(
  2043. optional_any: {
  2044. [type.googleapis.com/google.protobuf.Value] {
  2045. struct_value: {
  2046. fields: {
  2047. key: "foo"
  2048. value: {
  2049. number_value: 1
  2050. }
  2051. }
  2052. }
  2053. }
  2054. }
  2055. )");
  2056. RunValidJsonTest(
  2057. "AnyWithValueForInteger",
  2058. R"({
  2059. "optionalAny": {
  2060. "@type": "type.googleapis.com/google.protobuf.Value",
  2061. "value": 1
  2062. }
  2063. })",
  2064. R"(
  2065. optional_any: {
  2066. [type.googleapis.com/google.protobuf.Value] {
  2067. number_value: 1
  2068. }
  2069. }
  2070. )");
  2071. bool ok = true;
  2072. if (!CheckSetEmpty(expected_to_fail_, "nonexistent_tests.txt",
  2073. "These tests were listed in the failure list, but they "
  2074. "don't exist. Remove them from the failure list by "
  2075. "running:\n"
  2076. " ./update_failure_list.py " + failure_list_filename_ +
  2077. " --remove nonexistent_tests.txt")) {
  2078. ok = false;
  2079. }
  2080. if (!CheckSetEmpty(unexpected_failing_tests_, "failing_tests.txt",
  2081. "These tests failed. If they can't be fixed right now, "
  2082. "you can add them to the failure list so the overall "
  2083. "suite can succeed. Add them to the failure list by "
  2084. "running:\n"
  2085. " ./update_failure_list.py " + failure_list_filename_ +
  2086. " --add failing_tests.txt")) {
  2087. ok = false;
  2088. }
  2089. if (!CheckSetEmpty(unexpected_succeeding_tests_, "succeeding_tests.txt",
  2090. "These tests succeeded, even though they were listed in "
  2091. "the failure list. Remove them from the failure list "
  2092. "by running:\n"
  2093. " ./update_failure_list.py " + failure_list_filename_ +
  2094. " --remove succeeding_tests.txt")) {
  2095. ok = false;
  2096. }
  2097. if (verbose_) {
  2098. CheckSetEmpty(skipped_, "",
  2099. "These tests were skipped (probably because support for some "
  2100. "features is not implemented)");
  2101. }
  2102. StringAppendF(&output_,
  2103. "CONFORMANCE SUITE %s: %d successes, %d skipped, "
  2104. "%d expected failures, %d unexpected failures.\n",
  2105. ok ? "PASSED" : "FAILED", successes_, skipped_.size(),
  2106. expected_failures_, unexpected_failing_tests_.size());
  2107. StringAppendF(&output_, "\n");
  2108. output->assign(output_);
  2109. return ok;
  2110. }
  2111. } // namespace protobuf
  2112. } // namespace google