database_unittest.cc 189 KB

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