database_unittest.cc 162 KB

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