database_unittest.cc 191 KB

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