database_unittest.cc 166 KB

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