database_unittest.cc 154 KB

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