binary_json_conformance_suite.cc 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198
  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. UNSPECIFIED = 0,
  201. TRUE = 1,
  202. FALSE = 2,
  203. };
  204. const FieldDescriptor* GetFieldForType(FieldDescriptor::Type type,
  205. bool repeated, bool is_proto3,
  206. Packed packed = Packed::UNSPECIFIED) {
  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::TRUE && !f->is_packed()) ||
  213. (packed == Packed::FALSE && 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::TRUE) {
  223. packed_string = "Packed ";
  224. }
  225. if (packed == Packed::FALSE) {
  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 (int 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 proto3 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 proto3 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) {
  506. TestAllTypesProto3 prototype;
  507. ConformanceRequestSetting setting(
  508. level, conformance::JSON, conformance::JSON,
  509. conformance::JSON_TEST,
  510. prototype, test_name, input_json);
  511. const ConformanceRequest& request = setting.GetRequest();
  512. ConformanceResponse response;
  513. string effective_test_name =
  514. StrCat(setting.ConformanceLevelToString(level),
  515. ".Proto3.JsonInput.", 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::TRUE);
  711. const FieldDescriptor* unpacked_field =
  712. GetFieldForType(type, true, is_proto3, Packed::FALSE);
  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. RunValidJsonTestWithValidator(
  1659. "FieldNameWithNumbers", REQUIRED,
  1660. R"({
  1661. "field0name5": 5,
  1662. "field0Name6": 6
  1663. })",
  1664. [](const Json::Value& value) {
  1665. return value.isMember("field0name5") &&
  1666. value.isMember("field0Name6");
  1667. });
  1668. RunValidJsonTestWithValidator(
  1669. "FieldNameWithMixedCases", REQUIRED,
  1670. R"({
  1671. "fieldName7": 7,
  1672. "FieldName8": 8,
  1673. "fieldName9": 9,
  1674. "FieldName10": 10,
  1675. "FIELDNAME11": 11,
  1676. "FIELDName12": 12
  1677. })",
  1678. [](const Json::Value& value) {
  1679. return value.isMember("fieldName7") &&
  1680. value.isMember("FieldName8") &&
  1681. value.isMember("fieldName9") &&
  1682. value.isMember("FieldName10") &&
  1683. value.isMember("FIELDNAME11") &&
  1684. value.isMember("FIELDName12");
  1685. });
  1686. RunValidJsonTestWithValidator(
  1687. "FieldNameWithDoubleUnderscores", RECOMMENDED,
  1688. R"({
  1689. "FieldName13": 13,
  1690. "FieldName14": 14,
  1691. "fieldName15": 15,
  1692. "fieldName16": 16,
  1693. "fieldName17": 17,
  1694. "FieldName18": 18
  1695. })",
  1696. [](const Json::Value& value) {
  1697. return value.isMember("FieldName13") &&
  1698. value.isMember("FieldName14") &&
  1699. value.isMember("fieldName15") &&
  1700. value.isMember("fieldName16") &&
  1701. value.isMember("fieldName17") &&
  1702. value.isMember("FieldName18");
  1703. });
  1704. }
  1705. void BinaryAndJsonConformanceSuite::RunJsonTestsForNonRepeatedTypes() {
  1706. // Integer fields.
  1707. RunValidJsonTest(
  1708. "Int32FieldMaxValue", REQUIRED,
  1709. R"({"optionalInt32": 2147483647})",
  1710. "optional_int32: 2147483647");
  1711. RunValidJsonTest(
  1712. "Int32FieldMinValue", REQUIRED,
  1713. R"({"optionalInt32": -2147483648})",
  1714. "optional_int32: -2147483648");
  1715. RunValidJsonTest(
  1716. "Uint32FieldMaxValue", REQUIRED,
  1717. R"({"optionalUint32": 4294967295})",
  1718. "optional_uint32: 4294967295");
  1719. RunValidJsonTest(
  1720. "Int64FieldMaxValue", REQUIRED,
  1721. R"({"optionalInt64": "9223372036854775807"})",
  1722. "optional_int64: 9223372036854775807");
  1723. RunValidJsonTest(
  1724. "Int64FieldMinValue", REQUIRED,
  1725. R"({"optionalInt64": "-9223372036854775808"})",
  1726. "optional_int64: -9223372036854775808");
  1727. RunValidJsonTest(
  1728. "Uint64FieldMaxValue", REQUIRED,
  1729. R"({"optionalUint64": "18446744073709551615"})",
  1730. "optional_uint64: 18446744073709551615");
  1731. // While not the largest Int64, this is the largest
  1732. // Int64 which can be exactly represented within an
  1733. // IEEE-754 64-bit float, which is the expected level
  1734. // of interoperability guarantee. Larger values may
  1735. // work in some implementations, but should not be
  1736. // relied upon.
  1737. RunValidJsonTest(
  1738. "Int64FieldMaxValueNotQuoted", REQUIRED,
  1739. R"({"optionalInt64": 9223372036854774784})",
  1740. "optional_int64: 9223372036854774784");
  1741. RunValidJsonTest(
  1742. "Int64FieldMinValueNotQuoted", REQUIRED,
  1743. R"({"optionalInt64": -9223372036854775808})",
  1744. "optional_int64: -9223372036854775808");
  1745. // Largest interoperable Uint64; see comment above
  1746. // for Int64FieldMaxValueNotQuoted.
  1747. RunValidJsonTest(
  1748. "Uint64FieldMaxValueNotQuoted", REQUIRED,
  1749. R"({"optionalUint64": 18446744073709549568})",
  1750. "optional_uint64: 18446744073709549568");
  1751. // Values can be represented as JSON strings.
  1752. RunValidJsonTest(
  1753. "Int32FieldStringValue", REQUIRED,
  1754. R"({"optionalInt32": "2147483647"})",
  1755. "optional_int32: 2147483647");
  1756. RunValidJsonTest(
  1757. "Int32FieldStringValueEscaped", REQUIRED,
  1758. R"({"optionalInt32": "2\u003147483647"})",
  1759. "optional_int32: 2147483647");
  1760. // Parsers reject out-of-bound integer values.
  1761. ExpectParseFailureForJson(
  1762. "Int32FieldTooLarge", REQUIRED,
  1763. R"({"optionalInt32": 2147483648})");
  1764. ExpectParseFailureForJson(
  1765. "Int32FieldTooSmall", REQUIRED,
  1766. R"({"optionalInt32": -2147483649})");
  1767. ExpectParseFailureForJson(
  1768. "Uint32FieldTooLarge", REQUIRED,
  1769. R"({"optionalUint32": 4294967296})");
  1770. ExpectParseFailureForJson(
  1771. "Int64FieldTooLarge", REQUIRED,
  1772. R"({"optionalInt64": "9223372036854775808"})");
  1773. ExpectParseFailureForJson(
  1774. "Int64FieldTooSmall", REQUIRED,
  1775. R"({"optionalInt64": "-9223372036854775809"})");
  1776. ExpectParseFailureForJson(
  1777. "Uint64FieldTooLarge", REQUIRED,
  1778. R"({"optionalUint64": "18446744073709551616"})");
  1779. // Parser reject non-integer numeric values as well.
  1780. ExpectParseFailureForJson(
  1781. "Int32FieldNotInteger", REQUIRED,
  1782. R"({"optionalInt32": 0.5})");
  1783. ExpectParseFailureForJson(
  1784. "Uint32FieldNotInteger", REQUIRED,
  1785. R"({"optionalUint32": 0.5})");
  1786. ExpectParseFailureForJson(
  1787. "Int64FieldNotInteger", REQUIRED,
  1788. R"({"optionalInt64": "0.5"})");
  1789. ExpectParseFailureForJson(
  1790. "Uint64FieldNotInteger", REQUIRED,
  1791. R"({"optionalUint64": "0.5"})");
  1792. // Integers but represented as float values are accepted.
  1793. RunValidJsonTest(
  1794. "Int32FieldFloatTrailingZero", REQUIRED,
  1795. R"({"optionalInt32": 100000.000})",
  1796. "optional_int32: 100000");
  1797. RunValidJsonTest(
  1798. "Int32FieldExponentialFormat", REQUIRED,
  1799. R"({"optionalInt32": 1e5})",
  1800. "optional_int32: 100000");
  1801. RunValidJsonTest(
  1802. "Int32FieldMaxFloatValue", REQUIRED,
  1803. R"({"optionalInt32": 2.147483647e9})",
  1804. "optional_int32: 2147483647");
  1805. RunValidJsonTest(
  1806. "Int32FieldMinFloatValue", REQUIRED,
  1807. R"({"optionalInt32": -2.147483648e9})",
  1808. "optional_int32: -2147483648");
  1809. RunValidJsonTest(
  1810. "Uint32FieldMaxFloatValue", REQUIRED,
  1811. R"({"optionalUint32": 4.294967295e9})",
  1812. "optional_uint32: 4294967295");
  1813. // Parser reject non-numeric values.
  1814. ExpectParseFailureForJson(
  1815. "Int32FieldNotNumber", REQUIRED,
  1816. R"({"optionalInt32": "3x3"})");
  1817. ExpectParseFailureForJson(
  1818. "Uint32FieldNotNumber", REQUIRED,
  1819. R"({"optionalUint32": "3x3"})");
  1820. ExpectParseFailureForJson(
  1821. "Int64FieldNotNumber", REQUIRED,
  1822. R"({"optionalInt64": "3x3"})");
  1823. ExpectParseFailureForJson(
  1824. "Uint64FieldNotNumber", REQUIRED,
  1825. R"({"optionalUint64": "3x3"})");
  1826. // JSON does not allow "+" on numric values.
  1827. ExpectParseFailureForJson(
  1828. "Int32FieldPlusSign", REQUIRED,
  1829. R"({"optionalInt32": +1})");
  1830. // JSON doesn't allow leading 0s.
  1831. ExpectParseFailureForJson(
  1832. "Int32FieldLeadingZero", REQUIRED,
  1833. R"({"optionalInt32": 01})");
  1834. ExpectParseFailureForJson(
  1835. "Int32FieldNegativeWithLeadingZero", REQUIRED,
  1836. R"({"optionalInt32": -01})");
  1837. // String values must follow the same syntax rule. Specifically leading
  1838. // or trailing spaces are not allowed.
  1839. ExpectParseFailureForJson(
  1840. "Int32FieldLeadingSpace", REQUIRED,
  1841. R"({"optionalInt32": " 1"})");
  1842. ExpectParseFailureForJson(
  1843. "Int32FieldTrailingSpace", REQUIRED,
  1844. R"({"optionalInt32": "1 "})");
  1845. // 64-bit values are serialized as strings.
  1846. RunValidJsonTestWithValidator(
  1847. "Int64FieldBeString", RECOMMENDED,
  1848. R"({"optionalInt64": 1})",
  1849. [](const Json::Value& value) {
  1850. return value["optionalInt64"].type() == Json::stringValue &&
  1851. value["optionalInt64"].asString() == "1";
  1852. });
  1853. RunValidJsonTestWithValidator(
  1854. "Uint64FieldBeString", RECOMMENDED,
  1855. R"({"optionalUint64": 1})",
  1856. [](const Json::Value& value) {
  1857. return value["optionalUint64"].type() == Json::stringValue &&
  1858. value["optionalUint64"].asString() == "1";
  1859. });
  1860. // Bool fields.
  1861. RunValidJsonTest(
  1862. "BoolFieldTrue", REQUIRED,
  1863. R"({"optionalBool":true})",
  1864. "optional_bool: true");
  1865. RunValidJsonTest(
  1866. "BoolFieldFalse", REQUIRED,
  1867. R"({"optionalBool":false})",
  1868. "optional_bool: false");
  1869. // Other forms are not allowed.
  1870. ExpectParseFailureForJson(
  1871. "BoolFieldIntegerZero", RECOMMENDED,
  1872. R"({"optionalBool":0})");
  1873. ExpectParseFailureForJson(
  1874. "BoolFieldIntegerOne", RECOMMENDED,
  1875. R"({"optionalBool":1})");
  1876. ExpectParseFailureForJson(
  1877. "BoolFieldCamelCaseTrue", RECOMMENDED,
  1878. R"({"optionalBool":True})");
  1879. ExpectParseFailureForJson(
  1880. "BoolFieldCamelCaseFalse", RECOMMENDED,
  1881. R"({"optionalBool":False})");
  1882. ExpectParseFailureForJson(
  1883. "BoolFieldAllCapitalTrue", RECOMMENDED,
  1884. R"({"optionalBool":TRUE})");
  1885. ExpectParseFailureForJson(
  1886. "BoolFieldAllCapitalFalse", RECOMMENDED,
  1887. R"({"optionalBool":FALSE})");
  1888. ExpectParseFailureForJson(
  1889. "BoolFieldDoubleQuotedTrue", RECOMMENDED,
  1890. R"({"optionalBool":"true"})");
  1891. ExpectParseFailureForJson(
  1892. "BoolFieldDoubleQuotedFalse", RECOMMENDED,
  1893. R"({"optionalBool":"false"})");
  1894. // Float fields.
  1895. RunValidJsonTest(
  1896. "FloatFieldMinPositiveValue", REQUIRED,
  1897. R"({"optionalFloat": 1.175494e-38})",
  1898. "optional_float: 1.175494e-38");
  1899. RunValidJsonTest(
  1900. "FloatFieldMaxNegativeValue", REQUIRED,
  1901. R"({"optionalFloat": -1.175494e-38})",
  1902. "optional_float: -1.175494e-38");
  1903. RunValidJsonTest(
  1904. "FloatFieldMaxPositiveValue", REQUIRED,
  1905. R"({"optionalFloat": 3.402823e+38})",
  1906. "optional_float: 3.402823e+38");
  1907. RunValidJsonTest(
  1908. "FloatFieldMinNegativeValue", REQUIRED,
  1909. R"({"optionalFloat": 3.402823e+38})",
  1910. "optional_float: 3.402823e+38");
  1911. // Values can be quoted.
  1912. RunValidJsonTest(
  1913. "FloatFieldQuotedValue", REQUIRED,
  1914. R"({"optionalFloat": "1"})",
  1915. "optional_float: 1");
  1916. // Special values.
  1917. RunValidJsonTest(
  1918. "FloatFieldNan", REQUIRED,
  1919. R"({"optionalFloat": "NaN"})",
  1920. "optional_float: nan");
  1921. RunValidJsonTest(
  1922. "FloatFieldInfinity", REQUIRED,
  1923. R"({"optionalFloat": "Infinity"})",
  1924. "optional_float: inf");
  1925. RunValidJsonTest(
  1926. "FloatFieldNegativeInfinity", REQUIRED,
  1927. R"({"optionalFloat": "-Infinity"})",
  1928. "optional_float: -inf");
  1929. // Non-cannonical Nan will be correctly normalized.
  1930. {
  1931. TestAllTypesProto3 message;
  1932. // IEEE floating-point standard 32-bit quiet NaN:
  1933. // 0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  1934. message.set_optional_float(
  1935. WireFormatLite::DecodeFloat(0x7FA12345));
  1936. RunValidJsonTestWithProtobufInput(
  1937. "FloatFieldNormalizeQuietNan", REQUIRED, message,
  1938. "optional_float: nan");
  1939. // IEEE floating-point standard 64-bit signaling NaN:
  1940. // 1111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
  1941. message.set_optional_float(
  1942. WireFormatLite::DecodeFloat(0xFFB54321));
  1943. RunValidJsonTestWithProtobufInput(
  1944. "FloatFieldNormalizeSignalingNan", REQUIRED, message,
  1945. "optional_float: nan");
  1946. }
  1947. // Special values must be quoted.
  1948. ExpectParseFailureForJson(
  1949. "FloatFieldNanNotQuoted", RECOMMENDED,
  1950. R"({"optionalFloat": NaN})");
  1951. ExpectParseFailureForJson(
  1952. "FloatFieldInfinityNotQuoted", RECOMMENDED,
  1953. R"({"optionalFloat": Infinity})");
  1954. ExpectParseFailureForJson(
  1955. "FloatFieldNegativeInfinityNotQuoted", RECOMMENDED,
  1956. R"({"optionalFloat": -Infinity})");
  1957. // Parsers should reject out-of-bound values.
  1958. ExpectParseFailureForJson(
  1959. "FloatFieldTooSmall", REQUIRED,
  1960. R"({"optionalFloat": -3.502823e+38})");
  1961. ExpectParseFailureForJson(
  1962. "FloatFieldTooLarge", REQUIRED,
  1963. R"({"optionalFloat": 3.502823e+38})");
  1964. // Double fields.
  1965. RunValidJsonTest(
  1966. "DoubleFieldMinPositiveValue", REQUIRED,
  1967. R"({"optionalDouble": 2.22507e-308})",
  1968. "optional_double: 2.22507e-308");
  1969. RunValidJsonTest(
  1970. "DoubleFieldMaxNegativeValue", REQUIRED,
  1971. R"({"optionalDouble": -2.22507e-308})",
  1972. "optional_double: -2.22507e-308");
  1973. RunValidJsonTest(
  1974. "DoubleFieldMaxPositiveValue", REQUIRED,
  1975. R"({"optionalDouble": 1.79769e+308})",
  1976. "optional_double: 1.79769e+308");
  1977. RunValidJsonTest(
  1978. "DoubleFieldMinNegativeValue", REQUIRED,
  1979. R"({"optionalDouble": -1.79769e+308})",
  1980. "optional_double: -1.79769e+308");
  1981. // Values can be quoted.
  1982. RunValidJsonTest(
  1983. "DoubleFieldQuotedValue", REQUIRED,
  1984. R"({"optionalDouble": "1"})",
  1985. "optional_double: 1");
  1986. // Speical values.
  1987. RunValidJsonTest(
  1988. "DoubleFieldNan", REQUIRED,
  1989. R"({"optionalDouble": "NaN"})",
  1990. "optional_double: nan");
  1991. RunValidJsonTest(
  1992. "DoubleFieldInfinity", REQUIRED,
  1993. R"({"optionalDouble": "Infinity"})",
  1994. "optional_double: inf");
  1995. RunValidJsonTest(
  1996. "DoubleFieldNegativeInfinity", REQUIRED,
  1997. R"({"optionalDouble": "-Infinity"})",
  1998. "optional_double: -inf");
  1999. // Non-cannonical Nan will be correctly normalized.
  2000. {
  2001. TestAllTypesProto3 message;
  2002. message.set_optional_double(
  2003. WireFormatLite::DecodeDouble(0x7FFA123456789ABCLL));
  2004. RunValidJsonTestWithProtobufInput(
  2005. "DoubleFieldNormalizeQuietNan", REQUIRED, message,
  2006. "optional_double: nan");
  2007. message.set_optional_double(
  2008. WireFormatLite::DecodeDouble(0xFFFBCBA987654321LL));
  2009. RunValidJsonTestWithProtobufInput(
  2010. "DoubleFieldNormalizeSignalingNan", REQUIRED, message,
  2011. "optional_double: nan");
  2012. }
  2013. // Special values must be quoted.
  2014. ExpectParseFailureForJson(
  2015. "DoubleFieldNanNotQuoted", RECOMMENDED,
  2016. R"({"optionalDouble": NaN})");
  2017. ExpectParseFailureForJson(
  2018. "DoubleFieldInfinityNotQuoted", RECOMMENDED,
  2019. R"({"optionalDouble": Infinity})");
  2020. ExpectParseFailureForJson(
  2021. "DoubleFieldNegativeInfinityNotQuoted", RECOMMENDED,
  2022. R"({"optionalDouble": -Infinity})");
  2023. // Parsers should reject out-of-bound values.
  2024. ExpectParseFailureForJson(
  2025. "DoubleFieldTooSmall", REQUIRED,
  2026. R"({"optionalDouble": -1.89769e+308})");
  2027. ExpectParseFailureForJson(
  2028. "DoubleFieldTooLarge", REQUIRED,
  2029. R"({"optionalDouble": +1.89769e+308})");
  2030. // Enum fields.
  2031. RunValidJsonTest(
  2032. "EnumField", REQUIRED,
  2033. R"({"optionalNestedEnum": "FOO"})",
  2034. "optional_nested_enum: FOO");
  2035. // Enum fields with alias
  2036. RunValidJsonTest(
  2037. "EnumFieldWithAlias", REQUIRED,
  2038. R"({"optionalAliasedEnum": "ALIAS_BAZ"})",
  2039. "optional_aliased_enum: ALIAS_BAZ");
  2040. RunValidJsonTest(
  2041. "EnumFieldWithAliasUseAlias", REQUIRED,
  2042. R"({"optionalAliasedEnum": "QUX"})",
  2043. "optional_aliased_enum: ALIAS_BAZ");
  2044. RunValidJsonTest(
  2045. "EnumFieldWithAliasLowerCase", REQUIRED,
  2046. R"({"optionalAliasedEnum": "qux"})",
  2047. "optional_aliased_enum: ALIAS_BAZ");
  2048. RunValidJsonTest(
  2049. "EnumFieldWithAliasDifferentCase", REQUIRED,
  2050. R"({"optionalAliasedEnum": "bAz"})",
  2051. "optional_aliased_enum: ALIAS_BAZ");
  2052. // Enum values must be represented as strings.
  2053. ExpectParseFailureForJson(
  2054. "EnumFieldNotQuoted", REQUIRED,
  2055. R"({"optionalNestedEnum": FOO})");
  2056. // Numeric values are allowed.
  2057. RunValidJsonTest(
  2058. "EnumFieldNumericValueZero", REQUIRED,
  2059. R"({"optionalNestedEnum": 0})",
  2060. "optional_nested_enum: FOO");
  2061. RunValidJsonTest(
  2062. "EnumFieldNumericValueNonZero", REQUIRED,
  2063. R"({"optionalNestedEnum": 1})",
  2064. "optional_nested_enum: BAR");
  2065. // Unknown enum values are represented as numeric values.
  2066. RunValidJsonTestWithValidator(
  2067. "EnumFieldUnknownValue", REQUIRED,
  2068. R"({"optionalNestedEnum": 123})",
  2069. [](const Json::Value& value) {
  2070. return value["optionalNestedEnum"].type() == Json::intValue &&
  2071. value["optionalNestedEnum"].asInt() == 123;
  2072. });
  2073. // String fields.
  2074. RunValidJsonTest(
  2075. "StringField", REQUIRED,
  2076. R"({"optionalString": "Hello world!"})",
  2077. "optional_string: \"Hello world!\"");
  2078. RunValidJsonTest(
  2079. "StringFieldUnicode", REQUIRED,
  2080. // Google in Chinese.
  2081. R"({"optionalString": "谷歌"})",
  2082. R"(optional_string: "谷歌")");
  2083. RunValidJsonTest(
  2084. "StringFieldEscape", REQUIRED,
  2085. R"({"optionalString": "\"\\\/\b\f\n\r\t"})",
  2086. R"(optional_string: "\"\\/\b\f\n\r\t")");
  2087. RunValidJsonTest(
  2088. "StringFieldUnicodeEscape", REQUIRED,
  2089. R"({"optionalString": "\u8C37\u6B4C"})",
  2090. R"(optional_string: "谷歌")");
  2091. RunValidJsonTest(
  2092. "StringFieldUnicodeEscapeWithLowercaseHexLetters", REQUIRED,
  2093. R"({"optionalString": "\u8c37\u6b4c"})",
  2094. R"(optional_string: "谷歌")");
  2095. RunValidJsonTest(
  2096. "StringFieldSurrogatePair", REQUIRED,
  2097. // The character is an emoji: grinning face with smiling eyes. 😁
  2098. R"({"optionalString": "\uD83D\uDE01"})",
  2099. R"(optional_string: "\xF0\x9F\x98\x81")");
  2100. // Unicode escapes must start with "\u" (lowercase u).
  2101. ExpectParseFailureForJson(
  2102. "StringFieldUppercaseEscapeLetter", RECOMMENDED,
  2103. R"({"optionalString": "\U8C37\U6b4C"})");
  2104. ExpectParseFailureForJson(
  2105. "StringFieldInvalidEscape", RECOMMENDED,
  2106. R"({"optionalString": "\uXXXX\u6B4C"})");
  2107. ExpectParseFailureForJson(
  2108. "StringFieldUnterminatedEscape", RECOMMENDED,
  2109. R"({"optionalString": "\u8C3"})");
  2110. ExpectParseFailureForJson(
  2111. "StringFieldUnpairedHighSurrogate", RECOMMENDED,
  2112. R"({"optionalString": "\uD800"})");
  2113. ExpectParseFailureForJson(
  2114. "StringFieldUnpairedLowSurrogate", RECOMMENDED,
  2115. R"({"optionalString": "\uDC00"})");
  2116. ExpectParseFailureForJson(
  2117. "StringFieldSurrogateInWrongOrder", RECOMMENDED,
  2118. R"({"optionalString": "\uDE01\uD83D"})");
  2119. ExpectParseFailureForJson(
  2120. "StringFieldNotAString", REQUIRED,
  2121. R"({"optionalString": 12345})");
  2122. // Bytes fields.
  2123. RunValidJsonTest(
  2124. "BytesField", REQUIRED,
  2125. R"({"optionalBytes": "AQI="})",
  2126. R"(optional_bytes: "\x01\x02")");
  2127. RunValidJsonTest(
  2128. "BytesFieldBase64Url", RECOMMENDED,
  2129. R"({"optionalBytes": "-_"})",
  2130. R"(optional_bytes: "\xfb")");
  2131. // Message fields.
  2132. RunValidJsonTest(
  2133. "MessageField", REQUIRED,
  2134. R"({"optionalNestedMessage": {"a": 1234}})",
  2135. "optional_nested_message: {a: 1234}");
  2136. // Oneof fields.
  2137. ExpectParseFailureForJson(
  2138. "OneofFieldDuplicate", REQUIRED,
  2139. R"({"oneofUint32": 1, "oneofString": "test"})");
  2140. // Ensure zero values for oneof make it out/backs.
  2141. TestAllTypesProto3 messageProto3;
  2142. TestAllTypesProto2 messageProto2;
  2143. TestOneofMessage(messageProto3, true);
  2144. TestOneofMessage(messageProto2, false);
  2145. RunValidJsonTest(
  2146. "OneofZeroUint32", RECOMMENDED,
  2147. R"({"oneofUint32": 0})", "oneof_uint32: 0");
  2148. RunValidJsonTest(
  2149. "OneofZeroMessage", RECOMMENDED,
  2150. R"({"oneofNestedMessage": {}})", "oneof_nested_message: {}");
  2151. RunValidJsonTest(
  2152. "OneofZeroString", RECOMMENDED,
  2153. R"({"oneofString": ""})", "oneof_string: \"\"");
  2154. RunValidJsonTest(
  2155. "OneofZeroBytes", RECOMMENDED,
  2156. R"({"oneofBytes": ""})", "oneof_bytes: \"\"");
  2157. RunValidJsonTest(
  2158. "OneofZeroBool", RECOMMENDED,
  2159. R"({"oneofBool": false})", "oneof_bool: false");
  2160. RunValidJsonTest(
  2161. "OneofZeroUint64", RECOMMENDED,
  2162. R"({"oneofUint64": 0})", "oneof_uint64: 0");
  2163. RunValidJsonTest(
  2164. "OneofZeroFloat", RECOMMENDED,
  2165. R"({"oneofFloat": 0.0})", "oneof_float: 0");
  2166. RunValidJsonTest(
  2167. "OneofZeroDouble", RECOMMENDED,
  2168. R"({"oneofDouble": 0.0})", "oneof_double: 0");
  2169. RunValidJsonTest(
  2170. "OneofZeroEnum", RECOMMENDED,
  2171. R"({"oneofEnum":"FOO"})", "oneof_enum: FOO");
  2172. // Map fields.
  2173. RunValidJsonTest(
  2174. "Int32MapField", REQUIRED,
  2175. R"({"mapInt32Int32": {"1": 2, "3": 4}})",
  2176. "map_int32_int32: {key: 1 value: 2}"
  2177. "map_int32_int32: {key: 3 value: 4}");
  2178. ExpectParseFailureForJson(
  2179. "Int32MapFieldKeyNotQuoted", RECOMMENDED,
  2180. R"({"mapInt32Int32": {1: 2, 3: 4}})");
  2181. RunValidJsonTest(
  2182. "Uint32MapField", REQUIRED,
  2183. R"({"mapUint32Uint32": {"1": 2, "3": 4}})",
  2184. "map_uint32_uint32: {key: 1 value: 2}"
  2185. "map_uint32_uint32: {key: 3 value: 4}");
  2186. ExpectParseFailureForJson(
  2187. "Uint32MapFieldKeyNotQuoted", RECOMMENDED,
  2188. R"({"mapUint32Uint32": {1: 2, 3: 4}})");
  2189. RunValidJsonTest(
  2190. "Int64MapField", REQUIRED,
  2191. R"({"mapInt64Int64": {"1": 2, "3": 4}})",
  2192. "map_int64_int64: {key: 1 value: 2}"
  2193. "map_int64_int64: {key: 3 value: 4}");
  2194. ExpectParseFailureForJson(
  2195. "Int64MapFieldKeyNotQuoted", RECOMMENDED,
  2196. R"({"mapInt64Int64": {1: 2, 3: 4}})");
  2197. RunValidJsonTest(
  2198. "Uint64MapField", REQUIRED,
  2199. R"({"mapUint64Uint64": {"1": 2, "3": 4}})",
  2200. "map_uint64_uint64: {key: 1 value: 2}"
  2201. "map_uint64_uint64: {key: 3 value: 4}");
  2202. ExpectParseFailureForJson(
  2203. "Uint64MapFieldKeyNotQuoted", RECOMMENDED,
  2204. R"({"mapUint64Uint64": {1: 2, 3: 4}})");
  2205. RunValidJsonTest(
  2206. "BoolMapField", REQUIRED,
  2207. R"({"mapBoolBool": {"true": true, "false": false}})",
  2208. "map_bool_bool: {key: true value: true}"
  2209. "map_bool_bool: {key: false value: false}");
  2210. ExpectParseFailureForJson(
  2211. "BoolMapFieldKeyNotQuoted", RECOMMENDED,
  2212. R"({"mapBoolBool": {true: true, false: false}})");
  2213. RunValidJsonTest(
  2214. "MessageMapField", REQUIRED,
  2215. R"({
  2216. "mapStringNestedMessage": {
  2217. "hello": {"a": 1234},
  2218. "world": {"a": 5678}
  2219. }
  2220. })",
  2221. R"(
  2222. map_string_nested_message: {
  2223. key: "hello"
  2224. value: {a: 1234}
  2225. }
  2226. map_string_nested_message: {
  2227. key: "world"
  2228. value: {a: 5678}
  2229. }
  2230. )");
  2231. // Since Map keys are represented as JSON strings, escaping should be allowed.
  2232. RunValidJsonTest(
  2233. "Int32MapEscapedKey", REQUIRED,
  2234. R"({"mapInt32Int32": {"\u0031": 2}})",
  2235. "map_int32_int32: {key: 1 value: 2}");
  2236. RunValidJsonTest(
  2237. "Int64MapEscapedKey", REQUIRED,
  2238. R"({"mapInt64Int64": {"\u0031": 2}})",
  2239. "map_int64_int64: {key: 1 value: 2}");
  2240. RunValidJsonTest(
  2241. "BoolMapEscapedKey", REQUIRED,
  2242. R"({"mapBoolBool": {"tr\u0075e": true}})",
  2243. "map_bool_bool: {key: true value: true}");
  2244. // http://www.rfc-editor.org/rfc/rfc7159.txt says strings have to use double
  2245. // quotes.
  2246. ExpectParseFailureForJson("StringFieldSingleQuoteKey", RECOMMENDED,
  2247. R"({'optionalString': "Hello world!"})");
  2248. ExpectParseFailureForJson("StringFieldSingleQuoteValue", RECOMMENDED,
  2249. R"({"optionalString": 'Hello world!'})");
  2250. ExpectParseFailureForJson("StringFieldSingleQuoteBoth", RECOMMENDED,
  2251. R"({'optionalString': 'Hello world!'})");
  2252. }
  2253. void BinaryAndJsonConformanceSuite::RunJsonTestsForRepeatedTypes() {
  2254. // Repeated fields.
  2255. RunValidJsonTest("PrimitiveRepeatedField", REQUIRED,
  2256. R"({"repeatedInt32": [1, 2, 3, 4]})",
  2257. "repeated_int32: [1, 2, 3, 4]");
  2258. RunValidJsonTest("EnumRepeatedField", REQUIRED,
  2259. R"({"repeatedNestedEnum": ["FOO", "BAR", "BAZ"]})",
  2260. "repeated_nested_enum: [FOO, BAR, BAZ]");
  2261. RunValidJsonTest("StringRepeatedField", REQUIRED,
  2262. R"({"repeatedString": ["Hello", "world"]})",
  2263. R"(repeated_string: ["Hello", "world"])");
  2264. RunValidJsonTest("BytesRepeatedField", REQUIRED,
  2265. R"({"repeatedBytes": ["AAEC", "AQI="]})",
  2266. R"(repeated_bytes: ["\x00\x01\x02", "\x01\x02"])");
  2267. RunValidJsonTest("MessageRepeatedField", REQUIRED,
  2268. R"({"repeatedNestedMessage": [{"a": 1234}, {"a": 5678}]})",
  2269. "repeated_nested_message: {a: 1234}"
  2270. "repeated_nested_message: {a: 5678}");
  2271. // Repeated field elements are of incorrect type.
  2272. ExpectParseFailureForJson(
  2273. "RepeatedFieldWrongElementTypeExpectingIntegersGotBool", REQUIRED,
  2274. R"({"repeatedInt32": [1, false, 3, 4]})");
  2275. ExpectParseFailureForJson(
  2276. "RepeatedFieldWrongElementTypeExpectingIntegersGotString", REQUIRED,
  2277. R"({"repeatedInt32": [1, 2, "name", 4]})");
  2278. ExpectParseFailureForJson(
  2279. "RepeatedFieldWrongElementTypeExpectingIntegersGotMessage", REQUIRED,
  2280. R"({"repeatedInt32": [1, 2, 3, {"a": 4}]})");
  2281. ExpectParseFailureForJson(
  2282. "RepeatedFieldWrongElementTypeExpectingStringsGotInt", REQUIRED,
  2283. R"({"repeatedString": ["1", 2, "3", "4"]})");
  2284. ExpectParseFailureForJson(
  2285. "RepeatedFieldWrongElementTypeExpectingStringsGotBool", REQUIRED,
  2286. R"({"repeatedString": ["1", "2", false, "4"]})");
  2287. ExpectParseFailureForJson(
  2288. "RepeatedFieldWrongElementTypeExpectingStringsGotMessage", REQUIRED,
  2289. R"({"repeatedString": ["1", 2, "3", {"a": 4}]})");
  2290. ExpectParseFailureForJson(
  2291. "RepeatedFieldWrongElementTypeExpectingMessagesGotInt", REQUIRED,
  2292. R"({"repeatedNestedMessage": [{"a": 1}, 2]})");
  2293. ExpectParseFailureForJson(
  2294. "RepeatedFieldWrongElementTypeExpectingMessagesGotBool", REQUIRED,
  2295. R"({"repeatedNestedMessage": [{"a": 1}, false]})");
  2296. ExpectParseFailureForJson(
  2297. "RepeatedFieldWrongElementTypeExpectingMessagesGotString", REQUIRED,
  2298. R"({"repeatedNestedMessage": [{"a": 1}, "2"]})");
  2299. // Trailing comma in the repeated field is not allowed.
  2300. ExpectParseFailureForJson("RepeatedFieldTrailingComma", RECOMMENDED,
  2301. R"({"repeatedInt32": [1, 2, 3, 4,]})");
  2302. ExpectParseFailureForJson("RepeatedFieldTrailingCommaWithSpace", RECOMMENDED,
  2303. "{\"repeatedInt32\": [1, 2, 3, 4 ,]}");
  2304. ExpectParseFailureForJson("RepeatedFieldTrailingCommaWithSpaceCommaSpace",
  2305. RECOMMENDED,
  2306. "{\"repeatedInt32\": [1, 2, 3, 4 , ]}");
  2307. ExpectParseFailureForJson(
  2308. "RepeatedFieldTrailingCommaWithNewlines", RECOMMENDED,
  2309. "{\"repeatedInt32\": [\n 1,\n 2,\n 3,\n 4,\n]}");
  2310. }
  2311. void BinaryAndJsonConformanceSuite::RunJsonTestsForNullTypes() {
  2312. // "null" is accepted for all fields types.
  2313. RunValidJsonTest(
  2314. "AllFieldAcceptNull", REQUIRED,
  2315. R"({
  2316. "optionalInt32": null,
  2317. "optionalInt64": null,
  2318. "optionalUint32": null,
  2319. "optionalUint64": null,
  2320. "optionalSint32": null,
  2321. "optionalSint64": null,
  2322. "optionalFixed32": null,
  2323. "optionalFixed64": null,
  2324. "optionalSfixed32": null,
  2325. "optionalSfixed64": null,
  2326. "optionalFloat": null,
  2327. "optionalDouble": null,
  2328. "optionalBool": null,
  2329. "optionalString": null,
  2330. "optionalBytes": null,
  2331. "optionalNestedEnum": null,
  2332. "optionalNestedMessage": null,
  2333. "repeatedInt32": null,
  2334. "repeatedInt64": null,
  2335. "repeatedUint32": null,
  2336. "repeatedUint64": null,
  2337. "repeatedSint32": null,
  2338. "repeatedSint64": null,
  2339. "repeatedFixed32": null,
  2340. "repeatedFixed64": null,
  2341. "repeatedSfixed32": null,
  2342. "repeatedSfixed64": null,
  2343. "repeatedFloat": null,
  2344. "repeatedDouble": null,
  2345. "repeatedBool": null,
  2346. "repeatedString": null,
  2347. "repeatedBytes": null,
  2348. "repeatedNestedEnum": null,
  2349. "repeatedNestedMessage": null,
  2350. "mapInt32Int32": null,
  2351. "mapBoolBool": null,
  2352. "mapStringNestedMessage": null
  2353. })",
  2354. "");
  2355. // Repeated field elements cannot be null.
  2356. ExpectParseFailureForJson(
  2357. "RepeatedFieldPrimitiveElementIsNull", RECOMMENDED,
  2358. R"({"repeatedInt32": [1, null, 2]})");
  2359. ExpectParseFailureForJson(
  2360. "RepeatedFieldMessageElementIsNull", RECOMMENDED,
  2361. R"({"repeatedNestedMessage": [{"a":1}, null, {"a":2}]})");
  2362. // Map field keys cannot be null.
  2363. ExpectParseFailureForJson(
  2364. "MapFieldKeyIsNull", RECOMMENDED,
  2365. R"({"mapInt32Int32": {null: 1}})");
  2366. // Map field values cannot be null.
  2367. ExpectParseFailureForJson(
  2368. "MapFieldValueIsNull", RECOMMENDED,
  2369. R"({"mapInt32Int32": {"0": null}})");
  2370. }
  2371. void BinaryAndJsonConformanceSuite::RunJsonTestsForWrapperTypes() {
  2372. RunValidJsonTest("OptionalBoolWrapper", REQUIRED,
  2373. R"({"optionalBoolWrapper": false})",
  2374. "optional_bool_wrapper: {value: false}");
  2375. RunValidJsonTest(
  2376. "OptionalInt32Wrapper", REQUIRED,
  2377. R"({"optionalInt32Wrapper": 0})",
  2378. "optional_int32_wrapper: {value: 0}");
  2379. RunValidJsonTest(
  2380. "OptionalUint32Wrapper", REQUIRED,
  2381. R"({"optionalUint32Wrapper": 0})",
  2382. "optional_uint32_wrapper: {value: 0}");
  2383. RunValidJsonTest(
  2384. "OptionalInt64Wrapper", REQUIRED,
  2385. R"({"optionalInt64Wrapper": 0})",
  2386. "optional_int64_wrapper: {value: 0}");
  2387. RunValidJsonTest(
  2388. "OptionalUint64Wrapper", REQUIRED,
  2389. R"({"optionalUint64Wrapper": 0})",
  2390. "optional_uint64_wrapper: {value: 0}");
  2391. RunValidJsonTest(
  2392. "OptionalFloatWrapper", REQUIRED,
  2393. R"({"optionalFloatWrapper": 0})",
  2394. "optional_float_wrapper: {value: 0}");
  2395. RunValidJsonTest(
  2396. "OptionalDoubleWrapper", REQUIRED,
  2397. R"({"optionalDoubleWrapper": 0})",
  2398. "optional_double_wrapper: {value: 0}");
  2399. RunValidJsonTest(
  2400. "OptionalStringWrapper", REQUIRED,
  2401. R"({"optionalStringWrapper": ""})",
  2402. R"(optional_string_wrapper: {value: ""})");
  2403. RunValidJsonTest(
  2404. "OptionalBytesWrapper", REQUIRED,
  2405. R"({"optionalBytesWrapper": ""})",
  2406. R"(optional_bytes_wrapper: {value: ""})");
  2407. RunValidJsonTest(
  2408. "OptionalWrapperTypesWithNonDefaultValue", REQUIRED,
  2409. R"({
  2410. "optionalBoolWrapper": true,
  2411. "optionalInt32Wrapper": 1,
  2412. "optionalUint32Wrapper": 1,
  2413. "optionalInt64Wrapper": "1",
  2414. "optionalUint64Wrapper": "1",
  2415. "optionalFloatWrapper": 1,
  2416. "optionalDoubleWrapper": 1,
  2417. "optionalStringWrapper": "1",
  2418. "optionalBytesWrapper": "AQI="
  2419. })",
  2420. R"(
  2421. optional_bool_wrapper: {value: true}
  2422. optional_int32_wrapper: {value: 1}
  2423. optional_uint32_wrapper: {value: 1}
  2424. optional_int64_wrapper: {value: 1}
  2425. optional_uint64_wrapper: {value: 1}
  2426. optional_float_wrapper: {value: 1}
  2427. optional_double_wrapper: {value: 1}
  2428. optional_string_wrapper: {value: "1"}
  2429. optional_bytes_wrapper: {value: "\x01\x02"}
  2430. )");
  2431. RunValidJsonTest(
  2432. "RepeatedBoolWrapper", REQUIRED,
  2433. R"({"repeatedBoolWrapper": [true, false]})",
  2434. "repeated_bool_wrapper: {value: true}"
  2435. "repeated_bool_wrapper: {value: false}");
  2436. RunValidJsonTest(
  2437. "RepeatedInt32Wrapper", REQUIRED,
  2438. R"({"repeatedInt32Wrapper": [0, 1]})",
  2439. "repeated_int32_wrapper: {value: 0}"
  2440. "repeated_int32_wrapper: {value: 1}");
  2441. RunValidJsonTest(
  2442. "RepeatedUint32Wrapper", REQUIRED,
  2443. R"({"repeatedUint32Wrapper": [0, 1]})",
  2444. "repeated_uint32_wrapper: {value: 0}"
  2445. "repeated_uint32_wrapper: {value: 1}");
  2446. RunValidJsonTest(
  2447. "RepeatedInt64Wrapper", REQUIRED,
  2448. R"({"repeatedInt64Wrapper": [0, 1]})",
  2449. "repeated_int64_wrapper: {value: 0}"
  2450. "repeated_int64_wrapper: {value: 1}");
  2451. RunValidJsonTest(
  2452. "RepeatedUint64Wrapper", REQUIRED,
  2453. R"({"repeatedUint64Wrapper": [0, 1]})",
  2454. "repeated_uint64_wrapper: {value: 0}"
  2455. "repeated_uint64_wrapper: {value: 1}");
  2456. RunValidJsonTest(
  2457. "RepeatedFloatWrapper", REQUIRED,
  2458. R"({"repeatedFloatWrapper": [0, 1]})",
  2459. "repeated_float_wrapper: {value: 0}"
  2460. "repeated_float_wrapper: {value: 1}");
  2461. RunValidJsonTest(
  2462. "RepeatedDoubleWrapper", REQUIRED,
  2463. R"({"repeatedDoubleWrapper": [0, 1]})",
  2464. "repeated_double_wrapper: {value: 0}"
  2465. "repeated_double_wrapper: {value: 1}");
  2466. RunValidJsonTest(
  2467. "RepeatedStringWrapper", REQUIRED,
  2468. R"({"repeatedStringWrapper": ["", "AQI="]})",
  2469. R"(
  2470. repeated_string_wrapper: {value: ""}
  2471. repeated_string_wrapper: {value: "AQI="}
  2472. )");
  2473. RunValidJsonTest(
  2474. "RepeatedBytesWrapper", REQUIRED,
  2475. R"({"repeatedBytesWrapper": ["", "AQI="]})",
  2476. R"(
  2477. repeated_bytes_wrapper: {value: ""}
  2478. repeated_bytes_wrapper: {value: "\x01\x02"}
  2479. )");
  2480. RunValidJsonTest(
  2481. "WrapperTypesWithNullValue", REQUIRED,
  2482. R"({
  2483. "optionalBoolWrapper": null,
  2484. "optionalInt32Wrapper": null,
  2485. "optionalUint32Wrapper": null,
  2486. "optionalInt64Wrapper": null,
  2487. "optionalUint64Wrapper": null,
  2488. "optionalFloatWrapper": null,
  2489. "optionalDoubleWrapper": null,
  2490. "optionalStringWrapper": null,
  2491. "optionalBytesWrapper": null,
  2492. "repeatedBoolWrapper": null,
  2493. "repeatedInt32Wrapper": null,
  2494. "repeatedUint32Wrapper": null,
  2495. "repeatedInt64Wrapper": null,
  2496. "repeatedUint64Wrapper": null,
  2497. "repeatedFloatWrapper": null,
  2498. "repeatedDoubleWrapper": null,
  2499. "repeatedStringWrapper": null,
  2500. "repeatedBytesWrapper": null
  2501. })",
  2502. "");
  2503. // Duration
  2504. RunValidJsonTest(
  2505. "DurationMinValue", REQUIRED,
  2506. R"({"optionalDuration": "-315576000000.999999999s"})",
  2507. "optional_duration: {seconds: -315576000000 nanos: -999999999}");
  2508. RunValidJsonTest(
  2509. "DurationMaxValue", REQUIRED,
  2510. R"({"optionalDuration": "315576000000.999999999s"})",
  2511. "optional_duration: {seconds: 315576000000 nanos: 999999999}");
  2512. RunValidJsonTest(
  2513. "DurationRepeatedValue", REQUIRED,
  2514. R"({"repeatedDuration": ["1.5s", "-1.5s"]})",
  2515. "repeated_duration: {seconds: 1 nanos: 500000000}"
  2516. "repeated_duration: {seconds: -1 nanos: -500000000}");
  2517. RunValidJsonTest(
  2518. "DurationNull", REQUIRED,
  2519. R"({"optionalDuration": null})",
  2520. "");
  2521. ExpectParseFailureForJson(
  2522. "DurationMissingS", REQUIRED,
  2523. R"({"optionalDuration": "1"})");
  2524. ExpectParseFailureForJson(
  2525. "DurationJsonInputTooSmall", REQUIRED,
  2526. R"({"optionalDuration": "-315576000001.000000000s"})");
  2527. ExpectParseFailureForJson(
  2528. "DurationJsonInputTooLarge", REQUIRED,
  2529. R"({"optionalDuration": "315576000001.000000000s"})");
  2530. ExpectSerializeFailureForJson(
  2531. "DurationProtoInputTooSmall", REQUIRED,
  2532. "optional_duration: {seconds: -315576000001 nanos: 0}");
  2533. ExpectSerializeFailureForJson(
  2534. "DurationProtoInputTooLarge", REQUIRED,
  2535. "optional_duration: {seconds: 315576000001 nanos: 0}");
  2536. RunValidJsonTestWithValidator(
  2537. "DurationHasZeroFractionalDigit", RECOMMENDED,
  2538. R"({"optionalDuration": "1.000000000s"})",
  2539. [](const Json::Value& value) {
  2540. return value["optionalDuration"].asString() == "1s";
  2541. });
  2542. RunValidJsonTestWithValidator(
  2543. "DurationHas3FractionalDigits", RECOMMENDED,
  2544. R"({"optionalDuration": "1.010000000s"})",
  2545. [](const Json::Value& value) {
  2546. return value["optionalDuration"].asString() == "1.010s";
  2547. });
  2548. RunValidJsonTestWithValidator(
  2549. "DurationHas6FractionalDigits", RECOMMENDED,
  2550. R"({"optionalDuration": "1.000010000s"})",
  2551. [](const Json::Value& value) {
  2552. return value["optionalDuration"].asString() == "1.000010s";
  2553. });
  2554. RunValidJsonTestWithValidator(
  2555. "DurationHas9FractionalDigits", RECOMMENDED,
  2556. R"({"optionalDuration": "1.000000010s"})",
  2557. [](const Json::Value& value) {
  2558. return value["optionalDuration"].asString() == "1.000000010s";
  2559. });
  2560. // Timestamp
  2561. RunValidJsonTest(
  2562. "TimestampMinValue", REQUIRED,
  2563. R"({"optionalTimestamp": "0001-01-01T00:00:00Z"})",
  2564. "optional_timestamp: {seconds: -62135596800}");
  2565. RunValidJsonTest(
  2566. "TimestampMaxValue", REQUIRED,
  2567. R"({"optionalTimestamp": "9999-12-31T23:59:59.999999999Z"})",
  2568. "optional_timestamp: {seconds: 253402300799 nanos: 999999999}");
  2569. RunValidJsonTest(
  2570. "TimestampRepeatedValue", REQUIRED,
  2571. R"({
  2572. "repeatedTimestamp": [
  2573. "0001-01-01T00:00:00Z",
  2574. "9999-12-31T23:59:59.999999999Z"
  2575. ]
  2576. })",
  2577. "repeated_timestamp: {seconds: -62135596800}"
  2578. "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}");
  2579. RunValidJsonTest("TimestampLeap", REQUIRED,
  2580. R"({"optionalTimestamp": "1993-02-10T00:00:00.000Z"})",
  2581. "optional_timestamp: {seconds: 729302400}");
  2582. RunValidJsonTest("TimestampWithPositiveOffset", REQUIRED,
  2583. R"({"optionalTimestamp": "1970-01-01T08:00:01+08:00"})",
  2584. "optional_timestamp: {seconds: 1}");
  2585. RunValidJsonTest("TimestampWithNegativeOffset", REQUIRED,
  2586. R"({"optionalTimestamp": "1969-12-31T16:00:01-08:00"})",
  2587. "optional_timestamp: {seconds: 1}");
  2588. RunValidJsonTest(
  2589. "TimestampNull", REQUIRED,
  2590. R"({"optionalTimestamp": null})",
  2591. "");
  2592. ExpectParseFailureForJson(
  2593. "TimestampJsonInputTooSmall", REQUIRED,
  2594. R"({"optionalTimestamp": "0000-01-01T00:00:00Z"})");
  2595. ExpectParseFailureForJson(
  2596. "TimestampJsonInputTooLarge", REQUIRED,
  2597. R"({"optionalTimestamp": "10000-01-01T00:00:00Z"})");
  2598. ExpectParseFailureForJson(
  2599. "TimestampJsonInputMissingZ", REQUIRED,
  2600. R"({"optionalTimestamp": "0001-01-01T00:00:00"})");
  2601. ExpectParseFailureForJson(
  2602. "TimestampJsonInputMissingT", REQUIRED,
  2603. R"({"optionalTimestamp": "0001-01-01 00:00:00Z"})");
  2604. ExpectParseFailureForJson(
  2605. "TimestampJsonInputLowercaseZ", REQUIRED,
  2606. R"({"optionalTimestamp": "0001-01-01T00:00:00z"})");
  2607. ExpectParseFailureForJson(
  2608. "TimestampJsonInputLowercaseT", REQUIRED,
  2609. R"({"optionalTimestamp": "0001-01-01t00:00:00Z"})");
  2610. ExpectSerializeFailureForJson(
  2611. "TimestampProtoInputTooSmall", REQUIRED,
  2612. "optional_timestamp: {seconds: -62135596801}");
  2613. ExpectSerializeFailureForJson(
  2614. "TimestampProtoInputTooLarge", REQUIRED,
  2615. "optional_timestamp: {seconds: 253402300800}");
  2616. RunValidJsonTestWithValidator(
  2617. "TimestampZeroNormalized", RECOMMENDED,
  2618. R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
  2619. [](const Json::Value& value) {
  2620. return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
  2621. });
  2622. RunValidJsonTestWithValidator(
  2623. "TimestampHasZeroFractionalDigit", RECOMMENDED,
  2624. R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
  2625. [](const Json::Value& value) {
  2626. return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
  2627. });
  2628. RunValidJsonTestWithValidator(
  2629. "TimestampHas3FractionalDigits", RECOMMENDED,
  2630. R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
  2631. [](const Json::Value& value) {
  2632. return value["optionalTimestamp"].asString() ==
  2633. "1970-01-01T00:00:00.010Z";
  2634. });
  2635. RunValidJsonTestWithValidator(
  2636. "TimestampHas6FractionalDigits", RECOMMENDED,
  2637. R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
  2638. [](const Json::Value& value) {
  2639. return value["optionalTimestamp"].asString() ==
  2640. "1970-01-01T00:00:00.000010Z";
  2641. });
  2642. RunValidJsonTestWithValidator(
  2643. "TimestampHas9FractionalDigits", RECOMMENDED,
  2644. R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
  2645. [](const Json::Value& value) {
  2646. return value["optionalTimestamp"].asString() ==
  2647. "1970-01-01T00:00:00.000000010Z";
  2648. });
  2649. }
  2650. void BinaryAndJsonConformanceSuite::RunJsonTestsForFieldMask() {
  2651. RunValidJsonTest(
  2652. "FieldMask", REQUIRED,
  2653. R"({"optionalFieldMask": "foo,barBaz"})",
  2654. R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
  2655. RunValidJsonTest(
  2656. "EmptyFieldMask", REQUIRED,
  2657. R"({"optionalFieldMask": ""})",
  2658. R"(optional_field_mask: {})");
  2659. ExpectParseFailureForJson(
  2660. "FieldMaskInvalidCharacter", RECOMMENDED,
  2661. R"({"optionalFieldMask": "foo,bar_bar"})");
  2662. ExpectSerializeFailureForJson(
  2663. "FieldMaskPathsDontRoundTrip", RECOMMENDED,
  2664. R"(optional_field_mask: {paths: "fooBar"})");
  2665. ExpectSerializeFailureForJson(
  2666. "FieldMaskNumbersDontRoundTrip", RECOMMENDED,
  2667. R"(optional_field_mask: {paths: "foo_3_bar"})");
  2668. ExpectSerializeFailureForJson(
  2669. "FieldMaskTooManyUnderscore", RECOMMENDED,
  2670. R"(optional_field_mask: {paths: "foo__bar"})");
  2671. }
  2672. void BinaryAndJsonConformanceSuite::RunJsonTestsForStruct() {
  2673. RunValidJsonTest(
  2674. "Struct", REQUIRED,
  2675. R"({
  2676. "optionalStruct": {
  2677. "nullValue": null,
  2678. "intValue": 1234,
  2679. "boolValue": true,
  2680. "doubleValue": 1234.5678,
  2681. "stringValue": "Hello world!",
  2682. "listValue": [1234, "5678"],
  2683. "objectValue": {
  2684. "value": 0
  2685. }
  2686. }
  2687. })",
  2688. R"(
  2689. optional_struct: {
  2690. fields: {
  2691. key: "nullValue"
  2692. value: {null_value: NULL_VALUE}
  2693. }
  2694. fields: {
  2695. key: "intValue"
  2696. value: {number_value: 1234}
  2697. }
  2698. fields: {
  2699. key: "boolValue"
  2700. value: {bool_value: true}
  2701. }
  2702. fields: {
  2703. key: "doubleValue"
  2704. value: {number_value: 1234.5678}
  2705. }
  2706. fields: {
  2707. key: "stringValue"
  2708. value: {string_value: "Hello world!"}
  2709. }
  2710. fields: {
  2711. key: "listValue"
  2712. value: {
  2713. list_value: {
  2714. values: {
  2715. number_value: 1234
  2716. }
  2717. values: {
  2718. string_value: "5678"
  2719. }
  2720. }
  2721. }
  2722. }
  2723. fields: {
  2724. key: "objectValue"
  2725. value: {
  2726. struct_value: {
  2727. fields: {
  2728. key: "value"
  2729. value: {
  2730. number_value: 0
  2731. }
  2732. }
  2733. }
  2734. }
  2735. }
  2736. }
  2737. )");
  2738. RunValidJsonTest(
  2739. "StructWithEmptyListValue", REQUIRED,
  2740. R"({
  2741. "optionalStruct": {
  2742. "listValue": []
  2743. }
  2744. })",
  2745. R"(
  2746. optional_struct: {
  2747. fields: {
  2748. key: "listValue"
  2749. value: {
  2750. list_value: {
  2751. }
  2752. }
  2753. }
  2754. }
  2755. )");
  2756. }
  2757. void BinaryAndJsonConformanceSuite::RunJsonTestsForValue() {
  2758. RunValidJsonTest(
  2759. "ValueAcceptInteger", REQUIRED,
  2760. R"({"optionalValue": 1})",
  2761. "optional_value: { number_value: 1}");
  2762. RunValidJsonTest(
  2763. "ValueAcceptFloat", REQUIRED,
  2764. R"({"optionalValue": 1.5})",
  2765. "optional_value: { number_value: 1.5}");
  2766. RunValidJsonTest(
  2767. "ValueAcceptBool", REQUIRED,
  2768. R"({"optionalValue": false})",
  2769. "optional_value: { bool_value: false}");
  2770. RunValidJsonTest(
  2771. "ValueAcceptNull", REQUIRED,
  2772. R"({"optionalValue": null})",
  2773. "optional_value: { null_value: NULL_VALUE}");
  2774. RunValidJsonTest(
  2775. "ValueAcceptString", REQUIRED,
  2776. R"({"optionalValue": "hello"})",
  2777. R"(optional_value: { string_value: "hello"})");
  2778. RunValidJsonTest(
  2779. "ValueAcceptList", REQUIRED,
  2780. R"({"optionalValue": [0, "hello"]})",
  2781. R"(
  2782. optional_value: {
  2783. list_value: {
  2784. values: {
  2785. number_value: 0
  2786. }
  2787. values: {
  2788. string_value: "hello"
  2789. }
  2790. }
  2791. }
  2792. )");
  2793. RunValidJsonTest(
  2794. "ValueAcceptObject", REQUIRED,
  2795. R"({"optionalValue": {"value": 1}})",
  2796. R"(
  2797. optional_value: {
  2798. struct_value: {
  2799. fields: {
  2800. key: "value"
  2801. value: {
  2802. number_value: 1
  2803. }
  2804. }
  2805. }
  2806. }
  2807. )");
  2808. RunValidJsonTest(
  2809. "RepeatedValue", REQUIRED,
  2810. R"({
  2811. "repeatedValue": [["a"]]
  2812. })",
  2813. R"(
  2814. repeated_value: [
  2815. {
  2816. list_value: {
  2817. values: [
  2818. { string_value: "a"}
  2819. ]
  2820. }
  2821. }
  2822. ]
  2823. )");
  2824. RunValidJsonTest(
  2825. "RepeatedListValue", REQUIRED,
  2826. R"({
  2827. "repeatedListValue": [["a"]]
  2828. })",
  2829. R"(
  2830. repeated_list_value: [
  2831. {
  2832. values: [
  2833. { string_value: "a"}
  2834. ]
  2835. }
  2836. ]
  2837. )");
  2838. }
  2839. void BinaryAndJsonConformanceSuite::RunJsonTestsForAny() {
  2840. RunValidJsonTest(
  2841. "Any", REQUIRED,
  2842. R"({
  2843. "optionalAny": {
  2844. "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3",
  2845. "optionalInt32": 12345
  2846. }
  2847. })",
  2848. R"(
  2849. optional_any: {
  2850. [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
  2851. optional_int32: 12345
  2852. }
  2853. }
  2854. )");
  2855. RunValidJsonTest(
  2856. "AnyNested", REQUIRED,
  2857. R"({
  2858. "optionalAny": {
  2859. "@type": "type.googleapis.com/google.protobuf.Any",
  2860. "value": {
  2861. "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3",
  2862. "optionalInt32": 12345
  2863. }
  2864. }
  2865. })",
  2866. R"(
  2867. optional_any: {
  2868. [type.googleapis.com/google.protobuf.Any] {
  2869. [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
  2870. optional_int32: 12345
  2871. }
  2872. }
  2873. }
  2874. )");
  2875. // The special "@type" tag is not required to appear first.
  2876. RunValidJsonTest(
  2877. "AnyUnorderedTypeTag", REQUIRED,
  2878. R"({
  2879. "optionalAny": {
  2880. "optionalInt32": 12345,
  2881. "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3"
  2882. }
  2883. })",
  2884. R"(
  2885. optional_any: {
  2886. [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
  2887. optional_int32: 12345
  2888. }
  2889. }
  2890. )");
  2891. // Well-known types in Any.
  2892. RunValidJsonTest(
  2893. "AnyWithInt32ValueWrapper", REQUIRED,
  2894. R"({
  2895. "optionalAny": {
  2896. "@type": "type.googleapis.com/google.protobuf.Int32Value",
  2897. "value": 12345
  2898. }
  2899. })",
  2900. R"(
  2901. optional_any: {
  2902. [type.googleapis.com/google.protobuf.Int32Value] {
  2903. value: 12345
  2904. }
  2905. }
  2906. )");
  2907. RunValidJsonTest(
  2908. "AnyWithDuration", REQUIRED,
  2909. R"({
  2910. "optionalAny": {
  2911. "@type": "type.googleapis.com/google.protobuf.Duration",
  2912. "value": "1.5s"
  2913. }
  2914. })",
  2915. R"(
  2916. optional_any: {
  2917. [type.googleapis.com/google.protobuf.Duration] {
  2918. seconds: 1
  2919. nanos: 500000000
  2920. }
  2921. }
  2922. )");
  2923. RunValidJsonTest(
  2924. "AnyWithTimestamp", REQUIRED,
  2925. R"({
  2926. "optionalAny": {
  2927. "@type": "type.googleapis.com/google.protobuf.Timestamp",
  2928. "value": "1970-01-01T00:00:00Z"
  2929. }
  2930. })",
  2931. R"(
  2932. optional_any: {
  2933. [type.googleapis.com/google.protobuf.Timestamp] {
  2934. seconds: 0
  2935. nanos: 0
  2936. }
  2937. }
  2938. )");
  2939. RunValidJsonTest(
  2940. "AnyWithFieldMask", REQUIRED,
  2941. R"({
  2942. "optionalAny": {
  2943. "@type": "type.googleapis.com/google.protobuf.FieldMask",
  2944. "value": "foo,barBaz"
  2945. }
  2946. })",
  2947. R"(
  2948. optional_any: {
  2949. [type.googleapis.com/google.protobuf.FieldMask] {
  2950. paths: ["foo", "bar_baz"]
  2951. }
  2952. }
  2953. )");
  2954. RunValidJsonTest(
  2955. "AnyWithStruct", REQUIRED,
  2956. R"({
  2957. "optionalAny": {
  2958. "@type": "type.googleapis.com/google.protobuf.Struct",
  2959. "value": {
  2960. "foo": 1
  2961. }
  2962. }
  2963. })",
  2964. R"(
  2965. optional_any: {
  2966. [type.googleapis.com/google.protobuf.Struct] {
  2967. fields: {
  2968. key: "foo"
  2969. value: {
  2970. number_value: 1
  2971. }
  2972. }
  2973. }
  2974. }
  2975. )");
  2976. RunValidJsonTest(
  2977. "AnyWithValueForJsonObject", REQUIRED,
  2978. R"({
  2979. "optionalAny": {
  2980. "@type": "type.googleapis.com/google.protobuf.Value",
  2981. "value": {
  2982. "foo": 1
  2983. }
  2984. }
  2985. })",
  2986. R"(
  2987. optional_any: {
  2988. [type.googleapis.com/google.protobuf.Value] {
  2989. struct_value: {
  2990. fields: {
  2991. key: "foo"
  2992. value: {
  2993. number_value: 1
  2994. }
  2995. }
  2996. }
  2997. }
  2998. }
  2999. )");
  3000. RunValidJsonTest(
  3001. "AnyWithValueForInteger", REQUIRED,
  3002. R"({
  3003. "optionalAny": {
  3004. "@type": "type.googleapis.com/google.protobuf.Value",
  3005. "value": 1
  3006. }
  3007. })",
  3008. R"(
  3009. optional_any: {
  3010. [type.googleapis.com/google.protobuf.Value] {
  3011. number_value: 1
  3012. }
  3013. }
  3014. )");
  3015. }
  3016. } // namespace protobuf
  3017. } // namespace google