descriptor_unittest.cc 291 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file makes extensive use of RFC 3092. :)
  35. #include <limits>
  36. #include <memory>
  37. #include <vector>
  38. #include <google/protobuf/compiler/importer.h>
  39. #include <google/protobuf/compiler/parser.h>
  40. #include <google/protobuf/unittest.pb.h>
  41. #include <google/protobuf/unittest_custom_options.pb.h>
  42. #include <google/protobuf/unittest_lazy_dependencies.pb.h>
  43. #include <google/protobuf/unittest_proto3_arena.pb.h>
  44. #include <google/protobuf/io/tokenizer.h>
  45. #include <google/protobuf/io/zero_copy_stream_impl.h>
  46. #include <google/protobuf/descriptor.pb.h>
  47. #include <google/protobuf/descriptor.h>
  48. #include <google/protobuf/descriptor_database.h>
  49. #include <google/protobuf/dynamic_message.h>
  50. #include <google/protobuf/text_format.h>
  51. #include <google/protobuf/stubs/substitute.h>
  52. #include <google/protobuf/stubs/common.h>
  53. #include <google/protobuf/stubs/logging.h>
  54. #include <google/protobuf/stubs/logging.h>
  55. #include <google/protobuf/stubs/stringprintf.h>
  56. #include <google/protobuf/stubs/strutil.h>
  57. #include <google/protobuf/testing/googletest.h>
  58. #include <gtest/gtest.h>
  59. #include <google/protobuf/port_def.inc>
  60. namespace google {
  61. namespace protobuf {
  62. // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
  63. namespace descriptor_unittest {
  64. // Some helpers to make assembling descriptors faster.
  65. DescriptorProto* AddMessage(FileDescriptorProto* file,
  66. const std::string& name) {
  67. DescriptorProto* result = file->add_message_type();
  68. result->set_name(name);
  69. return result;
  70. }
  71. DescriptorProto* AddNestedMessage(DescriptorProto* parent,
  72. const std::string& name) {
  73. DescriptorProto* result = parent->add_nested_type();
  74. result->set_name(name);
  75. return result;
  76. }
  77. EnumDescriptorProto* AddEnum(FileDescriptorProto* file,
  78. const std::string& name) {
  79. EnumDescriptorProto* result = file->add_enum_type();
  80. result->set_name(name);
  81. return result;
  82. }
  83. EnumDescriptorProto* AddNestedEnum(DescriptorProto* parent,
  84. const std::string& name) {
  85. EnumDescriptorProto* result = parent->add_enum_type();
  86. result->set_name(name);
  87. return result;
  88. }
  89. ServiceDescriptorProto* AddService(FileDescriptorProto* file,
  90. const std::string& name) {
  91. ServiceDescriptorProto* result = file->add_service();
  92. result->set_name(name);
  93. return result;
  94. }
  95. FieldDescriptorProto* AddField(DescriptorProto* parent, const std::string& name,
  96. int number, FieldDescriptorProto::Label label,
  97. FieldDescriptorProto::Type type) {
  98. FieldDescriptorProto* result = parent->add_field();
  99. result->set_name(name);
  100. result->set_number(number);
  101. result->set_label(label);
  102. result->set_type(type);
  103. return result;
  104. }
  105. FieldDescriptorProto* AddExtension(FileDescriptorProto* file,
  106. const std::string& extendee,
  107. const std::string& name, int number,
  108. FieldDescriptorProto::Label label,
  109. FieldDescriptorProto::Type type) {
  110. FieldDescriptorProto* result = file->add_extension();
  111. result->set_name(name);
  112. result->set_number(number);
  113. result->set_label(label);
  114. result->set_type(type);
  115. result->set_extendee(extendee);
  116. return result;
  117. }
  118. FieldDescriptorProto* AddNestedExtension(DescriptorProto* parent,
  119. const std::string& extendee,
  120. const std::string& name, int number,
  121. FieldDescriptorProto::Label label,
  122. FieldDescriptorProto::Type type) {
  123. FieldDescriptorProto* result = parent->add_extension();
  124. result->set_name(name);
  125. result->set_number(number);
  126. result->set_label(label);
  127. result->set_type(type);
  128. result->set_extendee(extendee);
  129. return result;
  130. }
  131. DescriptorProto::ExtensionRange* AddExtensionRange(DescriptorProto* parent,
  132. int start, int end) {
  133. DescriptorProto::ExtensionRange* result = parent->add_extension_range();
  134. result->set_start(start);
  135. result->set_end(end);
  136. return result;
  137. }
  138. DescriptorProto::ReservedRange* AddReservedRange(DescriptorProto* parent,
  139. int start, int end) {
  140. DescriptorProto::ReservedRange* result = parent->add_reserved_range();
  141. result->set_start(start);
  142. result->set_end(end);
  143. return result;
  144. }
  145. EnumDescriptorProto::EnumReservedRange* AddReservedRange(
  146. EnumDescriptorProto* parent, int start, int end) {
  147. EnumDescriptorProto::EnumReservedRange* result = parent->add_reserved_range();
  148. result->set_start(start);
  149. result->set_end(end);
  150. return result;
  151. }
  152. EnumValueDescriptorProto* AddEnumValue(EnumDescriptorProto* enum_proto,
  153. const std::string& name, int number) {
  154. EnumValueDescriptorProto* result = enum_proto->add_value();
  155. result->set_name(name);
  156. result->set_number(number);
  157. return result;
  158. }
  159. MethodDescriptorProto* AddMethod(ServiceDescriptorProto* service,
  160. const std::string& name,
  161. const std::string& input_type,
  162. const std::string& output_type) {
  163. MethodDescriptorProto* result = service->add_method();
  164. result->set_name(name);
  165. result->set_input_type(input_type);
  166. result->set_output_type(output_type);
  167. return result;
  168. }
  169. // Empty enums technically aren't allowed. We need to insert a dummy value
  170. // into them.
  171. void AddEmptyEnum(FileDescriptorProto* file, const std::string& name) {
  172. AddEnumValue(AddEnum(file, name), name + "_DUMMY", 1);
  173. }
  174. class MockErrorCollector : public DescriptorPool::ErrorCollector {
  175. public:
  176. MockErrorCollector() {}
  177. ~MockErrorCollector() {}
  178. std::string text_;
  179. std::string warning_text_;
  180. // implements ErrorCollector ---------------------------------------
  181. void AddError(const std::string& filename, const std::string& element_name,
  182. const Message* descriptor, ErrorLocation location,
  183. const std::string& message) {
  184. const char* location_name = nullptr;
  185. switch (location) {
  186. case NAME:
  187. location_name = "NAME";
  188. break;
  189. case NUMBER:
  190. location_name = "NUMBER";
  191. break;
  192. case TYPE:
  193. location_name = "TYPE";
  194. break;
  195. case EXTENDEE:
  196. location_name = "EXTENDEE";
  197. break;
  198. case DEFAULT_VALUE:
  199. location_name = "DEFAULT_VALUE";
  200. break;
  201. case OPTION_NAME:
  202. location_name = "OPTION_NAME";
  203. break;
  204. case OPTION_VALUE:
  205. location_name = "OPTION_VALUE";
  206. break;
  207. case INPUT_TYPE:
  208. location_name = "INPUT_TYPE";
  209. break;
  210. case OUTPUT_TYPE:
  211. location_name = "OUTPUT_TYPE";
  212. break;
  213. case IMPORT:
  214. location_name = "IMPORT";
  215. break;
  216. case OTHER:
  217. location_name = "OTHER";
  218. break;
  219. }
  220. strings::SubstituteAndAppend(&text_, "$0: $1: $2: $3\n", filename,
  221. element_name, location_name, message);
  222. }
  223. // implements ErrorCollector ---------------------------------------
  224. void AddWarning(const std::string& filename, const std::string& element_name,
  225. const Message* descriptor, ErrorLocation location,
  226. const std::string& message) {
  227. const char* location_name = nullptr;
  228. switch (location) {
  229. case NAME:
  230. location_name = "NAME";
  231. break;
  232. case NUMBER:
  233. location_name = "NUMBER";
  234. break;
  235. case TYPE:
  236. location_name = "TYPE";
  237. break;
  238. case EXTENDEE:
  239. location_name = "EXTENDEE";
  240. break;
  241. case DEFAULT_VALUE:
  242. location_name = "DEFAULT_VALUE";
  243. break;
  244. case OPTION_NAME:
  245. location_name = "OPTION_NAME";
  246. break;
  247. case OPTION_VALUE:
  248. location_name = "OPTION_VALUE";
  249. break;
  250. case INPUT_TYPE:
  251. location_name = "INPUT_TYPE";
  252. break;
  253. case OUTPUT_TYPE:
  254. location_name = "OUTPUT_TYPE";
  255. break;
  256. case IMPORT:
  257. location_name = "IMPORT";
  258. break;
  259. case OTHER:
  260. location_name = "OTHER";
  261. break;
  262. }
  263. strings::SubstituteAndAppend(&warning_text_, "$0: $1: $2: $3\n", filename,
  264. element_name, location_name, message);
  265. }
  266. };
  267. // ===================================================================
  268. // Test simple files.
  269. class FileDescriptorTest : public testing::Test {
  270. protected:
  271. virtual void SetUp() {
  272. // Build descriptors for the following definitions:
  273. //
  274. // // in "foo.proto"
  275. // message FooMessage { extensions 1; }
  276. // enum FooEnum {FOO_ENUM_VALUE = 1;}
  277. // service FooService {}
  278. // extend FooMessage { optional int32 foo_extension = 1; }
  279. //
  280. // // in "bar.proto"
  281. // package bar_package;
  282. // message BarMessage { extensions 1; }
  283. // enum BarEnum {BAR_ENUM_VALUE = 1;}
  284. // service BarService {}
  285. // extend BarMessage { optional int32 bar_extension = 1; }
  286. //
  287. // Also, we have an empty file "baz.proto". This file's purpose is to
  288. // make sure that even though it has the same package as foo.proto,
  289. // searching it for members of foo.proto won't work.
  290. FileDescriptorProto foo_file;
  291. foo_file.set_name("foo.proto");
  292. AddExtensionRange(AddMessage(&foo_file, "FooMessage"), 1, 2);
  293. AddEnumValue(AddEnum(&foo_file, "FooEnum"), "FOO_ENUM_VALUE", 1);
  294. AddService(&foo_file, "FooService");
  295. AddExtension(&foo_file, "FooMessage", "foo_extension", 1,
  296. FieldDescriptorProto::LABEL_OPTIONAL,
  297. FieldDescriptorProto::TYPE_INT32);
  298. FileDescriptorProto bar_file;
  299. bar_file.set_name("bar.proto");
  300. bar_file.set_package("bar_package");
  301. bar_file.add_dependency("foo.proto");
  302. AddExtensionRange(AddMessage(&bar_file, "BarMessage"), 1, 2);
  303. AddEnumValue(AddEnum(&bar_file, "BarEnum"), "BAR_ENUM_VALUE", 1);
  304. AddService(&bar_file, "BarService");
  305. AddExtension(&bar_file, "bar_package.BarMessage", "bar_extension", 1,
  306. FieldDescriptorProto::LABEL_OPTIONAL,
  307. FieldDescriptorProto::TYPE_INT32);
  308. FileDescriptorProto baz_file;
  309. baz_file.set_name("baz.proto");
  310. // Build the descriptors and get the pointers.
  311. foo_file_ = pool_.BuildFile(foo_file);
  312. ASSERT_TRUE(foo_file_ != nullptr);
  313. bar_file_ = pool_.BuildFile(bar_file);
  314. ASSERT_TRUE(bar_file_ != nullptr);
  315. baz_file_ = pool_.BuildFile(baz_file);
  316. ASSERT_TRUE(baz_file_ != nullptr);
  317. ASSERT_EQ(1, foo_file_->message_type_count());
  318. foo_message_ = foo_file_->message_type(0);
  319. ASSERT_EQ(1, foo_file_->enum_type_count());
  320. foo_enum_ = foo_file_->enum_type(0);
  321. ASSERT_EQ(1, foo_enum_->value_count());
  322. foo_enum_value_ = foo_enum_->value(0);
  323. ASSERT_EQ(1, foo_file_->service_count());
  324. foo_service_ = foo_file_->service(0);
  325. ASSERT_EQ(1, foo_file_->extension_count());
  326. foo_extension_ = foo_file_->extension(0);
  327. ASSERT_EQ(1, bar_file_->message_type_count());
  328. bar_message_ = bar_file_->message_type(0);
  329. ASSERT_EQ(1, bar_file_->enum_type_count());
  330. bar_enum_ = bar_file_->enum_type(0);
  331. ASSERT_EQ(1, bar_enum_->value_count());
  332. bar_enum_value_ = bar_enum_->value(0);
  333. ASSERT_EQ(1, bar_file_->service_count());
  334. bar_service_ = bar_file_->service(0);
  335. ASSERT_EQ(1, bar_file_->extension_count());
  336. bar_extension_ = bar_file_->extension(0);
  337. }
  338. DescriptorPool pool_;
  339. const FileDescriptor* foo_file_;
  340. const FileDescriptor* bar_file_;
  341. const FileDescriptor* baz_file_;
  342. const Descriptor* foo_message_;
  343. const EnumDescriptor* foo_enum_;
  344. const EnumValueDescriptor* foo_enum_value_;
  345. const ServiceDescriptor* foo_service_;
  346. const FieldDescriptor* foo_extension_;
  347. const Descriptor* bar_message_;
  348. const EnumDescriptor* bar_enum_;
  349. const EnumValueDescriptor* bar_enum_value_;
  350. const ServiceDescriptor* bar_service_;
  351. const FieldDescriptor* bar_extension_;
  352. };
  353. TEST_F(FileDescriptorTest, Name) {
  354. EXPECT_EQ("foo.proto", foo_file_->name());
  355. EXPECT_EQ("bar.proto", bar_file_->name());
  356. EXPECT_EQ("baz.proto", baz_file_->name());
  357. }
  358. TEST_F(FileDescriptorTest, Package) {
  359. EXPECT_EQ("", foo_file_->package());
  360. EXPECT_EQ("bar_package", bar_file_->package());
  361. }
  362. TEST_F(FileDescriptorTest, Dependencies) {
  363. EXPECT_EQ(0, foo_file_->dependency_count());
  364. EXPECT_EQ(1, bar_file_->dependency_count());
  365. EXPECT_EQ(foo_file_, bar_file_->dependency(0));
  366. }
  367. TEST_F(FileDescriptorTest, FindMessageTypeByName) {
  368. EXPECT_EQ(foo_message_, foo_file_->FindMessageTypeByName("FooMessage"));
  369. EXPECT_EQ(bar_message_, bar_file_->FindMessageTypeByName("BarMessage"));
  370. EXPECT_TRUE(foo_file_->FindMessageTypeByName("BarMessage") == nullptr);
  371. EXPECT_TRUE(bar_file_->FindMessageTypeByName("FooMessage") == nullptr);
  372. EXPECT_TRUE(baz_file_->FindMessageTypeByName("FooMessage") == nullptr);
  373. EXPECT_TRUE(foo_file_->FindMessageTypeByName("NoSuchMessage") == nullptr);
  374. EXPECT_TRUE(foo_file_->FindMessageTypeByName("FooEnum") == nullptr);
  375. }
  376. TEST_F(FileDescriptorTest, FindEnumTypeByName) {
  377. EXPECT_EQ(foo_enum_, foo_file_->FindEnumTypeByName("FooEnum"));
  378. EXPECT_EQ(bar_enum_, bar_file_->FindEnumTypeByName("BarEnum"));
  379. EXPECT_TRUE(foo_file_->FindEnumTypeByName("BarEnum") == nullptr);
  380. EXPECT_TRUE(bar_file_->FindEnumTypeByName("FooEnum") == nullptr);
  381. EXPECT_TRUE(baz_file_->FindEnumTypeByName("FooEnum") == nullptr);
  382. EXPECT_TRUE(foo_file_->FindEnumTypeByName("NoSuchEnum") == nullptr);
  383. EXPECT_TRUE(foo_file_->FindEnumTypeByName("FooMessage") == nullptr);
  384. }
  385. TEST_F(FileDescriptorTest, FindEnumValueByName) {
  386. EXPECT_EQ(foo_enum_value_, foo_file_->FindEnumValueByName("FOO_ENUM_VALUE"));
  387. EXPECT_EQ(bar_enum_value_, bar_file_->FindEnumValueByName("BAR_ENUM_VALUE"));
  388. EXPECT_TRUE(foo_file_->FindEnumValueByName("BAR_ENUM_VALUE") == nullptr);
  389. EXPECT_TRUE(bar_file_->FindEnumValueByName("FOO_ENUM_VALUE") == nullptr);
  390. EXPECT_TRUE(baz_file_->FindEnumValueByName("FOO_ENUM_VALUE") == nullptr);
  391. EXPECT_TRUE(foo_file_->FindEnumValueByName("NO_SUCH_VALUE") == nullptr);
  392. EXPECT_TRUE(foo_file_->FindEnumValueByName("FooMessage") == nullptr);
  393. }
  394. TEST_F(FileDescriptorTest, FindServiceByName) {
  395. EXPECT_EQ(foo_service_, foo_file_->FindServiceByName("FooService"));
  396. EXPECT_EQ(bar_service_, bar_file_->FindServiceByName("BarService"));
  397. EXPECT_TRUE(foo_file_->FindServiceByName("BarService") == nullptr);
  398. EXPECT_TRUE(bar_file_->FindServiceByName("FooService") == nullptr);
  399. EXPECT_TRUE(baz_file_->FindServiceByName("FooService") == nullptr);
  400. EXPECT_TRUE(foo_file_->FindServiceByName("NoSuchService") == nullptr);
  401. EXPECT_TRUE(foo_file_->FindServiceByName("FooMessage") == nullptr);
  402. }
  403. TEST_F(FileDescriptorTest, FindExtensionByName) {
  404. EXPECT_EQ(foo_extension_, foo_file_->FindExtensionByName("foo_extension"));
  405. EXPECT_EQ(bar_extension_, bar_file_->FindExtensionByName("bar_extension"));
  406. EXPECT_TRUE(foo_file_->FindExtensionByName("bar_extension") == nullptr);
  407. EXPECT_TRUE(bar_file_->FindExtensionByName("foo_extension") == nullptr);
  408. EXPECT_TRUE(baz_file_->FindExtensionByName("foo_extension") == nullptr);
  409. EXPECT_TRUE(foo_file_->FindExtensionByName("no_such_extension") == nullptr);
  410. EXPECT_TRUE(foo_file_->FindExtensionByName("FooMessage") == nullptr);
  411. }
  412. TEST_F(FileDescriptorTest, FindExtensionByNumber) {
  413. EXPECT_EQ(foo_extension_, pool_.FindExtensionByNumber(foo_message_, 1));
  414. EXPECT_EQ(bar_extension_, pool_.FindExtensionByNumber(bar_message_, 1));
  415. EXPECT_TRUE(pool_.FindExtensionByNumber(foo_message_, 2) == nullptr);
  416. }
  417. TEST_F(FileDescriptorTest, BuildAgain) {
  418. // Test that if te call BuildFile again on the same input we get the same
  419. // FileDescriptor back.
  420. FileDescriptorProto file;
  421. foo_file_->CopyTo(&file);
  422. EXPECT_EQ(foo_file_, pool_.BuildFile(file));
  423. // But if we change the file then it won't work.
  424. file.set_package("some.other.package");
  425. EXPECT_TRUE(pool_.BuildFile(file) == nullptr);
  426. }
  427. TEST_F(FileDescriptorTest, BuildAgainWithSyntax) {
  428. // Test that if te call BuildFile again on the same input we get the same
  429. // FileDescriptor back even if syntax param is specified.
  430. FileDescriptorProto proto_syntax2;
  431. proto_syntax2.set_name("foo_syntax2");
  432. proto_syntax2.set_syntax("proto2");
  433. const FileDescriptor* proto2_descriptor = pool_.BuildFile(proto_syntax2);
  434. EXPECT_TRUE(proto2_descriptor != nullptr);
  435. EXPECT_EQ(proto2_descriptor, pool_.BuildFile(proto_syntax2));
  436. FileDescriptorProto implicit_proto2;
  437. implicit_proto2.set_name("foo_implicit_syntax2");
  438. const FileDescriptor* implicit_proto2_descriptor =
  439. pool_.BuildFile(implicit_proto2);
  440. EXPECT_TRUE(implicit_proto2_descriptor != nullptr);
  441. // We get the same FileDescriptor back if syntax param is explicitly
  442. // specified.
  443. implicit_proto2.set_syntax("proto2");
  444. EXPECT_EQ(implicit_proto2_descriptor, pool_.BuildFile(implicit_proto2));
  445. FileDescriptorProto proto_syntax3;
  446. proto_syntax3.set_name("foo_syntax3");
  447. proto_syntax3.set_syntax("proto3");
  448. const FileDescriptor* proto3_descriptor = pool_.BuildFile(proto_syntax3);
  449. EXPECT_TRUE(proto3_descriptor != nullptr);
  450. EXPECT_EQ(proto3_descriptor, pool_.BuildFile(proto_syntax3));
  451. }
  452. TEST_F(FileDescriptorTest, Syntax) {
  453. FileDescriptorProto proto;
  454. proto.set_name("foo");
  455. // Enable the test when we also populate the syntax for proto2.
  456. #if 0
  457. {
  458. proto.set_syntax("proto2");
  459. DescriptorPool pool;
  460. const FileDescriptor* file = pool.BuildFile(proto);
  461. EXPECT_TRUE(file != nullptr);
  462. EXPECT_EQ(FileDescriptor::SYNTAX_PROTO2, file->syntax());
  463. FileDescriptorProto other;
  464. file->CopyTo(&other);
  465. EXPECT_EQ("proto2", other.syntax());
  466. }
  467. #endif
  468. {
  469. proto.set_syntax("proto3");
  470. DescriptorPool pool;
  471. const FileDescriptor* file = pool.BuildFile(proto);
  472. EXPECT_TRUE(file != nullptr);
  473. EXPECT_EQ(FileDescriptor::SYNTAX_PROTO3, file->syntax());
  474. FileDescriptorProto other;
  475. file->CopyTo(&other);
  476. EXPECT_EQ("proto3", other.syntax());
  477. }
  478. }
  479. void ExtractDebugString(
  480. const FileDescriptor* file, std::set<std::string>* visited,
  481. std::vector<std::pair<std::string, std::string>>* debug_strings) {
  482. if (!visited->insert(file->name()).second) {
  483. return;
  484. }
  485. for (int i = 0; i < file->dependency_count(); ++i) {
  486. ExtractDebugString(file->dependency(i), visited, debug_strings);
  487. }
  488. debug_strings->push_back(std::make_pair(file->name(), file->DebugString()));
  489. }
  490. class SimpleErrorCollector : public io::ErrorCollector {
  491. public:
  492. // implements ErrorCollector ---------------------------------------
  493. void AddError(int line, int column, const std::string& message) {
  494. last_error_ = StringPrintf("%d:%d:", line, column) + message;
  495. }
  496. const std::string& last_error() { return last_error_; }
  497. private:
  498. std::string last_error_;
  499. };
  500. // Test that the result of FileDescriptor::DebugString() can be used to create
  501. // the original descriptors.
  502. TEST_F(FileDescriptorTest, DebugStringRoundTrip) {
  503. std::set<std::string> visited;
  504. std::vector<std::pair<std::string, std::string>> debug_strings;
  505. ExtractDebugString(protobuf_unittest::TestAllTypes::descriptor()->file(),
  506. &visited, &debug_strings);
  507. ExtractDebugString(
  508. protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file(),
  509. &visited, &debug_strings);
  510. ExtractDebugString(proto3_arena_unittest::TestAllTypes::descriptor()->file(),
  511. &visited, &debug_strings);
  512. ASSERT_GE(debug_strings.size(), 3);
  513. DescriptorPool pool;
  514. for (int i = 0; i < debug_strings.size(); ++i) {
  515. const std::string& name = debug_strings[i].first;
  516. const std::string& content = debug_strings[i].second;
  517. io::ArrayInputStream input_stream(content.data(), content.size());
  518. SimpleErrorCollector error_collector;
  519. io::Tokenizer tokenizer(&input_stream, &error_collector);
  520. compiler::Parser parser;
  521. parser.RecordErrorsTo(&error_collector);
  522. FileDescriptorProto proto;
  523. ASSERT_TRUE(parser.Parse(&tokenizer, &proto))
  524. << error_collector.last_error() << "\n"
  525. << content;
  526. ASSERT_EQ("", error_collector.last_error());
  527. proto.set_name(name);
  528. const FileDescriptor* descriptor = pool.BuildFile(proto);
  529. ASSERT_TRUE(descriptor != nullptr) << proto.DebugString();
  530. EXPECT_EQ(content, descriptor->DebugString());
  531. }
  532. }
  533. // ===================================================================
  534. // Test simple flat messages and fields.
  535. class DescriptorTest : public testing::Test {
  536. protected:
  537. virtual void SetUp() {
  538. // Build descriptors for the following definitions:
  539. //
  540. // // in "foo.proto"
  541. // message TestForeign {}
  542. // enum TestEnum {}
  543. //
  544. // message TestMessage {
  545. // required string foo = 1;
  546. // optional TestEnum bar = 6;
  547. // repeated TestForeign baz = 500000000;
  548. // optional group qux = 15 {}
  549. // }
  550. //
  551. // // in "bar.proto"
  552. // package corge.grault;
  553. // message TestMessage2 {
  554. // required string foo = 1;
  555. // required string bar = 2;
  556. // required string quux = 6;
  557. // }
  558. //
  559. // // in "map.proto"
  560. // message TestMessage3 {
  561. // map<int32, int32> map_int32_int32 = 1;
  562. // }
  563. //
  564. // // in "json.proto"
  565. // message TestMessage4 {
  566. // optional int32 field_name1 = 1;
  567. // optional int32 fieldName2 = 2;
  568. // optional int32 FieldName3 = 3;
  569. // optional int32 _field_name4 = 4;
  570. // optional int32 FIELD_NAME5 = 5;
  571. // optional int32 field_name6 = 6 [json_name = "@type"];
  572. // }
  573. //
  574. // We cheat and use TestForeign as the type for qux rather than create
  575. // an actual nested type.
  576. //
  577. // Since all primitive types (including string) use the same building
  578. // code, there's no need to test each one individually.
  579. //
  580. // TestMessage2 is primarily here to test FindFieldByName and friends.
  581. // All messages created from the same DescriptorPool share the same lookup
  582. // table, so we need to insure that they don't interfere.
  583. FileDescriptorProto foo_file;
  584. foo_file.set_name("foo.proto");
  585. AddMessage(&foo_file, "TestForeign");
  586. AddEmptyEnum(&foo_file, "TestEnum");
  587. DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
  588. AddField(message, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED,
  589. FieldDescriptorProto::TYPE_STRING);
  590. AddField(message, "bar", 6, FieldDescriptorProto::LABEL_OPTIONAL,
  591. FieldDescriptorProto::TYPE_ENUM)
  592. ->set_type_name("TestEnum");
  593. AddField(message, "baz", 500000000, FieldDescriptorProto::LABEL_REPEATED,
  594. FieldDescriptorProto::TYPE_MESSAGE)
  595. ->set_type_name("TestForeign");
  596. AddField(message, "qux", 15, FieldDescriptorProto::LABEL_OPTIONAL,
  597. FieldDescriptorProto::TYPE_GROUP)
  598. ->set_type_name("TestForeign");
  599. FileDescriptorProto bar_file;
  600. bar_file.set_name("bar.proto");
  601. bar_file.set_package("corge.grault");
  602. DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
  603. AddField(message2, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED,
  604. FieldDescriptorProto::TYPE_STRING);
  605. AddField(message2, "bar", 2, FieldDescriptorProto::LABEL_REQUIRED,
  606. FieldDescriptorProto::TYPE_STRING);
  607. AddField(message2, "quux", 6, FieldDescriptorProto::LABEL_REQUIRED,
  608. FieldDescriptorProto::TYPE_STRING);
  609. FileDescriptorProto map_file;
  610. map_file.set_name("map.proto");
  611. DescriptorProto* message3 = AddMessage(&map_file, "TestMessage3");
  612. DescriptorProto* entry = AddNestedMessage(message3, "MapInt32Int32Entry");
  613. AddField(entry, "key", 1, FieldDescriptorProto::LABEL_OPTIONAL,
  614. FieldDescriptorProto::TYPE_INT32);
  615. AddField(entry, "value", 2, FieldDescriptorProto::LABEL_OPTIONAL,
  616. FieldDescriptorProto::TYPE_INT32);
  617. entry->mutable_options()->set_map_entry(true);
  618. AddField(message3, "map_int32_int32", 1,
  619. FieldDescriptorProto::LABEL_REPEATED,
  620. FieldDescriptorProto::TYPE_MESSAGE)
  621. ->set_type_name("MapInt32Int32Entry");
  622. FileDescriptorProto json_file;
  623. json_file.set_name("json.proto");
  624. json_file.set_syntax("proto3");
  625. DescriptorProto* message4 = AddMessage(&json_file, "TestMessage4");
  626. AddField(message4, "field_name1", 1, FieldDescriptorProto::LABEL_OPTIONAL,
  627. FieldDescriptorProto::TYPE_INT32);
  628. AddField(message4, "fieldName2", 2, FieldDescriptorProto::LABEL_OPTIONAL,
  629. FieldDescriptorProto::TYPE_INT32);
  630. AddField(message4, "FieldName3", 3, FieldDescriptorProto::LABEL_OPTIONAL,
  631. FieldDescriptorProto::TYPE_INT32);
  632. AddField(message4, "_field_name4", 4, FieldDescriptorProto::LABEL_OPTIONAL,
  633. FieldDescriptorProto::TYPE_INT32);
  634. AddField(message4, "FIELD_NAME5", 5, FieldDescriptorProto::LABEL_OPTIONAL,
  635. FieldDescriptorProto::TYPE_INT32);
  636. AddField(message4, "field_name6", 6, FieldDescriptorProto::LABEL_OPTIONAL,
  637. FieldDescriptorProto::TYPE_INT32)
  638. ->set_json_name("@type");
  639. // Build the descriptors and get the pointers.
  640. foo_file_ = pool_.BuildFile(foo_file);
  641. ASSERT_TRUE(foo_file_ != nullptr);
  642. bar_file_ = pool_.BuildFile(bar_file);
  643. ASSERT_TRUE(bar_file_ != nullptr);
  644. map_file_ = pool_.BuildFile(map_file);
  645. ASSERT_TRUE(map_file_ != nullptr);
  646. json_file_ = pool_.BuildFile(json_file);
  647. ASSERT_TRUE(json_file_ != nullptr);
  648. ASSERT_EQ(1, foo_file_->enum_type_count());
  649. enum_ = foo_file_->enum_type(0);
  650. ASSERT_EQ(2, foo_file_->message_type_count());
  651. foreign_ = foo_file_->message_type(0);
  652. message_ = foo_file_->message_type(1);
  653. ASSERT_EQ(4, message_->field_count());
  654. foo_ = message_->field(0);
  655. bar_ = message_->field(1);
  656. baz_ = message_->field(2);
  657. qux_ = message_->field(3);
  658. ASSERT_EQ(1, bar_file_->message_type_count());
  659. message2_ = bar_file_->message_type(0);
  660. ASSERT_EQ(3, message2_->field_count());
  661. foo2_ = message2_->field(0);
  662. bar2_ = message2_->field(1);
  663. quux2_ = message2_->field(2);
  664. ASSERT_EQ(1, map_file_->message_type_count());
  665. message3_ = map_file_->message_type(0);
  666. ASSERT_EQ(1, message3_->field_count());
  667. map_ = message3_->field(0);
  668. ASSERT_EQ(1, json_file_->message_type_count());
  669. message4_ = json_file_->message_type(0);
  670. }
  671. void CopyWithJsonName(const Descriptor* message, DescriptorProto* proto) {
  672. message->CopyTo(proto);
  673. message->CopyJsonNameTo(proto);
  674. }
  675. DescriptorPool pool_;
  676. const FileDescriptor* foo_file_;
  677. const FileDescriptor* bar_file_;
  678. const FileDescriptor* map_file_;
  679. const FileDescriptor* json_file_;
  680. const Descriptor* message_;
  681. const Descriptor* message2_;
  682. const Descriptor* message3_;
  683. const Descriptor* message4_;
  684. const Descriptor* foreign_;
  685. const EnumDescriptor* enum_;
  686. const FieldDescriptor* foo_;
  687. const FieldDescriptor* bar_;
  688. const FieldDescriptor* baz_;
  689. const FieldDescriptor* qux_;
  690. const FieldDescriptor* foo2_;
  691. const FieldDescriptor* bar2_;
  692. const FieldDescriptor* quux2_;
  693. const FieldDescriptor* map_;
  694. };
  695. TEST_F(DescriptorTest, Name) {
  696. EXPECT_EQ("TestMessage", message_->name());
  697. EXPECT_EQ("TestMessage", message_->full_name());
  698. EXPECT_EQ(foo_file_, message_->file());
  699. EXPECT_EQ("TestMessage2", message2_->name());
  700. EXPECT_EQ("corge.grault.TestMessage2", message2_->full_name());
  701. EXPECT_EQ(bar_file_, message2_->file());
  702. }
  703. TEST_F(DescriptorTest, ContainingType) {
  704. EXPECT_TRUE(message_->containing_type() == nullptr);
  705. EXPECT_TRUE(message2_->containing_type() == nullptr);
  706. }
  707. TEST_F(DescriptorTest, FieldsByIndex) {
  708. ASSERT_EQ(4, message_->field_count());
  709. EXPECT_EQ(foo_, message_->field(0));
  710. EXPECT_EQ(bar_, message_->field(1));
  711. EXPECT_EQ(baz_, message_->field(2));
  712. EXPECT_EQ(qux_, message_->field(3));
  713. }
  714. TEST_F(DescriptorTest, FindFieldByName) {
  715. // All messages in the same DescriptorPool share a single lookup table for
  716. // fields. So, in addition to testing that FindFieldByName finds the fields
  717. // of the message, we need to test that it does *not* find the fields of
  718. // *other* messages.
  719. EXPECT_EQ(foo_, message_->FindFieldByName("foo"));
  720. EXPECT_EQ(bar_, message_->FindFieldByName("bar"));
  721. EXPECT_EQ(baz_, message_->FindFieldByName("baz"));
  722. EXPECT_EQ(qux_, message_->FindFieldByName("qux"));
  723. EXPECT_TRUE(message_->FindFieldByName("no_such_field") == nullptr);
  724. EXPECT_TRUE(message_->FindFieldByName("quux") == nullptr);
  725. EXPECT_EQ(foo2_, message2_->FindFieldByName("foo"));
  726. EXPECT_EQ(bar2_, message2_->FindFieldByName("bar"));
  727. EXPECT_EQ(quux2_, message2_->FindFieldByName("quux"));
  728. EXPECT_TRUE(message2_->FindFieldByName("baz") == nullptr);
  729. EXPECT_TRUE(message2_->FindFieldByName("qux") == nullptr);
  730. }
  731. TEST_F(DescriptorTest, FindFieldByNumber) {
  732. EXPECT_EQ(foo_, message_->FindFieldByNumber(1));
  733. EXPECT_EQ(bar_, message_->FindFieldByNumber(6));
  734. EXPECT_EQ(baz_, message_->FindFieldByNumber(500000000));
  735. EXPECT_EQ(qux_, message_->FindFieldByNumber(15));
  736. EXPECT_TRUE(message_->FindFieldByNumber(837592) == nullptr);
  737. EXPECT_TRUE(message_->FindFieldByNumber(2) == nullptr);
  738. EXPECT_EQ(foo2_, message2_->FindFieldByNumber(1));
  739. EXPECT_EQ(bar2_, message2_->FindFieldByNumber(2));
  740. EXPECT_EQ(quux2_, message2_->FindFieldByNumber(6));
  741. EXPECT_TRUE(message2_->FindFieldByNumber(15) == nullptr);
  742. EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == nullptr);
  743. }
  744. TEST_F(DescriptorTest, FieldName) {
  745. EXPECT_EQ("foo", foo_->name());
  746. EXPECT_EQ("bar", bar_->name());
  747. EXPECT_EQ("baz", baz_->name());
  748. EXPECT_EQ("qux", qux_->name());
  749. }
  750. TEST_F(DescriptorTest, FieldFullName) {
  751. EXPECT_EQ("TestMessage.foo", foo_->full_name());
  752. EXPECT_EQ("TestMessage.bar", bar_->full_name());
  753. EXPECT_EQ("TestMessage.baz", baz_->full_name());
  754. EXPECT_EQ("TestMessage.qux", qux_->full_name());
  755. EXPECT_EQ("corge.grault.TestMessage2.foo", foo2_->full_name());
  756. EXPECT_EQ("corge.grault.TestMessage2.bar", bar2_->full_name());
  757. EXPECT_EQ("corge.grault.TestMessage2.quux", quux2_->full_name());
  758. }
  759. TEST_F(DescriptorTest, PrintableNameIsFullNameForNonExtensionFields) {
  760. EXPECT_EQ("TestMessage.foo", foo_->PrintableNameForExtension());
  761. EXPECT_EQ("TestMessage.bar", bar_->PrintableNameForExtension());
  762. EXPECT_EQ("TestMessage.baz", baz_->PrintableNameForExtension());
  763. EXPECT_EQ("TestMessage.qux", qux_->PrintableNameForExtension());
  764. EXPECT_EQ("corge.grault.TestMessage2.foo",
  765. foo2_->PrintableNameForExtension());
  766. EXPECT_EQ("corge.grault.TestMessage2.bar",
  767. bar2_->PrintableNameForExtension());
  768. EXPECT_EQ("corge.grault.TestMessage2.quux",
  769. quux2_->PrintableNameForExtension());
  770. }
  771. TEST_F(DescriptorTest, PrintableNameIsFullNameForNonMessageSetExtension) {
  772. EXPECT_EQ("protobuf_unittest.Aggregate.nested",
  773. protobuf_unittest::Aggregate::descriptor()
  774. ->FindExtensionByName("nested")
  775. ->PrintableNameForExtension());
  776. }
  777. TEST_F(DescriptorTest, PrintableNameIsExtendingTypeForMessageSetExtension) {
  778. EXPECT_EQ("protobuf_unittest.AggregateMessageSetElement",
  779. protobuf_unittest::AggregateMessageSetElement::descriptor()
  780. ->FindExtensionByName("message_set_extension")
  781. ->PrintableNameForExtension());
  782. }
  783. TEST_F(DescriptorTest, FieldJsonName) {
  784. EXPECT_EQ("fieldName1", message4_->field(0)->json_name());
  785. EXPECT_EQ("fieldName2", message4_->field(1)->json_name());
  786. EXPECT_EQ("FieldName3", message4_->field(2)->json_name());
  787. EXPECT_EQ("FieldName4", message4_->field(3)->json_name());
  788. EXPECT_EQ("FIELDNAME5", message4_->field(4)->json_name());
  789. EXPECT_EQ("@type", message4_->field(5)->json_name());
  790. DescriptorProto proto;
  791. message4_->CopyTo(&proto);
  792. ASSERT_EQ(6, proto.field_size());
  793. EXPECT_FALSE(proto.field(0).has_json_name());
  794. EXPECT_FALSE(proto.field(1).has_json_name());
  795. EXPECT_FALSE(proto.field(2).has_json_name());
  796. EXPECT_FALSE(proto.field(3).has_json_name());
  797. EXPECT_FALSE(proto.field(4).has_json_name());
  798. EXPECT_EQ("@type", proto.field(5).json_name());
  799. proto.Clear();
  800. CopyWithJsonName(message4_, &proto);
  801. ASSERT_EQ(6, proto.field_size());
  802. EXPECT_EQ("fieldName1", proto.field(0).json_name());
  803. EXPECT_EQ("fieldName2", proto.field(1).json_name());
  804. EXPECT_EQ("FieldName3", proto.field(2).json_name());
  805. EXPECT_EQ("FieldName4", proto.field(3).json_name());
  806. EXPECT_EQ("FIELDNAME5", proto.field(4).json_name());
  807. EXPECT_EQ("@type", proto.field(5).json_name());
  808. // Test generated descriptor.
  809. const Descriptor* generated = protobuf_unittest::TestJsonName::descriptor();
  810. ASSERT_EQ(6, generated->field_count());
  811. EXPECT_EQ("fieldName1", generated->field(0)->json_name());
  812. EXPECT_EQ("fieldName2", generated->field(1)->json_name());
  813. EXPECT_EQ("FieldName3", generated->field(2)->json_name());
  814. EXPECT_EQ("FieldName4", generated->field(3)->json_name());
  815. EXPECT_EQ("FIELDNAME5", generated->field(4)->json_name());
  816. EXPECT_EQ("@type", generated->field(5)->json_name());
  817. }
  818. TEST_F(DescriptorTest, FieldFile) {
  819. EXPECT_EQ(foo_file_, foo_->file());
  820. EXPECT_EQ(foo_file_, bar_->file());
  821. EXPECT_EQ(foo_file_, baz_->file());
  822. EXPECT_EQ(foo_file_, qux_->file());
  823. EXPECT_EQ(bar_file_, foo2_->file());
  824. EXPECT_EQ(bar_file_, bar2_->file());
  825. EXPECT_EQ(bar_file_, quux2_->file());
  826. }
  827. TEST_F(DescriptorTest, FieldIndex) {
  828. EXPECT_EQ(0, foo_->index());
  829. EXPECT_EQ(1, bar_->index());
  830. EXPECT_EQ(2, baz_->index());
  831. EXPECT_EQ(3, qux_->index());
  832. }
  833. TEST_F(DescriptorTest, FieldNumber) {
  834. EXPECT_EQ(1, foo_->number());
  835. EXPECT_EQ(6, bar_->number());
  836. EXPECT_EQ(500000000, baz_->number());
  837. EXPECT_EQ(15, qux_->number());
  838. }
  839. TEST_F(DescriptorTest, FieldType) {
  840. EXPECT_EQ(FieldDescriptor::TYPE_STRING, foo_->type());
  841. EXPECT_EQ(FieldDescriptor::TYPE_ENUM, bar_->type());
  842. EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_->type());
  843. EXPECT_EQ(FieldDescriptor::TYPE_GROUP, qux_->type());
  844. }
  845. TEST_F(DescriptorTest, FieldLabel) {
  846. EXPECT_EQ(FieldDescriptor::LABEL_REQUIRED, foo_->label());
  847. EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, bar_->label());
  848. EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, baz_->label());
  849. EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, qux_->label());
  850. EXPECT_TRUE(foo_->is_required());
  851. EXPECT_FALSE(foo_->is_optional());
  852. EXPECT_FALSE(foo_->is_repeated());
  853. EXPECT_FALSE(bar_->is_required());
  854. EXPECT_TRUE(bar_->is_optional());
  855. EXPECT_FALSE(bar_->is_repeated());
  856. EXPECT_FALSE(baz_->is_required());
  857. EXPECT_FALSE(baz_->is_optional());
  858. EXPECT_TRUE(baz_->is_repeated());
  859. }
  860. TEST_F(DescriptorTest, IsMap) {
  861. EXPECT_TRUE(map_->is_map());
  862. EXPECT_FALSE(baz_->is_map());
  863. EXPECT_TRUE(map_->message_type()->options().map_entry());
  864. }
  865. TEST_F(DescriptorTest, FieldHasDefault) {
  866. EXPECT_FALSE(foo_->has_default_value());
  867. EXPECT_FALSE(bar_->has_default_value());
  868. EXPECT_FALSE(baz_->has_default_value());
  869. EXPECT_FALSE(qux_->has_default_value());
  870. }
  871. TEST_F(DescriptorTest, FieldContainingType) {
  872. EXPECT_EQ(message_, foo_->containing_type());
  873. EXPECT_EQ(message_, bar_->containing_type());
  874. EXPECT_EQ(message_, baz_->containing_type());
  875. EXPECT_EQ(message_, qux_->containing_type());
  876. EXPECT_EQ(message2_, foo2_->containing_type());
  877. EXPECT_EQ(message2_, bar2_->containing_type());
  878. EXPECT_EQ(message2_, quux2_->containing_type());
  879. }
  880. TEST_F(DescriptorTest, FieldMessageType) {
  881. EXPECT_TRUE(foo_->message_type() == nullptr);
  882. EXPECT_TRUE(bar_->message_type() == nullptr);
  883. EXPECT_EQ(foreign_, baz_->message_type());
  884. EXPECT_EQ(foreign_, qux_->message_type());
  885. }
  886. TEST_F(DescriptorTest, FieldEnumType) {
  887. EXPECT_TRUE(foo_->enum_type() == nullptr);
  888. EXPECT_TRUE(baz_->enum_type() == nullptr);
  889. EXPECT_TRUE(qux_->enum_type() == nullptr);
  890. EXPECT_EQ(enum_, bar_->enum_type());
  891. }
  892. // ===================================================================
  893. // Test simple flat messages and fields.
  894. class OneofDescriptorTest : public testing::Test {
  895. protected:
  896. virtual void SetUp() {
  897. // Build descriptors for the following definitions:
  898. //
  899. // package garply;
  900. // message TestOneof {
  901. // optional int32 a = 1;
  902. // oneof foo {
  903. // string b = 2;
  904. // TestOneof c = 3;
  905. // }
  906. // oneof bar {
  907. // float d = 4;
  908. // }
  909. // }
  910. FileDescriptorProto baz_file;
  911. baz_file.set_name("baz.proto");
  912. baz_file.set_package("garply");
  913. DescriptorProto* oneof_message = AddMessage(&baz_file, "TestOneof");
  914. oneof_message->add_oneof_decl()->set_name("foo");
  915. oneof_message->add_oneof_decl()->set_name("bar");
  916. AddField(oneof_message, "a", 1, FieldDescriptorProto::LABEL_OPTIONAL,
  917. FieldDescriptorProto::TYPE_INT32);
  918. AddField(oneof_message, "b", 2, FieldDescriptorProto::LABEL_OPTIONAL,
  919. FieldDescriptorProto::TYPE_STRING);
  920. oneof_message->mutable_field(1)->set_oneof_index(0);
  921. AddField(oneof_message, "c", 3, FieldDescriptorProto::LABEL_OPTIONAL,
  922. FieldDescriptorProto::TYPE_MESSAGE);
  923. oneof_message->mutable_field(2)->set_oneof_index(0);
  924. oneof_message->mutable_field(2)->set_type_name("TestOneof");
  925. AddField(oneof_message, "d", 4, FieldDescriptorProto::LABEL_OPTIONAL,
  926. FieldDescriptorProto::TYPE_FLOAT);
  927. oneof_message->mutable_field(3)->set_oneof_index(1);
  928. // Build the descriptors and get the pointers.
  929. baz_file_ = pool_.BuildFile(baz_file);
  930. ASSERT_TRUE(baz_file_ != nullptr);
  931. ASSERT_EQ(1, baz_file_->message_type_count());
  932. oneof_message_ = baz_file_->message_type(0);
  933. ASSERT_EQ(2, oneof_message_->oneof_decl_count());
  934. oneof_ = oneof_message_->oneof_decl(0);
  935. oneof2_ = oneof_message_->oneof_decl(1);
  936. ASSERT_EQ(4, oneof_message_->field_count());
  937. a_ = oneof_message_->field(0);
  938. b_ = oneof_message_->field(1);
  939. c_ = oneof_message_->field(2);
  940. d_ = oneof_message_->field(3);
  941. }
  942. DescriptorPool pool_;
  943. const FileDescriptor* baz_file_;
  944. const Descriptor* oneof_message_;
  945. const OneofDescriptor* oneof_;
  946. const OneofDescriptor* oneof2_;
  947. const FieldDescriptor* a_;
  948. const FieldDescriptor* b_;
  949. const FieldDescriptor* c_;
  950. const FieldDescriptor* d_;
  951. };
  952. TEST_F(OneofDescriptorTest, Normal) {
  953. EXPECT_EQ("foo", oneof_->name());
  954. EXPECT_EQ("garply.TestOneof.foo", oneof_->full_name());
  955. EXPECT_EQ(0, oneof_->index());
  956. ASSERT_EQ(2, oneof_->field_count());
  957. EXPECT_EQ(b_, oneof_->field(0));
  958. EXPECT_EQ(c_, oneof_->field(1));
  959. EXPECT_TRUE(a_->containing_oneof() == nullptr);
  960. EXPECT_EQ(oneof_, b_->containing_oneof());
  961. EXPECT_EQ(oneof_, c_->containing_oneof());
  962. }
  963. TEST_F(OneofDescriptorTest, FindByName) {
  964. EXPECT_EQ(oneof_, oneof_message_->FindOneofByName("foo"));
  965. EXPECT_EQ(oneof2_, oneof_message_->FindOneofByName("bar"));
  966. EXPECT_TRUE(oneof_message_->FindOneofByName("no_such_oneof") == nullptr);
  967. }
  968. // ===================================================================
  969. class StylizedFieldNamesTest : public testing::Test {
  970. protected:
  971. void SetUp() {
  972. FileDescriptorProto file;
  973. file.set_name("foo.proto");
  974. AddExtensionRange(AddMessage(&file, "ExtendableMessage"), 1, 1000);
  975. DescriptorProto* message = AddMessage(&file, "TestMessage");
  976. AddField(message, "foo_foo", 1, FieldDescriptorProto::LABEL_OPTIONAL,
  977. FieldDescriptorProto::TYPE_INT32);
  978. AddField(message, "FooBar", 2, FieldDescriptorProto::LABEL_OPTIONAL,
  979. FieldDescriptorProto::TYPE_INT32);
  980. AddField(message, "fooBaz", 3, FieldDescriptorProto::LABEL_OPTIONAL,
  981. FieldDescriptorProto::TYPE_INT32);
  982. AddField(message, "fooFoo", 4, // Camel-case conflict with foo_foo.
  983. FieldDescriptorProto::LABEL_OPTIONAL,
  984. FieldDescriptorProto::TYPE_INT32);
  985. AddField(message, "foobar", 5, // Lower-case conflict with FooBar.
  986. FieldDescriptorProto::LABEL_OPTIONAL,
  987. FieldDescriptorProto::TYPE_INT32);
  988. AddNestedExtension(message, "ExtendableMessage", "bar_foo", 1,
  989. FieldDescriptorProto::LABEL_OPTIONAL,
  990. FieldDescriptorProto::TYPE_INT32);
  991. AddNestedExtension(message, "ExtendableMessage", "BarBar", 2,
  992. FieldDescriptorProto::LABEL_OPTIONAL,
  993. FieldDescriptorProto::TYPE_INT32);
  994. AddNestedExtension(message, "ExtendableMessage", "BarBaz", 3,
  995. FieldDescriptorProto::LABEL_OPTIONAL,
  996. FieldDescriptorProto::TYPE_INT32);
  997. AddNestedExtension(message, "ExtendableMessage", "barFoo", 4, // Conflict
  998. FieldDescriptorProto::LABEL_OPTIONAL,
  999. FieldDescriptorProto::TYPE_INT32);
  1000. AddNestedExtension(message, "ExtendableMessage", "barbar", 5, // Conflict
  1001. FieldDescriptorProto::LABEL_OPTIONAL,
  1002. FieldDescriptorProto::TYPE_INT32);
  1003. AddExtension(&file, "ExtendableMessage", "baz_foo", 11,
  1004. FieldDescriptorProto::LABEL_OPTIONAL,
  1005. FieldDescriptorProto::TYPE_INT32);
  1006. AddExtension(&file, "ExtendableMessage", "BazBar", 12,
  1007. FieldDescriptorProto::LABEL_OPTIONAL,
  1008. FieldDescriptorProto::TYPE_INT32);
  1009. AddExtension(&file, "ExtendableMessage", "BazBaz", 13,
  1010. FieldDescriptorProto::LABEL_OPTIONAL,
  1011. FieldDescriptorProto::TYPE_INT32);
  1012. AddExtension(&file, "ExtendableMessage", "bazFoo", 14, // Conflict
  1013. FieldDescriptorProto::LABEL_OPTIONAL,
  1014. FieldDescriptorProto::TYPE_INT32);
  1015. AddExtension(&file, "ExtendableMessage", "bazbar", 15, // Conflict
  1016. FieldDescriptorProto::LABEL_OPTIONAL,
  1017. FieldDescriptorProto::TYPE_INT32);
  1018. file_ = pool_.BuildFile(file);
  1019. ASSERT_TRUE(file_ != nullptr);
  1020. ASSERT_EQ(2, file_->message_type_count());
  1021. message_ = file_->message_type(1);
  1022. ASSERT_EQ("TestMessage", message_->name());
  1023. ASSERT_EQ(5, message_->field_count());
  1024. ASSERT_EQ(5, message_->extension_count());
  1025. ASSERT_EQ(5, file_->extension_count());
  1026. }
  1027. DescriptorPool pool_;
  1028. const FileDescriptor* file_;
  1029. const Descriptor* message_;
  1030. };
  1031. TEST_F(StylizedFieldNamesTest, LowercaseName) {
  1032. EXPECT_EQ("foo_foo", message_->field(0)->lowercase_name());
  1033. EXPECT_EQ("foobar", message_->field(1)->lowercase_name());
  1034. EXPECT_EQ("foobaz", message_->field(2)->lowercase_name());
  1035. EXPECT_EQ("foofoo", message_->field(3)->lowercase_name());
  1036. EXPECT_EQ("foobar", message_->field(4)->lowercase_name());
  1037. EXPECT_EQ("bar_foo", message_->extension(0)->lowercase_name());
  1038. EXPECT_EQ("barbar", message_->extension(1)->lowercase_name());
  1039. EXPECT_EQ("barbaz", message_->extension(2)->lowercase_name());
  1040. EXPECT_EQ("barfoo", message_->extension(3)->lowercase_name());
  1041. EXPECT_EQ("barbar", message_->extension(4)->lowercase_name());
  1042. EXPECT_EQ("baz_foo", file_->extension(0)->lowercase_name());
  1043. EXPECT_EQ("bazbar", file_->extension(1)->lowercase_name());
  1044. EXPECT_EQ("bazbaz", file_->extension(2)->lowercase_name());
  1045. EXPECT_EQ("bazfoo", file_->extension(3)->lowercase_name());
  1046. EXPECT_EQ("bazbar", file_->extension(4)->lowercase_name());
  1047. }
  1048. TEST_F(StylizedFieldNamesTest, CamelcaseName) {
  1049. EXPECT_EQ("fooFoo", message_->field(0)->camelcase_name());
  1050. EXPECT_EQ("fooBar", message_->field(1)->camelcase_name());
  1051. EXPECT_EQ("fooBaz", message_->field(2)->camelcase_name());
  1052. EXPECT_EQ("fooFoo", message_->field(3)->camelcase_name());
  1053. EXPECT_EQ("foobar", message_->field(4)->camelcase_name());
  1054. EXPECT_EQ("barFoo", message_->extension(0)->camelcase_name());
  1055. EXPECT_EQ("barBar", message_->extension(1)->camelcase_name());
  1056. EXPECT_EQ("barBaz", message_->extension(2)->camelcase_name());
  1057. EXPECT_EQ("barFoo", message_->extension(3)->camelcase_name());
  1058. EXPECT_EQ("barbar", message_->extension(4)->camelcase_name());
  1059. EXPECT_EQ("bazFoo", file_->extension(0)->camelcase_name());
  1060. EXPECT_EQ("bazBar", file_->extension(1)->camelcase_name());
  1061. EXPECT_EQ("bazBaz", file_->extension(2)->camelcase_name());
  1062. EXPECT_EQ("bazFoo", file_->extension(3)->camelcase_name());
  1063. EXPECT_EQ("bazbar", file_->extension(4)->camelcase_name());
  1064. }
  1065. TEST_F(StylizedFieldNamesTest, FindByLowercaseName) {
  1066. EXPECT_EQ(message_->field(0), message_->FindFieldByLowercaseName("foo_foo"));
  1067. EXPECT_EQ(message_->field(1), message_->FindFieldByLowercaseName("foobar"));
  1068. EXPECT_EQ(message_->field(2), message_->FindFieldByLowercaseName("foobaz"));
  1069. EXPECT_TRUE(message_->FindFieldByLowercaseName("FooBar") == nullptr);
  1070. EXPECT_TRUE(message_->FindFieldByLowercaseName("fooBaz") == nullptr);
  1071. EXPECT_TRUE(message_->FindFieldByLowercaseName("bar_foo") == nullptr);
  1072. EXPECT_TRUE(message_->FindFieldByLowercaseName("nosuchfield") == nullptr);
  1073. EXPECT_EQ(message_->extension(0),
  1074. message_->FindExtensionByLowercaseName("bar_foo"));
  1075. EXPECT_EQ(message_->extension(1),
  1076. message_->FindExtensionByLowercaseName("barbar"));
  1077. EXPECT_EQ(message_->extension(2),
  1078. message_->FindExtensionByLowercaseName("barbaz"));
  1079. EXPECT_TRUE(message_->FindExtensionByLowercaseName("BarBar") == nullptr);
  1080. EXPECT_TRUE(message_->FindExtensionByLowercaseName("barBaz") == nullptr);
  1081. EXPECT_TRUE(message_->FindExtensionByLowercaseName("foo_foo") == nullptr);
  1082. EXPECT_TRUE(message_->FindExtensionByLowercaseName("nosuchfield") == nullptr);
  1083. EXPECT_EQ(file_->extension(0),
  1084. file_->FindExtensionByLowercaseName("baz_foo"));
  1085. EXPECT_EQ(file_->extension(1), file_->FindExtensionByLowercaseName("bazbar"));
  1086. EXPECT_EQ(file_->extension(2), file_->FindExtensionByLowercaseName("bazbaz"));
  1087. EXPECT_TRUE(file_->FindExtensionByLowercaseName("BazBar") == nullptr);
  1088. EXPECT_TRUE(file_->FindExtensionByLowercaseName("bazBaz") == nullptr);
  1089. EXPECT_TRUE(file_->FindExtensionByLowercaseName("nosuchfield") == nullptr);
  1090. }
  1091. TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) {
  1092. EXPECT_EQ(message_->field(0), message_->FindFieldByCamelcaseName("fooFoo"));
  1093. EXPECT_EQ(message_->field(1), message_->FindFieldByCamelcaseName("fooBar"));
  1094. EXPECT_EQ(message_->field(2), message_->FindFieldByCamelcaseName("fooBaz"));
  1095. EXPECT_TRUE(message_->FindFieldByCamelcaseName("foo_foo") == nullptr);
  1096. EXPECT_TRUE(message_->FindFieldByCamelcaseName("FooBar") == nullptr);
  1097. EXPECT_TRUE(message_->FindFieldByCamelcaseName("barFoo") == nullptr);
  1098. EXPECT_TRUE(message_->FindFieldByCamelcaseName("nosuchfield") == nullptr);
  1099. EXPECT_EQ(message_->extension(0),
  1100. message_->FindExtensionByCamelcaseName("barFoo"));
  1101. EXPECT_EQ(message_->extension(1),
  1102. message_->FindExtensionByCamelcaseName("barBar"));
  1103. EXPECT_EQ(message_->extension(2),
  1104. message_->FindExtensionByCamelcaseName("barBaz"));
  1105. EXPECT_TRUE(message_->FindExtensionByCamelcaseName("bar_foo") == nullptr);
  1106. EXPECT_TRUE(message_->FindExtensionByCamelcaseName("BarBar") == nullptr);
  1107. EXPECT_TRUE(message_->FindExtensionByCamelcaseName("fooFoo") == nullptr);
  1108. EXPECT_TRUE(message_->FindExtensionByCamelcaseName("nosuchfield") == nullptr);
  1109. EXPECT_EQ(file_->extension(0), file_->FindExtensionByCamelcaseName("bazFoo"));
  1110. EXPECT_EQ(file_->extension(1), file_->FindExtensionByCamelcaseName("bazBar"));
  1111. EXPECT_EQ(file_->extension(2), file_->FindExtensionByCamelcaseName("bazBaz"));
  1112. EXPECT_TRUE(file_->FindExtensionByCamelcaseName("baz_foo") == nullptr);
  1113. EXPECT_TRUE(file_->FindExtensionByCamelcaseName("BazBar") == nullptr);
  1114. EXPECT_TRUE(file_->FindExtensionByCamelcaseName("nosuchfield") == nullptr);
  1115. }
  1116. // ===================================================================
  1117. // Test enum descriptors.
  1118. class EnumDescriptorTest : public testing::Test {
  1119. protected:
  1120. virtual void SetUp() {
  1121. // Build descriptors for the following definitions:
  1122. //
  1123. // // in "foo.proto"
  1124. // enum TestEnum {
  1125. // FOO = 1;
  1126. // BAR = 2;
  1127. // }
  1128. //
  1129. // // in "bar.proto"
  1130. // package corge.grault;
  1131. // enum TestEnum2 {
  1132. // FOO = 1;
  1133. // BAZ = 3;
  1134. // }
  1135. //
  1136. // TestEnum2 is primarily here to test FindValueByName and friends.
  1137. // All enums created from the same DescriptorPool share the same lookup
  1138. // table, so we need to insure that they don't interfere.
  1139. // TestEnum
  1140. FileDescriptorProto foo_file;
  1141. foo_file.set_name("foo.proto");
  1142. EnumDescriptorProto* enum_proto = AddEnum(&foo_file, "TestEnum");
  1143. AddEnumValue(enum_proto, "FOO", 1);
  1144. AddEnumValue(enum_proto, "BAR", 2);
  1145. // TestEnum2
  1146. FileDescriptorProto bar_file;
  1147. bar_file.set_name("bar.proto");
  1148. bar_file.set_package("corge.grault");
  1149. EnumDescriptorProto* enum2_proto = AddEnum(&bar_file, "TestEnum2");
  1150. AddEnumValue(enum2_proto, "FOO", 1);
  1151. AddEnumValue(enum2_proto, "BAZ", 3);
  1152. // Build the descriptors and get the pointers.
  1153. foo_file_ = pool_.BuildFile(foo_file);
  1154. ASSERT_TRUE(foo_file_ != nullptr);
  1155. bar_file_ = pool_.BuildFile(bar_file);
  1156. ASSERT_TRUE(bar_file_ != nullptr);
  1157. ASSERT_EQ(1, foo_file_->enum_type_count());
  1158. enum_ = foo_file_->enum_type(0);
  1159. ASSERT_EQ(2, enum_->value_count());
  1160. foo_ = enum_->value(0);
  1161. bar_ = enum_->value(1);
  1162. ASSERT_EQ(1, bar_file_->enum_type_count());
  1163. enum2_ = bar_file_->enum_type(0);
  1164. ASSERT_EQ(2, enum2_->value_count());
  1165. foo2_ = enum2_->value(0);
  1166. baz2_ = enum2_->value(1);
  1167. }
  1168. DescriptorPool pool_;
  1169. const FileDescriptor* foo_file_;
  1170. const FileDescriptor* bar_file_;
  1171. const EnumDescriptor* enum_;
  1172. const EnumDescriptor* enum2_;
  1173. const EnumValueDescriptor* foo_;
  1174. const EnumValueDescriptor* bar_;
  1175. const EnumValueDescriptor* foo2_;
  1176. const EnumValueDescriptor* baz2_;
  1177. };
  1178. TEST_F(EnumDescriptorTest, Name) {
  1179. EXPECT_EQ("TestEnum", enum_->name());
  1180. EXPECT_EQ("TestEnum", enum_->full_name());
  1181. EXPECT_EQ(foo_file_, enum_->file());
  1182. EXPECT_EQ("TestEnum2", enum2_->name());
  1183. EXPECT_EQ("corge.grault.TestEnum2", enum2_->full_name());
  1184. EXPECT_EQ(bar_file_, enum2_->file());
  1185. }
  1186. TEST_F(EnumDescriptorTest, ContainingType) {
  1187. EXPECT_TRUE(enum_->containing_type() == nullptr);
  1188. EXPECT_TRUE(enum2_->containing_type() == nullptr);
  1189. }
  1190. TEST_F(EnumDescriptorTest, ValuesByIndex) {
  1191. ASSERT_EQ(2, enum_->value_count());
  1192. EXPECT_EQ(foo_, enum_->value(0));
  1193. EXPECT_EQ(bar_, enum_->value(1));
  1194. }
  1195. TEST_F(EnumDescriptorTest, FindValueByName) {
  1196. EXPECT_EQ(foo_, enum_->FindValueByName("FOO"));
  1197. EXPECT_EQ(bar_, enum_->FindValueByName("BAR"));
  1198. EXPECT_EQ(foo2_, enum2_->FindValueByName("FOO"));
  1199. EXPECT_EQ(baz2_, enum2_->FindValueByName("BAZ"));
  1200. EXPECT_TRUE(enum_->FindValueByName("NO_SUCH_VALUE") == nullptr);
  1201. EXPECT_TRUE(enum_->FindValueByName("BAZ") == nullptr);
  1202. EXPECT_TRUE(enum2_->FindValueByName("BAR") == nullptr);
  1203. }
  1204. TEST_F(EnumDescriptorTest, FindValueByNumber) {
  1205. EXPECT_EQ(foo_, enum_->FindValueByNumber(1));
  1206. EXPECT_EQ(bar_, enum_->FindValueByNumber(2));
  1207. EXPECT_EQ(foo2_, enum2_->FindValueByNumber(1));
  1208. EXPECT_EQ(baz2_, enum2_->FindValueByNumber(3));
  1209. EXPECT_TRUE(enum_->FindValueByNumber(416) == nullptr);
  1210. EXPECT_TRUE(enum_->FindValueByNumber(3) == nullptr);
  1211. EXPECT_TRUE(enum2_->FindValueByNumber(2) == nullptr);
  1212. }
  1213. TEST_F(EnumDescriptorTest, ValueName) {
  1214. EXPECT_EQ("FOO", foo_->name());
  1215. EXPECT_EQ("BAR", bar_->name());
  1216. }
  1217. TEST_F(EnumDescriptorTest, ValueFullName) {
  1218. EXPECT_EQ("FOO", foo_->full_name());
  1219. EXPECT_EQ("BAR", bar_->full_name());
  1220. EXPECT_EQ("corge.grault.FOO", foo2_->full_name());
  1221. EXPECT_EQ("corge.grault.BAZ", baz2_->full_name());
  1222. }
  1223. TEST_F(EnumDescriptorTest, ValueIndex) {
  1224. EXPECT_EQ(0, foo_->index());
  1225. EXPECT_EQ(1, bar_->index());
  1226. }
  1227. TEST_F(EnumDescriptorTest, ValueNumber) {
  1228. EXPECT_EQ(1, foo_->number());
  1229. EXPECT_EQ(2, bar_->number());
  1230. }
  1231. TEST_F(EnumDescriptorTest, ValueType) {
  1232. EXPECT_EQ(enum_, foo_->type());
  1233. EXPECT_EQ(enum_, bar_->type());
  1234. EXPECT_EQ(enum2_, foo2_->type());
  1235. EXPECT_EQ(enum2_, baz2_->type());
  1236. }
  1237. // ===================================================================
  1238. // Test service descriptors.
  1239. class ServiceDescriptorTest : public testing::Test {
  1240. protected:
  1241. virtual void SetUp() {
  1242. // Build descriptors for the following messages and service:
  1243. // // in "foo.proto"
  1244. // message FooRequest {}
  1245. // message FooResponse {}
  1246. // message BarRequest {}
  1247. // message BarResponse {}
  1248. // message BazRequest {}
  1249. // message BazResponse {}
  1250. //
  1251. // service TestService {
  1252. // rpc Foo(FooRequest) returns (FooResponse);
  1253. // rpc Bar(BarRequest) returns (BarResponse);
  1254. // }
  1255. //
  1256. // // in "bar.proto"
  1257. // package corge.grault
  1258. // service TestService2 {
  1259. // rpc Foo(FooRequest) returns (FooResponse);
  1260. // rpc Baz(BazRequest) returns (BazResponse);
  1261. // }
  1262. FileDescriptorProto foo_file;
  1263. foo_file.set_name("foo.proto");
  1264. AddMessage(&foo_file, "FooRequest");
  1265. AddMessage(&foo_file, "FooResponse");
  1266. AddMessage(&foo_file, "BarRequest");
  1267. AddMessage(&foo_file, "BarResponse");
  1268. AddMessage(&foo_file, "BazRequest");
  1269. AddMessage(&foo_file, "BazResponse");
  1270. ServiceDescriptorProto* service = AddService(&foo_file, "TestService");
  1271. AddMethod(service, "Foo", "FooRequest", "FooResponse");
  1272. AddMethod(service, "Bar", "BarRequest", "BarResponse");
  1273. FileDescriptorProto bar_file;
  1274. bar_file.set_name("bar.proto");
  1275. bar_file.set_package("corge.grault");
  1276. bar_file.add_dependency("foo.proto");
  1277. ServiceDescriptorProto* service2 = AddService(&bar_file, "TestService2");
  1278. AddMethod(service2, "Foo", "FooRequest", "FooResponse");
  1279. AddMethod(service2, "Baz", "BazRequest", "BazResponse");
  1280. // Build the descriptors and get the pointers.
  1281. foo_file_ = pool_.BuildFile(foo_file);
  1282. ASSERT_TRUE(foo_file_ != nullptr);
  1283. bar_file_ = pool_.BuildFile(bar_file);
  1284. ASSERT_TRUE(bar_file_ != nullptr);
  1285. ASSERT_EQ(6, foo_file_->message_type_count());
  1286. foo_request_ = foo_file_->message_type(0);
  1287. foo_response_ = foo_file_->message_type(1);
  1288. bar_request_ = foo_file_->message_type(2);
  1289. bar_response_ = foo_file_->message_type(3);
  1290. baz_request_ = foo_file_->message_type(4);
  1291. baz_response_ = foo_file_->message_type(5);
  1292. ASSERT_EQ(1, foo_file_->service_count());
  1293. service_ = foo_file_->service(0);
  1294. ASSERT_EQ(2, service_->method_count());
  1295. foo_ = service_->method(0);
  1296. bar_ = service_->method(1);
  1297. ASSERT_EQ(1, bar_file_->service_count());
  1298. service2_ = bar_file_->service(0);
  1299. ASSERT_EQ(2, service2_->method_count());
  1300. foo2_ = service2_->method(0);
  1301. baz2_ = service2_->method(1);
  1302. }
  1303. DescriptorPool pool_;
  1304. const FileDescriptor* foo_file_;
  1305. const FileDescriptor* bar_file_;
  1306. const Descriptor* foo_request_;
  1307. const Descriptor* foo_response_;
  1308. const Descriptor* bar_request_;
  1309. const Descriptor* bar_response_;
  1310. const Descriptor* baz_request_;
  1311. const Descriptor* baz_response_;
  1312. const ServiceDescriptor* service_;
  1313. const ServiceDescriptor* service2_;
  1314. const MethodDescriptor* foo_;
  1315. const MethodDescriptor* bar_;
  1316. const MethodDescriptor* foo2_;
  1317. const MethodDescriptor* baz2_;
  1318. };
  1319. TEST_F(ServiceDescriptorTest, Name) {
  1320. EXPECT_EQ("TestService", service_->name());
  1321. EXPECT_EQ("TestService", service_->full_name());
  1322. EXPECT_EQ(foo_file_, service_->file());
  1323. EXPECT_EQ("TestService2", service2_->name());
  1324. EXPECT_EQ("corge.grault.TestService2", service2_->full_name());
  1325. EXPECT_EQ(bar_file_, service2_->file());
  1326. }
  1327. TEST_F(ServiceDescriptorTest, MethodsByIndex) {
  1328. ASSERT_EQ(2, service_->method_count());
  1329. EXPECT_EQ(foo_, service_->method(0));
  1330. EXPECT_EQ(bar_, service_->method(1));
  1331. }
  1332. TEST_F(ServiceDescriptorTest, FindMethodByName) {
  1333. EXPECT_EQ(foo_, service_->FindMethodByName("Foo"));
  1334. EXPECT_EQ(bar_, service_->FindMethodByName("Bar"));
  1335. EXPECT_EQ(foo2_, service2_->FindMethodByName("Foo"));
  1336. EXPECT_EQ(baz2_, service2_->FindMethodByName("Baz"));
  1337. EXPECT_TRUE(service_->FindMethodByName("NoSuchMethod") == nullptr);
  1338. EXPECT_TRUE(service_->FindMethodByName("Baz") == nullptr);
  1339. EXPECT_TRUE(service2_->FindMethodByName("Bar") == nullptr);
  1340. }
  1341. TEST_F(ServiceDescriptorTest, MethodName) {
  1342. EXPECT_EQ("Foo", foo_->name());
  1343. EXPECT_EQ("Bar", bar_->name());
  1344. }
  1345. TEST_F(ServiceDescriptorTest, MethodFullName) {
  1346. EXPECT_EQ("TestService.Foo", foo_->full_name());
  1347. EXPECT_EQ("TestService.Bar", bar_->full_name());
  1348. EXPECT_EQ("corge.grault.TestService2.Foo", foo2_->full_name());
  1349. EXPECT_EQ("corge.grault.TestService2.Baz", baz2_->full_name());
  1350. }
  1351. TEST_F(ServiceDescriptorTest, MethodIndex) {
  1352. EXPECT_EQ(0, foo_->index());
  1353. EXPECT_EQ(1, bar_->index());
  1354. }
  1355. TEST_F(ServiceDescriptorTest, MethodParent) {
  1356. EXPECT_EQ(service_, foo_->service());
  1357. EXPECT_EQ(service_, bar_->service());
  1358. }
  1359. TEST_F(ServiceDescriptorTest, MethodInputType) {
  1360. EXPECT_EQ(foo_request_, foo_->input_type());
  1361. EXPECT_EQ(bar_request_, bar_->input_type());
  1362. }
  1363. TEST_F(ServiceDescriptorTest, MethodOutputType) {
  1364. EXPECT_EQ(foo_response_, foo_->output_type());
  1365. EXPECT_EQ(bar_response_, bar_->output_type());
  1366. }
  1367. // ===================================================================
  1368. // Test nested types.
  1369. class NestedDescriptorTest : public testing::Test {
  1370. protected:
  1371. virtual void SetUp() {
  1372. // Build descriptors for the following definitions:
  1373. //
  1374. // // in "foo.proto"
  1375. // message TestMessage {
  1376. // message Foo {}
  1377. // message Bar {}
  1378. // enum Baz { A = 1; }
  1379. // enum Qux { B = 1; }
  1380. // }
  1381. //
  1382. // // in "bar.proto"
  1383. // package corge.grault;
  1384. // message TestMessage2 {
  1385. // message Foo {}
  1386. // message Baz {}
  1387. // enum Qux { A = 1; }
  1388. // enum Quux { C = 1; }
  1389. // }
  1390. //
  1391. // TestMessage2 is primarily here to test FindNestedTypeByName and friends.
  1392. // All messages created from the same DescriptorPool share the same lookup
  1393. // table, so we need to insure that they don't interfere.
  1394. //
  1395. // We add enum values to the enums in order to test searching for enum
  1396. // values across a message's scope.
  1397. FileDescriptorProto foo_file;
  1398. foo_file.set_name("foo.proto");
  1399. DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
  1400. AddNestedMessage(message, "Foo");
  1401. AddNestedMessage(message, "Bar");
  1402. EnumDescriptorProto* baz = AddNestedEnum(message, "Baz");
  1403. AddEnumValue(baz, "A", 1);
  1404. EnumDescriptorProto* qux = AddNestedEnum(message, "Qux");
  1405. AddEnumValue(qux, "B", 1);
  1406. FileDescriptorProto bar_file;
  1407. bar_file.set_name("bar.proto");
  1408. bar_file.set_package("corge.grault");
  1409. DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
  1410. AddNestedMessage(message2, "Foo");
  1411. AddNestedMessage(message2, "Baz");
  1412. EnumDescriptorProto* qux2 = AddNestedEnum(message2, "Qux");
  1413. AddEnumValue(qux2, "A", 1);
  1414. EnumDescriptorProto* quux2 = AddNestedEnum(message2, "Quux");
  1415. AddEnumValue(quux2, "C", 1);
  1416. // Build the descriptors and get the pointers.
  1417. foo_file_ = pool_.BuildFile(foo_file);
  1418. ASSERT_TRUE(foo_file_ != nullptr);
  1419. bar_file_ = pool_.BuildFile(bar_file);
  1420. ASSERT_TRUE(bar_file_ != nullptr);
  1421. ASSERT_EQ(1, foo_file_->message_type_count());
  1422. message_ = foo_file_->message_type(0);
  1423. ASSERT_EQ(2, message_->nested_type_count());
  1424. foo_ = message_->nested_type(0);
  1425. bar_ = message_->nested_type(1);
  1426. ASSERT_EQ(2, message_->enum_type_count());
  1427. baz_ = message_->enum_type(0);
  1428. qux_ = message_->enum_type(1);
  1429. ASSERT_EQ(1, baz_->value_count());
  1430. a_ = baz_->value(0);
  1431. ASSERT_EQ(1, qux_->value_count());
  1432. b_ = qux_->value(0);
  1433. ASSERT_EQ(1, bar_file_->message_type_count());
  1434. message2_ = bar_file_->message_type(0);
  1435. ASSERT_EQ(2, message2_->nested_type_count());
  1436. foo2_ = message2_->nested_type(0);
  1437. baz2_ = message2_->nested_type(1);
  1438. ASSERT_EQ(2, message2_->enum_type_count());
  1439. qux2_ = message2_->enum_type(0);
  1440. quux2_ = message2_->enum_type(1);
  1441. ASSERT_EQ(1, qux2_->value_count());
  1442. a2_ = qux2_->value(0);
  1443. ASSERT_EQ(1, quux2_->value_count());
  1444. c2_ = quux2_->value(0);
  1445. }
  1446. DescriptorPool pool_;
  1447. const FileDescriptor* foo_file_;
  1448. const FileDescriptor* bar_file_;
  1449. const Descriptor* message_;
  1450. const Descriptor* message2_;
  1451. const Descriptor* foo_;
  1452. const Descriptor* bar_;
  1453. const EnumDescriptor* baz_;
  1454. const EnumDescriptor* qux_;
  1455. const EnumValueDescriptor* a_;
  1456. const EnumValueDescriptor* b_;
  1457. const Descriptor* foo2_;
  1458. const Descriptor* baz2_;
  1459. const EnumDescriptor* qux2_;
  1460. const EnumDescriptor* quux2_;
  1461. const EnumValueDescriptor* a2_;
  1462. const EnumValueDescriptor* c2_;
  1463. };
  1464. TEST_F(NestedDescriptorTest, MessageName) {
  1465. EXPECT_EQ("Foo", foo_->name());
  1466. EXPECT_EQ("Bar", bar_->name());
  1467. EXPECT_EQ("Foo", foo2_->name());
  1468. EXPECT_EQ("Baz", baz2_->name());
  1469. EXPECT_EQ("TestMessage.Foo", foo_->full_name());
  1470. EXPECT_EQ("TestMessage.Bar", bar_->full_name());
  1471. EXPECT_EQ("corge.grault.TestMessage2.Foo", foo2_->full_name());
  1472. EXPECT_EQ("corge.grault.TestMessage2.Baz", baz2_->full_name());
  1473. }
  1474. TEST_F(NestedDescriptorTest, MessageContainingType) {
  1475. EXPECT_EQ(message_, foo_->containing_type());
  1476. EXPECT_EQ(message_, bar_->containing_type());
  1477. EXPECT_EQ(message2_, foo2_->containing_type());
  1478. EXPECT_EQ(message2_, baz2_->containing_type());
  1479. }
  1480. TEST_F(NestedDescriptorTest, NestedMessagesByIndex) {
  1481. ASSERT_EQ(2, message_->nested_type_count());
  1482. EXPECT_EQ(foo_, message_->nested_type(0));
  1483. EXPECT_EQ(bar_, message_->nested_type(1));
  1484. }
  1485. TEST_F(NestedDescriptorTest, FindFieldByNameDoesntFindNestedTypes) {
  1486. EXPECT_TRUE(message_->FindFieldByName("Foo") == nullptr);
  1487. EXPECT_TRUE(message_->FindFieldByName("Qux") == nullptr);
  1488. EXPECT_TRUE(message_->FindExtensionByName("Foo") == nullptr);
  1489. EXPECT_TRUE(message_->FindExtensionByName("Qux") == nullptr);
  1490. }
  1491. TEST_F(NestedDescriptorTest, FindNestedTypeByName) {
  1492. EXPECT_EQ(foo_, message_->FindNestedTypeByName("Foo"));
  1493. EXPECT_EQ(bar_, message_->FindNestedTypeByName("Bar"));
  1494. EXPECT_EQ(foo2_, message2_->FindNestedTypeByName("Foo"));
  1495. EXPECT_EQ(baz2_, message2_->FindNestedTypeByName("Baz"));
  1496. EXPECT_TRUE(message_->FindNestedTypeByName("NoSuchType") == nullptr);
  1497. EXPECT_TRUE(message_->FindNestedTypeByName("Baz") == nullptr);
  1498. EXPECT_TRUE(message2_->FindNestedTypeByName("Bar") == nullptr);
  1499. EXPECT_TRUE(message_->FindNestedTypeByName("Qux") == nullptr);
  1500. }
  1501. TEST_F(NestedDescriptorTest, EnumName) {
  1502. EXPECT_EQ("Baz", baz_->name());
  1503. EXPECT_EQ("Qux", qux_->name());
  1504. EXPECT_EQ("Qux", qux2_->name());
  1505. EXPECT_EQ("Quux", quux2_->name());
  1506. EXPECT_EQ("TestMessage.Baz", baz_->full_name());
  1507. EXPECT_EQ("TestMessage.Qux", qux_->full_name());
  1508. EXPECT_EQ("corge.grault.TestMessage2.Qux", qux2_->full_name());
  1509. EXPECT_EQ("corge.grault.TestMessage2.Quux", quux2_->full_name());
  1510. }
  1511. TEST_F(NestedDescriptorTest, EnumContainingType) {
  1512. EXPECT_EQ(message_, baz_->containing_type());
  1513. EXPECT_EQ(message_, qux_->containing_type());
  1514. EXPECT_EQ(message2_, qux2_->containing_type());
  1515. EXPECT_EQ(message2_, quux2_->containing_type());
  1516. }
  1517. TEST_F(NestedDescriptorTest, NestedEnumsByIndex) {
  1518. ASSERT_EQ(2, message_->nested_type_count());
  1519. EXPECT_EQ(foo_, message_->nested_type(0));
  1520. EXPECT_EQ(bar_, message_->nested_type(1));
  1521. }
  1522. TEST_F(NestedDescriptorTest, FindEnumTypeByName) {
  1523. EXPECT_EQ(baz_, message_->FindEnumTypeByName("Baz"));
  1524. EXPECT_EQ(qux_, message_->FindEnumTypeByName("Qux"));
  1525. EXPECT_EQ(qux2_, message2_->FindEnumTypeByName("Qux"));
  1526. EXPECT_EQ(quux2_, message2_->FindEnumTypeByName("Quux"));
  1527. EXPECT_TRUE(message_->FindEnumTypeByName("NoSuchType") == nullptr);
  1528. EXPECT_TRUE(message_->FindEnumTypeByName("Quux") == nullptr);
  1529. EXPECT_TRUE(message2_->FindEnumTypeByName("Baz") == nullptr);
  1530. EXPECT_TRUE(message_->FindEnumTypeByName("Foo") == nullptr);
  1531. }
  1532. TEST_F(NestedDescriptorTest, FindEnumValueByName) {
  1533. EXPECT_EQ(a_, message_->FindEnumValueByName("A"));
  1534. EXPECT_EQ(b_, message_->FindEnumValueByName("B"));
  1535. EXPECT_EQ(a2_, message2_->FindEnumValueByName("A"));
  1536. EXPECT_EQ(c2_, message2_->FindEnumValueByName("C"));
  1537. EXPECT_TRUE(message_->FindEnumValueByName("NO_SUCH_VALUE") == nullptr);
  1538. EXPECT_TRUE(message_->FindEnumValueByName("C") == nullptr);
  1539. EXPECT_TRUE(message2_->FindEnumValueByName("B") == nullptr);
  1540. EXPECT_TRUE(message_->FindEnumValueByName("Foo") == nullptr);
  1541. }
  1542. // ===================================================================
  1543. // Test extensions.
  1544. class ExtensionDescriptorTest : public testing::Test {
  1545. protected:
  1546. virtual void SetUp() {
  1547. // Build descriptors for the following definitions:
  1548. //
  1549. // enum Baz {}
  1550. // message Qux {}
  1551. //
  1552. // message Foo {
  1553. // extensions 10 to 19;
  1554. // extensions 30 to 39;
  1555. // }
  1556. // extends Foo with optional int32 foo_int32 = 10;
  1557. // extends Foo with repeated TestEnum foo_enum = 19;
  1558. // message Bar {
  1559. // extends Foo with optional Qux foo_message = 30;
  1560. // // (using Qux as the group type)
  1561. // extends Foo with repeated group foo_group = 39;
  1562. // }
  1563. FileDescriptorProto foo_file;
  1564. foo_file.set_name("foo.proto");
  1565. AddEmptyEnum(&foo_file, "Baz");
  1566. AddMessage(&foo_file, "Qux");
  1567. DescriptorProto* foo = AddMessage(&foo_file, "Foo");
  1568. AddExtensionRange(foo, 10, 20);
  1569. AddExtensionRange(foo, 30, 40);
  1570. AddExtension(&foo_file, "Foo", "foo_int32", 10,
  1571. FieldDescriptorProto::LABEL_OPTIONAL,
  1572. FieldDescriptorProto::TYPE_INT32);
  1573. AddExtension(&foo_file, "Foo", "foo_enum", 19,
  1574. FieldDescriptorProto::LABEL_REPEATED,
  1575. FieldDescriptorProto::TYPE_ENUM)
  1576. ->set_type_name("Baz");
  1577. DescriptorProto* bar = AddMessage(&foo_file, "Bar");
  1578. AddNestedExtension(bar, "Foo", "foo_message", 30,
  1579. FieldDescriptorProto::LABEL_OPTIONAL,
  1580. FieldDescriptorProto::TYPE_MESSAGE)
  1581. ->set_type_name("Qux");
  1582. AddNestedExtension(bar, "Foo", "foo_group", 39,
  1583. FieldDescriptorProto::LABEL_REPEATED,
  1584. FieldDescriptorProto::TYPE_GROUP)
  1585. ->set_type_name("Qux");
  1586. // Build the descriptors and get the pointers.
  1587. foo_file_ = pool_.BuildFile(foo_file);
  1588. ASSERT_TRUE(foo_file_ != nullptr);
  1589. ASSERT_EQ(1, foo_file_->enum_type_count());
  1590. baz_ = foo_file_->enum_type(0);
  1591. ASSERT_EQ(3, foo_file_->message_type_count());
  1592. qux_ = foo_file_->message_type(0);
  1593. foo_ = foo_file_->message_type(1);
  1594. bar_ = foo_file_->message_type(2);
  1595. }
  1596. DescriptorPool pool_;
  1597. const FileDescriptor* foo_file_;
  1598. const Descriptor* foo_;
  1599. const Descriptor* bar_;
  1600. const EnumDescriptor* baz_;
  1601. const Descriptor* qux_;
  1602. };
  1603. TEST_F(ExtensionDescriptorTest, ExtensionRanges) {
  1604. EXPECT_EQ(0, bar_->extension_range_count());
  1605. ASSERT_EQ(2, foo_->extension_range_count());
  1606. EXPECT_EQ(10, foo_->extension_range(0)->start);
  1607. EXPECT_EQ(30, foo_->extension_range(1)->start);
  1608. EXPECT_EQ(20, foo_->extension_range(0)->end);
  1609. EXPECT_EQ(40, foo_->extension_range(1)->end);
  1610. };
  1611. TEST_F(ExtensionDescriptorTest, Extensions) {
  1612. EXPECT_EQ(0, foo_->extension_count());
  1613. ASSERT_EQ(2, foo_file_->extension_count());
  1614. ASSERT_EQ(2, bar_->extension_count());
  1615. EXPECT_TRUE(foo_file_->extension(0)->is_extension());
  1616. EXPECT_TRUE(foo_file_->extension(1)->is_extension());
  1617. EXPECT_TRUE(bar_->extension(0)->is_extension());
  1618. EXPECT_TRUE(bar_->extension(1)->is_extension());
  1619. EXPECT_EQ("foo_int32", foo_file_->extension(0)->name());
  1620. EXPECT_EQ("foo_enum", foo_file_->extension(1)->name());
  1621. EXPECT_EQ("foo_message", bar_->extension(0)->name());
  1622. EXPECT_EQ("foo_group", bar_->extension(1)->name());
  1623. EXPECT_EQ(10, foo_file_->extension(0)->number());
  1624. EXPECT_EQ(19, foo_file_->extension(1)->number());
  1625. EXPECT_EQ(30, bar_->extension(0)->number());
  1626. EXPECT_EQ(39, bar_->extension(1)->number());
  1627. EXPECT_EQ(FieldDescriptor::TYPE_INT32, foo_file_->extension(0)->type());
  1628. EXPECT_EQ(FieldDescriptor::TYPE_ENUM, foo_file_->extension(1)->type());
  1629. EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, bar_->extension(0)->type());
  1630. EXPECT_EQ(FieldDescriptor::TYPE_GROUP, bar_->extension(1)->type());
  1631. EXPECT_EQ(baz_, foo_file_->extension(1)->enum_type());
  1632. EXPECT_EQ(qux_, bar_->extension(0)->message_type());
  1633. EXPECT_EQ(qux_, bar_->extension(1)->message_type());
  1634. EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, foo_file_->extension(0)->label());
  1635. EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, foo_file_->extension(1)->label());
  1636. EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, bar_->extension(0)->label());
  1637. EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, bar_->extension(1)->label());
  1638. EXPECT_EQ(foo_, foo_file_->extension(0)->containing_type());
  1639. EXPECT_EQ(foo_, foo_file_->extension(1)->containing_type());
  1640. EXPECT_EQ(foo_, bar_->extension(0)->containing_type());
  1641. EXPECT_EQ(foo_, bar_->extension(1)->containing_type());
  1642. EXPECT_TRUE(foo_file_->extension(0)->extension_scope() == nullptr);
  1643. EXPECT_TRUE(foo_file_->extension(1)->extension_scope() == nullptr);
  1644. EXPECT_EQ(bar_, bar_->extension(0)->extension_scope());
  1645. EXPECT_EQ(bar_, bar_->extension(1)->extension_scope());
  1646. };
  1647. TEST_F(ExtensionDescriptorTest, IsExtensionNumber) {
  1648. EXPECT_FALSE(foo_->IsExtensionNumber(9));
  1649. EXPECT_TRUE(foo_->IsExtensionNumber(10));
  1650. EXPECT_TRUE(foo_->IsExtensionNumber(19));
  1651. EXPECT_FALSE(foo_->IsExtensionNumber(20));
  1652. EXPECT_FALSE(foo_->IsExtensionNumber(29));
  1653. EXPECT_TRUE(foo_->IsExtensionNumber(30));
  1654. EXPECT_TRUE(foo_->IsExtensionNumber(39));
  1655. EXPECT_FALSE(foo_->IsExtensionNumber(40));
  1656. }
  1657. TEST_F(ExtensionDescriptorTest, FindExtensionByName) {
  1658. // Note that FileDescriptor::FindExtensionByName() is tested by
  1659. // FileDescriptorTest.
  1660. ASSERT_EQ(2, bar_->extension_count());
  1661. EXPECT_EQ(bar_->extension(0), bar_->FindExtensionByName("foo_message"));
  1662. EXPECT_EQ(bar_->extension(1), bar_->FindExtensionByName("foo_group"));
  1663. EXPECT_TRUE(bar_->FindExtensionByName("no_such_extension") == nullptr);
  1664. EXPECT_TRUE(foo_->FindExtensionByName("foo_int32") == nullptr);
  1665. EXPECT_TRUE(foo_->FindExtensionByName("foo_message") == nullptr);
  1666. }
  1667. TEST_F(ExtensionDescriptorTest, FindExtensionByPrintableName) {
  1668. EXPECT_TRUE(pool_.FindExtensionByPrintableName(foo_, "no_such_extension") ==
  1669. nullptr);
  1670. EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "no_such_extension") ==
  1671. nullptr);
  1672. ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "Bar.foo_message") ==
  1673. nullptr);
  1674. ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "Bar.foo_group") ==
  1675. nullptr);
  1676. EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_message") ==
  1677. nullptr);
  1678. EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_group") == nullptr);
  1679. EXPECT_EQ(bar_->FindExtensionByName("foo_message"),
  1680. pool_.FindExtensionByPrintableName(foo_, "Bar.foo_message"));
  1681. EXPECT_EQ(bar_->FindExtensionByName("foo_group"),
  1682. pool_.FindExtensionByPrintableName(foo_, "Bar.foo_group"));
  1683. ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "foo_int32") ==
  1684. nullptr);
  1685. ASSERT_FALSE(pool_.FindExtensionByPrintableName(foo_, "foo_enum") == nullptr);
  1686. EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_int32") == nullptr);
  1687. EXPECT_TRUE(pool_.FindExtensionByPrintableName(bar_, "foo_enum") == nullptr);
  1688. EXPECT_EQ(foo_file_->FindExtensionByName("foo_int32"),
  1689. pool_.FindExtensionByPrintableName(foo_, "foo_int32"));
  1690. EXPECT_EQ(foo_file_->FindExtensionByName("foo_enum"),
  1691. pool_.FindExtensionByPrintableName(foo_, "foo_enum"));
  1692. }
  1693. TEST_F(ExtensionDescriptorTest, FindAllExtensions) {
  1694. std::vector<const FieldDescriptor*> extensions;
  1695. pool_.FindAllExtensions(foo_, &extensions);
  1696. ASSERT_EQ(4, extensions.size());
  1697. EXPECT_EQ(10, extensions[0]->number());
  1698. EXPECT_EQ(19, extensions[1]->number());
  1699. EXPECT_EQ(30, extensions[2]->number());
  1700. EXPECT_EQ(39, extensions[3]->number());
  1701. }
  1702. TEST_F(ExtensionDescriptorTest, DuplicateFieldNumber) {
  1703. DescriptorPool pool;
  1704. FileDescriptorProto file_proto;
  1705. // Add "google/protobuf/descriptor.proto".
  1706. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  1707. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  1708. // Add "foo.proto":
  1709. // import "google/protobuf/descriptor.proto";
  1710. // extend google.protobuf.FieldOptions {
  1711. // optional int32 option1 = 1000;
  1712. // }
  1713. file_proto.Clear();
  1714. file_proto.set_name("foo.proto");
  1715. file_proto.add_dependency("google/protobuf/descriptor.proto");
  1716. AddExtension(&file_proto, "google.protobuf.FieldOptions", "option1", 1000,
  1717. FieldDescriptorProto::LABEL_OPTIONAL,
  1718. FieldDescriptorProto::TYPE_INT32);
  1719. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  1720. // Add "bar.proto":
  1721. // import "google/protobuf/descriptor.proto";
  1722. // extend google.protobuf.FieldOptions {
  1723. // optional int32 option2 = 1000;
  1724. // }
  1725. file_proto.Clear();
  1726. file_proto.set_name("bar.proto");
  1727. file_proto.add_dependency("google/protobuf/descriptor.proto");
  1728. AddExtension(&file_proto, "google.protobuf.FieldOptions", "option2", 1000,
  1729. FieldDescriptorProto::LABEL_OPTIONAL,
  1730. FieldDescriptorProto::TYPE_INT32);
  1731. // Currently we only generate a warning for conflicting extension numbers.
  1732. // TODO(xiaofeng): Change it to an error.
  1733. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  1734. }
  1735. // ===================================================================
  1736. // Test reserved fields.
  1737. class ReservedDescriptorTest : public testing::Test {
  1738. protected:
  1739. virtual void SetUp() {
  1740. // Build descriptors for the following definitions:
  1741. //
  1742. // message Foo {
  1743. // reserved 2, 9 to 11, 15;
  1744. // reserved "foo", "bar";
  1745. // }
  1746. FileDescriptorProto foo_file;
  1747. foo_file.set_name("foo.proto");
  1748. DescriptorProto* foo = AddMessage(&foo_file, "Foo");
  1749. AddReservedRange(foo, 2, 3);
  1750. AddReservedRange(foo, 9, 12);
  1751. AddReservedRange(foo, 15, 16);
  1752. foo->add_reserved_name("foo");
  1753. foo->add_reserved_name("bar");
  1754. // Build the descriptors and get the pointers.
  1755. foo_file_ = pool_.BuildFile(foo_file);
  1756. ASSERT_TRUE(foo_file_ != nullptr);
  1757. ASSERT_EQ(1, foo_file_->message_type_count());
  1758. foo_ = foo_file_->message_type(0);
  1759. }
  1760. DescriptorPool pool_;
  1761. const FileDescriptor* foo_file_;
  1762. const Descriptor* foo_;
  1763. };
  1764. TEST_F(ReservedDescriptorTest, ReservedRanges) {
  1765. ASSERT_EQ(3, foo_->reserved_range_count());
  1766. EXPECT_EQ(2, foo_->reserved_range(0)->start);
  1767. EXPECT_EQ(3, foo_->reserved_range(0)->end);
  1768. EXPECT_EQ(9, foo_->reserved_range(1)->start);
  1769. EXPECT_EQ(12, foo_->reserved_range(1)->end);
  1770. EXPECT_EQ(15, foo_->reserved_range(2)->start);
  1771. EXPECT_EQ(16, foo_->reserved_range(2)->end);
  1772. };
  1773. TEST_F(ReservedDescriptorTest, IsReservedNumber) {
  1774. EXPECT_FALSE(foo_->IsReservedNumber(1));
  1775. EXPECT_TRUE(foo_->IsReservedNumber(2));
  1776. EXPECT_FALSE(foo_->IsReservedNumber(3));
  1777. EXPECT_FALSE(foo_->IsReservedNumber(8));
  1778. EXPECT_TRUE(foo_->IsReservedNumber(9));
  1779. EXPECT_TRUE(foo_->IsReservedNumber(10));
  1780. EXPECT_TRUE(foo_->IsReservedNumber(11));
  1781. EXPECT_FALSE(foo_->IsReservedNumber(12));
  1782. EXPECT_FALSE(foo_->IsReservedNumber(13));
  1783. EXPECT_FALSE(foo_->IsReservedNumber(14));
  1784. EXPECT_TRUE(foo_->IsReservedNumber(15));
  1785. EXPECT_FALSE(foo_->IsReservedNumber(16));
  1786. };
  1787. TEST_F(ReservedDescriptorTest, ReservedNames) {
  1788. ASSERT_EQ(2, foo_->reserved_name_count());
  1789. EXPECT_EQ("foo", foo_->reserved_name(0));
  1790. EXPECT_EQ("bar", foo_->reserved_name(1));
  1791. };
  1792. TEST_F(ReservedDescriptorTest, IsReservedName) {
  1793. EXPECT_TRUE(foo_->IsReservedName("foo"));
  1794. EXPECT_TRUE(foo_->IsReservedName("bar"));
  1795. EXPECT_FALSE(foo_->IsReservedName("baz"));
  1796. };
  1797. // ===================================================================
  1798. // Test reserved enum fields.
  1799. class ReservedEnumDescriptorTest : public testing::Test {
  1800. protected:
  1801. virtual void SetUp() {
  1802. // Build descriptors for the following definitions:
  1803. //
  1804. // enum Foo {
  1805. // BAR = 1;
  1806. // reserved 2, 9 to 11, 15;
  1807. // reserved "foo", "bar";
  1808. // }
  1809. FileDescriptorProto foo_file;
  1810. foo_file.set_name("foo.proto");
  1811. EnumDescriptorProto* foo = AddEnum(&foo_file, "Foo");
  1812. EnumDescriptorProto* edge1 = AddEnum(&foo_file, "Edge1");
  1813. EnumDescriptorProto* edge2 = AddEnum(&foo_file, "Edge2");
  1814. AddEnumValue(foo, "BAR", 4);
  1815. AddReservedRange(foo, -5, -3);
  1816. AddReservedRange(foo, -2, 1);
  1817. AddReservedRange(foo, 2, 3);
  1818. AddReservedRange(foo, 9, 12);
  1819. AddReservedRange(foo, 15, 16);
  1820. foo->add_reserved_name("foo");
  1821. foo->add_reserved_name("bar");
  1822. // Some additional edge cases that cover most or all of the range of enum
  1823. // values
  1824. // Note: We use INT_MAX as the maximum reserved range upper bound,
  1825. // inclusive.
  1826. AddEnumValue(edge1, "EDGE1", 1);
  1827. AddReservedRange(edge1, 10, INT_MAX);
  1828. AddEnumValue(edge2, "EDGE2", 15);
  1829. AddReservedRange(edge2, INT_MIN, 10);
  1830. // Build the descriptors and get the pointers.
  1831. foo_file_ = pool_.BuildFile(foo_file);
  1832. ASSERT_TRUE(foo_file_ != nullptr);
  1833. ASSERT_EQ(3, foo_file_->enum_type_count());
  1834. foo_ = foo_file_->enum_type(0);
  1835. edge1_ = foo_file_->enum_type(1);
  1836. edge2_ = foo_file_->enum_type(2);
  1837. }
  1838. DescriptorPool pool_;
  1839. const FileDescriptor* foo_file_;
  1840. const EnumDescriptor* foo_;
  1841. const EnumDescriptor* edge1_;
  1842. const EnumDescriptor* edge2_;
  1843. };
  1844. TEST_F(ReservedEnumDescriptorTest, ReservedRanges) {
  1845. ASSERT_EQ(5, foo_->reserved_range_count());
  1846. EXPECT_EQ(-5, foo_->reserved_range(0)->start);
  1847. EXPECT_EQ(-3, foo_->reserved_range(0)->end);
  1848. EXPECT_EQ(-2, foo_->reserved_range(1)->start);
  1849. EXPECT_EQ(1, foo_->reserved_range(1)->end);
  1850. EXPECT_EQ(2, foo_->reserved_range(2)->start);
  1851. EXPECT_EQ(3, foo_->reserved_range(2)->end);
  1852. EXPECT_EQ(9, foo_->reserved_range(3)->start);
  1853. EXPECT_EQ(12, foo_->reserved_range(3)->end);
  1854. EXPECT_EQ(15, foo_->reserved_range(4)->start);
  1855. EXPECT_EQ(16, foo_->reserved_range(4)->end);
  1856. ASSERT_EQ(1, edge1_->reserved_range_count());
  1857. EXPECT_EQ(10, edge1_->reserved_range(0)->start);
  1858. EXPECT_EQ(INT_MAX, edge1_->reserved_range(0)->end);
  1859. ASSERT_EQ(1, edge2_->reserved_range_count());
  1860. EXPECT_EQ(INT_MIN, edge2_->reserved_range(0)->start);
  1861. EXPECT_EQ(10, edge2_->reserved_range(0)->end);
  1862. }
  1863. TEST_F(ReservedEnumDescriptorTest, IsReservedNumber) {
  1864. EXPECT_TRUE(foo_->IsReservedNumber(-5));
  1865. EXPECT_TRUE(foo_->IsReservedNumber(-4));
  1866. EXPECT_TRUE(foo_->IsReservedNumber(-3));
  1867. EXPECT_TRUE(foo_->IsReservedNumber(-2));
  1868. EXPECT_TRUE(foo_->IsReservedNumber(-1));
  1869. EXPECT_TRUE(foo_->IsReservedNumber(0));
  1870. EXPECT_TRUE(foo_->IsReservedNumber(1));
  1871. EXPECT_TRUE(foo_->IsReservedNumber(2));
  1872. EXPECT_TRUE(foo_->IsReservedNumber(3));
  1873. EXPECT_FALSE(foo_->IsReservedNumber(8));
  1874. EXPECT_TRUE(foo_->IsReservedNumber(9));
  1875. EXPECT_TRUE(foo_->IsReservedNumber(10));
  1876. EXPECT_TRUE(foo_->IsReservedNumber(11));
  1877. EXPECT_TRUE(foo_->IsReservedNumber(12));
  1878. EXPECT_FALSE(foo_->IsReservedNumber(13));
  1879. EXPECT_FALSE(foo_->IsReservedNumber(13));
  1880. EXPECT_FALSE(foo_->IsReservedNumber(14));
  1881. EXPECT_TRUE(foo_->IsReservedNumber(15));
  1882. EXPECT_TRUE(foo_->IsReservedNumber(16));
  1883. EXPECT_FALSE(foo_->IsReservedNumber(17));
  1884. EXPECT_FALSE(edge1_->IsReservedNumber(9));
  1885. EXPECT_TRUE(edge1_->IsReservedNumber(10));
  1886. EXPECT_TRUE(edge1_->IsReservedNumber(INT_MAX - 1));
  1887. EXPECT_TRUE(edge1_->IsReservedNumber(INT_MAX));
  1888. EXPECT_TRUE(edge2_->IsReservedNumber(INT_MIN));
  1889. EXPECT_TRUE(edge2_->IsReservedNumber(9));
  1890. EXPECT_TRUE(edge2_->IsReservedNumber(10));
  1891. EXPECT_FALSE(edge2_->IsReservedNumber(11));
  1892. }
  1893. TEST_F(ReservedEnumDescriptorTest, ReservedNames) {
  1894. ASSERT_EQ(2, foo_->reserved_name_count());
  1895. EXPECT_EQ("foo", foo_->reserved_name(0));
  1896. EXPECT_EQ("bar", foo_->reserved_name(1));
  1897. }
  1898. TEST_F(ReservedEnumDescriptorTest, IsReservedName) {
  1899. EXPECT_TRUE(foo_->IsReservedName("foo"));
  1900. EXPECT_TRUE(foo_->IsReservedName("bar"));
  1901. EXPECT_FALSE(foo_->IsReservedName("baz"));
  1902. }
  1903. // ===================================================================
  1904. class MiscTest : public testing::Test {
  1905. protected:
  1906. // Function which makes a field descriptor of the given type.
  1907. const FieldDescriptor* GetFieldDescriptorOfType(FieldDescriptor::Type type) {
  1908. FileDescriptorProto file_proto;
  1909. file_proto.set_name("foo.proto");
  1910. AddEmptyEnum(&file_proto, "DummyEnum");
  1911. DescriptorProto* message = AddMessage(&file_proto, "TestMessage");
  1912. FieldDescriptorProto* field = AddField(
  1913. message, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL,
  1914. static_cast<FieldDescriptorProto::Type>(static_cast<int>(type)));
  1915. if (type == FieldDescriptor::TYPE_MESSAGE ||
  1916. type == FieldDescriptor::TYPE_GROUP) {
  1917. field->set_type_name("TestMessage");
  1918. } else if (type == FieldDescriptor::TYPE_ENUM) {
  1919. field->set_type_name("DummyEnum");
  1920. }
  1921. // Build the descriptors and get the pointers.
  1922. pool_.reset(new DescriptorPool());
  1923. const FileDescriptor* file = pool_->BuildFile(file_proto);
  1924. if (file != nullptr && file->message_type_count() == 1 &&
  1925. file->message_type(0)->field_count() == 1) {
  1926. return file->message_type(0)->field(0);
  1927. } else {
  1928. return nullptr;
  1929. }
  1930. }
  1931. const char* GetTypeNameForFieldType(FieldDescriptor::Type type) {
  1932. const FieldDescriptor* field = GetFieldDescriptorOfType(type);
  1933. return field != nullptr ? field->type_name() : "";
  1934. }
  1935. FieldDescriptor::CppType GetCppTypeForFieldType(FieldDescriptor::Type type) {
  1936. const FieldDescriptor* field = GetFieldDescriptorOfType(type);
  1937. return field != nullptr ? field->cpp_type()
  1938. : static_cast<FieldDescriptor::CppType>(0);
  1939. }
  1940. const char* GetCppTypeNameForFieldType(FieldDescriptor::Type type) {
  1941. const FieldDescriptor* field = GetFieldDescriptorOfType(type);
  1942. return field != nullptr ? field->cpp_type_name() : "";
  1943. }
  1944. const Descriptor* GetMessageDescriptorForFieldType(
  1945. FieldDescriptor::Type type) {
  1946. const FieldDescriptor* field = GetFieldDescriptorOfType(type);
  1947. return field != nullptr ? field->message_type() : nullptr;
  1948. }
  1949. const EnumDescriptor* GetEnumDescriptorForFieldType(
  1950. FieldDescriptor::Type type) {
  1951. const FieldDescriptor* field = GetFieldDescriptorOfType(type);
  1952. return field != nullptr ? field->enum_type() : nullptr;
  1953. }
  1954. std::unique_ptr<DescriptorPool> pool_;
  1955. };
  1956. TEST_F(MiscTest, TypeNames) {
  1957. // Test that correct type names are returned.
  1958. typedef FieldDescriptor FD; // avoid ugly line wrapping
  1959. EXPECT_STREQ("double", GetTypeNameForFieldType(FD::TYPE_DOUBLE));
  1960. EXPECT_STREQ("float", GetTypeNameForFieldType(FD::TYPE_FLOAT));
  1961. EXPECT_STREQ("int64", GetTypeNameForFieldType(FD::TYPE_INT64));
  1962. EXPECT_STREQ("uint64", GetTypeNameForFieldType(FD::TYPE_UINT64));
  1963. EXPECT_STREQ("int32", GetTypeNameForFieldType(FD::TYPE_INT32));
  1964. EXPECT_STREQ("fixed64", GetTypeNameForFieldType(FD::TYPE_FIXED64));
  1965. EXPECT_STREQ("fixed32", GetTypeNameForFieldType(FD::TYPE_FIXED32));
  1966. EXPECT_STREQ("bool", GetTypeNameForFieldType(FD::TYPE_BOOL));
  1967. EXPECT_STREQ("string", GetTypeNameForFieldType(FD::TYPE_STRING));
  1968. EXPECT_STREQ("group", GetTypeNameForFieldType(FD::TYPE_GROUP));
  1969. EXPECT_STREQ("message", GetTypeNameForFieldType(FD::TYPE_MESSAGE));
  1970. EXPECT_STREQ("bytes", GetTypeNameForFieldType(FD::TYPE_BYTES));
  1971. EXPECT_STREQ("uint32", GetTypeNameForFieldType(FD::TYPE_UINT32));
  1972. EXPECT_STREQ("enum", GetTypeNameForFieldType(FD::TYPE_ENUM));
  1973. EXPECT_STREQ("sfixed32", GetTypeNameForFieldType(FD::TYPE_SFIXED32));
  1974. EXPECT_STREQ("sfixed64", GetTypeNameForFieldType(FD::TYPE_SFIXED64));
  1975. EXPECT_STREQ("sint32", GetTypeNameForFieldType(FD::TYPE_SINT32));
  1976. EXPECT_STREQ("sint64", GetTypeNameForFieldType(FD::TYPE_SINT64));
  1977. }
  1978. TEST_F(MiscTest, StaticTypeNames) {
  1979. // Test that correct type names are returned.
  1980. typedef FieldDescriptor FD; // avoid ugly line wrapping
  1981. EXPECT_STREQ("double", FD::TypeName(FD::TYPE_DOUBLE));
  1982. EXPECT_STREQ("float", FD::TypeName(FD::TYPE_FLOAT));
  1983. EXPECT_STREQ("int64", FD::TypeName(FD::TYPE_INT64));
  1984. EXPECT_STREQ("uint64", FD::TypeName(FD::TYPE_UINT64));
  1985. EXPECT_STREQ("int32", FD::TypeName(FD::TYPE_INT32));
  1986. EXPECT_STREQ("fixed64", FD::TypeName(FD::TYPE_FIXED64));
  1987. EXPECT_STREQ("fixed32", FD::TypeName(FD::TYPE_FIXED32));
  1988. EXPECT_STREQ("bool", FD::TypeName(FD::TYPE_BOOL));
  1989. EXPECT_STREQ("string", FD::TypeName(FD::TYPE_STRING));
  1990. EXPECT_STREQ("group", FD::TypeName(FD::TYPE_GROUP));
  1991. EXPECT_STREQ("message", FD::TypeName(FD::TYPE_MESSAGE));
  1992. EXPECT_STREQ("bytes", FD::TypeName(FD::TYPE_BYTES));
  1993. EXPECT_STREQ("uint32", FD::TypeName(FD::TYPE_UINT32));
  1994. EXPECT_STREQ("enum", FD::TypeName(FD::TYPE_ENUM));
  1995. EXPECT_STREQ("sfixed32", FD::TypeName(FD::TYPE_SFIXED32));
  1996. EXPECT_STREQ("sfixed64", FD::TypeName(FD::TYPE_SFIXED64));
  1997. EXPECT_STREQ("sint32", FD::TypeName(FD::TYPE_SINT32));
  1998. EXPECT_STREQ("sint64", FD::TypeName(FD::TYPE_SINT64));
  1999. }
  2000. TEST_F(MiscTest, CppTypes) {
  2001. // Test that CPP types are assigned correctly.
  2002. typedef FieldDescriptor FD; // avoid ugly line wrapping
  2003. EXPECT_EQ(FD::CPPTYPE_DOUBLE, GetCppTypeForFieldType(FD::TYPE_DOUBLE));
  2004. EXPECT_EQ(FD::CPPTYPE_FLOAT, GetCppTypeForFieldType(FD::TYPE_FLOAT));
  2005. EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_INT64));
  2006. EXPECT_EQ(FD::CPPTYPE_UINT64, GetCppTypeForFieldType(FD::TYPE_UINT64));
  2007. EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_INT32));
  2008. EXPECT_EQ(FD::CPPTYPE_UINT64, GetCppTypeForFieldType(FD::TYPE_FIXED64));
  2009. EXPECT_EQ(FD::CPPTYPE_UINT32, GetCppTypeForFieldType(FD::TYPE_FIXED32));
  2010. EXPECT_EQ(FD::CPPTYPE_BOOL, GetCppTypeForFieldType(FD::TYPE_BOOL));
  2011. EXPECT_EQ(FD::CPPTYPE_STRING, GetCppTypeForFieldType(FD::TYPE_STRING));
  2012. EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_GROUP));
  2013. EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_MESSAGE));
  2014. EXPECT_EQ(FD::CPPTYPE_STRING, GetCppTypeForFieldType(FD::TYPE_BYTES));
  2015. EXPECT_EQ(FD::CPPTYPE_UINT32, GetCppTypeForFieldType(FD::TYPE_UINT32));
  2016. EXPECT_EQ(FD::CPPTYPE_ENUM, GetCppTypeForFieldType(FD::TYPE_ENUM));
  2017. EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_SFIXED32));
  2018. EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_SFIXED64));
  2019. EXPECT_EQ(FD::CPPTYPE_INT32, GetCppTypeForFieldType(FD::TYPE_SINT32));
  2020. EXPECT_EQ(FD::CPPTYPE_INT64, GetCppTypeForFieldType(FD::TYPE_SINT64));
  2021. }
  2022. TEST_F(MiscTest, CppTypeNames) {
  2023. // Test that correct CPP type names are returned.
  2024. typedef FieldDescriptor FD; // avoid ugly line wrapping
  2025. EXPECT_STREQ("double", GetCppTypeNameForFieldType(FD::TYPE_DOUBLE));
  2026. EXPECT_STREQ("float", GetCppTypeNameForFieldType(FD::TYPE_FLOAT));
  2027. EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_INT64));
  2028. EXPECT_STREQ("uint64", GetCppTypeNameForFieldType(FD::TYPE_UINT64));
  2029. EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_INT32));
  2030. EXPECT_STREQ("uint64", GetCppTypeNameForFieldType(FD::TYPE_FIXED64));
  2031. EXPECT_STREQ("uint32", GetCppTypeNameForFieldType(FD::TYPE_FIXED32));
  2032. EXPECT_STREQ("bool", GetCppTypeNameForFieldType(FD::TYPE_BOOL));
  2033. EXPECT_STREQ("string", GetCppTypeNameForFieldType(FD::TYPE_STRING));
  2034. EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_GROUP));
  2035. EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_MESSAGE));
  2036. EXPECT_STREQ("string", GetCppTypeNameForFieldType(FD::TYPE_BYTES));
  2037. EXPECT_STREQ("uint32", GetCppTypeNameForFieldType(FD::TYPE_UINT32));
  2038. EXPECT_STREQ("enum", GetCppTypeNameForFieldType(FD::TYPE_ENUM));
  2039. EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_SFIXED32));
  2040. EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_SFIXED64));
  2041. EXPECT_STREQ("int32", GetCppTypeNameForFieldType(FD::TYPE_SINT32));
  2042. EXPECT_STREQ("int64", GetCppTypeNameForFieldType(FD::TYPE_SINT64));
  2043. }
  2044. TEST_F(MiscTest, StaticCppTypeNames) {
  2045. // Test that correct CPP type names are returned.
  2046. typedef FieldDescriptor FD; // avoid ugly line wrapping
  2047. EXPECT_STREQ("int32", FD::CppTypeName(FD::CPPTYPE_INT32));
  2048. EXPECT_STREQ("int64", FD::CppTypeName(FD::CPPTYPE_INT64));
  2049. EXPECT_STREQ("uint32", FD::CppTypeName(FD::CPPTYPE_UINT32));
  2050. EXPECT_STREQ("uint64", FD::CppTypeName(FD::CPPTYPE_UINT64));
  2051. EXPECT_STREQ("double", FD::CppTypeName(FD::CPPTYPE_DOUBLE));
  2052. EXPECT_STREQ("float", FD::CppTypeName(FD::CPPTYPE_FLOAT));
  2053. EXPECT_STREQ("bool", FD::CppTypeName(FD::CPPTYPE_BOOL));
  2054. EXPECT_STREQ("enum", FD::CppTypeName(FD::CPPTYPE_ENUM));
  2055. EXPECT_STREQ("string", FD::CppTypeName(FD::CPPTYPE_STRING));
  2056. EXPECT_STREQ("message", FD::CppTypeName(FD::CPPTYPE_MESSAGE));
  2057. }
  2058. TEST_F(MiscTest, MessageType) {
  2059. // Test that message_type() is nullptr for non-aggregate fields
  2060. typedef FieldDescriptor FD; // avoid ugly line wrapping
  2061. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_DOUBLE));
  2062. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_FLOAT));
  2063. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_INT64));
  2064. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_UINT64));
  2065. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_INT32));
  2066. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_FIXED64));
  2067. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_FIXED32));
  2068. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_BOOL));
  2069. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_STRING));
  2070. EXPECT_TRUE(nullptr != GetMessageDescriptorForFieldType(FD::TYPE_GROUP));
  2071. EXPECT_TRUE(nullptr != GetMessageDescriptorForFieldType(FD::TYPE_MESSAGE));
  2072. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_BYTES));
  2073. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_UINT32));
  2074. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_ENUM));
  2075. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED32));
  2076. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED64));
  2077. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SINT32));
  2078. EXPECT_TRUE(nullptr == GetMessageDescriptorForFieldType(FD::TYPE_SINT64));
  2079. }
  2080. TEST_F(MiscTest, EnumType) {
  2081. // Test that enum_type() is nullptr for non-enum fields
  2082. typedef FieldDescriptor FD; // avoid ugly line wrapping
  2083. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_DOUBLE));
  2084. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_FLOAT));
  2085. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_INT64));
  2086. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_UINT64));
  2087. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_INT32));
  2088. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_FIXED64));
  2089. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_FIXED32));
  2090. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_BOOL));
  2091. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_STRING));
  2092. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_GROUP));
  2093. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_MESSAGE));
  2094. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_BYTES));
  2095. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_UINT32));
  2096. EXPECT_TRUE(nullptr != GetEnumDescriptorForFieldType(FD::TYPE_ENUM));
  2097. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED32));
  2098. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED64));
  2099. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SINT32));
  2100. EXPECT_TRUE(nullptr == GetEnumDescriptorForFieldType(FD::TYPE_SINT64));
  2101. }
  2102. TEST_F(MiscTest, DefaultValues) {
  2103. // Test that setting default values works.
  2104. FileDescriptorProto file_proto;
  2105. file_proto.set_name("foo.proto");
  2106. EnumDescriptorProto* enum_type_proto = AddEnum(&file_proto, "DummyEnum");
  2107. AddEnumValue(enum_type_proto, "A", 1);
  2108. AddEnumValue(enum_type_proto, "B", 2);
  2109. DescriptorProto* message_proto = AddMessage(&file_proto, "TestMessage");
  2110. typedef FieldDescriptorProto FD; // avoid ugly line wrapping
  2111. const FD::Label label = FD::LABEL_OPTIONAL;
  2112. // Create fields of every CPP type with default values.
  2113. AddField(message_proto, "int32", 1, label, FD::TYPE_INT32)
  2114. ->set_default_value("-1");
  2115. AddField(message_proto, "int64", 2, label, FD::TYPE_INT64)
  2116. ->set_default_value("-1000000000000");
  2117. AddField(message_proto, "uint32", 3, label, FD::TYPE_UINT32)
  2118. ->set_default_value("42");
  2119. AddField(message_proto, "uint64", 4, label, FD::TYPE_UINT64)
  2120. ->set_default_value("2000000000000");
  2121. AddField(message_proto, "float", 5, label, FD::TYPE_FLOAT)
  2122. ->set_default_value("4.5");
  2123. AddField(message_proto, "double", 6, label, FD::TYPE_DOUBLE)
  2124. ->set_default_value("10e100");
  2125. AddField(message_proto, "bool", 7, label, FD::TYPE_BOOL)
  2126. ->set_default_value("true");
  2127. AddField(message_proto, "string", 8, label, FD::TYPE_STRING)
  2128. ->set_default_value("hello");
  2129. AddField(message_proto, "data", 9, label, FD::TYPE_BYTES)
  2130. ->set_default_value("\\001\\002\\003");
  2131. FieldDescriptorProto* enum_field =
  2132. AddField(message_proto, "enum", 10, label, FD::TYPE_ENUM);
  2133. enum_field->set_type_name("DummyEnum");
  2134. enum_field->set_default_value("B");
  2135. // Strings are allowed to have empty defaults. (At one point, due to
  2136. // a bug, empty defaults for strings were rejected. Oops.)
  2137. AddField(message_proto, "empty_string", 11, label, FD::TYPE_STRING)
  2138. ->set_default_value("");
  2139. // Add a second set of fields with implicit defalut values.
  2140. AddField(message_proto, "implicit_int32", 21, label, FD::TYPE_INT32);
  2141. AddField(message_proto, "implicit_int64", 22, label, FD::TYPE_INT64);
  2142. AddField(message_proto, "implicit_uint32", 23, label, FD::TYPE_UINT32);
  2143. AddField(message_proto, "implicit_uint64", 24, label, FD::TYPE_UINT64);
  2144. AddField(message_proto, "implicit_float", 25, label, FD::TYPE_FLOAT);
  2145. AddField(message_proto, "implicit_double", 26, label, FD::TYPE_DOUBLE);
  2146. AddField(message_proto, "implicit_bool", 27, label, FD::TYPE_BOOL);
  2147. AddField(message_proto, "implicit_string", 28, label, FD::TYPE_STRING);
  2148. AddField(message_proto, "implicit_data", 29, label, FD::TYPE_BYTES);
  2149. AddField(message_proto, "implicit_enum", 30, label, FD::TYPE_ENUM)
  2150. ->set_type_name("DummyEnum");
  2151. // Build it.
  2152. DescriptorPool pool;
  2153. const FileDescriptor* file = pool.BuildFile(file_proto);
  2154. ASSERT_TRUE(file != nullptr);
  2155. ASSERT_EQ(1, file->enum_type_count());
  2156. const EnumDescriptor* enum_type = file->enum_type(0);
  2157. ASSERT_EQ(2, enum_type->value_count());
  2158. const EnumValueDescriptor* enum_value_a = enum_type->value(0);
  2159. const EnumValueDescriptor* enum_value_b = enum_type->value(1);
  2160. ASSERT_EQ(1, file->message_type_count());
  2161. const Descriptor* message = file->message_type(0);
  2162. ASSERT_EQ(21, message->field_count());
  2163. // Check the default values.
  2164. ASSERT_TRUE(message->field(0)->has_default_value());
  2165. ASSERT_TRUE(message->field(1)->has_default_value());
  2166. ASSERT_TRUE(message->field(2)->has_default_value());
  2167. ASSERT_TRUE(message->field(3)->has_default_value());
  2168. ASSERT_TRUE(message->field(4)->has_default_value());
  2169. ASSERT_TRUE(message->field(5)->has_default_value());
  2170. ASSERT_TRUE(message->field(6)->has_default_value());
  2171. ASSERT_TRUE(message->field(7)->has_default_value());
  2172. ASSERT_TRUE(message->field(8)->has_default_value());
  2173. ASSERT_TRUE(message->field(9)->has_default_value());
  2174. ASSERT_TRUE(message->field(10)->has_default_value());
  2175. EXPECT_EQ(-1, message->field(0)->default_value_int32());
  2176. EXPECT_EQ(-PROTOBUF_ULONGLONG(1000000000000),
  2177. message->field(1)->default_value_int64());
  2178. EXPECT_EQ(42, message->field(2)->default_value_uint32());
  2179. EXPECT_EQ(PROTOBUF_ULONGLONG(2000000000000),
  2180. message->field(3)->default_value_uint64());
  2181. EXPECT_EQ(4.5, message->field(4)->default_value_float());
  2182. EXPECT_EQ(10e100, message->field(5)->default_value_double());
  2183. EXPECT_TRUE(message->field(6)->default_value_bool());
  2184. EXPECT_EQ("hello", message->field(7)->default_value_string());
  2185. EXPECT_EQ("\001\002\003", message->field(8)->default_value_string());
  2186. EXPECT_EQ(enum_value_b, message->field(9)->default_value_enum());
  2187. EXPECT_EQ("", message->field(10)->default_value_string());
  2188. ASSERT_FALSE(message->field(11)->has_default_value());
  2189. ASSERT_FALSE(message->field(12)->has_default_value());
  2190. ASSERT_FALSE(message->field(13)->has_default_value());
  2191. ASSERT_FALSE(message->field(14)->has_default_value());
  2192. ASSERT_FALSE(message->field(15)->has_default_value());
  2193. ASSERT_FALSE(message->field(16)->has_default_value());
  2194. ASSERT_FALSE(message->field(17)->has_default_value());
  2195. ASSERT_FALSE(message->field(18)->has_default_value());
  2196. ASSERT_FALSE(message->field(19)->has_default_value());
  2197. ASSERT_FALSE(message->field(20)->has_default_value());
  2198. EXPECT_EQ(0, message->field(11)->default_value_int32());
  2199. EXPECT_EQ(0, message->field(12)->default_value_int64());
  2200. EXPECT_EQ(0, message->field(13)->default_value_uint32());
  2201. EXPECT_EQ(0, message->field(14)->default_value_uint64());
  2202. EXPECT_EQ(0.0f, message->field(15)->default_value_float());
  2203. EXPECT_EQ(0.0, message->field(16)->default_value_double());
  2204. EXPECT_FALSE(message->field(17)->default_value_bool());
  2205. EXPECT_EQ("", message->field(18)->default_value_string());
  2206. EXPECT_EQ("", message->field(19)->default_value_string());
  2207. EXPECT_EQ(enum_value_a, message->field(20)->default_value_enum());
  2208. }
  2209. TEST_F(MiscTest, FieldOptions) {
  2210. // Try setting field options.
  2211. FileDescriptorProto file_proto;
  2212. file_proto.set_name("foo.proto");
  2213. DescriptorProto* message_proto = AddMessage(&file_proto, "TestMessage");
  2214. AddField(message_proto, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL,
  2215. FieldDescriptorProto::TYPE_INT32);
  2216. FieldDescriptorProto* bar_proto =
  2217. AddField(message_proto, "bar", 2, FieldDescriptorProto::LABEL_OPTIONAL,
  2218. FieldDescriptorProto::TYPE_INT32);
  2219. FieldOptions* options = bar_proto->mutable_options();
  2220. options->set_ctype(FieldOptions::CORD);
  2221. // Build the descriptors and get the pointers.
  2222. DescriptorPool pool;
  2223. const FileDescriptor* file = pool.BuildFile(file_proto);
  2224. ASSERT_TRUE(file != nullptr);
  2225. ASSERT_EQ(1, file->message_type_count());
  2226. const Descriptor* message = file->message_type(0);
  2227. ASSERT_EQ(2, message->field_count());
  2228. const FieldDescriptor* foo = message->field(0);
  2229. const FieldDescriptor* bar = message->field(1);
  2230. // "foo" had no options set, so it should return the default options.
  2231. EXPECT_EQ(&FieldOptions::default_instance(), &foo->options());
  2232. // "bar" had options set.
  2233. EXPECT_NE(&FieldOptions::default_instance(), options);
  2234. EXPECT_TRUE(bar->options().has_ctype());
  2235. EXPECT_EQ(FieldOptions::CORD, bar->options().ctype());
  2236. }
  2237. // ===================================================================
  2238. enum DescriptorPoolMode { NO_DATABASE, FALLBACK_DATABASE };
  2239. class AllowUnknownDependenciesTest
  2240. : public testing::TestWithParam<DescriptorPoolMode> {
  2241. protected:
  2242. DescriptorPoolMode mode() { return GetParam(); }
  2243. virtual void SetUp() {
  2244. FileDescriptorProto foo_proto, bar_proto;
  2245. switch (mode()) {
  2246. case NO_DATABASE:
  2247. pool_.reset(new DescriptorPool);
  2248. break;
  2249. case FALLBACK_DATABASE:
  2250. pool_.reset(new DescriptorPool(&db_));
  2251. break;
  2252. }
  2253. pool_->AllowUnknownDependencies();
  2254. ASSERT_TRUE(TextFormat::ParseFromString(
  2255. "name: 'foo.proto'"
  2256. "dependency: 'bar.proto'"
  2257. "dependency: 'baz.proto'"
  2258. "message_type {"
  2259. " name: 'Foo'"
  2260. " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'Bar' }"
  2261. " field { name:'baz' number:2 label:LABEL_OPTIONAL type_name:'Baz' }"
  2262. " field { name:'qux' number:3 label:LABEL_OPTIONAL"
  2263. " type_name: '.corge.Qux'"
  2264. " type: TYPE_ENUM"
  2265. " options {"
  2266. " uninterpreted_option {"
  2267. " name {"
  2268. " name_part: 'grault'"
  2269. " is_extension: true"
  2270. " }"
  2271. " positive_int_value: 1234"
  2272. " }"
  2273. " }"
  2274. " }"
  2275. "}",
  2276. &foo_proto));
  2277. ASSERT_TRUE(
  2278. TextFormat::ParseFromString("name: 'bar.proto'"
  2279. "message_type { name: 'Bar' }",
  2280. &bar_proto));
  2281. // Collect pointers to stuff.
  2282. bar_file_ = BuildFile(bar_proto);
  2283. ASSERT_TRUE(bar_file_ != nullptr);
  2284. ASSERT_EQ(1, bar_file_->message_type_count());
  2285. bar_type_ = bar_file_->message_type(0);
  2286. foo_file_ = BuildFile(foo_proto);
  2287. ASSERT_TRUE(foo_file_ != nullptr);
  2288. ASSERT_EQ(1, foo_file_->message_type_count());
  2289. foo_type_ = foo_file_->message_type(0);
  2290. ASSERT_EQ(3, foo_type_->field_count());
  2291. bar_field_ = foo_type_->field(0);
  2292. baz_field_ = foo_type_->field(1);
  2293. qux_field_ = foo_type_->field(2);
  2294. }
  2295. const FileDescriptor* BuildFile(const FileDescriptorProto& proto) {
  2296. switch (mode()) {
  2297. case NO_DATABASE:
  2298. return pool_->BuildFile(proto);
  2299. break;
  2300. case FALLBACK_DATABASE: {
  2301. EXPECT_TRUE(db_.Add(proto));
  2302. return pool_->FindFileByName(proto.name());
  2303. }
  2304. }
  2305. GOOGLE_LOG(FATAL) << "Can't get here.";
  2306. return nullptr;
  2307. }
  2308. const FileDescriptor* bar_file_;
  2309. const Descriptor* bar_type_;
  2310. const FileDescriptor* foo_file_;
  2311. const Descriptor* foo_type_;
  2312. const FieldDescriptor* bar_field_;
  2313. const FieldDescriptor* baz_field_;
  2314. const FieldDescriptor* qux_field_;
  2315. SimpleDescriptorDatabase db_; // used if in FALLBACK_DATABASE mode.
  2316. std::unique_ptr<DescriptorPool> pool_;
  2317. };
  2318. TEST_P(AllowUnknownDependenciesTest, PlaceholderFile) {
  2319. ASSERT_EQ(2, foo_file_->dependency_count());
  2320. EXPECT_EQ(bar_file_, foo_file_->dependency(0));
  2321. EXPECT_FALSE(bar_file_->is_placeholder());
  2322. const FileDescriptor* baz_file = foo_file_->dependency(1);
  2323. EXPECT_EQ("baz.proto", baz_file->name());
  2324. EXPECT_EQ(0, baz_file->message_type_count());
  2325. EXPECT_TRUE(baz_file->is_placeholder());
  2326. // Placeholder files should not be findable.
  2327. EXPECT_EQ(bar_file_, pool_->FindFileByName(bar_file_->name()));
  2328. EXPECT_TRUE(pool_->FindFileByName(baz_file->name()) == nullptr);
  2329. // Copy*To should not crash for placeholder files.
  2330. FileDescriptorProto baz_file_proto;
  2331. baz_file->CopyTo(&baz_file_proto);
  2332. baz_file->CopySourceCodeInfoTo(&baz_file_proto);
  2333. EXPECT_FALSE(baz_file_proto.has_source_code_info());
  2334. }
  2335. TEST_P(AllowUnknownDependenciesTest, PlaceholderTypes) {
  2336. ASSERT_EQ(FieldDescriptor::TYPE_MESSAGE, bar_field_->type());
  2337. EXPECT_EQ(bar_type_, bar_field_->message_type());
  2338. EXPECT_FALSE(bar_type_->is_placeholder());
  2339. ASSERT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_field_->type());
  2340. const Descriptor* baz_type = baz_field_->message_type();
  2341. EXPECT_EQ("Baz", baz_type->name());
  2342. EXPECT_EQ("Baz", baz_type->full_name());
  2343. EXPECT_EQ(0, baz_type->extension_range_count());
  2344. EXPECT_TRUE(baz_type->is_placeholder());
  2345. ASSERT_EQ(FieldDescriptor::TYPE_ENUM, qux_field_->type());
  2346. const EnumDescriptor* qux_type = qux_field_->enum_type();
  2347. EXPECT_EQ("Qux", qux_type->name());
  2348. EXPECT_EQ("corge.Qux", qux_type->full_name());
  2349. EXPECT_TRUE(qux_type->is_placeholder());
  2350. // Placeholder types should not be findable.
  2351. EXPECT_EQ(bar_type_, pool_->FindMessageTypeByName(bar_type_->full_name()));
  2352. EXPECT_TRUE(pool_->FindMessageTypeByName(baz_type->full_name()) == nullptr);
  2353. EXPECT_TRUE(pool_->FindEnumTypeByName(qux_type->full_name()) == nullptr);
  2354. }
  2355. TEST_P(AllowUnknownDependenciesTest, CopyTo) {
  2356. // FieldDescriptor::CopyTo() should write non-fully-qualified type names
  2357. // for placeholder types which were not originally fully-qualified.
  2358. FieldDescriptorProto proto;
  2359. // Bar is not a placeholder, so it is fully-qualified.
  2360. bar_field_->CopyTo(&proto);
  2361. EXPECT_EQ(".Bar", proto.type_name());
  2362. EXPECT_EQ(FieldDescriptorProto::TYPE_MESSAGE, proto.type());
  2363. // Baz is an unqualified placeholder.
  2364. proto.Clear();
  2365. baz_field_->CopyTo(&proto);
  2366. EXPECT_EQ("Baz", proto.type_name());
  2367. EXPECT_FALSE(proto.has_type());
  2368. // Qux is a fully-qualified placeholder.
  2369. proto.Clear();
  2370. qux_field_->CopyTo(&proto);
  2371. EXPECT_EQ(".corge.Qux", proto.type_name());
  2372. EXPECT_EQ(FieldDescriptorProto::TYPE_ENUM, proto.type());
  2373. }
  2374. TEST_P(AllowUnknownDependenciesTest, CustomOptions) {
  2375. // Qux should still have the uninterpreted option attached.
  2376. ASSERT_EQ(1, qux_field_->options().uninterpreted_option_size());
  2377. const UninterpretedOption& option =
  2378. qux_field_->options().uninterpreted_option(0);
  2379. ASSERT_EQ(1, option.name_size());
  2380. EXPECT_EQ("grault", option.name(0).name_part());
  2381. }
  2382. TEST_P(AllowUnknownDependenciesTest, UnknownExtendee) {
  2383. // Test that we can extend an unknown type. This is slightly tricky because
  2384. // it means that the placeholder type must have an extension range.
  2385. FileDescriptorProto extension_proto;
  2386. ASSERT_TRUE(TextFormat::ParseFromString(
  2387. "name: 'extension.proto'"
  2388. "extension { extendee: 'UnknownType' name:'some_extension' number:123"
  2389. " label:LABEL_OPTIONAL type:TYPE_INT32 }",
  2390. &extension_proto));
  2391. const FileDescriptor* file = BuildFile(extension_proto);
  2392. ASSERT_TRUE(file != nullptr);
  2393. ASSERT_EQ(1, file->extension_count());
  2394. const Descriptor* extendee = file->extension(0)->containing_type();
  2395. EXPECT_EQ("UnknownType", extendee->name());
  2396. EXPECT_TRUE(extendee->is_placeholder());
  2397. ASSERT_EQ(1, extendee->extension_range_count());
  2398. EXPECT_EQ(1, extendee->extension_range(0)->start);
  2399. EXPECT_EQ(FieldDescriptor::kMaxNumber + 1, extendee->extension_range(0)->end);
  2400. }
  2401. TEST_P(AllowUnknownDependenciesTest, CustomOption) {
  2402. // Test that we can use a custom option without having parsed
  2403. // descriptor.proto.
  2404. FileDescriptorProto option_proto;
  2405. ASSERT_TRUE(TextFormat::ParseFromString(
  2406. "name: \"unknown_custom_options.proto\" "
  2407. "dependency: \"google/protobuf/descriptor.proto\" "
  2408. "extension { "
  2409. " extendee: \"google.protobuf.FileOptions\" "
  2410. " name: \"some_option\" "
  2411. " number: 123456 "
  2412. " label: LABEL_OPTIONAL "
  2413. " type: TYPE_INT32 "
  2414. "} "
  2415. "options { "
  2416. " uninterpreted_option { "
  2417. " name { "
  2418. " name_part: \"some_option\" "
  2419. " is_extension: true "
  2420. " } "
  2421. " positive_int_value: 1234 "
  2422. " } "
  2423. " uninterpreted_option { "
  2424. " name { "
  2425. " name_part: \"unknown_option\" "
  2426. " is_extension: true "
  2427. " } "
  2428. " positive_int_value: 1234 "
  2429. " } "
  2430. " uninterpreted_option { "
  2431. " name { "
  2432. " name_part: \"optimize_for\" "
  2433. " is_extension: false "
  2434. " } "
  2435. " identifier_value: \"SPEED\" "
  2436. " } "
  2437. "}",
  2438. &option_proto));
  2439. const FileDescriptor* file = BuildFile(option_proto);
  2440. ASSERT_TRUE(file != nullptr);
  2441. // Verify that no extension options were set, but they were left as
  2442. // uninterpreted_options.
  2443. std::vector<const FieldDescriptor*> fields;
  2444. file->options().GetReflection()->ListFields(file->options(), &fields);
  2445. ASSERT_EQ(2, fields.size());
  2446. EXPECT_TRUE(file->options().has_optimize_for());
  2447. EXPECT_EQ(2, file->options().uninterpreted_option_size());
  2448. }
  2449. TEST_P(AllowUnknownDependenciesTest,
  2450. UndeclaredDependencyTriggersBuildOfDependency) {
  2451. // Crazy case: suppose foo.proto refers to a symbol without declaring the
  2452. // dependency that finds it. In the event that the pool is backed by a
  2453. // DescriptorDatabase, the pool will attempt to find the symbol in the
  2454. // database. If successful, it will build the undeclared dependency to verify
  2455. // that the file does indeed contain the symbol. If that file fails to build,
  2456. // then its descriptors must be rolled back. However, we still want foo.proto
  2457. // to build successfully, since we are allowing unknown dependencies.
  2458. FileDescriptorProto undeclared_dep_proto;
  2459. // We make this file fail to build by giving it two fields with tag 1.
  2460. ASSERT_TRUE(TextFormat::ParseFromString(
  2461. "name: \"invalid_file_as_undeclared_dep.proto\" "
  2462. "package: \"undeclared\" "
  2463. "message_type: { "
  2464. " name: \"Quux\" "
  2465. " field { "
  2466. " name:'qux' number:1 label:LABEL_OPTIONAL type: TYPE_INT32 "
  2467. " }"
  2468. " field { "
  2469. " name:'quux' number:1 label:LABEL_OPTIONAL type: TYPE_INT64 "
  2470. " }"
  2471. "}",
  2472. &undeclared_dep_proto));
  2473. // We can't use the BuildFile() helper because we don't actually want to build
  2474. // it into the descriptor pool in the fallback database case: it just needs to
  2475. // be sitting in the database so that it gets built during the building of
  2476. // test.proto below.
  2477. switch (mode()) {
  2478. case NO_DATABASE: {
  2479. ASSERT_TRUE(pool_->BuildFile(undeclared_dep_proto) == nullptr);
  2480. break;
  2481. }
  2482. case FALLBACK_DATABASE: {
  2483. ASSERT_TRUE(db_.Add(undeclared_dep_proto));
  2484. }
  2485. }
  2486. FileDescriptorProto test_proto;
  2487. ASSERT_TRUE(TextFormat::ParseFromString(
  2488. "name: \"test.proto\" "
  2489. "message_type: { "
  2490. " name: \"Corge\" "
  2491. " field { "
  2492. " name:'quux' number:1 label: LABEL_OPTIONAL "
  2493. " type_name:'undeclared.Quux' type: TYPE_MESSAGE "
  2494. " }"
  2495. "}",
  2496. &test_proto));
  2497. const FileDescriptor* file = BuildFile(test_proto);
  2498. ASSERT_TRUE(file != nullptr);
  2499. GOOGLE_LOG(INFO) << file->DebugString();
  2500. EXPECT_EQ(0, file->dependency_count());
  2501. ASSERT_EQ(1, file->message_type_count());
  2502. const Descriptor* corge_desc = file->message_type(0);
  2503. ASSERT_EQ("Corge", corge_desc->name());
  2504. ASSERT_EQ(1, corge_desc->field_count());
  2505. EXPECT_FALSE(corge_desc->is_placeholder());
  2506. const FieldDescriptor* quux_field = corge_desc->field(0);
  2507. ASSERT_EQ(FieldDescriptor::TYPE_MESSAGE, quux_field->type());
  2508. ASSERT_EQ("Quux", quux_field->message_type()->name());
  2509. ASSERT_EQ("undeclared.Quux", quux_field->message_type()->full_name());
  2510. EXPECT_TRUE(quux_field->message_type()->is_placeholder());
  2511. // The place holder type should not be findable.
  2512. ASSERT_TRUE(pool_->FindMessageTypeByName("undeclared.Quux") == nullptr);
  2513. }
  2514. INSTANTIATE_TEST_SUITE_P(DatabaseSource, AllowUnknownDependenciesTest,
  2515. testing::Values(NO_DATABASE, FALLBACK_DATABASE));
  2516. // ===================================================================
  2517. TEST(CustomOptions, OptionLocations) {
  2518. const Descriptor* message =
  2519. protobuf_unittest::TestMessageWithCustomOptions::descriptor();
  2520. const FileDescriptor* file = message->file();
  2521. const FieldDescriptor* field = message->FindFieldByName("field1");
  2522. const OneofDescriptor* oneof = message->FindOneofByName("AnOneof");
  2523. const EnumDescriptor* enm = message->FindEnumTypeByName("AnEnum");
  2524. // TODO(benjy): Support EnumValue options, once the compiler does.
  2525. const ServiceDescriptor* service =
  2526. file->FindServiceByName("TestServiceWithCustomOptions");
  2527. const MethodDescriptor* method = service->FindMethodByName("Foo");
  2528. EXPECT_EQ(PROTOBUF_LONGLONG(9876543210),
  2529. file->options().GetExtension(protobuf_unittest::file_opt1));
  2530. EXPECT_EQ(-56,
  2531. message->options().GetExtension(protobuf_unittest::message_opt1));
  2532. EXPECT_EQ(PROTOBUF_LONGLONG(8765432109),
  2533. field->options().GetExtension(protobuf_unittest::field_opt1));
  2534. EXPECT_EQ(42, // Check that we get the default for an option we don't set.
  2535. field->options().GetExtension(protobuf_unittest::field_opt2));
  2536. EXPECT_EQ(-99, oneof->options().GetExtension(protobuf_unittest::oneof_opt1));
  2537. EXPECT_EQ(-789, enm->options().GetExtension(protobuf_unittest::enum_opt1));
  2538. EXPECT_EQ(123, enm->value(1)->options().GetExtension(
  2539. protobuf_unittest::enum_value_opt1));
  2540. EXPECT_EQ(PROTOBUF_LONGLONG(-9876543210),
  2541. service->options().GetExtension(protobuf_unittest::service_opt1));
  2542. EXPECT_EQ(protobuf_unittest::METHODOPT1_VAL2,
  2543. method->options().GetExtension(protobuf_unittest::method_opt1));
  2544. // See that the regular options went through unscathed.
  2545. EXPECT_TRUE(message->options().has_message_set_wire_format());
  2546. EXPECT_EQ(FieldOptions::CORD, field->options().ctype());
  2547. }
  2548. TEST(CustomOptions, OptionTypes) {
  2549. const MessageOptions* options = nullptr;
  2550. options =
  2551. &protobuf_unittest::CustomOptionMinIntegerValues::descriptor()->options();
  2552. EXPECT_EQ(false, options->GetExtension(protobuf_unittest::bool_opt));
  2553. EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::int32_opt));
  2554. EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::int64_opt));
  2555. EXPECT_EQ(0, options->GetExtension(protobuf_unittest::uint32_opt));
  2556. EXPECT_EQ(0, options->GetExtension(protobuf_unittest::uint64_opt));
  2557. EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::sint32_opt));
  2558. EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::sint64_opt));
  2559. EXPECT_EQ(0, options->GetExtension(protobuf_unittest::fixed32_opt));
  2560. EXPECT_EQ(0, options->GetExtension(protobuf_unittest::fixed64_opt));
  2561. EXPECT_EQ(kint32min, options->GetExtension(protobuf_unittest::sfixed32_opt));
  2562. EXPECT_EQ(kint64min, options->GetExtension(protobuf_unittest::sfixed64_opt));
  2563. options =
  2564. &protobuf_unittest::CustomOptionMaxIntegerValues::descriptor()->options();
  2565. EXPECT_EQ(true, options->GetExtension(protobuf_unittest::bool_opt));
  2566. EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::int32_opt));
  2567. EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::int64_opt));
  2568. EXPECT_EQ(kuint32max, options->GetExtension(protobuf_unittest::uint32_opt));
  2569. EXPECT_EQ(kuint64max, options->GetExtension(protobuf_unittest::uint64_opt));
  2570. EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::sint32_opt));
  2571. EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::sint64_opt));
  2572. EXPECT_EQ(kuint32max, options->GetExtension(protobuf_unittest::fixed32_opt));
  2573. EXPECT_EQ(kuint64max, options->GetExtension(protobuf_unittest::fixed64_opt));
  2574. EXPECT_EQ(kint32max, options->GetExtension(protobuf_unittest::sfixed32_opt));
  2575. EXPECT_EQ(kint64max, options->GetExtension(protobuf_unittest::sfixed64_opt));
  2576. options = &protobuf_unittest::CustomOptionOtherValues::descriptor()->options();
  2577. EXPECT_EQ(-100, options->GetExtension(protobuf_unittest::int32_opt));
  2578. EXPECT_FLOAT_EQ(12.3456789,
  2579. options->GetExtension(protobuf_unittest::float_opt));
  2580. EXPECT_DOUBLE_EQ(1.234567890123456789,
  2581. options->GetExtension(protobuf_unittest::double_opt));
  2582. EXPECT_EQ("Hello, \"World\"",
  2583. options->GetExtension(protobuf_unittest::string_opt));
  2584. EXPECT_EQ(std::string("Hello\0World", 11),
  2585. options->GetExtension(protobuf_unittest::bytes_opt));
  2586. EXPECT_EQ(protobuf_unittest::DummyMessageContainingEnum::TEST_OPTION_ENUM_TYPE2,
  2587. options->GetExtension(protobuf_unittest::enum_opt));
  2588. options =
  2589. &protobuf_unittest::SettingRealsFromPositiveInts::descriptor()->options();
  2590. EXPECT_FLOAT_EQ(12, options->GetExtension(protobuf_unittest::float_opt));
  2591. EXPECT_DOUBLE_EQ(154, options->GetExtension(protobuf_unittest::double_opt));
  2592. options =
  2593. &protobuf_unittest::SettingRealsFromNegativeInts::descriptor()->options();
  2594. EXPECT_FLOAT_EQ(-12, options->GetExtension(protobuf_unittest::float_opt));
  2595. EXPECT_DOUBLE_EQ(-154, options->GetExtension(protobuf_unittest::double_opt));
  2596. }
  2597. TEST(CustomOptions, ComplexExtensionOptions) {
  2598. const MessageOptions* options =
  2599. &protobuf_unittest::VariousComplexOptions::descriptor()->options();
  2600. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1).foo(), 42);
  2601. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1)
  2602. .GetExtension(protobuf_unittest::quux),
  2603. 324);
  2604. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt1)
  2605. .GetExtension(protobuf_unittest::corge)
  2606. .qux(),
  2607. 876);
  2608. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).baz(), 987);
  2609. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2)
  2610. .GetExtension(protobuf_unittest::grault),
  2611. 654);
  2612. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).bar().foo(),
  2613. 743);
  2614. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2)
  2615. .bar()
  2616. .GetExtension(protobuf_unittest::quux),
  2617. 1999);
  2618. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2)
  2619. .bar()
  2620. .GetExtension(protobuf_unittest::corge)
  2621. .qux(),
  2622. 2008);
  2623. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2)
  2624. .GetExtension(protobuf_unittest::garply)
  2625. .foo(),
  2626. 741);
  2627. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2)
  2628. .GetExtension(protobuf_unittest::garply)
  2629. .GetExtension(protobuf_unittest::quux),
  2630. 1998);
  2631. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2)
  2632. .GetExtension(protobuf_unittest::garply)
  2633. .GetExtension(protobuf_unittest::corge)
  2634. .qux(),
  2635. 2121);
  2636. EXPECT_EQ(options
  2637. ->GetExtension(protobuf_unittest::ComplexOptionType2::
  2638. ComplexOptionType4::complex_opt4)
  2639. .waldo(),
  2640. 1971);
  2641. EXPECT_EQ(options->GetExtension(protobuf_unittest::complex_opt2).fred().waldo(),
  2642. 321);
  2643. EXPECT_EQ(9, options->GetExtension(protobuf_unittest::complex_opt3).qux());
  2644. EXPECT_EQ(22, options->GetExtension(protobuf_unittest::complex_opt3)
  2645. .complexoptiontype5()
  2646. .plugh());
  2647. EXPECT_EQ(24, options->GetExtension(protobuf_unittest::complexopt6).xyzzy());
  2648. }
  2649. TEST(CustomOptions, OptionsFromOtherFile) {
  2650. // Test that to use a custom option, we only need to import the file
  2651. // defining the option; we do not also have to import descriptor.proto.
  2652. DescriptorPool pool;
  2653. FileDescriptorProto file_proto;
  2654. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  2655. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2656. protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo(
  2657. &file_proto);
  2658. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2659. ASSERT_TRUE(TextFormat::ParseFromString(
  2660. "name: \"custom_options_import.proto\" "
  2661. "package: \"protobuf_unittest\" "
  2662. "dependency: \"google/protobuf/unittest_custom_options.proto\" "
  2663. "options { "
  2664. " uninterpreted_option { "
  2665. " name { "
  2666. " name_part: \"file_opt1\" "
  2667. " is_extension: true "
  2668. " } "
  2669. " positive_int_value: 1234 "
  2670. " } "
  2671. // Test a non-extension option too. (At one point this failed due to a
  2672. // bug.)
  2673. " uninterpreted_option { "
  2674. " name { "
  2675. " name_part: \"java_package\" "
  2676. " is_extension: false "
  2677. " } "
  2678. " string_value: \"foo\" "
  2679. " } "
  2680. // Test that enum-typed options still work too. (At one point this also
  2681. // failed due to a bug.)
  2682. " uninterpreted_option { "
  2683. " name { "
  2684. " name_part: \"optimize_for\" "
  2685. " is_extension: false "
  2686. " } "
  2687. " identifier_value: \"SPEED\" "
  2688. " } "
  2689. "}",
  2690. &file_proto));
  2691. const FileDescriptor* file = pool.BuildFile(file_proto);
  2692. ASSERT_TRUE(file != nullptr);
  2693. EXPECT_EQ(1234, file->options().GetExtension(protobuf_unittest::file_opt1));
  2694. EXPECT_TRUE(file->options().has_java_package());
  2695. EXPECT_EQ("foo", file->options().java_package());
  2696. EXPECT_TRUE(file->options().has_optimize_for());
  2697. EXPECT_EQ(FileOptions::SPEED, file->options().optimize_for());
  2698. }
  2699. TEST(CustomOptions, MessageOptionThreeFieldsSet) {
  2700. // This tests a bug which previously existed in custom options parsing. The
  2701. // bug occurred when you defined a custom option with message type and then
  2702. // set three fields of that option on a single definition (see the example
  2703. // below). The bug is a bit hard to explain, so check the change history if
  2704. // you want to know more.
  2705. DescriptorPool pool;
  2706. FileDescriptorProto file_proto;
  2707. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  2708. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2709. protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo(
  2710. &file_proto);
  2711. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2712. // The following represents the definition:
  2713. //
  2714. // import "google/protobuf/unittest_custom_options.proto"
  2715. // package protobuf_unittest;
  2716. // message Foo {
  2717. // option (complex_opt1).foo = 1234;
  2718. // option (complex_opt1).foo2 = 1234;
  2719. // option (complex_opt1).foo3 = 1234;
  2720. // }
  2721. ASSERT_TRUE(TextFormat::ParseFromString(
  2722. "name: \"custom_options_import.proto\" "
  2723. "package: \"protobuf_unittest\" "
  2724. "dependency: \"google/protobuf/unittest_custom_options.proto\" "
  2725. "message_type { "
  2726. " name: \"Foo\" "
  2727. " options { "
  2728. " uninterpreted_option { "
  2729. " name { "
  2730. " name_part: \"complex_opt1\" "
  2731. " is_extension: true "
  2732. " } "
  2733. " name { "
  2734. " name_part: \"foo\" "
  2735. " is_extension: false "
  2736. " } "
  2737. " positive_int_value: 1234 "
  2738. " } "
  2739. " uninterpreted_option { "
  2740. " name { "
  2741. " name_part: \"complex_opt1\" "
  2742. " is_extension: true "
  2743. " } "
  2744. " name { "
  2745. " name_part: \"foo2\" "
  2746. " is_extension: false "
  2747. " } "
  2748. " positive_int_value: 1234 "
  2749. " } "
  2750. " uninterpreted_option { "
  2751. " name { "
  2752. " name_part: \"complex_opt1\" "
  2753. " is_extension: true "
  2754. " } "
  2755. " name { "
  2756. " name_part: \"foo3\" "
  2757. " is_extension: false "
  2758. " } "
  2759. " positive_int_value: 1234 "
  2760. " } "
  2761. " } "
  2762. "}",
  2763. &file_proto));
  2764. const FileDescriptor* file = pool.BuildFile(file_proto);
  2765. ASSERT_TRUE(file != nullptr);
  2766. ASSERT_EQ(1, file->message_type_count());
  2767. const MessageOptions& options = file->message_type(0)->options();
  2768. EXPECT_EQ(1234, options.GetExtension(protobuf_unittest::complex_opt1).foo());
  2769. }
  2770. TEST(CustomOptions, MessageOptionRepeatedLeafFieldSet) {
  2771. // This test verifies that repeated fields in custom options can be
  2772. // given multiple values by repeating the option with a different value.
  2773. // This test checks repeated leaf values. Each repeated custom value
  2774. // appears in a different uninterpreted_option, which will be concatenated
  2775. // when they are merged into the final option value.
  2776. DescriptorPool pool;
  2777. FileDescriptorProto file_proto;
  2778. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  2779. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2780. protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo(
  2781. &file_proto);
  2782. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2783. // The following represents the definition:
  2784. //
  2785. // import "google/protobuf/unittest_custom_options.proto"
  2786. // package protobuf_unittest;
  2787. // message Foo {
  2788. // option (complex_opt1).foo4 = 12;
  2789. // option (complex_opt1).foo4 = 34;
  2790. // option (complex_opt1).foo4 = 56;
  2791. // }
  2792. ASSERT_TRUE(TextFormat::ParseFromString(
  2793. "name: \"custom_options_import.proto\" "
  2794. "package: \"protobuf_unittest\" "
  2795. "dependency: \"google/protobuf/unittest_custom_options.proto\" "
  2796. "message_type { "
  2797. " name: \"Foo\" "
  2798. " options { "
  2799. " uninterpreted_option { "
  2800. " name { "
  2801. " name_part: \"complex_opt1\" "
  2802. " is_extension: true "
  2803. " } "
  2804. " name { "
  2805. " name_part: \"foo4\" "
  2806. " is_extension: false "
  2807. " } "
  2808. " positive_int_value: 12 "
  2809. " } "
  2810. " uninterpreted_option { "
  2811. " name { "
  2812. " name_part: \"complex_opt1\" "
  2813. " is_extension: true "
  2814. " } "
  2815. " name { "
  2816. " name_part: \"foo4\" "
  2817. " is_extension: false "
  2818. " } "
  2819. " positive_int_value: 34 "
  2820. " } "
  2821. " uninterpreted_option { "
  2822. " name { "
  2823. " name_part: \"complex_opt1\" "
  2824. " is_extension: true "
  2825. " } "
  2826. " name { "
  2827. " name_part: \"foo4\" "
  2828. " is_extension: false "
  2829. " } "
  2830. " positive_int_value: 56 "
  2831. " } "
  2832. " } "
  2833. "}",
  2834. &file_proto));
  2835. const FileDescriptor* file = pool.BuildFile(file_proto);
  2836. ASSERT_TRUE(file != nullptr);
  2837. ASSERT_EQ(1, file->message_type_count());
  2838. const MessageOptions& options = file->message_type(0)->options();
  2839. EXPECT_EQ(3, options.GetExtension(protobuf_unittest::complex_opt1).foo4_size());
  2840. EXPECT_EQ(12, options.GetExtension(protobuf_unittest::complex_opt1).foo4(0));
  2841. EXPECT_EQ(34, options.GetExtension(protobuf_unittest::complex_opt1).foo4(1));
  2842. EXPECT_EQ(56, options.GetExtension(protobuf_unittest::complex_opt1).foo4(2));
  2843. }
  2844. TEST(CustomOptions, MessageOptionRepeatedMsgFieldSet) {
  2845. // This test verifies that repeated fields in custom options can be
  2846. // given multiple values by repeating the option with a different value.
  2847. // This test checks repeated message values. Each repeated custom value
  2848. // appears in a different uninterpreted_option, which will be concatenated
  2849. // when they are merged into the final option value.
  2850. DescriptorPool pool;
  2851. FileDescriptorProto file_proto;
  2852. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  2853. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2854. protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo(
  2855. &file_proto);
  2856. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2857. // The following represents the definition:
  2858. //
  2859. // import "google/protobuf/unittest_custom_options.proto"
  2860. // package protobuf_unittest;
  2861. // message Foo {
  2862. // option (complex_opt2).barney = {waldo: 1};
  2863. // option (complex_opt2).barney = {waldo: 10};
  2864. // option (complex_opt2).barney = {waldo: 100};
  2865. // }
  2866. ASSERT_TRUE(TextFormat::ParseFromString(
  2867. "name: \"custom_options_import.proto\" "
  2868. "package: \"protobuf_unittest\" "
  2869. "dependency: \"google/protobuf/unittest_custom_options.proto\" "
  2870. "message_type { "
  2871. " name: \"Foo\" "
  2872. " options { "
  2873. " uninterpreted_option { "
  2874. " name { "
  2875. " name_part: \"complex_opt2\" "
  2876. " is_extension: true "
  2877. " } "
  2878. " name { "
  2879. " name_part: \"barney\" "
  2880. " is_extension: false "
  2881. " } "
  2882. " aggregate_value: \"waldo: 1\" "
  2883. " } "
  2884. " uninterpreted_option { "
  2885. " name { "
  2886. " name_part: \"complex_opt2\" "
  2887. " is_extension: true "
  2888. " } "
  2889. " name { "
  2890. " name_part: \"barney\" "
  2891. " is_extension: false "
  2892. " } "
  2893. " aggregate_value: \"waldo: 10\" "
  2894. " } "
  2895. " uninterpreted_option { "
  2896. " name { "
  2897. " name_part: \"complex_opt2\" "
  2898. " is_extension: true "
  2899. " } "
  2900. " name { "
  2901. " name_part: \"barney\" "
  2902. " is_extension: false "
  2903. " } "
  2904. " aggregate_value: \"waldo: 100\" "
  2905. " } "
  2906. " } "
  2907. "}",
  2908. &file_proto));
  2909. const FileDescriptor* file = pool.BuildFile(file_proto);
  2910. ASSERT_TRUE(file != nullptr);
  2911. ASSERT_EQ(1, file->message_type_count());
  2912. const MessageOptions& options = file->message_type(0)->options();
  2913. EXPECT_EQ(3,
  2914. options.GetExtension(protobuf_unittest::complex_opt2).barney_size());
  2915. EXPECT_EQ(
  2916. 1, options.GetExtension(protobuf_unittest::complex_opt2).barney(0).waldo());
  2917. EXPECT_EQ(
  2918. 10,
  2919. options.GetExtension(protobuf_unittest::complex_opt2).barney(1).waldo());
  2920. EXPECT_EQ(
  2921. 100,
  2922. options.GetExtension(protobuf_unittest::complex_opt2).barney(2).waldo());
  2923. }
  2924. // Check that aggregate options were parsed and saved correctly in
  2925. // the appropriate descriptors.
  2926. TEST(CustomOptions, AggregateOptions) {
  2927. const Descriptor* msg = protobuf_unittest::AggregateMessage::descriptor();
  2928. const FileDescriptor* file = msg->file();
  2929. const FieldDescriptor* field = msg->FindFieldByName("fieldname");
  2930. const EnumDescriptor* enumd = file->FindEnumTypeByName("AggregateEnum");
  2931. const EnumValueDescriptor* enumv = enumd->FindValueByName("VALUE");
  2932. const ServiceDescriptor* service =
  2933. file->FindServiceByName("AggregateService");
  2934. const MethodDescriptor* method = service->FindMethodByName("Method");
  2935. // Tests for the different types of data embedded in fileopt
  2936. const protobuf_unittest::Aggregate& file_options =
  2937. file->options().GetExtension(protobuf_unittest::fileopt);
  2938. EXPECT_EQ(100, file_options.i());
  2939. EXPECT_EQ("FileAnnotation", file_options.s());
  2940. EXPECT_EQ("NestedFileAnnotation", file_options.sub().s());
  2941. EXPECT_EQ("FileExtensionAnnotation",
  2942. file_options.file().GetExtension(protobuf_unittest::fileopt).s());
  2943. EXPECT_EQ("EmbeddedMessageSetElement",
  2944. file_options.mset()
  2945. .GetExtension(protobuf_unittest::AggregateMessageSetElement ::
  2946. message_set_extension)
  2947. .s());
  2948. // Simple tests for all the other types of annotations
  2949. EXPECT_EQ("MessageAnnotation",
  2950. msg->options().GetExtension(protobuf_unittest::msgopt).s());
  2951. EXPECT_EQ("FieldAnnotation",
  2952. field->options().GetExtension(protobuf_unittest::fieldopt).s());
  2953. EXPECT_EQ("EnumAnnotation",
  2954. enumd->options().GetExtension(protobuf_unittest::enumopt).s());
  2955. EXPECT_EQ("EnumValueAnnotation",
  2956. enumv->options().GetExtension(protobuf_unittest::enumvalopt).s());
  2957. EXPECT_EQ("ServiceAnnotation",
  2958. service->options().GetExtension(protobuf_unittest::serviceopt).s());
  2959. EXPECT_EQ("MethodAnnotation",
  2960. method->options().GetExtension(protobuf_unittest::methodopt).s());
  2961. }
  2962. TEST(CustomOptions, UnusedImportWarning) {
  2963. DescriptorPool pool;
  2964. FileDescriptorProto file_proto;
  2965. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  2966. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2967. protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file()->CopyTo(
  2968. &file_proto);
  2969. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2970. pool.AddUnusedImportTrackFile("custom_options_import.proto");
  2971. ASSERT_TRUE(TextFormat::ParseFromString(
  2972. "name: \"custom_options_import.proto\" "
  2973. "package: \"protobuf_unittest\" "
  2974. "dependency: \"google/protobuf/unittest_custom_options.proto\" ",
  2975. &file_proto));
  2976. MockErrorCollector error_collector;
  2977. EXPECT_TRUE(pool.BuildFileCollectingErrors(file_proto, &error_collector));
  2978. EXPECT_EQ("", error_collector.warning_text_);
  2979. }
  2980. // Verifies that proto files can correctly be parsed, even if the
  2981. // custom options defined in the file are incompatible with those
  2982. // compiled in the binary. See http://b/19276250.
  2983. TEST(CustomOptions, OptionsWithIncompatibleDescriptors) {
  2984. DescriptorPool pool;
  2985. FileDescriptorProto file_proto;
  2986. MessageOptions::descriptor()->file()->CopyTo(&file_proto);
  2987. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  2988. // Create a new file descriptor proto containing a subset of the
  2989. // messages defined in google/protobuf/unittest_custom_options.proto.
  2990. file_proto.Clear();
  2991. file_proto.set_name("unittest_custom_options.proto");
  2992. file_proto.set_package("protobuf_unittest");
  2993. file_proto.add_dependency("google/protobuf/descriptor.proto");
  2994. // Add the "required_enum_opt" extension.
  2995. FieldDescriptorProto* extension = file_proto.add_extension();
  2996. protobuf_unittest::OldOptionType::descriptor()
  2997. ->file()
  2998. ->FindExtensionByName("required_enum_opt")
  2999. ->CopyTo(extension);
  3000. // Add a test message that uses the "required_enum_opt" option.
  3001. DescriptorProto* test_message_type = file_proto.add_message_type();
  3002. protobuf_unittest::TestMessageWithRequiredEnumOption::descriptor()->CopyTo(
  3003. test_message_type);
  3004. // Instruct the extension to use NewOptionType instead of
  3005. // OldOptionType, and add the descriptor of NewOptionType.
  3006. extension->set_type_name(".protobuf_unittest.NewOptionType");
  3007. DescriptorProto* new_option_type = file_proto.add_message_type();
  3008. protobuf_unittest::NewOptionType::descriptor()->CopyTo(new_option_type);
  3009. // Replace the value of the "required_enum_opt" option used in the
  3010. // test message with an enum value that only exists in NewOptionType.
  3011. ASSERT_TRUE(
  3012. TextFormat::ParseFromString("uninterpreted_option { "
  3013. " name { "
  3014. " name_part: 'required_enum_opt' "
  3015. " is_extension: true "
  3016. " } "
  3017. " aggregate_value: 'value: NEW_VALUE'"
  3018. "}",
  3019. test_message_type->mutable_options()));
  3020. // Adding the file descriptor to the pool should fail.
  3021. EXPECT_TRUE(pool.BuildFile(file_proto) == nullptr);
  3022. }
  3023. // Test that FileDescriptor::DebugString() formats custom options correctly.
  3024. TEST(CustomOptions, DebugString) {
  3025. DescriptorPool pool;
  3026. FileDescriptorProto file_proto;
  3027. MessageOptions::descriptor()->file()->CopyTo(&file_proto);
  3028. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  3029. // Add "foo.proto":
  3030. // import "google/protobuf/descriptor.proto";
  3031. // package "protobuf_unittest";
  3032. // option (protobuf_unittest.cc_option1) = 1;
  3033. // option (protobuf_unittest.cc_option2) = 2;
  3034. // extend google.protobuf.FieldOptions {
  3035. // optional int32 cc_option1 = 7736974;
  3036. // optional int32 cc_option2 = 7736975;
  3037. // }
  3038. ASSERT_TRUE(TextFormat::ParseFromString(
  3039. "name: \"foo.proto\" "
  3040. "package: \"protobuf_unittest\" "
  3041. "dependency: \"google/protobuf/descriptor.proto\" "
  3042. "options { "
  3043. " uninterpreted_option { "
  3044. " name { "
  3045. " name_part: \"protobuf_unittest.cc_option1\" "
  3046. " is_extension: true "
  3047. " } "
  3048. " positive_int_value: 1 "
  3049. " } "
  3050. " uninterpreted_option { "
  3051. " name { "
  3052. " name_part: \"protobuf_unittest.cc_option2\" "
  3053. " is_extension: true "
  3054. " } "
  3055. " positive_int_value: 2 "
  3056. " } "
  3057. "} "
  3058. "extension { "
  3059. " name: \"cc_option1\" "
  3060. " extendee: \".google.protobuf.FileOptions\" "
  3061. // This field number is intentionally chosen to be the same as
  3062. // (.fileopt1) defined in unittest_custom_options.proto (linked
  3063. // in this test binary). This is to test whether we are messing
  3064. // generated pool with custom descriptor pools when dealing with
  3065. // custom options.
  3066. " number: 7736974 "
  3067. " label: LABEL_OPTIONAL "
  3068. " type: TYPE_INT32 "
  3069. "}"
  3070. "extension { "
  3071. " name: \"cc_option2\" "
  3072. " extendee: \".google.protobuf.FileOptions\" "
  3073. " number: 7736975 "
  3074. " label: LABEL_OPTIONAL "
  3075. " type: TYPE_INT32 "
  3076. "}",
  3077. &file_proto));
  3078. const FileDescriptor* descriptor = pool.BuildFile(file_proto);
  3079. ASSERT_TRUE(descriptor != nullptr);
  3080. EXPECT_EQ(2, descriptor->extension_count());
  3081. ASSERT_EQ(
  3082. "syntax = \"proto2\";\n"
  3083. "\n"
  3084. "import \"google/protobuf/descriptor.proto\";\n"
  3085. "package protobuf_unittest;\n"
  3086. "\n"
  3087. "option (.protobuf_unittest.cc_option1) = 1;\n"
  3088. "option (.protobuf_unittest.cc_option2) = 2;\n"
  3089. "\n"
  3090. "extend .google.protobuf.FileOptions {\n"
  3091. " optional int32 cc_option1 = 7736974;\n"
  3092. " optional int32 cc_option2 = 7736975;\n"
  3093. "}\n"
  3094. "\n",
  3095. descriptor->DebugString());
  3096. }
  3097. // ===================================================================
  3098. class ValidationErrorTest : public testing::Test {
  3099. protected:
  3100. // Parse file_text as a FileDescriptorProto in text format and add it
  3101. // to the DescriptorPool. Expect no errors.
  3102. const FileDescriptor* BuildFile(const std::string& file_text) {
  3103. FileDescriptorProto file_proto;
  3104. EXPECT_TRUE(TextFormat::ParseFromString(file_text, &file_proto));
  3105. return GOOGLE_CHECK_NOTNULL(pool_.BuildFile(file_proto));
  3106. }
  3107. // Parse file_text as a FileDescriptorProto in text format and add it
  3108. // to the DescriptorPool. Expect errors to be produced which match the
  3109. // given error text.
  3110. void BuildFileWithErrors(const std::string& file_text,
  3111. const std::string& expected_errors) {
  3112. FileDescriptorProto file_proto;
  3113. ASSERT_TRUE(TextFormat::ParseFromString(file_text, &file_proto));
  3114. MockErrorCollector error_collector;
  3115. EXPECT_TRUE(pool_.BuildFileCollectingErrors(file_proto, &error_collector) ==
  3116. nullptr);
  3117. EXPECT_EQ(expected_errors, error_collector.text_);
  3118. }
  3119. // Parse file_text as a FileDescriptorProto in text format and add it
  3120. // to the DescriptorPool. Expect errors to be produced which match the
  3121. // given warning text.
  3122. void BuildFileWithWarnings(const std::string& file_text,
  3123. const std::string& expected_warnings) {
  3124. FileDescriptorProto file_proto;
  3125. ASSERT_TRUE(TextFormat::ParseFromString(file_text, &file_proto));
  3126. MockErrorCollector error_collector;
  3127. EXPECT_TRUE(pool_.BuildFileCollectingErrors(file_proto, &error_collector));
  3128. EXPECT_EQ(expected_warnings, error_collector.warning_text_);
  3129. }
  3130. // Builds some already-parsed file in our test pool.
  3131. void BuildFileInTestPool(const FileDescriptor* file) {
  3132. FileDescriptorProto file_proto;
  3133. file->CopyTo(&file_proto);
  3134. ASSERT_TRUE(pool_.BuildFile(file_proto) != nullptr);
  3135. }
  3136. // Build descriptor.proto in our test pool. This allows us to extend it in
  3137. // the test pool, so we can test custom options.
  3138. void BuildDescriptorMessagesInTestPool() {
  3139. BuildFileInTestPool(DescriptorProto::descriptor()->file());
  3140. }
  3141. DescriptorPool pool_;
  3142. };
  3143. TEST_F(ValidationErrorTest, AlreadyDefined) {
  3144. BuildFileWithErrors(
  3145. "name: \"foo.proto\" "
  3146. "message_type { name: \"Foo\" }"
  3147. "message_type { name: \"Foo\" }",
  3148. "foo.proto: Foo: NAME: \"Foo\" is already defined.\n");
  3149. }
  3150. TEST_F(ValidationErrorTest, AlreadyDefinedInPackage) {
  3151. BuildFileWithErrors(
  3152. "name: \"foo.proto\" "
  3153. "package: \"foo.bar\" "
  3154. "message_type { name: \"Foo\" }"
  3155. "message_type { name: \"Foo\" }",
  3156. "foo.proto: foo.bar.Foo: NAME: \"Foo\" is already defined in "
  3157. "\"foo.bar\".\n");
  3158. }
  3159. TEST_F(ValidationErrorTest, AlreadyDefinedInOtherFile) {
  3160. BuildFile(
  3161. "name: \"foo.proto\" "
  3162. "message_type { name: \"Foo\" }");
  3163. BuildFileWithErrors(
  3164. "name: \"bar.proto\" "
  3165. "message_type { name: \"Foo\" }",
  3166. "bar.proto: Foo: NAME: \"Foo\" is already defined in file "
  3167. "\"foo.proto\".\n");
  3168. }
  3169. TEST_F(ValidationErrorTest, PackageAlreadyDefined) {
  3170. BuildFile(
  3171. "name: \"foo.proto\" "
  3172. "message_type { name: \"foo\" }");
  3173. BuildFileWithErrors(
  3174. "name: \"bar.proto\" "
  3175. "package: \"foo.bar\"",
  3176. "bar.proto: foo: NAME: \"foo\" is already defined (as something other "
  3177. "than a package) in file \"foo.proto\".\n");
  3178. }
  3179. TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParent) {
  3180. BuildFileWithErrors(
  3181. "name: \"foo.proto\" "
  3182. "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } "
  3183. "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ",
  3184. "foo.proto: FOO: NAME: \"FOO\" is already defined.\n"
  3185. "foo.proto: FOO: NAME: Note that enum values use C++ scoping rules, "
  3186. "meaning that enum values are siblings of their type, not children of "
  3187. "it. Therefore, \"FOO\" must be unique within the global scope, not "
  3188. "just within \"Bar\".\n");
  3189. }
  3190. TEST_F(ValidationErrorTest, EnumValueAlreadyDefinedInParentNonGlobal) {
  3191. BuildFileWithErrors(
  3192. "name: \"foo.proto\" "
  3193. "package: \"pkg\" "
  3194. "enum_type { name: \"Foo\" value { name: \"FOO\" number: 1 } } "
  3195. "enum_type { name: \"Bar\" value { name: \"FOO\" number: 1 } } ",
  3196. "foo.proto: pkg.FOO: NAME: \"FOO\" is already defined in \"pkg\".\n"
  3197. "foo.proto: pkg.FOO: NAME: Note that enum values use C++ scoping rules, "
  3198. "meaning that enum values are siblings of their type, not children of "
  3199. "it. Therefore, \"FOO\" must be unique within \"pkg\", not just within "
  3200. "\"Bar\".\n");
  3201. }
  3202. TEST_F(ValidationErrorTest, MissingName) {
  3203. BuildFileWithErrors(
  3204. "name: \"foo.proto\" "
  3205. "message_type { }",
  3206. "foo.proto: : NAME: Missing name.\n");
  3207. }
  3208. TEST_F(ValidationErrorTest, InvalidName) {
  3209. BuildFileWithErrors(
  3210. "name: \"foo.proto\" "
  3211. "message_type { name: \"$\" }",
  3212. "foo.proto: $: NAME: \"$\" is not a valid identifier.\n");
  3213. }
  3214. TEST_F(ValidationErrorTest, InvalidPackageName) {
  3215. BuildFileWithErrors(
  3216. "name: \"foo.proto\" "
  3217. "package: \"foo.$\"",
  3218. "foo.proto: foo.$: NAME: \"$\" is not a valid identifier.\n");
  3219. }
  3220. TEST_F(ValidationErrorTest, MissingFileName) {
  3221. BuildFileWithErrors("",
  3222. ": : OTHER: Missing field: FileDescriptorProto.name.\n");
  3223. }
  3224. TEST_F(ValidationErrorTest, DupeDependency) {
  3225. BuildFile("name: \"foo.proto\"");
  3226. BuildFileWithErrors(
  3227. "name: \"bar.proto\" "
  3228. "dependency: \"foo.proto\" "
  3229. "dependency: \"foo.proto\" ",
  3230. "bar.proto: foo.proto: IMPORT: Import \"foo.proto\" was listed twice.\n");
  3231. }
  3232. TEST_F(ValidationErrorTest, UnknownDependency) {
  3233. BuildFileWithErrors(
  3234. "name: \"bar.proto\" "
  3235. "dependency: \"foo.proto\" ",
  3236. "bar.proto: foo.proto: IMPORT: Import \"foo.proto\" has not been "
  3237. "loaded.\n");
  3238. }
  3239. TEST_F(ValidationErrorTest, InvalidPublicDependencyIndex) {
  3240. BuildFile("name: \"foo.proto\"");
  3241. BuildFileWithErrors(
  3242. "name: \"bar.proto\" "
  3243. "dependency: \"foo.proto\" "
  3244. "public_dependency: 1",
  3245. "bar.proto: bar.proto: OTHER: Invalid public dependency index.\n");
  3246. }
  3247. TEST_F(ValidationErrorTest, ForeignUnimportedPackageNoCrash) {
  3248. // Used to crash: If we depend on a non-existent file and then refer to a
  3249. // package defined in a file that we didn't import, and that package is
  3250. // nested within a parent package which this file is also in, and we don't
  3251. // include that parent package in the name (i.e. we do a relative lookup)...
  3252. // Yes, really.
  3253. BuildFile(
  3254. "name: 'foo.proto' "
  3255. "package: 'outer.foo' ");
  3256. BuildFileWithErrors(
  3257. "name: 'bar.proto' "
  3258. "dependency: 'baz.proto' "
  3259. "package: 'outer.bar' "
  3260. "message_type { "
  3261. " name: 'Bar' "
  3262. " field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'foo.Foo' }"
  3263. "}",
  3264. "bar.proto: baz.proto: IMPORT: Import \"baz.proto\" has not been "
  3265. "loaded.\n"
  3266. "bar.proto: outer.bar.Bar.bar: TYPE: \"outer.foo\" seems to be defined "
  3267. "in "
  3268. "\"foo.proto\", which is not imported by \"bar.proto\". To use it here, "
  3269. "please add the necessary import.\n");
  3270. }
  3271. TEST_F(ValidationErrorTest, DupeFile) {
  3272. BuildFile(
  3273. "name: \"foo.proto\" "
  3274. "message_type { name: \"Foo\" }");
  3275. // Note: We should *not* get redundant errors about "Foo" already being
  3276. // defined.
  3277. BuildFileWithErrors(
  3278. "name: \"foo.proto\" "
  3279. "message_type { name: \"Foo\" } "
  3280. // Add another type so that the files aren't identical (in which case
  3281. // there would be no error).
  3282. "enum_type { name: \"Bar\" }",
  3283. "foo.proto: foo.proto: OTHER: A file with this name is already in the "
  3284. "pool.\n");
  3285. }
  3286. TEST_F(ValidationErrorTest, FieldInExtensionRange) {
  3287. BuildFileWithErrors(
  3288. "name: \"foo.proto\" "
  3289. "message_type {"
  3290. " name: \"Foo\""
  3291. " field { name: \"foo\" number: 9 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3292. "}"
  3293. " field { name: \"bar\" number: 10 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3294. "}"
  3295. " field { name: \"baz\" number: 19 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3296. "}"
  3297. " field { name: \"qux\" number: 20 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3298. "}"
  3299. " extension_range { start: 10 end: 20 }"
  3300. "}",
  3301. "foo.proto: Foo.bar: NUMBER: Extension range 10 to 19 includes field "
  3302. "\"bar\" (10).\n"
  3303. "foo.proto: Foo.baz: NUMBER: Extension range 10 to 19 includes field "
  3304. "\"baz\" (19).\n");
  3305. }
  3306. TEST_F(ValidationErrorTest, OverlappingExtensionRanges) {
  3307. BuildFileWithErrors(
  3308. "name: \"foo.proto\" "
  3309. "message_type {"
  3310. " name: \"Foo\""
  3311. " extension_range { start: 10 end: 20 }"
  3312. " extension_range { start: 20 end: 30 }"
  3313. " extension_range { start: 19 end: 21 }"
  3314. "}",
  3315. "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with "
  3316. "already-defined range 10 to 19.\n"
  3317. "foo.proto: Foo: NUMBER: Extension range 19 to 20 overlaps with "
  3318. "already-defined range 20 to 29.\n");
  3319. }
  3320. TEST_F(ValidationErrorTest, ReservedFieldError) {
  3321. BuildFileWithErrors(
  3322. "name: \"foo.proto\" "
  3323. "message_type {"
  3324. " name: \"Foo\""
  3325. " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3326. "}"
  3327. " reserved_range { start: 10 end: 20 }"
  3328. "}",
  3329. "foo.proto: Foo.foo: NUMBER: Field \"foo\" uses reserved number 15.\n");
  3330. }
  3331. TEST_F(ValidationErrorTest, ReservedExtensionRangeError) {
  3332. BuildFileWithErrors(
  3333. "name: \"foo.proto\" "
  3334. "message_type {"
  3335. " name: \"Foo\""
  3336. " extension_range { start: 10 end: 20 }"
  3337. " reserved_range { start: 5 end: 15 }"
  3338. "}",
  3339. "foo.proto: Foo: NUMBER: Extension range 10 to 19"
  3340. " overlaps with reserved range 5 to 14.\n");
  3341. }
  3342. TEST_F(ValidationErrorTest, ReservedExtensionRangeAdjacent) {
  3343. BuildFile(
  3344. "name: \"foo.proto\" "
  3345. "message_type {"
  3346. " name: \"Foo\""
  3347. " extension_range { start: 10 end: 20 }"
  3348. " reserved_range { start: 5 end: 10 }"
  3349. "}");
  3350. }
  3351. TEST_F(ValidationErrorTest, ReservedRangeOverlap) {
  3352. BuildFileWithErrors(
  3353. "name: \"foo.proto\" "
  3354. "message_type {"
  3355. " name: \"Foo\""
  3356. " reserved_range { start: 10 end: 20 }"
  3357. " reserved_range { start: 5 end: 15 }"
  3358. "}",
  3359. "foo.proto: Foo: NUMBER: Reserved range 5 to 14"
  3360. " overlaps with already-defined range 10 to 19.\n");
  3361. }
  3362. TEST_F(ValidationErrorTest, ReservedNameError) {
  3363. BuildFileWithErrors(
  3364. "name: \"foo.proto\" "
  3365. "message_type {"
  3366. " name: \"Foo\""
  3367. " field { name: \"foo\" number: 15 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3368. "}"
  3369. " field { name: \"bar\" number: 16 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3370. "}"
  3371. " field { name: \"baz\" number: 17 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3372. "}"
  3373. " reserved_name: \"foo\""
  3374. " reserved_name: \"bar\""
  3375. "}",
  3376. "foo.proto: Foo.foo: NAME: Field name \"foo\" is reserved.\n"
  3377. "foo.proto: Foo.bar: NAME: Field name \"bar\" is reserved.\n");
  3378. }
  3379. TEST_F(ValidationErrorTest, ReservedNameRedundant) {
  3380. BuildFileWithErrors(
  3381. "name: \"foo.proto\" "
  3382. "message_type {"
  3383. " name: \"Foo\""
  3384. " reserved_name: \"foo\""
  3385. " reserved_name: \"foo\""
  3386. "}",
  3387. "foo.proto: foo: NAME: Field name \"foo\" is reserved multiple times.\n");
  3388. }
  3389. TEST_F(ValidationErrorTest, ReservedFieldsDebugString) {
  3390. const FileDescriptor* file = BuildFile(
  3391. "name: \"foo.proto\" "
  3392. "message_type {"
  3393. " name: \"Foo\""
  3394. " reserved_name: \"foo\""
  3395. " reserved_name: \"bar\""
  3396. " reserved_range { start: 5 end: 6 }"
  3397. " reserved_range { start: 10 end: 20 }"
  3398. "}");
  3399. ASSERT_EQ(
  3400. "syntax = \"proto2\";\n\n"
  3401. "message Foo {\n"
  3402. " reserved 5, 10 to 19;\n"
  3403. " reserved \"foo\", \"bar\";\n"
  3404. "}\n\n",
  3405. file->DebugString());
  3406. }
  3407. TEST_F(ValidationErrorTest, EnumReservedFieldError) {
  3408. BuildFileWithErrors(
  3409. "name: \"foo.proto\" "
  3410. "enum_type {"
  3411. " name: \"Foo\""
  3412. " value { name:\"BAR\" number:15 }"
  3413. " reserved_range { start: 10 end: 20 }"
  3414. "}",
  3415. "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number 15.\n");
  3416. }
  3417. TEST_F(ValidationErrorTest, EnumNegativeReservedFieldError) {
  3418. BuildFileWithErrors(
  3419. "name: \"foo.proto\" "
  3420. "enum_type {"
  3421. " name: \"Foo\""
  3422. " value { name:\"BAR\" number:-15 }"
  3423. " reserved_range { start: -20 end: -10 }"
  3424. "}",
  3425. "foo.proto: BAR: NUMBER: Enum value \"BAR\" uses reserved number -15.\n");
  3426. }
  3427. TEST_F(ValidationErrorTest, EnumReservedRangeOverlap) {
  3428. BuildFileWithErrors(
  3429. "name: \"foo.proto\" "
  3430. "enum_type {"
  3431. " name: \"Foo\""
  3432. " value { name:\"BAR\" number:0 }"
  3433. " reserved_range { start: 10 end: 20 }"
  3434. " reserved_range { start: 5 end: 15 }"
  3435. "}",
  3436. "foo.proto: Foo: NUMBER: Reserved range 5 to 15"
  3437. " overlaps with already-defined range 10 to 20.\n");
  3438. }
  3439. TEST_F(ValidationErrorTest, EnumReservedRangeOverlapByOne) {
  3440. BuildFileWithErrors(
  3441. "name: \"foo.proto\" "
  3442. "enum_type {"
  3443. " name: \"Foo\""
  3444. " value { name:\"BAR\" number:0 }"
  3445. " reserved_range { start: 10 end: 20 }"
  3446. " reserved_range { start: 5 end: 10 }"
  3447. "}",
  3448. "foo.proto: Foo: NUMBER: Reserved range 5 to 10"
  3449. " overlaps with already-defined range 10 to 20.\n");
  3450. }
  3451. TEST_F(ValidationErrorTest, EnumNegativeReservedRangeOverlap) {
  3452. BuildFileWithErrors(
  3453. "name: \"foo.proto\" "
  3454. "enum_type {"
  3455. " name: \"Foo\""
  3456. " value { name:\"BAR\" number:0 }"
  3457. " reserved_range { start: -20 end: -10 }"
  3458. " reserved_range { start: -15 end: -5 }"
  3459. "}",
  3460. "foo.proto: Foo: NUMBER: Reserved range -15 to -5"
  3461. " overlaps with already-defined range -20 to -10.\n");
  3462. }
  3463. TEST_F(ValidationErrorTest, EnumMixedReservedRangeOverlap) {
  3464. BuildFileWithErrors(
  3465. "name: \"foo.proto\" "
  3466. "enum_type {"
  3467. " name: \"Foo\""
  3468. " value { name:\"BAR\" number:20 }"
  3469. " reserved_range { start: -20 end: 10 }"
  3470. " reserved_range { start: -15 end: 5 }"
  3471. "}",
  3472. "foo.proto: Foo: NUMBER: Reserved range -15 to 5"
  3473. " overlaps with already-defined range -20 to 10.\n");
  3474. }
  3475. TEST_F(ValidationErrorTest, EnumMixedReservedRangeOverlap2) {
  3476. BuildFileWithErrors(
  3477. "name: \"foo.proto\" "
  3478. "enum_type {"
  3479. " name: \"Foo\""
  3480. " value { name:\"BAR\" number:20 }"
  3481. " reserved_range { start: -20 end: 10 }"
  3482. " reserved_range { start: 10 end: 10 }"
  3483. "}",
  3484. "foo.proto: Foo: NUMBER: Reserved range 10 to 10"
  3485. " overlaps with already-defined range -20 to 10.\n");
  3486. }
  3487. TEST_F(ValidationErrorTest, EnumReservedRangeStartGreaterThanEnd) {
  3488. BuildFileWithErrors(
  3489. "name: \"foo.proto\" "
  3490. "enum_type {"
  3491. " name: \"Foo\""
  3492. " value { name:\"BAR\" number:20 }"
  3493. " reserved_range { start: 11 end: 10 }"
  3494. "}",
  3495. "foo.proto: Foo: NUMBER: Reserved range end number must be greater"
  3496. " than start number.\n");
  3497. }
  3498. TEST_F(ValidationErrorTest, EnumReservedNameError) {
  3499. BuildFileWithErrors(
  3500. "name: \"foo.proto\" "
  3501. "enum_type {"
  3502. " name: \"Foo\""
  3503. " value { name:\"FOO\" number:15 }"
  3504. " value { name:\"BAR\" number:15 }"
  3505. " reserved_name: \"FOO\""
  3506. " reserved_name: \"BAR\""
  3507. "}",
  3508. "foo.proto: FOO: NAME: Enum value \"FOO\" is reserved.\n"
  3509. "foo.proto: BAR: NAME: Enum value \"BAR\" is reserved.\n");
  3510. }
  3511. TEST_F(ValidationErrorTest, EnumReservedNameRedundant) {
  3512. BuildFileWithErrors(
  3513. "name: \"foo.proto\" "
  3514. "enum_type {"
  3515. " name: \"Foo\""
  3516. " value { name:\"FOO\" number:15 }"
  3517. " reserved_name: \"foo\""
  3518. " reserved_name: \"foo\""
  3519. "}",
  3520. "foo.proto: foo: NAME: Enum value \"foo\" is reserved multiple times.\n");
  3521. }
  3522. TEST_F(ValidationErrorTest, EnumReservedFieldsDebugString) {
  3523. const FileDescriptor* file = BuildFile(
  3524. "name: \"foo.proto\" "
  3525. "enum_type {"
  3526. " name: \"Foo\""
  3527. " value { name:\"FOO\" number:3 }"
  3528. " reserved_name: \"foo\""
  3529. " reserved_name: \"bar\""
  3530. " reserved_range { start: -6 end: -6 }"
  3531. " reserved_range { start: -5 end: -4 }"
  3532. " reserved_range { start: -1 end: 1 }"
  3533. " reserved_range { start: 5 end: 5 }"
  3534. " reserved_range { start: 10 end: 19 }"
  3535. "}");
  3536. ASSERT_EQ(
  3537. "syntax = \"proto2\";\n\n"
  3538. "enum Foo {\n"
  3539. " FOO = 3;\n"
  3540. " reserved -6, -5 to -4, -1 to 1, 5, 10 to 19;\n"
  3541. " reserved \"foo\", \"bar\";\n"
  3542. "}\n\n",
  3543. file->DebugString());
  3544. }
  3545. TEST_F(ValidationErrorTest, InvalidDefaults) {
  3546. BuildFileWithErrors(
  3547. "name: \"foo.proto\" "
  3548. "message_type {"
  3549. " name: \"Foo\""
  3550. // Invalid number.
  3551. " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32"
  3552. " default_value: \"abc\" }"
  3553. // Empty default value.
  3554. " field { name: \"bar\" number: 2 label: LABEL_OPTIONAL type: TYPE_INT32"
  3555. " default_value: \"\" }"
  3556. // Invalid boolean.
  3557. " field { name: \"baz\" number: 3 label: LABEL_OPTIONAL type: TYPE_BOOL"
  3558. " default_value: \"abc\" }"
  3559. // Messages can't have defaults.
  3560. " field { name: \"qux\" number: 4 label: LABEL_OPTIONAL type: "
  3561. "TYPE_MESSAGE"
  3562. " default_value: \"abc\" type_name: \"Foo\" }"
  3563. // Same thing, but we don't know that this field has message type until
  3564. // we look up the type name.
  3565. " field { name: \"quux\" number: 5 label: LABEL_OPTIONAL"
  3566. " default_value: \"abc\" type_name: \"Foo\" }"
  3567. // Repeateds can't have defaults.
  3568. " field { name: \"corge\" number: 6 label: LABEL_REPEATED type: "
  3569. "TYPE_INT32"
  3570. " default_value: \"1\" }"
  3571. "}",
  3572. "foo.proto: Foo.foo: DEFAULT_VALUE: Couldn't parse default value "
  3573. "\"abc\".\n"
  3574. "foo.proto: Foo.bar: DEFAULT_VALUE: Couldn't parse default value \"\".\n"
  3575. "foo.proto: Foo.baz: DEFAULT_VALUE: Boolean default must be true or "
  3576. "false.\n"
  3577. "foo.proto: Foo.qux: DEFAULT_VALUE: Messages can't have default values.\n"
  3578. "foo.proto: Foo.corge: DEFAULT_VALUE: Repeated fields can't have default "
  3579. "values.\n"
  3580. // This ends up being reported later because the error is detected at
  3581. // cross-linking time.
  3582. "foo.proto: Foo.quux: DEFAULT_VALUE: Messages can't have default "
  3583. "values.\n");
  3584. }
  3585. TEST_F(ValidationErrorTest, NegativeFieldNumber) {
  3586. BuildFileWithErrors(
  3587. "name: \"foo.proto\" "
  3588. "message_type {"
  3589. " name: \"Foo\""
  3590. " field { name: \"foo\" number: -1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3591. "}"
  3592. "}",
  3593. "foo.proto: Foo.foo: NUMBER: Field numbers must be positive integers.\n");
  3594. }
  3595. TEST_F(ValidationErrorTest, HugeFieldNumber) {
  3596. BuildFileWithErrors(
  3597. "name: \"foo.proto\" "
  3598. "message_type {"
  3599. " name: \"Foo\""
  3600. " field { name: \"foo\" number: 0x70000000 "
  3601. " label:LABEL_OPTIONAL type:TYPE_INT32 }"
  3602. "}",
  3603. "foo.proto: Foo.foo: NUMBER: Field numbers cannot be greater than "
  3604. "536870911.\n");
  3605. }
  3606. TEST_F(ValidationErrorTest, ReservedFieldNumber) {
  3607. BuildFileWithErrors(
  3608. "name: \"foo.proto\" "
  3609. "message_type {"
  3610. " name: \"Foo\""
  3611. " field {name:\"foo\" number: 18999 label:LABEL_OPTIONAL "
  3612. "type:TYPE_INT32 }"
  3613. " field {name:\"bar\" number: 19000 label:LABEL_OPTIONAL "
  3614. "type:TYPE_INT32 }"
  3615. " field {name:\"baz\" number: 19999 label:LABEL_OPTIONAL "
  3616. "type:TYPE_INT32 }"
  3617. " field {name:\"qux\" number: 20000 label:LABEL_OPTIONAL "
  3618. "type:TYPE_INT32 }"
  3619. "}",
  3620. "foo.proto: Foo.bar: NUMBER: Field numbers 19000 through 19999 are "
  3621. "reserved for the protocol buffer library implementation.\n"
  3622. "foo.proto: Foo.baz: NUMBER: Field numbers 19000 through 19999 are "
  3623. "reserved for the protocol buffer library implementation.\n");
  3624. }
  3625. TEST_F(ValidationErrorTest, ExtensionMissingExtendee) {
  3626. BuildFileWithErrors(
  3627. "name: \"foo.proto\" "
  3628. "message_type {"
  3629. " name: \"Foo\""
  3630. " extension { name: \"foo\" number: 1 label: LABEL_OPTIONAL"
  3631. " type_name: \"Foo\" }"
  3632. "}",
  3633. "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee not set for "
  3634. "extension field.\n");
  3635. }
  3636. TEST_F(ValidationErrorTest, NonExtensionWithExtendee) {
  3637. BuildFileWithErrors(
  3638. "name: \"foo.proto\" "
  3639. "message_type {"
  3640. " name: \"Bar\""
  3641. " extension_range { start: 1 end: 2 }"
  3642. "}"
  3643. "message_type {"
  3644. " name: \"Foo\""
  3645. " field { name: \"foo\" number: 1 label: LABEL_OPTIONAL"
  3646. " type_name: \"Foo\" extendee: \"Bar\" }"
  3647. "}",
  3648. "foo.proto: Foo.foo: EXTENDEE: FieldDescriptorProto.extendee set for "
  3649. "non-extension field.\n");
  3650. }
  3651. TEST_F(ValidationErrorTest, FieldOneofIndexTooLarge) {
  3652. BuildFileWithErrors(
  3653. "name: \"foo.proto\" "
  3654. "message_type {"
  3655. " name: \"Foo\""
  3656. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3657. " oneof_index: 1 }"
  3658. " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3659. " oneof_index: 0 }"
  3660. " oneof_decl { name:\"bar\" }"
  3661. "}",
  3662. "foo.proto: Foo.foo: TYPE: FieldDescriptorProto.oneof_index 1 is out of "
  3663. "range for type \"Foo\".\n");
  3664. }
  3665. TEST_F(ValidationErrorTest, FieldOneofIndexNegative) {
  3666. BuildFileWithErrors(
  3667. "name: \"foo.proto\" "
  3668. "message_type {"
  3669. " name: \"Foo\""
  3670. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3671. " oneof_index: -1 }"
  3672. " field { name:\"dummy\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3673. " oneof_index: 0 }"
  3674. " oneof_decl { name:\"bar\" }"
  3675. "}",
  3676. "foo.proto: Foo.foo: TYPE: FieldDescriptorProto.oneof_index -1 is out "
  3677. "of "
  3678. "range for type \"Foo\".\n");
  3679. }
  3680. TEST_F(ValidationErrorTest, OneofFieldsConsecutiveDefinition) {
  3681. // Fields belonging to the same oneof must be defined consecutively.
  3682. BuildFileWithErrors(
  3683. "name: \"foo.proto\" "
  3684. "message_type {"
  3685. " name: \"Foo\""
  3686. " field { name:\"foo1\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3687. " oneof_index: 0 }"
  3688. " field { name:\"bar\" number: 2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  3689. " field { name:\"foo2\" number: 3 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3690. " oneof_index: 0 }"
  3691. " oneof_decl { name:\"foos\" }"
  3692. "}",
  3693. "foo.proto: Foo.bar: TYPE: Fields in the same oneof must be defined "
  3694. "consecutively. \"bar\" cannot be defined before the completion of the "
  3695. "\"foos\" oneof definition.\n");
  3696. // Prevent interleaved fields, which belong to different oneofs.
  3697. BuildFileWithErrors(
  3698. "name: \"foo2.proto\" "
  3699. "message_type {"
  3700. " name: \"Foo2\""
  3701. " field { name:\"foo1\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3702. " oneof_index: 0 }"
  3703. " field { name:\"bar1\" number: 2 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3704. " oneof_index: 1 }"
  3705. " field { name:\"foo2\" number: 3 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3706. " oneof_index: 0 }"
  3707. " field { name:\"bar2\" number: 4 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3708. " oneof_index: 1 }"
  3709. " oneof_decl { name:\"foos\" }"
  3710. " oneof_decl { name:\"bars\" }"
  3711. "}",
  3712. "foo2.proto: Foo2.bar1: TYPE: Fields in the same oneof must be defined "
  3713. "consecutively. \"bar1\" cannot be defined before the completion of the "
  3714. "\"foos\" oneof definition.\n"
  3715. "foo2.proto: Foo2.foo2: TYPE: Fields in the same oneof must be defined "
  3716. "consecutively. \"foo2\" cannot be defined before the completion of the "
  3717. "\"bars\" oneof definition.\n");
  3718. // Another case for normal fields and different oneof fields interleave.
  3719. BuildFileWithErrors(
  3720. "name: \"foo3.proto\" "
  3721. "message_type {"
  3722. " name: \"Foo3\""
  3723. " field { name:\"foo1\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3724. " oneof_index: 0 }"
  3725. " field { name:\"bar1\" number: 2 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3726. " oneof_index: 1 }"
  3727. " field { name:\"baz\" number: 3 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  3728. " field { name:\"foo2\" number: 4 label:LABEL_OPTIONAL type:TYPE_INT32 "
  3729. " oneof_index: 0 }"
  3730. " oneof_decl { name:\"foos\" }"
  3731. " oneof_decl { name:\"bars\" }"
  3732. "}",
  3733. "foo3.proto: Foo3.baz: TYPE: Fields in the same oneof must be defined "
  3734. "consecutively. \"baz\" cannot be defined before the completion of the "
  3735. "\"foos\" oneof definition.\n");
  3736. }
  3737. TEST_F(ValidationErrorTest, FieldNumberConflict) {
  3738. BuildFileWithErrors(
  3739. "name: \"foo.proto\" "
  3740. "message_type {"
  3741. " name: \"Foo\""
  3742. " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  3743. " field { name: \"bar\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  3744. "}",
  3745. "foo.proto: Foo.bar: NUMBER: Field number 1 has already been used in "
  3746. "\"Foo\" by field \"foo\".\n");
  3747. }
  3748. TEST_F(ValidationErrorTest, BadMessageSetExtensionType) {
  3749. BuildFileWithErrors(
  3750. "name: \"foo.proto\" "
  3751. "message_type {"
  3752. " name: \"MessageSet\""
  3753. " options { message_set_wire_format: true }"
  3754. " extension_range { start: 4 end: 5 }"
  3755. "}"
  3756. "message_type {"
  3757. " name: \"Foo\""
  3758. " extension { name:\"foo\" number:4 label:LABEL_OPTIONAL type:TYPE_INT32"
  3759. " extendee: \"MessageSet\" }"
  3760. "}",
  3761. "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional "
  3762. "messages.\n");
  3763. }
  3764. TEST_F(ValidationErrorTest, BadMessageSetExtensionLabel) {
  3765. BuildFileWithErrors(
  3766. "name: \"foo.proto\" "
  3767. "message_type {"
  3768. " name: \"MessageSet\""
  3769. " options { message_set_wire_format: true }"
  3770. " extension_range { start: 4 end: 5 }"
  3771. "}"
  3772. "message_type {"
  3773. " name: \"Foo\""
  3774. " extension { name:\"foo\" number:4 label:LABEL_REPEATED "
  3775. "type:TYPE_MESSAGE"
  3776. " type_name: \"Foo\" extendee: \"MessageSet\" }"
  3777. "}",
  3778. "foo.proto: Foo.foo: TYPE: Extensions of MessageSets must be optional "
  3779. "messages.\n");
  3780. }
  3781. TEST_F(ValidationErrorTest, FieldInMessageSet) {
  3782. BuildFileWithErrors(
  3783. "name: \"foo.proto\" "
  3784. "message_type {"
  3785. " name: \"Foo\""
  3786. " options { message_set_wire_format: true }"
  3787. " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  3788. "}",
  3789. "foo.proto: Foo.foo: NAME: MessageSets cannot have fields, only "
  3790. "extensions.\n");
  3791. }
  3792. TEST_F(ValidationErrorTest, NegativeExtensionRangeNumber) {
  3793. BuildFileWithErrors(
  3794. "name: \"foo.proto\" "
  3795. "message_type {"
  3796. " name: \"Foo\""
  3797. " extension_range { start: -10 end: -1 }"
  3798. "}",
  3799. "foo.proto: Foo: NUMBER: Extension numbers must be positive integers.\n");
  3800. }
  3801. TEST_F(ValidationErrorTest, HugeExtensionRangeNumber) {
  3802. BuildFileWithErrors(
  3803. "name: \"foo.proto\" "
  3804. "message_type {"
  3805. " name: \"Foo\""
  3806. " extension_range { start: 1 end: 0x70000000 }"
  3807. "}",
  3808. "foo.proto: Foo: NUMBER: Extension numbers cannot be greater than "
  3809. "536870911.\n");
  3810. }
  3811. TEST_F(ValidationErrorTest, ExtensionRangeEndBeforeStart) {
  3812. BuildFileWithErrors(
  3813. "name: \"foo.proto\" "
  3814. "message_type {"
  3815. " name: \"Foo\""
  3816. " extension_range { start: 10 end: 10 }"
  3817. " extension_range { start: 10 end: 5 }"
  3818. "}",
  3819. "foo.proto: Foo: NUMBER: Extension range end number must be greater than "
  3820. "start number.\n"
  3821. "foo.proto: Foo: NUMBER: Extension range end number must be greater than "
  3822. "start number.\n");
  3823. }
  3824. TEST_F(ValidationErrorTest, EmptyEnum) {
  3825. BuildFileWithErrors(
  3826. "name: \"foo.proto\" "
  3827. "enum_type { name: \"Foo\" }"
  3828. // Also use the empty enum in a message to make sure there are no crashes
  3829. // during validation (possible if the code attempts to derive a default
  3830. // value for the field).
  3831. "message_type {"
  3832. " name: \"Bar\""
  3833. " field { name: \"foo\" number: 1 label:LABEL_OPTIONAL "
  3834. "type_name:\"Foo\" }"
  3835. " field { name: \"bar\" number: 2 label:LABEL_OPTIONAL "
  3836. "type_name:\"Foo\" "
  3837. " default_value: \"NO_SUCH_VALUE\" }"
  3838. "}",
  3839. "foo.proto: Foo: NAME: Enums must contain at least one value.\n"
  3840. "foo.proto: Bar.bar: DEFAULT_VALUE: Enum type \"Foo\" has no value named "
  3841. "\"NO_SUCH_VALUE\".\n");
  3842. }
  3843. TEST_F(ValidationErrorTest, UndefinedExtendee) {
  3844. BuildFileWithErrors(
  3845. "name: \"foo.proto\" "
  3846. "message_type {"
  3847. " name: \"Foo\""
  3848. " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32"
  3849. " extendee: \"Bar\" }"
  3850. "}",
  3851. "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not defined.\n");
  3852. }
  3853. TEST_F(ValidationErrorTest, NonMessageExtendee) {
  3854. BuildFileWithErrors(
  3855. "name: \"foo.proto\" "
  3856. "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } }"
  3857. "message_type {"
  3858. " name: \"Foo\""
  3859. " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32"
  3860. " extendee: \"Bar\" }"
  3861. "}",
  3862. "foo.proto: Foo.foo: EXTENDEE: \"Bar\" is not a message type.\n");
  3863. }
  3864. TEST_F(ValidationErrorTest, NotAnExtensionNumber) {
  3865. BuildFileWithErrors(
  3866. "name: \"foo.proto\" "
  3867. "message_type {"
  3868. " name: \"Bar\""
  3869. "}"
  3870. "message_type {"
  3871. " name: \"Foo\""
  3872. " extension { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32"
  3873. " extendee: \"Bar\" }"
  3874. "}",
  3875. "foo.proto: Foo.foo: NUMBER: \"Bar\" does not declare 1 as an extension "
  3876. "number.\n");
  3877. }
  3878. TEST_F(ValidationErrorTest, RequiredExtension) {
  3879. BuildFileWithErrors(
  3880. "name: \"foo.proto\" "
  3881. "message_type {"
  3882. " name: \"Bar\""
  3883. " extension_range { start: 1000 end: 10000 }"
  3884. "}"
  3885. "message_type {"
  3886. " name: \"Foo\""
  3887. " extension {"
  3888. " name:\"foo\""
  3889. " number:1000"
  3890. " label:LABEL_REQUIRED"
  3891. " type:TYPE_INT32"
  3892. " extendee: \"Bar\""
  3893. " }"
  3894. "}",
  3895. "foo.proto: Foo.foo: TYPE: The extension Foo.foo cannot be required.\n");
  3896. }
  3897. TEST_F(ValidationErrorTest, UndefinedFieldType) {
  3898. BuildFileWithErrors(
  3899. "name: \"foo.proto\" "
  3900. "message_type {"
  3901. " name: \"Foo\""
  3902. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  3903. "}",
  3904. "foo.proto: Foo.foo: TYPE: \"Bar\" is not defined.\n");
  3905. }
  3906. TEST_F(ValidationErrorTest, UndefinedFieldTypeWithDefault) {
  3907. // See b/12533582. Previously this failed because the default value was not
  3908. // accepted by the parser, which assumed an enum type, leading to an unclear
  3909. // error message. We want this input to yield a validation error instead,
  3910. // since the unknown type is the primary problem.
  3911. BuildFileWithErrors(
  3912. "name: \"foo.proto\" "
  3913. "message_type {"
  3914. " name: \"Foo\""
  3915. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"int\" "
  3916. " default_value:\"1\" }"
  3917. "}",
  3918. "foo.proto: Foo.foo: TYPE: \"int\" is not defined.\n");
  3919. }
  3920. TEST_F(ValidationErrorTest, UndefinedNestedFieldType) {
  3921. BuildFileWithErrors(
  3922. "name: \"foo.proto\" "
  3923. "message_type {"
  3924. " name: \"Foo\""
  3925. " nested_type { name:\"Baz\" }"
  3926. " field { name:\"foo\" number:1"
  3927. " label:LABEL_OPTIONAL"
  3928. " type_name:\"Foo.Baz.Bar\" }"
  3929. "}",
  3930. "foo.proto: Foo.foo: TYPE: \"Foo.Baz.Bar\" is not defined.\n");
  3931. }
  3932. TEST_F(ValidationErrorTest, FieldTypeDefinedInUndeclaredDependency) {
  3933. BuildFile(
  3934. "name: \"bar.proto\" "
  3935. "message_type { name: \"Bar\" } ");
  3936. BuildFileWithErrors(
  3937. "name: \"foo.proto\" "
  3938. "message_type {"
  3939. " name: \"Foo\""
  3940. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  3941. "}",
  3942. "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", "
  3943. "which is not imported by \"foo.proto\". To use it here, please add the "
  3944. "necessary import.\n");
  3945. }
  3946. TEST_F(ValidationErrorTest, FieldTypeDefinedInIndirectDependency) {
  3947. // Test for hidden dependencies.
  3948. //
  3949. // // bar.proto
  3950. // message Bar{}
  3951. //
  3952. // // forward.proto
  3953. // import "bar.proto"
  3954. //
  3955. // // foo.proto
  3956. // import "forward.proto"
  3957. // message Foo {
  3958. // optional Bar foo = 1; // Error, needs to import bar.proto explicitly.
  3959. // }
  3960. //
  3961. BuildFile(
  3962. "name: \"bar.proto\" "
  3963. "message_type { name: \"Bar\" }");
  3964. BuildFile(
  3965. "name: \"forward.proto\""
  3966. "dependency: \"bar.proto\"");
  3967. BuildFileWithErrors(
  3968. "name: \"foo.proto\" "
  3969. "dependency: \"forward.proto\" "
  3970. "message_type {"
  3971. " name: \"Foo\""
  3972. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  3973. "}",
  3974. "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", "
  3975. "which is not imported by \"foo.proto\". To use it here, please add the "
  3976. "necessary import.\n");
  3977. }
  3978. TEST_F(ValidationErrorTest, FieldTypeDefinedInPublicDependency) {
  3979. // Test for public dependencies.
  3980. //
  3981. // // bar.proto
  3982. // message Bar{}
  3983. //
  3984. // // forward.proto
  3985. // import public "bar.proto"
  3986. //
  3987. // // foo.proto
  3988. // import "forward.proto"
  3989. // message Foo {
  3990. // optional Bar foo = 1; // Correct. "bar.proto" is public imported into
  3991. // // forward.proto, so when "foo.proto" imports
  3992. // // "forward.proto", it imports "bar.proto" too.
  3993. // }
  3994. //
  3995. BuildFile(
  3996. "name: \"bar.proto\" "
  3997. "message_type { name: \"Bar\" }");
  3998. BuildFile(
  3999. "name: \"forward.proto\""
  4000. "dependency: \"bar.proto\" "
  4001. "public_dependency: 0");
  4002. BuildFile(
  4003. "name: \"foo.proto\" "
  4004. "dependency: \"forward.proto\" "
  4005. "message_type {"
  4006. " name: \"Foo\""
  4007. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  4008. "}");
  4009. }
  4010. TEST_F(ValidationErrorTest, FieldTypeDefinedInTransitivePublicDependency) {
  4011. // Test for public dependencies.
  4012. //
  4013. // // bar.proto
  4014. // message Bar{}
  4015. //
  4016. // // forward.proto
  4017. // import public "bar.proto"
  4018. //
  4019. // // forward2.proto
  4020. // import public "forward.proto"
  4021. //
  4022. // // foo.proto
  4023. // import "forward2.proto"
  4024. // message Foo {
  4025. // optional Bar foo = 1; // Correct, public imports are transitive.
  4026. // }
  4027. //
  4028. BuildFile(
  4029. "name: \"bar.proto\" "
  4030. "message_type { name: \"Bar\" }");
  4031. BuildFile(
  4032. "name: \"forward.proto\""
  4033. "dependency: \"bar.proto\" "
  4034. "public_dependency: 0");
  4035. BuildFile(
  4036. "name: \"forward2.proto\""
  4037. "dependency: \"forward.proto\" "
  4038. "public_dependency: 0");
  4039. BuildFile(
  4040. "name: \"foo.proto\" "
  4041. "dependency: \"forward2.proto\" "
  4042. "message_type {"
  4043. " name: \"Foo\""
  4044. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  4045. "}");
  4046. }
  4047. TEST_F(ValidationErrorTest,
  4048. FieldTypeDefinedInPrivateDependencyOfPublicDependency) {
  4049. // Test for public dependencies.
  4050. //
  4051. // // bar.proto
  4052. // message Bar{}
  4053. //
  4054. // // forward.proto
  4055. // import "bar.proto"
  4056. //
  4057. // // forward2.proto
  4058. // import public "forward.proto"
  4059. //
  4060. // // foo.proto
  4061. // import "forward2.proto"
  4062. // message Foo {
  4063. // optional Bar foo = 1; // Error, the "bar.proto" is not public imported
  4064. // // into "forward.proto", so will not be imported
  4065. // // into either "forward2.proto" or "foo.proto".
  4066. // }
  4067. //
  4068. BuildFile(
  4069. "name: \"bar.proto\" "
  4070. "message_type { name: \"Bar\" }");
  4071. BuildFile(
  4072. "name: \"forward.proto\""
  4073. "dependency: \"bar.proto\"");
  4074. BuildFile(
  4075. "name: \"forward2.proto\""
  4076. "dependency: \"forward.proto\" "
  4077. "public_dependency: 0");
  4078. BuildFileWithErrors(
  4079. "name: \"foo.proto\" "
  4080. "dependency: \"forward2.proto\" "
  4081. "message_type {"
  4082. " name: \"Foo\""
  4083. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  4084. "}",
  4085. "foo.proto: Foo.foo: TYPE: \"Bar\" seems to be defined in \"bar.proto\", "
  4086. "which is not imported by \"foo.proto\". To use it here, please add the "
  4087. "necessary import.\n");
  4088. }
  4089. TEST_F(ValidationErrorTest, SearchMostLocalFirst) {
  4090. // The following should produce an error that Bar.Baz is resolved but
  4091. // not defined:
  4092. // message Bar { message Baz {} }
  4093. // message Foo {
  4094. // message Bar {
  4095. // // Placing "message Baz{}" here, or removing Foo.Bar altogether,
  4096. // // would fix the error.
  4097. // }
  4098. // optional Bar.Baz baz = 1;
  4099. // }
  4100. // An one point the lookup code incorrectly did not produce an error in this
  4101. // case, because when looking for Bar.Baz, it would try "Foo.Bar.Baz" first,
  4102. // fail, and ten try "Bar.Baz" and succeed, even though "Bar" should actually
  4103. // refer to the inner Bar, not the outer one.
  4104. BuildFileWithErrors(
  4105. "name: \"foo.proto\" "
  4106. "message_type {"
  4107. " name: \"Bar\""
  4108. " nested_type { name: \"Baz\" }"
  4109. "}"
  4110. "message_type {"
  4111. " name: \"Foo\""
  4112. " nested_type { name: \"Bar\" }"
  4113. " field { name:\"baz\" number:1 label:LABEL_OPTIONAL"
  4114. " type_name:\"Bar.Baz\" }"
  4115. "}",
  4116. "foo.proto: Foo.baz: TYPE: \"Bar.Baz\" is resolved to \"Foo.Bar.Baz\","
  4117. " which is not defined. The innermost scope is searched first in name "
  4118. "resolution. Consider using a leading '.'(i.e., \".Bar.Baz\") to start "
  4119. "from the outermost scope.\n");
  4120. }
  4121. TEST_F(ValidationErrorTest, SearchMostLocalFirst2) {
  4122. // This test would find the most local "Bar" first, and does, but
  4123. // proceeds to find the outer one because the inner one's not an
  4124. // aggregate.
  4125. BuildFile(
  4126. "name: \"foo.proto\" "
  4127. "message_type {"
  4128. " name: \"Bar\""
  4129. " nested_type { name: \"Baz\" }"
  4130. "}"
  4131. "message_type {"
  4132. " name: \"Foo\""
  4133. " field { name: \"Bar\" number:1 type:TYPE_BYTES } "
  4134. " field { name:\"baz\" number:2 label:LABEL_OPTIONAL"
  4135. " type_name:\"Bar.Baz\" }"
  4136. "}");
  4137. }
  4138. TEST_F(ValidationErrorTest, PackageOriginallyDeclaredInTransitiveDependent) {
  4139. // Imagine we have the following:
  4140. //
  4141. // foo.proto:
  4142. // package foo.bar;
  4143. // bar.proto:
  4144. // package foo.bar;
  4145. // import "foo.proto";
  4146. // message Bar {}
  4147. // baz.proto:
  4148. // package foo;
  4149. // import "bar.proto"
  4150. // message Baz { optional bar.Bar qux = 1; }
  4151. //
  4152. // When validating baz.proto, we will look up "bar.Bar". As part of this
  4153. // lookup, we first lookup "bar" then try to find "Bar" within it. "bar"
  4154. // should resolve to "foo.bar". Note, though, that "foo.bar" was originally
  4155. // defined in foo.proto, which is not a direct dependency of baz.proto. The
  4156. // implementation of FindSymbol() normally only returns symbols in direct
  4157. // dependencies, not indirect ones. This test insures that this does not
  4158. // prevent it from finding "foo.bar".
  4159. BuildFile(
  4160. "name: \"foo.proto\" "
  4161. "package: \"foo.bar\" ");
  4162. BuildFile(
  4163. "name: \"bar.proto\" "
  4164. "package: \"foo.bar\" "
  4165. "dependency: \"foo.proto\" "
  4166. "message_type { name: \"Bar\" }");
  4167. BuildFile(
  4168. "name: \"baz.proto\" "
  4169. "package: \"foo\" "
  4170. "dependency: \"bar.proto\" "
  4171. "message_type { "
  4172. " name: \"Baz\" "
  4173. " field { name:\"qux\" number:1 label:LABEL_OPTIONAL "
  4174. " type_name:\"bar.Bar\" }"
  4175. "}");
  4176. }
  4177. TEST_F(ValidationErrorTest, FieldTypeNotAType) {
  4178. BuildFileWithErrors(
  4179. "name: \"foo.proto\" "
  4180. "message_type {"
  4181. " name: \"Foo\""
  4182. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL "
  4183. " type_name:\".Foo.bar\" }"
  4184. " field { name:\"bar\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  4185. "}",
  4186. "foo.proto: Foo.foo: TYPE: \".Foo.bar\" is not a type.\n");
  4187. }
  4188. TEST_F(ValidationErrorTest, RelativeFieldTypeNotAType) {
  4189. BuildFileWithErrors(
  4190. "name: \"foo.proto\" "
  4191. "message_type {"
  4192. " nested_type {"
  4193. " name: \"Bar\""
  4194. " field { name:\"Baz\" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  4195. " }"
  4196. " name: \"Foo\""
  4197. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL "
  4198. " type_name:\"Bar.Baz\" }"
  4199. "}",
  4200. "foo.proto: Foo.foo: TYPE: \"Bar.Baz\" is not a type.\n");
  4201. }
  4202. TEST_F(ValidationErrorTest, FieldTypeMayBeItsName) {
  4203. BuildFile(
  4204. "name: \"foo.proto\" "
  4205. "message_type {"
  4206. " name: \"Bar\""
  4207. "}"
  4208. "message_type {"
  4209. " name: \"Foo\""
  4210. " field { name:\"Bar\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\" }"
  4211. "}");
  4212. }
  4213. TEST_F(ValidationErrorTest, EnumFieldTypeIsMessage) {
  4214. BuildFileWithErrors(
  4215. "name: \"foo.proto\" "
  4216. "message_type { name: \"Bar\" } "
  4217. "message_type {"
  4218. " name: \"Foo\""
  4219. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM"
  4220. " type_name:\"Bar\" }"
  4221. "}",
  4222. "foo.proto: Foo.foo: TYPE: \"Bar\" is not an enum type.\n");
  4223. }
  4224. TEST_F(ValidationErrorTest, MessageFieldTypeIsEnum) {
  4225. BuildFileWithErrors(
  4226. "name: \"foo.proto\" "
  4227. "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } "
  4228. "message_type {"
  4229. " name: \"Foo\""
  4230. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE"
  4231. " type_name:\"Bar\" }"
  4232. "}",
  4233. "foo.proto: Foo.foo: TYPE: \"Bar\" is not a message type.\n");
  4234. }
  4235. TEST_F(ValidationErrorTest, BadEnumDefaultValue) {
  4236. BuildFileWithErrors(
  4237. "name: \"foo.proto\" "
  4238. "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } "
  4239. "message_type {"
  4240. " name: \"Foo\""
  4241. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\""
  4242. " default_value:\"NO_SUCH_VALUE\" }"
  4243. "}",
  4244. "foo.proto: Foo.foo: DEFAULT_VALUE: Enum type \"Bar\" has no value named "
  4245. "\"NO_SUCH_VALUE\".\n");
  4246. }
  4247. TEST_F(ValidationErrorTest, EnumDefaultValueIsInteger) {
  4248. BuildFileWithErrors(
  4249. "name: \"foo.proto\" "
  4250. "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } "
  4251. "message_type {"
  4252. " name: \"Foo\""
  4253. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type_name:\"Bar\""
  4254. " default_value:\"0\" }"
  4255. "}",
  4256. "foo.proto: Foo.foo: DEFAULT_VALUE: Default value for an enum field must "
  4257. "be an identifier.\n");
  4258. }
  4259. TEST_F(ValidationErrorTest, PrimitiveWithTypeName) {
  4260. BuildFileWithErrors(
  4261. "name: \"foo.proto\" "
  4262. "message_type {"
  4263. " name: \"Foo\""
  4264. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_INT32"
  4265. " type_name:\"Foo\" }"
  4266. "}",
  4267. "foo.proto: Foo.foo: TYPE: Field with primitive type has type_name.\n");
  4268. }
  4269. TEST_F(ValidationErrorTest, NonPrimitiveWithoutTypeName) {
  4270. BuildFileWithErrors(
  4271. "name: \"foo.proto\" "
  4272. "message_type {"
  4273. " name: \"Foo\""
  4274. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE }"
  4275. "}",
  4276. "foo.proto: Foo.foo: TYPE: Field with message or enum type missing "
  4277. "type_name.\n");
  4278. }
  4279. TEST_F(ValidationErrorTest, OneofWithNoFields) {
  4280. BuildFileWithErrors(
  4281. "name: \"foo.proto\" "
  4282. "message_type {"
  4283. " name: \"Foo\""
  4284. " oneof_decl { name:\"bar\" }"
  4285. "}",
  4286. "foo.proto: Foo.bar: NAME: Oneof must have at least one field.\n");
  4287. }
  4288. TEST_F(ValidationErrorTest, OneofLabelMismatch) {
  4289. BuildFileWithErrors(
  4290. "name: \"foo.proto\" "
  4291. "message_type {"
  4292. " name: \"Foo\""
  4293. " field { name:\"foo\" number:1 label:LABEL_REPEATED type:TYPE_INT32 "
  4294. " oneof_index:0 }"
  4295. " oneof_decl { name:\"bar\" }"
  4296. "}",
  4297. "foo.proto: Foo.foo: NAME: Fields of oneofs must themselves have label "
  4298. "LABEL_OPTIONAL.\n");
  4299. }
  4300. TEST_F(ValidationErrorTest, InputTypeNotDefined) {
  4301. BuildFileWithErrors(
  4302. "name: \"foo.proto\" "
  4303. "message_type { name: \"Foo\" } "
  4304. "service {"
  4305. " name: \"TestService\""
  4306. " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }"
  4307. "}",
  4308. "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not defined.\n"
  4309. );
  4310. }
  4311. TEST_F(ValidationErrorTest, InputTypeNotAMessage) {
  4312. BuildFileWithErrors(
  4313. "name: \"foo.proto\" "
  4314. "message_type { name: \"Foo\" } "
  4315. "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } "
  4316. "service {"
  4317. " name: \"TestService\""
  4318. " method { name: \"A\" input_type: \"Bar\" output_type: \"Foo\" }"
  4319. "}",
  4320. "foo.proto: TestService.A: INPUT_TYPE: \"Bar\" is not a message type.\n"
  4321. );
  4322. }
  4323. TEST_F(ValidationErrorTest, OutputTypeNotDefined) {
  4324. BuildFileWithErrors(
  4325. "name: \"foo.proto\" "
  4326. "message_type { name: \"Foo\" } "
  4327. "service {"
  4328. " name: \"TestService\""
  4329. " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }"
  4330. "}",
  4331. "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not defined.\n"
  4332. );
  4333. }
  4334. TEST_F(ValidationErrorTest, OutputTypeNotAMessage) {
  4335. BuildFileWithErrors(
  4336. "name: \"foo.proto\" "
  4337. "message_type { name: \"Foo\" } "
  4338. "enum_type { name: \"Bar\" value { name:\"DUMMY\" number:0 } } "
  4339. "service {"
  4340. " name: \"TestService\""
  4341. " method { name: \"A\" input_type: \"Foo\" output_type: \"Bar\" }"
  4342. "}",
  4343. "foo.proto: TestService.A: OUTPUT_TYPE: \"Bar\" is not a message type.\n"
  4344. );
  4345. }
  4346. TEST_F(ValidationErrorTest, IllegalPackedField) {
  4347. BuildFileWithErrors(
  4348. "name: \"foo.proto\" "
  4349. "message_type {\n"
  4350. " name: \"Foo\""
  4351. " field { name:\"packed_string\" number:1 label:LABEL_REPEATED "
  4352. " type:TYPE_STRING "
  4353. " options { uninterpreted_option {"
  4354. " name { name_part: \"packed\" is_extension: false }"
  4355. " identifier_value: \"true\" }}}\n"
  4356. " field { name:\"packed_message\" number:3 label:LABEL_REPEATED "
  4357. " type_name: \"Foo\""
  4358. " options { uninterpreted_option {"
  4359. " name { name_part: \"packed\" is_extension: false }"
  4360. " identifier_value: \"true\" }}}\n"
  4361. " field { name:\"optional_int32\" number: 4 label: LABEL_OPTIONAL "
  4362. " type:TYPE_INT32 "
  4363. " options { uninterpreted_option {"
  4364. " name { name_part: \"packed\" is_extension: false }"
  4365. " identifier_value: \"true\" }}}\n"
  4366. "}",
  4367. "foo.proto: Foo.packed_string: TYPE: [packed = true] can only be "
  4368. "specified for repeated primitive fields.\n"
  4369. "foo.proto: Foo.packed_message: TYPE: [packed = true] can only be "
  4370. "specified for repeated primitive fields.\n"
  4371. "foo.proto: Foo.optional_int32: TYPE: [packed = true] can only be "
  4372. "specified for repeated primitive fields.\n");
  4373. }
  4374. TEST_F(ValidationErrorTest, OptionWrongType) {
  4375. BuildFileWithErrors(
  4376. "name: \"foo.proto\" "
  4377. "message_type { "
  4378. " name: \"TestMessage\" "
  4379. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING "
  4380. " options { uninterpreted_option { name { name_part: \"ctype\" "
  4381. " is_extension: false }"
  4382. " positive_int_value: 1 }"
  4383. " }"
  4384. " }"
  4385. "}\n",
  4386. "foo.proto: TestMessage.foo: OPTION_VALUE: Value must be identifier for "
  4387. "enum-valued option \"google.protobuf.FieldOptions.ctype\".\n");
  4388. }
  4389. TEST_F(ValidationErrorTest, OptionExtendsAtomicType) {
  4390. BuildFileWithErrors(
  4391. "name: \"foo.proto\" "
  4392. "message_type { "
  4393. " name: \"TestMessage\" "
  4394. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_STRING "
  4395. " options { uninterpreted_option { name { name_part: \"ctype\" "
  4396. " is_extension: false }"
  4397. " name { name_part: \"foo\" "
  4398. " is_extension: true }"
  4399. " positive_int_value: 1 }"
  4400. " }"
  4401. " }"
  4402. "}\n",
  4403. "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" is an "
  4404. "atomic type, not a message.\n");
  4405. }
  4406. TEST_F(ValidationErrorTest, DupOption) {
  4407. BuildFileWithErrors(
  4408. "name: \"foo.proto\" "
  4409. "message_type { "
  4410. " name: \"TestMessage\" "
  4411. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_UINT32 "
  4412. " options { uninterpreted_option { name { name_part: \"ctype\" "
  4413. " is_extension: false }"
  4414. " identifier_value: \"CORD\" }"
  4415. " uninterpreted_option { name { name_part: \"ctype\" "
  4416. " is_extension: false }"
  4417. " identifier_value: \"CORD\" }"
  4418. " }"
  4419. " }"
  4420. "}\n",
  4421. "foo.proto: TestMessage.foo: OPTION_NAME: Option \"ctype\" was "
  4422. "already set.\n");
  4423. }
  4424. TEST_F(ValidationErrorTest, InvalidOptionName) {
  4425. BuildFileWithErrors(
  4426. "name: \"foo.proto\" "
  4427. "message_type { "
  4428. " name: \"TestMessage\" "
  4429. " field { name:\"foo\" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL "
  4430. " options { uninterpreted_option { "
  4431. " name { name_part: \"uninterpreted_option\" "
  4432. " is_extension: false }"
  4433. " positive_int_value: 1 "
  4434. " }"
  4435. " }"
  4436. " }"
  4437. "}\n",
  4438. "foo.proto: TestMessage.foo: OPTION_NAME: Option must not use "
  4439. "reserved name \"uninterpreted_option\".\n");
  4440. }
  4441. TEST_F(ValidationErrorTest, RepeatedMessageOption) {
  4442. BuildDescriptorMessagesInTestPool();
  4443. BuildFileWithErrors(
  4444. "name: \"foo.proto\" "
  4445. "dependency: \"google/protobuf/descriptor.proto\" "
  4446. "message_type: { name: \"Bar\" field: { "
  4447. " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } "
  4448. "} "
  4449. "extension { name: \"bar\" number: 7672757 label: LABEL_REPEATED "
  4450. " type: TYPE_MESSAGE type_name: \"Bar\" "
  4451. " extendee: \"google.protobuf.FileOptions\" }"
  4452. "options { uninterpreted_option { name { name_part: \"bar\" "
  4453. " is_extension: true } "
  4454. " name { name_part: \"foo\" "
  4455. " is_extension: false } "
  4456. " positive_int_value: 1 } }",
  4457. "foo.proto: foo.proto: OPTION_NAME: Option field \"(bar)\" is a "
  4458. "repeated message. Repeated message options must be initialized "
  4459. "using an aggregate value.\n");
  4460. }
  4461. TEST_F(ValidationErrorTest, ResolveUndefinedOption) {
  4462. // The following should produce an eror that baz.bar is resolved but not
  4463. // defined.
  4464. // foo.proto:
  4465. // package baz
  4466. // import google/protobuf/descriptor.proto
  4467. // message Bar { optional int32 foo = 1; }
  4468. // extend FileOptions { optional Bar bar = 7672757; }
  4469. //
  4470. // qux.proto:
  4471. // package qux.baz
  4472. // option (baz.bar).foo = 1;
  4473. //
  4474. // Although "baz.bar" is already defined, the lookup code will try
  4475. // "qux.baz.bar", since it's the match from the innermost scope, which will
  4476. // cause a symbol not defined error.
  4477. BuildDescriptorMessagesInTestPool();
  4478. BuildFile(
  4479. "name: \"foo.proto\" "
  4480. "package: \"baz\" "
  4481. "dependency: \"google/protobuf/descriptor.proto\" "
  4482. "message_type: { name: \"Bar\" field: { "
  4483. " name: \"foo\" number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 } "
  4484. "} "
  4485. "extension { name: \"bar\" number: 7672757 label: LABEL_OPTIONAL "
  4486. " type: TYPE_MESSAGE type_name: \"Bar\" "
  4487. " extendee: \"google.protobuf.FileOptions\" }");
  4488. BuildFileWithErrors(
  4489. "name: \"qux.proto\" "
  4490. "package: \"qux.baz\" "
  4491. "options { uninterpreted_option { name { name_part: \"baz.bar\" "
  4492. " is_extension: true } "
  4493. " name { name_part: \"foo\" "
  4494. " is_extension: false } "
  4495. " positive_int_value: 1 } }",
  4496. "qux.proto: qux.proto: OPTION_NAME: Option \"(baz.bar)\" is resolved to "
  4497. "\"(qux.baz.bar)\","
  4498. " which is not defined. The innermost scope is searched first in name "
  4499. "resolution. Consider using a leading '.'(i.e., \"(.baz.bar)\") to start "
  4500. "from the outermost scope.\n");
  4501. }
  4502. TEST_F(ValidationErrorTest, UnknownOption) {
  4503. BuildFileWithErrors(
  4504. "name: \"qux.proto\" "
  4505. "package: \"qux.baz\" "
  4506. "options { uninterpreted_option { name { name_part: \"baaz.bar\" "
  4507. " is_extension: true } "
  4508. " name { name_part: \"foo\" "
  4509. " is_extension: false } "
  4510. " positive_int_value: 1 } }",
  4511. "qux.proto: qux.proto: OPTION_NAME: Option \"(baaz.bar)\" unknown. "
  4512. "Ensure "
  4513. "that your proto definition file imports the proto which defines the "
  4514. "option.\n");
  4515. }
  4516. TEST_F(ValidationErrorTest, CustomOptionConflictingFieldNumber) {
  4517. BuildDescriptorMessagesInTestPool();
  4518. BuildFileWithErrors(
  4519. "name: \"foo.proto\" "
  4520. "dependency: \"google/protobuf/descriptor.proto\" "
  4521. "extension { name: \"foo1\" number: 7672757 label: LABEL_OPTIONAL "
  4522. " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }"
  4523. "extension { name: \"foo2\" number: 7672757 label: LABEL_OPTIONAL "
  4524. " type: TYPE_INT32 extendee: \"google.protobuf.FieldOptions\" }",
  4525. "foo.proto: foo2: NUMBER: Extension number 7672757 has already been used "
  4526. "in \"google.protobuf.FieldOptions\" by extension \"foo1\".\n");
  4527. }
  4528. TEST_F(ValidationErrorTest, Int32OptionValueOutOfPositiveRange) {
  4529. BuildDescriptorMessagesInTestPool();
  4530. BuildFileWithErrors(
  4531. "name: \"foo.proto\" "
  4532. "dependency: \"google/protobuf/descriptor.proto\" "
  4533. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4534. " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }"
  4535. "options { uninterpreted_option { name { name_part: \"foo\" "
  4536. " is_extension: true } "
  4537. " positive_int_value: 0x80000000 } "
  4538. "}",
  4539. "foo.proto: foo.proto: OPTION_VALUE: Value out of range "
  4540. "for int32 option \"foo\".\n");
  4541. }
  4542. TEST_F(ValidationErrorTest, Int32OptionValueOutOfNegativeRange) {
  4543. BuildDescriptorMessagesInTestPool();
  4544. BuildFileWithErrors(
  4545. "name: \"foo.proto\" "
  4546. "dependency: \"google/protobuf/descriptor.proto\" "
  4547. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4548. " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }"
  4549. "options { uninterpreted_option { name { name_part: \"foo\" "
  4550. " is_extension: true } "
  4551. " negative_int_value: -0x80000001 } "
  4552. "}",
  4553. "foo.proto: foo.proto: OPTION_VALUE: Value out of range "
  4554. "for int32 option \"foo\".\n");
  4555. }
  4556. TEST_F(ValidationErrorTest, Int32OptionValueIsNotPositiveInt) {
  4557. BuildDescriptorMessagesInTestPool();
  4558. BuildFileWithErrors(
  4559. "name: \"foo.proto\" "
  4560. "dependency: \"google/protobuf/descriptor.proto\" "
  4561. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4562. " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }"
  4563. "options { uninterpreted_option { name { name_part: \"foo\" "
  4564. " is_extension: true } "
  4565. " string_value: \"5\" } }",
  4566. "foo.proto: foo.proto: OPTION_VALUE: Value must be integer "
  4567. "for int32 option \"foo\".\n");
  4568. }
  4569. TEST_F(ValidationErrorTest, Int64OptionValueOutOfRange) {
  4570. BuildDescriptorMessagesInTestPool();
  4571. BuildFileWithErrors(
  4572. "name: \"foo.proto\" "
  4573. "dependency: \"google/protobuf/descriptor.proto\" "
  4574. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4575. " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }"
  4576. "options { uninterpreted_option { name { name_part: \"foo\" "
  4577. " is_extension: true } "
  4578. " positive_int_value: 0x8000000000000000 "
  4579. "} "
  4580. "}",
  4581. "foo.proto: foo.proto: OPTION_VALUE: Value out of range "
  4582. "for int64 option \"foo\".\n");
  4583. }
  4584. TEST_F(ValidationErrorTest, Int64OptionValueIsNotPositiveInt) {
  4585. BuildDescriptorMessagesInTestPool();
  4586. BuildFileWithErrors(
  4587. "name: \"foo.proto\" "
  4588. "dependency: \"google/protobuf/descriptor.proto\" "
  4589. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4590. " type: TYPE_INT64 extendee: \"google.protobuf.FileOptions\" }"
  4591. "options { uninterpreted_option { name { name_part: \"foo\" "
  4592. " is_extension: true } "
  4593. " identifier_value: \"5\" } }",
  4594. "foo.proto: foo.proto: OPTION_VALUE: Value must be integer "
  4595. "for int64 option \"foo\".\n");
  4596. }
  4597. TEST_F(ValidationErrorTest, UInt32OptionValueOutOfRange) {
  4598. BuildDescriptorMessagesInTestPool();
  4599. BuildFileWithErrors(
  4600. "name: \"foo.proto\" "
  4601. "dependency: \"google/protobuf/descriptor.proto\" "
  4602. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4603. " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }"
  4604. "options { uninterpreted_option { name { name_part: \"foo\" "
  4605. " is_extension: true } "
  4606. " positive_int_value: 0x100000000 } }",
  4607. "foo.proto: foo.proto: OPTION_VALUE: Value out of range "
  4608. "for uint32 option \"foo\".\n");
  4609. }
  4610. TEST_F(ValidationErrorTest, UInt32OptionValueIsNotPositiveInt) {
  4611. BuildDescriptorMessagesInTestPool();
  4612. BuildFileWithErrors(
  4613. "name: \"foo.proto\" "
  4614. "dependency: \"google/protobuf/descriptor.proto\" "
  4615. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4616. " type: TYPE_UINT32 extendee: \"google.protobuf.FileOptions\" }"
  4617. "options { uninterpreted_option { name { name_part: \"foo\" "
  4618. " is_extension: true } "
  4619. " double_value: -5.6 } }",
  4620. "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer "
  4621. "for uint32 option \"foo\".\n");
  4622. }
  4623. TEST_F(ValidationErrorTest, UInt64OptionValueIsNotPositiveInt) {
  4624. BuildDescriptorMessagesInTestPool();
  4625. BuildFileWithErrors(
  4626. "name: \"foo.proto\" "
  4627. "dependency: \"google/protobuf/descriptor.proto\" "
  4628. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4629. " type: TYPE_UINT64 extendee: \"google.protobuf.FileOptions\" }"
  4630. "options { uninterpreted_option { name { name_part: \"foo\" "
  4631. " is_extension: true } "
  4632. " negative_int_value: -5 } }",
  4633. "foo.proto: foo.proto: OPTION_VALUE: Value must be non-negative integer "
  4634. "for uint64 option \"foo\".\n");
  4635. }
  4636. TEST_F(ValidationErrorTest, FloatOptionValueIsNotNumber) {
  4637. BuildDescriptorMessagesInTestPool();
  4638. BuildFileWithErrors(
  4639. "name: \"foo.proto\" "
  4640. "dependency: \"google/protobuf/descriptor.proto\" "
  4641. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4642. " type: TYPE_FLOAT extendee: \"google.protobuf.FileOptions\" }"
  4643. "options { uninterpreted_option { name { name_part: \"foo\" "
  4644. " is_extension: true } "
  4645. " string_value: \"bar\" } }",
  4646. "foo.proto: foo.proto: OPTION_VALUE: Value must be number "
  4647. "for float option \"foo\".\n");
  4648. }
  4649. TEST_F(ValidationErrorTest, DoubleOptionValueIsNotNumber) {
  4650. BuildDescriptorMessagesInTestPool();
  4651. BuildFileWithErrors(
  4652. "name: \"foo.proto\" "
  4653. "dependency: \"google/protobuf/descriptor.proto\" "
  4654. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4655. " type: TYPE_DOUBLE extendee: \"google.protobuf.FileOptions\" }"
  4656. "options { uninterpreted_option { name { name_part: \"foo\" "
  4657. " is_extension: true } "
  4658. " string_value: \"bar\" } }",
  4659. "foo.proto: foo.proto: OPTION_VALUE: Value must be number "
  4660. "for double option \"foo\".\n");
  4661. }
  4662. TEST_F(ValidationErrorTest, BoolOptionValueIsNotTrueOrFalse) {
  4663. BuildDescriptorMessagesInTestPool();
  4664. BuildFileWithErrors(
  4665. "name: \"foo.proto\" "
  4666. "dependency: \"google/protobuf/descriptor.proto\" "
  4667. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4668. " type: TYPE_BOOL extendee: \"google.protobuf.FileOptions\" }"
  4669. "options { uninterpreted_option { name { name_part: \"foo\" "
  4670. " is_extension: true } "
  4671. " identifier_value: \"bar\" } }",
  4672. "foo.proto: foo.proto: OPTION_VALUE: Value must be \"true\" or \"false\" "
  4673. "for boolean option \"foo\".\n");
  4674. }
  4675. TEST_F(ValidationErrorTest, EnumOptionValueIsNotIdentifier) {
  4676. BuildDescriptorMessagesInTestPool();
  4677. BuildFileWithErrors(
  4678. "name: \"foo.proto\" "
  4679. "dependency: \"google/protobuf/descriptor.proto\" "
  4680. "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } "
  4681. " value { name: \"BAZ\" number: 2 } }"
  4682. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4683. " type: TYPE_ENUM type_name: \"FooEnum\" "
  4684. " extendee: \"google.protobuf.FileOptions\" }"
  4685. "options { uninterpreted_option { name { name_part: \"foo\" "
  4686. " is_extension: true } "
  4687. " string_value: \"QUUX\" } }",
  4688. "foo.proto: foo.proto: OPTION_VALUE: Value must be identifier for "
  4689. "enum-valued option \"foo\".\n");
  4690. }
  4691. TEST_F(ValidationErrorTest, EnumOptionValueIsNotEnumValueName) {
  4692. BuildDescriptorMessagesInTestPool();
  4693. BuildFileWithErrors(
  4694. "name: \"foo.proto\" "
  4695. "dependency: \"google/protobuf/descriptor.proto\" "
  4696. "enum_type { name: \"FooEnum\" value { name: \"BAR\" number: 1 } "
  4697. " value { name: \"BAZ\" number: 2 } }"
  4698. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4699. " type: TYPE_ENUM type_name: \"FooEnum\" "
  4700. " extendee: \"google.protobuf.FileOptions\" }"
  4701. "options { uninterpreted_option { name { name_part: \"foo\" "
  4702. " is_extension: true } "
  4703. " identifier_value: \"QUUX\" } }",
  4704. "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum\" has no value "
  4705. "named \"QUUX\" for option \"foo\".\n");
  4706. }
  4707. TEST_F(ValidationErrorTest, EnumOptionValueIsSiblingEnumValueName) {
  4708. BuildDescriptorMessagesInTestPool();
  4709. BuildFileWithErrors(
  4710. "name: \"foo.proto\" "
  4711. "dependency: \"google/protobuf/descriptor.proto\" "
  4712. "enum_type { name: \"FooEnum1\" value { name: \"BAR\" number: 1 } "
  4713. " value { name: \"BAZ\" number: 2 } }"
  4714. "enum_type { name: \"FooEnum2\" value { name: \"QUX\" number: 1 } "
  4715. " value { name: \"QUUX\" number: 2 } }"
  4716. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4717. " type: TYPE_ENUM type_name: \"FooEnum1\" "
  4718. " extendee: \"google.protobuf.FileOptions\" }"
  4719. "options { uninterpreted_option { name { name_part: \"foo\" "
  4720. " is_extension: true } "
  4721. " identifier_value: \"QUUX\" } }",
  4722. "foo.proto: foo.proto: OPTION_VALUE: Enum type \"FooEnum1\" has no value "
  4723. "named \"QUUX\" for option \"foo\". This appears to be a value from a "
  4724. "sibling type.\n");
  4725. }
  4726. TEST_F(ValidationErrorTest, StringOptionValueIsNotString) {
  4727. BuildDescriptorMessagesInTestPool();
  4728. BuildFileWithErrors(
  4729. "name: \"foo.proto\" "
  4730. "dependency: \"google/protobuf/descriptor.proto\" "
  4731. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4732. " type: TYPE_STRING extendee: \"google.protobuf.FileOptions\" }"
  4733. "options { uninterpreted_option { name { name_part: \"foo\" "
  4734. " is_extension: true } "
  4735. " identifier_value: \"QUUX\" } }",
  4736. "foo.proto: foo.proto: OPTION_VALUE: Value must be quoted string "
  4737. "for "
  4738. "string option \"foo\".\n");
  4739. }
  4740. TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) {
  4741. BuildFileWithErrors(
  4742. "name: \"foo.proto\" "
  4743. "package: \"foo\" "
  4744. "message_type {"
  4745. " name: \"Foo\""
  4746. " extension_range { start: 10 end: 20 }"
  4747. "}"
  4748. "extension {"
  4749. " name: \"value\""
  4750. " number: 10"
  4751. " label: LABEL_OPTIONAL"
  4752. " type: TYPE_INT32"
  4753. " extendee: \"foo.Foo\""
  4754. " json_name: \"myName\""
  4755. "}",
  4756. "foo.proto: foo.value: OPTION_NAME: option json_name is not allowed on "
  4757. "extension fields.\n");
  4758. }
  4759. TEST_F(ValidationErrorTest, DuplicateExtensionFieldNumber) {
  4760. BuildDescriptorMessagesInTestPool();
  4761. BuildFile(
  4762. "name: \"foo.proto\" "
  4763. "dependency: \"google/protobuf/descriptor.proto\" "
  4764. "extension { name: \"option1\" number: 1000 label: LABEL_OPTIONAL "
  4765. " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }");
  4766. BuildFileWithWarnings(
  4767. "name: \"bar.proto\" "
  4768. "dependency: \"google/protobuf/descriptor.proto\" "
  4769. "extension { name: \"option2\" number: 1000 label: LABEL_OPTIONAL "
  4770. " type: TYPE_INT32 extendee: \"google.protobuf.FileOptions\" }",
  4771. "bar.proto: option2: NUMBER: Extension number 1000 has already been used "
  4772. "in \"google.protobuf.FileOptions\" by extension \"option1\" defined in "
  4773. "foo.proto.\n");
  4774. }
  4775. // Helper function for tests that check for aggregate value parsing
  4776. // errors. The "value" argument is embedded inside the
  4777. // "uninterpreted_option" portion of the result.
  4778. static std::string EmbedAggregateValue(const char* value) {
  4779. return strings::Substitute(
  4780. "name: \"foo.proto\" "
  4781. "dependency: \"google/protobuf/descriptor.proto\" "
  4782. "message_type { name: \"Foo\" } "
  4783. "extension { name: \"foo\" number: 7672757 label: LABEL_OPTIONAL "
  4784. " type: TYPE_MESSAGE type_name: \"Foo\" "
  4785. " extendee: \"google.protobuf.FileOptions\" }"
  4786. "options { uninterpreted_option { name { name_part: \"foo\" "
  4787. " is_extension: true } "
  4788. " $0 } }",
  4789. value);
  4790. }
  4791. TEST_F(ValidationErrorTest, AggregateValueNotFound) {
  4792. BuildDescriptorMessagesInTestPool();
  4793. BuildFileWithErrors(
  4794. EmbedAggregateValue("string_value: \"\""),
  4795. "foo.proto: foo.proto: OPTION_VALUE: Option \"foo\" is a message. "
  4796. "To set the entire message, use syntax like "
  4797. "\"foo = { <proto text format> }\". To set fields within it, use "
  4798. "syntax like \"foo.foo = value\".\n");
  4799. }
  4800. TEST_F(ValidationErrorTest, AggregateValueParseError) {
  4801. BuildDescriptorMessagesInTestPool();
  4802. BuildFileWithErrors(
  4803. EmbedAggregateValue("aggregate_value: \"1+2\""),
  4804. "foo.proto: foo.proto: OPTION_VALUE: Error while parsing option "
  4805. "value for \"foo\": Expected identifier, got: 1\n");
  4806. }
  4807. TEST_F(ValidationErrorTest, AggregateValueUnknownFields) {
  4808. BuildDescriptorMessagesInTestPool();
  4809. BuildFileWithErrors(
  4810. EmbedAggregateValue("aggregate_value: \"x:100\""),
  4811. "foo.proto: foo.proto: OPTION_VALUE: Error while parsing option "
  4812. "value for \"foo\": Message type \"Foo\" has no field named \"x\".\n");
  4813. }
  4814. TEST_F(ValidationErrorTest, NotLiteImportsLite) {
  4815. BuildFile(
  4816. "name: \"bar.proto\" "
  4817. "options { optimize_for: LITE_RUNTIME } ");
  4818. BuildFileWithErrors(
  4819. "name: \"foo.proto\" "
  4820. "dependency: \"bar.proto\" ",
  4821. "foo.proto: bar.proto: IMPORT: Files that do not use optimize_for = "
  4822. "LITE_RUNTIME cannot import files which do use this option. This file "
  4823. "is not lite, but it imports \"bar.proto\" which is.\n");
  4824. }
  4825. TEST_F(ValidationErrorTest, LiteExtendsNotLite) {
  4826. BuildFile(
  4827. "name: \"bar.proto\" "
  4828. "message_type: {"
  4829. " name: \"Bar\""
  4830. " extension_range { start: 1 end: 1000 }"
  4831. "}");
  4832. BuildFileWithErrors(
  4833. "name: \"foo.proto\" "
  4834. "dependency: \"bar.proto\" "
  4835. "options { optimize_for: LITE_RUNTIME } "
  4836. "extension { name: \"ext\" number: 123 label: LABEL_OPTIONAL "
  4837. " type: TYPE_INT32 extendee: \"Bar\" }",
  4838. "foo.proto: ext: EXTENDEE: Extensions to non-lite types can only be "
  4839. "declared in non-lite files. Note that you cannot extend a non-lite "
  4840. "type to contain a lite type, but the reverse is allowed.\n");
  4841. }
  4842. TEST_F(ValidationErrorTest, NoLiteServices) {
  4843. BuildFileWithErrors(
  4844. "name: \"foo.proto\" "
  4845. "options {"
  4846. " optimize_for: LITE_RUNTIME"
  4847. " cc_generic_services: true"
  4848. " java_generic_services: true"
  4849. "} "
  4850. "service { name: \"Foo\" }",
  4851. "foo.proto: Foo: NAME: Files with optimize_for = LITE_RUNTIME cannot "
  4852. "define services unless you set both options cc_generic_services and "
  4853. "java_generic_services to false.\n");
  4854. BuildFile(
  4855. "name: \"bar.proto\" "
  4856. "options {"
  4857. " optimize_for: LITE_RUNTIME"
  4858. " cc_generic_services: false"
  4859. " java_generic_services: false"
  4860. "} "
  4861. "service { name: \"Bar\" }");
  4862. }
  4863. TEST_F(ValidationErrorTest, RollbackAfterError) {
  4864. // Build a file which contains every kind of construct but references an
  4865. // undefined type. All these constructs will be added to the symbol table
  4866. // before the undefined type error is noticed. The DescriptorPool will then
  4867. // have to roll everything back.
  4868. BuildFileWithErrors(
  4869. "name: \"foo.proto\" "
  4870. "message_type {"
  4871. " name: \"TestMessage\""
  4872. " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }"
  4873. "} "
  4874. "enum_type {"
  4875. " name: \"TestEnum\""
  4876. " value { name:\"BAR\" number:1 }"
  4877. "} "
  4878. "service {"
  4879. " name: \"TestService\""
  4880. " method {"
  4881. " name: \"Baz\""
  4882. " input_type: \"NoSuchType\"" // error
  4883. " output_type: \"TestMessage\""
  4884. " }"
  4885. "}",
  4886. "foo.proto: TestService.Baz: INPUT_TYPE: \"NoSuchType\" is not defined.\n"
  4887. );
  4888. // Make sure that if we build the same file again with the error fixed,
  4889. // it works. If the above rollback was incomplete, then some symbols will
  4890. // be left defined, and this second attempt will fail since it tries to
  4891. // re-define the same symbols.
  4892. BuildFile(
  4893. "name: \"foo.proto\" "
  4894. "message_type {"
  4895. " name: \"TestMessage\""
  4896. " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }"
  4897. "} "
  4898. "enum_type {"
  4899. " name: \"TestEnum\""
  4900. " value { name:\"BAR\" number:1 }"
  4901. "} "
  4902. "service {"
  4903. " name: \"TestService\""
  4904. " method { name:\"Baz\""
  4905. " input_type:\"TestMessage\""
  4906. " output_type:\"TestMessage\" }"
  4907. "}");
  4908. }
  4909. TEST_F(ValidationErrorTest, ErrorsReportedToLogError) {
  4910. // Test that errors are reported to GOOGLE_LOG(ERROR) if no error collector is
  4911. // provided.
  4912. FileDescriptorProto file_proto;
  4913. ASSERT_TRUE(
  4914. TextFormat::ParseFromString("name: \"foo.proto\" "
  4915. "message_type { name: \"Foo\" } "
  4916. "message_type { name: \"Foo\" } ",
  4917. &file_proto));
  4918. std::vector<std::string> errors;
  4919. {
  4920. ScopedMemoryLog log;
  4921. EXPECT_TRUE(pool_.BuildFile(file_proto) == nullptr);
  4922. errors = log.GetMessages(ERROR);
  4923. }
  4924. ASSERT_EQ(2, errors.size());
  4925. EXPECT_EQ("Invalid proto descriptor for file \"foo.proto\":", errors[0]);
  4926. EXPECT_EQ(" Foo: \"Foo\" is already defined.", errors[1]);
  4927. }
  4928. TEST_F(ValidationErrorTest, DisallowEnumAlias) {
  4929. BuildFileWithErrors(
  4930. "name: \"foo.proto\" "
  4931. "enum_type {"
  4932. " name: \"Bar\""
  4933. " value { name:\"ENUM_A\" number:0 }"
  4934. " value { name:\"ENUM_B\" number:0 }"
  4935. "}",
  4936. "foo.proto: Bar: NUMBER: "
  4937. "\"ENUM_B\" uses the same enum value as \"ENUM_A\". "
  4938. "If this is intended, set 'option allow_alias = true;' to the enum "
  4939. "definition.\n");
  4940. }
  4941. TEST_F(ValidationErrorTest, AllowEnumAlias) {
  4942. BuildFile(
  4943. "name: \"foo.proto\" "
  4944. "enum_type {"
  4945. " name: \"Bar\""
  4946. " value { name:\"ENUM_A\" number:0 }"
  4947. " value { name:\"ENUM_B\" number:0 }"
  4948. " options { allow_alias: true }"
  4949. "}");
  4950. }
  4951. TEST_F(ValidationErrorTest, UnusedImportWarning) {
  4952. pool_.AddUnusedImportTrackFile("bar.proto");
  4953. BuildFile(
  4954. "name: \"bar.proto\" "
  4955. "message_type { name: \"Bar\" }");
  4956. pool_.AddUnusedImportTrackFile("base.proto");
  4957. BuildFile(
  4958. "name: \"base.proto\" "
  4959. "message_type { name: \"Base\" }");
  4960. pool_.AddUnusedImportTrackFile("baz.proto");
  4961. BuildFile(
  4962. "name: \"baz.proto\" "
  4963. "message_type { name: \"Baz\" }");
  4964. pool_.AddUnusedImportTrackFile("public.proto");
  4965. BuildFile(
  4966. "name: \"public.proto\" "
  4967. "dependency: \"bar.proto\""
  4968. "public_dependency: 0");
  4969. // // forward.proto
  4970. // import "base.proto" // No warning: Base message is used.
  4971. // import "bar.proto" // Will log a warning.
  4972. // import public "baz.proto" // No warning: Do not track import public.
  4973. // import "public.proto" // No warning: public.proto has import public.
  4974. // message Forward {
  4975. // optional Base base = 1;
  4976. // }
  4977. //
  4978. pool_.AddUnusedImportTrackFile("forward.proto");
  4979. BuildFileWithWarnings(
  4980. "name: \"forward.proto\""
  4981. "dependency: \"base.proto\""
  4982. "dependency: \"bar.proto\""
  4983. "dependency: \"baz.proto\""
  4984. "dependency: \"public.proto\""
  4985. "public_dependency: 2 "
  4986. "message_type {"
  4987. " name: \"Forward\""
  4988. " field { name:\"base\" number:1 label:LABEL_OPTIONAL "
  4989. "type_name:\"Base\" }"
  4990. "}",
  4991. "forward.proto: bar.proto: IMPORT: Import bar.proto but not used.\n");
  4992. }
  4993. namespace {
  4994. void FillValidMapEntry(FileDescriptorProto* file_proto) {
  4995. ASSERT_TRUE(TextFormat::ParseFromString(
  4996. "name: 'foo.proto' "
  4997. "message_type { "
  4998. " name: 'Foo' "
  4999. " field { "
  5000. " name: 'foo_map' number: 1 label:LABEL_REPEATED "
  5001. " type_name: 'FooMapEntry' "
  5002. " } "
  5003. " nested_type { "
  5004. " name: 'FooMapEntry' "
  5005. " options { map_entry: true } "
  5006. " field { "
  5007. " name: 'key' number: 1 type:TYPE_INT32 label:LABEL_OPTIONAL "
  5008. " } "
  5009. " field { "
  5010. " name: 'value' number: 2 type:TYPE_INT32 label:LABEL_OPTIONAL "
  5011. " } "
  5012. " } "
  5013. "} "
  5014. "message_type { "
  5015. " name: 'Bar' "
  5016. " extension_range { start: 1 end: 10 }"
  5017. "} ",
  5018. file_proto));
  5019. }
  5020. static const char* kMapEntryErrorMessage =
  5021. "foo.proto: Foo.foo_map: TYPE: map_entry should not be set explicitly. "
  5022. "Use map<KeyType, ValueType> instead.\n";
  5023. static const char* kMapEntryKeyTypeErrorMessage =
  5024. "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot be float/double, "
  5025. "bytes or message types.\n";
  5026. } // namespace
  5027. TEST_F(ValidationErrorTest, MapEntryBase) {
  5028. FileDescriptorProto file_proto;
  5029. FillValidMapEntry(&file_proto);
  5030. BuildFile(file_proto.DebugString());
  5031. }
  5032. TEST_F(ValidationErrorTest, MapEntryExtensionRange) {
  5033. FileDescriptorProto file_proto;
  5034. FillValidMapEntry(&file_proto);
  5035. TextFormat::MergeFromString(
  5036. "extension_range { "
  5037. " start: 10 end: 20 "
  5038. "} ",
  5039. file_proto.mutable_message_type(0)->mutable_nested_type(0));
  5040. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5041. }
  5042. TEST_F(ValidationErrorTest, MapEntryExtension) {
  5043. FileDescriptorProto file_proto;
  5044. FillValidMapEntry(&file_proto);
  5045. TextFormat::MergeFromString(
  5046. "extension { "
  5047. " name: 'foo_ext' extendee: '.Bar' number: 5"
  5048. "} ",
  5049. file_proto.mutable_message_type(0)->mutable_nested_type(0));
  5050. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5051. }
  5052. TEST_F(ValidationErrorTest, MapEntryNestedType) {
  5053. FileDescriptorProto file_proto;
  5054. FillValidMapEntry(&file_proto);
  5055. TextFormat::MergeFromString(
  5056. "nested_type { "
  5057. " name: 'Bar' "
  5058. "} ",
  5059. file_proto.mutable_message_type(0)->mutable_nested_type(0));
  5060. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5061. }
  5062. TEST_F(ValidationErrorTest, MapEntryEnumTypes) {
  5063. FileDescriptorProto file_proto;
  5064. FillValidMapEntry(&file_proto);
  5065. TextFormat::MergeFromString(
  5066. "enum_type { "
  5067. " name: 'BarEnum' "
  5068. " value { name: 'BAR_BAR' number:0 } "
  5069. "} ",
  5070. file_proto.mutable_message_type(0)->mutable_nested_type(0));
  5071. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5072. }
  5073. TEST_F(ValidationErrorTest, MapEntryExtraField) {
  5074. FileDescriptorProto file_proto;
  5075. FillValidMapEntry(&file_proto);
  5076. TextFormat::MergeFromString(
  5077. "field { "
  5078. " name: 'other_field' "
  5079. " label: LABEL_OPTIONAL "
  5080. " type: TYPE_INT32 "
  5081. " number: 3 "
  5082. "} ",
  5083. file_proto.mutable_message_type(0)->mutable_nested_type(0));
  5084. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5085. }
  5086. TEST_F(ValidationErrorTest, MapEntryMessageName) {
  5087. FileDescriptorProto file_proto;
  5088. FillValidMapEntry(&file_proto);
  5089. file_proto.mutable_message_type(0)->mutable_nested_type(0)->set_name(
  5090. "OtherMapEntry");
  5091. file_proto.mutable_message_type(0)->mutable_field(0)->set_type_name(
  5092. "OtherMapEntry");
  5093. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5094. }
  5095. TEST_F(ValidationErrorTest, MapEntryNoneRepeatedMapEntry) {
  5096. FileDescriptorProto file_proto;
  5097. FillValidMapEntry(&file_proto);
  5098. file_proto.mutable_message_type(0)->mutable_field(0)->set_label(
  5099. FieldDescriptorProto::LABEL_OPTIONAL);
  5100. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5101. }
  5102. TEST_F(ValidationErrorTest, MapEntryDifferentContainingType) {
  5103. FileDescriptorProto file_proto;
  5104. FillValidMapEntry(&file_proto);
  5105. // Move the nested MapEntry message into the top level, which should not pass
  5106. // the validation.
  5107. file_proto.mutable_message_type()->AddAllocated(
  5108. file_proto.mutable_message_type(0)->mutable_nested_type()->ReleaseLast());
  5109. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5110. }
  5111. TEST_F(ValidationErrorTest, MapEntryKeyName) {
  5112. FileDescriptorProto file_proto;
  5113. FillValidMapEntry(&file_proto);
  5114. FieldDescriptorProto* key =
  5115. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5116. 0);
  5117. key->set_name("Key");
  5118. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5119. }
  5120. TEST_F(ValidationErrorTest, MapEntryKeyLabel) {
  5121. FileDescriptorProto file_proto;
  5122. FillValidMapEntry(&file_proto);
  5123. FieldDescriptorProto* key =
  5124. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5125. 0);
  5126. key->set_label(FieldDescriptorProto::LABEL_REQUIRED);
  5127. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5128. }
  5129. TEST_F(ValidationErrorTest, MapEntryKeyNumber) {
  5130. FileDescriptorProto file_proto;
  5131. FillValidMapEntry(&file_proto);
  5132. FieldDescriptorProto* key =
  5133. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5134. 0);
  5135. key->set_number(3);
  5136. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5137. }
  5138. TEST_F(ValidationErrorTest, MapEntryValueName) {
  5139. FileDescriptorProto file_proto;
  5140. FillValidMapEntry(&file_proto);
  5141. FieldDescriptorProto* value =
  5142. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5143. 1);
  5144. value->set_name("Value");
  5145. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5146. }
  5147. TEST_F(ValidationErrorTest, MapEntryValueLabel) {
  5148. FileDescriptorProto file_proto;
  5149. FillValidMapEntry(&file_proto);
  5150. FieldDescriptorProto* value =
  5151. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5152. 1);
  5153. value->set_label(FieldDescriptorProto::LABEL_REQUIRED);
  5154. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5155. }
  5156. TEST_F(ValidationErrorTest, MapEntryValueNumber) {
  5157. FileDescriptorProto file_proto;
  5158. FillValidMapEntry(&file_proto);
  5159. FieldDescriptorProto* value =
  5160. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5161. 1);
  5162. value->set_number(3);
  5163. BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
  5164. }
  5165. TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) {
  5166. FileDescriptorProto file_proto;
  5167. FillValidMapEntry(&file_proto);
  5168. FieldDescriptorProto* key =
  5169. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5170. 0);
  5171. key->set_type(FieldDescriptorProto::TYPE_FLOAT);
  5172. BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
  5173. }
  5174. TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) {
  5175. FileDescriptorProto file_proto;
  5176. FillValidMapEntry(&file_proto);
  5177. FieldDescriptorProto* key =
  5178. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5179. 0);
  5180. key->set_type(FieldDescriptorProto::TYPE_DOUBLE);
  5181. BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
  5182. }
  5183. TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) {
  5184. FileDescriptorProto file_proto;
  5185. FillValidMapEntry(&file_proto);
  5186. FieldDescriptorProto* key =
  5187. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5188. 0);
  5189. key->set_type(FieldDescriptorProto::TYPE_BYTES);
  5190. BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
  5191. }
  5192. TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) {
  5193. FileDescriptorProto file_proto;
  5194. FillValidMapEntry(&file_proto);
  5195. FieldDescriptorProto* key =
  5196. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5197. 0);
  5198. key->clear_type();
  5199. key->set_type_name("BarEnum");
  5200. EnumDescriptorProto* enum_proto = file_proto.add_enum_type();
  5201. enum_proto->set_name("BarEnum");
  5202. EnumValueDescriptorProto* enum_value_proto = enum_proto->add_value();
  5203. enum_value_proto->set_name("BAR_VALUE0");
  5204. enum_value_proto->set_number(0);
  5205. BuildFileWithErrors(file_proto.DebugString(),
  5206. "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot "
  5207. "be enum types.\n");
  5208. // Enum keys are not allowed in proto3 as well.
  5209. // Get rid of extensions for proto3 to make it proto3 compatible.
  5210. file_proto.mutable_message_type()->RemoveLast();
  5211. file_proto.set_syntax("proto3");
  5212. BuildFileWithErrors(file_proto.DebugString(),
  5213. "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot "
  5214. "be enum types.\n");
  5215. }
  5216. TEST_F(ValidationErrorTest, MapEntryKeyTypeMessage) {
  5217. FileDescriptorProto file_proto;
  5218. FillValidMapEntry(&file_proto);
  5219. FieldDescriptorProto* key =
  5220. file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
  5221. 0);
  5222. key->clear_type();
  5223. key->set_type_name(".Bar");
  5224. BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
  5225. }
  5226. TEST_F(ValidationErrorTest, MapEntryConflictsWithField) {
  5227. FileDescriptorProto file_proto;
  5228. FillValidMapEntry(&file_proto);
  5229. TextFormat::MergeFromString(
  5230. "field { "
  5231. " name: 'FooMapEntry' "
  5232. " type: TYPE_INT32 "
  5233. " label: LABEL_OPTIONAL "
  5234. " number: 100 "
  5235. "}",
  5236. file_proto.mutable_message_type(0));
  5237. BuildFileWithErrors(
  5238. file_proto.DebugString(),
  5239. "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
  5240. "\"Foo\".\n"
  5241. "foo.proto: Foo.foo_map: TYPE: \"FooMapEntry\" is not defined.\n"
  5242. "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
  5243. "with an existing field.\n");
  5244. }
  5245. TEST_F(ValidationErrorTest, MapEntryConflictsWithMessage) {
  5246. FileDescriptorProto file_proto;
  5247. FillValidMapEntry(&file_proto);
  5248. TextFormat::MergeFromString(
  5249. "nested_type { "
  5250. " name: 'FooMapEntry' "
  5251. "}",
  5252. file_proto.mutable_message_type(0));
  5253. BuildFileWithErrors(
  5254. file_proto.DebugString(),
  5255. "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
  5256. "\"Foo\".\n"
  5257. "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
  5258. "with an existing nested message type.\n");
  5259. }
  5260. TEST_F(ValidationErrorTest, MapEntryConflictsWithEnum) {
  5261. FileDescriptorProto file_proto;
  5262. FillValidMapEntry(&file_proto);
  5263. TextFormat::MergeFromString(
  5264. "enum_type { "
  5265. " name: 'FooMapEntry' "
  5266. " value { name: 'ENTRY_FOO' number: 0 }"
  5267. "}",
  5268. file_proto.mutable_message_type(0));
  5269. BuildFileWithErrors(
  5270. file_proto.DebugString(),
  5271. "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
  5272. "\"Foo\".\n"
  5273. "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
  5274. "with an existing enum type.\n");
  5275. }
  5276. TEST_F(ValidationErrorTest, EnumValuesConflictWithDifferentCasing) {
  5277. BuildFileWithErrors(
  5278. "syntax: 'proto3'"
  5279. "name: 'foo.proto' "
  5280. "enum_type {"
  5281. " name: 'FooEnum' "
  5282. " value { name: 'BAR' number: 0 }"
  5283. " value { name: 'bar' number: 1 }"
  5284. "}",
  5285. "foo.proto: bar: NAME: Enum name bar has the same name as BAR "
  5286. "if you ignore case and strip out the enum name prefix (if any). "
  5287. "This is error-prone and can lead to undefined behavior. "
  5288. "Please avoid doing this. If you are using allow_alias, please assign "
  5289. "the same numeric value to both enums.\n");
  5290. // Not an error because both enums are mapped to the same value.
  5291. BuildFile(
  5292. "syntax: 'proto3'"
  5293. "name: 'foo.proto' "
  5294. "enum_type {"
  5295. " name: 'FooEnum' "
  5296. " options { allow_alias: true }"
  5297. " value { name: 'UNKNOWN' number: 0 }"
  5298. " value { name: 'BAR' number: 1 }"
  5299. " value { name: 'bar' number: 1 }"
  5300. "}");
  5301. }
  5302. TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) {
  5303. BuildFileWithErrors(
  5304. "syntax: 'proto3'"
  5305. "name: 'foo.proto' "
  5306. "enum_type {"
  5307. " name: 'FooEnum' "
  5308. " value { name: 'FOO_ENUM_BAZ' number: 0 }"
  5309. " value { name: 'BAZ' number: 1 }"
  5310. "}",
  5311. "foo.proto: BAZ: NAME: Enum name BAZ has the same name as FOO_ENUM_BAZ "
  5312. "if you ignore case and strip out the enum name prefix (if any). "
  5313. "This is error-prone and can lead to undefined behavior. "
  5314. "Please avoid doing this. If you are using allow_alias, please assign "
  5315. "the same numeric value to both enums.\n");
  5316. BuildFileWithErrors(
  5317. "syntax: 'proto3'"
  5318. "name: 'foo.proto' "
  5319. "enum_type {"
  5320. " name: 'FooEnum' "
  5321. " value { name: 'FOOENUM_BAZ' number: 0 }"
  5322. " value { name: 'BAZ' number: 1 }"
  5323. "}",
  5324. "foo.proto: BAZ: NAME: Enum name BAZ has the same name as FOOENUM_BAZ "
  5325. "if you ignore case and strip out the enum name prefix (if any). "
  5326. "This is error-prone and can lead to undefined behavior. "
  5327. "Please avoid doing this. If you are using allow_alias, please assign "
  5328. "the same numeric value to both enums.\n");
  5329. BuildFileWithErrors(
  5330. "syntax: 'proto3'"
  5331. "name: 'foo.proto' "
  5332. "enum_type {"
  5333. " name: 'FooEnum' "
  5334. " value { name: 'FOO_ENUM_BAR_BAZ' number: 0 }"
  5335. " value { name: 'BAR__BAZ' number: 1 }"
  5336. "}",
  5337. "foo.proto: BAR__BAZ: NAME: Enum name BAR__BAZ has the same name as "
  5338. "FOO_ENUM_BAR_BAZ if you ignore case and strip out the enum name prefix "
  5339. "(if any). This is error-prone and can lead to undefined behavior. "
  5340. "Please avoid doing this. If you are using allow_alias, please assign "
  5341. "the same numeric value to both enums.\n");
  5342. BuildFileWithErrors(
  5343. "syntax: 'proto3'"
  5344. "name: 'foo.proto' "
  5345. "enum_type {"
  5346. " name: 'FooEnum' "
  5347. " value { name: 'FOO_ENUM__BAR_BAZ' number: 0 }"
  5348. " value { name: 'BAR_BAZ' number: 1 }"
  5349. "}",
  5350. "foo.proto: BAR_BAZ: NAME: Enum name BAR_BAZ has the same name as "
  5351. "FOO_ENUM__BAR_BAZ if you ignore case and strip out the enum name prefix "
  5352. "(if any). This is error-prone and can lead to undefined behavior. "
  5353. "Please avoid doing this. If you are using allow_alias, please assign "
  5354. "the same numeric value to both enums.\n");
  5355. // This isn't an error because the underscore will cause the PascalCase to
  5356. // differ by case (BarBaz vs. Barbaz).
  5357. BuildFile(
  5358. "syntax: 'proto3'"
  5359. "name: 'foo.proto' "
  5360. "enum_type {"
  5361. " name: 'FooEnum' "
  5362. " value { name: 'BAR_BAZ' number: 0 }"
  5363. " value { name: 'BARBAZ' number: 1 }"
  5364. "}");
  5365. }
  5366. TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) {
  5367. FileDescriptorProto file_proto;
  5368. FillValidMapEntry(&file_proto);
  5369. TextFormat::MergeFromString(
  5370. "oneof_decl { "
  5371. " name: 'FooMapEntry' "
  5372. "}"
  5373. "field { "
  5374. " name: 'int_field' "
  5375. " type: TYPE_INT32 "
  5376. " label: LABEL_OPTIONAL "
  5377. " oneof_index: 0 "
  5378. " number: 100 "
  5379. "} ",
  5380. file_proto.mutable_message_type(0));
  5381. BuildFileWithErrors(
  5382. file_proto.DebugString(),
  5383. "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
  5384. "\"Foo\".\n"
  5385. "foo.proto: Foo.foo_map: TYPE: \"FooMapEntry\" is not defined.\n"
  5386. "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
  5387. "with an existing oneof type.\n");
  5388. }
  5389. TEST_F(ValidationErrorTest, MapEntryUsesNoneZeroEnumDefaultValue) {
  5390. BuildFileWithErrors(
  5391. "name: \"foo.proto\" "
  5392. "enum_type {"
  5393. " name: \"Bar\""
  5394. " value { name:\"ENUM_A\" number:1 }"
  5395. " value { name:\"ENUM_B\" number:2 }"
  5396. "}"
  5397. "message_type {"
  5398. " name: 'Foo' "
  5399. " field { "
  5400. " name: 'foo_map' number: 1 label:LABEL_REPEATED "
  5401. " type_name: 'FooMapEntry' "
  5402. " } "
  5403. " nested_type { "
  5404. " name: 'FooMapEntry' "
  5405. " options { map_entry: true } "
  5406. " field { "
  5407. " name: 'key' number: 1 type:TYPE_INT32 label:LABEL_OPTIONAL "
  5408. " } "
  5409. " field { "
  5410. " name: 'value' number: 2 type_name:\"Bar\" label:LABEL_OPTIONAL "
  5411. " } "
  5412. " } "
  5413. "}",
  5414. "foo.proto: Foo.foo_map: "
  5415. "TYPE: Enum value in map must define 0 as the first value.\n");
  5416. }
  5417. TEST_F(ValidationErrorTest, Proto3RequiredFields) {
  5418. BuildFileWithErrors(
  5419. "name: 'foo.proto' "
  5420. "syntax: 'proto3' "
  5421. "message_type { "
  5422. " name: 'Foo' "
  5423. " field { name:'foo' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } "
  5424. "}",
  5425. "foo.proto: Foo.foo: TYPE: Required fields are not allowed in "
  5426. "proto3.\n");
  5427. // applied to nested types as well.
  5428. BuildFileWithErrors(
  5429. "name: 'foo.proto' "
  5430. "syntax: 'proto3' "
  5431. "message_type { "
  5432. " name: 'Foo' "
  5433. " nested_type { "
  5434. " name : 'Bar' "
  5435. " field { name:'bar' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } "
  5436. " } "
  5437. "}",
  5438. "foo.proto: Foo.Bar.bar: TYPE: Required fields are not allowed in "
  5439. "proto3.\n");
  5440. // optional and repeated fields are OK.
  5441. BuildFile(
  5442. "name: 'foo.proto' "
  5443. "syntax: 'proto3' "
  5444. "message_type { "
  5445. " name: 'Foo' "
  5446. " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } "
  5447. " field { name:'bar' number:2 label:LABEL_REPEATED type:TYPE_INT32 } "
  5448. "}");
  5449. }
  5450. TEST_F(ValidationErrorTest, ValidateProto3DefaultValue) {
  5451. BuildFileWithErrors(
  5452. "name: 'foo.proto' "
  5453. "syntax: 'proto3' "
  5454. "message_type { "
  5455. " name: 'Foo' "
  5456. " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  5457. " default_value: '1' }"
  5458. "}",
  5459. "foo.proto: Foo.foo: DEFAULT_VALUE: Explicit default values are not "
  5460. "allowed in proto3.\n");
  5461. BuildFileWithErrors(
  5462. "name: 'foo.proto' "
  5463. "syntax: 'proto3' "
  5464. "message_type { "
  5465. " name: 'Foo' "
  5466. " nested_type { "
  5467. " name : 'Bar' "
  5468. " field { name:'bar' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 "
  5469. " default_value: '1' }"
  5470. " } "
  5471. "}",
  5472. "foo.proto: Foo.Bar.bar: DEFAULT_VALUE: Explicit default values are not "
  5473. "allowed in proto3.\n");
  5474. }
  5475. TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) {
  5476. BuildFileWithErrors(
  5477. "name: 'foo.proto' "
  5478. "syntax: 'proto3' "
  5479. "message_type { "
  5480. " name: 'Foo' "
  5481. " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } "
  5482. " extension_range { start:10 end:100 } "
  5483. "}",
  5484. "foo.proto: Foo: NUMBER: Extension ranges are not allowed in "
  5485. "proto3.\n");
  5486. BuildFileWithErrors(
  5487. "name: 'foo.proto' "
  5488. "syntax: 'proto3' "
  5489. "message_type { "
  5490. " name: 'Foo' "
  5491. " nested_type { "
  5492. " name : 'Bar' "
  5493. " field { name:'bar' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } "
  5494. " extension_range { start:10 end:100 } "
  5495. " } "
  5496. "}",
  5497. "foo.proto: Foo.Bar: NUMBER: Extension ranges are not allowed in "
  5498. "proto3.\n");
  5499. }
  5500. TEST_F(ValidationErrorTest, ValidateProto3MessageSetWireFormat) {
  5501. BuildFileWithErrors(
  5502. "name: 'foo.proto' "
  5503. "syntax: 'proto3' "
  5504. "message_type { "
  5505. " name: 'Foo' "
  5506. " options { message_set_wire_format: true } "
  5507. "}",
  5508. "foo.proto: Foo: NAME: MessageSet is not supported "
  5509. "in proto3.\n");
  5510. }
  5511. TEST_F(ValidationErrorTest, ValidateProto3Enum) {
  5512. BuildFileWithErrors(
  5513. "name: 'foo.proto' "
  5514. "syntax: 'proto3' "
  5515. "enum_type { "
  5516. " name: 'FooEnum' "
  5517. " value { name: 'FOO_FOO' number:1 } "
  5518. "}",
  5519. "foo.proto: FooEnum: NUMBER: The first enum value must be "
  5520. "zero in proto3.\n");
  5521. BuildFileWithErrors(
  5522. "name: 'foo.proto' "
  5523. "syntax: 'proto3' "
  5524. "message_type { "
  5525. " name: 'Foo' "
  5526. " enum_type { "
  5527. " name: 'FooEnum' "
  5528. " value { name: 'FOO_FOO' number:1 } "
  5529. " } "
  5530. "}",
  5531. "foo.proto: Foo.FooEnum: NUMBER: The first enum value must be "
  5532. "zero in proto3.\n");
  5533. // valid case.
  5534. BuildFile(
  5535. "name: 'foo.proto' "
  5536. "syntax: 'proto3' "
  5537. "enum_type { "
  5538. " name: 'FooEnum' "
  5539. " value { name: 'FOO_FOO' number:0 } "
  5540. "}");
  5541. }
  5542. TEST_F(ValidationErrorTest, ValidateProto3Group) {
  5543. BuildFileWithErrors(
  5544. "name: 'foo.proto' "
  5545. "syntax: 'proto3' "
  5546. "message_type { "
  5547. " name: 'Foo' "
  5548. " nested_type { "
  5549. " name: 'FooGroup' "
  5550. " } "
  5551. " field { name:'foo_group' number: 1 label:LABEL_OPTIONAL "
  5552. " type: TYPE_GROUP type_name:'FooGroup' } "
  5553. "}",
  5554. "foo.proto: Foo.foo_group: TYPE: Groups are not supported in proto3 "
  5555. "syntax.\n");
  5556. }
  5557. TEST_F(ValidationErrorTest, ValidateProto3EnumFromProto2) {
  5558. // Define an enum in a proto2 file.
  5559. BuildFile(
  5560. "name: 'foo.proto' "
  5561. "package: 'foo' "
  5562. "syntax: 'proto2' "
  5563. "enum_type { "
  5564. " name: 'FooEnum' "
  5565. " value { name: 'DEFAULT_OPTION' number:0 } "
  5566. "}");
  5567. // Now try to refer to it. (All tests in the fixture use the same pool, so we
  5568. // can refer to the enum above in this definition.)
  5569. BuildFileWithErrors(
  5570. "name: 'bar.proto' "
  5571. "dependency: 'foo.proto' "
  5572. "syntax: 'proto3' "
  5573. "message_type { "
  5574. " name: 'Foo' "
  5575. " field { name:'bar' number:1 label:LABEL_OPTIONAL type:TYPE_ENUM "
  5576. " type_name: 'foo.FooEnum' }"
  5577. "}",
  5578. "bar.proto: Foo.bar: TYPE: Enum type \"foo.FooEnum\" is not a proto3 "
  5579. "enum, but is used in \"Foo\" which is a proto3 message type.\n");
  5580. }
  5581. TEST_F(ValidationErrorTest, ValidateProto3Extension) {
  5582. // Valid for options.
  5583. DescriptorPool pool;
  5584. FileDescriptorProto file_proto;
  5585. // Add "google/protobuf/descriptor.proto".
  5586. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
  5587. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  5588. // Add "foo.proto":
  5589. // import "google/protobuf/descriptor.proto";
  5590. // extend google.protobuf.FieldOptions {
  5591. // optional int32 option1 = 1000;
  5592. // }
  5593. file_proto.Clear();
  5594. file_proto.set_name("foo.proto");
  5595. file_proto.set_syntax("proto3");
  5596. file_proto.add_dependency("google/protobuf/descriptor.proto");
  5597. AddExtension(&file_proto, "google.protobuf.FieldOptions", "option1", 1000,
  5598. FieldDescriptorProto::LABEL_OPTIONAL,
  5599. FieldDescriptorProto::TYPE_INT32);
  5600. ASSERT_TRUE(pool.BuildFile(file_proto) != nullptr);
  5601. // Copy and change the package of the descriptor.proto
  5602. BuildFile(
  5603. "name: 'google.protobuf.proto' "
  5604. "syntax: 'proto2' "
  5605. "message_type { "
  5606. " name: 'Container' extension_range { start: 1 end: 1000 } "
  5607. "}");
  5608. BuildFileWithErrors(
  5609. "name: 'bar.proto' "
  5610. "syntax: 'proto3' "
  5611. "dependency: 'google.protobuf.proto' "
  5612. "extension { "
  5613. " name: 'bar' number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 "
  5614. " extendee: 'Container' "
  5615. "}",
  5616. "bar.proto: bar: EXTENDEE: Extensions in proto3 are only allowed for "
  5617. "defining options.\n");
  5618. }
  5619. // Test that field names that may conflict in JSON is not allowed by protoc.
  5620. TEST_F(ValidationErrorTest, ValidateProto3JsonName) {
  5621. // The comparison is case-insensitive.
  5622. BuildFileWithErrors(
  5623. "name: 'foo.proto' "
  5624. "syntax: 'proto3' "
  5625. "message_type {"
  5626. " name: 'Foo'"
  5627. " field { name:'name' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  5628. " field { name:'Name' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  5629. "}",
  5630. "foo.proto: Foo: NAME: The JSON camel-case name of field \"Name\" "
  5631. "conflicts with field \"name\". This is not allowed in proto3.\n");
  5632. // Underscores are ignored.
  5633. BuildFileWithErrors(
  5634. "name: 'foo.proto' "
  5635. "syntax: 'proto3' "
  5636. "message_type {"
  5637. " name: 'Foo'"
  5638. " field { name:'ab' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  5639. " field { name:'_a__b_' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
  5640. "}",
  5641. "foo.proto: Foo: NAME: The JSON camel-case name of field \"_a__b_\" "
  5642. "conflicts with field \"ab\". This is not allowed in proto3.\n");
  5643. }
  5644. // ===================================================================
  5645. // DescriptorDatabase
  5646. static void AddToDatabase(SimpleDescriptorDatabase* database,
  5647. const char* file_text) {
  5648. FileDescriptorProto file_proto;
  5649. EXPECT_TRUE(TextFormat::ParseFromString(file_text, &file_proto));
  5650. database->Add(file_proto);
  5651. }
  5652. class DatabaseBackedPoolTest : public testing::Test {
  5653. protected:
  5654. DatabaseBackedPoolTest() {}
  5655. SimpleDescriptorDatabase database_;
  5656. virtual void SetUp() {
  5657. AddToDatabase(
  5658. &database_,
  5659. "name: 'foo.proto' "
  5660. "message_type { name:'Foo' extension_range { start: 1 end: 100 } } "
  5661. "enum_type { name:'TestEnum' value { name:'DUMMY' number:0 } } "
  5662. "service { name:'TestService' } ");
  5663. AddToDatabase(&database_,
  5664. "name: 'bar.proto' "
  5665. "dependency: 'foo.proto' "
  5666. "message_type { name:'Bar' } "
  5667. "extension { name:'foo_ext' extendee: '.Foo' number:5 "
  5668. " label:LABEL_OPTIONAL type:TYPE_INT32 } ");
  5669. // Baz has an undeclared dependency on Foo.
  5670. AddToDatabase(
  5671. &database_,
  5672. "name: 'baz.proto' "
  5673. "message_type { "
  5674. " name:'Baz' "
  5675. " field { name:'foo' number:1 label:LABEL_OPTIONAL type_name:'Foo' } "
  5676. "}");
  5677. }
  5678. // We can't inject a file containing errors into a DescriptorPool, so we
  5679. // need an actual mock DescriptorDatabase to test errors.
  5680. class ErrorDescriptorDatabase : public DescriptorDatabase {
  5681. public:
  5682. ErrorDescriptorDatabase() {}
  5683. ~ErrorDescriptorDatabase() {}
  5684. // implements DescriptorDatabase ---------------------------------
  5685. bool FindFileByName(const std::string& filename,
  5686. FileDescriptorProto* output) {
  5687. // error.proto and error2.proto cyclically import each other.
  5688. if (filename == "error.proto") {
  5689. output->Clear();
  5690. output->set_name("error.proto");
  5691. output->add_dependency("error2.proto");
  5692. return true;
  5693. } else if (filename == "error2.proto") {
  5694. output->Clear();
  5695. output->set_name("error2.proto");
  5696. output->add_dependency("error.proto");
  5697. return true;
  5698. } else {
  5699. return false;
  5700. }
  5701. }
  5702. bool FindFileContainingSymbol(const std::string& symbol_name,
  5703. FileDescriptorProto* output) {
  5704. return false;
  5705. }
  5706. bool FindFileContainingExtension(const std::string& containing_type,
  5707. int field_number,
  5708. FileDescriptorProto* output) {
  5709. return false;
  5710. }
  5711. };
  5712. // A DescriptorDatabase that counts how many times each method has been
  5713. // called and forwards to some other DescriptorDatabase.
  5714. class CallCountingDatabase : public DescriptorDatabase {
  5715. public:
  5716. CallCountingDatabase(DescriptorDatabase* wrapped_db)
  5717. : wrapped_db_(wrapped_db) {
  5718. Clear();
  5719. }
  5720. ~CallCountingDatabase() {}
  5721. DescriptorDatabase* wrapped_db_;
  5722. int call_count_;
  5723. void Clear() { call_count_ = 0; }
  5724. // implements DescriptorDatabase ---------------------------------
  5725. bool FindFileByName(const std::string& filename,
  5726. FileDescriptorProto* output) {
  5727. ++call_count_;
  5728. return wrapped_db_->FindFileByName(filename, output);
  5729. }
  5730. bool FindFileContainingSymbol(const std::string& symbol_name,
  5731. FileDescriptorProto* output) {
  5732. ++call_count_;
  5733. return wrapped_db_->FindFileContainingSymbol(symbol_name, output);
  5734. }
  5735. bool FindFileContainingExtension(const std::string& containing_type,
  5736. int field_number,
  5737. FileDescriptorProto* output) {
  5738. ++call_count_;
  5739. return wrapped_db_->FindFileContainingExtension(containing_type,
  5740. field_number, output);
  5741. }
  5742. };
  5743. // A DescriptorDatabase which falsely always returns foo.proto when searching
  5744. // for any symbol or extension number. This shouldn't cause the
  5745. // DescriptorPool to reload foo.proto if it is already loaded.
  5746. class FalsePositiveDatabase : public DescriptorDatabase {
  5747. public:
  5748. FalsePositiveDatabase(DescriptorDatabase* wrapped_db)
  5749. : wrapped_db_(wrapped_db) {}
  5750. ~FalsePositiveDatabase() {}
  5751. DescriptorDatabase* wrapped_db_;
  5752. // implements DescriptorDatabase ---------------------------------
  5753. bool FindFileByName(const std::string& filename,
  5754. FileDescriptorProto* output) {
  5755. return wrapped_db_->FindFileByName(filename, output);
  5756. }
  5757. bool FindFileContainingSymbol(const std::string& symbol_name,
  5758. FileDescriptorProto* output) {
  5759. return FindFileByName("foo.proto", output);
  5760. }
  5761. bool FindFileContainingExtension(const std::string& containing_type,
  5762. int field_number,
  5763. FileDescriptorProto* output) {
  5764. return FindFileByName("foo.proto", output);
  5765. }
  5766. };
  5767. };
  5768. TEST_F(DatabaseBackedPoolTest, FindFileByName) {
  5769. DescriptorPool pool(&database_);
  5770. const FileDescriptor* foo = pool.FindFileByName("foo.proto");
  5771. ASSERT_TRUE(foo != nullptr);
  5772. EXPECT_EQ("foo.proto", foo->name());
  5773. ASSERT_EQ(1, foo->message_type_count());
  5774. EXPECT_EQ("Foo", foo->message_type(0)->name());
  5775. EXPECT_EQ(foo, pool.FindFileByName("foo.proto"));
  5776. EXPECT_TRUE(pool.FindFileByName("no_such_file.proto") == nullptr);
  5777. }
  5778. TEST_F(DatabaseBackedPoolTest, FindDependencyBeforeDependent) {
  5779. DescriptorPool pool(&database_);
  5780. const FileDescriptor* foo = pool.FindFileByName("foo.proto");
  5781. ASSERT_TRUE(foo != nullptr);
  5782. EXPECT_EQ("foo.proto", foo->name());
  5783. ASSERT_EQ(1, foo->message_type_count());
  5784. EXPECT_EQ("Foo", foo->message_type(0)->name());
  5785. const FileDescriptor* bar = pool.FindFileByName("bar.proto");
  5786. ASSERT_TRUE(bar != nullptr);
  5787. EXPECT_EQ("bar.proto", bar->name());
  5788. ASSERT_EQ(1, bar->message_type_count());
  5789. EXPECT_EQ("Bar", bar->message_type(0)->name());
  5790. ASSERT_EQ(1, bar->dependency_count());
  5791. EXPECT_EQ(foo, bar->dependency(0));
  5792. }
  5793. TEST_F(DatabaseBackedPoolTest, FindDependentBeforeDependency) {
  5794. DescriptorPool pool(&database_);
  5795. const FileDescriptor* bar = pool.FindFileByName("bar.proto");
  5796. ASSERT_TRUE(bar != nullptr);
  5797. EXPECT_EQ("bar.proto", bar->name());
  5798. ASSERT_EQ(1, bar->message_type_count());
  5799. ASSERT_EQ("Bar", bar->message_type(0)->name());
  5800. const FileDescriptor* foo = pool.FindFileByName("foo.proto");
  5801. ASSERT_TRUE(foo != nullptr);
  5802. EXPECT_EQ("foo.proto", foo->name());
  5803. ASSERT_EQ(1, foo->message_type_count());
  5804. ASSERT_EQ("Foo", foo->message_type(0)->name());
  5805. ASSERT_EQ(1, bar->dependency_count());
  5806. EXPECT_EQ(foo, bar->dependency(0));
  5807. }
  5808. TEST_F(DatabaseBackedPoolTest, FindFileContainingSymbol) {
  5809. DescriptorPool pool(&database_);
  5810. const FileDescriptor* file = pool.FindFileContainingSymbol("Foo");
  5811. ASSERT_TRUE(file != nullptr);
  5812. EXPECT_EQ("foo.proto", file->name());
  5813. EXPECT_EQ(file, pool.FindFileByName("foo.proto"));
  5814. EXPECT_TRUE(pool.FindFileContainingSymbol("NoSuchSymbol") == nullptr);
  5815. }
  5816. TEST_F(DatabaseBackedPoolTest, FindMessageTypeByName) {
  5817. DescriptorPool pool(&database_);
  5818. const Descriptor* type = pool.FindMessageTypeByName("Foo");
  5819. ASSERT_TRUE(type != nullptr);
  5820. EXPECT_EQ("Foo", type->name());
  5821. EXPECT_EQ(type->file(), pool.FindFileByName("foo.proto"));
  5822. EXPECT_TRUE(pool.FindMessageTypeByName("NoSuchType") == nullptr);
  5823. }
  5824. TEST_F(DatabaseBackedPoolTest, FindExtensionByNumber) {
  5825. DescriptorPool pool(&database_);
  5826. const Descriptor* foo = pool.FindMessageTypeByName("Foo");
  5827. ASSERT_TRUE(foo != nullptr);
  5828. const FieldDescriptor* extension = pool.FindExtensionByNumber(foo, 5);
  5829. ASSERT_TRUE(extension != nullptr);
  5830. EXPECT_EQ("foo_ext", extension->name());
  5831. EXPECT_EQ(extension->file(), pool.FindFileByName("bar.proto"));
  5832. EXPECT_TRUE(pool.FindExtensionByNumber(foo, 12) == nullptr);
  5833. }
  5834. TEST_F(DatabaseBackedPoolTest, FindAllExtensions) {
  5835. DescriptorPool pool(&database_);
  5836. const Descriptor* foo = pool.FindMessageTypeByName("Foo");
  5837. for (int i = 0; i < 2; ++i) {
  5838. // Repeat the lookup twice, to check that we get consistent
  5839. // results despite the fallback database lookup mutating the pool.
  5840. std::vector<const FieldDescriptor*> extensions;
  5841. pool.FindAllExtensions(foo, &extensions);
  5842. ASSERT_EQ(1, extensions.size());
  5843. EXPECT_EQ(5, extensions[0]->number());
  5844. }
  5845. }
  5846. TEST_F(DatabaseBackedPoolTest, ErrorWithoutErrorCollector) {
  5847. ErrorDescriptorDatabase error_database;
  5848. DescriptorPool pool(&error_database);
  5849. std::vector<std::string> errors;
  5850. {
  5851. ScopedMemoryLog log;
  5852. EXPECT_TRUE(pool.FindFileByName("error.proto") == nullptr);
  5853. errors = log.GetMessages(ERROR);
  5854. }
  5855. EXPECT_FALSE(errors.empty());
  5856. }
  5857. TEST_F(DatabaseBackedPoolTest, ErrorWithErrorCollector) {
  5858. ErrorDescriptorDatabase error_database;
  5859. MockErrorCollector error_collector;
  5860. DescriptorPool pool(&error_database, &error_collector);
  5861. EXPECT_TRUE(pool.FindFileByName("error.proto") == nullptr);
  5862. EXPECT_EQ(
  5863. "error.proto: error2.proto: IMPORT: File recursively imports itself: "
  5864. "error.proto -> error2.proto -> error.proto\n"
  5865. "error2.proto: error.proto: IMPORT: Import \"error.proto\" was not "
  5866. "found or had errors.\n"
  5867. "error.proto: error2.proto: IMPORT: Import \"error2.proto\" was not "
  5868. "found or had errors.\n",
  5869. error_collector.text_);
  5870. }
  5871. TEST_F(DatabaseBackedPoolTest, UndeclaredDependencyOnUnbuiltType) {
  5872. // Check that we find and report undeclared dependencies on types that exist
  5873. // in the descriptor database but that have not not been built yet.
  5874. MockErrorCollector error_collector;
  5875. DescriptorPool pool(&database_, &error_collector);
  5876. EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == nullptr);
  5877. EXPECT_EQ(
  5878. "baz.proto: Baz.foo: TYPE: \"Foo\" seems to be defined in \"foo.proto\", "
  5879. "which is not imported by \"baz.proto\". To use it here, please add "
  5880. "the necessary import.\n",
  5881. error_collector.text_);
  5882. }
  5883. TEST_F(DatabaseBackedPoolTest, RollbackAfterError) {
  5884. // Make sure that all traces of bad types are removed from the pool. This used
  5885. // to be b/4529436, due to the fact that a symbol resolution failure could
  5886. // potentially cause another file to be recursively built, which would trigger
  5887. // a checkpoint _past_ possibly invalid symbols.
  5888. // Baz is defined in the database, but the file is invalid because it is
  5889. // missing a necessary import.
  5890. DescriptorPool pool(&database_);
  5891. EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == nullptr);
  5892. // Make sure that searching again for the file or the type fails.
  5893. EXPECT_TRUE(pool.FindFileByName("baz.proto") == nullptr);
  5894. EXPECT_TRUE(pool.FindMessageTypeByName("Baz") == nullptr);
  5895. }
  5896. TEST_F(DatabaseBackedPoolTest, UnittestProto) {
  5897. // Try to load all of unittest.proto from a DescriptorDatabase. This should
  5898. // thoroughly test all paths through DescriptorBuilder to insure that there
  5899. // are no deadlocking problems when pool_->mutex_ is non-null.
  5900. const FileDescriptor* original_file =
  5901. protobuf_unittest::TestAllTypes::descriptor()->file();
  5902. DescriptorPoolDatabase database(*DescriptorPool::generated_pool());
  5903. DescriptorPool pool(&database);
  5904. const FileDescriptor* file_from_database =
  5905. pool.FindFileByName(original_file->name());
  5906. ASSERT_TRUE(file_from_database != nullptr);
  5907. FileDescriptorProto original_file_proto;
  5908. original_file->CopyTo(&original_file_proto);
  5909. FileDescriptorProto file_from_database_proto;
  5910. file_from_database->CopyTo(&file_from_database_proto);
  5911. EXPECT_EQ(original_file_proto.DebugString(),
  5912. file_from_database_proto.DebugString());
  5913. // Also verify that CopyTo() did not omit any information.
  5914. EXPECT_EQ(original_file->DebugString(), file_from_database->DebugString());
  5915. }
  5916. TEST_F(DatabaseBackedPoolTest, DoesntRetryDbUnnecessarily) {
  5917. // Searching for a child of an existing descriptor should never fall back
  5918. // to the DescriptorDatabase even if it isn't found, because we know all
  5919. // children are already loaded.
  5920. CallCountingDatabase call_counter(&database_);
  5921. DescriptorPool pool(&call_counter);
  5922. const FileDescriptor* file = pool.FindFileByName("foo.proto");
  5923. ASSERT_TRUE(file != nullptr);
  5924. const Descriptor* foo = pool.FindMessageTypeByName("Foo");
  5925. ASSERT_TRUE(foo != nullptr);
  5926. const EnumDescriptor* test_enum = pool.FindEnumTypeByName("TestEnum");
  5927. ASSERT_TRUE(test_enum != nullptr);
  5928. const ServiceDescriptor* test_service = pool.FindServiceByName("TestService");
  5929. ASSERT_TRUE(test_service != nullptr);
  5930. EXPECT_NE(0, call_counter.call_count_);
  5931. call_counter.Clear();
  5932. EXPECT_TRUE(foo->FindFieldByName("no_such_field") == nullptr);
  5933. EXPECT_TRUE(foo->FindExtensionByName("no_such_extension") == nullptr);
  5934. EXPECT_TRUE(foo->FindNestedTypeByName("NoSuchMessageType") == nullptr);
  5935. EXPECT_TRUE(foo->FindEnumTypeByName("NoSuchEnumType") == nullptr);
  5936. EXPECT_TRUE(foo->FindEnumValueByName("NO_SUCH_VALUE") == nullptr);
  5937. EXPECT_TRUE(test_enum->FindValueByName("NO_SUCH_VALUE") == nullptr);
  5938. EXPECT_TRUE(test_service->FindMethodByName("NoSuchMethod") == nullptr);
  5939. EXPECT_TRUE(file->FindMessageTypeByName("NoSuchMessageType") == nullptr);
  5940. EXPECT_TRUE(file->FindEnumTypeByName("NoSuchEnumType") == nullptr);
  5941. EXPECT_TRUE(file->FindEnumValueByName("NO_SUCH_VALUE") == nullptr);
  5942. EXPECT_TRUE(file->FindServiceByName("NO_SUCH_VALUE") == nullptr);
  5943. EXPECT_TRUE(file->FindExtensionByName("no_such_extension") == nullptr);
  5944. EXPECT_TRUE(pool.FindFileContainingSymbol("Foo.no.such.field") == nullptr);
  5945. EXPECT_TRUE(pool.FindFileContainingSymbol("Foo.no_such_field") == nullptr);
  5946. EXPECT_TRUE(pool.FindMessageTypeByName("Foo.NoSuchMessageType") == nullptr);
  5947. EXPECT_TRUE(pool.FindFieldByName("Foo.no_such_field") == nullptr);
  5948. EXPECT_TRUE(pool.FindExtensionByName("Foo.no_such_extension") == nullptr);
  5949. EXPECT_TRUE(pool.FindEnumTypeByName("Foo.NoSuchEnumType") == nullptr);
  5950. EXPECT_TRUE(pool.FindEnumValueByName("Foo.NO_SUCH_VALUE") == nullptr);
  5951. EXPECT_TRUE(pool.FindMethodByName("TestService.NoSuchMethod") == nullptr);
  5952. EXPECT_EQ(0, call_counter.call_count_);
  5953. }
  5954. TEST_F(DatabaseBackedPoolTest, DoesntReloadFilesUncesessarily) {
  5955. // If FindFileContainingSymbol() or FindFileContainingExtension() return a
  5956. // file that is already in the DescriptorPool, it should not attempt to
  5957. // reload the file.
  5958. FalsePositiveDatabase false_positive_database(&database_);
  5959. MockErrorCollector error_collector;
  5960. DescriptorPool pool(&false_positive_database, &error_collector);
  5961. // First make sure foo.proto is loaded.
  5962. const Descriptor* foo = pool.FindMessageTypeByName("Foo");
  5963. ASSERT_TRUE(foo != nullptr);
  5964. // Try inducing false positives.
  5965. EXPECT_TRUE(pool.FindMessageTypeByName("NoSuchSymbol") == nullptr);
  5966. EXPECT_TRUE(pool.FindExtensionByNumber(foo, 22) == nullptr);
  5967. // No errors should have been reported. (If foo.proto was incorrectly
  5968. // loaded multiple times, errors would have been reported.)
  5969. EXPECT_EQ("", error_collector.text_);
  5970. }
  5971. // DescriptorDatabase that attempts to induce exponentially-bad performance
  5972. // in DescriptorPool. For every positive N, the database contains a file
  5973. // fileN.proto, which defines a message MessageN, which contains fields of
  5974. // type MessageK for all K in [0,N). Message0 is not defined anywhere
  5975. // (file0.proto exists, but is empty), so every other file and message type
  5976. // will fail to build.
  5977. //
  5978. // If the DescriptorPool is not careful to memoize errors, an attempt to
  5979. // build a descriptor for MessageN can require O(2^N) time.
  5980. class ExponentialErrorDatabase : public DescriptorDatabase {
  5981. public:
  5982. ExponentialErrorDatabase() {}
  5983. ~ExponentialErrorDatabase() {}
  5984. // implements DescriptorDatabase ---------------------------------
  5985. bool FindFileByName(const std::string& filename,
  5986. FileDescriptorProto* output) {
  5987. int file_num = -1;
  5988. FullMatch(filename, "file", ".proto", &file_num);
  5989. if (file_num > -1) {
  5990. return PopulateFile(file_num, output);
  5991. } else {
  5992. return false;
  5993. }
  5994. }
  5995. bool FindFileContainingSymbol(const std::string& symbol_name,
  5996. FileDescriptorProto* output) {
  5997. int file_num = -1;
  5998. FullMatch(symbol_name, "Message", "", &file_num);
  5999. if (file_num > 0) {
  6000. return PopulateFile(file_num, output);
  6001. } else {
  6002. return false;
  6003. }
  6004. }
  6005. bool FindFileContainingExtension(const std::string& containing_type,
  6006. int field_number,
  6007. FileDescriptorProto* output) {
  6008. return false;
  6009. }
  6010. private:
  6011. void FullMatch(const std::string& name, const std::string& begin_with,
  6012. const std::string& end_with, int* file_num) {
  6013. int begin_size = begin_with.size();
  6014. int end_size = end_with.size();
  6015. if (name.substr(0, begin_size) != begin_with ||
  6016. name.substr(name.size() - end_size, end_size) != end_with) {
  6017. return;
  6018. }
  6019. safe_strto32(
  6020. name.substr(begin_size, name.size() - end_size - begin_size), file_num);
  6021. }
  6022. bool PopulateFile(int file_num, FileDescriptorProto* output) {
  6023. using strings::Substitute;
  6024. GOOGLE_CHECK_GE(file_num, 0);
  6025. output->Clear();
  6026. output->set_name(Substitute("file$0.proto", file_num));
  6027. // file0.proto doesn't define Message0
  6028. if (file_num > 0) {
  6029. DescriptorProto* message = output->add_message_type();
  6030. message->set_name(Substitute("Message$0", file_num));
  6031. for (int i = 0; i < file_num; ++i) {
  6032. output->add_dependency(Substitute("file$0.proto", i));
  6033. FieldDescriptorProto* field = message->add_field();
  6034. field->set_name(Substitute("field$0", i));
  6035. field->set_number(i);
  6036. field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
  6037. field->set_type(FieldDescriptorProto::TYPE_MESSAGE);
  6038. field->set_type_name(Substitute("Message$0", i));
  6039. }
  6040. }
  6041. return true;
  6042. }
  6043. };
  6044. TEST_F(DatabaseBackedPoolTest, DoesntReloadKnownBadFiles) {
  6045. ExponentialErrorDatabase error_database;
  6046. DescriptorPool pool(&error_database);
  6047. GOOGLE_LOG(INFO) << "A timeout in this test probably indicates a real bug.";
  6048. EXPECT_TRUE(pool.FindFileByName("file40.proto") == nullptr);
  6049. EXPECT_TRUE(pool.FindMessageTypeByName("Message40") == nullptr);
  6050. }
  6051. TEST_F(DatabaseBackedPoolTest, DoesntFallbackOnWrongType) {
  6052. // If a lookup finds a symbol of the wrong type (e.g. we pass a type name
  6053. // to FindFieldByName()), we should fail fast, without checking the fallback
  6054. // database.
  6055. CallCountingDatabase call_counter(&database_);
  6056. DescriptorPool pool(&call_counter);
  6057. const FileDescriptor* file = pool.FindFileByName("foo.proto");
  6058. ASSERT_TRUE(file != nullptr);
  6059. const Descriptor* foo = pool.FindMessageTypeByName("Foo");
  6060. ASSERT_TRUE(foo != nullptr);
  6061. const EnumDescriptor* test_enum = pool.FindEnumTypeByName("TestEnum");
  6062. ASSERT_TRUE(test_enum != nullptr);
  6063. EXPECT_NE(0, call_counter.call_count_);
  6064. call_counter.Clear();
  6065. EXPECT_TRUE(pool.FindMessageTypeByName("TestEnum") == nullptr);
  6066. EXPECT_TRUE(pool.FindFieldByName("Foo") == nullptr);
  6067. EXPECT_TRUE(pool.FindExtensionByName("Foo") == nullptr);
  6068. EXPECT_TRUE(pool.FindEnumTypeByName("Foo") == nullptr);
  6069. EXPECT_TRUE(pool.FindEnumValueByName("Foo") == nullptr);
  6070. EXPECT_TRUE(pool.FindServiceByName("Foo") == nullptr);
  6071. EXPECT_TRUE(pool.FindMethodByName("Foo") == nullptr);
  6072. EXPECT_EQ(0, call_counter.call_count_);
  6073. }
  6074. // ===================================================================
  6075. class AbortingErrorCollector : public DescriptorPool::ErrorCollector {
  6076. public:
  6077. AbortingErrorCollector() {}
  6078. virtual void AddError(const std::string& filename,
  6079. const std::string& element_name, const Message* message,
  6080. ErrorLocation location,
  6081. const std::string& error_message) {
  6082. GOOGLE_LOG(FATAL) << "AddError() called unexpectedly: " << filename << " ["
  6083. << element_name << "]: " << error_message;
  6084. }
  6085. private:
  6086. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AbortingErrorCollector);
  6087. };
  6088. // A source tree containing only one file.
  6089. class SingletonSourceTree : public compiler::SourceTree {
  6090. public:
  6091. SingletonSourceTree(const std::string& filename, const std::string& contents)
  6092. : filename_(filename), contents_(contents) {}
  6093. virtual io::ZeroCopyInputStream* Open(const std::string& filename) {
  6094. return filename == filename_
  6095. ? new io::ArrayInputStream(contents_.data(), contents_.size())
  6096. : nullptr;
  6097. }
  6098. private:
  6099. const std::string filename_;
  6100. const std::string contents_;
  6101. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SingletonSourceTree);
  6102. };
  6103. const char* const kSourceLocationTestInput =
  6104. "syntax = \"proto2\";\n"
  6105. "option java_package = \"com.foo.bar\";\n"
  6106. "option (test_file_opt) = \"foobar\";\n"
  6107. "message A {\n"
  6108. " option (test_msg_opt) = \"foobar\";\n"
  6109. " optional int32 a = 1 [deprecated = true];\n"
  6110. " message B {\n"
  6111. " required double b = 1 [(test_field_opt) = \"foobar\"];\n"
  6112. " }\n"
  6113. " oneof c {\n"
  6114. " option (test_oneof_opt) = \"foobar\";\n"
  6115. " string d = 2;\n"
  6116. " string e = 3;\n"
  6117. " string f = 4;\n"
  6118. " }\n"
  6119. "}\n"
  6120. "enum Indecision {\n"
  6121. " option (test_enum_opt) = 21;\n"
  6122. " option (test_enum_opt) = 42;\n"
  6123. " option (test_enum_opt) = 63;\n"
  6124. " YES = 1 [(test_enumval_opt).a = 100];\n"
  6125. " NO = 2 [(test_enumval_opt) = {a:200}];\n"
  6126. " MAYBE = 3;\n"
  6127. "}\n"
  6128. "service S {\n"
  6129. " option (test_svc_opt) = {a:100};\n"
  6130. " option (test_svc_opt) = {a:200};\n"
  6131. " option (test_svc_opt) = {a:300};\n"
  6132. " rpc Method(A) returns (A.B);\n"
  6133. // Put an empty line here to make the source location range match.
  6134. "\n"
  6135. " rpc OtherMethod(A) returns (A) {\n"
  6136. " option deprecated = true;\n"
  6137. " option (test_method_opt) = \"foobar\";\n"
  6138. " }\n"
  6139. "}\n"
  6140. "message MessageWithExtensions {\n"
  6141. " extensions 1000 to 2000, 2001 to max [(test_ext_opt) = \"foobar\"];\n"
  6142. "}\n"
  6143. "extend MessageWithExtensions {\n"
  6144. " repeated int32 int32_extension = 1001 [packed=true];\n"
  6145. "}\n"
  6146. "message C {\n"
  6147. " extend MessageWithExtensions {\n"
  6148. " optional C message_extension = 1002;\n"
  6149. " }\n"
  6150. "}\n"
  6151. "import \"google/protobuf/descriptor.proto\";\n"
  6152. "extend google.protobuf.FileOptions {\n"
  6153. " optional string test_file_opt = 10101;\n"
  6154. "}\n"
  6155. "extend google.protobuf.MessageOptions {\n"
  6156. " optional string test_msg_opt = 10101;\n"
  6157. "}\n"
  6158. "extend google.protobuf.FieldOptions {\n"
  6159. " optional string test_field_opt = 10101;\n"
  6160. "}\n"
  6161. "extend google.protobuf.EnumOptions {\n"
  6162. " repeated int32 test_enum_opt = 10101;\n"
  6163. "}\n"
  6164. "extend google.protobuf.EnumValueOptions {\n"
  6165. " optional A test_enumval_opt = 10101;\n"
  6166. "}\n"
  6167. "extend google.protobuf.ServiceOptions {\n"
  6168. " repeated A test_svc_opt = 10101;\n"
  6169. "}\n"
  6170. "extend google.protobuf.MethodOptions {\n"
  6171. " optional string test_method_opt = 10101;\n"
  6172. "}\n"
  6173. "extend google.protobuf.OneofOptions {\n"
  6174. " optional string test_oneof_opt = 10101;\n"
  6175. "}\n"
  6176. "extend google.protobuf.ExtensionRangeOptions {\n"
  6177. " optional string test_ext_opt = 10101;\n"
  6178. "}\n";
  6179. class SourceLocationTest : public testing::Test {
  6180. public:
  6181. SourceLocationTest()
  6182. : source_tree_("/test/test.proto", kSourceLocationTestInput),
  6183. simple_db_(),
  6184. source_tree_db_(&source_tree_),
  6185. merged_db_(&simple_db_, &source_tree_db_),
  6186. pool_(&merged_db_, &collector_) {
  6187. // we need descriptor.proto to be accessible by the pool
  6188. // since our test file imports it
  6189. FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto_);
  6190. simple_db_.Add(file_proto_);
  6191. }
  6192. static std::string PrintSourceLocation(const SourceLocation& loc) {
  6193. return strings::Substitute("$0:$1-$2:$3", 1 + loc.start_line,
  6194. 1 + loc.start_column, 1 + loc.end_line,
  6195. 1 + loc.end_column);
  6196. }
  6197. private:
  6198. FileDescriptorProto file_proto_;
  6199. AbortingErrorCollector collector_;
  6200. SingletonSourceTree source_tree_;
  6201. SimpleDescriptorDatabase simple_db_; // contains descriptor.proto
  6202. compiler::SourceTreeDescriptorDatabase source_tree_db_; // loads test.proto
  6203. MergedDescriptorDatabase merged_db_; // combines above two dbs
  6204. protected:
  6205. DescriptorPool pool_;
  6206. // tag number of all custom options in above test file
  6207. static const int kCustomOptionFieldNumber = 10101;
  6208. // tag number of field "a" in message type "A" in above test file
  6209. static const int kAFieldNumber = 1;
  6210. };
  6211. // TODO(adonovan): implement support for option fields and for
  6212. // subparts of declarations.
  6213. TEST_F(SourceLocationTest, GetSourceLocation) {
  6214. SourceLocation loc;
  6215. const FileDescriptor* file_desc =
  6216. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6217. const Descriptor* a_desc = file_desc->FindMessageTypeByName("A");
  6218. EXPECT_TRUE(a_desc->GetSourceLocation(&loc));
  6219. EXPECT_EQ("4:1-16:2", PrintSourceLocation(loc));
  6220. const Descriptor* a_b_desc = a_desc->FindNestedTypeByName("B");
  6221. EXPECT_TRUE(a_b_desc->GetSourceLocation(&loc));
  6222. EXPECT_EQ("7:3-9:4", PrintSourceLocation(loc));
  6223. const EnumDescriptor* e_desc = file_desc->FindEnumTypeByName("Indecision");
  6224. EXPECT_TRUE(e_desc->GetSourceLocation(&loc));
  6225. EXPECT_EQ("17:1-24:2", PrintSourceLocation(loc));
  6226. const EnumValueDescriptor* yes_desc = e_desc->FindValueByName("YES");
  6227. EXPECT_TRUE(yes_desc->GetSourceLocation(&loc));
  6228. EXPECT_EQ("21:3-21:42", PrintSourceLocation(loc));
  6229. const ServiceDescriptor* s_desc = file_desc->FindServiceByName("S");
  6230. EXPECT_TRUE(s_desc->GetSourceLocation(&loc));
  6231. EXPECT_EQ("25:1-35:2", PrintSourceLocation(loc));
  6232. const MethodDescriptor* m_desc = s_desc->FindMethodByName("Method");
  6233. EXPECT_TRUE(m_desc->GetSourceLocation(&loc));
  6234. EXPECT_EQ("29:3-29:31", PrintSourceLocation(loc));
  6235. }
  6236. TEST_F(SourceLocationTest, ExtensionSourceLocation) {
  6237. SourceLocation loc;
  6238. const FileDescriptor* file_desc =
  6239. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6240. const FieldDescriptor* int32_extension_desc =
  6241. file_desc->FindExtensionByName("int32_extension");
  6242. EXPECT_TRUE(int32_extension_desc->GetSourceLocation(&loc));
  6243. EXPECT_EQ("40:3-40:55", PrintSourceLocation(loc));
  6244. const Descriptor* c_desc = file_desc->FindMessageTypeByName("C");
  6245. EXPECT_TRUE(c_desc->GetSourceLocation(&loc));
  6246. EXPECT_EQ("42:1-46:2", PrintSourceLocation(loc));
  6247. const FieldDescriptor* message_extension_desc =
  6248. c_desc->FindExtensionByName("message_extension");
  6249. EXPECT_TRUE(message_extension_desc->GetSourceLocation(&loc));
  6250. EXPECT_EQ("44:5-44:41", PrintSourceLocation(loc));
  6251. }
  6252. TEST_F(SourceLocationTest, InterpretedOptionSourceLocation) {
  6253. // This one's a doozy. It checks every kind of option, including
  6254. // extension range options.
  6255. // We are verifying that the file's source info contains correct
  6256. // info for interpreted options and that it does *not* contain
  6257. // any info for corresponding uninterpreted option path.
  6258. SourceLocation loc;
  6259. const FileDescriptor* file_desc =
  6260. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6261. // File options
  6262. {
  6263. int path[] = {FileDescriptorProto::kOptionsFieldNumber,
  6264. FileOptions::kJavaPackageFieldNumber};
  6265. int unint[] = {FileDescriptorProto::kOptionsFieldNumber,
  6266. FileOptions::kUninterpretedOptionFieldNumber, 0};
  6267. std::vector<int> vpath(path, path + 2);
  6268. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6269. EXPECT_EQ("2:1-2:37", PrintSourceLocation(loc));
  6270. std::vector<int> vunint(unint, unint + 3);
  6271. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6272. }
  6273. {
  6274. int path[] = {FileDescriptorProto::kOptionsFieldNumber,
  6275. kCustomOptionFieldNumber};
  6276. int unint[] = {FileDescriptorProto::kOptionsFieldNumber,
  6277. FileOptions::kUninterpretedOptionFieldNumber, 1};
  6278. std::vector<int> vpath(path, path + 2);
  6279. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6280. EXPECT_EQ("3:1-3:35", PrintSourceLocation(loc));
  6281. std::vector<int> vunint(unint, unint + 3);
  6282. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6283. }
  6284. // Message option
  6285. {
  6286. int path[] = {FileDescriptorProto::kMessageTypeFieldNumber, 0,
  6287. DescriptorProto::kOptionsFieldNumber,
  6288. kCustomOptionFieldNumber};
  6289. int unint[] = {FileDescriptorProto::kMessageTypeFieldNumber, 0,
  6290. DescriptorProto::kOptionsFieldNumber,
  6291. MessageOptions::kUninterpretedOptionFieldNumber, 0};
  6292. std::vector<int> vpath(path, path + 4);
  6293. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6294. EXPECT_EQ("5:3-5:36", PrintSourceLocation(loc));
  6295. std::vector<int> vunint(unint, unint + 5);
  6296. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6297. }
  6298. // Field option
  6299. {
  6300. int path[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6301. 0,
  6302. DescriptorProto::kFieldFieldNumber,
  6303. 0,
  6304. FieldDescriptorProto::kOptionsFieldNumber,
  6305. FieldOptions::kDeprecatedFieldNumber};
  6306. int unint[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6307. 0,
  6308. DescriptorProto::kFieldFieldNumber,
  6309. 0,
  6310. FieldDescriptorProto::kOptionsFieldNumber,
  6311. FieldOptions::kUninterpretedOptionFieldNumber,
  6312. 0};
  6313. std::vector<int> vpath(path, path + 6);
  6314. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6315. EXPECT_EQ("6:25-6:42", PrintSourceLocation(loc));
  6316. std::vector<int> vunint(unint, unint + 7);
  6317. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6318. }
  6319. // Nested message option
  6320. {
  6321. int path[] = {
  6322. FileDescriptorProto::kMessageTypeFieldNumber, 0,
  6323. DescriptorProto::kNestedTypeFieldNumber, 0,
  6324. DescriptorProto::kFieldFieldNumber, 0,
  6325. FieldDescriptorProto::kOptionsFieldNumber, kCustomOptionFieldNumber};
  6326. int unint[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6327. 0,
  6328. DescriptorProto::kNestedTypeFieldNumber,
  6329. 0,
  6330. DescriptorProto::kFieldFieldNumber,
  6331. 0,
  6332. FieldDescriptorProto::kOptionsFieldNumber,
  6333. FieldOptions::kUninterpretedOptionFieldNumber,
  6334. 0};
  6335. std::vector<int> vpath(path, path + 8);
  6336. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6337. EXPECT_EQ("8:28-8:55", PrintSourceLocation(loc));
  6338. std::vector<int> vunint(unint, unint + 9);
  6339. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6340. }
  6341. // One-of option
  6342. {
  6343. int path[] = {
  6344. FileDescriptorProto::kMessageTypeFieldNumber, 0,
  6345. DescriptorProto::kOneofDeclFieldNumber, 0,
  6346. OneofDescriptorProto::kOptionsFieldNumber, kCustomOptionFieldNumber};
  6347. int unint[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6348. 0,
  6349. DescriptorProto::kOneofDeclFieldNumber,
  6350. 0,
  6351. OneofDescriptorProto::kOptionsFieldNumber,
  6352. OneofOptions::kUninterpretedOptionFieldNumber,
  6353. 0};
  6354. std::vector<int> vpath(path, path + 6);
  6355. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6356. EXPECT_EQ("11:5-11:40", PrintSourceLocation(loc));
  6357. std::vector<int> vunint(unint, unint + 7);
  6358. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6359. }
  6360. // Enum option, repeated options
  6361. {
  6362. int path[] = {FileDescriptorProto::kEnumTypeFieldNumber, 0,
  6363. EnumDescriptorProto::kOptionsFieldNumber,
  6364. kCustomOptionFieldNumber, 0};
  6365. int unint[] = {FileDescriptorProto::kEnumTypeFieldNumber, 0,
  6366. EnumDescriptorProto::kOptionsFieldNumber,
  6367. EnumOptions::kUninterpretedOptionFieldNumber, 0};
  6368. std::vector<int> vpath(path, path + 5);
  6369. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6370. EXPECT_EQ("18:3-18:31", PrintSourceLocation(loc));
  6371. std::vector<int> vunint(unint, unint + 5);
  6372. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6373. }
  6374. {
  6375. int path[] = {FileDescriptorProto::kEnumTypeFieldNumber, 0,
  6376. EnumDescriptorProto::kOptionsFieldNumber,
  6377. kCustomOptionFieldNumber, 1};
  6378. int unint[] = {FileDescriptorProto::kEnumTypeFieldNumber, 0,
  6379. EnumDescriptorProto::kOptionsFieldNumber,
  6380. EnumOptions::kUninterpretedOptionFieldNumber, 1};
  6381. std::vector<int> vpath(path, path + 5);
  6382. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6383. EXPECT_EQ("19:3-19:31", PrintSourceLocation(loc));
  6384. std::vector<int> vunint(unint, unint + 5);
  6385. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6386. }
  6387. {
  6388. int path[] = {FileDescriptorProto::kEnumTypeFieldNumber, 0,
  6389. EnumDescriptorProto::kOptionsFieldNumber,
  6390. kCustomOptionFieldNumber, 2};
  6391. int unint[] = {FileDescriptorProto::kEnumTypeFieldNumber, 0,
  6392. EnumDescriptorProto::kOptionsFieldNumber,
  6393. OneofOptions::kUninterpretedOptionFieldNumber, 2};
  6394. std::vector<int> vpath(path, path + 5);
  6395. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6396. EXPECT_EQ("20:3-20:31", PrintSourceLocation(loc));
  6397. std::vector<int> vunint(unint, unint + 5);
  6398. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6399. }
  6400. // Enum value options
  6401. {
  6402. // option w/ message type that directly sets field
  6403. int path[] = {FileDescriptorProto::kEnumTypeFieldNumber,
  6404. 0,
  6405. EnumDescriptorProto::kValueFieldNumber,
  6406. 0,
  6407. EnumValueDescriptorProto::kOptionsFieldNumber,
  6408. kCustomOptionFieldNumber,
  6409. kAFieldNumber};
  6410. int unint[] = {FileDescriptorProto::kEnumTypeFieldNumber,
  6411. 0,
  6412. EnumDescriptorProto::kValueFieldNumber,
  6413. 0,
  6414. EnumValueDescriptorProto::kOptionsFieldNumber,
  6415. EnumValueOptions::kUninterpretedOptionFieldNumber,
  6416. 0};
  6417. std::vector<int> vpath(path, path + 7);
  6418. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6419. EXPECT_EQ("21:14-21:40", PrintSourceLocation(loc));
  6420. std::vector<int> vunint(unint, unint + 7);
  6421. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6422. }
  6423. {
  6424. int path[] = {FileDescriptorProto::kEnumTypeFieldNumber,
  6425. 0,
  6426. EnumDescriptorProto::kValueFieldNumber,
  6427. 1,
  6428. EnumValueDescriptorProto::kOptionsFieldNumber,
  6429. kCustomOptionFieldNumber};
  6430. int unint[] = {FileDescriptorProto::kEnumTypeFieldNumber,
  6431. 0,
  6432. EnumDescriptorProto::kValueFieldNumber,
  6433. 1,
  6434. EnumValueDescriptorProto::kOptionsFieldNumber,
  6435. EnumValueOptions::kUninterpretedOptionFieldNumber,
  6436. 0};
  6437. std::vector<int> vpath(path, path + 6);
  6438. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6439. EXPECT_EQ("22:14-22:42", PrintSourceLocation(loc));
  6440. std::vector<int> vunint(unint, unint + 7);
  6441. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6442. }
  6443. // Service option, repeated options
  6444. {
  6445. int path[] = {FileDescriptorProto::kServiceFieldNumber, 0,
  6446. ServiceDescriptorProto::kOptionsFieldNumber,
  6447. kCustomOptionFieldNumber, 0};
  6448. int unint[] = {FileDescriptorProto::kServiceFieldNumber, 0,
  6449. ServiceDescriptorProto::kOptionsFieldNumber,
  6450. ServiceOptions::kUninterpretedOptionFieldNumber, 0};
  6451. std::vector<int> vpath(path, path + 5);
  6452. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6453. EXPECT_EQ("26:3-26:35", PrintSourceLocation(loc));
  6454. std::vector<int> vunint(unint, unint + 5);
  6455. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6456. }
  6457. {
  6458. int path[] = {FileDescriptorProto::kServiceFieldNumber, 0,
  6459. ServiceDescriptorProto::kOptionsFieldNumber,
  6460. kCustomOptionFieldNumber, 1};
  6461. int unint[] = {FileDescriptorProto::kServiceFieldNumber, 0,
  6462. ServiceDescriptorProto::kOptionsFieldNumber,
  6463. ServiceOptions::kUninterpretedOptionFieldNumber, 1};
  6464. std::vector<int> vpath(path, path + 5);
  6465. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6466. EXPECT_EQ("27:3-27:35", PrintSourceLocation(loc));
  6467. std::vector<int> vunint(unint, unint + 5);
  6468. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6469. }
  6470. {
  6471. int path[] = {FileDescriptorProto::kServiceFieldNumber, 0,
  6472. ServiceDescriptorProto::kOptionsFieldNumber,
  6473. kCustomOptionFieldNumber, 2};
  6474. int unint[] = {FileDescriptorProto::kServiceFieldNumber, 0,
  6475. ServiceDescriptorProto::kOptionsFieldNumber,
  6476. ServiceOptions::kUninterpretedOptionFieldNumber, 2};
  6477. std::vector<int> vpath(path, path + 5);
  6478. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6479. EXPECT_EQ("28:3-28:35", PrintSourceLocation(loc));
  6480. std::vector<int> vunint(unint, unint + 5);
  6481. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6482. }
  6483. // Method options
  6484. {
  6485. int path[] = {FileDescriptorProto::kServiceFieldNumber,
  6486. 0,
  6487. ServiceDescriptorProto::kMethodFieldNumber,
  6488. 1,
  6489. MethodDescriptorProto::kOptionsFieldNumber,
  6490. MethodOptions::kDeprecatedFieldNumber};
  6491. int unint[] = {FileDescriptorProto::kServiceFieldNumber,
  6492. 0,
  6493. ServiceDescriptorProto::kMethodFieldNumber,
  6494. 1,
  6495. MethodDescriptorProto::kOptionsFieldNumber,
  6496. MethodOptions::kUninterpretedOptionFieldNumber,
  6497. 0};
  6498. std::vector<int> vpath(path, path + 6);
  6499. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6500. EXPECT_EQ("32:5-32:30", PrintSourceLocation(loc));
  6501. std::vector<int> vunint(unint, unint + 7);
  6502. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6503. }
  6504. {
  6505. int path[] = {
  6506. FileDescriptorProto::kServiceFieldNumber, 0,
  6507. ServiceDescriptorProto::kMethodFieldNumber, 1,
  6508. MethodDescriptorProto::kOptionsFieldNumber, kCustomOptionFieldNumber};
  6509. int unint[] = {FileDescriptorProto::kServiceFieldNumber,
  6510. 0,
  6511. ServiceDescriptorProto::kMethodFieldNumber,
  6512. 1,
  6513. MethodDescriptorProto::kOptionsFieldNumber,
  6514. MethodOptions::kUninterpretedOptionFieldNumber,
  6515. 1};
  6516. std::vector<int> vpath(path, path + 6);
  6517. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6518. EXPECT_EQ("33:5-33:41", PrintSourceLocation(loc));
  6519. std::vector<int> vunint(unint, unint + 7);
  6520. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6521. }
  6522. // Extension range options
  6523. {
  6524. int path[] = {FileDescriptorProto::kMessageTypeFieldNumber, 1,
  6525. DescriptorProto::kExtensionRangeFieldNumber, 0,
  6526. DescriptorProto_ExtensionRange::kOptionsFieldNumber};
  6527. std::vector<int> vpath(path, path + 5);
  6528. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6529. EXPECT_EQ("37:40-37:67", PrintSourceLocation(loc));
  6530. }
  6531. {
  6532. int path[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6533. 1,
  6534. DescriptorProto::kExtensionRangeFieldNumber,
  6535. 0,
  6536. DescriptorProto_ExtensionRange::kOptionsFieldNumber,
  6537. kCustomOptionFieldNumber};
  6538. int unint[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6539. 1,
  6540. DescriptorProto::kExtensionRangeFieldNumber,
  6541. 0,
  6542. DescriptorProto_ExtensionRange::kOptionsFieldNumber,
  6543. ExtensionRangeOptions::kUninterpretedOptionFieldNumber,
  6544. 0};
  6545. std::vector<int> vpath(path, path + 6);
  6546. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6547. EXPECT_EQ("37:41-37:66", PrintSourceLocation(loc));
  6548. std::vector<int> vunint(unint, unint + 7);
  6549. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6550. }
  6551. {
  6552. int path[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6553. 1,
  6554. DescriptorProto::kExtensionRangeFieldNumber,
  6555. 1,
  6556. DescriptorProto_ExtensionRange::kOptionsFieldNumber,
  6557. kCustomOptionFieldNumber};
  6558. int unint[] = {FileDescriptorProto::kMessageTypeFieldNumber,
  6559. 1,
  6560. DescriptorProto::kExtensionRangeFieldNumber,
  6561. 1,
  6562. DescriptorProto_ExtensionRange::kOptionsFieldNumber,
  6563. ExtensionRangeOptions::kUninterpretedOptionFieldNumber,
  6564. 0};
  6565. std::vector<int> vpath(path, path + 6);
  6566. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6567. EXPECT_EQ("37:41-37:66", PrintSourceLocation(loc));
  6568. std::vector<int> vunint(unint, unint + 7);
  6569. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6570. }
  6571. // Field option on extension
  6572. {
  6573. int path[] = {FileDescriptorProto::kExtensionFieldNumber, 0,
  6574. FieldDescriptorProto::kOptionsFieldNumber,
  6575. FieldOptions::kPackedFieldNumber};
  6576. int unint[] = {FileDescriptorProto::kExtensionFieldNumber, 0,
  6577. FieldDescriptorProto::kOptionsFieldNumber,
  6578. FieldOptions::kUninterpretedOptionFieldNumber, 0};
  6579. std::vector<int> vpath(path, path + 4);
  6580. EXPECT_TRUE(file_desc->GetSourceLocation(vpath, &loc));
  6581. EXPECT_EQ("40:42-40:53", PrintSourceLocation(loc));
  6582. std::vector<int> vunint(unint, unint + 5);
  6583. EXPECT_FALSE(file_desc->GetSourceLocation(vunint, &loc));
  6584. }
  6585. }
  6586. // Missing SourceCodeInfo doesn't cause crash:
  6587. TEST_F(SourceLocationTest, GetSourceLocation_MissingSourceCodeInfo) {
  6588. SourceLocation loc;
  6589. const FileDescriptor* file_desc =
  6590. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6591. FileDescriptorProto proto;
  6592. file_desc->CopyTo(&proto); // Note, this discards the SourceCodeInfo.
  6593. EXPECT_FALSE(proto.has_source_code_info());
  6594. DescriptorPool bad1_pool(&pool_);
  6595. const FileDescriptor* bad1_file_desc =
  6596. GOOGLE_CHECK_NOTNULL(bad1_pool.BuildFile(proto));
  6597. const Descriptor* bad1_a_desc = bad1_file_desc->FindMessageTypeByName("A");
  6598. EXPECT_FALSE(bad1_a_desc->GetSourceLocation(&loc));
  6599. }
  6600. // Corrupt SourceCodeInfo doesn't cause crash:
  6601. TEST_F(SourceLocationTest, GetSourceLocation_BogusSourceCodeInfo) {
  6602. SourceLocation loc;
  6603. const FileDescriptor* file_desc =
  6604. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6605. FileDescriptorProto proto;
  6606. file_desc->CopyTo(&proto); // Note, this discards the SourceCodeInfo.
  6607. EXPECT_FALSE(proto.has_source_code_info());
  6608. SourceCodeInfo_Location* loc_msg =
  6609. proto.mutable_source_code_info()->add_location();
  6610. loc_msg->add_path(1);
  6611. loc_msg->add_path(2);
  6612. loc_msg->add_path(3);
  6613. loc_msg->add_span(4);
  6614. loc_msg->add_span(5);
  6615. loc_msg->add_span(6);
  6616. DescriptorPool bad2_pool(&pool_);
  6617. const FileDescriptor* bad2_file_desc =
  6618. GOOGLE_CHECK_NOTNULL(bad2_pool.BuildFile(proto));
  6619. const Descriptor* bad2_a_desc = bad2_file_desc->FindMessageTypeByName("A");
  6620. EXPECT_FALSE(bad2_a_desc->GetSourceLocation(&loc));
  6621. }
  6622. // ===================================================================
  6623. const char* const kCopySourceCodeInfoToTestInput =
  6624. "syntax = \"proto2\";\n"
  6625. "message Foo {}\n";
  6626. // Required since source code information is not preserved by
  6627. // FileDescriptorTest.
  6628. class CopySourceCodeInfoToTest : public testing::Test {
  6629. public:
  6630. CopySourceCodeInfoToTest()
  6631. : source_tree_("/test/test.proto", kCopySourceCodeInfoToTestInput),
  6632. db_(&source_tree_),
  6633. pool_(&db_, &collector_) {}
  6634. private:
  6635. AbortingErrorCollector collector_;
  6636. SingletonSourceTree source_tree_;
  6637. compiler::SourceTreeDescriptorDatabase db_;
  6638. protected:
  6639. DescriptorPool pool_;
  6640. };
  6641. TEST_F(CopySourceCodeInfoToTest, CopyTo_DoesNotCopySourceCodeInfo) {
  6642. const FileDescriptor* file_desc =
  6643. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6644. FileDescriptorProto file_desc_proto;
  6645. ASSERT_FALSE(file_desc_proto.has_source_code_info());
  6646. file_desc->CopyTo(&file_desc_proto);
  6647. EXPECT_FALSE(file_desc_proto.has_source_code_info());
  6648. }
  6649. TEST_F(CopySourceCodeInfoToTest, CopySourceCodeInfoTo) {
  6650. const FileDescriptor* file_desc =
  6651. GOOGLE_CHECK_NOTNULL(pool_.FindFileByName("/test/test.proto"));
  6652. FileDescriptorProto file_desc_proto;
  6653. ASSERT_FALSE(file_desc_proto.has_source_code_info());
  6654. file_desc->CopySourceCodeInfoTo(&file_desc_proto);
  6655. const SourceCodeInfo& info = file_desc_proto.source_code_info();
  6656. ASSERT_EQ(4, info.location_size());
  6657. // Get the Foo message location
  6658. const SourceCodeInfo_Location& foo_location = info.location(2);
  6659. ASSERT_EQ(2, foo_location.path_size());
  6660. EXPECT_EQ(FileDescriptorProto::kMessageTypeFieldNumber, foo_location.path(0));
  6661. EXPECT_EQ(0, foo_location.path(1)); // Foo is the first message defined
  6662. ASSERT_EQ(3, foo_location.span_size()); // Foo spans one line
  6663. EXPECT_EQ(1, foo_location.span(0)); // Foo is declared on line 1
  6664. EXPECT_EQ(0, foo_location.span(1)); // Foo starts at column 0
  6665. EXPECT_EQ(14, foo_location.span(2)); // Foo ends on column 14
  6666. }
  6667. // ===================================================================
  6668. class LazilyBuildDependenciesTest : public testing::Test {
  6669. public:
  6670. LazilyBuildDependenciesTest() : pool_(&db_, nullptr) {
  6671. pool_.InternalSetLazilyBuildDependencies();
  6672. }
  6673. void ParseProtoAndAddToDb(const char* proto) {
  6674. FileDescriptorProto tmp;
  6675. ASSERT_TRUE(TextFormat::ParseFromString(proto, &tmp));
  6676. db_.Add(tmp);
  6677. }
  6678. void ParseProtoAndAddToDb(const std::string& proto) {
  6679. FileDescriptorProto tmp;
  6680. ASSERT_TRUE(TextFormat::ParseFromString(proto, &tmp));
  6681. db_.Add(tmp);
  6682. }
  6683. void AddSimpleMessageProtoFileToDb(const char* file_name,
  6684. const char* message_name) {
  6685. ParseProtoAndAddToDb("name: '" + std::string(file_name) +
  6686. ".proto' "
  6687. "package: \"protobuf_unittest\" "
  6688. "message_type { "
  6689. " name:'" +
  6690. std::string(message_name) +
  6691. "' "
  6692. " field { name:'a' number:1 "
  6693. " label:LABEL_OPTIONAL "
  6694. " type_name:'int32' } "
  6695. "}");
  6696. }
  6697. void AddSimpleEnumProtoFileToDb(const char* file_name, const char* enum_name,
  6698. const char* enum_value_name) {
  6699. ParseProtoAndAddToDb("name: '" + std::string(file_name) +
  6700. ".proto' "
  6701. "package: 'protobuf_unittest' "
  6702. "enum_type { "
  6703. " name:'" +
  6704. std::string(enum_name) +
  6705. "' "
  6706. " value { name:'" +
  6707. std::string(enum_value_name) +
  6708. "' number:1 } "
  6709. "}");
  6710. }
  6711. protected:
  6712. SimpleDescriptorDatabase db_;
  6713. DescriptorPool pool_;
  6714. };
  6715. TEST_F(LazilyBuildDependenciesTest, Message) {
  6716. ParseProtoAndAddToDb(
  6717. "name: 'foo.proto' "
  6718. "package: 'protobuf_unittest' "
  6719. "dependency: 'bar.proto' "
  6720. "message_type { "
  6721. " name:'Foo' "
  6722. " field { name:'bar' number:1 label:LABEL_OPTIONAL "
  6723. "type_name:'.protobuf_unittest.Bar' } "
  6724. "}");
  6725. AddSimpleMessageProtoFileToDb("bar", "Bar");
  6726. // Verify neither has been built yet.
  6727. EXPECT_FALSE(pool_.InternalIsFileLoaded("foo.proto"));
  6728. EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto"));
  6729. const FileDescriptor* file = pool_.FindFileByName("foo.proto");
  6730. // Verify only foo gets built when asking for foo.proto
  6731. EXPECT_TRUE(file != nullptr);
  6732. EXPECT_TRUE(pool_.InternalIsFileLoaded("foo.proto"));
  6733. EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto"));
  6734. // Verify calling FindFieldBy* works when the type of the field was
  6735. // not built at cross link time. Verify this doesn't build the file
  6736. // the field's type is defined in, as well.
  6737. const Descriptor* desc = file->FindMessageTypeByName("Foo");
  6738. const FieldDescriptor* field = desc->FindFieldByName("bar");
  6739. EXPECT_TRUE(field != nullptr);
  6740. EXPECT_EQ(field, desc->FindFieldByNumber(1));
  6741. EXPECT_EQ(field, desc->FindFieldByLowercaseName("bar"));
  6742. EXPECT_EQ(field, desc->FindFieldByCamelcaseName("bar"));
  6743. EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto"));
  6744. // Finally, verify that if we call message_type() on the field, we will
  6745. // buid the file where the message is defined, and get a valid descriptor
  6746. EXPECT_TRUE(field->message_type() != nullptr);
  6747. EXPECT_TRUE(pool_.InternalIsFileLoaded("bar.proto"));
  6748. }
  6749. TEST_F(LazilyBuildDependenciesTest, Enum) {
  6750. ParseProtoAndAddToDb(
  6751. "name: 'foo.proto' "
  6752. "package: 'protobuf_unittest' "
  6753. "dependency: 'enum1.proto' "
  6754. "dependency: 'enum2.proto' "
  6755. "message_type { "
  6756. " name:'Lazy' "
  6757. " field { name:'enum1' number:1 label:LABEL_OPTIONAL "
  6758. "type_name:'.protobuf_unittest.Enum1' } "
  6759. " field { name:'enum2' number:1 label:LABEL_OPTIONAL "
  6760. "type_name:'.protobuf_unittest.Enum2' } "
  6761. "}");
  6762. AddSimpleEnumProtoFileToDb("enum1", "Enum1", "ENUM1");
  6763. AddSimpleEnumProtoFileToDb("enum2", "Enum2", "ENUM2");
  6764. const FileDescriptor* file = pool_.FindFileByName("foo.proto");
  6765. // Verify calling enum_type() on a field whose definition is not
  6766. // yet built will build the file and return a descriptor.
  6767. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum1.proto"));
  6768. const Descriptor* desc = file->FindMessageTypeByName("Lazy");
  6769. EXPECT_TRUE(desc != nullptr);
  6770. const FieldDescriptor* field = desc->FindFieldByName("enum1");
  6771. EXPECT_TRUE(field != nullptr);
  6772. EXPECT_TRUE(field->enum_type() != nullptr);
  6773. EXPECT_TRUE(pool_.InternalIsFileLoaded("enum1.proto"));
  6774. // Verify calling default_value_enum() on a field whose definition is not
  6775. // yet built will build the file and return a descriptor to the value.
  6776. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum2.proto"));
  6777. field = desc->FindFieldByName("enum2");
  6778. EXPECT_TRUE(field != nullptr);
  6779. EXPECT_TRUE(field->default_value_enum() != nullptr);
  6780. EXPECT_TRUE(pool_.InternalIsFileLoaded("enum2.proto"));
  6781. }
  6782. TEST_F(LazilyBuildDependenciesTest, Type) {
  6783. ParseProtoAndAddToDb(
  6784. "name: 'foo.proto' "
  6785. "package: 'protobuf_unittest' "
  6786. "dependency: 'message1.proto' "
  6787. "dependency: 'message2.proto' "
  6788. "dependency: 'enum1.proto' "
  6789. "dependency: 'enum2.proto' "
  6790. "message_type { "
  6791. " name:'Lazy' "
  6792. " field { name:'message1' number:1 label:LABEL_OPTIONAL "
  6793. "type_name:'.protobuf_unittest.Message1' } "
  6794. " field { name:'message2' number:1 label:LABEL_OPTIONAL "
  6795. "type_name:'.protobuf_unittest.Message2' } "
  6796. " field { name:'enum1' number:1 label:LABEL_OPTIONAL "
  6797. "type_name:'.protobuf_unittest.Enum1' } "
  6798. " field { name:'enum2' number:1 label:LABEL_OPTIONAL "
  6799. "type_name:'.protobuf_unittest.Enum2' } "
  6800. "}");
  6801. AddSimpleMessageProtoFileToDb("message1", "Message1");
  6802. AddSimpleMessageProtoFileToDb("message2", "Message2");
  6803. AddSimpleEnumProtoFileToDb("enum1", "Enum1", "ENUM1");
  6804. AddSimpleEnumProtoFileToDb("enum2", "Enum2", "ENUM2");
  6805. const FileDescriptor* file = pool_.FindFileByName("foo.proto");
  6806. // Verify calling type() on a field that is a message type will
  6807. // build the type defined in another file.
  6808. EXPECT_FALSE(pool_.InternalIsFileLoaded("message1.proto"));
  6809. const Descriptor* desc = file->FindMessageTypeByName("Lazy");
  6810. EXPECT_TRUE(desc != nullptr);
  6811. const FieldDescriptor* field = desc->FindFieldByName("message1");
  6812. EXPECT_TRUE(field != nullptr);
  6813. EXPECT_EQ(field->type(), FieldDescriptor::TYPE_MESSAGE);
  6814. EXPECT_TRUE(pool_.InternalIsFileLoaded("message1.proto"));
  6815. // Verify calling cpp_type() on a field that is a message type will
  6816. // build the type defined in another file.
  6817. EXPECT_FALSE(pool_.InternalIsFileLoaded("message2.proto"));
  6818. field = desc->FindFieldByName("message2");
  6819. EXPECT_TRUE(field != nullptr);
  6820. EXPECT_EQ(field->cpp_type(), FieldDescriptor::CPPTYPE_MESSAGE);
  6821. EXPECT_TRUE(pool_.InternalIsFileLoaded("message2.proto"));
  6822. // Verify calling type() on a field that is an enum type will
  6823. // build the type defined in another file.
  6824. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum1.proto"));
  6825. field = desc->FindFieldByName("enum1");
  6826. EXPECT_TRUE(field != nullptr);
  6827. EXPECT_EQ(field->type(), FieldDescriptor::TYPE_ENUM);
  6828. EXPECT_TRUE(pool_.InternalIsFileLoaded("enum1.proto"));
  6829. // Verify calling cpp_type() on a field that is an enum type will
  6830. // build the type defined in another file.
  6831. EXPECT_FALSE(pool_.InternalIsFileLoaded("enum2.proto"));
  6832. field = desc->FindFieldByName("enum2");
  6833. EXPECT_TRUE(field != nullptr);
  6834. EXPECT_EQ(field->cpp_type(), FieldDescriptor::CPPTYPE_ENUM);
  6835. EXPECT_TRUE(pool_.InternalIsFileLoaded("enum2.proto"));
  6836. }
  6837. TEST_F(LazilyBuildDependenciesTest, Extension) {
  6838. ParseProtoAndAddToDb(
  6839. "name: 'foo.proto' "
  6840. "package: 'protobuf_unittest' "
  6841. "dependency: 'bar.proto' "
  6842. "dependency: 'baz.proto' "
  6843. "extension { extendee: '.protobuf_unittest.Bar' name:'bar' number:11"
  6844. " label:LABEL_OPTIONAL type_name:'.protobuf_unittest.Baz' }");
  6845. ParseProtoAndAddToDb(
  6846. "name: 'bar.proto' "
  6847. "package: 'protobuf_unittest' "
  6848. "message_type { "
  6849. " name:'Bar' "
  6850. " extension_range { start: 10 end: 20 }"
  6851. "}");
  6852. AddSimpleMessageProtoFileToDb("baz", "Baz");
  6853. // Verify none have been built yet.
  6854. EXPECT_FALSE(pool_.InternalIsFileLoaded("foo.proto"));
  6855. EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto"));
  6856. EXPECT_FALSE(pool_.InternalIsFileLoaded("baz.proto"));
  6857. const FileDescriptor* file = pool_.FindFileByName("foo.proto");
  6858. // Verify foo.bar gets loaded, and bar.proto gets loaded
  6859. // to register the extension. baz.proto should not get loaded.
  6860. EXPECT_TRUE(file != nullptr);
  6861. EXPECT_TRUE(pool_.InternalIsFileLoaded("foo.proto"));
  6862. EXPECT_TRUE(pool_.InternalIsFileLoaded("bar.proto"));
  6863. EXPECT_FALSE(pool_.InternalIsFileLoaded("baz.proto"));
  6864. }
  6865. TEST_F(LazilyBuildDependenciesTest, Service) {
  6866. ParseProtoAndAddToDb(
  6867. "name: 'foo.proto' "
  6868. "package: 'protobuf_unittest' "
  6869. "dependency: 'message1.proto' "
  6870. "dependency: 'message2.proto' "
  6871. "dependency: 'message3.proto' "
  6872. "dependency: 'message4.proto' "
  6873. "service {"
  6874. " name: 'LazyService'"
  6875. " method { name: 'A' input_type: '.protobuf_unittest.Message1' "
  6876. " output_type: '.protobuf_unittest.Message2' }"
  6877. "}");
  6878. AddSimpleMessageProtoFileToDb("message1", "Message1");
  6879. AddSimpleMessageProtoFileToDb("message2", "Message2");
  6880. AddSimpleMessageProtoFileToDb("message3", "Message3");
  6881. AddSimpleMessageProtoFileToDb("message4", "Message4");
  6882. const FileDescriptor* file = pool_.FindFileByName("foo.proto");
  6883. // Verify calling FindServiceByName or FindMethodByName doesn't build the
  6884. // files defining the input and output type, and input_type() and
  6885. // output_type() does indeed build the appropriate files.
  6886. const ServiceDescriptor* service = file->FindServiceByName("LazyService");
  6887. EXPECT_TRUE(service != nullptr);
  6888. const MethodDescriptor* method = service->FindMethodByName("A");
  6889. EXPECT_FALSE(pool_.InternalIsFileLoaded("message1.proto"));
  6890. EXPECT_FALSE(pool_.InternalIsFileLoaded("message2.proto"));
  6891. EXPECT_TRUE(method != nullptr);
  6892. EXPECT_TRUE(method->input_type() != nullptr);
  6893. EXPECT_TRUE(pool_.InternalIsFileLoaded("message1.proto"));
  6894. EXPECT_FALSE(pool_.InternalIsFileLoaded("message2.proto"));
  6895. EXPECT_TRUE(method->output_type() != nullptr);
  6896. EXPECT_TRUE(pool_.InternalIsFileLoaded("message2.proto"));
  6897. }
  6898. TEST_F(LazilyBuildDependenciesTest, GeneratedFile) {
  6899. // Most testing is done with custom pools with lazy dependencies forced on,
  6900. // do some sanity checking that lazy imports is on by default for the
  6901. // generated pool, and do custom options testing with generated to
  6902. // be able to use the GetExtension ids for the custom options.
  6903. // Verify none of the files are loaded yet.
  6904. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6905. "google/protobuf/unittest_lazy_dependencies.proto"));
  6906. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6907. "google/protobuf/unittest_lazy_dependencies_custom_option.proto"));
  6908. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6909. "google/protobuf/unittest_lazy_dependencies_enum.proto"));
  6910. // Verify calling autogenerated function to get a descriptor in the base
  6911. // file will build that file but none of it's imports. This verifies that
  6912. // lazily_build_dependencies_ is set on the generated pool, and also that
  6913. // the generated function "descriptor()" doesn't somehow subvert the laziness
  6914. // by manually loading the dependencies or something.
  6915. EXPECT_TRUE(protobuf_unittest::lazy_imports::ImportedMessage::descriptor() !=
  6916. nullptr);
  6917. EXPECT_TRUE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6918. "google/protobuf/unittest_lazy_dependencies.proto"));
  6919. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6920. "google/protobuf/unittest_lazy_dependencies_custom_option.proto"));
  6921. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6922. "google/protobuf/unittest_lazy_dependencies_enum.proto"));
  6923. // Verify custom options work when defined in an import that isn't loaded,
  6924. // and that a non-default value of a custom option doesn't load the file
  6925. // where that enum is defined.
  6926. const MessageOptions& options =
  6927. protobuf_unittest::lazy_imports::MessageCustomOption::descriptor()
  6928. ->options();
  6929. protobuf_unittest::lazy_imports::LazyEnum custom_option_value =
  6930. options.GetExtension(protobuf_unittest::lazy_imports::lazy_enum_option);
  6931. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6932. "google/protobuf/unittest_lazy_dependencies_custom_option.proto"));
  6933. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6934. "google/protobuf/unittest_lazy_dependencies_enum.proto"));
  6935. EXPECT_EQ(custom_option_value, protobuf_unittest::lazy_imports::LAZY_ENUM_1);
  6936. const MessageOptions& options2 =
  6937. protobuf_unittest::lazy_imports::MessageCustomOption2::descriptor()
  6938. ->options();
  6939. custom_option_value =
  6940. options2.GetExtension(protobuf_unittest::lazy_imports::lazy_enum_option);
  6941. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6942. "google/protobuf/unittest_lazy_dependencies_custom_option.proto"));
  6943. EXPECT_FALSE(DescriptorPool::generated_pool()->InternalIsFileLoaded(
  6944. "google/protobuf/unittest_lazy_dependencies_enum.proto"));
  6945. EXPECT_EQ(custom_option_value, protobuf_unittest::lazy_imports::LAZY_ENUM_0);
  6946. }
  6947. TEST_F(LazilyBuildDependenciesTest, Dependency) {
  6948. ParseProtoAndAddToDb(
  6949. "name: 'foo.proto' "
  6950. "package: 'protobuf_unittest' "
  6951. "dependency: 'bar.proto' "
  6952. "message_type { "
  6953. " name:'Foo' "
  6954. " field { name:'bar' number:1 label:LABEL_OPTIONAL "
  6955. "type_name:'.protobuf_unittest.Bar' } "
  6956. "}");
  6957. ParseProtoAndAddToDb(
  6958. "name: 'bar.proto' "
  6959. "package: 'protobuf_unittest' "
  6960. "dependency: 'baz.proto' "
  6961. "message_type { "
  6962. " name:'Bar' "
  6963. " field { name:'baz' number:1 label:LABEL_OPTIONAL "
  6964. "type_name:'.protobuf_unittest.Baz' } "
  6965. "}");
  6966. AddSimpleMessageProtoFileToDb("baz", "Baz");
  6967. const FileDescriptor* foo_file = pool_.FindFileByName("foo.proto");
  6968. EXPECT_TRUE(foo_file != nullptr);
  6969. // As expected, requesting foo.proto shouldn't build it's dependencies
  6970. EXPECT_TRUE(pool_.InternalIsFileLoaded("foo.proto"));
  6971. EXPECT_FALSE(pool_.InternalIsFileLoaded("bar.proto"));
  6972. EXPECT_FALSE(pool_.InternalIsFileLoaded("baz.proto"));
  6973. // Verify calling dependency(N) will build the dependency, but
  6974. // not that file's dependencies.
  6975. const FileDescriptor* bar_file = foo_file->dependency(0);
  6976. EXPECT_TRUE(bar_file != nullptr);
  6977. EXPECT_TRUE(pool_.InternalIsFileLoaded("bar.proto"));
  6978. EXPECT_FALSE(pool_.InternalIsFileLoaded("baz.proto"));
  6979. }
  6980. // ===================================================================
  6981. } // namespace descriptor_unittest
  6982. } // namespace protobuf
  6983. } // namespace google