database_unittest.cc 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "faked_nsec3.h"
  15. #include <exceptions/exceptions.h>
  16. #include <dns/masterload.h>
  17. #include <dns/name.h>
  18. #include <dns/rrttl.h>
  19. #include <dns/rrset.h>
  20. #include <dns/nsec3hash.h>
  21. #include <exceptions/exceptions.h>
  22. #include <datasrc/database.h>
  23. #include <datasrc/zone.h>
  24. #include <datasrc/zone_finder.h>
  25. #include <datasrc/data_source.h>
  26. #include <datasrc/zone_iterator.h>
  27. #include <datasrc/sqlite3_accessor.h>
  28. #include <testutils/dnsmessage_test.h>
  29. #include <gtest/gtest.h>
  30. #include <boost/bind.hpp>
  31. #include <boost/shared_ptr.hpp>
  32. #include <boost/lexical_cast.hpp>
  33. #include <cstdlib>
  34. #include <map>
  35. #include <vector>
  36. using namespace isc::datasrc;
  37. using namespace std;
  38. // don't import the entire boost namespace. It will unexpectedly hide uint32_t
  39. // for some systems.
  40. using boost::dynamic_pointer_cast;
  41. using boost::lexical_cast;
  42. using namespace isc::dns;
  43. using namespace isc::testutils;
  44. using namespace isc::datasrc::test;
  45. namespace {
  46. // Imaginary zone IDs used in the mock accessor below.
  47. const int READONLY_ZONE_ID = 42;
  48. const int NEW_ZONE_ID = 420;
  49. const int WRITABLE_ZONE_ID = 4200;
  50. // Commonly used test data
  51. const char* const TEST_RECORDS[][5] = {
  52. // some plain data
  53. {"www.example.org.", "A", "3600", "", "192.0.2.1"},
  54. {"www.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  55. {"www.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  56. {"www.example.org.", "NSEC", "3600", "", "www2.example.org. A AAAA NSEC RRSIG"},
  57. {"www.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  58. {"www2.example.org.", "A", "3600", "", "192.0.2.1"},
  59. {"www2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  60. {"www2.example.org.", "A", "3600", "", "192.0.2.2"},
  61. {"cname.example.org.", "CNAME", "3600", "", "www.example.org."},
  62. // some DNSSEC-'signed' data
  63. {"signed1.example.org.", "A", "3600", "", "192.0.2.1"},
  64. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  65. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  66. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  67. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  68. {"signed1.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  69. {"signedcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  70. {"signedcname1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  71. // special case might fail; sig is for cname, which isn't there (should be ignored)
  72. // (ignoring of 'normal' other type is done above by www.)
  73. {"acnamesig1.example.org.", "A", "3600", "", "192.0.2.1"},
  74. {"acnamesig1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  75. {"acnamesig1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  76. // let's pretend we have a database that is not careful
  77. // about the order in which it returns data
  78. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  79. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  80. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  81. {"signed2.example.org.", "A", "3600", "", "192.0.2.1"},
  82. {"signed2.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  83. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  84. {"signedcname2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  85. {"signedcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  86. {"acnamesig2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  87. {"acnamesig2.example.org.", "A", "3600", "", "192.0.2.1"},
  88. {"acnamesig2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  89. {"acnamesig3.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  90. {"acnamesig3.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  91. {"acnamesig3.example.org.", "A", "3600", "", "192.0.2.1"},
  92. {"ttldiff1.example.org.", "A", "3600", "", "192.0.2.1"},
  93. {"ttldiff1.example.org.", "A", "360", "", "192.0.2.2"},
  94. {"ttldiff2.example.org.", "A", "360", "", "192.0.2.1"},
  95. {"ttldiff2.example.org.", "A", "3600", "", "192.0.2.2"},
  96. // also add some intentionally bad data
  97. {"badcname1.example.org.", "A", "3600", "", "192.0.2.1"},
  98. {"badcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  99. {"badcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  100. {"badcname2.example.org.", "A", "3600", "", "192.0.2.1"},
  101. {"badcname3.example.org.", "CNAME", "3600", "", "www.example.org."},
  102. {"badcname3.example.org.", "CNAME", "3600", "", "www.example2.org."},
  103. {"badrdata.example.org.", "A", "3600", "", "bad"},
  104. {"badtype.example.org.", "BAD_TYPE", "3600", "", "192.0.2.1"},
  105. {"badttl.example.org.", "A", "badttl", "", "192.0.2.1"},
  106. {"badsig.example.org.", "A", "badttl", "", "192.0.2.1"},
  107. {"badsig.example.org.", "RRSIG", "3600", "", "A 5 3 3600 somebaddata 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  108. {"badsigtype.example.org.", "A", "3600", "", "192.0.2.1"},
  109. {"badsigtype.example.org.", "RRSIG", "3600", "TXT", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  110. // Data for testing delegation (with NS and DNAME)
  111. {"delegation.example.org.", "NS", "3600", "", "ns.example.com."},
  112. {"delegation.example.org.", "NS", "3600", "",
  113. "ns.delegation.example.org."},
  114. {"delegation.example.org.", "DS", "3600", "", "1 1 2 abcd"},
  115. {"delegation.example.org.", "RRSIG", "3600", "", "NS 5 3 3600 "
  116. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  117. {"delegation.example.org.", "RRSIG", "3600", "", "DS 5 3 3600 "
  118. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  119. {"ns.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  120. {"deep.below.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  121. {"dname.example.org.", "A", "3600", "", "192.0.2.1"},
  122. {"dname.example.org.", "DNAME", "3600", "", "dname.example.com."},
  123. {"dname.example.org.", "RRSIG", "3600", "",
  124. "DNAME 5 3 3600 20000101000000 20000201000000 12345 "
  125. "example.org. FAKEFAKEFAKE"},
  126. {"below.dname.example.org.", "A", "3600", "", "192.0.2.1"},
  127. // Insecure delegation (i.e., no DS at the delegation point)
  128. {"insecdelegation.example.org.", "NS", "3600", "", "ns.example.com."},
  129. {"insecdelegation.example.org.", "NSEC", "3600", "",
  130. "dummy.example.org. NS NSEC"},
  131. // and a DS under the zone cut. Such an RR shouldn't exist in a sane zone,
  132. // but it could by error or some malicious attempt. It shouldn't confuse
  133. // the implementation)
  134. {"child.insecdelegation.example.org.", "DS", "3600", "", "DS 5 3 3600 "
  135. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  136. // Delegation NS and other ordinary type of RR coexist at the same
  137. // name. This is deviant (except for some special cases like the other
  138. // RR could be used for addressing the NS name), but as long as the
  139. // other records are hidden behind the delegation for normal queries
  140. // it's not necessarily harmful. (so "broken" may be too strong, but we
  141. // keep the name since it could be in a chain of sorted names for DNSSEC
  142. // processing and renaming them may have other bad effects for tests).
  143. {"brokenns1.example.org.", "A", "3600", "", "192.0.2.1"},
  144. {"brokenns1.example.org.", "NS", "3600", "", "ns.example.com."},
  145. // Now double DNAME, to test failure mode
  146. {"baddname.example.org.", "DNAME", "3600", "", "dname1.example.com."},
  147. {"baddname.example.org.", "DNAME", "3600", "", "dname2.example.com."},
  148. // Put some data into apex (including NS) so we can check our NS
  149. // doesn't break anything
  150. {"example.org.", "SOA", "3600", "", "ns1.example.org. admin.example.org. "
  151. "1234 3600 1800 2419200 7200" },
  152. {"example.org.", "NS", "3600", "", "ns.example.com."},
  153. {"example.org.", "A", "3600", "", "192.0.2.1"},
  154. // Note that the RDATA text is "normalized", i.e., identical to what
  155. // Rdata::toText() would produce. some tests rely on that behavior.
  156. {"example.org.", "NSEC", "3600", "",
  157. "acnamesig1.example.org. A NS RRSIG NSEC"},
  158. {"example.org.", "RRSIG", "3600", "", "SOA 5 3 3600 20000101000000 "
  159. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  160. {"example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 "
  161. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  162. {"example.org.", "RRSIG", "3600", "", "NS 5 3 3600 20000101000000 "
  163. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  164. // This is because of empty domain test
  165. {"a.b.example.org.", "A", "3600", "", "192.0.2.1"},
  166. // Something for wildcards
  167. {"*.wild.example.org.", "A", "3600", "", "192.0.2.5"},
  168. {"*.wild.example.org.", "RRSIG", "3600", "A", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  169. {"*.wild.example.org.", "NSEC", "3600", "", "cancel.here.wild.example.org. A NSEC RRSIG"},
  170. {"*.wild.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  171. {"cancel.here.wild.example.org.", "AAAA", "3600", "", "2001:db8::5"},
  172. {"delegatedwild.example.org.", "NS", "3600", "", "ns.example.com."},
  173. {"*.delegatedwild.example.org.", "A", "3600", "", "192.0.2.5"},
  174. {"wild.*.foo.example.org.", "A", "3600", "", "192.0.2.5"},
  175. {"wild.*.foo.*.bar.example.org.", "A", "3600", "", "192.0.2.5"},
  176. {"wild.*.foo.*.bar.example.org.", "NSEC", "3600", "",
  177. "brokenns1.example.org. A NSEC"},
  178. {"bao.example.org.", "NSEC", "3600", "", "wild.*.foo.*.bar.example.org. NSEC"},
  179. {"*.cnamewild.example.org.", "CNAME", "3600", "", "www.example.org."},
  180. {"*.dnamewild.example.org.", "DNAME", "3600", "", "dname.example.com."},
  181. {"*.nswild.example.org.", "NS", "3600", "", "ns.example.com."},
  182. // For NSEC empty non-terminal
  183. {"l.example.org.", "NSEC", "3600", "", "empty.nonterminal.example.org. NSEC"},
  184. {"empty.nonterminal.example.org.", "A", "3600", "", "192.0.2.1"},
  185. // Invalid rdata
  186. {"invalidrdata.example.org.", "A", "3600", "", "Bunch of nonsense"},
  187. {"invalidrdata2.example.org.", "A", "3600", "", "192.0.2.1"},
  188. {"invalidrdata2.example.org.", "RRSIG", "3600", "", "Nonsense"},
  189. {NULL, NULL, NULL, NULL, NULL},
  190. };
  191. // NSEC3PARAM at the zone origin and its RRSIG. These will be added
  192. // separately for some NSEC3 related tests.
  193. const char* TEST_NSEC3PARAM_RECORDS[][5] = {
  194. {"example.org.", "NSEC3PARAM", "3600", "", "1 0 12 aabbccdd"},
  195. {"example.org.", "RRSIG", "3600", "", "NSEC3PARAM 5 3 3600 20000101000000 "
  196. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  197. {NULL, NULL, NULL, NULL, NULL}
  198. };
  199. // FIXME: Taken from a different test. Fill with proper data when creating a test.
  200. const char* TEST_NSEC3_RECORDS[][5] = {
  201. {apex_hash, "NSEC3", "300", "", "1 1 12 AABBCCDD 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG"},
  202. {apex_hash, "RRSIG", "300", "", "NSEC3 5 4 7200 20100410172647 20100311172647 63192 example.org. gNIVj4T8t51fEU6kOPpvK7HOGBFZGbalN5ZK mInyrww6UWZsUNdw07ge6/U6HfG+/s61RZ/L is2M6yUWHyXbNbj/QqwqgadG5dhxTArfuR02 xP600x0fWX8LXzW4yLMdKVxGbzYT+vvGz71o 8gHSY5vYTtothcZQa4BMKhmGQEk="},
  203. {ns1_hash, "NSEC3", "300", "", "1 1 12 AABBCCDD 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG"},
  204. {ns1_hash, "RRSIG", "300", "", "NSEC3 5 4 7200 20100410172647 20100311172647 63192 example.org. gNIVj4T8t51fEU6kOPpvK7HOGBFZGbalN5ZK mInyrww6UWZsUNdw07ge6/U6HfG+/s61RZ/L is2M6yUWHyXbNbj/QqwqgadG5dhxTArfuR02 xP600x0fWX8LXzW4yLMdKVxGbzYT+vvGz71o 8gHSY5vYTtothcZQa4BMKhmGQEk="},
  205. {w_hash, "NSEC3", "300", "", "1 1 12 AABBCCDD 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG"},
  206. {w_hash, "RRSIG", "300", "", "NSEC3 5 4 7200 20100410172647 20100311172647 63192 example.org. gNIVj4T8t51fEU6kOPpvK7HOGBFZGbalN5ZK mInyrww6UWZsUNdw07ge6/U6HfG+/s61RZ/L is2M6yUWHyXbNbj/QqwqgadG5dhxTArfuR02 xP600x0fWX8LXzW4yLMdKVxGbzYT+vvGz71o 8gHSY5vYTtothcZQa4BMKhmGQEk="},
  207. {zzz_hash, "NSEC3", "300", "", "1 1 12 AABBCCDD 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG"},
  208. {zzz_hash, "RRSIG", "300", "", "NSEC3 5 4 7200 20100410172647 20100311172647 63192 example.org. gNIVj4T8t51fEU6kOPpvK7HOGBFZGbalN5ZK mInyrww6UWZsUNdw07ge6/U6HfG+/s61RZ/L is2M6yUWHyXbNbj/QqwqgadG5dhxTArfuR02 xP600x0fWX8LXzW4yLMdKVxGbzYT+vvGz71o 8gHSY5vYTtothcZQa4BMKhmGQEk="},
  209. {NULL, NULL, NULL, NULL, NULL}
  210. };
  211. /*
  212. * An accessor with minimum implementation, keeping the original
  213. * "NotImplemented" methods.
  214. */
  215. class NopAccessor : public DatabaseAccessor {
  216. public:
  217. NopAccessor() : database_name_("mock_database") {
  218. zones_["example.org."] = READONLY_ZONE_ID;
  219. zones_["null.example.org."] = 13;
  220. zones_["empty.example.org."] = 0;
  221. zones_["bad.example.org."] = -1;
  222. }
  223. virtual std::pair<bool, int> getZone(const std::string& name) const {
  224. std::map<std::string, int>::const_iterator found = zones_.find(name);
  225. if (found != zones_.end()) {
  226. return (std::pair<bool, int>(true, found->second));
  227. } else {
  228. return (std::pair<bool, int>(false, 0));
  229. }
  230. }
  231. // A simple implementation of addZone.
  232. virtual int addZone(const std::string& zone_name) {
  233. if (zone_name == "example.com.") {
  234. zones_[zone_name] = NEW_ZONE_ID;
  235. }
  236. // for simplicity we assume zone_name is in zones_ at this point
  237. return (zones_[zone_name]);
  238. }
  239. // A simple implementation of deleteZone.
  240. virtual void deleteZone(int zone_id) {
  241. std::map<std::string, int>::iterator it = zones_.begin();
  242. std::map<std::string, int>::iterator end = zones_.end();
  243. while (it != end) {
  244. if (it->second == zone_id) {
  245. zones_.erase(it);
  246. return;
  247. }
  248. ++it;
  249. }
  250. }
  251. virtual boost::shared_ptr<DatabaseAccessor> clone() {
  252. // This accessor is stateless, so we can simply return a new instance.
  253. return (boost::shared_ptr<DatabaseAccessor>(new NopAccessor));
  254. }
  255. virtual std::pair<bool, int> startUpdateZone(const std::string&, bool) {
  256. // return dummy value. unused anyway.
  257. return (pair<bool, int>(true, 0));
  258. }
  259. virtual void startTransaction() {}
  260. virtual void commit() {}
  261. virtual void rollback() {}
  262. virtual void addRecordToZone(const string (&)[ADD_COLUMN_COUNT]) {}
  263. virtual void addNSEC3RecordToZone(const string (&)[ADD_NSEC3_COLUMN_COUNT])
  264. {}
  265. virtual void deleteRecordInZone(const string (&)[DEL_PARAM_COUNT]) {}
  266. virtual void deleteNSEC3RecordInZone(const string (&)[DEL_PARAM_COUNT]) {}
  267. virtual void addRecordDiff(int, uint32_t, DiffOperation,
  268. const std::string (&)[DIFF_PARAM_COUNT]) {}
  269. virtual const std::string& getDBName() const {
  270. return (database_name_);
  271. }
  272. virtual IteratorContextPtr getRecords(const std::string&, int, bool)
  273. const
  274. {
  275. isc_throw(isc::NotImplemented,
  276. "This database datasource can't be iterated");
  277. }
  278. virtual IteratorContextPtr getNSEC3Records(const std::string&, int) const {
  279. isc_throw(isc::NotImplemented, "This test database datasource won't "
  280. "give you any NSEC3. Ever. Ask someone else.");
  281. }
  282. virtual IteratorContextPtr getAllRecords(int) const {
  283. isc_throw(isc::NotImplemented,
  284. "This database datasource can't be iterated");
  285. }
  286. virtual IteratorContextPtr getDiffs(int, uint32_t, uint32_t) const {
  287. isc_throw(isc::NotImplemented,
  288. "This database datasource doesn't support diffs");
  289. }
  290. virtual std::string findPreviousName(int, const std::string&) const {
  291. isc_throw(isc::NotImplemented,
  292. "This data source doesn't support DNSSEC");
  293. }
  294. virtual std::string findPreviousNSEC3Hash(int, const std::string&) const {
  295. isc_throw(isc::NotImplemented,
  296. "This test database knows nothing about NSEC3 nor order");
  297. }
  298. private:
  299. const std::string database_name_;
  300. std::map<std::string, int> zones_;
  301. };
  302. /**
  303. * Single journal entry in the mock database.
  304. *
  305. * All the members there are public for simplicity, as it only stores data.
  306. * We use the implicit constructor and operator. The members can't be const
  307. * because of the assignment operator (used in the vectors).
  308. */
  309. struct JournalEntry {
  310. JournalEntry(int id, uint32_t serial,
  311. DatabaseAccessor::DiffOperation operation,
  312. const std::string (&data)[DatabaseAccessor::DIFF_PARAM_COUNT])
  313. : id_(id), serial_(serial), operation_(operation)
  314. {
  315. data_[DatabaseAccessor::DIFF_NAME] = data[DatabaseAccessor::DIFF_NAME];
  316. data_[DatabaseAccessor::DIFF_TYPE] = data[DatabaseAccessor::DIFF_TYPE];
  317. data_[DatabaseAccessor::DIFF_TTL] = data[DatabaseAccessor::DIFF_TTL];
  318. data_[DatabaseAccessor::DIFF_RDATA] =
  319. data[DatabaseAccessor::DIFF_RDATA];
  320. }
  321. JournalEntry(int id, uint32_t serial,
  322. DatabaseAccessor::DiffOperation operation,
  323. const std::string& name, const std::string& type,
  324. const std::string& ttl, const std::string& rdata):
  325. id_(id), serial_(serial), operation_(operation)
  326. {
  327. data_[DatabaseAccessor::DIFF_NAME] = name;
  328. data_[DatabaseAccessor::DIFF_TYPE] = type;
  329. data_[DatabaseAccessor::DIFF_TTL] = ttl;
  330. data_[DatabaseAccessor::DIFF_RDATA] = rdata;
  331. }
  332. int id_;
  333. uint32_t serial_;
  334. DatabaseAccessor::DiffOperation operation_;
  335. std::string data_[DatabaseAccessor::DIFF_PARAM_COUNT];
  336. bool operator==(const JournalEntry& other) const {
  337. for (size_t i = 0; i < DatabaseAccessor::DIFF_PARAM_COUNT; ++ i) {
  338. if (data_[i] != other.data_[i]) {
  339. return false;
  340. }
  341. }
  342. // No need to check data here, checked above
  343. return (id_ == other.id_ && serial_ == other.serial_ &&
  344. operation_ == other.operation_);
  345. }
  346. };
  347. /*
  348. * A virtual database accessor that pretends it contains single zone --
  349. * example.org.
  350. *
  351. * It has the same getZone method as NopConnection, but it provides
  352. * implementation of the optional functionality.
  353. */
  354. class MockAccessor : public NopAccessor {
  355. // Type of mock database "row"s. This is a map whose keys are the
  356. // own names. We internally sort them by the name comparison order.
  357. struct NameCompare : public binary_function<string, string, bool> {
  358. bool operator()(const string& n1, const string& n2) const {
  359. return (Name(n1).compare(Name(n2)).getOrder() < 0);
  360. }
  361. };
  362. typedef std::map<std::string,
  363. std::vector< std::vector<std::string> >,
  364. NameCompare > Domains;
  365. public:
  366. MockAccessor() : rollbacked_(false), did_transaction_(false) {
  367. readonly_records_ = &readonly_records_master_;
  368. update_records_ = &update_records_master_;
  369. nsec3_namespace_ = &nsec3_namespace_master_;
  370. update_nsec3_namespace_ = &update_nsec3_namespace_master_;
  371. empty_records_ = &empty_records_master_;
  372. journal_entries_ = &journal_entries_master_;
  373. fillData();
  374. }
  375. virtual boost::shared_ptr<DatabaseAccessor> clone() {
  376. boost::shared_ptr<MockAccessor> cloned_accessor(new MockAccessor());
  377. cloned_accessor->readonly_records_ = &readonly_records_master_;
  378. cloned_accessor->update_records_ = &update_records_master_;
  379. cloned_accessor->nsec3_namespace_ = &nsec3_namespace_master_;
  380. cloned_accessor->update_nsec3_namespace_ =
  381. &update_nsec3_namespace_master_;
  382. cloned_accessor->empty_records_ = &empty_records_master_;
  383. cloned_accessor->journal_entries_ = &journal_entries_master_;
  384. latest_clone_ = cloned_accessor;
  385. return (cloned_accessor);
  386. }
  387. virtual void startTransaction() {
  388. // Currently we only use this transaction for simple read-only
  389. // operations. So we just make a local copy of the data (we don't
  390. // care about what happens after commit() or rollback()).
  391. // Obviously as a consequence, if a test case tries to make multiple
  392. // transactions on a single mock accessor it will fail.
  393. // Check any attempt of multiple transactions
  394. if (did_transaction_) {
  395. isc_throw(DataSourceError, "MockAccessor::startTransaction() "
  396. "called multiple times - likely a bug in the test");
  397. }
  398. readonly_records_copy_ = *readonly_records_;
  399. readonly_records_ = &readonly_records_copy_;
  400. did_transaction_ = true;
  401. }
  402. // If the test needs multiple calls to startTransaction() and knows it's
  403. // safe, it can use this method to disable the safeguard check in
  404. // startTransaction(); the test can also use this method by emulating a
  405. // lock conflict by setting is_allowed to false.
  406. void allowMoreTransaction(bool is_allowed) {
  407. did_transaction_ = !is_allowed;
  408. }
  409. private:
  410. class DomainIterator : public IteratorContext {
  411. public:
  412. DomainIterator(const std::vector<std::vector<std::string> >& domain) :
  413. domain_(domain),
  414. position_(domain_.begin())
  415. {}
  416. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) {
  417. if (position_ == domain_.end()) {
  418. return (false);
  419. } else {
  420. for (size_t i(0); i < COLUMN_COUNT; ++ i) {
  421. columns[i] = (*position_)[i];
  422. }
  423. ++ position_;
  424. return (true);
  425. }
  426. }
  427. private:
  428. const std::vector<std::vector<std::string> > domain_;
  429. std::vector<std::vector<std::string> >::const_iterator position_;
  430. };
  431. class MockNameIteratorContext : public IteratorContext {
  432. public:
  433. MockNameIteratorContext(const MockAccessor& mock_accessor, int zone_id,
  434. const std::string& name, bool subdomains) :
  435. searched_name_(name), cur_record_(0)
  436. {
  437. // 'hardcoded' names to trigger exceptions
  438. // On these names some exceptions are thrown, to test the robustness
  439. // of the find() method.
  440. if (searched_name_ == "dsexception.example.org.") {
  441. isc_throw(DataSourceError, "datasource exception on search");
  442. } else if (searched_name_ == "iscexception.example.org.") {
  443. isc_throw(isc::Exception, "isc exception on search");
  444. } else if (searched_name_ == "basicexception.example.org.") {
  445. throw std::exception();
  446. }
  447. cur_record_ = 0;
  448. const Domains& cur_records = mock_accessor.getMockRecords(zone_id);
  449. if (cur_records.count(name) > 0) {
  450. // we're not aiming for efficiency in this test, simply
  451. // copy the relevant vector from records
  452. cur_name = cur_records.find(name)->second;
  453. } else if (subdomains) {
  454. cur_name.clear();
  455. // Just walk everything and check if it is a subdomain.
  456. // If it is, just copy all data from there.
  457. for (Domains::const_iterator i = cur_records.begin();
  458. i != cur_records.end(); ++i) {
  459. const Name local(i->first);
  460. if (local.compare(Name(name)).getRelation() ==
  461. isc::dns::NameComparisonResult::SUBDOMAIN) {
  462. cur_name.insert(cur_name.end(), i->second.begin(),
  463. i->second.end());
  464. }
  465. }
  466. } else {
  467. cur_name.clear();
  468. }
  469. }
  470. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) {
  471. if (searched_name_ == "dsexception.getnext.example.org.") {
  472. isc_throw(DataSourceError, "datasource exception on getnextrecord");
  473. } else if (searched_name_ == "iscexception.getnext.example.org.") {
  474. isc_throw(isc::Exception, "isc exception on getnextrecord");
  475. } else if (searched_name_ ==
  476. "basicexception.getnext.example.org.") {
  477. throw std::exception();
  478. }
  479. if (cur_record_ < cur_name.size()) {
  480. for (size_t i = 0; i < COLUMN_COUNT; ++i) {
  481. columns[i] = cur_name[cur_record_][i];
  482. }
  483. cur_record_++;
  484. return (true);
  485. } else {
  486. return (false);
  487. }
  488. }
  489. private:
  490. const std::string searched_name_;
  491. size_t cur_record_;
  492. std::vector< std::vector<std::string> > cur_name;
  493. };
  494. class MockIteratorContext : public IteratorContext {
  495. private:
  496. int step;
  497. const Domains& domains_;
  498. public:
  499. MockIteratorContext(const Domains& domains) :
  500. step(0), domains_(domains)
  501. { }
  502. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  503. // A special case: if the given set of domains is already empty,
  504. // we always return false.
  505. if (domains_.empty()) {
  506. return (false);
  507. }
  508. // Return faked data for tests
  509. // This is the sequence of zone data in the order of appearance
  510. // in the returned sequence from this iterator.
  511. typedef const char* ColumnText[4];
  512. const ColumnText zone_data[] = {
  513. // A couple of basic RRs at the zone origin.
  514. {"example.org", "A", "3600", "192.0.2.1"},
  515. {"example.org", "SOA", "3600", "ns1.example.org. "
  516. "admin.example.org. 1234 3600 1800 2419200 7200"},
  517. // RRsets sharing the same owner name with multiple RRs.
  518. {"x.example.org", "A", "300", "192.0.2.1"},
  519. {"x.example.org", "A", "300", "192.0.2.2"},
  520. {"x.example.org", "AAAA", "300", "2001:db8::1"},
  521. {"x.example.org", "AAAA", "300", "2001:db8::2"},
  522. // RRSIGs. Covered types are different and these two should
  523. // be distinguished.
  524. {"x.example.org", "RRSIG", "300",
  525. "A 5 3 3600 20000101000000 20000201000000 12345 "
  526. "example.org. FAKEFAKEFAKE"},
  527. {"x.example.org", "RRSIG", "300",
  528. "AAAA 5 3 3600 20000101000000 20000201000000 12345 "
  529. "example.org. FAKEFAKEFAKEFAKE"},
  530. // Mixture of different TTLs. Covering both cases of small
  531. // then large and large then small. In either case the smaller
  532. // TTL should win.
  533. {"ttldiff.example.org", "A", "300", "192.0.2.1"},
  534. {"ttldiff.example.org", "A", "600", "192.0.2.2"},
  535. {"ttldiff2.example.org", "AAAA", "600", "2001:db8::1"},
  536. {"ttldiff2.example.org", "AAAA", "300", "2001:db8::2"}};
  537. const size_t num_rrs = sizeof(zone_data) / sizeof(zone_data[0]);
  538. if (step > num_rrs) {
  539. ADD_FAILURE() << "Request past the end of iterator context";
  540. } else if (step < num_rrs) {
  541. data[DatabaseAccessor::NAME_COLUMN] = zone_data[step][0];
  542. data[DatabaseAccessor::TYPE_COLUMN] = zone_data[step][1];
  543. data[DatabaseAccessor::TTL_COLUMN] = zone_data[step][2];
  544. data[DatabaseAccessor::RDATA_COLUMN] = zone_data[step][3];
  545. ++step;
  546. return (true);
  547. }
  548. return (false);
  549. }
  550. };
  551. class EmptyIteratorContext : public IteratorContext {
  552. public:
  553. virtual bool getNext(string(&)[COLUMN_COUNT]) {
  554. return (false);
  555. }
  556. };
  557. class BadIteratorContext : public IteratorContext {
  558. private:
  559. int step;
  560. public:
  561. BadIteratorContext() :
  562. step(0)
  563. { }
  564. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  565. switch (step ++) {
  566. case 0:
  567. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  568. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  569. data[DatabaseAccessor::TTL_COLUMN] = "300";
  570. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  571. return (true);
  572. case 1:
  573. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  574. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  575. data[DatabaseAccessor::TTL_COLUMN] = "301";
  576. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.2";
  577. return (true);
  578. default:
  579. ADD_FAILURE() <<
  580. "Request past the end of iterator context";
  581. case 2:
  582. return (false);
  583. }
  584. }
  585. };
  586. class MockDiffIteratorContext : public IteratorContext {
  587. const vector<JournalEntry> diffs_;
  588. vector<JournalEntry>::const_iterator it_;
  589. public:
  590. MockDiffIteratorContext(const vector<JournalEntry>& diffs) :
  591. diffs_(diffs), it_(diffs_.begin())
  592. {}
  593. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  594. if (it_ == diffs_.end()) {
  595. return (false);
  596. }
  597. data[DatabaseAccessor::NAME_COLUMN] =
  598. (*it_).data_[DatabaseAccessor::DIFF_NAME];
  599. data[DatabaseAccessor::TYPE_COLUMN] =
  600. (*it_).data_[DatabaseAccessor::DIFF_TYPE];
  601. data[DatabaseAccessor::TTL_COLUMN] =
  602. (*it_).data_[DatabaseAccessor::DIFF_TTL];
  603. data[DatabaseAccessor::RDATA_COLUMN] =
  604. (*it_).data_[DatabaseAccessor::DIFF_RDATA];
  605. ++it_;
  606. return (true);
  607. }
  608. };
  609. public:
  610. virtual IteratorContextPtr getAllRecords(int id) const {
  611. if (id == READONLY_ZONE_ID) {
  612. return (IteratorContextPtr(new MockIteratorContext(
  613. *readonly_records_)));
  614. } else if (id == 13) {
  615. return (IteratorContextPtr());
  616. } else if (id == 0) {
  617. return (IteratorContextPtr(new EmptyIteratorContext()));
  618. } else if (id == -1) {
  619. return (IteratorContextPtr(new BadIteratorContext()));
  620. } else {
  621. isc_throw(isc::Unexpected, "Unknown zone ID");
  622. }
  623. }
  624. virtual IteratorContextPtr getRecords(const std::string& name, int id,
  625. bool subdomains) const
  626. {
  627. if (id == READONLY_ZONE_ID || id == WRITABLE_ZONE_ID) {
  628. return (IteratorContextPtr(
  629. new MockNameIteratorContext(*this, id, name,
  630. subdomains)));
  631. } else {
  632. // This iterator is bogus, but for the cases tested below that's
  633. // sufficient.
  634. return (IteratorContextPtr(
  635. new MockNameIteratorContext(*this, READONLY_ZONE_ID,
  636. name, subdomains)));
  637. }
  638. }
  639. virtual IteratorContextPtr getNSEC3Records(const std::string& hash,
  640. int) const
  641. {
  642. Domains::const_iterator it(nsec3_namespace_->find(hash));
  643. if (it == nsec3_namespace_->end()) {
  644. return (IteratorContextPtr(new EmptyIteratorContext()));
  645. } else {
  646. return (IteratorContextPtr(new DomainIterator(it->second)));
  647. }
  648. }
  649. virtual pair<bool, int> startUpdateZone(const std::string& zone_name,
  650. bool replace)
  651. {
  652. const pair<bool, int> zone_info = getZone(zone_name);
  653. if (!zone_info.first) {
  654. return (pair<bool, int>(false, 0));
  655. }
  656. // Prepare the record set for update. If replacing the existing one,
  657. // we use an empty set; otherwise we use a writable copy of the
  658. // original.
  659. if (replace) {
  660. update_records_->clear();
  661. update_nsec3_namespace_->clear();
  662. } else {
  663. *update_records_ = *readonly_records_;
  664. *update_nsec3_namespace_ = nsec3_namespace_master_;
  665. }
  666. if (zone_name == "bad.example.org.") {
  667. return (pair<bool, int>(true, -1));
  668. } else if (zone_name == "null.example.org.") {
  669. return (pair<bool, int>(true, 13));
  670. } else {
  671. return (pair<bool, int>(true, WRITABLE_ZONE_ID));
  672. }
  673. }
  674. virtual void commit() {
  675. *readonly_records_ = *update_records_;
  676. *nsec3_namespace_ = *update_nsec3_namespace_;
  677. }
  678. virtual void rollback() {
  679. // Special hook: if something with a name of "throw.example.org"
  680. // has been added, trigger an imaginary unexpected event with an
  681. // exception.
  682. if (update_records_->count("throw.example.org.") > 0) {
  683. isc_throw(DataSourceError, "unexpected failure in rollback");
  684. }
  685. rollbacked_ = true;
  686. }
  687. private:
  688. // Common subroutine for addRecordToZone and addNSEC3RecordToZone.
  689. void addRecord(Domains& domains,
  690. const string (&columns)[ADD_COLUMN_COUNT])
  691. {
  692. // Copy the current value to cur_name. If it doesn't exist,
  693. // operator[] will create a new one.
  694. cur_name_ = domains[columns[ADD_NAME]];
  695. vector<string> record_columns;
  696. record_columns.push_back(columns[ADD_TYPE]);
  697. record_columns.push_back(columns[ADD_TTL]);
  698. record_columns.push_back(columns[ADD_SIGTYPE]);
  699. record_columns.push_back(columns[ADD_RDATA]);
  700. record_columns.push_back(columns[ADD_NAME]);
  701. // copy back the added entry
  702. cur_name_.push_back(record_columns);
  703. domains[columns[DatabaseAccessor::ADD_NAME]] = cur_name_;
  704. // remember this one so that test cases can check it.
  705. copy(columns, columns + DatabaseAccessor::ADD_COLUMN_COUNT,
  706. columns_lastadded_);
  707. }
  708. public:
  709. virtual void addRecordToZone(const string (&columns)[ADD_COLUMN_COUNT]) {
  710. addRecord(*update_records_, columns);
  711. }
  712. virtual void addNSEC3RecordToZone(
  713. const string (&columns)[ADD_NSEC3_COLUMN_COUNT])
  714. {
  715. // Convert the NSEC3 parameters in the normal (non NSEC3) style so
  716. // we can share the merge code, and then update using addRecord().
  717. string normal_columns[ADD_COLUMN_COUNT];
  718. normal_columns[ADD_TYPE] = columns[ADD_NSEC3_TYPE];
  719. normal_columns[ADD_TTL] = columns[ADD_NSEC3_TTL];
  720. normal_columns[ADD_SIGTYPE] = "";
  721. normal_columns[ADD_RDATA] = columns[ADD_NSEC3_RDATA];
  722. normal_columns[ADD_NAME] = columns[ADD_NSEC3_HASH];
  723. addRecord(*update_nsec3_namespace_, normal_columns);
  724. }
  725. private:
  726. // Helper predicate class used in deleteRecordInZone().
  727. struct deleteMatch {
  728. deleteMatch(const string& type, const string& rdata) :
  729. type_(type), rdata_(rdata)
  730. {}
  731. bool operator()(const vector<string>& row) const {
  732. return (row[0] == type_ && row[3] == rdata_);
  733. }
  734. const string& type_;
  735. const string& rdata_;
  736. };
  737. // Common subroutine for deleteRecordinZone and deleteNSEC3RecordInZone.
  738. void deleteRecord(Domains& domains,
  739. const string (&params)[DEL_PARAM_COUNT])
  740. {
  741. vector<vector<string> >& records =
  742. domains[params[DatabaseAccessor::DEL_NAME]];
  743. records.erase(remove_if(records.begin(), records.end(),
  744. deleteMatch(
  745. params[DatabaseAccessor::DEL_TYPE],
  746. params[DatabaseAccessor::DEL_RDATA])),
  747. records.end());
  748. if (records.empty()) {
  749. domains.erase(params[DatabaseAccessor::DEL_NAME]);
  750. }
  751. }
  752. public:
  753. virtual void deleteRecordInZone(const string (&params)[DEL_PARAM_COUNT]) {
  754. deleteRecord(*update_records_, params);
  755. }
  756. virtual void deleteNSEC3RecordInZone(
  757. const string (&params)[DEL_PARAM_COUNT])
  758. {
  759. deleteRecord(*update_nsec3_namespace_, params);
  760. }
  761. //
  762. // Helper methods to keep track of some update related activities
  763. //
  764. bool isRollbacked() const {
  765. return (rollbacked_);
  766. }
  767. const string* getLastAdded() const {
  768. return (columns_lastadded_);
  769. }
  770. // This allows the test code to get the accessor used in an update context
  771. boost::shared_ptr<const MockAccessor> getLatestClone() const {
  772. return (latest_clone_);
  773. }
  774. virtual std::string findPreviousName(int id, const std::string& rname)
  775. const
  776. {
  777. if (id == -1) {
  778. isc_throw(isc::NotImplemented, "Test not implemented behaviour");
  779. } else if (id == READONLY_ZONE_ID) {
  780. // For some specific names we intentionally return broken or
  781. // unexpected result.
  782. if (rname == "org.example.badnsec2.") {
  783. return ("badnsec1.example.org.");
  784. } else if (rname == "org.example.brokenname.") {
  785. return ("brokenname...example.org.");
  786. } else if (rname == "org.example.notimplnsec." ||
  787. rname == "org.example.wild.here.") {
  788. isc_throw(isc::NotImplemented, "Not implemented in this test");
  789. }
  790. // For the general case, we search for the first name N in the
  791. // domains that meets N >= reverse(rname) using lower_bound.
  792. // The "previous name" is the name of the previous entry of N.
  793. // Note that Domains are internally sorted by the Name comparison
  794. // order. Due to the API requirement we are given a reversed
  795. // name (rname), so we need to reverse it again to convert it
  796. // to the original name.
  797. Domains::const_iterator it(readonly_records_->lower_bound(
  798. Name(rname).reverse().toText()));
  799. if (it == readonly_records_->begin()) {
  800. isc_throw(isc::Unexpected, "Unexpected name");
  801. }
  802. if (it == readonly_records_->end()) {
  803. return ((*readonly_records_->rbegin()).first);
  804. }
  805. return ((*(--it)).first);
  806. } else {
  807. isc_throw(isc::Unexpected, "Unknown zone ID");
  808. }
  809. }
  810. virtual std::string findPreviousNSEC3Hash(int,
  811. const std::string& hash) const
  812. {
  813. // TODO: Provide some broken data, but it is not known yet how broken
  814. // they'll have to be.
  815. Domains::const_iterator it(nsec3_namespace_->lower_bound(hash));
  816. // We got just after the one we want
  817. if (it == nsec3_namespace_->begin()) {
  818. // Hmm, we got something really small. So we wrap around.
  819. // This is one after the last, so after decreasing it we'll get
  820. // the biggest.
  821. it = nsec3_namespace_->end();
  822. }
  823. return ((--it)->first);
  824. }
  825. virtual void addRecordDiff(int id, uint32_t serial,
  826. DiffOperation operation,
  827. const std::string (&data)[DIFF_PARAM_COUNT])
  828. {
  829. if (id == 13) { // The null zone doesn't support journaling
  830. isc_throw(isc::NotImplemented, "Test not implemented behaviour");
  831. } else if (id == -1) { // Bad zone throws
  832. isc_throw(DataSourceError, "Test error");
  833. } else {
  834. journal_entries_->push_back(JournalEntry(id, serial, operation,
  835. data));
  836. }
  837. }
  838. virtual IteratorContextPtr getDiffs(int id, uint32_t start,
  839. uint32_t end) const
  840. {
  841. vector<JournalEntry> selected_jnl;
  842. for (vector<JournalEntry>::const_iterator it =
  843. journal_entries_->begin();
  844. it != journal_entries_->end(); ++it)
  845. {
  846. // For simplicity we assume this method is called for the
  847. // "readonly" zone possibly after making updates on the "writable"
  848. // copy and committing them.
  849. if (id != READONLY_ZONE_ID) {
  850. continue;
  851. }
  852. // Note: the following logic is not 100% accurate in terms of
  853. // serial number arithmetic; we prefer brevity for testing.
  854. // Skip until we see the starting serial. Once we started
  855. // recording this condition is ignored (to support wrap-around
  856. // case). Also, it ignores the RR type; it only checks the
  857. // versions.
  858. if ((*it).serial_ < start && selected_jnl.empty()) {
  859. continue;
  860. }
  861. if ((*it).serial_ > end) { // gone over the end serial. we're done.
  862. break;
  863. }
  864. selected_jnl.push_back(*it);
  865. }
  866. // Check if we've found the requested range. If not, throw.
  867. if (selected_jnl.empty() || selected_jnl.front().serial_ != start ||
  868. selected_jnl.back().serial_ != end) {
  869. isc_throw(NoSuchSerial, "requested diff range is not found");
  870. }
  871. return (IteratorContextPtr(new MockDiffIteratorContext(selected_jnl)));
  872. }
  873. // Check the journal is as expected and clear the journal
  874. void checkJournal(const std::vector<JournalEntry> &expected) const {
  875. std::vector<JournalEntry> journal;
  876. // Clean the journal, but keep local copy to check
  877. journal.swap(*journal_entries_);
  878. ASSERT_EQ(expected.size(), journal.size());
  879. for (size_t i = 0; i < expected.size(); ++ i) {
  880. EXPECT_TRUE(expected[i] == journal[i]);
  881. }
  882. }
  883. private:
  884. // The following member variables are storage and/or update work space
  885. // of the test zone. The "master"s are the real objects that contain
  886. // the data, and they are shared among all accessors cloned from
  887. // an initially created one. The "copy" data will be used for read-only
  888. // transaction. The pointer members allow the sharing.
  889. // "readonly" is for normal lookups. "update" is the workspace for
  890. // updates. When update starts it will be initialized either as an
  891. // empty set (when replacing the entire zone) or as a copy of the
  892. // "readonly" one. "empty" is a sentinel to produce negative results.
  893. Domains readonly_records_master_;
  894. Domains readonly_records_copy_;
  895. Domains* readonly_records_;
  896. Domains update_records_master_;
  897. Domains* update_records_;
  898. Domains nsec3_namespace_master_;
  899. Domains* nsec3_namespace_;
  900. Domains update_nsec3_namespace_master_;
  901. Domains* update_nsec3_namespace_;
  902. const Domains empty_records_master_;
  903. const Domains* empty_records_;
  904. // The journal data
  905. std::vector<JournalEntry> journal_entries_master_;
  906. std::vector<JournalEntry>* journal_entries_;
  907. // used as temporary storage after searchForRecord() and during
  908. // getNextRecord() calls, as well as during the building of the
  909. // fake data
  910. std::vector< std::vector<std::string> > cur_name_;
  911. // The columns that were most recently added via addRecordToZone()
  912. string columns_lastadded_[ADD_COLUMN_COUNT];
  913. // Whether rollback operation has been performed for the database.
  914. // Not useful except for purely testing purpose.
  915. bool rollbacked_;
  916. // Remember the mock accessor that was last cloned
  917. boost::shared_ptr<MockAccessor> latest_clone_;
  918. // Internal flag for duplicate check
  919. bool did_transaction_;
  920. const Domains& getMockRecords(int zone_id) const {
  921. if (zone_id == READONLY_ZONE_ID) {
  922. return (*readonly_records_);
  923. } else if (zone_id == WRITABLE_ZONE_ID) {
  924. return (*update_records_);
  925. }
  926. return (*empty_records_);
  927. }
  928. // Adds one record to the current name in the database
  929. // The actual data will not be added to 'records' until
  930. // addCurName() is called
  931. void addRecord(const std::string& type,
  932. const std::string& ttl,
  933. const std::string& sigtype,
  934. const std::string& rdata) {
  935. std::vector<std::string> columns;
  936. columns.push_back(type);
  937. columns.push_back(ttl);
  938. columns.push_back(sigtype);
  939. columns.push_back(rdata);
  940. cur_name_.push_back(columns);
  941. }
  942. // Adds all records we just built with calls to addRecords
  943. // to the actual fake database. This will clear cur_name_,
  944. // so we can immediately start adding new records.
  945. void addCurName(const std::string& name) {
  946. ASSERT_EQ(0, readonly_records_->count(name));
  947. // Append the name to all of them
  948. for (std::vector<std::vector<std::string> >::iterator
  949. i = cur_name_.begin(); i != cur_name_.end(); ++ i) {
  950. i->push_back(name);
  951. }
  952. (*readonly_records_)[name] = cur_name_;
  953. cur_name_.clear();
  954. }
  955. // Works in a similar way to addCurName, but it is added to
  956. // the NSEC3 namespace. You don't provide the full name, only
  957. // the hash part.
  958. void addCurHash(const std::string& hash) {
  959. ASSERT_EQ(0, nsec3_namespace_->count(hash));
  960. // Append the name to all of them
  961. for (std::vector<std::vector<std::string> >::iterator
  962. i = cur_name_.begin(); i != cur_name_.end(); ++ i) {
  963. i->push_back(hash);
  964. }
  965. (*nsec3_namespace_)[hash] = cur_name_;
  966. cur_name_.clear();
  967. }
  968. // Fills the database with zone data.
  969. // This method constructs a number of resource records (with addRecord),
  970. // which will all be added for one domain name to the fake database
  971. // (with addCurName). So for instance the first set of calls create
  972. // data for the name 'www.example.org', which will consist of one A RRset
  973. // of one record, and one AAAA RRset of two records.
  974. // The order in which they are added is the order in which getNextRecord()
  975. // will return them (so we can test whether find() etc. support data that
  976. // might not come in 'normal' order)
  977. // It shall immediately fail if you try to add the same name twice.
  978. void fillData() {
  979. const char* prev_name = NULL;
  980. for (int i = 0; TEST_RECORDS[i][0] != NULL; ++i) {
  981. if (prev_name != NULL &&
  982. strcmp(prev_name, TEST_RECORDS[i][0]) != 0) {
  983. addCurName(prev_name);
  984. }
  985. prev_name = TEST_RECORDS[i][0];
  986. addRecord(TEST_RECORDS[i][1], TEST_RECORDS[i][2],
  987. TEST_RECORDS[i][3], TEST_RECORDS[i][4]);
  988. }
  989. addCurName(prev_name);
  990. prev_name = NULL;
  991. for (int i = 0; TEST_NSEC3_RECORDS[i][0] != NULL; ++i) {
  992. if (prev_name != NULL &&
  993. strcmp(prev_name, TEST_NSEC3_RECORDS[i][0]) != 0) {
  994. addCurHash(prev_name);
  995. }
  996. prev_name = TEST_NSEC3_RECORDS[i][0];
  997. addRecord(TEST_NSEC3_RECORDS[i][1], TEST_NSEC3_RECORDS[i][2],
  998. TEST_NSEC3_RECORDS[i][3], TEST_NSEC3_RECORDS[i][4]);
  999. }
  1000. addCurHash(prev_name);
  1001. }
  1002. public:
  1003. // This adds the NSEC3PARAM into the apex, so we can perform some NSEC3
  1004. // tests. Note that the NSEC3 namespace is available in other tests, but
  1005. // it should not be accessed at that time.
  1006. void enableNSEC3() {
  1007. for (int i = 0; TEST_NSEC3PARAM_RECORDS[i][0] != NULL; ++i) {
  1008. vector<string> param;
  1009. param.push_back(TEST_NSEC3PARAM_RECORDS[i][1]); // RRtype
  1010. param.push_back(TEST_NSEC3PARAM_RECORDS[i][2]); // TTL
  1011. param.push_back(""); // sigtype, unused
  1012. param.push_back(TEST_NSEC3PARAM_RECORDS[i][4]); // RDATA
  1013. param.push_back(TEST_NSEC3PARAM_RECORDS[i][0]); // owner name
  1014. (*readonly_records_)[param.back()].push_back(param);
  1015. }
  1016. }
  1017. };
  1018. // This tests the default getRecords behaviour, throwing NotImplemented
  1019. TEST(DatabaseConnectionTest, getRecords) {
  1020. EXPECT_THROW(NopAccessor().getRecords(".", 1, false),
  1021. isc::NotImplemented);
  1022. }
  1023. // This tests the default getAllRecords behaviour, throwing NotImplemented
  1024. TEST(DatabaseConnectionTest, getAllRecords) {
  1025. // The parameters don't matter
  1026. EXPECT_THROW(NopAccessor().getAllRecords(1),
  1027. isc::NotImplemented);
  1028. }
  1029. // This test fixture is templated so that we can share (most of) the test
  1030. // cases with different types of data sources. Note that in test cases
  1031. // we need to use 'this' to refer to member variables of the test class.
  1032. template <typename ACCESSOR_TYPE>
  1033. class DatabaseClientTest : public ::testing::Test {
  1034. public:
  1035. DatabaseClientTest() : zname_("example.org"), qname_("www.example.org"),
  1036. qclass_(RRClass::IN()), qtype_(RRType::A()),
  1037. rrttl_(3600)
  1038. {
  1039. createClient();
  1040. // set up the commonly used finder.
  1041. DataSourceClient::FindResult zone(client_->findZone(zname_));
  1042. assert(zone.code == result::SUCCESS);
  1043. finder_ = dynamic_pointer_cast<DatabaseClient::Finder>(
  1044. zone.zone_finder);
  1045. // Test IN/A RDATA to be added in update tests. Intentionally using
  1046. // different data than the initial data configured in the MockAccessor.
  1047. rrset_.reset(new RRset(qname_, qclass_, qtype_, rrttl_));
  1048. rrset_->addRdata(rdata::createRdata(rrset_->getType(),
  1049. rrset_->getClass(), "192.0.2.2"));
  1050. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  1051. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  1052. "ns1.example.org. admin.example.org. "
  1053. "1234 3600 1800 2419200 7200"));
  1054. // And its RRSIG. Also different from the configured one.
  1055. rrsigset_.reset(new RRset(qname_, qclass_, RRType::RRSIG(),
  1056. rrttl_));
  1057. rrsigset_->addRdata(rdata::createRdata(rrsigset_->getType(),
  1058. rrsigset_->getClass(),
  1059. "A 5 3 0 20000101000000 "
  1060. "20000201000000 0 example.org. "
  1061. "FAKEFAKEFAKE"));
  1062. }
  1063. ~ DatabaseClientTest() {
  1064. // Make sure we return the default creator no matter if we set it or not
  1065. setNSEC3HashCreator(NULL);
  1066. }
  1067. /*
  1068. * We initialize the client from a function, so we can call it multiple
  1069. * times per test.
  1070. */
  1071. void createClient() {
  1072. // To make sure we always have empty diffs table at the beginning of
  1073. // each test, we re-install the writable data source here.
  1074. // Note: this is SQLite3 specific and a waste (though otherwise
  1075. // harmless) for other types of data sources. If and when we support
  1076. // more types of data sources in this test framework, we should
  1077. // probably move this to some specialized templated method specific
  1078. // to SQLite3 (or for even a longer term we should add an API to
  1079. // purge the diffs table).
  1080. const char* const install_cmd = INSTALL_PROG " -c " TEST_DATA_COMMONDIR
  1081. "/rwtest.sqlite3 " TEST_DATA_BUILDDIR
  1082. "/rwtest.sqlite3.copied";
  1083. if (system(install_cmd) != 0) {
  1084. // any exception will do, this is failure in test setup, but nice
  1085. // to show the command that fails, and shouldn't be caught
  1086. isc_throw(isc::Exception,
  1087. "Error setting up; command failed: " << install_cmd);
  1088. }
  1089. current_accessor_ = new ACCESSOR_TYPE();
  1090. is_mock_ = (dynamic_cast<MockAccessor*>(current_accessor_) != NULL);
  1091. client_.reset(new DatabaseClient(qclass_,
  1092. boost::shared_ptr<ACCESSOR_TYPE>(
  1093. current_accessor_)));
  1094. }
  1095. /**
  1096. * Check the zone finder is a valid one and references the zone ID and
  1097. * database available here.
  1098. */
  1099. void checkZoneFinder(const DataSourceClient::FindResult& zone) {
  1100. ASSERT_NE(ZoneFinderPtr(), zone.zone_finder) << "No zone finder";
  1101. boost::shared_ptr<DatabaseClient::Finder> finder(
  1102. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  1103. ASSERT_NE(boost::shared_ptr<DatabaseClient::Finder>(), finder) <<
  1104. "Wrong type of finder";
  1105. if (is_mock_) {
  1106. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  1107. }
  1108. EXPECT_EQ(current_accessor_, &finder->getAccessor());
  1109. }
  1110. boost::shared_ptr<DatabaseClient::Finder> getFinder() {
  1111. DataSourceClient::FindResult zone(client_->findZone(zname_));
  1112. EXPECT_EQ(result::SUCCESS, zone.code);
  1113. boost::shared_ptr<DatabaseClient::Finder> finder(
  1114. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  1115. if (is_mock_) {
  1116. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  1117. }
  1118. return (finder);
  1119. }
  1120. // Helper methods for update tests
  1121. bool isRollbacked(bool expected = false) const {
  1122. if (is_mock_) {
  1123. const MockAccessor& mock_accessor =
  1124. dynamic_cast<const MockAccessor&>(*update_accessor_);
  1125. return (mock_accessor.isRollbacked());
  1126. } else {
  1127. return (expected);
  1128. }
  1129. }
  1130. void checkLastAdded(const char* const expected[]) const {
  1131. if (is_mock_) {
  1132. const MockAccessor* mock_accessor =
  1133. dynamic_cast<const MockAccessor*>(current_accessor_);
  1134. for (int i = 0; i < DatabaseAccessor::ADD_COLUMN_COUNT; ++i) {
  1135. EXPECT_EQ(expected[i],
  1136. mock_accessor->getLatestClone()->getLastAdded()[i]);
  1137. }
  1138. }
  1139. }
  1140. void setUpdateAccessor() {
  1141. if (is_mock_) {
  1142. const MockAccessor* mock_accessor =
  1143. dynamic_cast<const MockAccessor*>(current_accessor_);
  1144. update_accessor_ = mock_accessor->getLatestClone();
  1145. }
  1146. }
  1147. void checkJournal(const vector<JournalEntry>& expected) {
  1148. if (is_mock_) {
  1149. const MockAccessor* mock_accessor =
  1150. dynamic_cast<const MockAccessor*>(current_accessor_);
  1151. mock_accessor->checkJournal(expected);
  1152. } else {
  1153. // For other generic databases, retrieve the diff using the
  1154. // reader class and compare the resulting sequence of RRset.
  1155. // For simplicity we only consider the case where the expected
  1156. // sequence is not empty.
  1157. ASSERT_FALSE(expected.empty());
  1158. const Name zone_name(expected.front().
  1159. data_[DatabaseAccessor::DIFF_NAME]);
  1160. ZoneJournalReaderPtr jnl_reader =
  1161. client_->getJournalReader(zone_name,
  1162. expected.front().serial_,
  1163. expected.back().serial_).second;
  1164. ASSERT_TRUE(jnl_reader);
  1165. ConstRRsetPtr rrset;
  1166. vector<JournalEntry>::const_iterator it = expected.begin();
  1167. for (rrset = jnl_reader->getNextDiff();
  1168. rrset && it != expected.end();
  1169. rrset = jnl_reader->getNextDiff(), ++it) {
  1170. typedef DatabaseAccessor Accessor;
  1171. RRsetPtr expected_rrset(
  1172. new RRset(Name((*it).data_[Accessor::DIFF_NAME]),
  1173. qclass_,
  1174. RRType((*it).data_[Accessor::DIFF_TYPE]),
  1175. RRTTL((*it).data_[Accessor::DIFF_TTL])));
  1176. expected_rrset->addRdata(
  1177. rdata::createRdata(expected_rrset->getType(),
  1178. expected_rrset->getClass(),
  1179. (*it).data_[Accessor::DIFF_RDATA]));
  1180. rrsetCheck(expected_rrset, rrset);
  1181. }
  1182. // We should have examined all entries of both expected and
  1183. // actual data.
  1184. EXPECT_TRUE(it == expected.end());
  1185. ASSERT_FALSE(rrset);
  1186. }
  1187. }
  1188. // Mock-only; control whether to allow subsequent transaction.
  1189. void allowMoreTransaction(bool is_allowed) {
  1190. if (is_mock_) {
  1191. // Use a separate variable for MockAccessor&; some compilers
  1192. // would be confused otherwise.
  1193. MockAccessor& mock_accessor =
  1194. dynamic_cast<MockAccessor&>(*current_accessor_);
  1195. mock_accessor.allowMoreTransaction(is_allowed);
  1196. }
  1197. }
  1198. // Some tests only work for MockAccessor. We remember whether our accessor
  1199. // is of that type.
  1200. bool is_mock_;
  1201. // Will be deleted by client_, just keep the current value for comparison.
  1202. ACCESSOR_TYPE* current_accessor_;
  1203. boost::shared_ptr<DatabaseClient> client_;
  1204. const std::string database_name_;
  1205. // The zone finder of the test zone commonly used in various tests.
  1206. boost::shared_ptr<DatabaseClient::Finder> finder_;
  1207. // Some shortcut variables for commonly used test parameters
  1208. const Name zname_; // the zone name stored in the test data source
  1209. const Name qname_; // commonly used name to be found
  1210. const RRClass qclass_; // commonly used RR class used with qname
  1211. const RRType qtype_; // commonly used RR type used with qname
  1212. const RRTTL rrttl_; // commonly used RR TTL
  1213. RRsetPtr rrset_; // for adding/deleting an RRset
  1214. RRsetPtr rrsigset_; // for adding/deleting an RRset
  1215. RRsetPtr soa_; // for adding/deleting an RRset
  1216. // update related objects to be tested
  1217. ZoneUpdaterPtr updater_;
  1218. boost::shared_ptr<const DatabaseAccessor> update_accessor_;
  1219. // placeholders
  1220. const std::vector<std::string> empty_rdatas_; // for NXRRSET/NXDOMAIN
  1221. std::vector<std::string> expected_rdatas_;
  1222. std::vector<std::string> expected_sig_rdatas_;
  1223. // A creator for use in several NSEC3 related tests.
  1224. TestNSEC3HashCreator test_nsec3_hash_creator_;
  1225. };
  1226. class TestSQLite3Accessor : public SQLite3Accessor {
  1227. public:
  1228. TestSQLite3Accessor() : SQLite3Accessor(
  1229. TEST_DATA_BUILDDIR "/rwtest.sqlite3.copied", "IN")
  1230. {
  1231. startUpdateZone("example.org.", true);
  1232. string columns[ADD_COLUMN_COUNT];
  1233. for (int i = 0; TEST_RECORDS[i][0] != NULL; ++i) {
  1234. columns[ADD_NAME] = TEST_RECORDS[i][0];
  1235. columns[ADD_REV_NAME] = Name(columns[ADD_NAME]).reverse().toText();
  1236. columns[ADD_TYPE] = TEST_RECORDS[i][1];
  1237. columns[ADD_TTL] = TEST_RECORDS[i][2];
  1238. columns[ADD_SIGTYPE] = TEST_RECORDS[i][3];
  1239. columns[ADD_RDATA] = TEST_RECORDS[i][4];
  1240. addRecordToZone(columns);
  1241. }
  1242. // We don't add NSEC3s until we are explicitly told we need them
  1243. // in enableNSEC3(); these would break some non NSEC3 tests.
  1244. commit();
  1245. }
  1246. void enableNSEC3() {
  1247. startUpdateZone("example.org.", false);
  1248. // Add NSECPARAM at the zone origin
  1249. for (int i = 0; TEST_NSEC3PARAM_RECORDS[i][0] != NULL; ++i) {
  1250. const string param_columns[ADD_COLUMN_COUNT] = {
  1251. TEST_NSEC3PARAM_RECORDS[i][0], // name
  1252. Name(param_columns[ADD_NAME]).reverse().toText(), // revname
  1253. TEST_NSEC3PARAM_RECORDS[i][2], // TTL
  1254. TEST_NSEC3PARAM_RECORDS[i][1], // RR type
  1255. TEST_NSEC3PARAM_RECORDS[i][3], // sigtype
  1256. TEST_NSEC3PARAM_RECORDS[i][4] }; // RDATA
  1257. addRecordToZone(param_columns);
  1258. }
  1259. // Add NSEC3s
  1260. for (int i = 0; TEST_NSEC3_RECORDS[i][0] != NULL; ++i) {
  1261. const string nsec3_columns[ADD_NSEC3_COLUMN_COUNT] = {
  1262. Name(TEST_NSEC3_RECORDS[i][0]).split(0, 1).toText(true),
  1263. TEST_NSEC3_RECORDS[i][2], // TTL
  1264. TEST_NSEC3_RECORDS[i][1], // RR type
  1265. TEST_NSEC3_RECORDS[i][4] }; // RDATA
  1266. addNSEC3RecordToZone(nsec3_columns);
  1267. }
  1268. commit();
  1269. }
  1270. };
  1271. // The following two lines instantiate test cases with concrete accessor
  1272. // classes to be tested.
  1273. // XXX: clang++ installed on our FreeBSD buildbot cannot complete compiling
  1274. // this file, seemingly due to the size of the code. We'll consider more
  1275. // complete workaround, but for a short term workaround we'll reduce the
  1276. // number of tested accessor classes (thus reducing the amount of code
  1277. // to be compiled) for this particular environment.
  1278. #if defined(__clang__) && defined(__FreeBSD__)
  1279. typedef ::testing::Types<MockAccessor> TestAccessorTypes;
  1280. #else
  1281. typedef ::testing::Types<MockAccessor, TestSQLite3Accessor> TestAccessorTypes;
  1282. #endif
  1283. TYPED_TEST_CASE(DatabaseClientTest, TestAccessorTypes);
  1284. // In some cases the entire test fixture is for the mock accessor only.
  1285. // We use the usual TEST_F for them with the corresponding specialized class
  1286. // to make the code simpler.
  1287. typedef DatabaseClientTest<MockAccessor> MockDatabaseClientTest;
  1288. TYPED_TEST(DatabaseClientTest, zoneNotFound) {
  1289. DataSourceClient::FindResult zone(
  1290. this->client_->findZone(Name("example.com")));
  1291. EXPECT_EQ(result::NOTFOUND, zone.code);
  1292. }
  1293. TYPED_TEST(DatabaseClientTest, exactZone) {
  1294. DataSourceClient::FindResult zone(
  1295. this->client_->findZone(Name("example.org")));
  1296. EXPECT_EQ(result::SUCCESS, zone.code);
  1297. this->checkZoneFinder(zone);
  1298. }
  1299. TYPED_TEST(DatabaseClientTest, superZone) {
  1300. DataSourceClient::FindResult zone(this->client_->findZone(Name(
  1301. "sub.example.org")));
  1302. EXPECT_EQ(result::PARTIALMATCH, zone.code);
  1303. this->checkZoneFinder(zone);
  1304. }
  1305. // This test doesn't depend on derived accessor class, so we use TEST().
  1306. TEST(GenericDatabaseClientTest, noAccessorException) {
  1307. // We need a dummy variable here; some compiler would regard it a mere
  1308. // declaration instead of an instantiation and make the test fail.
  1309. EXPECT_THROW(DatabaseClient dummy(RRClass::IN(),
  1310. boost::shared_ptr<DatabaseAccessor>()),
  1311. isc::InvalidParameter);
  1312. }
  1313. // If the zone doesn't exist, exception is thrown
  1314. TYPED_TEST(DatabaseClientTest, noZoneIterator) {
  1315. EXPECT_THROW(this->client_->getIterator(Name("example.com")),
  1316. DataSourceError);
  1317. }
  1318. // If the zone doesn't exist and iteration is not implemented, it still throws
  1319. // the exception it doesn't exist
  1320. TEST(GenericDatabaseClientTest, noZoneNotImplementedIterator) {
  1321. EXPECT_THROW(DatabaseClient(RRClass::IN(),
  1322. boost::shared_ptr<DatabaseAccessor>(
  1323. new NopAccessor())).getIterator(
  1324. Name("example.com")),
  1325. DataSourceError);
  1326. }
  1327. TEST(GenericDatabaseClientTest, notImplementedIterator) {
  1328. EXPECT_THROW(DatabaseClient(RRClass::IN(),
  1329. boost::shared_ptr<DatabaseAccessor>(
  1330. new NopAccessor())).getIterator(Name("example.org")),
  1331. isc::NotImplemented);
  1332. }
  1333. // Pretend a bug in the connection and pass NULL as the context
  1334. // Should not crash, but gracefully throw. Works for the mock accessor only.
  1335. TEST_F(MockDatabaseClientTest, nullIteratorContext) {
  1336. EXPECT_THROW(this->client_->getIterator(Name("null.example.org")),
  1337. isc::Unexpected);
  1338. }
  1339. // It doesn't crash or anything if the zone is completely empty.
  1340. // Works for the mock accessor only.
  1341. TEST_F(MockDatabaseClientTest, emptyIterator) {
  1342. ZoneIteratorPtr it(this->client_->getIterator(Name("empty.example.org")));
  1343. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  1344. // This is past the end, it should throw
  1345. EXPECT_THROW(it->getNextRRset(), isc::Unexpected);
  1346. }
  1347. // checks if the given rrset matches the
  1348. // given name, class, type and rdatas
  1349. void
  1350. checkRRset(isc::dns::ConstRRsetPtr rrset,
  1351. const isc::dns::Name& name,
  1352. const isc::dns::RRClass& rrclass,
  1353. const isc::dns::RRType& rrtype,
  1354. const isc::dns::RRTTL& rrttl,
  1355. const std::vector<std::string>& rdatas) {
  1356. isc::dns::RRsetPtr expected_rrset(
  1357. new isc::dns::RRset(name, rrclass, rrtype, rrttl));
  1358. for (unsigned int i = 0; i < rdatas.size(); ++i) {
  1359. expected_rrset->addRdata(
  1360. isc::dns::rdata::createRdata(rrtype, rrclass,
  1361. rdatas[i]));
  1362. }
  1363. rrsetCheck(expected_rrset, rrset);
  1364. }
  1365. // Iterate through a zone, common case
  1366. TYPED_TEST(DatabaseClientTest, iterator) {
  1367. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  1368. ConstRRsetPtr rrset(it->getNextRRset());
  1369. ASSERT_NE(ConstRRsetPtr(), rrset);
  1370. // The first name should be the zone origin.
  1371. EXPECT_EQ(this->zname_, rrset->getName());
  1372. }
  1373. // Supplemental structure used in the couple of tests below. It represents
  1374. // parameters of an expected RRset containing up to two RDATAs. If it contains
  1375. // only one RDATA, rdata2 is NULL.
  1376. struct ExpectedRRset {
  1377. const char* const name;
  1378. const RRType rrtype;
  1379. const RRTTL rrttl;
  1380. const char* const rdata1;
  1381. const char* const rdata2;
  1382. };
  1383. // Common checker for the iterator tests below. It extracts RRsets from the
  1384. // give iterator and compare them to the expected sequence.
  1385. void
  1386. checkIteratorSequence(ZoneIterator& it, ExpectedRRset expected_sequence[],
  1387. size_t num_rrsets)
  1388. {
  1389. vector<string> expected_rdatas;
  1390. for (size_t i = 0; i < num_rrsets; ++i) {
  1391. const ConstRRsetPtr rrset = it.getNextRRset();
  1392. ASSERT_TRUE(rrset);
  1393. expected_rdatas.clear();
  1394. expected_rdatas.push_back(expected_sequence[i].rdata1);
  1395. if (expected_sequence[i].rdata2 != NULL) {
  1396. expected_rdatas.push_back(expected_sequence[i].rdata2);
  1397. }
  1398. checkRRset(rrset, Name(expected_sequence[i].name), RRClass::IN(),
  1399. expected_sequence[i].rrtype, expected_sequence[i].rrttl,
  1400. expected_rdatas);
  1401. }
  1402. EXPECT_FALSE(it.getNextRRset());
  1403. }
  1404. TEST_F(MockDatabaseClientTest, iterator) {
  1405. // This version of test creates an iterator that combines same types of
  1406. // RRs into single RRsets.
  1407. ExpectedRRset expected_sequence[] = {
  1408. {"example.org", RRType::A(), rrttl_, "192.0.2.1", NULL},
  1409. {"example.org", RRType::SOA(), rrttl_,
  1410. "ns1.example.org. admin.example.org. 1234 3600 1800 2419200 7200",
  1411. NULL},
  1412. {"x.example.org", RRType::A(), RRTTL(300), "192.0.2.1", "192.0.2.2"},
  1413. {"x.example.org", RRType::AAAA(), RRTTL(300),
  1414. "2001:db8::1", "2001:db8::2"},
  1415. {"x.example.org", RRType::RRSIG(), RRTTL(300),
  1416. "A 5 3 3600 20000101000000 20000201000000 12345 example.org. "
  1417. "FAKEFAKEFAKE", NULL},
  1418. {"x.example.org", RRType::RRSIG(), RRTTL(300),
  1419. "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. "
  1420. "FAKEFAKEFAKEFAKE", NULL},
  1421. {"ttldiff.example.org", RRType::A(), RRTTL(300),
  1422. "192.0.2.1", "192.0.2.2"},
  1423. {"ttldiff2.example.org", RRType::AAAA(), RRTTL(300),
  1424. "2001:db8::1", "2001:db8::2"}
  1425. };
  1426. checkIteratorSequence(*client_->getIterator(Name("example.org")),
  1427. expected_sequence,
  1428. sizeof(expected_sequence) /
  1429. sizeof(expected_sequence[0]));
  1430. }
  1431. TEST_F(MockDatabaseClientTest, iteratorSeparateRRs) {
  1432. // This version of test creates an iterator that separates all RRs as
  1433. // individual RRsets. In particular, it preserves the TTLs of an RRset
  1434. // even if they are different.
  1435. ExpectedRRset expected_sequence[] = {
  1436. {"example.org", RRType::A(), rrttl_, "192.0.2.1", NULL},
  1437. {"example.org", RRType::SOA(), rrttl_,
  1438. "ns1.example.org. admin.example.org. 1234 3600 1800 2419200 7200",
  1439. NULL},
  1440. {"x.example.org", RRType::A(), RRTTL(300), "192.0.2.1", NULL},
  1441. {"x.example.org", RRType::A(), RRTTL(300), "192.0.2.2", NULL},
  1442. {"x.example.org", RRType::AAAA(), RRTTL(300), "2001:db8::1", NULL},
  1443. {"x.example.org", RRType::AAAA(), RRTTL(300), "2001:db8::2", NULL},
  1444. {"x.example.org", RRType::RRSIG(), RRTTL(300),
  1445. "A 5 3 3600 20000101000000 20000201000000 12345 example.org. "
  1446. "FAKEFAKEFAKE", NULL},
  1447. {"x.example.org", RRType::RRSIG(), RRTTL(300),
  1448. "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. "
  1449. "FAKEFAKEFAKEFAKE", NULL},
  1450. {"ttldiff.example.org", RRType::A(), RRTTL(300), "192.0.2.1", NULL},
  1451. {"ttldiff.example.org", RRType::A(), RRTTL(600), "192.0.2.2", NULL},
  1452. {"ttldiff2.example.org", RRType::AAAA(), RRTTL(600), "2001:db8::1",
  1453. NULL},
  1454. {"ttldiff2.example.org", RRType::AAAA(), RRTTL(300), "2001:db8::2",
  1455. NULL}
  1456. };
  1457. checkIteratorSequence(*client_->getIterator(Name("example.org"), true),
  1458. expected_sequence,
  1459. sizeof(expected_sequence) /
  1460. sizeof(expected_sequence[0]));
  1461. }
  1462. // This has inconsistent TTL in the set (the rest, like nonsense in
  1463. // the data is handled in rdata itself). Works for the mock accessor only.
  1464. TEST_F(MockDatabaseClientTest, badIterator) {
  1465. // It should not throw, but get the lowest one of them
  1466. ZoneIteratorPtr it(this->client_->getIterator(Name("bad.example.org")));
  1467. EXPECT_EQ(it->getNextRRset()->getTTL(), isc::dns::RRTTL(300));
  1468. }
  1469. TYPED_TEST(DatabaseClientTest, getSOAFromIterator) {
  1470. vector<string> soa_data;
  1471. soa_data.push_back("ns1.example.org. admin.example.org. "
  1472. "1234 3600 1800 2419200 7200");
  1473. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1474. ASSERT_TRUE(it);
  1475. checkRRset(it->getSOA(), this->zname_, this->qclass_, RRType::SOA(),
  1476. this->rrttl_, soa_data);
  1477. // Iterate over the zone until we find an SOA. Although there's a broken
  1478. // RDATA that would trigger an exception in getNextRRset(), we should
  1479. // reach the SOA as the sequence should be sorted and the SOA is at
  1480. // the origin name (which has no bogus data).
  1481. ConstRRsetPtr rrset;
  1482. while ((rrset = it->getNextRRset()) != ConstRRsetPtr() &&
  1483. rrset->getType() != RRType::SOA()) {
  1484. ;
  1485. }
  1486. ASSERT_TRUE(rrset);
  1487. // It should be identical to the result of getSOA().
  1488. rrsetCheck(it->getSOA(), rrset);
  1489. }
  1490. TYPED_TEST(DatabaseClientTest, noSOAFromIterator) {
  1491. // First, empty the zone.
  1492. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1493. this->updater_->commit();
  1494. // Then getSOA() should return NULL.
  1495. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1496. ASSERT_TRUE(it);
  1497. EXPECT_FALSE(it->getSOA());
  1498. }
  1499. TYPED_TEST(DatabaseClientTest, iterateThenUpdate) {
  1500. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1501. ASSERT_TRUE(it);
  1502. // Try to empty the zone after getting the iterator. Depending on the
  1503. // underlying data source, it may result in an exception due to the
  1504. // transaction for the iterator. In either case the integrity of the
  1505. // iterator result should be reserved.
  1506. try {
  1507. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1508. this->updater_->commit();
  1509. // Confirm at least it doesn't contain any SOA
  1510. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1511. this->getFinder()->find(this->zname_, RRType::SOA())->code);
  1512. } catch (const DataSourceError&) {}
  1513. ConstRRsetPtr rrset;
  1514. while ((rrset = it->getNextRRset()) != ConstRRsetPtr() &&
  1515. rrset->getType() != RRType::SOA()) {
  1516. ;
  1517. }
  1518. ASSERT_TRUE(rrset);
  1519. // It should be identical to the result of getSOA().
  1520. rrsetCheck(it->getSOA(), rrset);
  1521. }
  1522. TYPED_TEST(DatabaseClientTest, updateThenIterateThenUpdate) {
  1523. // First clear the zone.
  1524. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1525. this->updater_->commit();
  1526. // Then iterate over it. It should immediately reach the end, at which
  1527. // point the transaction should be committed.
  1528. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1529. ASSERT_TRUE(it);
  1530. EXPECT_FALSE(it->getNextRRset());
  1531. // So another update attempt should succeed, too.
  1532. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1533. this->updater_->commit();
  1534. }
  1535. TYPED_TEST(DatabaseClientTest, updateAfterDeleteIterator) {
  1536. // Similar to the previous case, but we delete the iterator in the
  1537. // middle of zone. The transaction should be canceled (actually no
  1538. // different from commit though) at that point.
  1539. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1540. ASSERT_TRUE(it);
  1541. EXPECT_TRUE(it->getNextRRset());
  1542. it.reset();
  1543. // So another update attempt should succeed.
  1544. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1545. this->updater_->commit();
  1546. }
  1547. void
  1548. findTestCommon(ZoneFinder& finder, const isc::dns::Name& name,
  1549. const isc::dns::RRType& type,
  1550. ConstZoneFinderContextPtr actual_result,
  1551. const isc::dns::RRType& expected_type,
  1552. const isc::dns::RRTTL expected_ttl,
  1553. ZoneFinder::Result expected_result,
  1554. const std::vector<string>& expected_rdatas,
  1555. const std::vector<string>& expected_sig_rdatas,
  1556. ZoneFinder::FindResultFlags expected_flags,
  1557. const isc::dns::Name& expected_name,
  1558. const ZoneFinder::FindOptions options)
  1559. {
  1560. ASSERT_EQ(expected_result, actual_result->code) << name << " " << type;
  1561. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_WILDCARD) != 0,
  1562. actual_result->isWildcard());
  1563. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0,
  1564. actual_result->isNSECSigned());
  1565. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0,
  1566. actual_result->isNSEC3Signed());
  1567. if (!expected_rdatas.empty() && actual_result->rrset) {
  1568. checkRRset(actual_result->rrset,
  1569. expected_name != Name::ROOT_NAME() ? expected_name :
  1570. name, finder.getClass(), expected_type, expected_ttl,
  1571. expected_rdatas);
  1572. if ((options & ZoneFinder::FIND_DNSSEC) == ZoneFinder::FIND_DNSSEC) {
  1573. if (!expected_sig_rdatas.empty() &&
  1574. actual_result->rrset->getRRsig()) {
  1575. checkRRset(actual_result->rrset->getRRsig(),
  1576. expected_name != Name::ROOT_NAME() ?
  1577. expected_name : name,
  1578. finder.getClass(),
  1579. isc::dns::RRType::RRSIG(), expected_ttl,
  1580. expected_sig_rdatas);
  1581. } else if (expected_sig_rdatas.empty()) {
  1582. EXPECT_EQ(isc::dns::RRsetPtr(),
  1583. actual_result->rrset->getRRsig()) <<
  1584. "Unexpected RRSIG: " <<
  1585. actual_result->rrset->getRRsig()->toText();
  1586. } else {
  1587. ADD_FAILURE() << "Missing RRSIG";
  1588. }
  1589. } else if (actual_result->rrset->getRRsig()) {
  1590. EXPECT_EQ(isc::dns::RRsetPtr(), actual_result->rrset->getRRsig())
  1591. << "Unexpected RRSIG: "
  1592. << actual_result->rrset->getRRsig()->toText();
  1593. }
  1594. } else if (expected_rdatas.empty()) {
  1595. EXPECT_EQ(isc::dns::RRsetPtr(), actual_result->rrset) <<
  1596. "Unexpected RRset: " << actual_result->rrset->toText();
  1597. } else {
  1598. ADD_FAILURE() << "Missing result";
  1599. }
  1600. }
  1601. void
  1602. doFindTest(ZoneFinder& finder,
  1603. const isc::dns::Name& name,
  1604. const isc::dns::RRType& type,
  1605. const isc::dns::RRType& expected_type,
  1606. const isc::dns::RRTTL expected_ttl,
  1607. ZoneFinder::Result expected_result,
  1608. const std::vector<std::string>& expected_rdatas,
  1609. const std::vector<std::string>& expected_sig_rdatas,
  1610. ZoneFinder::FindResultFlags expected_flags =
  1611. ZoneFinder::RESULT_DEFAULT,
  1612. const isc::dns::Name& expected_name = isc::dns::Name::ROOT_NAME(),
  1613. const ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT)
  1614. {
  1615. SCOPED_TRACE("doFindTest " + name.toText() + " " + type.toText());
  1616. ConstZoneFinderContextPtr result = finder.find(name, type, options);
  1617. findTestCommon(finder, name, type, result, expected_type, expected_ttl,
  1618. expected_result, expected_rdatas, expected_sig_rdatas,
  1619. expected_flags, expected_name, options);
  1620. }
  1621. void
  1622. doFindAtOriginTest(ZoneFinder& finder,
  1623. const isc::dns::Name& origin,
  1624. const isc::dns::RRType& type,
  1625. const isc::dns::RRType& expected_type,
  1626. const isc::dns::RRTTL expected_ttl,
  1627. ZoneFinder::Result expected_result,
  1628. const std::vector<std::string>& expected_rdatas,
  1629. const std::vector<std::string>& expected_sig_rdatas,
  1630. ZoneFinder::FindResultFlags expected_flags =
  1631. ZoneFinder::RESULT_DEFAULT,
  1632. const isc::dns::Name& expected_name =
  1633. isc::dns::Name::ROOT_NAME(),
  1634. const ZoneFinder::FindOptions options =
  1635. ZoneFinder::FIND_DEFAULT)
  1636. {
  1637. SCOPED_TRACE("doFindOriginTest " + origin.toText() + " " + type.toText());
  1638. ConstZoneFinderContextPtr result =
  1639. finder.findAtOrigin(type, false, options);
  1640. findTestCommon(finder, origin, type, result, expected_type, expected_ttl,
  1641. expected_result, expected_rdatas, expected_sig_rdatas,
  1642. expected_flags, expected_name, options);
  1643. }
  1644. void
  1645. doFindAllTestResult(ZoneFinder& finder, const isc::dns::Name& name,
  1646. ZoneFinder::Result expected_result,
  1647. const isc::dns::RRType expected_type,
  1648. std::vector<std::string> expected_rdata,
  1649. const isc::dns::Name& expected_name =
  1650. isc::dns::Name::ROOT_NAME(),
  1651. const ZoneFinder::FindOptions options =
  1652. ZoneFinder::FIND_DEFAULT,
  1653. ZoneFinder::FindResultFlags expected_flags =
  1654. ZoneFinder::RESULT_DEFAULT)
  1655. {
  1656. SCOPED_TRACE("All test for " + name.toText());
  1657. std::vector<ConstRRsetPtr> target;
  1658. ConstZoneFinderContextPtr result(finder.findAll(name, target, options));
  1659. EXPECT_TRUE(target.empty());
  1660. EXPECT_EQ(expected_result, result->code);
  1661. EXPECT_EQ(expected_type, result->rrset->getType());
  1662. if (expected_flags != ZoneFinder::RESULT_DEFAULT){
  1663. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_WILDCARD) != 0,
  1664. result->isWildcard());
  1665. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0,
  1666. result->isNSECSigned());
  1667. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0,
  1668. result->isNSEC3Signed());
  1669. }
  1670. RdataIteratorPtr it(result->rrset->getRdataIterator());
  1671. std::vector<std::string> rdata;
  1672. while (!it->isLast()) {
  1673. rdata.push_back(it->getCurrent().toText());
  1674. it->next();
  1675. }
  1676. std::sort(rdata.begin(), rdata.end());
  1677. std::sort(expected_rdata.begin(), expected_rdata.end());
  1678. ASSERT_EQ(expected_rdata.size(), rdata.size());
  1679. for (size_t i(0); i < expected_rdata.size(); ++ i) {
  1680. EXPECT_EQ(expected_rdata[i], rdata[i]);
  1681. }
  1682. EXPECT_TRUE(expected_rdata == rdata);
  1683. EXPECT_EQ(expected_name == isc::dns::Name::ROOT_NAME() ? name :
  1684. expected_name, result->rrset->getName());
  1685. }
  1686. // When asking for an RRset where RRs somehow have different TTLs, it should
  1687. // convert to the lowest one.
  1688. TEST_F(MockDatabaseClientTest, ttldiff) {
  1689. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  1690. // Walk through the full iterator, we should see 1 rrset with name
  1691. // ttldiff1.example.org., and two rdatas. Same for ttldiff2
  1692. Name name("ttldiff.example.org.");
  1693. bool found = false;
  1694. //bool found2 = false;
  1695. ConstRRsetPtr rrset = it->getNextRRset();
  1696. while(rrset != ConstRRsetPtr()) {
  1697. if (rrset->getName() == name) {
  1698. ASSERT_FALSE(found);
  1699. ASSERT_EQ(2, rrset->getRdataCount());
  1700. ASSERT_EQ(RRTTL(300), rrset->getTTL());
  1701. found = true;
  1702. }
  1703. rrset = it->getNextRRset();
  1704. }
  1705. ASSERT_TRUE(found);
  1706. }
  1707. // Unless we ask for individual RRs in our iterator request. In that case
  1708. // every RR should go into its own 'rrset'
  1709. TEST_F(MockDatabaseClientTest, ttldiff_separate_rrs) {
  1710. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org"), true));
  1711. // Walk through the full iterator, we should see 1 rrset with name
  1712. // ttldiff1.example.org., and two rdatas. Same for ttldiff2
  1713. Name name("ttldiff.example.org.");
  1714. int found1 = false;
  1715. int found2 = false;
  1716. ConstRRsetPtr rrset = it->getNextRRset();
  1717. while(rrset != ConstRRsetPtr()) {
  1718. if (rrset->getName() == name) {
  1719. ASSERT_EQ(1, rrset->getRdataCount());
  1720. // We should find 1 'rrset' with TTL 300 and one with TTL 600
  1721. if (rrset->getTTL() == RRTTL(300)) {
  1722. ASSERT_FALSE(found1);
  1723. found1 = true;
  1724. } else if (rrset->getTTL() == RRTTL(600)) {
  1725. ASSERT_FALSE(found2);
  1726. found2 = true;
  1727. } else {
  1728. FAIL() << "Found unexpected TTL: " <<
  1729. rrset->getTTL().toText();
  1730. }
  1731. }
  1732. rrset = it->getNextRRset();
  1733. }
  1734. ASSERT_TRUE(found1);
  1735. ASSERT_TRUE(found2);
  1736. }
  1737. TYPED_TEST(DatabaseClientTest, find) {
  1738. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1739. this->expected_rdatas_.clear();
  1740. this->expected_sig_rdatas_.clear();
  1741. this->expected_rdatas_.push_back("192.0.2.1");
  1742. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1743. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1744. this->expected_rdatas_, this->expected_sig_rdatas_);
  1745. this->expected_rdatas_.clear();
  1746. this->expected_sig_rdatas_.clear();
  1747. this->expected_rdatas_.push_back("192.0.2.1");
  1748. this->expected_rdatas_.push_back("192.0.2.2");
  1749. doFindTest(*finder, isc::dns::Name("www2.example.org."),
  1750. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1751. this->expected_rdatas_, this->expected_sig_rdatas_);
  1752. this->expected_rdatas_.clear();
  1753. this->expected_sig_rdatas_.clear();
  1754. this->expected_rdatas_.push_back("2001:db8::1");
  1755. this->expected_rdatas_.push_back("2001:db8::2");
  1756. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1757. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1758. this->rrttl_,
  1759. ZoneFinder::SUCCESS,
  1760. this->expected_rdatas_, this->expected_sig_rdatas_);
  1761. this->expected_rdatas_.clear();
  1762. this->expected_sig_rdatas_.clear();
  1763. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1764. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
  1765. this->rrttl_,
  1766. ZoneFinder::NXRRSET,
  1767. this->expected_rdatas_, this->expected_sig_rdatas_);
  1768. this->expected_rdatas_.clear();
  1769. this->expected_sig_rdatas_.clear();
  1770. this->expected_rdatas_.push_back("www.example.org.");
  1771. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  1772. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1773. ZoneFinder::CNAME, this->expected_rdatas_,
  1774. this->expected_sig_rdatas_);
  1775. this->expected_rdatas_.clear();
  1776. this->expected_sig_rdatas_.clear();
  1777. this->expected_rdatas_.push_back("www.example.org.");
  1778. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  1779. isc::dns::RRType::CNAME(), isc::dns::RRType::CNAME(),
  1780. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1781. this->expected_sig_rdatas_);
  1782. this->expected_rdatas_.clear();
  1783. this->expected_sig_rdatas_.clear();
  1784. doFindTest(*finder, isc::dns::Name("doesnotexist.example.org."),
  1785. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1786. this->expected_rdatas_, this->expected_sig_rdatas_);
  1787. this->expected_rdatas_.clear();
  1788. this->expected_sig_rdatas_.clear();
  1789. this->expected_rdatas_.push_back("192.0.2.1");
  1790. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1791. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1792. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1793. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1794. this->expected_rdatas_, this->expected_sig_rdatas_);
  1795. this->expected_rdatas_.clear();
  1796. this->expected_sig_rdatas_.clear();
  1797. this->expected_rdatas_.push_back("2001:db8::1");
  1798. this->expected_rdatas_.push_back("2001:db8::2");
  1799. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1800. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1801. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1802. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1803. this->expected_sig_rdatas_);
  1804. this->expected_rdatas_.clear();
  1805. this->expected_sig_rdatas_.clear();
  1806. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1807. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1808. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1809. this->expected_sig_rdatas_);
  1810. this->expected_rdatas_.clear();
  1811. this->expected_sig_rdatas_.clear();
  1812. this->expected_rdatas_.push_back("www.example.org.");
  1813. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1814. doFindTest(*finder, isc::dns::Name("signedcname1.example.org."),
  1815. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1816. ZoneFinder::CNAME, this->expected_rdatas_,
  1817. this->expected_sig_rdatas_);
  1818. this->expected_rdatas_.clear();
  1819. this->expected_sig_rdatas_.clear();
  1820. this->expected_rdatas_.push_back("192.0.2.1");
  1821. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1822. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1823. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1824. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1825. this->expected_rdatas_, this->expected_sig_rdatas_);
  1826. this->expected_rdatas_.clear();
  1827. this->expected_sig_rdatas_.clear();
  1828. this->expected_rdatas_.push_back("2001:db8::2");
  1829. this->expected_rdatas_.push_back("2001:db8::1");
  1830. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1831. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1832. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1833. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1834. this->expected_sig_rdatas_);
  1835. this->expected_rdatas_.clear();
  1836. this->expected_sig_rdatas_.clear();
  1837. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1838. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1839. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1840. this->expected_sig_rdatas_);
  1841. this->expected_rdatas_.clear();
  1842. this->expected_sig_rdatas_.clear();
  1843. this->expected_rdatas_.push_back("www.example.org.");
  1844. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1845. doFindTest(*finder, isc::dns::Name("signedcname2.example.org."),
  1846. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1847. ZoneFinder::CNAME, this->expected_rdatas_,
  1848. this->expected_sig_rdatas_);
  1849. this->expected_rdatas_.clear();
  1850. this->expected_sig_rdatas_.clear();
  1851. this->expected_rdatas_.push_back("192.0.2.1");
  1852. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1853. doFindTest(*finder, isc::dns::Name("acnamesig1.example.org."),
  1854. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1855. this->expected_rdatas_, this->expected_sig_rdatas_);
  1856. this->expected_rdatas_.clear();
  1857. this->expected_sig_rdatas_.clear();
  1858. this->expected_rdatas_.push_back("192.0.2.1");
  1859. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1860. doFindTest(*finder, isc::dns::Name("acnamesig2.example.org."),
  1861. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1862. this->expected_rdatas_, this->expected_sig_rdatas_);
  1863. this->expected_rdatas_.clear();
  1864. this->expected_sig_rdatas_.clear();
  1865. this->expected_rdatas_.push_back("192.0.2.1");
  1866. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1867. doFindTest(*finder, isc::dns::Name("acnamesig3.example.org."),
  1868. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1869. this->expected_rdatas_, this->expected_sig_rdatas_);
  1870. this->expected_rdatas_.clear();
  1871. this->expected_sig_rdatas_.clear();
  1872. this->expected_rdatas_.push_back("192.0.2.1");
  1873. this->expected_rdatas_.push_back("192.0.2.2");
  1874. doFindTest(*finder, isc::dns::Name("ttldiff1.example.org."),
  1875. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1876. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1877. this->expected_sig_rdatas_);
  1878. this->expected_rdatas_.clear();
  1879. this->expected_sig_rdatas_.clear();
  1880. this->expected_rdatas_.push_back("192.0.2.1");
  1881. this->expected_rdatas_.push_back("192.0.2.2");
  1882. doFindTest(*finder, isc::dns::Name("ttldiff2.example.org."),
  1883. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1884. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1885. this->expected_sig_rdatas_);
  1886. EXPECT_THROW(finder->find(isc::dns::Name("badcname1.example.org."),
  1887. this->qtype_,
  1888. ZoneFinder::FIND_DEFAULT),
  1889. DataSourceError);
  1890. EXPECT_THROW(finder->find(isc::dns::Name("badcname2.example.org."),
  1891. this->qtype_,
  1892. ZoneFinder::FIND_DEFAULT),
  1893. DataSourceError);
  1894. EXPECT_THROW(finder->find(isc::dns::Name("badcname3.example.org."),
  1895. this->qtype_,
  1896. ZoneFinder::FIND_DEFAULT),
  1897. DataSourceError);
  1898. EXPECT_THROW(finder->find(isc::dns::Name("badrdata.example.org."),
  1899. this->qtype_,
  1900. ZoneFinder::FIND_DEFAULT),
  1901. DataSourceError);
  1902. EXPECT_THROW(finder->find(isc::dns::Name("badtype.example.org."),
  1903. this->qtype_,
  1904. ZoneFinder::FIND_DEFAULT),
  1905. DataSourceError);
  1906. EXPECT_THROW(finder->find(isc::dns::Name("badttl.example.org."),
  1907. this->qtype_,
  1908. ZoneFinder::FIND_DEFAULT),
  1909. DataSourceError);
  1910. EXPECT_THROW(finder->find(isc::dns::Name("badsig.example.org."),
  1911. this->qtype_,
  1912. ZoneFinder::FIND_DEFAULT),
  1913. DataSourceError);
  1914. // Trigger the hardcoded exceptions and see if find() has cleaned up
  1915. if (this->is_mock_) {
  1916. EXPECT_THROW(finder->find(Name("dsexception.example.org."),
  1917. this->qtype_,
  1918. ZoneFinder::FIND_DEFAULT),
  1919. DataSourceError);
  1920. EXPECT_THROW(finder->find(Name("iscexception.example.org."),
  1921. this->qtype_,
  1922. ZoneFinder::FIND_DEFAULT),
  1923. isc::Exception);
  1924. EXPECT_THROW(finder->find(Name("basicexception.example.org."),
  1925. this->qtype_,
  1926. ZoneFinder::FIND_DEFAULT),
  1927. std::exception);
  1928. EXPECT_THROW(finder->find(Name("dsexception.getnext.example.org"),
  1929. this->qtype_,
  1930. ZoneFinder::FIND_DEFAULT),
  1931. DataSourceError);
  1932. EXPECT_THROW(finder->find(Name("iscexception.getnext.example.org."),
  1933. this->qtype_,
  1934. ZoneFinder::FIND_DEFAULT),
  1935. isc::Exception);
  1936. EXPECT_THROW(finder->find(Name("basicexception.getnext.example.org."),
  1937. this->qtype_,
  1938. ZoneFinder::FIND_DEFAULT),
  1939. std::exception);
  1940. }
  1941. // This RRSIG has the wrong sigtype field, which should be
  1942. // an error if we decide to keep using that field
  1943. // Right now the field is ignored, so it does not error
  1944. this->expected_rdatas_.clear();
  1945. this->expected_sig_rdatas_.clear();
  1946. this->expected_rdatas_.push_back("192.0.2.1");
  1947. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1948. doFindTest(*finder, isc::dns::Name("badsigtype.example.org."),
  1949. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1950. this->expected_rdatas_, this->expected_sig_rdatas_);
  1951. }
  1952. TYPED_TEST(DatabaseClientTest, findAtOrigin) {
  1953. ZoneFinderPtr finder(this->getFinder());
  1954. // Specified type of RR exists, no DNSSEC
  1955. this->expected_rdatas_.clear();
  1956. this->expected_rdatas_.push_back("ns.example.com.");
  1957. doFindAtOriginTest(*finder, this->zname_, RRType::NS(), RRType::NS(),
  1958. this->rrttl_, ZoneFinder::SUCCESS,
  1959. this->expected_rdatas_, this->expected_sig_rdatas_);
  1960. // Specified type of RR exists, with DNSSEC
  1961. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 "
  1962. "20000201000000 12345 example.org. "
  1963. "FAKEFAKEFAKE");
  1964. doFindAtOriginTest(*finder, this->zname_, RRType::NS(), RRType::NS(),
  1965. this->rrttl_, ZoneFinder::SUCCESS,
  1966. this->expected_rdatas_, this->expected_sig_rdatas_,
  1967. ZoneFinder::RESULT_DEFAULT, this->zname_,
  1968. ZoneFinder::FIND_DNSSEC);
  1969. // Specified type of RR doesn't exist, no DNSSEC
  1970. this->expected_rdatas_.clear();
  1971. this->expected_sig_rdatas_.clear();
  1972. doFindAtOriginTest(*finder, this->zname_, RRType::TXT(), this->qtype_,
  1973. this->rrttl_, ZoneFinder::NXRRSET,
  1974. this->expected_rdatas_, this->expected_sig_rdatas_);
  1975. // Specified type of RR doesn't exist, with DNSSEC
  1976. this->expected_rdatas_.clear();
  1977. this->expected_sig_rdatas_.clear();
  1978. this->expected_rdatas_.push_back(
  1979. "acnamesig1.example.org. A NS RRSIG NSEC");
  1980. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1981. "20000201000000 12345 example.org. "
  1982. "FAKEFAKEFAKE");
  1983. doFindAtOriginTest(*finder, this->zname_, RRType::TXT(), RRType::NSEC(),
  1984. this->rrttl_, ZoneFinder::NXRRSET,
  1985. this->expected_rdatas_, this->expected_sig_rdatas_,
  1986. ZoneFinder::RESULT_NSEC_SIGNED,
  1987. this->zname_, ZoneFinder::FIND_DNSSEC);
  1988. // Specified type of RR doesn't exist, with DNSSEC, enabling NSEC3
  1989. this->current_accessor_->enableNSEC3();
  1990. this->expected_rdatas_.clear();
  1991. this->expected_sig_rdatas_.clear();
  1992. doFindAtOriginTest(*finder, this->zname_, RRType::TXT(), RRType::TXT(),
  1993. this->rrttl_, ZoneFinder::NXRRSET,
  1994. this->expected_rdatas_, this->expected_sig_rdatas_,
  1995. ZoneFinder::RESULT_NSEC3_SIGNED,
  1996. this->zname_, ZoneFinder::FIND_DNSSEC);
  1997. }
  1998. TYPED_TEST(DatabaseClientTest, findOutOfZone) {
  1999. // If the query name is out-of-zone it should result in an exception
  2000. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2001. vector<ConstRRsetPtr> target;
  2002. // Superdomain
  2003. EXPECT_THROW(finder->find(Name("org"), this->qtype_), OutOfZone);
  2004. EXPECT_THROW(finder->findAll(Name("org"), target), OutOfZone);
  2005. // sharing a common ancestor
  2006. EXPECT_THROW(finder->find(Name("noexample.org"), this->qtype_), OutOfZone);
  2007. EXPECT_THROW(finder->findAll(Name("noexample.org"), target), OutOfZone);
  2008. // totally unrelated domain, smaller number of labels
  2009. EXPECT_THROW(finder->find(Name("com"), this->qtype_), OutOfZone);
  2010. EXPECT_THROW(finder->findAll(Name("com"), target), OutOfZone);
  2011. // totally unrelated domain, same number of labels
  2012. EXPECT_THROW(finder->find(Name("example.com"), this->qtype_), OutOfZone);
  2013. EXPECT_THROW(finder->findAll(Name("example.com"), target), OutOfZone);
  2014. // totally unrelated domain, larger number of labels
  2015. EXPECT_THROW(finder->find(Name("more.example.com"), this->qtype_),
  2016. OutOfZone);
  2017. EXPECT_THROW(finder->findAll(Name("more.example.com"), target), OutOfZone);
  2018. }
  2019. TYPED_TEST(DatabaseClientTest, findDelegation) {
  2020. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2021. // The apex should not be considered delegation point and we can access
  2022. // data
  2023. this->expected_rdatas_.clear();
  2024. this->expected_sig_rdatas_.clear();
  2025. this->expected_rdatas_.push_back("192.0.2.1");
  2026. doFindTest(*finder, isc::dns::Name("example.org."),
  2027. this->qtype_, this->qtype_,
  2028. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2029. this->expected_sig_rdatas_);
  2030. this->expected_rdatas_.clear();
  2031. this->expected_rdatas_.push_back("ns.example.com.");
  2032. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  2033. "12345 example.org. FAKEFAKEFAKE");
  2034. doFindTest(*finder, isc::dns::Name("example.org."),
  2035. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  2036. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2037. this->expected_sig_rdatas_);
  2038. // Check when we ask for something below delegation point, we get the NS
  2039. // (Both when the RRset there exists and doesn't)
  2040. this->expected_rdatas_.clear();
  2041. this->expected_sig_rdatas_.clear();
  2042. this->expected_rdatas_.push_back("ns.example.com.");
  2043. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  2044. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  2045. "12345 example.org. FAKEFAKEFAKE");
  2046. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  2047. this->qtype_, isc::dns::RRType::NS(),
  2048. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  2049. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2050. isc::dns::Name("delegation.example.org."));
  2051. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  2052. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  2053. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  2054. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2055. isc::dns::Name("delegation.example.org."));
  2056. doFindTest(*finder, isc::dns::Name("deep.below.delegation.example.org."),
  2057. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  2058. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  2059. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2060. isc::dns::Name("delegation.example.org."));
  2061. // Even when we check directly at the delegation point, we should get
  2062. // the NS
  2063. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  2064. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  2065. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  2066. this->expected_sig_rdatas_);
  2067. // And when we ask directly for the NS, we should still get delegation
  2068. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  2069. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  2070. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  2071. this->expected_sig_rdatas_);
  2072. // Now test delegation. If it is below the delegation point, we should get
  2073. // the DNAME (the one with data under DNAME is invalid zone, but we test
  2074. // the behaviour anyway just to make sure)
  2075. this->expected_rdatas_.clear();
  2076. this->expected_rdatas_.push_back("dname.example.com.");
  2077. this->expected_sig_rdatas_.clear();
  2078. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  2079. "20000201000000 12345 example.org. "
  2080. "FAKEFAKEFAKE");
  2081. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  2082. this->qtype_, isc::dns::RRType::DNAME(),
  2083. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  2084. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2085. isc::dns::Name("dname.example.org."));
  2086. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  2087. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  2088. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  2089. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2090. isc::dns::Name("dname.example.org."));
  2091. // below.dname.example.org. has an A record
  2092. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  2093. isc::dns::RRType::A(), isc::dns::RRType::DNAME(),
  2094. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  2095. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2096. isc::dns::Name("dname.example.org."));
  2097. doFindTest(*finder, isc::dns::Name("really.deep.below.dname.example.org."),
  2098. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  2099. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  2100. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2101. isc::dns::Name("dname.example.org."));
  2102. // Asking directly for DNAME should give SUCCESS
  2103. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  2104. isc::dns::RRType::DNAME(), isc::dns::RRType::DNAME(),
  2105. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2106. this->expected_sig_rdatas_);
  2107. // But we don't delegate at DNAME point
  2108. this->expected_rdatas_.clear();
  2109. this->expected_rdatas_.push_back("192.0.2.1");
  2110. this->expected_sig_rdatas_.clear();
  2111. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  2112. this->qtype_, this->qtype_,
  2113. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2114. this->expected_sig_rdatas_);
  2115. this->expected_rdatas_.clear();
  2116. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  2117. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2118. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  2119. this->expected_sig_rdatas_);
  2120. // This is broken dname, it contains two targets
  2121. EXPECT_THROW(finder->find(isc::dns::Name("below.baddname.example.org."),
  2122. this->qtype_,
  2123. ZoneFinder::FIND_DEFAULT),
  2124. DataSourceError);
  2125. // NS and other type coexist: deviant and not necessarily harmful.
  2126. // It should normally just result in DELEGATION; if GLUE_OK is specified,
  2127. // the other RR should be visible.
  2128. this->expected_rdatas_.clear();
  2129. this->expected_rdatas_.push_back("ns.example.com");
  2130. doFindTest(*finder, Name("brokenns1.example.org"), this->qtype_,
  2131. RRType::NS(), this->rrttl_, ZoneFinder::DELEGATION,
  2132. this->expected_rdatas_, this->empty_rdatas_,
  2133. ZoneFinder::RESULT_DEFAULT);
  2134. this->expected_rdatas_.clear();
  2135. this->expected_rdatas_.push_back("192.0.2.1");
  2136. doFindTest(*finder, Name("brokenns1.example.org"), this->qtype_,
  2137. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2138. this->expected_rdatas_, this->empty_rdatas_,
  2139. ZoneFinder::RESULT_DEFAULT, Name("brokenns1.example.org"),
  2140. ZoneFinder::FIND_GLUE_OK);
  2141. }
  2142. TYPED_TEST(DatabaseClientTest, findDS) {
  2143. // Type DS query is an exception to the general delegation case; the NS
  2144. // should be ignored and it should be treated just like normal
  2145. // authoritative data.
  2146. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2147. // DS exists at the delegation point. It should be returned with result
  2148. // code of SUCCESS.
  2149. this->expected_rdatas_.push_back("1 1 2 abcd"),
  2150. this->expected_sig_rdatas_.push_back("DS 5 3 3600 20000101000000 "
  2151. "20000201000000 12345 example.org. "
  2152. "FAKEFAKEFAKE");
  2153. doFindTest(*finder, Name("delegation.example.org."),
  2154. RRType::DS(), RRType::DS(), this->rrttl_, ZoneFinder::SUCCESS,
  2155. this->expected_rdatas_, this->expected_sig_rdatas_,
  2156. ZoneFinder::RESULT_DEFAULT);
  2157. // DS doesn't exist at the delegation point. The result should be
  2158. // NXRRSET, and if DNSSEC is requested and the zone is NSEC-signed,
  2159. // the corresponding NSEC should be returned (normally with its RRSIG,
  2160. // but in this simplified test setup it's omitted in the test data).
  2161. this->expected_rdatas_.clear();
  2162. this->expected_rdatas_.push_back("dummy.example.org. NS NSEC");
  2163. this->expected_sig_rdatas_.clear();
  2164. doFindTest(*finder, Name("insecdelegation.example.org."),
  2165. RRType::DS(), RRType::NSEC(), this->rrttl_, ZoneFinder::NXRRSET,
  2166. this->expected_rdatas_, this->expected_sig_rdatas_,
  2167. ZoneFinder::RESULT_NSEC_SIGNED,
  2168. Name("insecdelegation.example.org."), ZoneFinder::FIND_DNSSEC);
  2169. // Some insane case: DS under a zone cut. It's included in the DB, but
  2170. // shouldn't be visible via finder.
  2171. this->expected_rdatas_.clear();
  2172. this->expected_rdatas_.push_back("ns.example.com");
  2173. doFindTest(*finder, Name("child.insecdelegation.example.org"),
  2174. RRType::DS(), RRType::NS(), this->rrttl_,
  2175. ZoneFinder::DELEGATION, this->expected_rdatas_,
  2176. this->empty_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2177. Name("insecdelegation.example.org."), ZoneFinder::FIND_DNSSEC);
  2178. }
  2179. TYPED_TEST(DatabaseClientTest, emptyDomain) {
  2180. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2181. // This domain doesn't exist, but a subdomain of it does.
  2182. // Therefore we should pretend the domain is there, but contains no RRsets
  2183. doFindTest(*finder, isc::dns::Name("b.example.org."), this->qtype_,
  2184. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2185. this->expected_rdatas_, this->expected_sig_rdatas_);
  2186. }
  2187. // Glue-OK mode. Just go through NS delegations down (but not through
  2188. // DNAME) and pretend it is not there.
  2189. TYPED_TEST(DatabaseClientTest, glueOK) {
  2190. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2191. this->expected_rdatas_.clear();
  2192. this->expected_sig_rdatas_.clear();
  2193. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  2194. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2195. this->rrttl_, ZoneFinder::NXRRSET,
  2196. this->expected_rdatas_, this->expected_sig_rdatas_,
  2197. ZoneFinder::RESULT_DEFAULT,
  2198. isc::dns::Name("ns.delegation.example.org."),
  2199. ZoneFinder::FIND_GLUE_OK);
  2200. doFindTest(*finder, isc::dns::Name("nothere.delegation.example.org."),
  2201. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2202. this->rrttl_, ZoneFinder::NXDOMAIN,
  2203. this->expected_rdatas_, this->expected_sig_rdatas_,
  2204. ZoneFinder::RESULT_DEFAULT,
  2205. isc::dns::Name("nothere.delegation.example.org."),
  2206. ZoneFinder::FIND_GLUE_OK);
  2207. this->expected_rdatas_.push_back("192.0.2.1");
  2208. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  2209. this->qtype_, this->qtype_,
  2210. this->rrttl_, ZoneFinder::SUCCESS,
  2211. this->expected_rdatas_, this->expected_sig_rdatas_,
  2212. ZoneFinder::RESULT_DEFAULT,
  2213. isc::dns::Name("ns.delegation.example.org."),
  2214. ZoneFinder::FIND_GLUE_OK);
  2215. this->expected_rdatas_.clear();
  2216. this->expected_rdatas_.push_back("ns.example.com.");
  2217. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  2218. this->expected_sig_rdatas_.clear();
  2219. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 "
  2220. "20000201000000 12345 example.org. "
  2221. "FAKEFAKEFAKE");
  2222. // When we request the NS, it should be SUCCESS, not DELEGATION
  2223. // (different in GLUE_OK)
  2224. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  2225. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  2226. this->rrttl_, ZoneFinder::SUCCESS,
  2227. this->expected_rdatas_, this->expected_sig_rdatas_,
  2228. ZoneFinder::RESULT_DEFAULT,
  2229. isc::dns::Name("delegation.example.org."),
  2230. ZoneFinder::FIND_GLUE_OK);
  2231. this->expected_rdatas_.clear();
  2232. this->expected_rdatas_.push_back("dname.example.com.");
  2233. this->expected_sig_rdatas_.clear();
  2234. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  2235. "20000201000000 12345 example.org. "
  2236. "FAKEFAKEFAKE");
  2237. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  2238. this->qtype_, isc::dns::RRType::DNAME(),
  2239. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  2240. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2241. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  2242. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  2243. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  2244. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  2245. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2246. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  2247. }
  2248. TYPED_TEST(DatabaseClientTest, wildcard) {
  2249. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2250. // First, simple wildcard match
  2251. // Check also that the RRSIG is added from the wildcard (not modified)
  2252. this->expected_rdatas_.push_back("192.0.2.5");
  2253. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  2254. "20000201000000 12345 example.org. "
  2255. "FAKEFAKEFAKE");
  2256. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  2257. this->qtype_, this->qtype_, this->rrttl_,
  2258. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2259. this->expected_sig_rdatas_, ZoneFinder::RESULT_WILDCARD);
  2260. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  2261. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2262. this->expected_rdatas_, this->expected_sig_rdatas_,
  2263. ZoneFinder::RESULT_WILDCARD);
  2264. this->expected_rdatas_.clear();
  2265. this->expected_sig_rdatas_.clear();
  2266. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  2267. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2268. this->rrttl_, ZoneFinder::NXRRSET,
  2269. this->expected_rdatas_, this->expected_sig_rdatas_,
  2270. ZoneFinder::RESULT_WILDCARD);
  2271. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  2272. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2273. this->rrttl_, ZoneFinder::NXRRSET,
  2274. this->expected_rdatas_, this->expected_sig_rdatas_,
  2275. ZoneFinder::RESULT_WILDCARD);
  2276. // Direct request for this wildcard
  2277. this->expected_rdatas_.push_back("192.0.2.5");
  2278. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  2279. "20000201000000 12345 example.org. "
  2280. "FAKEFAKEFAKE");
  2281. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  2282. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2283. this->expected_rdatas_, this->expected_sig_rdatas_);
  2284. this->expected_rdatas_.clear();
  2285. this->expected_sig_rdatas_.clear();
  2286. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  2287. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2288. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  2289. this->expected_sig_rdatas_);
  2290. // This is nonsense, but check it doesn't match by some stupid accident
  2291. doFindTest(*finder, isc::dns::Name("a.*.wild.example.org"),
  2292. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2293. this->expected_rdatas_, this->expected_sig_rdatas_);
  2294. // These should be canceled, since it is below a domain which exitsts
  2295. doFindTest(*finder, isc::dns::Name("nothing.here.wild.example.org"),
  2296. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2297. this->expected_rdatas_, this->expected_sig_rdatas_);
  2298. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  2299. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2300. this->expected_rdatas_, this->expected_sig_rdatas_);
  2301. doFindTest(*finder,
  2302. isc::dns::Name("below.cancel.here.wild.example.org"),
  2303. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2304. this->expected_rdatas_, this->expected_sig_rdatas_);
  2305. // And this should be just plain empty non-terminal domain, check
  2306. // the wildcard doesn't hurt it
  2307. doFindTest(*finder, isc::dns::Name("here.wild.example.org"),
  2308. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2309. this->expected_rdatas_, this->expected_sig_rdatas_);
  2310. // Also make sure that the wildcard doesn't hurt the original data
  2311. // below the wildcard
  2312. this->expected_rdatas_.push_back("2001:db8::5");
  2313. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  2314. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  2315. this->rrttl_, ZoneFinder::SUCCESS,
  2316. this->expected_rdatas_, this->expected_sig_rdatas_);
  2317. this->expected_rdatas_.clear();
  2318. // How wildcard go together with delegation
  2319. this->expected_rdatas_.push_back("ns.example.com.");
  2320. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  2321. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  2322. ZoneFinder::DELEGATION, this->expected_rdatas_,
  2323. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2324. isc::dns::Name("delegatedwild.example.org"));
  2325. // FIXME: This doesn't look logically OK, GLUE_OK should make it transparent,
  2326. // so the match should either work or be canceled, but return NXDOMAIN
  2327. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  2328. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  2329. ZoneFinder::DELEGATION, this->expected_rdatas_,
  2330. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2331. isc::dns::Name("delegatedwild.example.org"),
  2332. ZoneFinder::FIND_GLUE_OK);
  2333. this->expected_rdatas_.clear();
  2334. this->expected_rdatas_.push_back("192.0.2.5");
  2335. // These are direct matches
  2336. const char* positive_names[] = {
  2337. "wild.*.foo.example.org.",
  2338. "wild.*.foo.*.bar.example.org.",
  2339. NULL
  2340. };
  2341. for (const char** name = positive_names; *name != NULL; ++ name) {
  2342. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  2343. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2344. this->expected_rdatas_,
  2345. this->expected_sig_rdatas_);
  2346. }
  2347. // These are wildcard matches against empty nonterminal asterisk
  2348. this->expected_rdatas_.clear();
  2349. const char* negative_names[] = {
  2350. "a.foo.example.org.",
  2351. "wild.bar.foo.example.org.",
  2352. "baz.foo.*.bar.example.org",
  2353. "baz.foo.baz.bar.example.org",
  2354. "*.foo.baz.bar.example.org",
  2355. NULL
  2356. };
  2357. // Unless FIND_DNSSEC is specified, this is no different from other
  2358. // NXRRSET case.
  2359. for (const char** name = negative_names; *name != NULL; ++ name) {
  2360. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  2361. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2362. this->expected_rdatas_, this->expected_sig_rdatas_,
  2363. ZoneFinder::RESULT_WILDCARD);
  2364. }
  2365. // With FIND_DNSSEC, it should have NSEC_SIGNED flag.
  2366. const char* negative_dnssec_names[] = {
  2367. "a.bar.example.org.",
  2368. "foo.baz.bar.example.org.",
  2369. "a.foo.bar.example.org.",
  2370. NULL
  2371. };
  2372. this->expected_rdatas_.clear();
  2373. this->expected_rdatas_.push_back("wild.*.foo.*.bar.example.org. NSEC");
  2374. this->expected_sig_rdatas_.clear();
  2375. for (const char** name = negative_dnssec_names; *name != NULL; ++ name) {
  2376. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  2377. RRType::NSEC(), this->rrttl_, ZoneFinder::NXRRSET,
  2378. this->expected_rdatas_, this->expected_sig_rdatas_,
  2379. ZoneFinder::RESULT_WILDCARD | ZoneFinder::RESULT_NSEC_SIGNED,
  2380. Name("bao.example.org."), ZoneFinder::FIND_DNSSEC);
  2381. }
  2382. // CNAME on a wildcard. Maybe not so common, but not disallowed.
  2383. this->expected_rdatas_.clear();
  2384. this->expected_rdatas_.push_back("www.example.org.");
  2385. this->expected_sig_rdatas_.clear();
  2386. doFindTest(*finder, isc::dns::Name("a.cnamewild.example.org."),
  2387. isc::dns::RRType::TXT(), isc::dns::RRType::CNAME(),
  2388. this->rrttl_, ZoneFinder::CNAME,
  2389. this->expected_rdatas_, this->expected_sig_rdatas_,
  2390. ZoneFinder::RESULT_WILDCARD);
  2391. // DNAME on a wildcard. In our implementation we ignore DNAMEs on a
  2392. // wildcard, but at a higher level we say the behavior is "unspecified".
  2393. // rfc2672bis strongly discourages the mixture of DNAME and wildcard
  2394. // (with SHOULD NOT).
  2395. doFindTest(*finder, Name("a.dnamewild.example.org."),
  2396. this->qtype_, this->qtype_, this->rrttl_,
  2397. ZoneFinder::NXRRSET, this->empty_rdatas_,
  2398. this->empty_rdatas_, ZoneFinder::RESULT_WILDCARD);
  2399. // Some strange things in the wild node
  2400. this->expected_rdatas_.clear();
  2401. this->expected_rdatas_.push_back("ns.example.com.");
  2402. doFindTest(*finder, isc::dns::Name("a.nswild.example.org."),
  2403. isc::dns::RRType::TXT(), isc::dns::RRType::NS(),
  2404. this->rrttl_, ZoneFinder::DELEGATION,
  2405. this->expected_rdatas_, this->empty_rdatas_,
  2406. ZoneFinder::RESULT_WILDCARD);
  2407. }
  2408. TYPED_TEST(DatabaseClientTest, noWildcard) {
  2409. // Tests with the NO_WILDCARD flag.
  2410. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2411. // This would match *.wild.example.org, but with NO_WILDCARD should
  2412. // result in NXDOMAIN.
  2413. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A "
  2414. "NSEC RRSIG");
  2415. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  2416. "20000201000000 12345 example.org. "
  2417. "FAKEFAKEFAKE");
  2418. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  2419. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  2420. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  2421. this->expected_sig_rdatas_, ZoneFinder::RESULT_NSEC_SIGNED,
  2422. Name("*.wild.example.org."),
  2423. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  2424. // Should be the same without FIND_DNSSEC (but in this case no RRsets
  2425. // will be returned)
  2426. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  2427. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  2428. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2429. this->empty_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2430. Name::ROOT_NAME(), // name is dummy
  2431. ZoneFinder::NO_WILDCARD);
  2432. // Same for wildcard empty non terminal.
  2433. this->expected_rdatas_.clear();
  2434. this->expected_rdatas_.push_back("brokenns1.example.org. A NSEC");
  2435. doFindTest(*finder, isc::dns::Name("a.bar.example.org"),
  2436. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  2437. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  2438. this->empty_rdatas_, ZoneFinder::RESULT_NSEC_SIGNED,
  2439. Name("wild.*.foo.*.bar.example.org"),
  2440. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  2441. // Search for a wildcard name with NO_WILDCARD. There should be no
  2442. // difference. This is, for example, necessary to provide non existence
  2443. // of matching wildcard for isnx.nonterminal.example.org.
  2444. this->expected_rdatas_.clear();
  2445. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  2446. doFindTest(*finder, isc::dns::Name("*.nonterminal.example.org"),
  2447. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  2448. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  2449. this->empty_rdatas_, ZoneFinder::RESULT_NSEC_SIGNED,
  2450. Name("l.example.org"),
  2451. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  2452. // On the other hand, if there's exact match for the wildcard name
  2453. // it should be found regardless of NO_WILDCARD.
  2454. this->expected_rdatas_.clear();
  2455. this->expected_rdatas_.push_back("192.0.2.5");
  2456. this->expected_sig_rdatas_.clear();
  2457. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  2458. "20000201000000 12345 example.org. "
  2459. "FAKEFAKEFAKE");
  2460. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  2461. this->qtype_, this->qtype_, this->rrttl_,
  2462. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2463. this->expected_sig_rdatas_, ZoneFinder::RESULT_DEFAULT,
  2464. Name("*.wild.example.org"), ZoneFinder::NO_WILDCARD);
  2465. }
  2466. TYPED_TEST(DatabaseClientTest, NXRRSET_NSEC) {
  2467. // The domain exists, but doesn't have this RRType
  2468. // So we should get its NSEC
  2469. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2470. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  2471. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  2472. "20000201000000 12345 example.org. "
  2473. "FAKEFAKEFAKE");
  2474. doFindTest(*finder, isc::dns::Name("www.example.org."),
  2475. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  2476. this->rrttl_, ZoneFinder::NXRRSET,
  2477. this->expected_rdatas_, this->expected_sig_rdatas_,
  2478. ZoneFinder::RESULT_NSEC_SIGNED, Name::ROOT_NAME(),
  2479. ZoneFinder::FIND_DNSSEC);
  2480. }
  2481. TYPED_TEST(DatabaseClientTest, wildcardNXRRSET_NSEC) {
  2482. // The domain exists, but doesn't have this RRType
  2483. // So we should get its NSEC
  2484. //
  2485. // The user will have to query us again to get the correct
  2486. // answer (eg. prove there's not an exact match)
  2487. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2488. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A NSEC "
  2489. "RRSIG");
  2490. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  2491. "20000201000000 12345 example.org. "
  2492. "FAKEFAKEFAKE");
  2493. // Note that the NSEC name should NOT be synthesized.
  2494. doFindTest(*finder, isc::dns::Name("a.wild.example.org."),
  2495. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  2496. this->rrttl_, ZoneFinder::NXRRSET,
  2497. this->expected_rdatas_, this->expected_sig_rdatas_,
  2498. ZoneFinder::RESULT_WILDCARD | ZoneFinder::RESULT_NSEC_SIGNED,
  2499. Name("*.wild.example.org"), ZoneFinder::FIND_DNSSEC);
  2500. }
  2501. // Subroutine for dnssecFlagCheck defined below. It performs some simple
  2502. // checks regarding DNSSEC related result flags for findAll().
  2503. void
  2504. dnssecFlagCheckForAny(ZoneFinder& finder, const Name& name,
  2505. ZoneFinder::FindResultFlags sec_flag)
  2506. {
  2507. std::vector<ConstRRsetPtr> target; // just for placeholder
  2508. ConstZoneFinderContextPtr all_result =
  2509. finder.findAll(name, target, ZoneFinder::FIND_DNSSEC);
  2510. EXPECT_EQ((sec_flag & ZoneFinder::RESULT_NSEC_SIGNED) != 0,
  2511. all_result->isNSECSigned());
  2512. EXPECT_EQ((sec_flag & ZoneFinder::RESULT_NSEC3_SIGNED) != 0,
  2513. all_result->isNSEC3Signed());
  2514. }
  2515. // Common tests about DNSSEC related result flags. Shared for the NSEC
  2516. // and NSEC3 cases.
  2517. void
  2518. dnssecFlagCheck(ZoneFinder& finder, ZoneFinder::FindResultFlags sec_flag) {
  2519. std::vector<std::string> expected_rdatas;
  2520. std::vector<std::string> expected_sig_rdatas;
  2521. // Check NXDOMAIN case in NSEC signed zone, and RESULT_NSEC_SIGNED flag
  2522. // should be returned to upper layer caller.
  2523. if ((sec_flag & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  2524. expected_rdatas.push_back("www2.example.org. A AAAA NSEC RRSIG");
  2525. expected_sig_rdatas.push_back("NSEC 5 3 3600 20000101000000 "
  2526. "20000201000000 12345 example.org. "
  2527. "FAKEFAKEFAKE");
  2528. }
  2529. doFindTest(finder, Name("www1.example.org"), RRType::A(), RRType::NSEC(),
  2530. RRTTL(3600), ZoneFinder::NXDOMAIN, expected_rdatas,
  2531. expected_sig_rdatas, sec_flag, Name("www.example.org."),
  2532. ZoneFinder::FIND_DNSSEC);
  2533. dnssecFlagCheckForAny(finder, Name("www1.example.org"), sec_flag);
  2534. // Check NXRRSET case in NSEC signed zone, and RESULT_NSEC_SIGNED flag
  2535. // should be return.
  2536. // No "findAll" test case for this because NXRRSET shouldn't happen for it.
  2537. expected_rdatas.clear();
  2538. expected_sig_rdatas.clear();
  2539. if ((sec_flag & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  2540. expected_rdatas.push_back("www2.example.org. A AAAA NSEC RRSIG");
  2541. expected_sig_rdatas.push_back("NSEC 5 3 3600 20000101000000 "
  2542. "20000201000000 12345 example.org. "
  2543. "FAKEFAKEFAKE");
  2544. }
  2545. doFindTest(finder, Name("www.example.org."), RRType::TXT(), RRType::NSEC(),
  2546. RRTTL(3600), ZoneFinder::NXRRSET, expected_rdatas,
  2547. expected_sig_rdatas, sec_flag, Name::ROOT_NAME(),
  2548. ZoneFinder::FIND_DNSSEC);
  2549. // Empty name, should result in NXRRSET (in this test setup the NSEC
  2550. // doesn't have RRSIG).
  2551. expected_rdatas.clear();
  2552. expected_sig_rdatas.clear();
  2553. if ((sec_flag & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  2554. expected_rdatas.push_back("empty.nonterminal.example.org. NSEC");
  2555. }
  2556. doFindTest(finder, Name("nonterminal.example.org."), RRType::A(),
  2557. RRType::NSEC(), RRTTL(3600), ZoneFinder::NXRRSET,
  2558. expected_rdatas,expected_sig_rdatas, sec_flag,
  2559. Name("l.example.org."), ZoneFinder::FIND_DNSSEC);
  2560. dnssecFlagCheckForAny(finder, Name("nonterminal.example.org"), sec_flag);
  2561. // Wildcard match
  2562. expected_rdatas.clear();
  2563. expected_sig_rdatas.clear();
  2564. expected_rdatas.push_back("192.0.2.5");
  2565. expected_sig_rdatas.push_back("A 5 3 3600 20000101000000 "
  2566. "20000201000000 12345 example.org. "
  2567. "FAKEFAKEFAKE");
  2568. doFindTest(finder, Name("b.a.wild.example.org"), RRType::A(),
  2569. RRType::A(), RRTTL(3600), ZoneFinder::SUCCESS, expected_rdatas,
  2570. expected_sig_rdatas, (ZoneFinder::RESULT_WILDCARD | sec_flag),
  2571. Name("b.a.wild.example.org"), ZoneFinder::FIND_DNSSEC);
  2572. dnssecFlagCheckForAny(finder, Name("b.a.wild.example.org"), sec_flag);
  2573. // Wildcard + NXRRSET (no "findAll" test for this case)
  2574. expected_rdatas.clear();
  2575. expected_sig_rdatas.clear();
  2576. if ((sec_flag & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  2577. expected_rdatas.push_back("cancel.here.wild.example.org. "
  2578. "A NSEC RRSIG");
  2579. expected_sig_rdatas.push_back("NSEC 5 3 3600 20000101000000 "
  2580. "20000201000000 12345 example.org. "
  2581. "FAKEFAKEFAKE");
  2582. }
  2583. doFindTest(finder, Name("b.a.wild.example.org"),
  2584. RRType::TXT(), RRType::NSEC(), RRTTL(3600), ZoneFinder::NXRRSET,
  2585. expected_rdatas, expected_sig_rdatas,
  2586. (ZoneFinder::RESULT_WILDCARD | sec_flag),
  2587. Name("*.wild.example.org"), ZoneFinder::FIND_DNSSEC);
  2588. // Empty wildcard (this NSEC doesn't have RRSIG in our test data)
  2589. expected_rdatas.clear();
  2590. expected_sig_rdatas.clear();
  2591. if ((sec_flag & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  2592. expected_rdatas.push_back("wild.*.foo.*.bar.example.org. NSEC");
  2593. }
  2594. doFindTest(finder, Name("foo.wild.bar.example.org"),
  2595. RRType::TXT(), RRType::NSEC(), RRTTL(3600), ZoneFinder::NXRRSET,
  2596. expected_rdatas, expected_sig_rdatas,
  2597. (ZoneFinder::RESULT_WILDCARD | sec_flag),
  2598. Name("bao.example.org"), ZoneFinder::FIND_DNSSEC);
  2599. dnssecFlagCheckForAny(finder, Name("foo.wild.bar.example.org"), sec_flag);
  2600. }
  2601. TYPED_TEST(DatabaseClientTest, dnssecResultFlags) {
  2602. // ZoneFinder::find() for negative cases and wildcard cases should check
  2603. // whether the zone is signed with NSEC or NSEC3.
  2604. // In the default test setup, the zone should be considered NSEC-signed
  2605. // (the apex node has an NSEC RR).
  2606. {
  2607. SCOPED_TRACE("NSEC only");
  2608. dnssecFlagCheck(*this->getFinder(), ZoneFinder::RESULT_NSEC_SIGNED);
  2609. }
  2610. // Then add an NSEC3PARAM RRset at the apex (it may look weird if the
  2611. // zone only has NSEC3PARM RRset (but no NSEC3s), but it is okay for the
  2612. // purpose of this test). The zone should now be considered NSEC3-signed.
  2613. // Note that the apex NSEC still exists; NSEC3 should override NSEC.
  2614. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2615. this->rrset_.reset(new RRset(this->zname_, this->qclass_,
  2616. RRType::NSEC3PARAM(), this->rrttl_));
  2617. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2618. this->rrset_->getClass(),
  2619. "1 0 12 aabbccdd"));
  2620. this->updater_->addRRset(*this->rrset_);
  2621. {
  2622. SCOPED_TRACE("NSEC and NSEC3");
  2623. dnssecFlagCheck(this->updater_->getFinder(),
  2624. ZoneFinder::RESULT_NSEC3_SIGNED);
  2625. }
  2626. // Next, delete the apex NSEC. Since NSEC3PARAM remains, the zone should
  2627. // still be considered NSEC3-signed.
  2628. RRsetPtr nsec_rrset(new RRset(this->zname_, this->qclass_, RRType::NSEC(),
  2629. this->rrttl_));
  2630. nsec_rrset->addRdata(rdata::createRdata(RRType::NSEC(), this->qclass_,
  2631. "acnamesig1.example.org. NS A "
  2632. "NSEC RRSIG"));
  2633. this->updater_->deleteRRset(*nsec_rrset);
  2634. {
  2635. SCOPED_TRACE("NSEC3 only");
  2636. dnssecFlagCheck(this->updater_->getFinder(),
  2637. ZoneFinder::RESULT_NSEC3_SIGNED);
  2638. }
  2639. // Finally, delete the NSEC3PARAM we just added above. The zone should
  2640. // then be considered unsigned.
  2641. this->updater_->deleteRRset(*this->rrset_);
  2642. {
  2643. SCOPED_TRACE("unsigned");
  2644. dnssecFlagCheck(this->updater_->getFinder(),
  2645. ZoneFinder::RESULT_DEFAULT);
  2646. }
  2647. }
  2648. TYPED_TEST(DatabaseClientTest, NXDOMAIN_NSEC) {
  2649. // The domain doesn't exist, so we must get the right NSEC
  2650. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2651. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  2652. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  2653. "20000201000000 12345 example.org. "
  2654. "FAKEFAKEFAKE");
  2655. doFindTest(*finder, isc::dns::Name("www1.example.org."),
  2656. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  2657. this->rrttl_, ZoneFinder::NXDOMAIN,
  2658. this->expected_rdatas_, this->expected_sig_rdatas_,
  2659. ZoneFinder::RESULT_NSEC_SIGNED, Name("www.example.org."),
  2660. ZoneFinder::FIND_DNSSEC);
  2661. this->expected_rdatas_.clear();
  2662. this->expected_rdatas_.push_back("acnamesig1.example.org. NS A NSEC RRSIG");
  2663. // This tests it works correctly in apex (there was a bug, where a check
  2664. // for NS-alone was there and it would throw).
  2665. doFindTest(*finder, isc::dns::Name("aa.example.org."),
  2666. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  2667. this->rrttl_, ZoneFinder::NXDOMAIN,
  2668. this->expected_rdatas_, this->expected_sig_rdatas_,
  2669. ZoneFinder::RESULT_NSEC_SIGNED, Name("example.org."),
  2670. ZoneFinder::FIND_DNSSEC);
  2671. // Check that if the DB doesn't support it, the exception from there
  2672. // is not propagated and it only does not include the NSEC
  2673. if (!this->is_mock_) {
  2674. return; // We don't make the real DB to throw
  2675. }
  2676. // In this case the accessor doesn't support findPreviousName(), but the
  2677. // zone apex has NSEC, and the zone itself is considered NSEC-signed.
  2678. doFindTest(*finder, Name("notimplnsec.example.org."),
  2679. RRType::TXT(), RRType::NSEC(), this->rrttl_,
  2680. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2681. this->empty_rdatas_, ZoneFinder::RESULT_NSEC_SIGNED,
  2682. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC);
  2683. }
  2684. TYPED_TEST(DatabaseClientTest, emptyNonterminalNSEC) {
  2685. // Same as NXDOMAIN_NSEC, but with empty non-terminal
  2686. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2687. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  2688. doFindTest(*finder, isc::dns::Name("nonterminal.example.org."),
  2689. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(), this->rrttl_,
  2690. ZoneFinder::NXRRSET, this->expected_rdatas_,
  2691. this->expected_sig_rdatas_,
  2692. ZoneFinder::RESULT_NSEC_SIGNED, Name("l.example.org."),
  2693. ZoneFinder::FIND_DNSSEC);
  2694. // Check that if the DB doesn't support it, the exception from there
  2695. // is not propagated and it only does not include the NSEC
  2696. if (!this->is_mock_) {
  2697. return; // We don't make the real DB to throw
  2698. }
  2699. // See the corresponding case of NXDOMAIN_NSEC.
  2700. doFindTest(*finder, Name("here.wild.example.org."),
  2701. RRType::TXT(), RRType::NSEC(), this->rrttl_,
  2702. ZoneFinder::NXRRSET, this->empty_rdatas_,
  2703. this->empty_rdatas_, ZoneFinder::RESULT_NSEC_SIGNED,
  2704. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC);
  2705. }
  2706. TYPED_TEST(DatabaseClientTest, anyFromFind) {
  2707. // Find will reject answering an ANY query
  2708. EXPECT_THROW(this->getFinder()->find(isc::dns::Name("www2.example.org."),
  2709. RRType::ANY()), isc::Unexpected);
  2710. }
  2711. TYPED_TEST(DatabaseClientTest, findRRSIGsWithoutDNSSEC) {
  2712. // Trying to find RRSIG records directly should work even if
  2713. // FIND_DNSSEC flag is not specified.
  2714. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2715. ConstZoneFinderContextPtr result =
  2716. finder->find(isc::dns::Name("signed1.example.org."), RRType::RRSIG());
  2717. EXPECT_EQ(ZoneFinder::SUCCESS, result->code);
  2718. std::vector<std::string> expected_rdata;
  2719. expected_rdata.push_back(TEST_RECORDS[10][4]);
  2720. expected_rdata.push_back(TEST_RECORDS[11][4]);
  2721. expected_rdata.push_back(TEST_RECORDS[14][4]);
  2722. RdataIteratorPtr it(result->rrset->getRdataIterator());
  2723. std::vector<std::string> rdata;
  2724. while (!it->isLast()) {
  2725. rdata.push_back(it->getCurrent().toText());
  2726. it->next();
  2727. }
  2728. std::sort(rdata.begin(), rdata.end());
  2729. std::sort(expected_rdata.begin(), expected_rdata.end());
  2730. ASSERT_EQ(expected_rdata.size(), rdata.size());
  2731. for (size_t i(0); i < expected_rdata.size(); ++ i) {
  2732. EXPECT_EQ(expected_rdata[i], rdata[i]);
  2733. }
  2734. }
  2735. // Test the findAll method.
  2736. TYPED_TEST(DatabaseClientTest, getAll) {
  2737. // The domain doesn't exist, so we must get the right NSEC
  2738. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2739. // It should act the same on the "failures"
  2740. std::vector<ConstRRsetPtr> target;
  2741. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  2742. finder->findAll(isc::dns::Name("nothere.example.org."),
  2743. target)->code);
  2744. EXPECT_TRUE(target.empty());
  2745. EXPECT_EQ(ZoneFinder::NXRRSET,
  2746. finder->findAll(isc::dns::Name("here.wild.example.org."),
  2747. target)->code);
  2748. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  2749. this->expected_rdatas_.push_back("ns.example.com.");
  2750. doFindAllTestResult(*finder, isc::dns::Name("xx.delegation.example.org."),
  2751. ZoneFinder::DELEGATION, RRType::NS(),
  2752. this->expected_rdatas_,
  2753. isc::dns::Name("delegation.example.org."));
  2754. this->expected_rdatas_.clear();
  2755. this->expected_rdatas_.push_back("www.example.org.");
  2756. doFindAllTestResult(*finder, isc::dns::Name("cname.example.org"),
  2757. ZoneFinder::CNAME, RRType::CNAME(),
  2758. this->expected_rdatas_);
  2759. this->expected_rdatas_.clear();
  2760. this->expected_rdatas_.push_back("dname.example.com.");
  2761. doFindAllTestResult(*finder, isc::dns::Name("a.dname.example.org"),
  2762. ZoneFinder::DNAME, RRType::DNAME(),
  2763. this->expected_rdatas_,
  2764. isc::dns::Name("dname.example.org."));
  2765. // It should get the data on success
  2766. EXPECT_EQ(ZoneFinder::SUCCESS,
  2767. finder->findAll(isc::dns::Name("www2.example.org."),
  2768. target)->code);
  2769. ASSERT_EQ(2, target.size());
  2770. size_t a_idx(target[1]->getType() == RRType::A());
  2771. EXPECT_EQ(RRType::A(), target[a_idx]->getType());
  2772. std::string previous;
  2773. size_t count(0);
  2774. for (RdataIteratorPtr it(target[a_idx]->getRdataIterator());
  2775. !it->isLast(); it->next()) {
  2776. count ++;
  2777. EXPECT_NE(previous, it->getCurrent().toText());
  2778. EXPECT_TRUE(it->getCurrent().toText() == "192.0.2.1" ||
  2779. it->getCurrent().toText() == "192.0.2.2");
  2780. previous = it->getCurrent().toText();
  2781. }
  2782. EXPECT_EQ(2, count);
  2783. EXPECT_EQ(RRType::AAAA(), target[1 - a_idx]->getType());
  2784. RdataIteratorPtr it(target[1 - a_idx]->getRdataIterator());
  2785. ASSERT_FALSE(it->isLast());
  2786. EXPECT_EQ("2001:db8::1", it->getCurrent().toText());
  2787. it->next();
  2788. EXPECT_TRUE(it->isLast());
  2789. // And on wildcard. Check the signatures as well.
  2790. target.clear();
  2791. ConstZoneFinderContextPtr result =
  2792. finder->findAll(Name("a.wild.example.org"), target,
  2793. ZoneFinder::FIND_DNSSEC);
  2794. EXPECT_EQ(ZoneFinder::SUCCESS, result->code);
  2795. EXPECT_TRUE(result->isWildcard());
  2796. EXPECT_TRUE(result->isNSECSigned());
  2797. EXPECT_FALSE(result->isNSEC3Signed());
  2798. ASSERT_EQ(2, target.size());
  2799. a_idx = target[1]->getType() == RRType::A();
  2800. EXPECT_EQ(RRType::A(), target[a_idx]->getType());
  2801. it = target[a_idx]->getRdataIterator();
  2802. ASSERT_FALSE(it->isLast());
  2803. EXPECT_EQ("192.0.2.5", it->getCurrent().toText());
  2804. it->next();
  2805. EXPECT_TRUE(it->isLast());
  2806. ConstRRsetPtr sig(target[a_idx]->getRRsig());
  2807. ASSERT_TRUE(sig);
  2808. EXPECT_EQ(RRType::RRSIG(), sig->getType());
  2809. EXPECT_EQ("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE",
  2810. sig->getRdataIterator()->getCurrent().toText());
  2811. EXPECT_EQ(RRType::NSEC(), target[1 - a_idx]->getType());
  2812. it = target[1 - a_idx]->getRdataIterator();
  2813. ASSERT_FALSE(it->isLast());
  2814. EXPECT_EQ("cancel.here.wild.example.org. A RRSIG NSEC",
  2815. it->getCurrent().toText());
  2816. it->next();
  2817. EXPECT_TRUE(it->isLast());
  2818. sig = target[1 - a_idx]->getRRsig();
  2819. ASSERT_TRUE(sig);
  2820. EXPECT_EQ(RRType::RRSIG(), sig->getType());
  2821. EXPECT_EQ("NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE",
  2822. sig->getRdataIterator()->getCurrent().toText());
  2823. }
  2824. TYPED_TEST(DatabaseClientTest, getOrigin) {
  2825. DataSourceClient::FindResult
  2826. zone(this->client_->findZone(Name("example.org")));
  2827. ASSERT_EQ(result::SUCCESS, zone.code);
  2828. boost::shared_ptr<DatabaseClient::Finder> finder(
  2829. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  2830. if (this->is_mock_) {
  2831. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  2832. }
  2833. EXPECT_EQ(this->zname_, finder->getOrigin());
  2834. }
  2835. TYPED_TEST(DatabaseClientTest, updaterFinder) {
  2836. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2837. ASSERT_TRUE(this->updater_);
  2838. // If this update isn't replacing the zone, the finder should work
  2839. // just like the normal find() case.
  2840. if (this->is_mock_) {
  2841. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  2842. this->updater_->getFinder());
  2843. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  2844. }
  2845. this->expected_rdatas_.clear();
  2846. this->expected_rdatas_.push_back("192.0.2.1");
  2847. doFindTest(this->updater_->getFinder(), this->qname_,
  2848. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2849. this->expected_rdatas_, this->empty_rdatas_);
  2850. // When replacing the zone, the updater's finder shouldn't see anything
  2851. // in the zone until something is added.
  2852. this->updater_.reset();
  2853. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2854. ASSERT_TRUE(this->updater_);
  2855. if (this->is_mock_) {
  2856. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  2857. this->updater_->getFinder());
  2858. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  2859. }
  2860. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2861. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2862. this->empty_rdatas_, this->empty_rdatas_);
  2863. }
  2864. TYPED_TEST(DatabaseClientTest, flushZone) {
  2865. // A simple update case: flush the entire zone
  2866. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2867. // Before update, the name exists.
  2868. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  2869. this->qtype_)->code);
  2870. // start update in the replace mode. the normal finder should still
  2871. // be able to see the record, but the updater's finder shouldn't.
  2872. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2873. this->setUpdateAccessor();
  2874. EXPECT_EQ(ZoneFinder::SUCCESS,
  2875. finder->find(this->qname_, this->qtype_)->code);
  2876. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  2877. this->updater_->getFinder().find(this->qname_,
  2878. this->qtype_)->code);
  2879. // commit the update. now the normal finder shouldn't see it.
  2880. this->updater_->commit();
  2881. EXPECT_EQ(ZoneFinder::NXDOMAIN, finder->find(this->qname_,
  2882. this->qtype_)->code);
  2883. // Check rollback wasn't accidentally performed.
  2884. EXPECT_FALSE(this->isRollbacked());
  2885. }
  2886. TYPED_TEST(DatabaseClientTest, updateCancel) {
  2887. // similar to the previous test, but destruct the updater before commit.
  2888. ZoneFinderPtr finder = this->client_->findZone(this->zname_).zone_finder;
  2889. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  2890. this->qtype_)->code);
  2891. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2892. this->setUpdateAccessor();
  2893. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  2894. this->updater_->getFinder().find(this->qname_,
  2895. this->qtype_)->code);
  2896. // DB should not have been rolled back yet.
  2897. EXPECT_FALSE(this->isRollbacked());
  2898. this->updater_.reset(); // destruct without commit
  2899. // reset() should have triggered rollback (although it doesn't affect
  2900. // anything to the mock accessor implementation except for the result of
  2901. // isRollbacked())
  2902. EXPECT_TRUE(this->isRollbacked(true));
  2903. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  2904. this->qtype_)->code);
  2905. }
  2906. TYPED_TEST(DatabaseClientTest, exceptionFromRollback) {
  2907. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2908. this->rrset_.reset(new RRset(Name("throw.example.org"), this->qclass_,
  2909. this->qtype_, this->rrttl_));
  2910. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2911. this->rrset_->getClass(),
  2912. "192.0.2.1"));
  2913. this->updater_->addRRset(*this->rrset_);
  2914. // destruct without commit. The added name will result in an exception
  2915. // in the MockAccessor's rollback method. It shouldn't be propagated.
  2916. EXPECT_NO_THROW(this->updater_.reset());
  2917. }
  2918. TYPED_TEST(DatabaseClientTest, duplicateCommit) {
  2919. // duplicate commit. should result in exception.
  2920. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2921. this->updater_->commit();
  2922. EXPECT_THROW(this->updater_->commit(), DataSourceError);
  2923. }
  2924. TYPED_TEST(DatabaseClientTest, addRRsetToNewZone) {
  2925. // Add a single RRset to a fresh empty zone
  2926. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2927. this->updater_->addRRset(*this->rrset_);
  2928. this->expected_rdatas_.clear();
  2929. this->expected_rdatas_.push_back("192.0.2.2");
  2930. {
  2931. SCOPED_TRACE("add RRset");
  2932. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2933. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2934. this->expected_rdatas_, this->empty_rdatas_);
  2935. }
  2936. // Similar to the previous case, but with RRSIG
  2937. this->updater_.reset();
  2938. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2939. this->updater_->addRRset(*this->rrset_);
  2940. this->updater_->addRRset(*this->rrsigset_);
  2941. // confirm the expected columns were passed to the accessor (if checkable).
  2942. const char* const rrsig_added[] = {
  2943. "www.example.org.", "org.example.www.", "3600", "RRSIG", "A",
  2944. "A 5 3 0 20000101000000 20000201000000 0 example.org. FAKEFAKEFAKE"
  2945. };
  2946. this->checkLastAdded(rrsig_added);
  2947. this->expected_sig_rdatas_.clear();
  2948. this->expected_sig_rdatas_.push_back(
  2949. rrsig_added[DatabaseAccessor::ADD_RDATA]);
  2950. {
  2951. SCOPED_TRACE("add RRset with RRSIG");
  2952. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2953. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2954. this->expected_rdatas_, this->expected_sig_rdatas_);
  2955. }
  2956. // Add the non RRSIG RRset again, to see the attempt of adding RRSIG
  2957. // causes any unexpected effect, in particular, whether the SIGTYPE
  2958. // field might remain.
  2959. this->updater_->addRRset(*this->rrset_);
  2960. const char* const rrset_added[] = {
  2961. "www.example.org.", "org.example.www.", "3600", "A", "", "192.0.2.2"
  2962. };
  2963. this->checkLastAdded(rrset_added);
  2964. }
  2965. //
  2966. // Below we define a set of NSEC3 update tests.
  2967. //
  2968. // Commonly used data for NSEC3 update tests below.
  2969. const char* const nsec3_hash = "1BB7SO0452U1QHL98UISNDD9218GELR5";
  2970. const char* const nsec3_rdata = "1 1 12 AABBCCDD "
  2971. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR NS SOA RRSIG NSEC3PARAM";
  2972. const char* const nsec3_rdata2 = "1 1 12 AABBCCDD "
  2973. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR NS SOA RRSIG"; // differ in bitmaps
  2974. const char* const nsec3_sig_rdata = "NSEC3 5 3 3600 20000101000000 "
  2975. "20000201000000 12345 example.org. FAKEFAKEFAKE";
  2976. const char* const nsec3_sig_rdata2 = "NSEC3 5 3 3600 20000101000000 "
  2977. "20000201000000 12345 example.org. FAKEFAKE"; // differ in the signature
  2978. // Commonly used subroutine that checks if we can get the expected record.
  2979. // According to the API, implementations can skip filling in columns other
  2980. // than those explicitly checked below, so we don't check them.
  2981. void
  2982. nsec3Check(const vector<ConstRRsetPtr>& expected_rrsets,
  2983. const Name& zone_name, const string& expected_hash,
  2984. DatabaseAccessor& accessor)
  2985. {
  2986. const int zone_id = accessor.getZone(zone_name.toText()).second;
  2987. DatabaseAccessor::IteratorContextPtr itctx =
  2988. accessor.getNSEC3Records(expected_hash, zone_id);
  2989. ASSERT_TRUE(itctx);
  2990. // Build a list of matched RRsets and compare the both expected and built
  2991. // ones as sets.
  2992. string columns[DatabaseAccessor::COLUMN_COUNT];
  2993. vector<ConstRRsetPtr> actual_rrsets;
  2994. while (itctx->getNext(columns)) {
  2995. actual_rrsets.push_back(
  2996. textToRRset(expected_hash + "." + zone_name.toText() + " " +
  2997. columns[DatabaseAccessor::TTL_COLUMN] + " IN " +
  2998. columns[DatabaseAccessor::TYPE_COLUMN] + " " +
  2999. columns[DatabaseAccessor::RDATA_COLUMN]));
  3000. }
  3001. rrsetsCheck(expected_rrsets.begin(), expected_rrsets.end(),
  3002. actual_rrsets.begin(), actual_rrsets.end());
  3003. }
  3004. TYPED_TEST(DatabaseClientTest, addDeleteNSEC3InZone) {
  3005. // Add one NSEC3 RR to the zone, delete it, and add another one.
  3006. this->updater_ = this->client_->getUpdater(this->zname_, true);
  3007. const ConstRRsetPtr nsec3_rrset =
  3008. textToRRset(string(nsec3_hash) + ".example.org. 3600 IN NSEC3 " +
  3009. string(nsec3_rdata));
  3010. const ConstRRsetPtr nsec3_rrset2 =
  3011. textToRRset(string(nsec3_hash) + ".example.org. 3600 IN NSEC3 " +
  3012. string(nsec3_rdata2));
  3013. this->updater_->addRRset(*nsec3_rrset);
  3014. this->updater_->deleteRRset(*nsec3_rrset);
  3015. this->updater_->addRRset(*nsec3_rrset2);
  3016. this->updater_->commit();
  3017. // Check if we can get the expected record.
  3018. vector<ConstRRsetPtr> expected_rrsets;
  3019. expected_rrsets.push_back(nsec3_rrset2);
  3020. nsec3Check(expected_rrsets, this->zname_, nsec3_hash,
  3021. *this->current_accessor_);
  3022. }
  3023. TYPED_TEST(DatabaseClientTest, addDeleteNSEC3AndRRSIGToZone) {
  3024. // Add one NSEC3 RR and its RRSIG to the zone, delete the RRSIG and add
  3025. // a new one.
  3026. this->updater_ = this->client_->getUpdater(this->zname_, true);
  3027. const ConstRRsetPtr nsec3_rrset =
  3028. textToRRset(string(nsec3_hash) + ".example.org. 3600 IN NSEC3 " +
  3029. string(nsec3_rdata));
  3030. const ConstRRsetPtr nsec3_sig_rrset =
  3031. textToRRset(string(nsec3_hash) + ".example.org. 3600 IN RRSIG " +
  3032. string(nsec3_sig_rdata));
  3033. const ConstRRsetPtr nsec3_sig_rrset2 =
  3034. textToRRset(string(nsec3_hash) + ".example.org. 3600 IN RRSIG " +
  3035. string(nsec3_sig_rdata2));
  3036. this->updater_->addRRset(*nsec3_rrset);
  3037. this->updater_->addRRset(*nsec3_sig_rrset);
  3038. this->updater_->deleteRRset(*nsec3_sig_rrset);
  3039. this->updater_->addRRset(*nsec3_sig_rrset2);
  3040. this->updater_->commit();
  3041. // Check if we can get the expected record.
  3042. vector<ConstRRsetPtr> expected_rrsets;
  3043. expected_rrsets.push_back(nsec3_rrset);
  3044. expected_rrsets.push_back(nsec3_sig_rrset2);
  3045. nsec3Check(expected_rrsets, this->zname_, nsec3_hash,
  3046. *this->current_accessor_);
  3047. }
  3048. TYPED_TEST(DatabaseClientTest, addRRsetToCurrentZone) {
  3049. // Similar to the previous test, but not replacing the existing data.
  3050. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  3051. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3052. this->updater_->addRRset(*this->rrset_);
  3053. // We should see both old and new data.
  3054. this->expected_rdatas_.clear();
  3055. this->expected_rdatas_.push_back("192.0.2.1");
  3056. this->expected_rdatas_.push_back("192.0.2.2");
  3057. {
  3058. SCOPED_TRACE("add RRset");
  3059. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3060. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3061. this->expected_rdatas_, this->empty_rdatas_);
  3062. }
  3063. this->updater_->commit();
  3064. {
  3065. SCOPED_TRACE("add RRset after commit");
  3066. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  3067. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  3068. this->empty_rdatas_);
  3069. }
  3070. }
  3071. TYPED_TEST(DatabaseClientTest, addMultipleRRs) {
  3072. // Similar to the previous case, but the added RRset contains multiple
  3073. // RRs.
  3074. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3075. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3076. this->rrset_->getClass(),
  3077. "192.0.2.3"));
  3078. this->updater_->addRRset(*this->rrset_);
  3079. this->expected_rdatas_.clear();
  3080. this->expected_rdatas_.push_back("192.0.2.1");
  3081. this->expected_rdatas_.push_back("192.0.2.2");
  3082. this->expected_rdatas_.push_back("192.0.2.3");
  3083. {
  3084. SCOPED_TRACE("add multiple RRs");
  3085. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3086. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3087. this->expected_rdatas_, this->empty_rdatas_);
  3088. }
  3089. }
  3090. TYPED_TEST(DatabaseClientTest, addRRsetOfLargerTTL) {
  3091. // Similar to the previous one, but the TTL of the added RRset is larger
  3092. // than that of the existing record. The finder should use the smaller
  3093. // one.
  3094. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3095. this->rrset_->setTTL(RRTTL(7200));
  3096. this->updater_->addRRset(*this->rrset_);
  3097. this->expected_rdatas_.clear();
  3098. this->expected_rdatas_.push_back("192.0.2.1");
  3099. this->expected_rdatas_.push_back("192.0.2.2");
  3100. {
  3101. SCOPED_TRACE("add RRset of larger TTL");
  3102. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3103. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3104. this->expected_rdatas_, this->empty_rdatas_);
  3105. }
  3106. }
  3107. TYPED_TEST(DatabaseClientTest, addRRsetOfSmallerTTL) {
  3108. // Similar to the previous one, but the added RRset has a smaller TTL.
  3109. // The added TTL should be used by the finder.
  3110. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3111. this->rrset_->setTTL(RRTTL(1800));
  3112. this->updater_->addRRset(*this->rrset_);
  3113. this->expected_rdatas_.clear();
  3114. this->expected_rdatas_.push_back("192.0.2.1");
  3115. this->expected_rdatas_.push_back("192.0.2.2");
  3116. {
  3117. SCOPED_TRACE("add RRset of smaller TTL");
  3118. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3119. this->qtype_, RRTTL(1800), ZoneFinder::SUCCESS,
  3120. this->expected_rdatas_, this->empty_rdatas_);
  3121. }
  3122. }
  3123. TYPED_TEST(DatabaseClientTest, addSameRR) {
  3124. // Add the same RR as that is already in the data source.
  3125. // Currently the add interface doesn't try to suppress the duplicate,
  3126. // neither does the finder. We may want to revisit it in future versions.
  3127. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3128. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  3129. this->rrttl_));
  3130. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3131. this->rrset_->getClass(),
  3132. "192.0.2.1"));
  3133. this->updater_->addRRset(*this->rrset_);
  3134. this->expected_rdatas_.clear();
  3135. this->expected_rdatas_.push_back("192.0.2.1");
  3136. this->expected_rdatas_.push_back("192.0.2.1");
  3137. {
  3138. SCOPED_TRACE("add same RR");
  3139. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3140. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3141. this->expected_rdatas_, this->empty_rdatas_);
  3142. }
  3143. }
  3144. TYPED_TEST(DatabaseClientTest, addDeviantRR) {
  3145. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3146. // RR class mismatch. This should be detected and rejected.
  3147. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  3148. this->rrttl_));
  3149. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3150. this->rrset_->getClass(),
  3151. "test text"));
  3152. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  3153. // Out-of-zone owner name. At a higher level this should be rejected,
  3154. // but it doesn't happen in this interface.
  3155. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  3156. this->qtype_, this->rrttl_));
  3157. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3158. this->rrset_->getClass(),
  3159. "192.0.2.100"));
  3160. this->updater_->addRRset(*this->rrset_);
  3161. this->expected_rdatas_.clear();
  3162. this->expected_rdatas_.push_back("192.0.2.100");
  3163. {
  3164. // Note: find() rejects out-of-zone query name with an exception
  3165. // regardless of whether adding the RR succeeded, so this check
  3166. // actually doesn't confirm it.
  3167. SCOPED_TRACE("add out-of-zone RR");
  3168. EXPECT_THROW(this->updater_->getFinder().find(Name("example.com"),
  3169. this->qtype_),
  3170. OutOfZone);
  3171. }
  3172. }
  3173. TYPED_TEST(DatabaseClientTest, addEmptyRRset) {
  3174. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3175. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  3176. this->rrttl_));
  3177. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  3178. }
  3179. TYPED_TEST(DatabaseClientTest, addAfterCommit) {
  3180. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3181. this->updater_->commit();
  3182. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  3183. }
  3184. TYPED_TEST(DatabaseClientTest, addRRsetWithRRSIG) {
  3185. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3186. this->rrset_->addRRsig(*this->rrsigset_);
  3187. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  3188. }
  3189. TYPED_TEST(DatabaseClientTest, deleteRRset) {
  3190. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  3191. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  3192. this->rrttl_));
  3193. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3194. this->rrset_->getClass(),
  3195. "192.0.2.1"));
  3196. // Delete one RR from an RRset
  3197. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3198. this->updater_->deleteRRset(*this->rrset_);
  3199. // Delete the only RR of a name
  3200. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  3201. RRType::CNAME(), this->rrttl_));
  3202. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3203. this->rrset_->getClass(),
  3204. "www.example.org"));
  3205. this->updater_->deleteRRset(*this->rrset_);
  3206. // The this->updater_ finder should immediately see the deleted results.
  3207. {
  3208. SCOPED_TRACE("delete RRset");
  3209. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3210. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  3211. this->empty_rdatas_, this->empty_rdatas_);
  3212. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  3213. this->qtype_, this->qtype_, this->rrttl_,
  3214. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  3215. this->empty_rdatas_);
  3216. }
  3217. // before committing the change, the original finder should see the
  3218. // original record.
  3219. {
  3220. SCOPED_TRACE("delete RRset before commit");
  3221. this->expected_rdatas_.push_back("192.0.2.1");
  3222. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  3223. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  3224. this->empty_rdatas_);
  3225. this->expected_rdatas_.clear();
  3226. this->expected_rdatas_.push_back("www.example.org.");
  3227. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  3228. RRType::CNAME(), this->rrttl_, ZoneFinder::CNAME,
  3229. this->expected_rdatas_, this->empty_rdatas_);
  3230. }
  3231. // once committed, the record should be removed from the original finder's
  3232. // view, too.
  3233. this->updater_->commit();
  3234. {
  3235. SCOPED_TRACE("delete RRset after commit");
  3236. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  3237. this->rrttl_, ZoneFinder::NXRRSET, this->empty_rdatas_,
  3238. this->empty_rdatas_);
  3239. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  3240. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  3241. this->empty_rdatas_, this->empty_rdatas_);
  3242. }
  3243. }
  3244. TYPED_TEST(DatabaseClientTest, deleteRRsetToNXDOMAIN) {
  3245. // similar to the previous case, but it removes the only record of the
  3246. // given name. a subsequent find() should result in NXDOMAIN.
  3247. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  3248. RRType::CNAME(), this->rrttl_));
  3249. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3250. this->rrset_->getClass(),
  3251. "www.example.org"));
  3252. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3253. this->updater_->deleteRRset(*this->rrset_);
  3254. {
  3255. SCOPED_TRACE("delete RRset to NXDOMAIN");
  3256. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  3257. this->qtype_, this->qtype_, this->rrttl_,
  3258. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  3259. this->empty_rdatas_);
  3260. }
  3261. }
  3262. TYPED_TEST(DatabaseClientTest, deleteMultipleRRs) {
  3263. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  3264. this->rrttl_));
  3265. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3266. this->rrset_->getClass(),
  3267. "2001:db8::1"));
  3268. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3269. this->rrset_->getClass(),
  3270. "2001:db8::2"));
  3271. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3272. this->updater_->deleteRRset(*this->rrset_);
  3273. {
  3274. SCOPED_TRACE("delete multiple RRs");
  3275. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  3276. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  3277. this->empty_rdatas_, this->empty_rdatas_);
  3278. }
  3279. }
  3280. TYPED_TEST(DatabaseClientTest, partialDelete) {
  3281. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  3282. this->rrttl_));
  3283. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3284. this->rrset_->getClass(),
  3285. "2001:db8::1"));
  3286. // This does not exist in the test data source:
  3287. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3288. this->rrset_->getClass(),
  3289. "2001:db8::3"));
  3290. // deleteRRset should succeed "silently", and subsequent find() should
  3291. // find the remaining RR.
  3292. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3293. this->updater_->deleteRRset(*this->rrset_);
  3294. {
  3295. SCOPED_TRACE("partial delete");
  3296. this->expected_rdatas_.push_back("2001:db8::2");
  3297. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  3298. RRType::AAAA(), this->rrttl_, ZoneFinder::SUCCESS,
  3299. this->expected_rdatas_, this->empty_rdatas_);
  3300. }
  3301. }
  3302. TYPED_TEST(DatabaseClientTest, deleteNoMatch) {
  3303. // similar to the previous test, but there's not even a match in the
  3304. // specified RRset. Essentially there's no difference in the result.
  3305. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3306. this->updater_->deleteRRset(*this->rrset_);
  3307. {
  3308. SCOPED_TRACE("delete no match");
  3309. this->expected_rdatas_.push_back("192.0.2.1");
  3310. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3311. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3312. this->expected_rdatas_, this->empty_rdatas_);
  3313. }
  3314. }
  3315. TYPED_TEST(DatabaseClientTest, deleteWithDifferentTTL) {
  3316. // Our delete interface simply ignores TTL (may change in a future version)
  3317. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  3318. RRTTL(1800)));
  3319. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3320. this->rrset_->getClass(),
  3321. "192.0.2.1"));
  3322. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3323. this->updater_->deleteRRset(*this->rrset_);
  3324. {
  3325. SCOPED_TRACE("delete RRset with a different TTL");
  3326. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3327. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  3328. this->empty_rdatas_, this->empty_rdatas_);
  3329. }
  3330. }
  3331. TYPED_TEST(DatabaseClientTest, deleteDeviantRR) {
  3332. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3333. // RR class mismatch. This should be detected and rejected.
  3334. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  3335. this->rrttl_));
  3336. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3337. this->rrset_->getClass(),
  3338. "test text"));
  3339. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  3340. // Out-of-zone owner name. At a higher level this should be rejected,
  3341. // but it doesn't happen in this interface.
  3342. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  3343. this->qtype_, this->rrttl_));
  3344. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3345. this->rrset_->getClass(),
  3346. "192.0.2.100"));
  3347. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->rrset_));
  3348. }
  3349. TYPED_TEST(DatabaseClientTest, deleteAfterCommit) {
  3350. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3351. this->updater_->commit();
  3352. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  3353. }
  3354. TYPED_TEST(DatabaseClientTest, deleteEmptyRRset) {
  3355. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3356. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  3357. this->rrttl_));
  3358. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  3359. }
  3360. TYPED_TEST(DatabaseClientTest, deleteRRsetWithRRSIG) {
  3361. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3362. this->rrset_->addRRsig(*this->rrsigset_);
  3363. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  3364. }
  3365. TYPED_TEST(DatabaseClientTest, compoundUpdate) {
  3366. // This test case performs an arbitrary chosen add/delete operations
  3367. // in a single update transaction. Essentially there is nothing new to
  3368. // test here, but there may be some bugs that was overlooked and can
  3369. // only happen in the compound update scenario, so we test it.
  3370. this->updater_ = this->client_->getUpdater(this->zname_, false);
  3371. // add a new RR to an existing RRset
  3372. this->updater_->addRRset(*this->rrset_);
  3373. this->expected_rdatas_.clear();
  3374. this->expected_rdatas_.push_back("192.0.2.1");
  3375. this->expected_rdatas_.push_back("192.0.2.2");
  3376. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3377. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3378. this->expected_rdatas_, this->empty_rdatas_);
  3379. // delete an existing RR
  3380. this->rrset_.reset(new RRset(Name("www.example.org"), this->qclass_,
  3381. this->qtype_, this->rrttl_));
  3382. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3383. this->rrset_->getClass(),
  3384. "192.0.2.1"));
  3385. this->updater_->deleteRRset(*this->rrset_);
  3386. this->expected_rdatas_.clear();
  3387. this->expected_rdatas_.push_back("192.0.2.2");
  3388. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3389. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3390. this->expected_rdatas_, this->empty_rdatas_);
  3391. // re-add it
  3392. this->updater_->addRRset(*this->rrset_);
  3393. this->expected_rdatas_.push_back("192.0.2.1");
  3394. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  3395. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  3396. this->expected_rdatas_, this->empty_rdatas_);
  3397. // add a new RR with a new name
  3398. const Name newname("newname.example.org");
  3399. const RRType newtype(RRType::AAAA());
  3400. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  3401. this->rrttl_, ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  3402. this->empty_rdatas_);
  3403. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  3404. this->rrttl_));
  3405. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3406. this->rrset_->getClass(),
  3407. "2001:db8::10"));
  3408. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3409. this->rrset_->getClass(),
  3410. "2001:db8::11"));
  3411. this->updater_->addRRset(*this->rrset_);
  3412. this->expected_rdatas_.clear();
  3413. this->expected_rdatas_.push_back("2001:db8::10");
  3414. this->expected_rdatas_.push_back("2001:db8::11");
  3415. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  3416. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  3417. this->empty_rdatas_);
  3418. // delete one RR from the previous set
  3419. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  3420. this->rrttl_));
  3421. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  3422. this->rrset_->getClass(),
  3423. "2001:db8::11"));
  3424. this->updater_->deleteRRset(*this->rrset_);
  3425. this->expected_rdatas_.clear();
  3426. this->expected_rdatas_.push_back("2001:db8::10");
  3427. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  3428. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  3429. this->empty_rdatas_);
  3430. // Commit the changes, confirm the entire changes applied.
  3431. this->updater_->commit();
  3432. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  3433. this->expected_rdatas_.clear();
  3434. this->expected_rdatas_.push_back("192.0.2.2");
  3435. this->expected_rdatas_.push_back("192.0.2.1");
  3436. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_, this->rrttl_,
  3437. ZoneFinder::SUCCESS, this->expected_rdatas_,
  3438. this->empty_rdatas_);
  3439. this->expected_rdatas_.clear();
  3440. this->expected_rdatas_.push_back("2001:db8::10");
  3441. doFindTest(*finder, newname, newtype, newtype, this->rrttl_,
  3442. ZoneFinder::SUCCESS, this->expected_rdatas_,
  3443. this->empty_rdatas_);
  3444. }
  3445. TYPED_TEST(DatabaseClientTest, invalidRdata) {
  3446. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  3447. EXPECT_THROW(finder->find(Name("invalidrdata.example.org."),
  3448. RRType::A()),
  3449. DataSourceError);
  3450. EXPECT_THROW(finder->find(Name("invalidrdata2.example.org."),
  3451. RRType::A(), ZoneFinder::FIND_DNSSEC),
  3452. DataSourceError);
  3453. }
  3454. TEST_F(MockDatabaseClientTest, missingNSEC) {
  3455. boost::shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  3456. /*
  3457. * FIXME: For now, we can't really distinguish this bogus input
  3458. * from not-signed zone so we can't throw. But once we can,
  3459. * enable the original test.
  3460. */
  3461. #if 0
  3462. EXPECT_THROW(finder->find(Name("badnsec2.example.org."), RRType::A(), NULL,
  3463. ZoneFinder::FIND_DNSSEC),
  3464. DataSourceError);
  3465. #endif
  3466. doFindTest(*finder, Name("badnsec2.example.org."), RRType::A(),
  3467. RRType::A(), this->rrttl_, ZoneFinder::NXDOMAIN,
  3468. this->expected_rdatas_, this->expected_sig_rdatas_);
  3469. }
  3470. /*
  3471. * Test correct use of the updater with a journal.
  3472. */
  3473. TYPED_TEST(DatabaseClientTest, journal) {
  3474. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3475. this->updater_->deleteRRset(*this->soa_);
  3476. this->updater_->deleteRRset(*this->rrset_);
  3477. this->soa_.reset(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  3478. this->rrttl_));
  3479. this->soa_->addRdata(rdata::createRdata(this->soa_->getType(),
  3480. this->soa_->getClass(),
  3481. "ns1.example.org. "
  3482. "admin.example.org. "
  3483. "1235 3600 1800 2419200 7200"));
  3484. this->updater_->addRRset(*this->soa_);
  3485. this->updater_->addRRset(*this->rrset_);
  3486. ASSERT_NO_THROW(this->updater_->commit());
  3487. std::vector<JournalEntry> expected;
  3488. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  3489. DatabaseAccessor::DIFF_DELETE,
  3490. "example.org.", "SOA", "3600",
  3491. "ns1.example.org. admin.example.org. "
  3492. "1234 3600 1800 2419200 7200"));
  3493. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  3494. DatabaseAccessor::DIFF_DELETE,
  3495. "www.example.org.", "A", "3600",
  3496. "192.0.2.2"));
  3497. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  3498. DatabaseAccessor::DIFF_ADD,
  3499. "example.org.", "SOA", "3600",
  3500. "ns1.example.org. admin.example.org. "
  3501. "1235 3600 1800 2419200 7200"));
  3502. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  3503. DatabaseAccessor::DIFF_ADD,
  3504. "www.example.org.", "A", "3600",
  3505. "192.0.2.2"));
  3506. this->checkJournal(expected);
  3507. }
  3508. TYPED_TEST(DatabaseClientTest, journalForNSEC3) {
  3509. // Similar to the previous test, but adding/deleting NSEC3 RRs, just to
  3510. // confirm that NSEC3 is not special for managing diffs.
  3511. const ConstRRsetPtr nsec3_rrset =
  3512. textToRRset(string(nsec3_hash) + ".example.org. 3600 IN NSEC3 " +
  3513. string(nsec3_rdata));
  3514. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3515. this->updater_->deleteRRset(*this->soa_);
  3516. this->updater_->deleteRRset(*nsec3_rrset);
  3517. this->soa_.reset(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  3518. this->rrttl_));
  3519. this->soa_->addRdata(rdata::createRdata(this->soa_->getType(),
  3520. this->soa_->getClass(),
  3521. "ns1.example.org. "
  3522. "admin.example.org. "
  3523. "1235 3600 1800 2419200 7200"));
  3524. this->updater_->addRRset(*this->soa_);
  3525. this->updater_->addRRset(*nsec3_rrset);
  3526. this->updater_->commit();
  3527. std::vector<JournalEntry> expected;
  3528. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  3529. DatabaseAccessor::DIFF_DELETE,
  3530. "example.org.", "SOA", "3600",
  3531. "ns1.example.org. admin.example.org. "
  3532. "1234 3600 1800 2419200 7200"));
  3533. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  3534. DatabaseAccessor::DIFF_DELETE,
  3535. string(nsec3_hash) + ".example.org.",
  3536. "NSEC3", "3600", nsec3_rdata));
  3537. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  3538. DatabaseAccessor::DIFF_ADD,
  3539. "example.org.", "SOA", "3600",
  3540. "ns1.example.org. admin.example.org. "
  3541. "1235 3600 1800 2419200 7200"));
  3542. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  3543. DatabaseAccessor::DIFF_ADD,
  3544. string(nsec3_hash) + ".example.org.",
  3545. "NSEC3", "3600", nsec3_rdata));
  3546. this->checkJournal(expected);
  3547. }
  3548. /*
  3549. * Push multiple delete-add sequences. Checks it is allowed and all is
  3550. * saved.
  3551. */
  3552. TYPED_TEST(DatabaseClientTest, journalMultiple) {
  3553. std::vector<JournalEntry> expected;
  3554. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3555. std::string soa_rdata = "ns1.example.org. admin.example.org. "
  3556. "1234 3600 1800 2419200 7200";
  3557. for (size_t i = 1; i < 100; ++ i) {
  3558. // Remove the old SOA
  3559. this->updater_->deleteRRset(*this->soa_);
  3560. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234 + i - 1,
  3561. DatabaseAccessor::DIFF_DELETE,
  3562. "example.org.", "SOA", "3600",
  3563. soa_rdata));
  3564. // Create a new SOA
  3565. soa_rdata = "ns1.example.org. admin.example.org. " +
  3566. lexical_cast<std::string>(1234 + i) + " 3600 1800 2419200 7200";
  3567. this->soa_.reset(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  3568. this->rrttl_));
  3569. this->soa_->addRdata(rdata::createRdata(this->soa_->getType(),
  3570. this->soa_->getClass(),
  3571. soa_rdata));
  3572. // Add the new SOA
  3573. this->updater_->addRRset(*this->soa_);
  3574. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234 + i,
  3575. DatabaseAccessor::DIFF_ADD,
  3576. "example.org.", "SOA", "3600",
  3577. soa_rdata));
  3578. }
  3579. ASSERT_NO_THROW(this->updater_->commit());
  3580. // Check the journal contains everything.
  3581. this->checkJournal(expected);
  3582. }
  3583. /*
  3584. * Test passing a forbidden sequence to it and expect it to throw.
  3585. *
  3586. * Note that we implicitly test in different testcases (these for add and
  3587. * delete) that if the journaling is false, it doesn't expect the order.
  3588. *
  3589. * In this test we don't check with the real databases as this case shouldn't
  3590. * contain backend specific behavior.
  3591. */
  3592. TEST_F(MockDatabaseClientTest, journalBadSequence) {
  3593. std::vector<JournalEntry> expected;
  3594. {
  3595. SCOPED_TRACE("Delete A before SOA");
  3596. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3597. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_),
  3598. isc::BadValue);
  3599. // Make sure the journal is empty now
  3600. this->checkJournal(expected);
  3601. }
  3602. {
  3603. SCOPED_TRACE("Add before delete");
  3604. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3605. EXPECT_THROW(this->updater_->addRRset(*this->soa_), isc::BadValue);
  3606. // Make sure the journal is empty now
  3607. this->checkJournal(expected);
  3608. }
  3609. {
  3610. SCOPED_TRACE("Add A before SOA");
  3611. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3612. // So far OK
  3613. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  3614. // But we miss the add SOA here
  3615. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), isc::BadValue);
  3616. // Make sure the journal contains only the first one
  3617. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  3618. DatabaseAccessor::DIFF_DELETE,
  3619. "example.org.", "SOA", "3600",
  3620. "ns1.example.org. admin.example.org. "
  3621. "1234 3600 1800 2419200 7200"));
  3622. this->checkJournal(expected);
  3623. }
  3624. {
  3625. SCOPED_TRACE("Commit before add");
  3626. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3627. // So far OK
  3628. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  3629. // Commit at the wrong time
  3630. EXPECT_THROW(updater_->commit(), isc::BadValue);
  3631. current_accessor_->checkJournal(expected);
  3632. }
  3633. {
  3634. SCOPED_TRACE("Delete two SOAs");
  3635. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3636. // So far OK
  3637. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  3638. // Delete the SOA again
  3639. EXPECT_THROW(this->updater_->deleteRRset(*this->soa_), isc::BadValue);
  3640. this->checkJournal(expected);
  3641. }
  3642. {
  3643. SCOPED_TRACE("Add two SOAs");
  3644. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3645. // So far OK
  3646. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  3647. // Still OK
  3648. EXPECT_NO_THROW(this->updater_->addRRset(*this->soa_));
  3649. // But this one is added again
  3650. EXPECT_THROW(this->updater_->addRRset(*this->soa_), isc::BadValue);
  3651. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  3652. DatabaseAccessor::DIFF_ADD,
  3653. "example.org.", "SOA", "3600",
  3654. "ns1.example.org. admin.example.org. "
  3655. "1234 3600 1800 2419200 7200"));
  3656. this->checkJournal(expected);
  3657. }
  3658. }
  3659. /*
  3660. * Test it rejects to store journals when we request it together with
  3661. * erasing the whole zone.
  3662. */
  3663. TYPED_TEST(DatabaseClientTest, journalOnErase) {
  3664. EXPECT_THROW(this->client_->getUpdater(this->zname_, true, true),
  3665. isc::BadValue);
  3666. }
  3667. /*
  3668. * Check that exception is propagated when the journal is not implemented.
  3669. */
  3670. TEST_F(MockDatabaseClientTest, journalNotImplemented) {
  3671. updater_ = client_->getUpdater(Name("null.example.org"), false, true);
  3672. EXPECT_THROW(updater_->deleteRRset(*soa_), isc::NotImplemented);
  3673. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  3674. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  3675. "ns1.example.org. admin.example.org. "
  3676. "1234 3600 1800 2419201 7200"));
  3677. EXPECT_THROW(updater_->addRRset(*soa_), isc::NotImplemented);
  3678. }
  3679. /*
  3680. * Test that different exceptions are propagated.
  3681. */
  3682. TEST_F(MockDatabaseClientTest, journalException) {
  3683. updater_ = client_->getUpdater(Name("bad.example.org"), false, true);
  3684. EXPECT_THROW(updater_->deleteRRset(*soa_), DataSourceError);
  3685. }
  3686. //
  3687. // Tests for the ZoneJournalReader
  3688. //
  3689. // Install a simple, commonly used diff sequence: making an update from one
  3690. // SOA to another. Return the end SOA RRset for the convenience of the caller.
  3691. ConstRRsetPtr
  3692. makeSimpleDiff(DataSourceClient& client, const Name& zname,
  3693. const RRClass& rrclass, ConstRRsetPtr begin_soa)
  3694. {
  3695. ZoneUpdaterPtr updater = client.getUpdater(zname, false, true);
  3696. updater->deleteRRset(*begin_soa);
  3697. RRsetPtr soa_end(new RRset(zname, rrclass, RRType::SOA(), RRTTL(3600)));
  3698. soa_end->addRdata(rdata::createRdata(RRType::SOA(), rrclass,
  3699. "ns1.example.org. admin.example.org. "
  3700. "1235 3600 1800 2419200 7200"));
  3701. updater->addRRset(*soa_end);
  3702. updater->commit();
  3703. return (soa_end);
  3704. }
  3705. TYPED_TEST(DatabaseClientTest, journalReader) {
  3706. // Check the simple case made by makeSimpleDiff.
  3707. ConstRRsetPtr soa_end = makeSimpleDiff(*this->client_, this->zname_,
  3708. this->qclass_, this->soa_);
  3709. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
  3710. this->client_->getJournalReader(this->zname_, 1234, 1235);
  3711. EXPECT_EQ(ZoneJournalReader::SUCCESS, result.first);
  3712. ZoneJournalReaderPtr jnl_reader = result.second;
  3713. ASSERT_TRUE(jnl_reader);
  3714. ConstRRsetPtr rrset = jnl_reader->getNextDiff();
  3715. ASSERT_TRUE(rrset);
  3716. rrsetCheck(this->soa_, rrset);
  3717. rrset = jnl_reader->getNextDiff();
  3718. ASSERT_TRUE(rrset);
  3719. rrsetCheck(soa_end, rrset);
  3720. rrset = jnl_reader->getNextDiff();
  3721. ASSERT_FALSE(rrset);
  3722. // Once it reaches the end of the sequence, further read attempt will
  3723. // result in exception.
  3724. EXPECT_THROW(jnl_reader->getNextDiff(), isc::InvalidOperation);
  3725. }
  3726. TYPED_TEST(DatabaseClientTest, readLargeJournal) {
  3727. // Similar to journalMultiple, but check that at a higher level.
  3728. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  3729. vector<ConstRRsetPtr> expected;
  3730. for (size_t i = 0; i < 100; ++i) {
  3731. // Create the old SOA and remove it, and record it in the expected list
  3732. RRsetPtr rrset1(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  3733. this->rrttl_));
  3734. string soa_rdata = "ns1.example.org. admin.example.org. " +
  3735. lexical_cast<std::string>(1234 + i) + " 3600 1800 2419200 7200";
  3736. rrset1->addRdata(rdata::createRdata(RRType::SOA(), this->qclass_,
  3737. soa_rdata));
  3738. this->updater_->deleteRRset(*rrset1);
  3739. expected.push_back(rrset1);
  3740. // Create a new SOA, add it, and record it.
  3741. RRsetPtr rrset2(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  3742. this->rrttl_));
  3743. soa_rdata = "ns1.example.org. admin.example.org. " +
  3744. lexical_cast<std::string>(1234 + i + 1) +
  3745. " 3600 1800 2419200 7200";
  3746. rrset2->addRdata(rdata::createRdata(RRType::SOA(), this->qclass_,
  3747. soa_rdata));
  3748. this->updater_->addRRset(*rrset2);
  3749. expected.push_back(rrset2);
  3750. }
  3751. this->updater_->commit();
  3752. ZoneJournalReaderPtr jnl_reader(this->client_->getJournalReader(
  3753. this->zname_, 1234, 1334).second);
  3754. ConstRRsetPtr actual;
  3755. int i = 0;
  3756. while ((actual = jnl_reader->getNextDiff()) != NULL) {
  3757. rrsetCheck(expected.at(i++), actual);
  3758. }
  3759. EXPECT_EQ(expected.size(), i); // we should have eaten all expected data
  3760. }
  3761. TYPED_TEST(DatabaseClientTest, readJournalForNoRange) {
  3762. makeSimpleDiff(*this->client_, this->zname_, this->qclass_, this->soa_);
  3763. // The specified range does not exist in the diff storage. The factory
  3764. // method should result in NO_SUCH_VERSION
  3765. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
  3766. this->client_->getJournalReader(this->zname_, 1200, 1235);
  3767. EXPECT_EQ(ZoneJournalReader::NO_SUCH_VERSION, result.first);
  3768. EXPECT_FALSE(result.second);
  3769. }
  3770. TYPED_TEST(DatabaseClientTest, journalReaderForNXZone) {
  3771. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
  3772. this->client_->getJournalReader(Name("nosuchzone"), 0, 1);
  3773. EXPECT_EQ(ZoneJournalReader::NO_SUCH_ZONE, result.first);
  3774. EXPECT_FALSE(result.second);
  3775. }
  3776. // A helper function for journalWithBadData. It installs a simple diff
  3777. // from one serial (of 'begin') to another ('begin' + 1), tweaking a specified
  3778. // field of data with some invalid value.
  3779. void
  3780. installBadDiff(MockAccessor& accessor, uint32_t begin,
  3781. DatabaseAccessor::DiffRecordParams modify_param,
  3782. const char* const data)
  3783. {
  3784. string data1[] = {"example.org.", "SOA", "3600", "ns. root. 1 1 1 1 1"};
  3785. string data2[] = {"example.org.", "SOA", "3600", "ns. root. 2 1 1 1 1"};
  3786. data1[modify_param] = data;
  3787. accessor.addRecordDiff(READONLY_ZONE_ID, begin,
  3788. DatabaseAccessor::DIFF_DELETE, data1);
  3789. accessor.addRecordDiff(READONLY_ZONE_ID, begin + 1,
  3790. DatabaseAccessor::DIFF_ADD, data2);
  3791. }
  3792. TEST_F(MockDatabaseClientTest, journalWithBadData) {
  3793. MockAccessor& mock_accessor =
  3794. dynamic_cast<MockAccessor&>(*current_accessor_);
  3795. // One of the fields from the data source is broken as an RR parameter.
  3796. // The journal reader should still be constructed, but getNextDiff()
  3797. // should result in exception.
  3798. installBadDiff(mock_accessor, 1, DatabaseAccessor::DIFF_NAME,
  3799. "example..org");
  3800. installBadDiff(mock_accessor, 3, DatabaseAccessor::DIFF_TYPE,
  3801. "bad-rrtype");
  3802. installBadDiff(mock_accessor, 5, DatabaseAccessor::DIFF_TTL,
  3803. "bad-ttl");
  3804. installBadDiff(mock_accessor, 7, DatabaseAccessor::DIFF_RDATA,
  3805. "bad rdata");
  3806. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 1, 2).
  3807. second->getNextDiff(), DataSourceError);
  3808. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 3, 4).
  3809. second->getNextDiff(), DataSourceError);
  3810. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 5, 6).
  3811. second->getNextDiff(), DataSourceError);
  3812. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 7, 8).
  3813. second->getNextDiff(), DataSourceError);
  3814. }
  3815. /// Let us test a little bit of NSEC3.
  3816. TYPED_TEST(DatabaseClientTest, findNSEC3) {
  3817. // Set up the faked hash calculator.
  3818. setNSEC3HashCreator(&this->test_nsec3_hash_creator_);
  3819. const DataSourceClient::FindResult
  3820. zone(this->client_->findZone(Name("example.org")));
  3821. ASSERT_EQ(result::SUCCESS, zone.code);
  3822. boost::shared_ptr<DatabaseClient::Finder> finder(
  3823. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  3824. // It'll complain if there is no NSEC3PARAM yet
  3825. EXPECT_THROW(finder->findNSEC3(Name("example.org"), false),
  3826. DataSourceError);
  3827. // And enable NSEC3 in the zone.
  3828. this->current_accessor_->enableNSEC3();
  3829. // The rest is in the function, it is shared with in-memory tests
  3830. performNSEC3Test(*finder, true);
  3831. }
  3832. TYPED_TEST(DatabaseClientTest, createZone) {
  3833. const Name new_name("example.com");
  3834. const DataSourceClient::FindResult
  3835. zone(this->client_->findZone(new_name));
  3836. ASSERT_EQ(result::NOTFOUND, zone.code);
  3837. // Adding a new zone; it should work and return true
  3838. ASSERT_TRUE(this->client_->createZone(new_name));
  3839. const DataSourceClient::FindResult
  3840. zone2(this->client_->findZone(new_name));
  3841. ASSERT_EQ(result::SUCCESS, zone2.code);
  3842. // And the second call should return false since
  3843. // it already exists
  3844. this->allowMoreTransaction(true);
  3845. ASSERT_FALSE(this->client_->createZone(new_name));
  3846. }
  3847. TYPED_TEST(DatabaseClientTest, createZoneRollbackOnLocked) {
  3848. const Name new_name("example.com");
  3849. isc::datasrc::ZoneUpdaterPtr updater =
  3850. this->client_->getUpdater(this->zname_, true);
  3851. this->allowMoreTransaction(false);
  3852. ASSERT_THROW(this->client_->createZone(new_name), DataSourceError);
  3853. // createZone started a transaction as well, but since it failed,
  3854. // it should have been rolled back. Roll back the other one as
  3855. // well, and the next attempt should succeed
  3856. updater.reset();
  3857. this->allowMoreTransaction(true);
  3858. ASSERT_TRUE(this->client_->createZone(new_name));
  3859. }
  3860. TYPED_TEST(DatabaseClientTest, createZoneRollbackOnExists) {
  3861. const Name new_name("example.com");
  3862. ASSERT_FALSE(this->client_->createZone(this->zname_));
  3863. // deleteZone started a transaction, but since the zone didn't even exist
  3864. // the transaction was not committed but should have been rolled back.
  3865. // The first transaction shouldn't leave any state, lock, etc, that
  3866. // would hinder the second attempt.
  3867. this->allowMoreTransaction(true);
  3868. ASSERT_TRUE(this->client_->createZone(new_name));
  3869. }
  3870. TYPED_TEST(DatabaseClientTest, deleteZone) {
  3871. // Check the zone currently exists.
  3872. EXPECT_EQ(result::SUCCESS, this->client_->findZone(this->zname_).code);
  3873. // Deleting an existing zone; it should work and return true (previously
  3874. // existed and is now deleted)
  3875. EXPECT_TRUE(this->client_->deleteZone(this->zname_));
  3876. // Now it's not found by findZone
  3877. EXPECT_EQ(result::NOTFOUND, this->client_->findZone(this->zname_).code);
  3878. // And the second call should return false since it doesn't exist any more
  3879. this->allowMoreTransaction(true);
  3880. EXPECT_FALSE(this->client_->deleteZone(this->zname_));
  3881. }
  3882. TYPED_TEST(DatabaseClientTest, deleteZoneRollbackOnLocked) {
  3883. isc::datasrc::ZoneUpdaterPtr updater =
  3884. this->client_->getUpdater(this->zname_, true);
  3885. // updater locks the DB so deleteZone() will fail.
  3886. this->allowMoreTransaction(false);
  3887. EXPECT_THROW(this->client_->deleteZone(this->zname_), DataSourceError);
  3888. // deleteZone started a transaction as well, but since it failed,
  3889. // it should have been rolled back. Roll back the other one as
  3890. // well, and the next attempt should succeed
  3891. updater.reset();
  3892. this->allowMoreTransaction(true);
  3893. EXPECT_TRUE(this->client_->deleteZone(this->zname_));
  3894. }
  3895. TYPED_TEST(DatabaseClientTest, deleteZoneRollbackOnNotFind) {
  3896. // attempt of deleting non-existent zone. result in false
  3897. const Name new_name("example.com");
  3898. EXPECT_FALSE(this->client_->deleteZone(new_name));
  3899. // deleteZone started a transaction, but since the zone didn't even exist
  3900. // the transaction was not committed but should have been rolled back.
  3901. // The first transaction shouldn't leave any state, lock, etc, that
  3902. // would hinder the second attempt.
  3903. this->allowMoreTransaction(true);
  3904. EXPECT_TRUE(this->client_->deleteZone(this->zname_));
  3905. }
  3906. TYPED_TEST_CASE(RRsetCollectionTest, TestAccessorTypes);
  3907. // This test fixture is templated so that we can share (most of) the test
  3908. // cases with different types of data sources. Note that in test cases
  3909. // we need to use 'this' to refer to member variables of the test class.
  3910. template <typename ACCESSOR_TYPE>
  3911. class RRsetCollectionTest : public DatabaseClientTest<ACCESSOR_TYPE> {
  3912. public:
  3913. RRsetCollectionTest() :
  3914. DatabaseClientTest<ACCESSOR_TYPE>(),
  3915. updater(this->client_->getUpdater(this->zname_, false)),
  3916. collection(updater->getRRsetCollection())
  3917. {}
  3918. ZoneUpdaterPtr updater;
  3919. isc::datasrc::RRsetCollectionBase& collection;
  3920. };
  3921. TYPED_TEST(RRsetCollectionTest, find) {
  3922. // Test the find() that returns ConstRRsetPtr
  3923. ConstRRsetPtr rrset = this->collection.find(Name("www.example.org."),
  3924. RRClass::IN(), RRType::A());
  3925. ASSERT_TRUE(rrset);
  3926. EXPECT_EQ(RRType::A(), rrset->getType());
  3927. EXPECT_EQ(RRTTL(3600), rrset->getTTL());
  3928. EXPECT_EQ(RRClass("IN"), rrset->getClass());
  3929. EXPECT_EQ(Name("www.example.org"), rrset->getName());
  3930. // foo.example.org doesn't exist
  3931. rrset = this->collection.find(Name("foo.example.org"), this->qclass_,
  3932. RRType::A());
  3933. EXPECT_FALSE(rrset);
  3934. // www.example.org exists, but not with MX
  3935. rrset = this->collection.find(Name("www.example.org"), this->qclass_,
  3936. RRType::MX());
  3937. EXPECT_FALSE(rrset);
  3938. // www.example.org exists, with AAAA
  3939. rrset = this->collection.find(Name("www.example.org"), this->qclass_,
  3940. RRType::AAAA());
  3941. EXPECT_TRUE(rrset);
  3942. // www.example.org with AAAA does not exist in RRClass::CH()
  3943. rrset = this->collection.find(Name("www.example.org"), RRClass::CH(),
  3944. RRType::AAAA());
  3945. EXPECT_FALSE(rrset);
  3946. // Out-of-zone find()s must not throw.
  3947. rrset = this->collection.find(Name("www.example.com"), this->qclass_,
  3948. RRType::A());
  3949. EXPECT_FALSE(rrset);
  3950. // "cname.example.org." with type CNAME should return the CNAME RRset
  3951. rrset = this->collection.find(Name("cname.example.org"), this->qclass_,
  3952. RRType::CNAME());
  3953. ASSERT_TRUE(rrset);
  3954. EXPECT_EQ(RRType::CNAME(), rrset->getType());
  3955. EXPECT_EQ(Name("cname.example.org"), rrset->getName());
  3956. // "cname.example.org." with type A should return nothing
  3957. rrset = this->collection.find(Name("cname.example.org"), this->qclass_,
  3958. RRType::A());
  3959. EXPECT_FALSE(rrset);
  3960. // "dname.example.org." with type DNAME should return the DNAME RRset
  3961. rrset = this->collection.find(Name("dname.example.org"), this->qclass_,
  3962. RRType::DNAME());
  3963. ASSERT_TRUE(rrset);
  3964. EXPECT_EQ(RRType::DNAME(), rrset->getType());
  3965. EXPECT_EQ(Name("dname.example.org"), rrset->getName());
  3966. // "below.dname.example.org." with type AAAA should return nothing
  3967. rrset = this->collection.find(Name("below.dname.example.org"),
  3968. this->qclass_, RRType::AAAA());
  3969. EXPECT_FALSE(rrset);
  3970. // "below.dname.example.org." with type A does not return the record
  3971. // (see top of file). See \c isc::datasrc::RRsetCollectionBase::find()
  3972. // documentation for details.
  3973. rrset = this->collection.find(Name("below.dname.example.org"),
  3974. this->qclass_, RRType::A());
  3975. EXPECT_FALSE(rrset);
  3976. // With the FIND_GLUE_OK option passed to ZoneFinder's find(),
  3977. // searching for "delegation.example.org." with type NS should
  3978. // return the NS record. Without FIND_GLUE_OK, ZoneFinder's find()
  3979. // would return DELEGATION and the find() below would return
  3980. // nothing.
  3981. rrset = this->collection.find(Name("delegation.example.org"),
  3982. this->qclass_, RRType::NS());
  3983. ASSERT_TRUE(rrset);
  3984. EXPECT_EQ(RRType::NS(), rrset->getType());
  3985. EXPECT_EQ(Name("delegation.example.org"), rrset->getName());
  3986. // With the NO_WILDCARD option passed to ZoneFinder's find(),
  3987. // searching for some "foo.wildcard.example.org." would make
  3988. // ZoneFinder's find() return NXDOMAIN, and the find() below should
  3989. // return nothing.
  3990. rrset = this->collection.find(Name("foo.wild.example.org"),
  3991. this->qclass_, RRType::A());
  3992. EXPECT_FALSE(rrset);
  3993. // Searching directly for "*.wild.example.org." should return the
  3994. // record.
  3995. rrset = this->collection.find(Name("*.wild.example.org"),
  3996. this->qclass_, RRType::A());
  3997. ASSERT_TRUE(rrset);
  3998. EXPECT_EQ(RRType::A(), rrset->getType());
  3999. EXPECT_EQ(Name("*.wild.example.org"), rrset->getName());
  4000. }
  4001. TYPED_TEST(RRsetCollectionTest, iteratorTest) {
  4002. // Iterators are currently not implemented.
  4003. EXPECT_THROW(this->collection.begin(), isc::NotImplemented);
  4004. EXPECT_THROW(this->collection.end(), isc::NotImplemented);
  4005. }
  4006. typedef RRsetCollectionTest<MockAccessor> MockRRsetCollectionTest;
  4007. TEST_F(MockRRsetCollectionTest, findError) {
  4008. // A test using the MockAccessor for checking that FindError is
  4009. // thrown properly if a find attempt using ZoneFinder results in a
  4010. // DataSourceError.
  4011. //
  4012. // The "dsexception.example.org." name is rigged by the MockAccessor
  4013. // to throw a DataSourceError.
  4014. EXPECT_THROW({
  4015. this->collection.find(Name("dsexception.example.org"), this->qclass_,
  4016. RRType::A());
  4017. }, RRsetCollectionError);
  4018. }
  4019. TYPED_TEST_CASE(RRsetCollectionAndUpdaterTest, TestAccessorTypes);
  4020. // This test fixture is templated so that we can share (most of) the test
  4021. // cases with different types of data sources. Note that in test cases
  4022. // we need to use 'this' to refer to member variables of the test class.
  4023. template <typename ACCESSOR_TYPE>
  4024. class RRsetCollectionAndUpdaterTest : public DatabaseClientTest<ACCESSOR_TYPE> {
  4025. public:
  4026. RRsetCollectionAndUpdaterTest() :
  4027. DatabaseClientTest<ACCESSOR_TYPE>(),
  4028. updater_(this->client_->getUpdater(this->zname_, false))
  4029. {}
  4030. ZoneUpdaterPtr updater_;
  4031. };
  4032. // Test that using addRRset() or deleteRRset() on the ZoneUpdater throws
  4033. // after an RRsetCollection is created.
  4034. TYPED_TEST(RRsetCollectionAndUpdaterTest, updateThrows) {
  4035. // 1. Addition test
  4036. // addRRset() must not throw.
  4037. this->updater_->addRRset(*this->rrset_);
  4038. // Now setup a new updater and call getRRsetCollection() on it.
  4039. this->updater_.reset();
  4040. this->updater_ = this->client_->getUpdater(this->zname_, false);
  4041. (void) this->updater_->getRRsetCollection();
  4042. // addRRset() must throw isc::InvalidOperation here.
  4043. EXPECT_THROW(this->updater_->addRRset(*this->rrset_),
  4044. isc::InvalidOperation);
  4045. // 2. Deletion test
  4046. // deleteRRset() must not throw.
  4047. this->updater_.reset();
  4048. this->updater_ = this->client_->getUpdater(this->zname_, false);
  4049. this->updater_->addRRset(*this->rrset_);
  4050. this->updater_->deleteRRset(*this->rrset_);
  4051. // Now setup a new updater and call getRRsetCollection() on it.
  4052. this->updater_.reset();
  4053. this->updater_ = this->client_->getUpdater(this->zname_, false);
  4054. this->updater_->addRRset(*this->rrset_);
  4055. (void) this->updater_->getRRsetCollection();
  4056. // deleteRRset() must throw isc::InvalidOperation here.
  4057. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_),
  4058. isc::InvalidOperation);
  4059. }
  4060. // Test that using an RRsetCollection after calling commit() on the
  4061. // ZoneUpdater throws, as the RRsetCollection is disabled.
  4062. TYPED_TEST(RRsetCollectionAndUpdaterTest, useAfterCommitThrows) {
  4063. isc::datasrc::RRsetCollectionBase& collection =
  4064. this->updater_->getRRsetCollection();
  4065. // find() must not throw here.
  4066. collection.find(Name("foo.wild.example.org"), this->qclass_, RRType::A());
  4067. this->updater_->commit();
  4068. // find() must throw RRsetCollectionError here, as the
  4069. // RRsetCollection is disabled.
  4070. EXPECT_THROW(collection.find(Name("foo.wild.example.org"),
  4071. this->qclass_, RRType::A()),
  4072. RRsetCollectionError);
  4073. }
  4074. }