binary_json_conformance_suite.cc 88 KB

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