database_unittest.cc 180 KB

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