binary_json_conformance_suite.cc 114 KB

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