binary_json_conformance_suite.cc 117 KB

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