binary_json_conformance_suite.cc 114 KB

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