database_unittest.cc 198 KB

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