database_unittest.cc 188 KB

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