database_unittest.cc 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985
  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 <boost/shared_ptr.hpp>
  15. #include <boost/lexical_cast.hpp>
  16. #include <gtest/gtest.h>
  17. #include <dns/name.h>
  18. #include <dns/rrttl.h>
  19. #include <dns/rrset.h>
  20. #include <exceptions/exceptions.h>
  21. #include <datasrc/database.h>
  22. #include <datasrc/zone.h>
  23. #include <datasrc/data_source.h>
  24. #include <datasrc/iterator.h>
  25. #include <datasrc/sqlite3_accessor.h>
  26. #include <testutils/dnsmessage_test.h>
  27. #include <map>
  28. using namespace isc::datasrc;
  29. using namespace std;
  30. // don't import the entire boost namespace. It will unexpectedly hide uint32_t
  31. // for some systems.
  32. using boost::shared_ptr;
  33. using boost::dynamic_pointer_cast;
  34. using boost::lexical_cast;
  35. using namespace isc::dns;
  36. namespace {
  37. // Imaginary zone IDs used in the mock accessor below.
  38. const int READONLY_ZONE_ID = 42;
  39. const int WRITABLE_ZONE_ID = 4200;
  40. // Commonly used test data
  41. const char* const TEST_RECORDS[][5] = {
  42. // some plain data
  43. {"www.example.org.", "A", "3600", "", "192.0.2.1"},
  44. {"www.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  45. {"www.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  46. {"www.example.org.", "NSEC", "3600", "", "www2.example.org. A AAAA NSEC RRSIG"},
  47. {"www.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  48. {"www2.example.org.", "A", "3600", "", "192.0.2.1"},
  49. {"www2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  50. {"www2.example.org.", "A", "3600", "", "192.0.2.2"},
  51. {"cname.example.org.", "CNAME", "3600", "", "www.example.org."},
  52. // some DNSSEC-'signed' data
  53. {"signed1.example.org.", "A", "3600", "", "192.0.2.1"},
  54. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  55. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  56. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  57. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  58. {"signed1.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  59. {"signedcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  60. {"signedcname1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  61. // special case might fail; sig is for cname, which isn't there (should be ignored)
  62. // (ignoring of 'normal' other type is done above by www.)
  63. {"acnamesig1.example.org.", "A", "3600", "", "192.0.2.1"},
  64. {"acnamesig1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  65. {"acnamesig1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  66. // let's pretend we have a database that is not careful
  67. // about the order in which it returns data
  68. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  69. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  70. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  71. {"signed2.example.org.", "A", "3600", "", "192.0.2.1"},
  72. {"signed2.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  73. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  74. {"signedcname2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  75. {"signedcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  76. {"acnamesig2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  77. {"acnamesig2.example.org.", "A", "3600", "", "192.0.2.1"},
  78. {"acnamesig2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  79. {"acnamesig3.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  80. {"acnamesig3.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  81. {"acnamesig3.example.org.", "A", "3600", "", "192.0.2.1"},
  82. {"ttldiff1.example.org.", "A", "3600", "", "192.0.2.1"},
  83. {"ttldiff1.example.org.", "A", "360", "", "192.0.2.2"},
  84. {"ttldiff2.example.org.", "A", "360", "", "192.0.2.1"},
  85. {"ttldiff2.example.org.", "A", "3600", "", "192.0.2.2"},
  86. // also add some intentionally bad data
  87. {"badcname1.example.org.", "A", "3600", "", "192.0.2.1"},
  88. {"badcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  89. {"badcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  90. {"badcname2.example.org.", "A", "3600", "", "192.0.2.1"},
  91. {"badcname3.example.org.", "CNAME", "3600", "", "www.example.org."},
  92. {"badcname3.example.org.", "CNAME", "3600", "", "www.example2.org."},
  93. {"badrdata.example.org.", "A", "3600", "", "bad"},
  94. {"badtype.example.org.", "BAD_TYPE", "3600", "", "192.0.2.1"},
  95. {"badttl.example.org.", "A", "badttl", "", "192.0.2.1"},
  96. {"badsig.example.org.", "A", "badttl", "", "192.0.2.1"},
  97. {"badsig.example.org.", "RRSIG", "3600", "", "A 5 3 3600 somebaddata 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  98. {"badsigtype.example.org.", "A", "3600", "", "192.0.2.1"},
  99. {"badsigtype.example.org.", "RRSIG", "3600", "TXT", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  100. // Data for testing delegation (with NS and DNAME)
  101. {"delegation.example.org.", "NS", "3600", "", "ns.example.com."},
  102. {"delegation.example.org.", "NS", "3600", "",
  103. "ns.delegation.example.org."},
  104. {"delegation.example.org.", "DS", "3600", "", "1 RSAMD5 2 abcd"},
  105. {"delegation.example.org.", "RRSIG", "3600", "", "NS 5 3 3600 "
  106. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  107. {"ns.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  108. {"deep.below.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  109. {"dname.example.org.", "A", "3600", "", "192.0.2.1"},
  110. {"dname.example.org.", "DNAME", "3600", "", "dname.example.com."},
  111. {"dname.example.org.", "RRSIG", "3600", "",
  112. "DNAME 5 3 3600 20000101000000 20000201000000 12345 "
  113. "example.org. FAKEFAKEFAKE"},
  114. {"below.dname.example.org.", "A", "3600", "", "192.0.2.1"},
  115. // Broken NS
  116. {"brokenns1.example.org.", "A", "3600", "", "192.0.2.1"},
  117. {"brokenns1.example.org.", "NS", "3600", "", "ns.example.com."},
  118. {"brokenns2.example.org.", "NS", "3600", "", "ns.example.com."},
  119. {"brokenns2.example.org.", "A", "3600", "", "192.0.2.1"},
  120. // Now double DNAME, to test failure mode
  121. {"baddname.example.org.", "DNAME", "3600", "", "dname1.example.com."},
  122. {"baddname.example.org.", "DNAME", "3600", "", "dname2.example.com."},
  123. // Put some data into apex (including NS) so we can check our NS
  124. // doesn't break anything
  125. {"example.org.", "SOA", "3600", "", "ns1.example.org. admin.example.org. "
  126. "1234 3600 1800 2419200 7200" },
  127. {"example.org.", "NS", "3600", "", "ns.example.com."},
  128. {"example.org.", "A", "3600", "", "192.0.2.1"},
  129. {"example.org.", "NSEC", "3600", "", "acnamesig1.example.org. NS A NSEC RRSIG"},
  130. {"example.org.", "RRSIG", "3600", "", "SOA 5 3 3600 20000101000000 "
  131. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  132. {"example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 "
  133. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  134. {"example.org.", "RRSIG", "3600", "", "NS 5 3 3600 20000101000000 "
  135. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  136. // This is because of empty domain test
  137. {"a.b.example.org.", "A", "3600", "", "192.0.2.1"},
  138. // Something for wildcards
  139. {"*.wild.example.org.", "A", "3600", "", "192.0.2.5"},
  140. {"*.wild.example.org.", "RRSIG", "3600", "A", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  141. {"*.wild.example.org.", "NSEC", "3600", "", "cancel.here.wild.example.org. A NSEC RRSIG"},
  142. {"*.wild.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  143. {"cancel.here.wild.example.org.", "AAAA", "3600", "", "2001:db8::5"},
  144. {"delegatedwild.example.org.", "NS", "3600", "", "ns.example.com."},
  145. {"*.delegatedwild.example.org.", "A", "3600", "", "192.0.2.5"},
  146. {"wild.*.foo.example.org.", "A", "3600", "", "192.0.2.5"},
  147. {"wild.*.foo.*.bar.example.org.", "A", "3600", "", "192.0.2.5"},
  148. {"wild.*.foo.*.bar.example.org.", "NSEC", "3600", "",
  149. "brokenns1.example.org. A NSEC"},
  150. {"bao.example.org.", "NSEC", "3600", "", "wild.*.foo.*.bar.example.org. NSEC"},
  151. {"*.cnamewild.example.org.", "CNAME", "3600", "", "www.example.org."},
  152. {"*.dnamewild.example.org.", "DNAME", "3600", "", "dname.example.com."},
  153. {"*.nswild.example.org.", "NS", "3600", "", "ns.example.com."},
  154. // For NSEC empty non-terminal
  155. {"l.example.org.", "NSEC", "3600", "", "empty.nonterminal.example.org. NSEC"},
  156. {"empty.nonterminal.example.org.", "A", "3600", "", "192.0.2.1"},
  157. // Invalid rdata
  158. {"invalidrdata.example.org.", "A", "3600", "", "Bunch of nonsense"},
  159. {"invalidrdata2.example.org.", "A", "3600", "", "192.0.2.1"},
  160. {"invalidrdata2.example.org.", "RRSIG", "3600", "", "Nonsense"},
  161. {NULL, NULL, NULL, NULL, NULL},
  162. };
  163. /*
  164. * An accessor with minimum implementation, keeping the original
  165. * "NotImplemented" methods.
  166. */
  167. class NopAccessor : public DatabaseAccessor {
  168. public:
  169. NopAccessor() : database_name_("mock_database")
  170. { }
  171. virtual std::pair<bool, int> getZone(const std::string& name) const {
  172. if (name == "example.org.") {
  173. return (std::pair<bool, int>(true, READONLY_ZONE_ID));
  174. } else if (name == "null.example.org.") {
  175. return (std::pair<bool, int>(true, 13));
  176. } else if (name == "empty.example.org.") {
  177. return (std::pair<bool, int>(true, 0));
  178. } else if (name == "bad.example.org.") {
  179. return (std::pair<bool, int>(true, -1));
  180. } else {
  181. return (std::pair<bool, int>(false, 0));
  182. }
  183. }
  184. virtual shared_ptr<DatabaseAccessor> clone() {
  185. // This accessor is stateless, so we can simply return a new instance.
  186. return (shared_ptr<DatabaseAccessor>(new NopAccessor));
  187. }
  188. virtual std::pair<bool, int> startUpdateZone(const std::string&, bool) {
  189. // return dummy value. unused anyway.
  190. return (pair<bool, int>(true, 0));
  191. }
  192. virtual void startTransaction() {}
  193. virtual void commit() {}
  194. virtual void rollback() {}
  195. virtual void addRecordToZone(const string (&)[ADD_COLUMN_COUNT]) {}
  196. virtual void deleteRecordInZone(const string (&)[DEL_PARAM_COUNT]) {}
  197. virtual void addRecordDiff(int, uint32_t, DiffOperation,
  198. const std::string (&)[DIFF_PARAM_COUNT]) {}
  199. virtual const std::string& getDBName() const {
  200. return (database_name_);
  201. }
  202. virtual IteratorContextPtr getRecords(const std::string&, int, bool)
  203. const
  204. {
  205. isc_throw(isc::NotImplemented,
  206. "This database datasource can't be iterated");
  207. }
  208. virtual IteratorContextPtr getAllRecords(int) const {
  209. isc_throw(isc::NotImplemented,
  210. "This database datasource can't be iterated");
  211. }
  212. virtual IteratorContextPtr getDiffs(int, uint32_t, uint32_t) const {
  213. isc_throw(isc::NotImplemented,
  214. "This database datasource can't be iterated");
  215. }
  216. virtual std::string findPreviousName(int, const std::string&) const {
  217. isc_throw(isc::NotImplemented,
  218. "This data source doesn't support DNSSEC");
  219. }
  220. private:
  221. const std::string database_name_;
  222. };
  223. /**
  224. * Single journal entry in the mock database.
  225. *
  226. * All the members there are public for simplicity, as it only stores data.
  227. * We use the implicit constructor and operator. The members can't be const
  228. * because of the assignment operator (used in the vectors).
  229. */
  230. struct JournalEntry {
  231. JournalEntry(int id, uint32_t serial,
  232. DatabaseAccessor::DiffOperation operation,
  233. const std::string (&data)[DatabaseAccessor::DIFF_PARAM_COUNT])
  234. : id_(id), serial_(serial), operation_(operation)
  235. {
  236. data_[DatabaseAccessor::DIFF_NAME] = data[DatabaseAccessor::DIFF_NAME];
  237. data_[DatabaseAccessor::DIFF_TYPE] = data[DatabaseAccessor::DIFF_TYPE];
  238. data_[DatabaseAccessor::DIFF_TTL] = data[DatabaseAccessor::DIFF_TTL];
  239. data_[DatabaseAccessor::DIFF_RDATA] =
  240. data[DatabaseAccessor::DIFF_RDATA];
  241. }
  242. JournalEntry(int id, uint32_t serial,
  243. DatabaseAccessor::DiffOperation operation,
  244. const std::string& name, const std::string& type,
  245. const std::string& ttl, const std::string& rdata):
  246. id_(id), serial_(serial), operation_(operation)
  247. {
  248. data_[DatabaseAccessor::DIFF_NAME] = name;
  249. data_[DatabaseAccessor::DIFF_TYPE] = type;
  250. data_[DatabaseAccessor::DIFF_TTL] = ttl;
  251. data_[DatabaseAccessor::DIFF_RDATA] = rdata;
  252. }
  253. int id_;
  254. uint32_t serial_;
  255. DatabaseAccessor::DiffOperation operation_;
  256. std::string data_[DatabaseAccessor::DIFF_PARAM_COUNT];
  257. bool operator==(const JournalEntry& other) const {
  258. bool data_equal(true);
  259. for (size_t i(0); i < DatabaseAccessor::DIFF_PARAM_COUNT; ++ i) {
  260. data_equal = data_equal && (data_[i] == other.data_[i]);
  261. }
  262. return (id_ == other.id_ && serial_ == other.serial_ &&
  263. operation_ == other.operation_ && data_equal);
  264. }
  265. };
  266. /*
  267. * A virtual database accessor that pretends it contains single zone --
  268. * example.org.
  269. *
  270. * It has the same getZone method as NopConnection, but it provides
  271. * implementation of the optional functionality.
  272. */
  273. class MockAccessor : public NopAccessor {
  274. // Type of mock database "row"s. This is a map whose keys are the
  275. // own names. We internally sort them by the name comparison order.
  276. struct NameCompare : public binary_function<string, string, bool> {
  277. bool operator()(const string& n1, const string& n2) const {
  278. return (Name(n1).compare(Name(n2)).getOrder() < 0);
  279. }
  280. };
  281. typedef std::map<std::string,
  282. std::vector< std::vector<std::string> >,
  283. NameCompare > Domains;
  284. public:
  285. MockAccessor() : rollbacked_(false), did_transaction_(false) {
  286. readonly_records_ = &readonly_records_master_;
  287. update_records_ = &update_records_master_;
  288. empty_records_ = &empty_records_master_;
  289. journal_entries_ = &journal_entries_master_;
  290. fillData();
  291. }
  292. virtual shared_ptr<DatabaseAccessor> clone() {
  293. shared_ptr<MockAccessor> cloned_accessor(new MockAccessor());
  294. cloned_accessor->readonly_records_ = &readonly_records_master_;
  295. cloned_accessor->update_records_ = &update_records_master_;
  296. cloned_accessor->empty_records_ = &empty_records_master_;
  297. cloned_accessor->journal_entries_ = &journal_entries_master_;
  298. latest_clone_ = cloned_accessor;
  299. return (cloned_accessor);
  300. }
  301. virtual void startTransaction() {
  302. // Currently we only use this transaction for simple read-only
  303. // operations. So we just make a local copy of the data (we don't
  304. // care about what happens after commit() or rollback()).
  305. // Obviously as a consequence, if a test case tries to make multiple
  306. // transactions on a single mock accessor it will fail.
  307. // Check any attempt of multiple transactions
  308. if (did_transaction_) {
  309. isc_throw(isc::Unexpected, "MockAccessor::startTransaction() "
  310. "called multiple times - likely a bug in the test");
  311. }
  312. readonly_records_copy_ = *readonly_records_;
  313. readonly_records_ = &readonly_records_copy_;
  314. did_transaction_ = true;
  315. }
  316. private:
  317. class MockNameIteratorContext : public IteratorContext {
  318. public:
  319. MockNameIteratorContext(const MockAccessor& mock_accessor, int zone_id,
  320. const std::string& name, bool subdomains) :
  321. searched_name_(name), cur_record_(0)
  322. {
  323. // 'hardcoded' names to trigger exceptions
  324. // On these names some exceptions are thrown, to test the robustness
  325. // of the find() method.
  326. if (searched_name_ == "dsexception.in.search.") {
  327. isc_throw(DataSourceError, "datasource exception on search");
  328. } else if (searched_name_ == "iscexception.in.search.") {
  329. isc_throw(isc::Exception, "isc exception on search");
  330. } else if (searched_name_ == "basicexception.in.search.") {
  331. throw std::exception();
  332. }
  333. cur_record_ = 0;
  334. const Domains& cur_records = mock_accessor.getMockRecords(zone_id);
  335. if (cur_records.count(name) > 0) {
  336. // we're not aiming for efficiency in this test, simply
  337. // copy the relevant vector from records
  338. cur_name = cur_records.find(name)->second;
  339. } else if (subdomains) {
  340. cur_name.clear();
  341. // Just walk everything and check if it is a subdomain.
  342. // If it is, just copy all data from there.
  343. for (Domains::const_iterator i(cur_records.begin());
  344. i != cur_records.end(); ++i) {
  345. const Name local(i->first);
  346. if (local.compare(Name(name)).getRelation() ==
  347. isc::dns::NameComparisonResult::SUBDOMAIN) {
  348. cur_name.insert(cur_name.end(), i->second.begin(),
  349. i->second.end());
  350. }
  351. }
  352. } else {
  353. cur_name.clear();
  354. }
  355. }
  356. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) {
  357. if (searched_name_ == "dsexception.in.getnext.") {
  358. isc_throw(DataSourceError, "datasource exception on getnextrecord");
  359. } else if (searched_name_ == "iscexception.in.getnext.") {
  360. isc_throw(isc::Exception, "isc exception on getnextrecord");
  361. } else if (searched_name_ == "basicexception.in.getnext.") {
  362. throw std::exception();
  363. }
  364. if (cur_record_ < cur_name.size()) {
  365. for (size_t i = 0; i < COLUMN_COUNT; ++i) {
  366. columns[i] = cur_name[cur_record_][i];
  367. }
  368. cur_record_++;
  369. return (true);
  370. } else {
  371. return (false);
  372. }
  373. }
  374. private:
  375. const std::string searched_name_;
  376. int cur_record_;
  377. std::vector< std::vector<std::string> > cur_name;
  378. };
  379. class MockIteratorContext : public IteratorContext {
  380. private:
  381. int step;
  382. const Domains& domains_;
  383. public:
  384. MockIteratorContext(const Domains& domains) :
  385. step(0), domains_(domains)
  386. { }
  387. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  388. // A special case: if the given set of domains is already empty,
  389. // we always return false.
  390. if (domains_.empty()) {
  391. return (false);
  392. }
  393. // Return faked data for tests
  394. switch (step ++) {
  395. case 0:
  396. data[DatabaseAccessor::NAME_COLUMN] = "example.org";
  397. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  398. data[DatabaseAccessor::TTL_COLUMN] = "3600";
  399. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  400. return (true);
  401. case 1:
  402. data[DatabaseAccessor::NAME_COLUMN] = "example.org";
  403. data[DatabaseAccessor::TYPE_COLUMN] = "SOA";
  404. data[DatabaseAccessor::TTL_COLUMN] = "3600";
  405. data[DatabaseAccessor::RDATA_COLUMN] = "ns1.example.org. admin.example.org. "
  406. "1234 3600 1800 2419200 7200";
  407. return (true);
  408. case 2:
  409. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  410. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  411. data[DatabaseAccessor::TTL_COLUMN] = "300";
  412. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  413. return (true);
  414. case 3:
  415. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  416. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  417. data[DatabaseAccessor::TTL_COLUMN] = "300";
  418. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.2";
  419. return (true);
  420. case 4:
  421. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  422. data[DatabaseAccessor::TYPE_COLUMN] = "AAAA";
  423. data[DatabaseAccessor::TTL_COLUMN] = "300";
  424. data[DatabaseAccessor::RDATA_COLUMN] = "2001:db8::1";
  425. return (true);
  426. case 5:
  427. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  428. data[DatabaseAccessor::TYPE_COLUMN] = "AAAA";
  429. data[DatabaseAccessor::TTL_COLUMN] = "300";
  430. data[DatabaseAccessor::RDATA_COLUMN] = "2001:db8::2";
  431. return (true);
  432. case 6:
  433. data[DatabaseAccessor::NAME_COLUMN] = "ttldiff.example.org";
  434. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  435. data[DatabaseAccessor::TTL_COLUMN] = "300";
  436. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  437. return (true);
  438. case 7:
  439. data[DatabaseAccessor::NAME_COLUMN] = "ttldiff.example.org";
  440. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  441. data[DatabaseAccessor::TTL_COLUMN] = "600";
  442. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.2";
  443. return (true);
  444. default:
  445. ADD_FAILURE() <<
  446. "Request past the end of iterator context";
  447. case 8:
  448. return (false);
  449. }
  450. }
  451. };
  452. class EmptyIteratorContext : public IteratorContext {
  453. public:
  454. virtual bool getNext(string(&)[COLUMN_COUNT]) {
  455. return (false);
  456. }
  457. };
  458. class BadIteratorContext : public IteratorContext {
  459. private:
  460. int step;
  461. public:
  462. BadIteratorContext() :
  463. step(0)
  464. { }
  465. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  466. switch (step ++) {
  467. case 0:
  468. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  469. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  470. data[DatabaseAccessor::TTL_COLUMN] = "300";
  471. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  472. return (true);
  473. case 1:
  474. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  475. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  476. data[DatabaseAccessor::TTL_COLUMN] = "301";
  477. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.2";
  478. return (true);
  479. default:
  480. ADD_FAILURE() <<
  481. "Request past the end of iterator context";
  482. case 2:
  483. return (false);
  484. }
  485. }
  486. };
  487. public:
  488. virtual IteratorContextPtr getAllRecords(int id) const {
  489. if (id == READONLY_ZONE_ID) {
  490. return (IteratorContextPtr(new MockIteratorContext(
  491. *readonly_records_)));
  492. } else if (id == 13) {
  493. return (IteratorContextPtr());
  494. } else if (id == 0) {
  495. return (IteratorContextPtr(new EmptyIteratorContext()));
  496. } else if (id == -1) {
  497. return (IteratorContextPtr(new BadIteratorContext()));
  498. } else {
  499. isc_throw(isc::Unexpected, "Unknown zone ID");
  500. }
  501. }
  502. virtual IteratorContextPtr getRecords(const std::string& name, int id,
  503. bool subdomains) const
  504. {
  505. if (id == READONLY_ZONE_ID || id == WRITABLE_ZONE_ID) {
  506. return (IteratorContextPtr(
  507. new MockNameIteratorContext(*this, id, name,
  508. subdomains)));
  509. } else {
  510. // This iterator is bogus, but for the cases tested below that's
  511. // sufficient.
  512. return (IteratorContextPtr(
  513. new MockNameIteratorContext(*this, READONLY_ZONE_ID,
  514. name, subdomains)));
  515. }
  516. }
  517. virtual pair<bool, int> startUpdateZone(const std::string& zone_name,
  518. bool replace)
  519. {
  520. const pair<bool, int> zone_info = getZone(zone_name);
  521. if (!zone_info.first) {
  522. return (pair<bool, int>(false, 0));
  523. }
  524. // Prepare the record set for update. If replacing the existing one,
  525. // we use an empty set; otherwise we use a writable copy of the
  526. // original.
  527. if (replace) {
  528. update_records_->clear();
  529. } else {
  530. *update_records_ = *readonly_records_;
  531. }
  532. if (zone_name == "bad.example.org.") {
  533. return (pair<bool, int>(true, -1));
  534. } else if (zone_name == "null.example.org.") {
  535. return (pair<bool, int>(true, 13));
  536. } else {
  537. return (pair<bool, int>(true, WRITABLE_ZONE_ID));
  538. }
  539. }
  540. virtual void commit() {
  541. *readonly_records_ = *update_records_;
  542. }
  543. virtual void rollback() {
  544. // Special hook: if something with a name of "throw.example.org"
  545. // has been added, trigger an imaginary unexpected event with an
  546. // exception.
  547. if (update_records_->count("throw.example.org.") > 0) {
  548. isc_throw(DataSourceError, "unexpected failure in rollback");
  549. }
  550. rollbacked_ = true;
  551. }
  552. virtual void addRecordToZone(const string (&columns)[ADD_COLUMN_COUNT]) {
  553. // Copy the current value to cur_name. If it doesn't exist,
  554. // operator[] will create a new one.
  555. cur_name_ = (*update_records_)[columns[DatabaseAccessor::ADD_NAME]];
  556. vector<string> record_columns;
  557. record_columns.push_back(columns[DatabaseAccessor::ADD_TYPE]);
  558. record_columns.push_back(columns[DatabaseAccessor::ADD_TTL]);
  559. record_columns.push_back(columns[DatabaseAccessor::ADD_SIGTYPE]);
  560. record_columns.push_back(columns[DatabaseAccessor::ADD_RDATA]);
  561. record_columns.push_back(columns[DatabaseAccessor::ADD_NAME]);
  562. // copy back the added entry
  563. cur_name_.push_back(record_columns);
  564. (*update_records_)[columns[DatabaseAccessor::ADD_NAME]] = cur_name_;
  565. // remember this one so that test cases can check it.
  566. copy(columns, columns + DatabaseAccessor::ADD_COLUMN_COUNT,
  567. columns_lastadded_);
  568. }
  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. virtual void deleteRecordInZone(const string (&params)[DEL_PARAM_COUNT]) {
  581. vector<vector<string> >& records =
  582. (*update_records_)[params[DatabaseAccessor::DEL_NAME]];
  583. records.erase(remove_if(records.begin(), records.end(),
  584. deleteMatch(
  585. params[DatabaseAccessor::DEL_TYPE],
  586. params[DatabaseAccessor::DEL_RDATA])),
  587. records.end());
  588. if (records.empty()) {
  589. (*update_records_).erase(params[DatabaseAccessor::DEL_NAME]);
  590. }
  591. }
  592. //
  593. // Helper methods to keep track of some update related activities
  594. //
  595. bool isRollbacked() const {
  596. return (rollbacked_);
  597. }
  598. const string* getLastAdded() const {
  599. return (columns_lastadded_);
  600. }
  601. // This allows the test code to get the accessor used in an update context
  602. shared_ptr<const MockAccessor> getLatestClone() const {
  603. return (latest_clone_);
  604. }
  605. virtual std::string findPreviousName(int id, const std::string& rname)
  606. const
  607. {
  608. if (id == -1) {
  609. isc_throw(isc::NotImplemented, "Test not implemented behaviour");
  610. } else if (id == READONLY_ZONE_ID) {
  611. // For some specific names we intentionally return broken or
  612. // unexpected result.
  613. if (rname == "org.example.badnsec2.") {
  614. return ("badnsec1.example.org.");
  615. } else if (rname == "org.example.brokenname.") {
  616. return ("brokenname...example.org.");
  617. } else if (rname == "org.example.notimplnsec." ||
  618. rname == "org.example.wild.here.") {
  619. isc_throw(isc::NotImplemented, "Not implemented in this test");
  620. }
  621. // For the general case, we search for the first name N in the
  622. // domains that meets N >= reverse(rname) using lower_bound.
  623. // The "previous name" is the name of the previous entry of N.
  624. // Note that Domains are internally sorted by the Name comparison
  625. // order. Due to the API requirement we are given a reversed
  626. // name (rname), so we need to reverse it again to convert it
  627. // to the original name.
  628. Domains::const_iterator it(readonly_records_->lower_bound(
  629. Name(rname).reverse().toText()));
  630. if (it == readonly_records_->begin()) {
  631. isc_throw(isc::Unexpected, "Unexpected name");
  632. }
  633. if (it == readonly_records_->end()) {
  634. return ((*readonly_records_->rbegin()).first);
  635. }
  636. return ((*(--it)).first);
  637. } else {
  638. isc_throw(isc::Unexpected, "Unknown zone ID");
  639. }
  640. }
  641. virtual void addRecordDiff(int id, uint32_t serial,
  642. DiffOperation operation,
  643. const std::string (&data)[DIFF_PARAM_COUNT])
  644. {
  645. if (id == 13) { // The null zone doesn't support journaling
  646. isc_throw(isc::NotImplemented, "Test not implemented behaviour");
  647. } else if (id == -1) { // Bad zone throws
  648. isc_throw(DataSourceError, "Test error");
  649. } else {
  650. journal_entries_->push_back(JournalEntry(id, serial, operation,
  651. data));
  652. }
  653. }
  654. // Check the journal is as expected and clear the journal
  655. void checkJournal(const std::vector<JournalEntry> &expected) {
  656. std::vector<JournalEntry> journal;
  657. // Clean the journal, but keep local copy to check
  658. journal.swap(*journal_entries_);
  659. ASSERT_EQ(expected.size(), journal.size());
  660. for (size_t i(0); i < expected.size(); ++ i) {
  661. EXPECT_TRUE(expected[i] == journal[i]);
  662. }
  663. }
  664. private:
  665. // The following member variables are storage and/or update work space
  666. // of the test zone. The "master"s are the real objects that contain
  667. // the data, and they are shared among all accessors cloned from
  668. // an initially created one. The "copy" data will be used for read-only
  669. // transaction. The pointer members allow the sharing.
  670. // "readonly" is for normal lookups. "update" is the workspace for
  671. // updates. When update starts it will be initialized either as an
  672. // empty set (when replacing the entire zone) or as a copy of the
  673. // "readonly" one. "empty" is a sentinel to produce negative results.
  674. Domains readonly_records_master_;
  675. Domains readonly_records_copy_;
  676. Domains* readonly_records_;
  677. Domains update_records_master_;
  678. Domains* update_records_;
  679. const Domains empty_records_master_;
  680. const Domains* empty_records_;
  681. // The journal data
  682. std::vector<JournalEntry> journal_entries_master_;
  683. std::vector<JournalEntry>* journal_entries_;
  684. // used as temporary storage after searchForRecord() and during
  685. // getNextRecord() calls, as well as during the building of the
  686. // fake data
  687. std::vector< std::vector<std::string> > cur_name_;
  688. // The columns that were most recently added via addRecordToZone()
  689. string columns_lastadded_[ADD_COLUMN_COUNT];
  690. // Whether rollback operation has been performed for the database.
  691. // Not useful except for purely testing purpose.
  692. bool rollbacked_;
  693. // Remember the mock accessor that was last cloned
  694. boost::shared_ptr<MockAccessor> latest_clone_;
  695. // Internal flag for duplicate check
  696. bool did_transaction_;
  697. const Domains& getMockRecords(int zone_id) const {
  698. if (zone_id == READONLY_ZONE_ID) {
  699. return (*readonly_records_);
  700. } else if (zone_id == WRITABLE_ZONE_ID) {
  701. return (*update_records_);
  702. }
  703. return (*empty_records_);
  704. }
  705. // Adds one record to the current name in the database
  706. // The actual data will not be added to 'records' until
  707. // addCurName() is called
  708. void addRecord(const std::string& type,
  709. const std::string& ttl,
  710. const std::string& sigtype,
  711. const std::string& rdata) {
  712. std::vector<std::string> columns;
  713. columns.push_back(type);
  714. columns.push_back(ttl);
  715. columns.push_back(sigtype);
  716. columns.push_back(rdata);
  717. cur_name_.push_back(columns);
  718. }
  719. // Adds all records we just built with calls to addRecords
  720. // to the actual fake database. This will clear cur_name_,
  721. // so we can immediately start adding new records.
  722. void addCurName(const std::string& name) {
  723. ASSERT_EQ(0, readonly_records_->count(name));
  724. // Append the name to all of them
  725. for (std::vector<std::vector<std::string> >::iterator
  726. i(cur_name_.begin()); i != cur_name_.end(); ++ i) {
  727. i->push_back(name);
  728. }
  729. (*readonly_records_)[name] = cur_name_;
  730. cur_name_.clear();
  731. }
  732. // Fills the database with zone data.
  733. // This method constructs a number of resource records (with addRecord),
  734. // which will all be added for one domain name to the fake database
  735. // (with addCurName). So for instance the first set of calls create
  736. // data for the name 'www.example.org', which will consist of one A RRset
  737. // of one record, and one AAAA RRset of two records.
  738. // The order in which they are added is the order in which getNextRecord()
  739. // will return them (so we can test whether find() etc. support data that
  740. // might not come in 'normal' order)
  741. // It shall immediately fail if you try to add the same name twice.
  742. void fillData() {
  743. const char* prev_name = NULL;
  744. for (int i = 0; TEST_RECORDS[i][0] != NULL; ++i) {
  745. if (prev_name != NULL &&
  746. strcmp(prev_name, TEST_RECORDS[i][0]) != 0) {
  747. addCurName(prev_name);
  748. }
  749. prev_name = TEST_RECORDS[i][0];
  750. addRecord(TEST_RECORDS[i][1], TEST_RECORDS[i][2],
  751. TEST_RECORDS[i][3], TEST_RECORDS[i][4]);
  752. }
  753. addCurName(prev_name);
  754. }
  755. };
  756. // This tests the default getRecords behaviour, throwing NotImplemented
  757. TEST(DatabaseConnectionTest, getRecords) {
  758. EXPECT_THROW(NopAccessor().getRecords(".", 1, false),
  759. isc::NotImplemented);
  760. }
  761. // This tests the default getAllRecords behaviour, throwing NotImplemented
  762. TEST(DatabaseConnectionTest, getAllRecords) {
  763. // The parameters don't matter
  764. EXPECT_THROW(NopAccessor().getAllRecords(1),
  765. isc::NotImplemented);
  766. }
  767. // This test fixture is templated so that we can share (most of) the test
  768. // cases with different types of data sources. Note that in test cases
  769. // we need to use 'this' to refer to member variables of the test class.
  770. template <typename ACCESSOR_TYPE>
  771. class DatabaseClientTest : public ::testing::Test {
  772. public:
  773. DatabaseClientTest() : zname_("example.org"), qname_("www.example.org"),
  774. qclass_(RRClass::IN()), qtype_(RRType::A()),
  775. rrttl_(3600)
  776. {
  777. createClient();
  778. // set up the commonly used finder.
  779. DataSourceClient::FindResult zone(client_->findZone(zname_));
  780. assert(zone.code == result::SUCCESS);
  781. finder_ = dynamic_pointer_cast<DatabaseClient::Finder>(
  782. zone.zone_finder);
  783. // Test IN/A RDATA to be added in update tests. Intentionally using
  784. // different data than the initial data configured in the MockAccessor.
  785. rrset_.reset(new RRset(qname_, qclass_, qtype_, rrttl_));
  786. rrset_->addRdata(rdata::createRdata(rrset_->getType(),
  787. rrset_->getClass(), "192.0.2.2"));
  788. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  789. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  790. "ns1.example.org. admin.example.org. "
  791. "1234 3600 1800 2419200 7200"));
  792. // And its RRSIG. Also different from the configured one.
  793. rrsigset_.reset(new RRset(qname_, qclass_, RRType::RRSIG(),
  794. rrttl_));
  795. rrsigset_->addRdata(rdata::createRdata(rrsigset_->getType(),
  796. rrsigset_->getClass(),
  797. "A 5 3 0 20000101000000 "
  798. "20000201000000 0 example.org. "
  799. "FAKEFAKEFAKE"));
  800. }
  801. /*
  802. * We initialize the client from a function, so we can call it multiple
  803. * times per test.
  804. */
  805. void createClient() {
  806. current_accessor_ = new ACCESSOR_TYPE();
  807. is_mock_ = (dynamic_cast<MockAccessor*>(current_accessor_) != NULL);
  808. client_.reset(new DatabaseClient(qclass_,
  809. shared_ptr<ACCESSOR_TYPE>(
  810. current_accessor_)));
  811. }
  812. /**
  813. * Check the zone finder is a valid one and references the zone ID and
  814. * database available here.
  815. */
  816. void checkZoneFinder(const DataSourceClient::FindResult& zone) {
  817. ASSERT_NE(ZoneFinderPtr(), zone.zone_finder) << "No zone finder";
  818. shared_ptr<DatabaseClient::Finder> finder(
  819. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  820. ASSERT_NE(shared_ptr<DatabaseClient::Finder>(), finder) <<
  821. "Wrong type of finder";
  822. if (is_mock_) {
  823. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  824. }
  825. EXPECT_EQ(current_accessor_, &finder->getAccessor());
  826. }
  827. shared_ptr<DatabaseClient::Finder> getFinder() {
  828. DataSourceClient::FindResult zone(client_->findZone(zname_));
  829. EXPECT_EQ(result::SUCCESS, zone.code);
  830. shared_ptr<DatabaseClient::Finder> finder(
  831. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  832. if (is_mock_) {
  833. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  834. }
  835. return (finder);
  836. }
  837. // Helper methods for update tests
  838. bool isRollbacked(bool expected = false) const {
  839. if (is_mock_) {
  840. const MockAccessor& mock_accessor =
  841. dynamic_cast<const MockAccessor&>(*update_accessor_);
  842. return (mock_accessor.isRollbacked());
  843. } else {
  844. return (expected);
  845. }
  846. }
  847. void checkLastAdded(const char* const expected[]) const {
  848. if (is_mock_) {
  849. const MockAccessor* mock_accessor =
  850. dynamic_cast<const MockAccessor*>(current_accessor_);
  851. for (int i = 0; i < DatabaseAccessor::ADD_COLUMN_COUNT; ++i) {
  852. EXPECT_EQ(expected[i],
  853. mock_accessor->getLatestClone()->getLastAdded()[i]);
  854. }
  855. }
  856. }
  857. void setUpdateAccessor() {
  858. if (is_mock_) {
  859. const MockAccessor* mock_accessor =
  860. dynamic_cast<const MockAccessor*>(current_accessor_);
  861. update_accessor_ = mock_accessor->getLatestClone();
  862. }
  863. }
  864. // Some tests only work for MockAccessor. We remember whether our accessor
  865. // is of that type.
  866. bool is_mock_;
  867. // Will be deleted by client_, just keep the current value for comparison.
  868. ACCESSOR_TYPE* current_accessor_;
  869. shared_ptr<DatabaseClient> client_;
  870. const std::string database_name_;
  871. // The zone finder of the test zone commonly used in various tests.
  872. shared_ptr<DatabaseClient::Finder> finder_;
  873. // Some shortcut variables for commonly used test parameters
  874. const Name zname_; // the zone name stored in the test data source
  875. const Name qname_; // commonly used name to be found
  876. const RRClass qclass_; // commonly used RR class used with qname
  877. const RRType qtype_; // commonly used RR type used with qname
  878. const RRTTL rrttl_; // commonly used RR TTL
  879. RRsetPtr rrset_; // for adding/deleting an RRset
  880. RRsetPtr rrsigset_; // for adding/deleting an RRset
  881. RRsetPtr soa_; // for adding/deleting an RRset
  882. // update related objects to be tested
  883. ZoneUpdaterPtr updater_;
  884. shared_ptr<const DatabaseAccessor> update_accessor_;
  885. // placeholders
  886. const std::vector<std::string> empty_rdatas_; // for NXRRSET/NXDOMAIN
  887. std::vector<std::string> expected_rdatas_;
  888. std::vector<std::string> expected_sig_rdatas_;
  889. };
  890. class TestSQLite3Accessor : public SQLite3Accessor {
  891. public:
  892. TestSQLite3Accessor() : SQLite3Accessor(
  893. TEST_DATA_BUILDDIR "/rwtest.sqlite3.copied", "IN")
  894. {
  895. startUpdateZone("example.org.", true);
  896. string columns[ADD_COLUMN_COUNT];
  897. for (int i = 0; TEST_RECORDS[i][0] != NULL; ++i) {
  898. columns[ADD_NAME] = TEST_RECORDS[i][0];
  899. columns[ADD_REV_NAME] = Name(columns[ADD_NAME]).reverse().toText();
  900. columns[ADD_TYPE] = TEST_RECORDS[i][1];
  901. columns[ADD_TTL] = TEST_RECORDS[i][2];
  902. columns[ADD_SIGTYPE] = TEST_RECORDS[i][3];
  903. columns[ADD_RDATA] = TEST_RECORDS[i][4];
  904. addRecordToZone(columns);
  905. }
  906. commit();
  907. }
  908. };
  909. // The following two lines instantiate test cases with concrete accessor
  910. // classes to be tested.
  911. // XXX: clang++ installed on our FreeBSD buildbot cannot complete compiling
  912. // this file, seemingly due to the size of the code. We'll consider more
  913. // complete workaround, but for a short term workaround we'll reduce the
  914. // number of tested accessor classes (thus reducing the amount of code
  915. // to be compiled) for this particular environment.
  916. #if defined(__clang__) && defined(__FreeBSD__)
  917. typedef ::testing::Types<MockAccessor> TestAccessorTypes;
  918. #else
  919. typedef ::testing::Types<MockAccessor, TestSQLite3Accessor> TestAccessorTypes;
  920. #endif
  921. TYPED_TEST_CASE(DatabaseClientTest, TestAccessorTypes);
  922. // In some cases the entire test fixture is for the mock accessor only.
  923. // We use the usual TEST_F for them with the corresponding specialized class
  924. // to make the code simpler.
  925. typedef DatabaseClientTest<MockAccessor> MockDatabaseClientTest;
  926. TYPED_TEST(DatabaseClientTest, zoneNotFound) {
  927. DataSourceClient::FindResult zone(
  928. this->client_->findZone(Name("example.com")));
  929. EXPECT_EQ(result::NOTFOUND, zone.code);
  930. }
  931. TYPED_TEST(DatabaseClientTest, exactZone) {
  932. DataSourceClient::FindResult zone(
  933. this->client_->findZone(Name("example.org")));
  934. EXPECT_EQ(result::SUCCESS, zone.code);
  935. this->checkZoneFinder(zone);
  936. }
  937. TYPED_TEST(DatabaseClientTest, superZone) {
  938. DataSourceClient::FindResult zone(this->client_->findZone(Name(
  939. "sub.example.org")));
  940. EXPECT_EQ(result::PARTIALMATCH, zone.code);
  941. this->checkZoneFinder(zone);
  942. }
  943. // This test doesn't depend on derived accessor class, so we use TEST().
  944. TEST(GenericDatabaseClientTest, noAccessorException) {
  945. // We need a dummy variable here; some compiler would regard it a mere
  946. // declaration instead of an instantiation and make the test fail.
  947. EXPECT_THROW(DatabaseClient dummy(RRClass::IN(),
  948. shared_ptr<DatabaseAccessor>()),
  949. isc::InvalidParameter);
  950. }
  951. // If the zone doesn't exist, exception is thrown
  952. TYPED_TEST(DatabaseClientTest, noZoneIterator) {
  953. EXPECT_THROW(this->client_->getIterator(Name("example.com")),
  954. DataSourceError);
  955. }
  956. // If the zone doesn't exist and iteration is not implemented, it still throws
  957. // the exception it doesn't exist
  958. TEST(GenericDatabaseClientTest, noZoneNotImplementedIterator) {
  959. EXPECT_THROW(DatabaseClient(RRClass::IN(),
  960. boost::shared_ptr<DatabaseAccessor>(
  961. new NopAccessor())).getIterator(
  962. Name("example.com")),
  963. DataSourceError);
  964. }
  965. TEST(GenericDatabaseClientTest, notImplementedIterator) {
  966. EXPECT_THROW(DatabaseClient(RRClass::IN(), shared_ptr<DatabaseAccessor>(
  967. new NopAccessor())).getIterator(Name("example.org")),
  968. isc::NotImplemented);
  969. }
  970. // Pretend a bug in the connection and pass NULL as the context
  971. // Should not crash, but gracefully throw. Works for the mock accessor only.
  972. TEST_F(MockDatabaseClientTest, nullIteratorContext) {
  973. EXPECT_THROW(this->client_->getIterator(Name("null.example.org")),
  974. isc::Unexpected);
  975. }
  976. // It doesn't crash or anything if the zone is completely empty.
  977. // Works for the mock accessor only.
  978. TEST_F(MockDatabaseClientTest, emptyIterator) {
  979. ZoneIteratorPtr it(this->client_->getIterator(Name("empty.example.org")));
  980. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  981. // This is past the end, it should throw
  982. EXPECT_THROW(it->getNextRRset(), isc::Unexpected);
  983. }
  984. // checks if the given rrset matches the
  985. // given name, class, type and rdatas
  986. void
  987. checkRRset(isc::dns::ConstRRsetPtr rrset,
  988. const isc::dns::Name& name,
  989. const isc::dns::RRClass& rrclass,
  990. const isc::dns::RRType& rrtype,
  991. const isc::dns::RRTTL& rrttl,
  992. const std::vector<std::string>& rdatas) {
  993. isc::dns::RRsetPtr expected_rrset(
  994. new isc::dns::RRset(name, rrclass, rrtype, rrttl));
  995. for (unsigned int i = 0; i < rdatas.size(); ++i) {
  996. expected_rrset->addRdata(
  997. isc::dns::rdata::createRdata(rrtype, rrclass,
  998. rdatas[i]));
  999. }
  1000. isc::testutils::rrsetCheck(expected_rrset, rrset);
  1001. }
  1002. // Iterate through a zone
  1003. TYPED_TEST(DatabaseClientTest, iterator) {
  1004. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  1005. ConstRRsetPtr rrset(it->getNextRRset());
  1006. ASSERT_NE(ConstRRsetPtr(), rrset);
  1007. // The first name should be the zone origin.
  1008. EXPECT_EQ(this->zname_, rrset->getName());
  1009. // The rest of the checks work only for the mock accessor.
  1010. if (!this->is_mock_) {
  1011. return;
  1012. }
  1013. this->expected_rdatas_.clear();
  1014. this->expected_rdatas_.push_back("192.0.2.1");
  1015. checkRRset(rrset, Name("example.org"), this->qclass_, RRType::A(),
  1016. this->rrttl_, this->expected_rdatas_);
  1017. rrset = it->getNextRRset();
  1018. this->expected_rdatas_.clear();
  1019. this->expected_rdatas_.push_back("ns1.example.org. admin.example.org. "
  1020. "1234 3600 1800 2419200 7200");
  1021. checkRRset(rrset, Name("example.org"), this->qclass_, RRType::SOA(),
  1022. this->rrttl_, this->expected_rdatas_);
  1023. rrset = it->getNextRRset();
  1024. this->expected_rdatas_.clear();
  1025. this->expected_rdatas_.push_back("192.0.2.1");
  1026. this->expected_rdatas_.push_back("192.0.2.2");
  1027. checkRRset(rrset, Name("x.example.org"), this->qclass_, RRType::A(),
  1028. RRTTL(300), this->expected_rdatas_);
  1029. rrset = it->getNextRRset();
  1030. this->expected_rdatas_.clear();
  1031. this->expected_rdatas_.push_back("2001:db8::1");
  1032. this->expected_rdatas_.push_back("2001:db8::2");
  1033. checkRRset(rrset, Name("x.example.org"), this->qclass_, RRType::AAAA(),
  1034. RRTTL(300), this->expected_rdatas_);
  1035. rrset = it->getNextRRset();
  1036. ASSERT_NE(ConstRRsetPtr(), rrset);
  1037. this->expected_rdatas_.clear();
  1038. this->expected_rdatas_.push_back("192.0.2.1");
  1039. this->expected_rdatas_.push_back("192.0.2.2");
  1040. checkRRset(rrset, Name("ttldiff.example.org"), this->qclass_, RRType::A(),
  1041. RRTTL(300), this->expected_rdatas_);
  1042. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  1043. }
  1044. // This has inconsistent TTL in the set (the rest, like nonsense in
  1045. // the data is handled in rdata itself). Works for the mock accessor only.
  1046. TEST_F(MockDatabaseClientTest, badIterator) {
  1047. // It should not throw, but get the lowest one of them
  1048. ZoneIteratorPtr it(this->client_->getIterator(Name("bad.example.org")));
  1049. EXPECT_EQ(it->getNextRRset()->getTTL(), isc::dns::RRTTL(300));
  1050. }
  1051. TYPED_TEST(DatabaseClientTest, getSOAFromIterator) {
  1052. vector<string> soa_data;
  1053. soa_data.push_back("ns1.example.org. admin.example.org. "
  1054. "1234 3600 1800 2419200 7200");
  1055. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1056. ASSERT_TRUE(it);
  1057. checkRRset(it->getSOA(), this->zname_, this->qclass_, RRType::SOA(),
  1058. this->rrttl_, soa_data);
  1059. // Iterate over the zone until we find an SOA. Although there's a broken
  1060. // RDATA that would trigger an exception in getNextRRset(), we should
  1061. // reach the SOA as the sequence should be sorted and the SOA is at
  1062. // the origin name (which has no bogus data).
  1063. ConstRRsetPtr rrset;
  1064. while ((rrset = it->getNextRRset()) != ConstRRsetPtr() &&
  1065. rrset->getType() != RRType::SOA()) {
  1066. ;
  1067. }
  1068. ASSERT_TRUE(rrset);
  1069. // It should be identical to the result of getSOA().
  1070. isc::testutils::rrsetCheck(it->getSOA(), rrset);
  1071. }
  1072. TYPED_TEST(DatabaseClientTest, noSOAFromIterator) {
  1073. // First, empty the zone.
  1074. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1075. this->updater_->commit();
  1076. // Then getSOA() should return NULL.
  1077. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1078. ASSERT_TRUE(it);
  1079. EXPECT_FALSE(it->getSOA());
  1080. }
  1081. TYPED_TEST(DatabaseClientTest, iterateThenUpdate) {
  1082. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1083. ASSERT_TRUE(it);
  1084. // Try to empty the zone after getting the iterator. Depending on the
  1085. // underlying data source, it may result in an exception due to the
  1086. // transaction for the iterator. In either case the integrity of the
  1087. // iterator result should be reserved.
  1088. try {
  1089. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1090. this->updater_->commit();
  1091. // Confirm at least it doesn't contain any SOA
  1092. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1093. this->getFinder()->find(this->zname_, RRType::SOA()).code);
  1094. } catch (const DataSourceError&) {}
  1095. ConstRRsetPtr rrset;
  1096. while ((rrset = it->getNextRRset()) != ConstRRsetPtr() &&
  1097. rrset->getType() != RRType::SOA()) {
  1098. ;
  1099. }
  1100. ASSERT_TRUE(rrset);
  1101. // It should be identical to the result of getSOA().
  1102. isc::testutils::rrsetCheck(it->getSOA(), rrset);
  1103. }
  1104. TYPED_TEST(DatabaseClientTest, updateThenIterateThenUpdate) {
  1105. // First clear the zone.
  1106. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1107. this->updater_->commit();
  1108. // Then iterate over it. It should immediately reach the end, at which
  1109. // point the transaction should be committed.
  1110. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1111. ASSERT_TRUE(it);
  1112. EXPECT_FALSE(it->getNextRRset());
  1113. // So another update attempt should succeed, too.
  1114. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1115. this->updater_->commit();
  1116. }
  1117. TYPED_TEST(DatabaseClientTest, updateAfterDeleteIterator) {
  1118. // Similar to the previous case, but we delete the iterator in the
  1119. // middle of zone. The transaction should be canceled (actually no
  1120. // different from commit though) at that point.
  1121. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1122. ASSERT_TRUE(it);
  1123. EXPECT_TRUE(it->getNextRRset());
  1124. it.reset();
  1125. // So another update attempt should succeed.
  1126. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1127. this->updater_->commit();
  1128. }
  1129. void
  1130. doFindTest(ZoneFinder& finder,
  1131. const isc::dns::Name& name,
  1132. const isc::dns::RRType& type,
  1133. const isc::dns::RRType& expected_type,
  1134. const isc::dns::RRTTL expected_ttl,
  1135. ZoneFinder::Result expected_result,
  1136. const std::vector<std::string>& expected_rdatas,
  1137. const std::vector<std::string>& expected_sig_rdatas,
  1138. const isc::dns::Name& expected_name = isc::dns::Name::ROOT_NAME(),
  1139. const ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT)
  1140. {
  1141. SCOPED_TRACE("doFindTest " + name.toText() + " " + type.toText());
  1142. const ZoneFinder::FindResult result = finder.find(name, type, NULL,
  1143. options);
  1144. ASSERT_EQ(expected_result, result.code) << name << " " << type;
  1145. if (!expected_rdatas.empty() && result.rrset) {
  1146. checkRRset(result.rrset, expected_name != Name(".") ? expected_name :
  1147. name, finder.getClass(), expected_type, expected_ttl,
  1148. expected_rdatas);
  1149. if (!expected_sig_rdatas.empty() && result.rrset->getRRsig()) {
  1150. checkRRset(result.rrset->getRRsig(), expected_name != Name(".") ?
  1151. expected_name : name, finder.getClass(),
  1152. isc::dns::RRType::RRSIG(), expected_ttl,
  1153. expected_sig_rdatas);
  1154. } else if (expected_sig_rdatas.empty()) {
  1155. EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset->getRRsig());
  1156. } else {
  1157. ADD_FAILURE() << "Missing RRSIG";
  1158. }
  1159. } else if (expected_rdatas.empty()) {
  1160. EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset);
  1161. } else {
  1162. ADD_FAILURE() << "Missing result";
  1163. }
  1164. }
  1165. // When asking for an RRset where RRs somehow have different TTLs, it should
  1166. // convert to the lowest one.
  1167. TEST_F(MockDatabaseClientTest, ttldiff) {
  1168. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  1169. // Walk through the full iterator, we should see 1 rrset with name
  1170. // ttldiff1.example.org., and two rdatas. Same for ttldiff2
  1171. Name name("ttldiff.example.org.");
  1172. bool found = false;
  1173. //bool found2 = false;
  1174. ConstRRsetPtr rrset = it->getNextRRset();
  1175. while(rrset != ConstRRsetPtr()) {
  1176. if (rrset->getName() == name) {
  1177. ASSERT_FALSE(found);
  1178. ASSERT_EQ(2, rrset->getRdataCount());
  1179. ASSERT_EQ(RRTTL(300), rrset->getTTL());
  1180. found = true;
  1181. }
  1182. rrset = it->getNextRRset();
  1183. }
  1184. ASSERT_TRUE(found);
  1185. }
  1186. // Unless we ask for individual RRs in our iterator request. In that case
  1187. // every RR should go into its own 'rrset'
  1188. TEST_F(MockDatabaseClientTest, ttldiff_no_adjust_ttl) {
  1189. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org"), false));
  1190. // Walk through the full iterator, we should see 1 rrset with name
  1191. // ttldiff1.example.org., and two rdatas. Same for ttldiff2
  1192. Name name("ttldiff.example.org.");
  1193. int found1 = false;
  1194. int found2 = false;
  1195. ConstRRsetPtr rrset = it->getNextRRset();
  1196. while(rrset != ConstRRsetPtr()) {
  1197. if (rrset->getName() == name) {
  1198. ASSERT_EQ(1, rrset->getRdataCount());
  1199. // We should find 1 'rrset' with TTL 300 and one with TTL 600
  1200. if (rrset->getTTL() == RRTTL(300)) {
  1201. ASSERT_FALSE(found1);
  1202. found1 = true;
  1203. } else if (rrset->getTTL() == RRTTL(600)) {
  1204. ASSERT_FALSE(found2);
  1205. found2 = true;
  1206. } else {
  1207. FAIL() << "Found unexpected TTL: " <<
  1208. rrset->getTTL().toText();
  1209. }
  1210. }
  1211. rrset = it->getNextRRset();
  1212. }
  1213. ASSERT_TRUE(found1);
  1214. ASSERT_TRUE(found2);
  1215. }
  1216. TYPED_TEST(DatabaseClientTest, find) {
  1217. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1218. this->expected_rdatas_.clear();
  1219. this->expected_sig_rdatas_.clear();
  1220. this->expected_rdatas_.push_back("192.0.2.1");
  1221. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1222. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1223. this->expected_rdatas_, this->expected_sig_rdatas_);
  1224. this->expected_rdatas_.clear();
  1225. this->expected_sig_rdatas_.clear();
  1226. this->expected_rdatas_.push_back("192.0.2.1");
  1227. this->expected_rdatas_.push_back("192.0.2.2");
  1228. doFindTest(*finder, isc::dns::Name("www2.example.org."),
  1229. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1230. this->expected_rdatas_, this->expected_sig_rdatas_);
  1231. this->expected_rdatas_.clear();
  1232. this->expected_sig_rdatas_.clear();
  1233. this->expected_rdatas_.push_back("2001:db8::1");
  1234. this->expected_rdatas_.push_back("2001:db8::2");
  1235. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1236. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1237. this->rrttl_,
  1238. ZoneFinder::SUCCESS,
  1239. this->expected_rdatas_, this->expected_sig_rdatas_);
  1240. this->expected_rdatas_.clear();
  1241. this->expected_sig_rdatas_.clear();
  1242. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1243. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
  1244. this->rrttl_,
  1245. ZoneFinder::NXRRSET,
  1246. this->expected_rdatas_, this->expected_sig_rdatas_);
  1247. this->expected_rdatas_.clear();
  1248. this->expected_sig_rdatas_.clear();
  1249. this->expected_rdatas_.push_back("www.example.org.");
  1250. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  1251. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1252. ZoneFinder::CNAME, this->expected_rdatas_,
  1253. this->expected_sig_rdatas_);
  1254. this->expected_rdatas_.clear();
  1255. this->expected_sig_rdatas_.clear();
  1256. this->expected_rdatas_.push_back("www.example.org.");
  1257. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  1258. isc::dns::RRType::CNAME(), isc::dns::RRType::CNAME(),
  1259. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1260. this->expected_sig_rdatas_);
  1261. this->expected_rdatas_.clear();
  1262. this->expected_sig_rdatas_.clear();
  1263. doFindTest(*finder, isc::dns::Name("doesnotexist.example.org."),
  1264. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1265. this->expected_rdatas_, this->expected_sig_rdatas_);
  1266. this->expected_rdatas_.clear();
  1267. this->expected_sig_rdatas_.clear();
  1268. this->expected_rdatas_.push_back("192.0.2.1");
  1269. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1270. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1271. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1272. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1273. this->expected_rdatas_, this->expected_sig_rdatas_);
  1274. this->expected_rdatas_.clear();
  1275. this->expected_sig_rdatas_.clear();
  1276. this->expected_rdatas_.push_back("2001:db8::1");
  1277. this->expected_rdatas_.push_back("2001:db8::2");
  1278. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1279. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1280. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1281. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1282. this->expected_sig_rdatas_);
  1283. this->expected_rdatas_.clear();
  1284. this->expected_sig_rdatas_.clear();
  1285. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1286. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1287. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1288. this->expected_sig_rdatas_);
  1289. this->expected_rdatas_.clear();
  1290. this->expected_sig_rdatas_.clear();
  1291. this->expected_rdatas_.push_back("www.example.org.");
  1292. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1293. doFindTest(*finder, isc::dns::Name("signedcname1.example.org."),
  1294. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1295. ZoneFinder::CNAME, this->expected_rdatas_,
  1296. this->expected_sig_rdatas_);
  1297. this->expected_rdatas_.clear();
  1298. this->expected_sig_rdatas_.clear();
  1299. this->expected_rdatas_.push_back("192.0.2.1");
  1300. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1301. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1302. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1303. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1304. this->expected_rdatas_, this->expected_sig_rdatas_);
  1305. this->expected_rdatas_.clear();
  1306. this->expected_sig_rdatas_.clear();
  1307. this->expected_rdatas_.push_back("2001:db8::2");
  1308. this->expected_rdatas_.push_back("2001:db8::1");
  1309. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1310. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1311. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1312. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1313. this->expected_sig_rdatas_);
  1314. this->expected_rdatas_.clear();
  1315. this->expected_sig_rdatas_.clear();
  1316. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1317. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1318. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1319. this->expected_sig_rdatas_);
  1320. this->expected_rdatas_.clear();
  1321. this->expected_sig_rdatas_.clear();
  1322. this->expected_rdatas_.push_back("www.example.org.");
  1323. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1324. doFindTest(*finder, isc::dns::Name("signedcname2.example.org."),
  1325. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1326. ZoneFinder::CNAME, this->expected_rdatas_,
  1327. this->expected_sig_rdatas_);
  1328. this->expected_rdatas_.clear();
  1329. this->expected_sig_rdatas_.clear();
  1330. this->expected_rdatas_.push_back("192.0.2.1");
  1331. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1332. doFindTest(*finder, isc::dns::Name("acnamesig1.example.org."),
  1333. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1334. this->expected_rdatas_, this->expected_sig_rdatas_);
  1335. this->expected_rdatas_.clear();
  1336. this->expected_sig_rdatas_.clear();
  1337. this->expected_rdatas_.push_back("192.0.2.1");
  1338. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1339. doFindTest(*finder, isc::dns::Name("acnamesig2.example.org."),
  1340. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1341. this->expected_rdatas_, this->expected_sig_rdatas_);
  1342. this->expected_rdatas_.clear();
  1343. this->expected_sig_rdatas_.clear();
  1344. this->expected_rdatas_.push_back("192.0.2.1");
  1345. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1346. doFindTest(*finder, isc::dns::Name("acnamesig3.example.org."),
  1347. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1348. this->expected_rdatas_, this->expected_sig_rdatas_);
  1349. this->expected_rdatas_.clear();
  1350. this->expected_sig_rdatas_.clear();
  1351. this->expected_rdatas_.push_back("192.0.2.1");
  1352. this->expected_rdatas_.push_back("192.0.2.2");
  1353. doFindTest(*finder, isc::dns::Name("ttldiff1.example.org."),
  1354. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1355. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1356. this->expected_sig_rdatas_);
  1357. this->expected_rdatas_.clear();
  1358. this->expected_sig_rdatas_.clear();
  1359. this->expected_rdatas_.push_back("192.0.2.1");
  1360. this->expected_rdatas_.push_back("192.0.2.2");
  1361. doFindTest(*finder, isc::dns::Name("ttldiff2.example.org."),
  1362. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1363. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1364. this->expected_sig_rdatas_);
  1365. EXPECT_THROW(finder->find(isc::dns::Name("badcname1.example.org."),
  1366. this->qtype_,
  1367. NULL, ZoneFinder::FIND_DEFAULT),
  1368. DataSourceError);
  1369. EXPECT_THROW(finder->find(isc::dns::Name("badcname2.example.org."),
  1370. this->qtype_,
  1371. NULL, ZoneFinder::FIND_DEFAULT),
  1372. DataSourceError);
  1373. EXPECT_THROW(finder->find(isc::dns::Name("badcname3.example.org."),
  1374. this->qtype_,
  1375. NULL, ZoneFinder::FIND_DEFAULT),
  1376. DataSourceError);
  1377. EXPECT_THROW(finder->find(isc::dns::Name("badrdata.example.org."),
  1378. this->qtype_,
  1379. NULL, ZoneFinder::FIND_DEFAULT),
  1380. DataSourceError);
  1381. EXPECT_THROW(finder->find(isc::dns::Name("badtype.example.org."),
  1382. this->qtype_,
  1383. NULL, ZoneFinder::FIND_DEFAULT),
  1384. DataSourceError);
  1385. EXPECT_THROW(finder->find(isc::dns::Name("badttl.example.org."),
  1386. this->qtype_,
  1387. NULL, ZoneFinder::FIND_DEFAULT),
  1388. DataSourceError);
  1389. EXPECT_THROW(finder->find(isc::dns::Name("badsig.example.org."),
  1390. this->qtype_,
  1391. NULL, ZoneFinder::FIND_DEFAULT),
  1392. DataSourceError);
  1393. // Trigger the hardcoded exceptions and see if find() has cleaned up
  1394. if (this->is_mock_) {
  1395. EXPECT_THROW(finder->find(isc::dns::Name("dsexception.in.search."),
  1396. this->qtype_,
  1397. NULL, ZoneFinder::FIND_DEFAULT),
  1398. DataSourceError);
  1399. EXPECT_THROW(finder->find(isc::dns::Name("iscexception.in.search."),
  1400. this->qtype_,
  1401. NULL, ZoneFinder::FIND_DEFAULT),
  1402. isc::Exception);
  1403. EXPECT_THROW(finder->find(isc::dns::Name("basicexception.in.search."),
  1404. this->qtype_,
  1405. NULL, ZoneFinder::FIND_DEFAULT),
  1406. std::exception);
  1407. EXPECT_THROW(finder->find(isc::dns::Name("dsexception.in.getnext."),
  1408. this->qtype_,
  1409. NULL, ZoneFinder::FIND_DEFAULT),
  1410. DataSourceError);
  1411. EXPECT_THROW(finder->find(isc::dns::Name("iscexception.in.getnext."),
  1412. this->qtype_,
  1413. NULL, ZoneFinder::FIND_DEFAULT),
  1414. isc::Exception);
  1415. EXPECT_THROW(finder->find(isc::dns::Name("basicexception.in.getnext."),
  1416. this->qtype_,
  1417. NULL, ZoneFinder::FIND_DEFAULT),
  1418. std::exception);
  1419. }
  1420. // This RRSIG has the wrong sigtype field, which should be
  1421. // an error if we decide to keep using that field
  1422. // Right now the field is ignored, so it does not error
  1423. this->expected_rdatas_.clear();
  1424. this->expected_sig_rdatas_.clear();
  1425. this->expected_rdatas_.push_back("192.0.2.1");
  1426. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1427. doFindTest(*finder, isc::dns::Name("badsigtype.example.org."),
  1428. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1429. this->expected_rdatas_, this->expected_sig_rdatas_);
  1430. }
  1431. TYPED_TEST(DatabaseClientTest, findDelegation) {
  1432. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1433. // The apex should not be considered delegation point and we can access
  1434. // data
  1435. this->expected_rdatas_.clear();
  1436. this->expected_sig_rdatas_.clear();
  1437. this->expected_rdatas_.push_back("192.0.2.1");
  1438. doFindTest(*finder, isc::dns::Name("example.org."),
  1439. this->qtype_, this->qtype_,
  1440. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1441. this->expected_sig_rdatas_);
  1442. this->expected_rdatas_.clear();
  1443. this->expected_rdatas_.push_back("ns.example.com.");
  1444. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  1445. "12345 example.org. FAKEFAKEFAKE");
  1446. doFindTest(*finder, isc::dns::Name("example.org."),
  1447. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1448. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1449. this->expected_sig_rdatas_);
  1450. // Check when we ask for something below delegation point, we get the NS
  1451. // (Both when the RRset there exists and doesn't)
  1452. this->expected_rdatas_.clear();
  1453. this->expected_sig_rdatas_.clear();
  1454. this->expected_rdatas_.push_back("ns.example.com.");
  1455. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  1456. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  1457. "12345 example.org. FAKEFAKEFAKE");
  1458. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1459. this->qtype_, isc::dns::RRType::NS(),
  1460. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1461. this->expected_sig_rdatas_,
  1462. isc::dns::Name("delegation.example.org."));
  1463. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1464. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1465. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1466. this->expected_sig_rdatas_,
  1467. isc::dns::Name("delegation.example.org."));
  1468. doFindTest(*finder, isc::dns::Name("deep.below.delegation.example.org."),
  1469. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1470. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1471. this->expected_sig_rdatas_,
  1472. isc::dns::Name("delegation.example.org."));
  1473. // Even when we check directly at the delegation point, we should get
  1474. // the NS
  1475. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1476. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1477. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1478. this->expected_sig_rdatas_);
  1479. // And when we ask direcly for the NS, we should still get delegation
  1480. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1481. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1482. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1483. this->expected_sig_rdatas_);
  1484. // Now test delegation. If it is below the delegation point, we should get
  1485. // the DNAME (the one with data under DNAME is invalid zone, but we test
  1486. // the behaviour anyway just to make sure)
  1487. this->expected_rdatas_.clear();
  1488. this->expected_rdatas_.push_back("dname.example.com.");
  1489. this->expected_sig_rdatas_.clear();
  1490. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  1491. "20000201000000 12345 example.org. "
  1492. "FAKEFAKEFAKE");
  1493. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1494. this->qtype_, isc::dns::RRType::DNAME(),
  1495. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1496. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1497. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1498. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1499. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1500. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1501. doFindTest(*finder, isc::dns::Name("really.deep.below.dname.example.org."),
  1502. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1503. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1504. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1505. // Asking direcly for DNAME should give SUCCESS
  1506. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1507. isc::dns::RRType::DNAME(), isc::dns::RRType::DNAME(),
  1508. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1509. this->expected_sig_rdatas_);
  1510. // But we don't delegate at DNAME point
  1511. this->expected_rdatas_.clear();
  1512. this->expected_rdatas_.push_back("192.0.2.1");
  1513. this->expected_sig_rdatas_.clear();
  1514. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1515. this->qtype_, this->qtype_,
  1516. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1517. this->expected_sig_rdatas_);
  1518. this->expected_rdatas_.clear();
  1519. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1520. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1521. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  1522. this->expected_sig_rdatas_);
  1523. // This is broken dname, it contains two targets
  1524. EXPECT_THROW(finder->find(isc::dns::Name("below.baddname.example.org."),
  1525. this->qtype_, NULL,
  1526. ZoneFinder::FIND_DEFAULT),
  1527. DataSourceError);
  1528. // Broken NS - it lives together with something else
  1529. EXPECT_THROW(finder->find(isc::dns::Name("brokenns1.example.org."),
  1530. this->qtype_, NULL,
  1531. ZoneFinder::FIND_DEFAULT),
  1532. DataSourceError);
  1533. EXPECT_THROW(finder->find(isc::dns::Name("brokenns2.example.org."),
  1534. this->qtype_, NULL,
  1535. ZoneFinder::FIND_DEFAULT),
  1536. DataSourceError);
  1537. }
  1538. TYPED_TEST(DatabaseClientTest, emptyDomain) {
  1539. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1540. // This domain doesn't exist, but a subdomain of it does.
  1541. // Therefore we should pretend the domain is there, but contains no RRsets
  1542. doFindTest(*finder, isc::dns::Name("b.example.org."), this->qtype_,
  1543. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1544. this->expected_rdatas_, this->expected_sig_rdatas_);
  1545. }
  1546. // Glue-OK mode. Just go through NS delegations down (but not through
  1547. // DNAME) and pretend it is not there.
  1548. TYPED_TEST(DatabaseClientTest, glueOK) {
  1549. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1550. this->expected_rdatas_.clear();
  1551. this->expected_sig_rdatas_.clear();
  1552. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1553. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1554. this->rrttl_, ZoneFinder::NXRRSET,
  1555. this->expected_rdatas_, this->expected_sig_rdatas_,
  1556. isc::dns::Name("ns.delegation.example.org."),
  1557. ZoneFinder::FIND_GLUE_OK);
  1558. doFindTest(*finder, isc::dns::Name("nothere.delegation.example.org."),
  1559. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1560. this->rrttl_, ZoneFinder::NXDOMAIN,
  1561. this->expected_rdatas_, this->expected_sig_rdatas_,
  1562. isc::dns::Name("nothere.delegation.example.org."),
  1563. ZoneFinder::FIND_GLUE_OK);
  1564. this->expected_rdatas_.push_back("192.0.2.1");
  1565. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1566. this->qtype_, this->qtype_,
  1567. this->rrttl_, ZoneFinder::SUCCESS,
  1568. this->expected_rdatas_, this->expected_sig_rdatas_,
  1569. isc::dns::Name("ns.delegation.example.org."),
  1570. ZoneFinder::FIND_GLUE_OK);
  1571. this->expected_rdatas_.clear();
  1572. this->expected_rdatas_.push_back("ns.example.com.");
  1573. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  1574. this->expected_sig_rdatas_.clear();
  1575. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 "
  1576. "20000201000000 12345 example.org. "
  1577. "FAKEFAKEFAKE");
  1578. // When we request the NS, it should be SUCCESS, not DELEGATION
  1579. // (different in GLUE_OK)
  1580. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1581. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1582. this->rrttl_, ZoneFinder::SUCCESS,
  1583. this->expected_rdatas_, this->expected_sig_rdatas_,
  1584. isc::dns::Name("delegation.example.org."),
  1585. ZoneFinder::FIND_GLUE_OK);
  1586. this->expected_rdatas_.clear();
  1587. this->expected_rdatas_.push_back("dname.example.com.");
  1588. this->expected_sig_rdatas_.clear();
  1589. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  1590. "20000201000000 12345 example.org. "
  1591. "FAKEFAKEFAKE");
  1592. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1593. this->qtype_, isc::dns::RRType::DNAME(),
  1594. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1595. this->expected_sig_rdatas_,
  1596. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  1597. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1598. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1599. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1600. this->expected_sig_rdatas_,
  1601. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  1602. }
  1603. TYPED_TEST(DatabaseClientTest, wildcard) {
  1604. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1605. // First, simple wildcard match
  1606. // Check also that the RRSIG is added from the wildcard (not modified)
  1607. this->expected_rdatas_.push_back("192.0.2.5");
  1608. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1609. "20000201000000 12345 example.org. "
  1610. "FAKEFAKEFAKE");
  1611. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1612. this->qtype_, this->qtype_, this->rrttl_,
  1613. ZoneFinder::WILDCARD, this->expected_rdatas_,
  1614. this->expected_sig_rdatas_);
  1615. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  1616. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::WILDCARD,
  1617. this->expected_rdatas_, this->expected_sig_rdatas_);
  1618. this->expected_rdatas_.clear();
  1619. this->expected_sig_rdatas_.clear();
  1620. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1621. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1622. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1623. this->expected_rdatas_, this->expected_sig_rdatas_);
  1624. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  1625. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1626. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1627. this->expected_rdatas_, this->expected_sig_rdatas_);
  1628. // Direct request for this wildcard
  1629. this->expected_rdatas_.push_back("192.0.2.5");
  1630. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1631. "20000201000000 12345 example.org. "
  1632. "FAKEFAKEFAKE");
  1633. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1634. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1635. this->expected_rdatas_, this->expected_sig_rdatas_);
  1636. this->expected_rdatas_.clear();
  1637. this->expected_sig_rdatas_.clear();
  1638. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1639. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1640. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  1641. this->expected_sig_rdatas_);
  1642. // This is nonsense, but check it doesn't match by some stupid accident
  1643. doFindTest(*finder, isc::dns::Name("a.*.wild.example.org"),
  1644. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1645. this->expected_rdatas_, this->expected_sig_rdatas_);
  1646. // These should be canceled, since it is below a domain which exitsts
  1647. doFindTest(*finder, isc::dns::Name("nothing.here.wild.example.org"),
  1648. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1649. this->expected_rdatas_, this->expected_sig_rdatas_);
  1650. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  1651. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1652. this->expected_rdatas_, this->expected_sig_rdatas_);
  1653. doFindTest(*finder,
  1654. isc::dns::Name("below.cancel.here.wild.example.org"),
  1655. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1656. this->expected_rdatas_, this->expected_sig_rdatas_);
  1657. // And this should be just plain empty non-terminal domain, check
  1658. // the wildcard doesn't hurt it
  1659. doFindTest(*finder, isc::dns::Name("here.wild.example.org"),
  1660. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1661. this->expected_rdatas_, this->expected_sig_rdatas_);
  1662. // Also make sure that the wildcard doesn't hurt the original data
  1663. // below the wildcard
  1664. this->expected_rdatas_.push_back("2001:db8::5");
  1665. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  1666. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1667. this->rrttl_, ZoneFinder::SUCCESS,
  1668. this->expected_rdatas_, this->expected_sig_rdatas_);
  1669. this->expected_rdatas_.clear();
  1670. // How wildcard go together with delegation
  1671. this->expected_rdatas_.push_back("ns.example.com.");
  1672. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  1673. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  1674. ZoneFinder::DELEGATION, this->expected_rdatas_,
  1675. this->expected_sig_rdatas_,
  1676. isc::dns::Name("delegatedwild.example.org"));
  1677. // FIXME: This doesn't look logically OK, GLUE_OK should make it transparent,
  1678. // so the match should either work or be canceled, but return NXDOMAIN
  1679. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  1680. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  1681. ZoneFinder::DELEGATION, this->expected_rdatas_,
  1682. this->expected_sig_rdatas_,
  1683. isc::dns::Name("delegatedwild.example.org"),
  1684. ZoneFinder::FIND_GLUE_OK);
  1685. this->expected_rdatas_.clear();
  1686. this->expected_rdatas_.push_back("192.0.2.5");
  1687. // These are direct matches
  1688. const char* positive_names[] = {
  1689. "wild.*.foo.example.org.",
  1690. "wild.*.foo.*.bar.example.org.",
  1691. NULL
  1692. };
  1693. for (const char** name(positive_names); *name != NULL; ++ name) {
  1694. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1695. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1696. this->expected_rdatas_,
  1697. this->expected_sig_rdatas_);
  1698. }
  1699. // These are wildcard matches against empty nonterminal asterisk
  1700. this->expected_rdatas_.clear();
  1701. const char* negative_names[] = {
  1702. "a.foo.example.org.",
  1703. "*.foo.example.org.",
  1704. "foo.example.org.",
  1705. "wild.bar.foo.example.org.",
  1706. "baz.foo.*.bar.example.org",
  1707. "baz.foo.baz.bar.example.org",
  1708. "*.foo.baz.bar.example.org",
  1709. "*.foo.*.bar.example.org",
  1710. "foo.*.bar.example.org",
  1711. "*.bar.example.org",
  1712. "bar.example.org",
  1713. NULL
  1714. };
  1715. // Unless FIND_DNSSEC is specified, this is no different from other
  1716. // NXRRSET case.
  1717. for (const char** name(negative_names); *name != NULL; ++ name) {
  1718. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1719. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1720. this->expected_rdatas_, this->expected_sig_rdatas_);
  1721. }
  1722. // With FIND_DNSSEC, it should result in WILDCARD_NXRRSET.
  1723. const char* negative_dnssec_names[] = {
  1724. "a.bar.example.org.",
  1725. "foo.baz.bar.example.org.",
  1726. "a.foo.bar.example.org.",
  1727. NULL
  1728. };
  1729. this->expected_rdatas_.clear();
  1730. this->expected_rdatas_.push_back("wild.*.foo.*.bar.example.org. NSEC");
  1731. this->expected_sig_rdatas_.clear();
  1732. for (const char** name(negative_dnssec_names); *name != NULL; ++ name) {
  1733. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1734. RRType::NSEC(), this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1735. this->expected_rdatas_, this->expected_sig_rdatas_,
  1736. Name("bao.example.org."), ZoneFinder::FIND_DNSSEC);
  1737. }
  1738. // CNAME on a wildcard. Maybe not so common, but not disallowed.
  1739. this->expected_rdatas_.clear();
  1740. this->expected_rdatas_.push_back("www.example.org.");
  1741. this->expected_sig_rdatas_.clear();
  1742. doFindTest(*finder, isc::dns::Name("a.cnamewild.example.org."),
  1743. isc::dns::RRType::TXT(), isc::dns::RRType::CNAME(),
  1744. this->rrttl_, ZoneFinder::WILDCARD_CNAME,
  1745. this->expected_rdatas_, this->expected_sig_rdatas_);
  1746. // DNAME on a wildcard. In our implementation we ignore DNAMEs on a
  1747. // wildcard, but at a higher level we say the behavior is "unspecified".
  1748. // rfc2672bis strongly discourages the mixture of DNAME and wildcard
  1749. // (with SHOULD NOT).
  1750. this->expected_rdatas_.clear();
  1751. this->expected_sig_rdatas_.clear();
  1752. doFindTest(*finder, Name("a.dnamewild.example.org."),
  1753. this->qtype_, this->qtype_, this->rrttl_,
  1754. ZoneFinder::WILDCARD_NXRRSET, this->expected_rdatas_,
  1755. this->expected_sig_rdatas_);
  1756. // Some strange things in the wild node
  1757. this->expected_rdatas_.clear();
  1758. this->expected_rdatas_.push_back("ns.example.com.");
  1759. doFindTest(*finder, isc::dns::Name("a.nswild.example.org."),
  1760. isc::dns::RRType::TXT(), isc::dns::RRType::NS(),
  1761. this->rrttl_, ZoneFinder::DELEGATION,
  1762. this->expected_rdatas_, this->expected_sig_rdatas_);
  1763. }
  1764. TYPED_TEST(DatabaseClientTest, noWildcard) {
  1765. // Tests with the NO_WILDCARD flag.
  1766. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1767. // This would match *.wild.example.org, but with NO_WILDCARD should
  1768. // result in NXDOMAIN.
  1769. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A "
  1770. "NSEC RRSIG");
  1771. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1772. "20000201000000 12345 example.org. "
  1773. "FAKEFAKEFAKE");
  1774. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1775. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1776. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1777. this->expected_sig_rdatas_, Name("*.wild.example.org."),
  1778. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1779. // Should be the same without FIND_DNSSEC (but in this case no RRsets
  1780. // will be returned)
  1781. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1782. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1783. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1784. this->empty_rdatas_, Name::ROOT_NAME(), // name is dummy
  1785. ZoneFinder::NO_WILDCARD);
  1786. // Same for wildcard empty non terminal.
  1787. this->expected_rdatas_.clear();
  1788. this->expected_rdatas_.push_back("brokenns1.example.org. A NSEC");
  1789. doFindTest(*finder, isc::dns::Name("a.bar.example.org"),
  1790. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1791. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1792. this->empty_rdatas_, Name("wild.*.foo.*.bar.example.org"),
  1793. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1794. // Search for a wildcard name with NO_WILDCARD. There should be no
  1795. // difference. This is, for example, necessary to provide non existence
  1796. // of matching wildcard for isnx.nonterminal.example.org.
  1797. this->expected_rdatas_.clear();
  1798. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  1799. doFindTest(*finder, isc::dns::Name("*.nonterminal.example.org"),
  1800. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1801. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1802. this->empty_rdatas_, Name("l.example.org"),
  1803. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1804. // On the other hand, if there's exact match for the wildcard name
  1805. // it should be found regardless of NO_WILDCARD.
  1806. this->expected_rdatas_.clear();
  1807. this->expected_rdatas_.push_back("192.0.2.5");
  1808. this->expected_sig_rdatas_.clear();
  1809. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1810. "20000201000000 12345 example.org. "
  1811. "FAKEFAKEFAKE");
  1812. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1813. this->qtype_, this->qtype_, this->rrttl_,
  1814. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1815. this->expected_sig_rdatas_, Name("*.wild.example.org"),
  1816. ZoneFinder::NO_WILDCARD);
  1817. }
  1818. TYPED_TEST(DatabaseClientTest, NXRRSET_NSEC) {
  1819. // The domain exists, but doesn't have this RRType
  1820. // So we should get its NSEC
  1821. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1822. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  1823. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1824. "20000201000000 12345 example.org. "
  1825. "FAKEFAKEFAKE");
  1826. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1827. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1828. this->rrttl_, ZoneFinder::NXRRSET,
  1829. this->expected_rdatas_, this->expected_sig_rdatas_,
  1830. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC);
  1831. }
  1832. TYPED_TEST(DatabaseClientTest, wildcardNXRRSET_NSEC) {
  1833. // The domain exists, but doesn't have this RRType
  1834. // So we should get its NSEC
  1835. //
  1836. // The user will have to query us again to get the correct
  1837. // answer (eg. prove there's not an exact match)
  1838. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1839. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A NSEC "
  1840. "RRSIG");
  1841. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1842. "20000201000000 12345 example.org. "
  1843. "FAKEFAKEFAKE");
  1844. // Note that the NSEC name should NOT be synthesized.
  1845. doFindTest(*finder, isc::dns::Name("a.wild.example.org."),
  1846. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1847. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1848. this->expected_rdatas_, this->expected_sig_rdatas_,
  1849. Name("*.wild.example.org"), ZoneFinder::FIND_DNSSEC);
  1850. }
  1851. TYPED_TEST(DatabaseClientTest, NXDOMAIN_NSEC) {
  1852. // The domain doesn't exist, so we must get the right NSEC
  1853. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1854. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  1855. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1856. "20000201000000 12345 example.org. "
  1857. "FAKEFAKEFAKE");
  1858. doFindTest(*finder, isc::dns::Name("www1.example.org."),
  1859. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1860. this->rrttl_, ZoneFinder::NXDOMAIN,
  1861. this->expected_rdatas_, this->expected_sig_rdatas_,
  1862. Name("www.example.org."), ZoneFinder::FIND_DNSSEC);
  1863. this->expected_rdatas_.clear();
  1864. this->expected_rdatas_.push_back("acnamesig1.example.org. NS A NSEC RRSIG");
  1865. // This tests it works correctly in apex (there was a bug, where a check
  1866. // for NS-alone was there and it would throw).
  1867. doFindTest(*finder, isc::dns::Name("aa.example.org."),
  1868. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1869. this->rrttl_, ZoneFinder::NXDOMAIN,
  1870. this->expected_rdatas_, this->expected_sig_rdatas_,
  1871. Name("example.org."), ZoneFinder::FIND_DNSSEC);
  1872. // Check that if the DB doesn't support it, the exception from there
  1873. // is not propagated and it only does not include the NSEC
  1874. if (!this->is_mock_) {
  1875. return; // We don't make the real DB to throw
  1876. }
  1877. EXPECT_NO_THROW(doFindTest(*finder,
  1878. isc::dns::Name("notimplnsec.example.org."),
  1879. isc::dns::RRType::TXT(),
  1880. isc::dns::RRType::NSEC(), this->rrttl_,
  1881. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1882. this->empty_rdatas_, Name::ROOT_NAME(),
  1883. ZoneFinder::FIND_DNSSEC));
  1884. }
  1885. TYPED_TEST(DatabaseClientTest, emptyNonterminalNSEC) {
  1886. // Same as NXDOMAIN_NSEC, but with empty non-terminal
  1887. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1888. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  1889. doFindTest(*finder, isc::dns::Name("nonterminal.example.org."),
  1890. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(), this->rrttl_,
  1891. ZoneFinder::NXRRSET,
  1892. this->expected_rdatas_, this->expected_sig_rdatas_,
  1893. Name("l.example.org."), ZoneFinder::FIND_DNSSEC);
  1894. // Check that if the DB doesn't support it, the exception from there
  1895. // is not propagated and it only does not include the NSEC
  1896. if (!this->is_mock_) {
  1897. return; // We don't make the real DB to throw
  1898. }
  1899. EXPECT_NO_THROW(doFindTest(*finder,
  1900. isc::dns::Name("here.wild.example.org."),
  1901. isc::dns::RRType::TXT(),
  1902. isc::dns::RRType::NSEC(),
  1903. this->rrttl_, ZoneFinder::NXRRSET,
  1904. this->empty_rdatas_, this->empty_rdatas_,
  1905. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC));
  1906. }
  1907. TYPED_TEST(DatabaseClientTest, getOrigin) {
  1908. DataSourceClient::FindResult
  1909. zone(this->client_->findZone(Name("example.org")));
  1910. ASSERT_EQ(result::SUCCESS, zone.code);
  1911. shared_ptr<DatabaseClient::Finder> finder(
  1912. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  1913. if (this->is_mock_) {
  1914. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  1915. }
  1916. EXPECT_EQ(this->zname_, finder->getOrigin());
  1917. }
  1918. TYPED_TEST(DatabaseClientTest, updaterFinder) {
  1919. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1920. ASSERT_TRUE(this->updater_);
  1921. // If this update isn't replacing the zone, the finder should work
  1922. // just like the normal find() case.
  1923. if (this->is_mock_) {
  1924. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  1925. this->updater_->getFinder());
  1926. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  1927. }
  1928. this->expected_rdatas_.clear();
  1929. this->expected_rdatas_.push_back("192.0.2.1");
  1930. doFindTest(this->updater_->getFinder(), this->qname_,
  1931. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1932. this->expected_rdatas_, this->empty_rdatas_);
  1933. // When replacing the zone, the updater's finder shouldn't see anything
  1934. // in the zone until something is added.
  1935. this->updater_.reset();
  1936. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1937. ASSERT_TRUE(this->updater_);
  1938. if (this->is_mock_) {
  1939. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  1940. this->updater_->getFinder());
  1941. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  1942. }
  1943. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1944. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1945. this->empty_rdatas_, this->empty_rdatas_);
  1946. }
  1947. TYPED_TEST(DatabaseClientTest, flushZone) {
  1948. // A simple update case: flush the entire zone
  1949. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1950. // Before update, the name exists.
  1951. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  1952. this->qtype_).code);
  1953. // start update in the replace mode. the normal finder should still
  1954. // be able to see the record, but the updater's finder shouldn't.
  1955. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1956. this->setUpdateAccessor();
  1957. EXPECT_EQ(ZoneFinder::SUCCESS,
  1958. finder->find(this->qname_, this->qtype_).code);
  1959. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1960. this->updater_->getFinder().find(this->qname_,
  1961. this->qtype_).code);
  1962. // commit the update. now the normal finder shouldn't see it.
  1963. this->updater_->commit();
  1964. EXPECT_EQ(ZoneFinder::NXDOMAIN, finder->find(this->qname_,
  1965. this->qtype_).code);
  1966. // Check rollback wasn't accidentally performed.
  1967. EXPECT_FALSE(this->isRollbacked());
  1968. }
  1969. TYPED_TEST(DatabaseClientTest, updateCancel) {
  1970. // similar to the previous test, but destruct the updater before commit.
  1971. ZoneFinderPtr finder = this->client_->findZone(this->zname_).zone_finder;
  1972. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  1973. this->qtype_).code);
  1974. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1975. this->setUpdateAccessor();
  1976. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1977. this->updater_->getFinder().find(this->qname_,
  1978. this->qtype_).code);
  1979. // DB should not have been rolled back yet.
  1980. EXPECT_FALSE(this->isRollbacked());
  1981. this->updater_.reset(); // destruct without commit
  1982. // reset() should have triggered rollback (although it doesn't affect
  1983. // anything to the mock accessor implementation except for the result of
  1984. // isRollbacked())
  1985. EXPECT_TRUE(this->isRollbacked(true));
  1986. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  1987. this->qtype_).code);
  1988. }
  1989. TYPED_TEST(DatabaseClientTest, exceptionFromRollback) {
  1990. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1991. this->rrset_.reset(new RRset(Name("throw.example.org"), this->qclass_,
  1992. this->qtype_, this->rrttl_));
  1993. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1994. this->rrset_->getClass(),
  1995. "192.0.2.1"));
  1996. this->updater_->addRRset(*this->rrset_);
  1997. // destruct without commit. The added name will result in an exception
  1998. // in the MockAccessor's rollback method. It shouldn't be propagated.
  1999. EXPECT_NO_THROW(this->updater_.reset());
  2000. }
  2001. TYPED_TEST(DatabaseClientTest, duplicateCommit) {
  2002. // duplicate commit. should result in exception.
  2003. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2004. this->updater_->commit();
  2005. EXPECT_THROW(this->updater_->commit(), DataSourceError);
  2006. }
  2007. TYPED_TEST(DatabaseClientTest, addRRsetToNewZone) {
  2008. // Add a single RRset to a fresh empty zone
  2009. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2010. this->updater_->addRRset(*this->rrset_);
  2011. this->expected_rdatas_.clear();
  2012. this->expected_rdatas_.push_back("192.0.2.2");
  2013. {
  2014. SCOPED_TRACE("add RRset");
  2015. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2016. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2017. this->expected_rdatas_, this->empty_rdatas_);
  2018. }
  2019. // Similar to the previous case, but with RRSIG
  2020. this->updater_.reset();
  2021. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2022. this->updater_->addRRset(*this->rrset_);
  2023. this->updater_->addRRset(*this->rrsigset_);
  2024. // confirm the expected columns were passed to the accessor (if checkable).
  2025. const char* const rrsig_added[] = {
  2026. "www.example.org.", "org.example.www.", "3600", "RRSIG", "A",
  2027. "A 5 3 0 20000101000000 20000201000000 0 example.org. FAKEFAKEFAKE"
  2028. };
  2029. this->checkLastAdded(rrsig_added);
  2030. this->expected_sig_rdatas_.clear();
  2031. this->expected_sig_rdatas_.push_back(
  2032. rrsig_added[DatabaseAccessor::ADD_RDATA]);
  2033. {
  2034. SCOPED_TRACE("add RRset with RRSIG");
  2035. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2036. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2037. this->expected_rdatas_, this->expected_sig_rdatas_);
  2038. }
  2039. // Add the non RRSIG RRset again, to see the attempt of adding RRSIG
  2040. // causes any unexpected effect, in particular, whether the SIGTYPE
  2041. // field might remain.
  2042. this->updater_->addRRset(*this->rrset_);
  2043. const char* const rrset_added[] = {
  2044. "www.example.org.", "org.example.www.", "3600", "A", "", "192.0.2.2"
  2045. };
  2046. this->checkLastAdded(rrset_added);
  2047. }
  2048. TYPED_TEST(DatabaseClientTest, addRRsetToCurrentZone) {
  2049. // Similar to the previous test, but not replacing the existing data.
  2050. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2051. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2052. this->updater_->addRRset(*this->rrset_);
  2053. // We should see both old and new data.
  2054. this->expected_rdatas_.clear();
  2055. this->expected_rdatas_.push_back("192.0.2.1");
  2056. this->expected_rdatas_.push_back("192.0.2.2");
  2057. {
  2058. SCOPED_TRACE("add RRset");
  2059. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2060. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2061. this->expected_rdatas_, this->empty_rdatas_);
  2062. }
  2063. this->updater_->commit();
  2064. {
  2065. SCOPED_TRACE("add RRset after commit");
  2066. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  2067. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2068. this->empty_rdatas_);
  2069. }
  2070. }
  2071. TYPED_TEST(DatabaseClientTest, addMultipleRRs) {
  2072. // Similar to the previous case, but the added RRset contains multiple
  2073. // RRs.
  2074. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2075. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2076. this->rrset_->getClass(),
  2077. "192.0.2.3"));
  2078. this->updater_->addRRset(*this->rrset_);
  2079. this->expected_rdatas_.clear();
  2080. this->expected_rdatas_.push_back("192.0.2.1");
  2081. this->expected_rdatas_.push_back("192.0.2.2");
  2082. this->expected_rdatas_.push_back("192.0.2.3");
  2083. {
  2084. SCOPED_TRACE("add multiple RRs");
  2085. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2086. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2087. this->expected_rdatas_, this->empty_rdatas_);
  2088. }
  2089. }
  2090. TYPED_TEST(DatabaseClientTest, addRRsetOfLargerTTL) {
  2091. // Similar to the previous one, but the TTL of the added RRset is larger
  2092. // than that of the existing record. The finder should use the smaller
  2093. // one.
  2094. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2095. this->rrset_->setTTL(RRTTL(7200));
  2096. this->updater_->addRRset(*this->rrset_);
  2097. this->expected_rdatas_.clear();
  2098. this->expected_rdatas_.push_back("192.0.2.1");
  2099. this->expected_rdatas_.push_back("192.0.2.2");
  2100. {
  2101. SCOPED_TRACE("add RRset of larger TTL");
  2102. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2103. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2104. this->expected_rdatas_, this->empty_rdatas_);
  2105. }
  2106. }
  2107. TYPED_TEST(DatabaseClientTest, addRRsetOfSmallerTTL) {
  2108. // Similar to the previous one, but the added RRset has a smaller TTL.
  2109. // The added TTL should be used by the finder.
  2110. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2111. this->rrset_->setTTL(RRTTL(1800));
  2112. this->updater_->addRRset(*this->rrset_);
  2113. this->expected_rdatas_.clear();
  2114. this->expected_rdatas_.push_back("192.0.2.1");
  2115. this->expected_rdatas_.push_back("192.0.2.2");
  2116. {
  2117. SCOPED_TRACE("add RRset of smaller TTL");
  2118. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2119. this->qtype_, RRTTL(1800), ZoneFinder::SUCCESS,
  2120. this->expected_rdatas_, this->empty_rdatas_);
  2121. }
  2122. }
  2123. TYPED_TEST(DatabaseClientTest, addSameRR) {
  2124. // Add the same RR as that is already in the data source.
  2125. // Currently the add interface doesn't try to suppress the duplicate,
  2126. // neither does the finder. We may want to revisit it in future versions.
  2127. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2128. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2129. this->rrttl_));
  2130. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2131. this->rrset_->getClass(),
  2132. "192.0.2.1"));
  2133. this->updater_->addRRset(*this->rrset_);
  2134. this->expected_rdatas_.clear();
  2135. this->expected_rdatas_.push_back("192.0.2.1");
  2136. this->expected_rdatas_.push_back("192.0.2.1");
  2137. {
  2138. SCOPED_TRACE("add same RR");
  2139. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2140. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2141. this->expected_rdatas_, this->empty_rdatas_);
  2142. }
  2143. }
  2144. TYPED_TEST(DatabaseClientTest, addDeviantRR) {
  2145. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2146. // RR class mismatch. This should be detected and rejected.
  2147. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  2148. this->rrttl_));
  2149. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2150. this->rrset_->getClass(),
  2151. "test text"));
  2152. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2153. // Out-of-zone owner name. At a higher level this should be rejected,
  2154. // but it doesn't happen in this interface.
  2155. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  2156. this->qtype_, this->rrttl_));
  2157. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2158. this->rrset_->getClass(),
  2159. "192.0.2.100"));
  2160. this->updater_->addRRset(*this->rrset_);
  2161. this->expected_rdatas_.clear();
  2162. this->expected_rdatas_.push_back("192.0.2.100");
  2163. {
  2164. // Note: with the find() implementation being more strict about
  2165. // zone cuts, this test may fail. Then the test should be updated.
  2166. SCOPED_TRACE("add out-of-zone RR");
  2167. doFindTest(this->updater_->getFinder(), Name("example.com"),
  2168. this->qtype_, this->qtype_, this->rrttl_,
  2169. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2170. this->empty_rdatas_);
  2171. }
  2172. }
  2173. TYPED_TEST(DatabaseClientTest, addEmptyRRset) {
  2174. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2175. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2176. this->rrttl_));
  2177. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2178. }
  2179. TYPED_TEST(DatabaseClientTest, addAfterCommit) {
  2180. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2181. this->updater_->commit();
  2182. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2183. }
  2184. TYPED_TEST(DatabaseClientTest, addRRsetWithRRSIG) {
  2185. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2186. this->rrset_->addRRsig(*this->rrsigset_);
  2187. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2188. }
  2189. TYPED_TEST(DatabaseClientTest, deleteRRset) {
  2190. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2191. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2192. this->rrttl_));
  2193. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2194. this->rrset_->getClass(),
  2195. "192.0.2.1"));
  2196. // Delete one RR from an RRset
  2197. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2198. this->updater_->deleteRRset(*this->rrset_);
  2199. // Delete the only RR of a name
  2200. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  2201. RRType::CNAME(), this->rrttl_));
  2202. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2203. this->rrset_->getClass(),
  2204. "www.example.org"));
  2205. this->updater_->deleteRRset(*this->rrset_);
  2206. // The this->updater_ finder should immediately see the deleted results.
  2207. {
  2208. SCOPED_TRACE("delete RRset");
  2209. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2210. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2211. this->empty_rdatas_, this->empty_rdatas_);
  2212. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  2213. this->qtype_, this->qtype_, this->rrttl_,
  2214. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2215. this->empty_rdatas_);
  2216. }
  2217. // before committing the change, the original finder should see the
  2218. // original record.
  2219. {
  2220. SCOPED_TRACE("delete RRset before commit");
  2221. this->expected_rdatas_.push_back("192.0.2.1");
  2222. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  2223. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2224. this->empty_rdatas_);
  2225. this->expected_rdatas_.clear();
  2226. this->expected_rdatas_.push_back("www.example.org.");
  2227. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  2228. RRType::CNAME(), this->rrttl_, ZoneFinder::CNAME,
  2229. this->expected_rdatas_, this->empty_rdatas_);
  2230. }
  2231. // once committed, the record should be removed from the original finder's
  2232. // view, too.
  2233. this->updater_->commit();
  2234. {
  2235. SCOPED_TRACE("delete RRset after commit");
  2236. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  2237. this->rrttl_, ZoneFinder::NXRRSET, this->empty_rdatas_,
  2238. this->empty_rdatas_);
  2239. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  2240. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2241. this->empty_rdatas_, this->empty_rdatas_);
  2242. }
  2243. }
  2244. TYPED_TEST(DatabaseClientTest, deleteRRsetToNXDOMAIN) {
  2245. // similar to the previous case, but it removes the only record of the
  2246. // given name. a subsequent find() should result in NXDOMAIN.
  2247. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  2248. RRType::CNAME(), this->rrttl_));
  2249. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2250. this->rrset_->getClass(),
  2251. "www.example.org"));
  2252. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2253. this->updater_->deleteRRset(*this->rrset_);
  2254. {
  2255. SCOPED_TRACE("delete RRset to NXDOMAIN");
  2256. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  2257. this->qtype_, this->qtype_, this->rrttl_,
  2258. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2259. this->empty_rdatas_);
  2260. }
  2261. }
  2262. TYPED_TEST(DatabaseClientTest, deleteMultipleRRs) {
  2263. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  2264. this->rrttl_));
  2265. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2266. this->rrset_->getClass(),
  2267. "2001:db8::1"));
  2268. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2269. this->rrset_->getClass(),
  2270. "2001:db8::2"));
  2271. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2272. this->updater_->deleteRRset(*this->rrset_);
  2273. {
  2274. SCOPED_TRACE("delete multiple RRs");
  2275. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  2276. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2277. this->empty_rdatas_, this->empty_rdatas_);
  2278. }
  2279. }
  2280. TYPED_TEST(DatabaseClientTest, partialDelete) {
  2281. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  2282. this->rrttl_));
  2283. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2284. this->rrset_->getClass(),
  2285. "2001:db8::1"));
  2286. // This does not exist in the test data source:
  2287. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2288. this->rrset_->getClass(),
  2289. "2001:db8::3"));
  2290. // deleteRRset should succeed "silently", and subsequent find() should
  2291. // find the remaining RR.
  2292. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2293. this->updater_->deleteRRset(*this->rrset_);
  2294. {
  2295. SCOPED_TRACE("partial delete");
  2296. this->expected_rdatas_.push_back("2001:db8::2");
  2297. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  2298. RRType::AAAA(), this->rrttl_, ZoneFinder::SUCCESS,
  2299. this->expected_rdatas_, this->empty_rdatas_);
  2300. }
  2301. }
  2302. TYPED_TEST(DatabaseClientTest, deleteNoMatch) {
  2303. // similar to the previous test, but there's not even a match in the
  2304. // specified RRset. Essentially there's no difference in the result.
  2305. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2306. this->updater_->deleteRRset(*this->rrset_);
  2307. {
  2308. SCOPED_TRACE("delete no match");
  2309. this->expected_rdatas_.push_back("192.0.2.1");
  2310. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2311. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2312. this->expected_rdatas_, this->empty_rdatas_);
  2313. }
  2314. }
  2315. TYPED_TEST(DatabaseClientTest, deleteWithDifferentTTL) {
  2316. // Our delete interface simply ignores TTL (may change in a future version)
  2317. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2318. RRTTL(1800)));
  2319. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2320. this->rrset_->getClass(),
  2321. "192.0.2.1"));
  2322. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2323. this->updater_->deleteRRset(*this->rrset_);
  2324. {
  2325. SCOPED_TRACE("delete RRset with a different TTL");
  2326. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2327. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2328. this->empty_rdatas_, this->empty_rdatas_);
  2329. }
  2330. }
  2331. TYPED_TEST(DatabaseClientTest, deleteDeviantRR) {
  2332. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2333. // RR class mismatch. This should be detected and rejected.
  2334. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  2335. this->rrttl_));
  2336. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2337. this->rrset_->getClass(),
  2338. "test text"));
  2339. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2340. // Out-of-zone owner name. At a higher level this should be rejected,
  2341. // but it doesn't happen in this interface.
  2342. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  2343. this->qtype_, this->rrttl_));
  2344. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2345. this->rrset_->getClass(),
  2346. "192.0.2.100"));
  2347. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->rrset_));
  2348. }
  2349. TYPED_TEST(DatabaseClientTest, deleteAfterCommit) {
  2350. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2351. this->updater_->commit();
  2352. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2353. }
  2354. TYPED_TEST(DatabaseClientTest, deleteEmptyRRset) {
  2355. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2356. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2357. this->rrttl_));
  2358. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2359. }
  2360. TYPED_TEST(DatabaseClientTest, deleteRRsetWithRRSIG) {
  2361. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2362. this->rrset_->addRRsig(*this->rrsigset_);
  2363. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2364. }
  2365. TYPED_TEST(DatabaseClientTest, compoundUpdate) {
  2366. // This test case performs an arbitrary chosen add/delete operations
  2367. // in a single update transaction. Essentially there is nothing new to
  2368. // test here, but there may be some bugs that was overlooked and can
  2369. // only happen in the compound update scenario, so we test it.
  2370. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2371. // add a new RR to an existing RRset
  2372. this->updater_->addRRset(*this->rrset_);
  2373. this->expected_rdatas_.clear();
  2374. this->expected_rdatas_.push_back("192.0.2.1");
  2375. this->expected_rdatas_.push_back("192.0.2.2");
  2376. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2377. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2378. this->expected_rdatas_, this->empty_rdatas_);
  2379. // delete an existing RR
  2380. this->rrset_.reset(new RRset(Name("www.example.org"), this->qclass_,
  2381. this->qtype_, this->rrttl_));
  2382. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2383. this->rrset_->getClass(),
  2384. "192.0.2.1"));
  2385. this->updater_->deleteRRset(*this->rrset_);
  2386. this->expected_rdatas_.clear();
  2387. this->expected_rdatas_.push_back("192.0.2.2");
  2388. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2389. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2390. this->expected_rdatas_, this->empty_rdatas_);
  2391. // re-add it
  2392. this->updater_->addRRset(*this->rrset_);
  2393. this->expected_rdatas_.push_back("192.0.2.1");
  2394. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2395. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2396. this->expected_rdatas_, this->empty_rdatas_);
  2397. // add a new RR with a new name
  2398. const Name newname("newname.example.org");
  2399. const RRType newtype(RRType::AAAA());
  2400. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2401. this->rrttl_, ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2402. this->empty_rdatas_);
  2403. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  2404. this->rrttl_));
  2405. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2406. this->rrset_->getClass(),
  2407. "2001:db8::10"));
  2408. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2409. this->rrset_->getClass(),
  2410. "2001:db8::11"));
  2411. this->updater_->addRRset(*this->rrset_);
  2412. this->expected_rdatas_.clear();
  2413. this->expected_rdatas_.push_back("2001:db8::10");
  2414. this->expected_rdatas_.push_back("2001:db8::11");
  2415. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2416. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2417. this->empty_rdatas_);
  2418. // delete one RR from the previous set
  2419. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  2420. this->rrttl_));
  2421. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2422. this->rrset_->getClass(),
  2423. "2001:db8::11"));
  2424. this->updater_->deleteRRset(*this->rrset_);
  2425. this->expected_rdatas_.clear();
  2426. this->expected_rdatas_.push_back("2001:db8::10");
  2427. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2428. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2429. this->empty_rdatas_);
  2430. // Commit the changes, confirm the entire changes applied.
  2431. this->updater_->commit();
  2432. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2433. this->expected_rdatas_.clear();
  2434. this->expected_rdatas_.push_back("192.0.2.2");
  2435. this->expected_rdatas_.push_back("192.0.2.1");
  2436. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_, this->rrttl_,
  2437. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2438. this->empty_rdatas_);
  2439. this->expected_rdatas_.clear();
  2440. this->expected_rdatas_.push_back("2001:db8::10");
  2441. doFindTest(*finder, newname, newtype, newtype, this->rrttl_,
  2442. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2443. this->empty_rdatas_);
  2444. }
  2445. TYPED_TEST(DatabaseClientTest, previous) {
  2446. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2447. EXPECT_EQ(Name("www.example.org."),
  2448. finder->findPreviousName(Name("www2.example.org.")));
  2449. // Check a name that doesn't exist there
  2450. EXPECT_EQ(Name("www.example.org."),
  2451. finder->findPreviousName(Name("www1.example.org.")));
  2452. if (this->is_mock_) { // We can't really force the DB to throw
  2453. // Check it doesn't crash or anything if the underlying DB throws
  2454. DataSourceClient::FindResult
  2455. zone(this->client_->findZone(Name("bad.example.org")));
  2456. finder =
  2457. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder);
  2458. EXPECT_THROW(finder->findPreviousName(Name("bad.example.org")),
  2459. isc::NotImplemented);
  2460. } else {
  2461. // No need to test this on mock one, because we test only that
  2462. // the exception gets through
  2463. // A name before the origin
  2464. EXPECT_THROW(finder->findPreviousName(Name("example.com")),
  2465. isc::NotImplemented);
  2466. }
  2467. }
  2468. TYPED_TEST(DatabaseClientTest, invalidRdata) {
  2469. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2470. EXPECT_THROW(finder->find(Name("invalidrdata.example.org."), RRType::A()),
  2471. DataSourceError);
  2472. EXPECT_THROW(finder->find(Name("invalidrdata2.example.org."), RRType::A()),
  2473. DataSourceError);
  2474. }
  2475. TEST_F(MockDatabaseClientTest, missingNSEC) {
  2476. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2477. /*
  2478. * FIXME: For now, we can't really distinguish this bogus input
  2479. * from not-signed zone so we can't throw. But once we can,
  2480. * enable the original test.
  2481. */
  2482. #if 0
  2483. EXPECT_THROW(finder->find(Name("badnsec2.example.org."), RRType::A(), NULL,
  2484. ZoneFinder::FIND_DNSSEC),
  2485. DataSourceError);
  2486. #endif
  2487. doFindTest(*finder, Name("badnsec2.example.org."), RRType::A(),
  2488. RRType::A(), this->rrttl_, ZoneFinder::NXDOMAIN,
  2489. this->expected_rdatas_, this->expected_sig_rdatas_);
  2490. }
  2491. TEST_F(MockDatabaseClientTest, badName) {
  2492. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2493. EXPECT_THROW(finder->findPreviousName(Name("brokenname.example.org.")),
  2494. DataSourceError);
  2495. }
  2496. /*
  2497. * Test correct use of the updater with a journal.
  2498. */
  2499. TEST_F(MockDatabaseClientTest, journal) {
  2500. updater_ = client_->getUpdater(zname_, false, true);
  2501. updater_->deleteRRset(*soa_);
  2502. updater_->deleteRRset(*rrset_);
  2503. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  2504. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  2505. "ns1.example.org. admin.example.org. "
  2506. "1235 3600 1800 2419200 7200"));
  2507. updater_->addRRset(*soa_);
  2508. updater_->addRRset(*rrset_);
  2509. ASSERT_NO_THROW(updater_->commit());
  2510. std::vector<JournalEntry> expected;
  2511. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2512. DatabaseAccessor::DIFF_DELETE,
  2513. "example.org.", "SOA", "3600",
  2514. "ns1.example.org. admin.example.org. "
  2515. "1234 3600 1800 2419200 7200"));
  2516. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2517. DatabaseAccessor::DIFF_DELETE,
  2518. "www.example.org.", "A", "3600",
  2519. "192.0.2.2"));
  2520. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  2521. DatabaseAccessor::DIFF_ADD,
  2522. "example.org.", "SOA", "3600",
  2523. "ns1.example.org. admin.example.org. "
  2524. "1235 3600 1800 2419200 7200"));
  2525. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  2526. DatabaseAccessor::DIFF_ADD,
  2527. "www.example.org.", "A", "3600",
  2528. "192.0.2.2"));
  2529. current_accessor_->checkJournal(expected);
  2530. }
  2531. /*
  2532. * Push multiple delete-add sequences. Checks it is allowed and all is
  2533. * saved.
  2534. */
  2535. TEST_F(MockDatabaseClientTest, journalMultiple) {
  2536. std::vector<JournalEntry> expected;
  2537. updater_ = client_->getUpdater(zname_, false, true);
  2538. std::string soa_rdata = "ns1.example.org. admin.example.org. "
  2539. "1234 3600 1800 2419200 7200";
  2540. for (size_t i(1); i < 100; ++ i) {
  2541. // Remove the old SOA
  2542. updater_->deleteRRset(*soa_);
  2543. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234 + i - 1,
  2544. DatabaseAccessor::DIFF_DELETE,
  2545. "example.org.", "SOA", "3600",
  2546. soa_rdata));
  2547. // Create a new SOA
  2548. soa_rdata = "ns1.example.org. admin.example.org. " +
  2549. lexical_cast<std::string>(1234 + i) + " 3600 1800 2419200 7200";
  2550. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  2551. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  2552. soa_rdata));
  2553. // Add the new SOA
  2554. updater_->addRRset(*soa_);
  2555. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234 + i,
  2556. DatabaseAccessor::DIFF_ADD,
  2557. "example.org.", "SOA", "3600",
  2558. soa_rdata));
  2559. }
  2560. ASSERT_NO_THROW(updater_->commit());
  2561. // Check the journal contains everything.
  2562. current_accessor_->checkJournal(expected);
  2563. }
  2564. /*
  2565. * Test passing a forbidden sequence to it and expect it to throw.
  2566. *
  2567. * Note that we implicitly test in different testcases (these for add and
  2568. * delete) that if the journaling is false, it doesn't expect the order.
  2569. */
  2570. TEST_F(MockDatabaseClientTest, journalBadSequence) {
  2571. std::vector<JournalEntry> expected;
  2572. {
  2573. SCOPED_TRACE("Delete A before SOA");
  2574. updater_ = client_->getUpdater(zname_, false, true);
  2575. EXPECT_THROW(updater_->deleteRRset(*rrset_), isc::BadValue);
  2576. // Make sure the journal is empty now
  2577. current_accessor_->checkJournal(expected);
  2578. }
  2579. {
  2580. SCOPED_TRACE("Add before delete");
  2581. updater_ = client_->getUpdater(zname_, false, true);
  2582. EXPECT_THROW(updater_->addRRset(*soa_), isc::BadValue);
  2583. // Make sure the journal is empty now
  2584. current_accessor_->checkJournal(expected);
  2585. }
  2586. {
  2587. SCOPED_TRACE("Add A before SOA");
  2588. updater_ = client_->getUpdater(zname_, false, true);
  2589. // So far OK
  2590. EXPECT_NO_THROW(updater_->deleteRRset(*soa_));
  2591. // But we miss the add SOA here
  2592. EXPECT_THROW(updater_->addRRset(*rrset_), isc::BadValue);
  2593. // Make sure the journal contains only the first one
  2594. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2595. DatabaseAccessor::DIFF_DELETE,
  2596. "example.org.", "SOA", "3600",
  2597. "ns1.example.org. admin.example.org. "
  2598. "1234 3600 1800 2419200 7200"));
  2599. current_accessor_->checkJournal(expected);
  2600. }
  2601. {
  2602. SCOPED_TRACE("Commit before add");
  2603. updater_ = client_->getUpdater(zname_, false, true);
  2604. // So far OK
  2605. EXPECT_NO_THROW(updater_->deleteRRset(*soa_));
  2606. // Commit at the wrong time
  2607. EXPECT_THROW(updater_->commit(), isc::Unexpected);
  2608. current_accessor_->checkJournal(expected);
  2609. }
  2610. {
  2611. SCOPED_TRACE("Delete two SOAs");
  2612. updater_ = client_->getUpdater(zname_, false, true);
  2613. // So far OK
  2614. EXPECT_NO_THROW(updater_->deleteRRset(*soa_));
  2615. // Delete the SOA again
  2616. EXPECT_THROW(updater_->deleteRRset(*soa_), isc::BadValue);
  2617. current_accessor_->checkJournal(expected);
  2618. }
  2619. {
  2620. SCOPED_TRACE("Add two SOAs");
  2621. updater_ = client_->getUpdater(zname_, false, true);
  2622. // So far OK
  2623. EXPECT_NO_THROW(updater_->deleteRRset(*soa_));
  2624. // Still OK
  2625. EXPECT_NO_THROW(updater_->addRRset(*soa_));
  2626. // But this one is added again
  2627. EXPECT_THROW(updater_->addRRset(*soa_), isc::BadValue);
  2628. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2629. DatabaseAccessor::DIFF_ADD,
  2630. "example.org.", "SOA", "3600",
  2631. "ns1.example.org. admin.example.org. "
  2632. "1234 3600 1800 2419200 7200"));
  2633. current_accessor_->checkJournal(expected);
  2634. }
  2635. }
  2636. /*
  2637. * Test it rejects to store journals when we request it together with
  2638. * erasing the whole zone.
  2639. */
  2640. TEST_F(MockDatabaseClientTest, journalOnErase) {
  2641. EXPECT_THROW(client_->getUpdater(zname_, true, true), isc::BadValue);
  2642. }
  2643. /*
  2644. * Check that the exception isn't raised when the journal is not implemented.
  2645. */
  2646. TEST_F(MockDatabaseClientTest, journalNotImplemented) {
  2647. updater_ = client_->getUpdater(Name("null.example.org"), false, true);
  2648. EXPECT_NO_THROW(updater_->deleteRRset(*soa_));
  2649. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  2650. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  2651. "ns1.example.org. admin.example.org. "
  2652. "1234 3600 1800 2419201 7200"));
  2653. EXPECT_NO_THROW(updater_->addRRset(*soa_));
  2654. }
  2655. /*
  2656. * Test that different exceptions are propagated.
  2657. */
  2658. TEST_F(MockDatabaseClientTest, journalException) {
  2659. updater_ = client_->getUpdater(Name("bad.example.org"), false, true);
  2660. EXPECT_THROW(updater_->deleteRRset(*soa_), DataSourceError);
  2661. }
  2662. TYPED_TEST(DatabaseClientTest, getJournalReader) {
  2663. ZoneJournalReaderPtr jnl_reader(this->client_->getJournalReader(
  2664. this->zname_, 0, 1));
  2665. EXPECT_FALSE(jnl_reader->getNextDiff());
  2666. EXPECT_THROW(this->client_->getJournalReader(Name("nosuchzone"), 0, 1),
  2667. DataSourceError);
  2668. }
  2669. }