binary_json_conformance_suite.cc 119 KB

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