database_unittest.cc 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260
  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::shared_ptr;
  36. using boost::dynamic_pointer_cast;
  37. using boost::lexical_cast;
  38. using namespace isc::dns;
  39. namespace {
  40. // Imaginary zone IDs used in the mock accessor below.
  41. const int READONLY_ZONE_ID = 42;
  42. const int WRITABLE_ZONE_ID = 4200;
  43. // Commonly used test data
  44. const char* const TEST_RECORDS[][5] = {
  45. // some plain data
  46. {"www.example.org.", "A", "3600", "", "192.0.2.1"},
  47. {"www.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  48. {"www.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  49. {"www.example.org.", "NSEC", "3600", "", "www2.example.org. A AAAA NSEC RRSIG"},
  50. {"www.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  51. {"www2.example.org.", "A", "3600", "", "192.0.2.1"},
  52. {"www2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  53. {"www2.example.org.", "A", "3600", "", "192.0.2.2"},
  54. {"cname.example.org.", "CNAME", "3600", "", "www.example.org."},
  55. // some DNSSEC-'signed' data
  56. {"signed1.example.org.", "A", "3600", "", "192.0.2.1"},
  57. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  58. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  59. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  60. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  61. {"signed1.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  62. {"signedcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  63. {"signedcname1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  64. // special case might fail; sig is for cname, which isn't there (should be ignored)
  65. // (ignoring of 'normal' other type is done above by www.)
  66. {"acnamesig1.example.org.", "A", "3600", "", "192.0.2.1"},
  67. {"acnamesig1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  68. {"acnamesig1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  69. // let's pretend we have a database that is not careful
  70. // about the order in which it returns data
  71. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  72. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  73. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  74. {"signed2.example.org.", "A", "3600", "", "192.0.2.1"},
  75. {"signed2.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  76. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  77. {"signedcname2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  78. {"signedcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  79. {"acnamesig2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  80. {"acnamesig2.example.org.", "A", "3600", "", "192.0.2.1"},
  81. {"acnamesig2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  82. {"acnamesig3.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  83. {"acnamesig3.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  84. {"acnamesig3.example.org.", "A", "3600", "", "192.0.2.1"},
  85. {"ttldiff1.example.org.", "A", "3600", "", "192.0.2.1"},
  86. {"ttldiff1.example.org.", "A", "360", "", "192.0.2.2"},
  87. {"ttldiff2.example.org.", "A", "360", "", "192.0.2.1"},
  88. {"ttldiff2.example.org.", "A", "3600", "", "192.0.2.2"},
  89. // also add some intentionally bad data
  90. {"badcname1.example.org.", "A", "3600", "", "192.0.2.1"},
  91. {"badcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  92. {"badcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  93. {"badcname2.example.org.", "A", "3600", "", "192.0.2.1"},
  94. {"badcname3.example.org.", "CNAME", "3600", "", "www.example.org."},
  95. {"badcname3.example.org.", "CNAME", "3600", "", "www.example2.org."},
  96. {"badrdata.example.org.", "A", "3600", "", "bad"},
  97. {"badtype.example.org.", "BAD_TYPE", "3600", "", "192.0.2.1"},
  98. {"badttl.example.org.", "A", "badttl", "", "192.0.2.1"},
  99. {"badsig.example.org.", "A", "badttl", "", "192.0.2.1"},
  100. {"badsig.example.org.", "RRSIG", "3600", "", "A 5 3 3600 somebaddata 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  101. {"badsigtype.example.org.", "A", "3600", "", "192.0.2.1"},
  102. {"badsigtype.example.org.", "RRSIG", "3600", "TXT", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  103. // Data for testing delegation (with NS and DNAME)
  104. {"delegation.example.org.", "NS", "3600", "", "ns.example.com."},
  105. {"delegation.example.org.", "NS", "3600", "",
  106. "ns.delegation.example.org."},
  107. {"delegation.example.org.", "DS", "3600", "", "1 RSAMD5 2 abcd"},
  108. {"delegation.example.org.", "RRSIG", "3600", "", "NS 5 3 3600 "
  109. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  110. {"ns.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  111. {"deep.below.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  112. {"dname.example.org.", "A", "3600", "", "192.0.2.1"},
  113. {"dname.example.org.", "DNAME", "3600", "", "dname.example.com."},
  114. {"dname.example.org.", "RRSIG", "3600", "",
  115. "DNAME 5 3 3600 20000101000000 20000201000000 12345 "
  116. "example.org. FAKEFAKEFAKE"},
  117. {"below.dname.example.org.", "A", "3600", "", "192.0.2.1"},
  118. // Broken NS
  119. {"brokenns1.example.org.", "A", "3600", "", "192.0.2.1"},
  120. {"brokenns1.example.org.", "NS", "3600", "", "ns.example.com."},
  121. {"brokenns2.example.org.", "NS", "3600", "", "ns.example.com."},
  122. {"brokenns2.example.org.", "A", "3600", "", "192.0.2.1"},
  123. // Now double DNAME, to test failure mode
  124. {"baddname.example.org.", "DNAME", "3600", "", "dname1.example.com."},
  125. {"baddname.example.org.", "DNAME", "3600", "", "dname2.example.com."},
  126. // Put some data into apex (including NS) so we can check our NS
  127. // doesn't break anything
  128. {"example.org.", "SOA", "3600", "", "ns1.example.org. admin.example.org. "
  129. "1234 3600 1800 2419200 7200" },
  130. {"example.org.", "NS", "3600", "", "ns.example.com."},
  131. {"example.org.", "A", "3600", "", "192.0.2.1"},
  132. {"example.org.", "NSEC", "3600", "", "acnamesig1.example.org. NS A NSEC RRSIG"},
  133. {"example.org.", "RRSIG", "3600", "", "SOA 5 3 3600 20000101000000 "
  134. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  135. {"example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 "
  136. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  137. {"example.org.", "RRSIG", "3600", "", "NS 5 3 3600 20000101000000 "
  138. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  139. // This is because of empty domain test
  140. {"a.b.example.org.", "A", "3600", "", "192.0.2.1"},
  141. // Something for wildcards
  142. {"*.wild.example.org.", "A", "3600", "", "192.0.2.5"},
  143. {"*.wild.example.org.", "RRSIG", "3600", "A", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  144. {"*.wild.example.org.", "NSEC", "3600", "", "cancel.here.wild.example.org. A NSEC RRSIG"},
  145. {"*.wild.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  146. {"cancel.here.wild.example.org.", "AAAA", "3600", "", "2001:db8::5"},
  147. {"delegatedwild.example.org.", "NS", "3600", "", "ns.example.com."},
  148. {"*.delegatedwild.example.org.", "A", "3600", "", "192.0.2.5"},
  149. {"wild.*.foo.example.org.", "A", "3600", "", "192.0.2.5"},
  150. {"wild.*.foo.*.bar.example.org.", "A", "3600", "", "192.0.2.5"},
  151. {"wild.*.foo.*.bar.example.org.", "NSEC", "3600", "",
  152. "brokenns1.example.org. A NSEC"},
  153. {"bao.example.org.", "NSEC", "3600", "", "wild.*.foo.*.bar.example.org. NSEC"},
  154. {"*.cnamewild.example.org.", "CNAME", "3600", "", "www.example.org."},
  155. {"*.dnamewild.example.org.", "DNAME", "3600", "", "dname.example.com."},
  156. {"*.nswild.example.org.", "NS", "3600", "", "ns.example.com."},
  157. // For NSEC empty non-terminal
  158. {"l.example.org.", "NSEC", "3600", "", "empty.nonterminal.example.org. NSEC"},
  159. {"empty.nonterminal.example.org.", "A", "3600", "", "192.0.2.1"},
  160. // Invalid rdata
  161. {"invalidrdata.example.org.", "A", "3600", "", "Bunch of nonsense"},
  162. {"invalidrdata2.example.org.", "A", "3600", "", "192.0.2.1"},
  163. {"invalidrdata2.example.org.", "RRSIG", "3600", "", "Nonsense"},
  164. {NULL, NULL, NULL, NULL, NULL},
  165. };
  166. /*
  167. * An accessor with minimum implementation, keeping the original
  168. * "NotImplemented" methods.
  169. */
  170. class NopAccessor : public DatabaseAccessor {
  171. public:
  172. NopAccessor() : database_name_("mock_database")
  173. { }
  174. virtual std::pair<bool, int> getZone(const std::string& name) const {
  175. if (name == "example.org.") {
  176. return (std::pair<bool, int>(true, READONLY_ZONE_ID));
  177. } else if (name == "null.example.org.") {
  178. return (std::pair<bool, int>(true, 13));
  179. } else if (name == "empty.example.org.") {
  180. return (std::pair<bool, int>(true, 0));
  181. } else if (name == "bad.example.org.") {
  182. return (std::pair<bool, int>(true, -1));
  183. } else {
  184. return (std::pair<bool, int>(false, 0));
  185. }
  186. }
  187. virtual shared_ptr<DatabaseAccessor> clone() {
  188. // This accessor is stateless, so we can simply return a new instance.
  189. return (shared_ptr<DatabaseAccessor>(new NopAccessor));
  190. }
  191. virtual std::pair<bool, int> startUpdateZone(const std::string&, bool) {
  192. // return dummy value. unused anyway.
  193. return (pair<bool, int>(true, 0));
  194. }
  195. virtual void startTransaction() {}
  196. virtual void commit() {}
  197. virtual void rollback() {}
  198. virtual void addRecordToZone(const string (&)[ADD_COLUMN_COUNT]) {}
  199. virtual void deleteRecordInZone(const string (&)[DEL_PARAM_COUNT]) {}
  200. virtual void addRecordDiff(int, uint32_t, DiffOperation,
  201. const std::string (&)[DIFF_PARAM_COUNT]) {}
  202. virtual const std::string& getDBName() const {
  203. return (database_name_);
  204. }
  205. virtual IteratorContextPtr getRecords(const std::string&, int, bool)
  206. const
  207. {
  208. isc_throw(isc::NotImplemented,
  209. "This database datasource can't be iterated");
  210. }
  211. virtual IteratorContextPtr getAllRecords(int) const {
  212. isc_throw(isc::NotImplemented,
  213. "This database datasource can't be iterated");
  214. }
  215. virtual IteratorContextPtr getDiffs(int, uint32_t, uint32_t) const {
  216. isc_throw(isc::NotImplemented,
  217. "This database datasource doesn't support diffs");
  218. }
  219. virtual std::string findPreviousName(int, const std::string&) const {
  220. isc_throw(isc::NotImplemented,
  221. "This data source doesn't support DNSSEC");
  222. }
  223. private:
  224. const std::string database_name_;
  225. };
  226. /**
  227. * Single journal entry in the mock database.
  228. *
  229. * All the members there are public for simplicity, as it only stores data.
  230. * We use the implicit constructor and operator. The members can't be const
  231. * because of the assignment operator (used in the vectors).
  232. */
  233. struct JournalEntry {
  234. JournalEntry(int id, uint32_t serial,
  235. DatabaseAccessor::DiffOperation operation,
  236. const std::string (&data)[DatabaseAccessor::DIFF_PARAM_COUNT])
  237. : id_(id), serial_(serial), operation_(operation)
  238. {
  239. data_[DatabaseAccessor::DIFF_NAME] = data[DatabaseAccessor::DIFF_NAME];
  240. data_[DatabaseAccessor::DIFF_TYPE] = data[DatabaseAccessor::DIFF_TYPE];
  241. data_[DatabaseAccessor::DIFF_TTL] = data[DatabaseAccessor::DIFF_TTL];
  242. data_[DatabaseAccessor::DIFF_RDATA] =
  243. data[DatabaseAccessor::DIFF_RDATA];
  244. }
  245. JournalEntry(int id, uint32_t serial,
  246. DatabaseAccessor::DiffOperation operation,
  247. const std::string& name, const std::string& type,
  248. const std::string& ttl, const std::string& rdata):
  249. id_(id), serial_(serial), operation_(operation)
  250. {
  251. data_[DatabaseAccessor::DIFF_NAME] = name;
  252. data_[DatabaseAccessor::DIFF_TYPE] = type;
  253. data_[DatabaseAccessor::DIFF_TTL] = ttl;
  254. data_[DatabaseAccessor::DIFF_RDATA] = rdata;
  255. }
  256. int id_;
  257. uint32_t serial_;
  258. DatabaseAccessor::DiffOperation operation_;
  259. std::string data_[DatabaseAccessor::DIFF_PARAM_COUNT];
  260. bool operator==(const JournalEntry& other) const {
  261. for (size_t i = 0; i < DatabaseAccessor::DIFF_PARAM_COUNT; ++ i) {
  262. if (data_[i] != other.data_[i]) {
  263. return false;
  264. }
  265. }
  266. // No need to check data here, checked above
  267. return (id_ == other.id_ && serial_ == other.serial_ &&
  268. operation_ == other.operation_);
  269. }
  270. };
  271. /*
  272. * A virtual database accessor that pretends it contains single zone --
  273. * example.org.
  274. *
  275. * It has the same getZone method as NopConnection, but it provides
  276. * implementation of the optional functionality.
  277. */
  278. class MockAccessor : public NopAccessor {
  279. // Type of mock database "row"s. This is a map whose keys are the
  280. // own names. We internally sort them by the name comparison order.
  281. struct NameCompare : public binary_function<string, string, bool> {
  282. bool operator()(const string& n1, const string& n2) const {
  283. return (Name(n1).compare(Name(n2)).getOrder() < 0);
  284. }
  285. };
  286. typedef std::map<std::string,
  287. std::vector< std::vector<std::string> >,
  288. NameCompare > Domains;
  289. public:
  290. MockAccessor() : rollbacked_(false), did_transaction_(false) {
  291. readonly_records_ = &readonly_records_master_;
  292. update_records_ = &update_records_master_;
  293. empty_records_ = &empty_records_master_;
  294. journal_entries_ = &journal_entries_master_;
  295. fillData();
  296. }
  297. virtual shared_ptr<DatabaseAccessor> clone() {
  298. shared_ptr<MockAccessor> cloned_accessor(new MockAccessor());
  299. cloned_accessor->readonly_records_ = &readonly_records_master_;
  300. cloned_accessor->update_records_ = &update_records_master_;
  301. cloned_accessor->empty_records_ = &empty_records_master_;
  302. cloned_accessor->journal_entries_ = &journal_entries_master_;
  303. latest_clone_ = cloned_accessor;
  304. return (cloned_accessor);
  305. }
  306. virtual void startTransaction() {
  307. // Currently we only use this transaction for simple read-only
  308. // operations. So we just make a local copy of the data (we don't
  309. // care about what happens after commit() or rollback()).
  310. // Obviously as a consequence, if a test case tries to make multiple
  311. // transactions on a single mock accessor it will fail.
  312. // Check any attempt of multiple transactions
  313. if (did_transaction_) {
  314. isc_throw(isc::Unexpected, "MockAccessor::startTransaction() "
  315. "called multiple times - likely a bug in the test");
  316. }
  317. readonly_records_copy_ = *readonly_records_;
  318. readonly_records_ = &readonly_records_copy_;
  319. did_transaction_ = true;
  320. }
  321. private:
  322. class MockNameIteratorContext : public IteratorContext {
  323. public:
  324. MockNameIteratorContext(const MockAccessor& mock_accessor, int zone_id,
  325. const std::string& name, bool subdomains) :
  326. searched_name_(name), cur_record_(0)
  327. {
  328. // 'hardcoded' names to trigger exceptions
  329. // On these names some exceptions are thrown, to test the robustness
  330. // of the find() method.
  331. if (searched_name_ == "dsexception.in.search.") {
  332. isc_throw(DataSourceError, "datasource exception on search");
  333. } else if (searched_name_ == "iscexception.in.search.") {
  334. isc_throw(isc::Exception, "isc exception on search");
  335. } else if (searched_name_ == "basicexception.in.search.") {
  336. throw std::exception();
  337. }
  338. cur_record_ = 0;
  339. const Domains& cur_records = mock_accessor.getMockRecords(zone_id);
  340. if (cur_records.count(name) > 0) {
  341. // we're not aiming for efficiency in this test, simply
  342. // copy the relevant vector from records
  343. cur_name = cur_records.find(name)->second;
  344. } else if (subdomains) {
  345. cur_name.clear();
  346. // Just walk everything and check if it is a subdomain.
  347. // If it is, just copy all data from there.
  348. for (Domains::const_iterator i = cur_records.begin();
  349. i != cur_records.end(); ++i) {
  350. const Name local(i->first);
  351. if (local.compare(Name(name)).getRelation() ==
  352. isc::dns::NameComparisonResult::SUBDOMAIN) {
  353. cur_name.insert(cur_name.end(), i->second.begin(),
  354. i->second.end());
  355. }
  356. }
  357. } else {
  358. cur_name.clear();
  359. }
  360. }
  361. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) {
  362. if (searched_name_ == "dsexception.in.getnext.") {
  363. isc_throw(DataSourceError, "datasource exception on getnextrecord");
  364. } else if (searched_name_ == "iscexception.in.getnext.") {
  365. isc_throw(isc::Exception, "isc exception on getnextrecord");
  366. } else if (searched_name_ == "basicexception.in.getnext.") {
  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. 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. 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. shared_ptr<DatabaseClient::Finder> finder(
  899. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  900. ASSERT_NE(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. shared_ptr<DatabaseClient::Finder> getFinder() {
  908. DataSourceClient::FindResult zone(client_->findZone(zname_));
  909. EXPECT_EQ(result::SUCCESS, zone.code);
  910. 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. shared_ptr<DatabaseClient> client_;
  991. const std::string database_name_;
  992. // The zone finder of the test zone commonly used in various tests.
  993. 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. 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. 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(), shared_ptr<DatabaseAccessor>(
  1088. new NopAccessor())).getIterator(Name("example.org")),
  1089. isc::NotImplemented);
  1090. }
  1091. // Pretend a bug in the connection and pass NULL as the context
  1092. // Should not crash, but gracefully throw. Works for the mock accessor only.
  1093. TEST_F(MockDatabaseClientTest, nullIteratorContext) {
  1094. EXPECT_THROW(this->client_->getIterator(Name("null.example.org")),
  1095. isc::Unexpected);
  1096. }
  1097. // It doesn't crash or anything if the zone is completely empty.
  1098. // Works for the mock accessor only.
  1099. TEST_F(MockDatabaseClientTest, emptyIterator) {
  1100. ZoneIteratorPtr it(this->client_->getIterator(Name("empty.example.org")));
  1101. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  1102. // This is past the end, it should throw
  1103. EXPECT_THROW(it->getNextRRset(), isc::Unexpected);
  1104. }
  1105. // checks if the given rrset matches the
  1106. // given name, class, type and rdatas
  1107. void
  1108. checkRRset(isc::dns::ConstRRsetPtr rrset,
  1109. const isc::dns::Name& name,
  1110. const isc::dns::RRClass& rrclass,
  1111. const isc::dns::RRType& rrtype,
  1112. const isc::dns::RRTTL& rrttl,
  1113. const std::vector<std::string>& rdatas) {
  1114. isc::dns::RRsetPtr expected_rrset(
  1115. new isc::dns::RRset(name, rrclass, rrtype, rrttl));
  1116. for (unsigned int i = 0; i < rdatas.size(); ++i) {
  1117. expected_rrset->addRdata(
  1118. isc::dns::rdata::createRdata(rrtype, rrclass,
  1119. rdatas[i]));
  1120. }
  1121. isc::testutils::rrsetCheck(expected_rrset, rrset);
  1122. }
  1123. // Iterate through a zone
  1124. TYPED_TEST(DatabaseClientTest, iterator) {
  1125. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  1126. ConstRRsetPtr rrset(it->getNextRRset());
  1127. ASSERT_NE(ConstRRsetPtr(), rrset);
  1128. // The first name should be the zone origin.
  1129. EXPECT_EQ(this->zname_, rrset->getName());
  1130. // The rest of the checks work only for the mock accessor.
  1131. if (!this->is_mock_) {
  1132. return;
  1133. }
  1134. this->expected_rdatas_.clear();
  1135. this->expected_rdatas_.push_back("192.0.2.1");
  1136. checkRRset(rrset, Name("example.org"), this->qclass_, RRType::A(),
  1137. this->rrttl_, this->expected_rdatas_);
  1138. rrset = it->getNextRRset();
  1139. this->expected_rdatas_.clear();
  1140. this->expected_rdatas_.push_back("ns1.example.org. admin.example.org. "
  1141. "1234 3600 1800 2419200 7200");
  1142. checkRRset(rrset, Name("example.org"), this->qclass_, RRType::SOA(),
  1143. this->rrttl_, this->expected_rdatas_);
  1144. rrset = it->getNextRRset();
  1145. this->expected_rdatas_.clear();
  1146. this->expected_rdatas_.push_back("192.0.2.1");
  1147. this->expected_rdatas_.push_back("192.0.2.2");
  1148. checkRRset(rrset, Name("x.example.org"), this->qclass_, RRType::A(),
  1149. RRTTL(300), this->expected_rdatas_);
  1150. rrset = it->getNextRRset();
  1151. this->expected_rdatas_.clear();
  1152. this->expected_rdatas_.push_back("2001:db8::1");
  1153. this->expected_rdatas_.push_back("2001:db8::2");
  1154. checkRRset(rrset, Name("x.example.org"), this->qclass_, RRType::AAAA(),
  1155. RRTTL(300), this->expected_rdatas_);
  1156. rrset = it->getNextRRset();
  1157. ASSERT_NE(ConstRRsetPtr(), rrset);
  1158. this->expected_rdatas_.clear();
  1159. this->expected_rdatas_.push_back("192.0.2.1");
  1160. this->expected_rdatas_.push_back("192.0.2.2");
  1161. checkRRset(rrset, Name("ttldiff.example.org"), this->qclass_, RRType::A(),
  1162. RRTTL(300), this->expected_rdatas_);
  1163. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  1164. }
  1165. // This has inconsistent TTL in the set (the rest, like nonsense in
  1166. // the data is handled in rdata itself). Works for the mock accessor only.
  1167. TEST_F(MockDatabaseClientTest, badIterator) {
  1168. // It should not throw, but get the lowest one of them
  1169. ZoneIteratorPtr it(this->client_->getIterator(Name("bad.example.org")));
  1170. EXPECT_EQ(it->getNextRRset()->getTTL(), isc::dns::RRTTL(300));
  1171. }
  1172. TYPED_TEST(DatabaseClientTest, getSOAFromIterator) {
  1173. vector<string> soa_data;
  1174. soa_data.push_back("ns1.example.org. admin.example.org. "
  1175. "1234 3600 1800 2419200 7200");
  1176. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1177. ASSERT_TRUE(it);
  1178. checkRRset(it->getSOA(), this->zname_, this->qclass_, RRType::SOA(),
  1179. this->rrttl_, soa_data);
  1180. // Iterate over the zone until we find an SOA. Although there's a broken
  1181. // RDATA that would trigger an exception in getNextRRset(), we should
  1182. // reach the SOA as the sequence should be sorted and the SOA is at
  1183. // the origin name (which has no bogus data).
  1184. ConstRRsetPtr rrset;
  1185. while ((rrset = it->getNextRRset()) != ConstRRsetPtr() &&
  1186. rrset->getType() != RRType::SOA()) {
  1187. ;
  1188. }
  1189. ASSERT_TRUE(rrset);
  1190. // It should be identical to the result of getSOA().
  1191. isc::testutils::rrsetCheck(it->getSOA(), rrset);
  1192. }
  1193. TYPED_TEST(DatabaseClientTest, noSOAFromIterator) {
  1194. // First, empty the zone.
  1195. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1196. this->updater_->commit();
  1197. // Then getSOA() should return NULL.
  1198. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1199. ASSERT_TRUE(it);
  1200. EXPECT_FALSE(it->getSOA());
  1201. }
  1202. TYPED_TEST(DatabaseClientTest, iterateThenUpdate) {
  1203. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1204. ASSERT_TRUE(it);
  1205. // Try to empty the zone after getting the iterator. Depending on the
  1206. // underlying data source, it may result in an exception due to the
  1207. // transaction for the iterator. In either case the integrity of the
  1208. // iterator result should be reserved.
  1209. try {
  1210. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1211. this->updater_->commit();
  1212. // Confirm at least it doesn't contain any SOA
  1213. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1214. this->getFinder()->find(this->zname_, RRType::SOA()).code);
  1215. } catch (const DataSourceError&) {}
  1216. ConstRRsetPtr rrset;
  1217. while ((rrset = it->getNextRRset()) != ConstRRsetPtr() &&
  1218. rrset->getType() != RRType::SOA()) {
  1219. ;
  1220. }
  1221. ASSERT_TRUE(rrset);
  1222. // It should be identical to the result of getSOA().
  1223. isc::testutils::rrsetCheck(it->getSOA(), rrset);
  1224. }
  1225. TYPED_TEST(DatabaseClientTest, updateThenIterateThenUpdate) {
  1226. // First clear the zone.
  1227. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1228. this->updater_->commit();
  1229. // Then iterate over it. It should immediately reach the end, at which
  1230. // point the transaction should be committed.
  1231. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1232. ASSERT_TRUE(it);
  1233. EXPECT_FALSE(it->getNextRRset());
  1234. // So another update attempt should succeed, too.
  1235. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1236. this->updater_->commit();
  1237. }
  1238. TYPED_TEST(DatabaseClientTest, updateAfterDeleteIterator) {
  1239. // Similar to the previous case, but we delete the iterator in the
  1240. // middle of zone. The transaction should be canceled (actually no
  1241. // different from commit though) at that point.
  1242. ZoneIteratorPtr it(this->client_->getIterator(this->zname_));
  1243. ASSERT_TRUE(it);
  1244. EXPECT_TRUE(it->getNextRRset());
  1245. it.reset();
  1246. // So another update attempt should succeed.
  1247. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1248. this->updater_->commit();
  1249. }
  1250. void
  1251. doFindTest(ZoneFinder& finder,
  1252. const isc::dns::Name& name,
  1253. const isc::dns::RRType& type,
  1254. const isc::dns::RRType& expected_type,
  1255. const isc::dns::RRTTL expected_ttl,
  1256. ZoneFinder::Result expected_result,
  1257. const std::vector<std::string>& expected_rdatas,
  1258. const std::vector<std::string>& expected_sig_rdatas,
  1259. const isc::dns::Name& expected_name = isc::dns::Name::ROOT_NAME(),
  1260. const ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT)
  1261. {
  1262. SCOPED_TRACE("doFindTest " + name.toText() + " " + type.toText());
  1263. const ZoneFinder::FindResult result = finder.find(name, type, NULL,
  1264. options);
  1265. ASSERT_EQ(expected_result, result.code) << name << " " << type;
  1266. if (!expected_rdatas.empty() && result.rrset) {
  1267. checkRRset(result.rrset, expected_name != Name(".") ? expected_name :
  1268. name, finder.getClass(), expected_type, expected_ttl,
  1269. expected_rdatas);
  1270. if (!expected_sig_rdatas.empty() && result.rrset->getRRsig()) {
  1271. checkRRset(result.rrset->getRRsig(), expected_name != Name(".") ?
  1272. expected_name : name, finder.getClass(),
  1273. isc::dns::RRType::RRSIG(), expected_ttl,
  1274. expected_sig_rdatas);
  1275. } else if (expected_sig_rdatas.empty()) {
  1276. EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset->getRRsig());
  1277. } else {
  1278. ADD_FAILURE() << "Missing RRSIG";
  1279. }
  1280. } else if (expected_rdatas.empty()) {
  1281. EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset);
  1282. } else {
  1283. ADD_FAILURE() << "Missing result";
  1284. }
  1285. }
  1286. // When asking for an RRset where RRs somehow have different TTLs, it should
  1287. // convert to the lowest one.
  1288. TEST_F(MockDatabaseClientTest, ttldiff) {
  1289. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  1290. // Walk through the full iterator, we should see 1 rrset with name
  1291. // ttldiff1.example.org., and two rdatas. Same for ttldiff2
  1292. Name name("ttldiff.example.org.");
  1293. bool found = false;
  1294. //bool found2 = false;
  1295. ConstRRsetPtr rrset = it->getNextRRset();
  1296. while(rrset != ConstRRsetPtr()) {
  1297. if (rrset->getName() == name) {
  1298. ASSERT_FALSE(found);
  1299. ASSERT_EQ(2, rrset->getRdataCount());
  1300. ASSERT_EQ(RRTTL(300), rrset->getTTL());
  1301. found = true;
  1302. }
  1303. rrset = it->getNextRRset();
  1304. }
  1305. ASSERT_TRUE(found);
  1306. }
  1307. // Unless we ask for individual RRs in our iterator request. In that case
  1308. // every RR should go into its own 'rrset'
  1309. TEST_F(MockDatabaseClientTest, ttldiff_separate_rrs) {
  1310. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org"), true));
  1311. // Walk through the full iterator, we should see 1 rrset with name
  1312. // ttldiff1.example.org., and two rdatas. Same for ttldiff2
  1313. Name name("ttldiff.example.org.");
  1314. int found1 = false;
  1315. int found2 = false;
  1316. ConstRRsetPtr rrset = it->getNextRRset();
  1317. while(rrset != ConstRRsetPtr()) {
  1318. if (rrset->getName() == name) {
  1319. ASSERT_EQ(1, rrset->getRdataCount());
  1320. // We should find 1 'rrset' with TTL 300 and one with TTL 600
  1321. if (rrset->getTTL() == RRTTL(300)) {
  1322. ASSERT_FALSE(found1);
  1323. found1 = true;
  1324. } else if (rrset->getTTL() == RRTTL(600)) {
  1325. ASSERT_FALSE(found2);
  1326. found2 = true;
  1327. } else {
  1328. FAIL() << "Found unexpected TTL: " <<
  1329. rrset->getTTL().toText();
  1330. }
  1331. }
  1332. rrset = it->getNextRRset();
  1333. }
  1334. ASSERT_TRUE(found1);
  1335. ASSERT_TRUE(found2);
  1336. }
  1337. TYPED_TEST(DatabaseClientTest, find) {
  1338. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1339. this->expected_rdatas_.clear();
  1340. this->expected_sig_rdatas_.clear();
  1341. this->expected_rdatas_.push_back("192.0.2.1");
  1342. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1343. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1344. this->expected_rdatas_, this->expected_sig_rdatas_);
  1345. this->expected_rdatas_.clear();
  1346. this->expected_sig_rdatas_.clear();
  1347. this->expected_rdatas_.push_back("192.0.2.1");
  1348. this->expected_rdatas_.push_back("192.0.2.2");
  1349. doFindTest(*finder, isc::dns::Name("www2.example.org."),
  1350. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1351. this->expected_rdatas_, this->expected_sig_rdatas_);
  1352. this->expected_rdatas_.clear();
  1353. this->expected_sig_rdatas_.clear();
  1354. this->expected_rdatas_.push_back("2001:db8::1");
  1355. this->expected_rdatas_.push_back("2001:db8::2");
  1356. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1357. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1358. this->rrttl_,
  1359. ZoneFinder::SUCCESS,
  1360. this->expected_rdatas_, this->expected_sig_rdatas_);
  1361. this->expected_rdatas_.clear();
  1362. this->expected_sig_rdatas_.clear();
  1363. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1364. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
  1365. this->rrttl_,
  1366. ZoneFinder::NXRRSET,
  1367. this->expected_rdatas_, this->expected_sig_rdatas_);
  1368. this->expected_rdatas_.clear();
  1369. this->expected_sig_rdatas_.clear();
  1370. this->expected_rdatas_.push_back("www.example.org.");
  1371. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  1372. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1373. ZoneFinder::CNAME, this->expected_rdatas_,
  1374. this->expected_sig_rdatas_);
  1375. this->expected_rdatas_.clear();
  1376. this->expected_sig_rdatas_.clear();
  1377. this->expected_rdatas_.push_back("www.example.org.");
  1378. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  1379. isc::dns::RRType::CNAME(), isc::dns::RRType::CNAME(),
  1380. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1381. this->expected_sig_rdatas_);
  1382. this->expected_rdatas_.clear();
  1383. this->expected_sig_rdatas_.clear();
  1384. doFindTest(*finder, isc::dns::Name("doesnotexist.example.org."),
  1385. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1386. this->expected_rdatas_, this->expected_sig_rdatas_);
  1387. this->expected_rdatas_.clear();
  1388. this->expected_sig_rdatas_.clear();
  1389. this->expected_rdatas_.push_back("192.0.2.1");
  1390. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1391. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1392. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1393. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1394. this->expected_rdatas_, this->expected_sig_rdatas_);
  1395. this->expected_rdatas_.clear();
  1396. this->expected_sig_rdatas_.clear();
  1397. this->expected_rdatas_.push_back("2001:db8::1");
  1398. this->expected_rdatas_.push_back("2001:db8::2");
  1399. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1400. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1401. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1402. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1403. this->expected_sig_rdatas_);
  1404. this->expected_rdatas_.clear();
  1405. this->expected_sig_rdatas_.clear();
  1406. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1407. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1408. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1409. this->expected_sig_rdatas_);
  1410. this->expected_rdatas_.clear();
  1411. this->expected_sig_rdatas_.clear();
  1412. this->expected_rdatas_.push_back("www.example.org.");
  1413. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1414. doFindTest(*finder, isc::dns::Name("signedcname1.example.org."),
  1415. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1416. ZoneFinder::CNAME, this->expected_rdatas_,
  1417. this->expected_sig_rdatas_);
  1418. this->expected_rdatas_.clear();
  1419. this->expected_sig_rdatas_.clear();
  1420. this->expected_rdatas_.push_back("192.0.2.1");
  1421. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1422. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1423. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1424. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1425. this->expected_rdatas_, this->expected_sig_rdatas_);
  1426. this->expected_rdatas_.clear();
  1427. this->expected_sig_rdatas_.clear();
  1428. this->expected_rdatas_.push_back("2001:db8::2");
  1429. this->expected_rdatas_.push_back("2001:db8::1");
  1430. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1431. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1432. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1433. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1434. this->expected_sig_rdatas_);
  1435. this->expected_rdatas_.clear();
  1436. this->expected_sig_rdatas_.clear();
  1437. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1438. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1439. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1440. this->expected_sig_rdatas_);
  1441. this->expected_rdatas_.clear();
  1442. this->expected_sig_rdatas_.clear();
  1443. this->expected_rdatas_.push_back("www.example.org.");
  1444. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1445. doFindTest(*finder, isc::dns::Name("signedcname2.example.org."),
  1446. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1447. ZoneFinder::CNAME, this->expected_rdatas_,
  1448. this->expected_sig_rdatas_);
  1449. this->expected_rdatas_.clear();
  1450. this->expected_sig_rdatas_.clear();
  1451. this->expected_rdatas_.push_back("192.0.2.1");
  1452. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1453. doFindTest(*finder, isc::dns::Name("acnamesig1.example.org."),
  1454. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1455. this->expected_rdatas_, this->expected_sig_rdatas_);
  1456. this->expected_rdatas_.clear();
  1457. this->expected_sig_rdatas_.clear();
  1458. this->expected_rdatas_.push_back("192.0.2.1");
  1459. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1460. doFindTest(*finder, isc::dns::Name("acnamesig2.example.org."),
  1461. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1462. this->expected_rdatas_, this->expected_sig_rdatas_);
  1463. this->expected_rdatas_.clear();
  1464. this->expected_sig_rdatas_.clear();
  1465. this->expected_rdatas_.push_back("192.0.2.1");
  1466. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1467. doFindTest(*finder, isc::dns::Name("acnamesig3.example.org."),
  1468. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1469. this->expected_rdatas_, this->expected_sig_rdatas_);
  1470. this->expected_rdatas_.clear();
  1471. this->expected_sig_rdatas_.clear();
  1472. this->expected_rdatas_.push_back("192.0.2.1");
  1473. this->expected_rdatas_.push_back("192.0.2.2");
  1474. doFindTest(*finder, isc::dns::Name("ttldiff1.example.org."),
  1475. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1476. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1477. this->expected_sig_rdatas_);
  1478. this->expected_rdatas_.clear();
  1479. this->expected_sig_rdatas_.clear();
  1480. this->expected_rdatas_.push_back("192.0.2.1");
  1481. this->expected_rdatas_.push_back("192.0.2.2");
  1482. doFindTest(*finder, isc::dns::Name("ttldiff2.example.org."),
  1483. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1484. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1485. this->expected_sig_rdatas_);
  1486. EXPECT_THROW(finder->find(isc::dns::Name("badcname1.example.org."),
  1487. this->qtype_,
  1488. NULL, ZoneFinder::FIND_DEFAULT),
  1489. DataSourceError);
  1490. EXPECT_THROW(finder->find(isc::dns::Name("badcname2.example.org."),
  1491. this->qtype_,
  1492. NULL, ZoneFinder::FIND_DEFAULT),
  1493. DataSourceError);
  1494. EXPECT_THROW(finder->find(isc::dns::Name("badcname3.example.org."),
  1495. this->qtype_,
  1496. NULL, ZoneFinder::FIND_DEFAULT),
  1497. DataSourceError);
  1498. EXPECT_THROW(finder->find(isc::dns::Name("badrdata.example.org."),
  1499. this->qtype_,
  1500. NULL, ZoneFinder::FIND_DEFAULT),
  1501. DataSourceError);
  1502. EXPECT_THROW(finder->find(isc::dns::Name("badtype.example.org."),
  1503. this->qtype_,
  1504. NULL, ZoneFinder::FIND_DEFAULT),
  1505. DataSourceError);
  1506. EXPECT_THROW(finder->find(isc::dns::Name("badttl.example.org."),
  1507. this->qtype_,
  1508. NULL, ZoneFinder::FIND_DEFAULT),
  1509. DataSourceError);
  1510. EXPECT_THROW(finder->find(isc::dns::Name("badsig.example.org."),
  1511. this->qtype_,
  1512. NULL, ZoneFinder::FIND_DEFAULT),
  1513. DataSourceError);
  1514. // Trigger the hardcoded exceptions and see if find() has cleaned up
  1515. if (this->is_mock_) {
  1516. EXPECT_THROW(finder->find(isc::dns::Name("dsexception.in.search."),
  1517. this->qtype_,
  1518. NULL, ZoneFinder::FIND_DEFAULT),
  1519. DataSourceError);
  1520. EXPECT_THROW(finder->find(isc::dns::Name("iscexception.in.search."),
  1521. this->qtype_,
  1522. NULL, ZoneFinder::FIND_DEFAULT),
  1523. isc::Exception);
  1524. EXPECT_THROW(finder->find(isc::dns::Name("basicexception.in.search."),
  1525. this->qtype_,
  1526. NULL, ZoneFinder::FIND_DEFAULT),
  1527. std::exception);
  1528. EXPECT_THROW(finder->find(isc::dns::Name("dsexception.in.getnext."),
  1529. this->qtype_,
  1530. NULL, ZoneFinder::FIND_DEFAULT),
  1531. DataSourceError);
  1532. EXPECT_THROW(finder->find(isc::dns::Name("iscexception.in.getnext."),
  1533. this->qtype_,
  1534. NULL, ZoneFinder::FIND_DEFAULT),
  1535. isc::Exception);
  1536. EXPECT_THROW(finder->find(isc::dns::Name("basicexception.in.getnext."),
  1537. this->qtype_,
  1538. NULL, ZoneFinder::FIND_DEFAULT),
  1539. std::exception);
  1540. }
  1541. // This RRSIG has the wrong sigtype field, which should be
  1542. // an error if we decide to keep using that field
  1543. // Right now the field is ignored, so it does not error
  1544. this->expected_rdatas_.clear();
  1545. this->expected_sig_rdatas_.clear();
  1546. this->expected_rdatas_.push_back("192.0.2.1");
  1547. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1548. doFindTest(*finder, isc::dns::Name("badsigtype.example.org."),
  1549. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1550. this->expected_rdatas_, this->expected_sig_rdatas_);
  1551. }
  1552. TYPED_TEST(DatabaseClientTest, findDelegation) {
  1553. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1554. // The apex should not be considered delegation point and we can access
  1555. // data
  1556. this->expected_rdatas_.clear();
  1557. this->expected_sig_rdatas_.clear();
  1558. this->expected_rdatas_.push_back("192.0.2.1");
  1559. doFindTest(*finder, isc::dns::Name("example.org."),
  1560. this->qtype_, this->qtype_,
  1561. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1562. this->expected_sig_rdatas_);
  1563. this->expected_rdatas_.clear();
  1564. this->expected_rdatas_.push_back("ns.example.com.");
  1565. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  1566. "12345 example.org. FAKEFAKEFAKE");
  1567. doFindTest(*finder, isc::dns::Name("example.org."),
  1568. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1569. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1570. this->expected_sig_rdatas_);
  1571. // Check when we ask for something below delegation point, we get the NS
  1572. // (Both when the RRset there exists and doesn't)
  1573. this->expected_rdatas_.clear();
  1574. this->expected_sig_rdatas_.clear();
  1575. this->expected_rdatas_.push_back("ns.example.com.");
  1576. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  1577. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  1578. "12345 example.org. FAKEFAKEFAKE");
  1579. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1580. this->qtype_, isc::dns::RRType::NS(),
  1581. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1582. this->expected_sig_rdatas_,
  1583. isc::dns::Name("delegation.example.org."));
  1584. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1585. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1586. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1587. this->expected_sig_rdatas_,
  1588. isc::dns::Name("delegation.example.org."));
  1589. doFindTest(*finder, isc::dns::Name("deep.below.delegation.example.org."),
  1590. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1591. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1592. this->expected_sig_rdatas_,
  1593. isc::dns::Name("delegation.example.org."));
  1594. // Even when we check directly at the delegation point, we should get
  1595. // the NS
  1596. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1597. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1598. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1599. this->expected_sig_rdatas_);
  1600. // And when we ask direcly for the NS, we should still get delegation
  1601. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1602. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1603. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1604. this->expected_sig_rdatas_);
  1605. // Now test delegation. If it is below the delegation point, we should get
  1606. // the DNAME (the one with data under DNAME is invalid zone, but we test
  1607. // the behaviour anyway just to make sure)
  1608. this->expected_rdatas_.clear();
  1609. this->expected_rdatas_.push_back("dname.example.com.");
  1610. this->expected_sig_rdatas_.clear();
  1611. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  1612. "20000201000000 12345 example.org. "
  1613. "FAKEFAKEFAKE");
  1614. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1615. this->qtype_, isc::dns::RRType::DNAME(),
  1616. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1617. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1618. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1619. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1620. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1621. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1622. doFindTest(*finder, isc::dns::Name("really.deep.below.dname.example.org."),
  1623. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1624. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1625. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1626. // Asking direcly for DNAME should give SUCCESS
  1627. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1628. isc::dns::RRType::DNAME(), isc::dns::RRType::DNAME(),
  1629. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1630. this->expected_sig_rdatas_);
  1631. // But we don't delegate at DNAME point
  1632. this->expected_rdatas_.clear();
  1633. this->expected_rdatas_.push_back("192.0.2.1");
  1634. this->expected_sig_rdatas_.clear();
  1635. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1636. this->qtype_, this->qtype_,
  1637. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1638. this->expected_sig_rdatas_);
  1639. this->expected_rdatas_.clear();
  1640. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1641. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1642. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  1643. this->expected_sig_rdatas_);
  1644. // This is broken dname, it contains two targets
  1645. EXPECT_THROW(finder->find(isc::dns::Name("below.baddname.example.org."),
  1646. this->qtype_, NULL,
  1647. ZoneFinder::FIND_DEFAULT),
  1648. DataSourceError);
  1649. // Broken NS - it lives together with something else
  1650. EXPECT_THROW(finder->find(isc::dns::Name("brokenns1.example.org."),
  1651. this->qtype_, NULL,
  1652. ZoneFinder::FIND_DEFAULT),
  1653. DataSourceError);
  1654. EXPECT_THROW(finder->find(isc::dns::Name("brokenns2.example.org."),
  1655. this->qtype_, NULL,
  1656. ZoneFinder::FIND_DEFAULT),
  1657. DataSourceError);
  1658. }
  1659. TYPED_TEST(DatabaseClientTest, emptyDomain) {
  1660. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1661. // This domain doesn't exist, but a subdomain of it does.
  1662. // Therefore we should pretend the domain is there, but contains no RRsets
  1663. doFindTest(*finder, isc::dns::Name("b.example.org."), this->qtype_,
  1664. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1665. this->expected_rdatas_, this->expected_sig_rdatas_);
  1666. }
  1667. // Glue-OK mode. Just go through NS delegations down (but not through
  1668. // DNAME) and pretend it is not there.
  1669. TYPED_TEST(DatabaseClientTest, glueOK) {
  1670. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1671. this->expected_rdatas_.clear();
  1672. this->expected_sig_rdatas_.clear();
  1673. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1674. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1675. this->rrttl_, ZoneFinder::NXRRSET,
  1676. this->expected_rdatas_, this->expected_sig_rdatas_,
  1677. isc::dns::Name("ns.delegation.example.org."),
  1678. ZoneFinder::FIND_GLUE_OK);
  1679. doFindTest(*finder, isc::dns::Name("nothere.delegation.example.org."),
  1680. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1681. this->rrttl_, ZoneFinder::NXDOMAIN,
  1682. this->expected_rdatas_, this->expected_sig_rdatas_,
  1683. isc::dns::Name("nothere.delegation.example.org."),
  1684. ZoneFinder::FIND_GLUE_OK);
  1685. this->expected_rdatas_.push_back("192.0.2.1");
  1686. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1687. this->qtype_, this->qtype_,
  1688. this->rrttl_, ZoneFinder::SUCCESS,
  1689. this->expected_rdatas_, this->expected_sig_rdatas_,
  1690. isc::dns::Name("ns.delegation.example.org."),
  1691. ZoneFinder::FIND_GLUE_OK);
  1692. this->expected_rdatas_.clear();
  1693. this->expected_rdatas_.push_back("ns.example.com.");
  1694. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  1695. this->expected_sig_rdatas_.clear();
  1696. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 "
  1697. "20000201000000 12345 example.org. "
  1698. "FAKEFAKEFAKE");
  1699. // When we request the NS, it should be SUCCESS, not DELEGATION
  1700. // (different in GLUE_OK)
  1701. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1702. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1703. this->rrttl_, ZoneFinder::SUCCESS,
  1704. this->expected_rdatas_, this->expected_sig_rdatas_,
  1705. isc::dns::Name("delegation.example.org."),
  1706. ZoneFinder::FIND_GLUE_OK);
  1707. this->expected_rdatas_.clear();
  1708. this->expected_rdatas_.push_back("dname.example.com.");
  1709. this->expected_sig_rdatas_.clear();
  1710. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  1711. "20000201000000 12345 example.org. "
  1712. "FAKEFAKEFAKE");
  1713. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1714. this->qtype_, isc::dns::RRType::DNAME(),
  1715. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1716. this->expected_sig_rdatas_,
  1717. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  1718. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1719. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1720. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1721. this->expected_sig_rdatas_,
  1722. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  1723. }
  1724. TYPED_TEST(DatabaseClientTest, wildcard) {
  1725. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1726. // First, simple wildcard match
  1727. // Check also that the RRSIG is added from the wildcard (not modified)
  1728. this->expected_rdatas_.push_back("192.0.2.5");
  1729. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1730. "20000201000000 12345 example.org. "
  1731. "FAKEFAKEFAKE");
  1732. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1733. this->qtype_, this->qtype_, this->rrttl_,
  1734. ZoneFinder::WILDCARD, this->expected_rdatas_,
  1735. this->expected_sig_rdatas_);
  1736. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  1737. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::WILDCARD,
  1738. this->expected_rdatas_, this->expected_sig_rdatas_);
  1739. this->expected_rdatas_.clear();
  1740. this->expected_sig_rdatas_.clear();
  1741. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1742. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1743. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1744. this->expected_rdatas_, this->expected_sig_rdatas_);
  1745. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  1746. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1747. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1748. this->expected_rdatas_, this->expected_sig_rdatas_);
  1749. // Direct request for this wildcard
  1750. this->expected_rdatas_.push_back("192.0.2.5");
  1751. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1752. "20000201000000 12345 example.org. "
  1753. "FAKEFAKEFAKE");
  1754. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1755. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1756. this->expected_rdatas_, this->expected_sig_rdatas_);
  1757. this->expected_rdatas_.clear();
  1758. this->expected_sig_rdatas_.clear();
  1759. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1760. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1761. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  1762. this->expected_sig_rdatas_);
  1763. // This is nonsense, but check it doesn't match by some stupid accident
  1764. doFindTest(*finder, isc::dns::Name("a.*.wild.example.org"),
  1765. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1766. this->expected_rdatas_, this->expected_sig_rdatas_);
  1767. // These should be canceled, since it is below a domain which exitsts
  1768. doFindTest(*finder, isc::dns::Name("nothing.here.wild.example.org"),
  1769. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1770. this->expected_rdatas_, this->expected_sig_rdatas_);
  1771. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  1772. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1773. this->expected_rdatas_, this->expected_sig_rdatas_);
  1774. doFindTest(*finder,
  1775. isc::dns::Name("below.cancel.here.wild.example.org"),
  1776. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1777. this->expected_rdatas_, this->expected_sig_rdatas_);
  1778. // And this should be just plain empty non-terminal domain, check
  1779. // the wildcard doesn't hurt it
  1780. doFindTest(*finder, isc::dns::Name("here.wild.example.org"),
  1781. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1782. this->expected_rdatas_, this->expected_sig_rdatas_);
  1783. // Also make sure that the wildcard doesn't hurt the original data
  1784. // below the wildcard
  1785. this->expected_rdatas_.push_back("2001:db8::5");
  1786. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  1787. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1788. this->rrttl_, ZoneFinder::SUCCESS,
  1789. this->expected_rdatas_, this->expected_sig_rdatas_);
  1790. this->expected_rdatas_.clear();
  1791. // How wildcard go together with delegation
  1792. this->expected_rdatas_.push_back("ns.example.com.");
  1793. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  1794. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  1795. ZoneFinder::DELEGATION, this->expected_rdatas_,
  1796. this->expected_sig_rdatas_,
  1797. isc::dns::Name("delegatedwild.example.org"));
  1798. // FIXME: This doesn't look logically OK, GLUE_OK should make it transparent,
  1799. // so the match should either work or be canceled, but return NXDOMAIN
  1800. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  1801. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  1802. ZoneFinder::DELEGATION, this->expected_rdatas_,
  1803. this->expected_sig_rdatas_,
  1804. isc::dns::Name("delegatedwild.example.org"),
  1805. ZoneFinder::FIND_GLUE_OK);
  1806. this->expected_rdatas_.clear();
  1807. this->expected_rdatas_.push_back("192.0.2.5");
  1808. // These are direct matches
  1809. const char* positive_names[] = {
  1810. "wild.*.foo.example.org.",
  1811. "wild.*.foo.*.bar.example.org.",
  1812. NULL
  1813. };
  1814. for (const char** name = positive_names; *name != NULL; ++ name) {
  1815. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1816. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1817. this->expected_rdatas_,
  1818. this->expected_sig_rdatas_);
  1819. }
  1820. // These are wildcard matches against empty nonterminal asterisk
  1821. this->expected_rdatas_.clear();
  1822. const char* negative_names[] = {
  1823. "a.foo.example.org.",
  1824. "*.foo.example.org.",
  1825. "foo.example.org.",
  1826. "wild.bar.foo.example.org.",
  1827. "baz.foo.*.bar.example.org",
  1828. "baz.foo.baz.bar.example.org",
  1829. "*.foo.baz.bar.example.org",
  1830. "*.foo.*.bar.example.org",
  1831. "foo.*.bar.example.org",
  1832. "*.bar.example.org",
  1833. "bar.example.org",
  1834. NULL
  1835. };
  1836. // Unless FIND_DNSSEC is specified, this is no different from other
  1837. // NXRRSET case.
  1838. for (const char** name = negative_names; *name != NULL; ++ name) {
  1839. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1840. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1841. this->expected_rdatas_, this->expected_sig_rdatas_);
  1842. }
  1843. // With FIND_DNSSEC, it should result in WILDCARD_NXRRSET.
  1844. const char* negative_dnssec_names[] = {
  1845. "a.bar.example.org.",
  1846. "foo.baz.bar.example.org.",
  1847. "a.foo.bar.example.org.",
  1848. NULL
  1849. };
  1850. this->expected_rdatas_.clear();
  1851. this->expected_rdatas_.push_back("wild.*.foo.*.bar.example.org. NSEC");
  1852. this->expected_sig_rdatas_.clear();
  1853. for (const char** name = negative_dnssec_names; *name != NULL; ++ name) {
  1854. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1855. RRType::NSEC(), this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1856. this->expected_rdatas_, this->expected_sig_rdatas_,
  1857. Name("bao.example.org."), ZoneFinder::FIND_DNSSEC);
  1858. }
  1859. // CNAME on a wildcard. Maybe not so common, but not disallowed.
  1860. this->expected_rdatas_.clear();
  1861. this->expected_rdatas_.push_back("www.example.org.");
  1862. this->expected_sig_rdatas_.clear();
  1863. doFindTest(*finder, isc::dns::Name("a.cnamewild.example.org."),
  1864. isc::dns::RRType::TXT(), isc::dns::RRType::CNAME(),
  1865. this->rrttl_, ZoneFinder::WILDCARD_CNAME,
  1866. this->expected_rdatas_, this->expected_sig_rdatas_);
  1867. // DNAME on a wildcard. In our implementation we ignore DNAMEs on a
  1868. // wildcard, but at a higher level we say the behavior is "unspecified".
  1869. // rfc2672bis strongly discourages the mixture of DNAME and wildcard
  1870. // (with SHOULD NOT).
  1871. this->expected_rdatas_.clear();
  1872. this->expected_sig_rdatas_.clear();
  1873. doFindTest(*finder, Name("a.dnamewild.example.org."),
  1874. this->qtype_, this->qtype_, this->rrttl_,
  1875. ZoneFinder::WILDCARD_NXRRSET, this->expected_rdatas_,
  1876. this->expected_sig_rdatas_);
  1877. // Some strange things in the wild node
  1878. this->expected_rdatas_.clear();
  1879. this->expected_rdatas_.push_back("ns.example.com.");
  1880. doFindTest(*finder, isc::dns::Name("a.nswild.example.org."),
  1881. isc::dns::RRType::TXT(), isc::dns::RRType::NS(),
  1882. this->rrttl_, ZoneFinder::DELEGATION,
  1883. this->expected_rdatas_, this->expected_sig_rdatas_);
  1884. }
  1885. TYPED_TEST(DatabaseClientTest, noWildcard) {
  1886. // Tests with the NO_WILDCARD flag.
  1887. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1888. // This would match *.wild.example.org, but with NO_WILDCARD should
  1889. // result in NXDOMAIN.
  1890. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A "
  1891. "NSEC RRSIG");
  1892. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1893. "20000201000000 12345 example.org. "
  1894. "FAKEFAKEFAKE");
  1895. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1896. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1897. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1898. this->expected_sig_rdatas_, Name("*.wild.example.org."),
  1899. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1900. // Should be the same without FIND_DNSSEC (but in this case no RRsets
  1901. // will be returned)
  1902. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1903. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1904. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1905. this->empty_rdatas_, Name::ROOT_NAME(), // name is dummy
  1906. ZoneFinder::NO_WILDCARD);
  1907. // Same for wildcard empty non terminal.
  1908. this->expected_rdatas_.clear();
  1909. this->expected_rdatas_.push_back("brokenns1.example.org. A NSEC");
  1910. doFindTest(*finder, isc::dns::Name("a.bar.example.org"),
  1911. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1912. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1913. this->empty_rdatas_, Name("wild.*.foo.*.bar.example.org"),
  1914. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1915. // Search for a wildcard name with NO_WILDCARD. There should be no
  1916. // difference. This is, for example, necessary to provide non existence
  1917. // of matching wildcard for isnx.nonterminal.example.org.
  1918. this->expected_rdatas_.clear();
  1919. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  1920. doFindTest(*finder, isc::dns::Name("*.nonterminal.example.org"),
  1921. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1922. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1923. this->empty_rdatas_, Name("l.example.org"),
  1924. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1925. // On the other hand, if there's exact match for the wildcard name
  1926. // it should be found regardless of NO_WILDCARD.
  1927. this->expected_rdatas_.clear();
  1928. this->expected_rdatas_.push_back("192.0.2.5");
  1929. this->expected_sig_rdatas_.clear();
  1930. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1931. "20000201000000 12345 example.org. "
  1932. "FAKEFAKEFAKE");
  1933. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1934. this->qtype_, this->qtype_, this->rrttl_,
  1935. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1936. this->expected_sig_rdatas_, Name("*.wild.example.org"),
  1937. ZoneFinder::NO_WILDCARD);
  1938. }
  1939. TYPED_TEST(DatabaseClientTest, NXRRSET_NSEC) {
  1940. // The domain exists, but doesn't have this RRType
  1941. // So we should get its NSEC
  1942. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1943. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  1944. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1945. "20000201000000 12345 example.org. "
  1946. "FAKEFAKEFAKE");
  1947. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1948. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1949. this->rrttl_, ZoneFinder::NXRRSET,
  1950. this->expected_rdatas_, this->expected_sig_rdatas_,
  1951. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC);
  1952. }
  1953. TYPED_TEST(DatabaseClientTest, wildcardNXRRSET_NSEC) {
  1954. // The domain exists, but doesn't have this RRType
  1955. // So we should get its NSEC
  1956. //
  1957. // The user will have to query us again to get the correct
  1958. // answer (eg. prove there's not an exact match)
  1959. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1960. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A NSEC "
  1961. "RRSIG");
  1962. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1963. "20000201000000 12345 example.org. "
  1964. "FAKEFAKEFAKE");
  1965. // Note that the NSEC name should NOT be synthesized.
  1966. doFindTest(*finder, isc::dns::Name("a.wild.example.org."),
  1967. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1968. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1969. this->expected_rdatas_, this->expected_sig_rdatas_,
  1970. Name("*.wild.example.org"), ZoneFinder::FIND_DNSSEC);
  1971. }
  1972. TYPED_TEST(DatabaseClientTest, NXDOMAIN_NSEC) {
  1973. // The domain doesn't exist, so we must get the right NSEC
  1974. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1975. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  1976. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1977. "20000201000000 12345 example.org. "
  1978. "FAKEFAKEFAKE");
  1979. doFindTest(*finder, isc::dns::Name("www1.example.org."),
  1980. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1981. this->rrttl_, ZoneFinder::NXDOMAIN,
  1982. this->expected_rdatas_, this->expected_sig_rdatas_,
  1983. Name("www.example.org."), ZoneFinder::FIND_DNSSEC);
  1984. this->expected_rdatas_.clear();
  1985. this->expected_rdatas_.push_back("acnamesig1.example.org. NS A NSEC RRSIG");
  1986. // This tests it works correctly in apex (there was a bug, where a check
  1987. // for NS-alone was there and it would throw).
  1988. doFindTest(*finder, isc::dns::Name("aa.example.org."),
  1989. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1990. this->rrttl_, ZoneFinder::NXDOMAIN,
  1991. this->expected_rdatas_, this->expected_sig_rdatas_,
  1992. Name("example.org."), ZoneFinder::FIND_DNSSEC);
  1993. // Check that if the DB doesn't support it, the exception from there
  1994. // is not propagated and it only does not include the NSEC
  1995. if (!this->is_mock_) {
  1996. return; // We don't make the real DB to throw
  1997. }
  1998. EXPECT_NO_THROW(doFindTest(*finder,
  1999. isc::dns::Name("notimplnsec.example.org."),
  2000. isc::dns::RRType::TXT(),
  2001. isc::dns::RRType::NSEC(), this->rrttl_,
  2002. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2003. this->empty_rdatas_, Name::ROOT_NAME(),
  2004. ZoneFinder::FIND_DNSSEC));
  2005. }
  2006. TYPED_TEST(DatabaseClientTest, emptyNonterminalNSEC) {
  2007. // Same as NXDOMAIN_NSEC, but with empty non-terminal
  2008. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2009. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  2010. doFindTest(*finder, isc::dns::Name("nonterminal.example.org."),
  2011. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(), this->rrttl_,
  2012. ZoneFinder::NXRRSET,
  2013. this->expected_rdatas_, this->expected_sig_rdatas_,
  2014. Name("l.example.org."), ZoneFinder::FIND_DNSSEC);
  2015. // Check that if the DB doesn't support it, the exception from there
  2016. // is not propagated and it only does not include the NSEC
  2017. if (!this->is_mock_) {
  2018. return; // We don't make the real DB to throw
  2019. }
  2020. EXPECT_NO_THROW(doFindTest(*finder,
  2021. isc::dns::Name("here.wild.example.org."),
  2022. isc::dns::RRType::TXT(),
  2023. isc::dns::RRType::NSEC(),
  2024. this->rrttl_, ZoneFinder::NXRRSET,
  2025. this->empty_rdatas_, this->empty_rdatas_,
  2026. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC));
  2027. }
  2028. TYPED_TEST(DatabaseClientTest, getOrigin) {
  2029. DataSourceClient::FindResult
  2030. zone(this->client_->findZone(Name("example.org")));
  2031. ASSERT_EQ(result::SUCCESS, zone.code);
  2032. shared_ptr<DatabaseClient::Finder> finder(
  2033. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  2034. if (this->is_mock_) {
  2035. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  2036. }
  2037. EXPECT_EQ(this->zname_, finder->getOrigin());
  2038. }
  2039. TYPED_TEST(DatabaseClientTest, updaterFinder) {
  2040. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2041. ASSERT_TRUE(this->updater_);
  2042. // If this update isn't replacing the zone, the finder should work
  2043. // just like the normal find() case.
  2044. if (this->is_mock_) {
  2045. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  2046. this->updater_->getFinder());
  2047. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  2048. }
  2049. this->expected_rdatas_.clear();
  2050. this->expected_rdatas_.push_back("192.0.2.1");
  2051. doFindTest(this->updater_->getFinder(), this->qname_,
  2052. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2053. this->expected_rdatas_, this->empty_rdatas_);
  2054. // When replacing the zone, the updater's finder shouldn't see anything
  2055. // in the zone until something is added.
  2056. this->updater_.reset();
  2057. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2058. ASSERT_TRUE(this->updater_);
  2059. if (this->is_mock_) {
  2060. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  2061. this->updater_->getFinder());
  2062. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  2063. }
  2064. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2065. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2066. this->empty_rdatas_, this->empty_rdatas_);
  2067. }
  2068. TYPED_TEST(DatabaseClientTest, flushZone) {
  2069. // A simple update case: flush the entire zone
  2070. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2071. // Before update, the name exists.
  2072. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  2073. this->qtype_).code);
  2074. // start update in the replace mode. the normal finder should still
  2075. // be able to see the record, but the updater's finder shouldn't.
  2076. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2077. this->setUpdateAccessor();
  2078. EXPECT_EQ(ZoneFinder::SUCCESS,
  2079. finder->find(this->qname_, this->qtype_).code);
  2080. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  2081. this->updater_->getFinder().find(this->qname_,
  2082. this->qtype_).code);
  2083. // commit the update. now the normal finder shouldn't see it.
  2084. this->updater_->commit();
  2085. EXPECT_EQ(ZoneFinder::NXDOMAIN, finder->find(this->qname_,
  2086. this->qtype_).code);
  2087. // Check rollback wasn't accidentally performed.
  2088. EXPECT_FALSE(this->isRollbacked());
  2089. }
  2090. TYPED_TEST(DatabaseClientTest, updateCancel) {
  2091. // similar to the previous test, but destruct the updater before commit.
  2092. ZoneFinderPtr finder = this->client_->findZone(this->zname_).zone_finder;
  2093. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  2094. this->qtype_).code);
  2095. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2096. this->setUpdateAccessor();
  2097. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  2098. this->updater_->getFinder().find(this->qname_,
  2099. this->qtype_).code);
  2100. // DB should not have been rolled back yet.
  2101. EXPECT_FALSE(this->isRollbacked());
  2102. this->updater_.reset(); // destruct without commit
  2103. // reset() should have triggered rollback (although it doesn't affect
  2104. // anything to the mock accessor implementation except for the result of
  2105. // isRollbacked())
  2106. EXPECT_TRUE(this->isRollbacked(true));
  2107. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  2108. this->qtype_).code);
  2109. }
  2110. TYPED_TEST(DatabaseClientTest, exceptionFromRollback) {
  2111. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2112. this->rrset_.reset(new RRset(Name("throw.example.org"), this->qclass_,
  2113. this->qtype_, this->rrttl_));
  2114. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2115. this->rrset_->getClass(),
  2116. "192.0.2.1"));
  2117. this->updater_->addRRset(*this->rrset_);
  2118. // destruct without commit. The added name will result in an exception
  2119. // in the MockAccessor's rollback method. It shouldn't be propagated.
  2120. EXPECT_NO_THROW(this->updater_.reset());
  2121. }
  2122. TYPED_TEST(DatabaseClientTest, duplicateCommit) {
  2123. // duplicate commit. should result in exception.
  2124. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2125. this->updater_->commit();
  2126. EXPECT_THROW(this->updater_->commit(), DataSourceError);
  2127. }
  2128. TYPED_TEST(DatabaseClientTest, addRRsetToNewZone) {
  2129. // Add a single RRset to a fresh empty zone
  2130. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2131. this->updater_->addRRset(*this->rrset_);
  2132. this->expected_rdatas_.clear();
  2133. this->expected_rdatas_.push_back("192.0.2.2");
  2134. {
  2135. SCOPED_TRACE("add RRset");
  2136. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2137. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2138. this->expected_rdatas_, this->empty_rdatas_);
  2139. }
  2140. // Similar to the previous case, but with RRSIG
  2141. this->updater_.reset();
  2142. this->updater_ = this->client_->getUpdater(this->zname_, true);
  2143. this->updater_->addRRset(*this->rrset_);
  2144. this->updater_->addRRset(*this->rrsigset_);
  2145. // confirm the expected columns were passed to the accessor (if checkable).
  2146. const char* const rrsig_added[] = {
  2147. "www.example.org.", "org.example.www.", "3600", "RRSIG", "A",
  2148. "A 5 3 0 20000101000000 20000201000000 0 example.org. FAKEFAKEFAKE"
  2149. };
  2150. this->checkLastAdded(rrsig_added);
  2151. this->expected_sig_rdatas_.clear();
  2152. this->expected_sig_rdatas_.push_back(
  2153. rrsig_added[DatabaseAccessor::ADD_RDATA]);
  2154. {
  2155. SCOPED_TRACE("add RRset with RRSIG");
  2156. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2157. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2158. this->expected_rdatas_, this->expected_sig_rdatas_);
  2159. }
  2160. // Add the non RRSIG RRset again, to see the attempt of adding RRSIG
  2161. // causes any unexpected effect, in particular, whether the SIGTYPE
  2162. // field might remain.
  2163. this->updater_->addRRset(*this->rrset_);
  2164. const char* const rrset_added[] = {
  2165. "www.example.org.", "org.example.www.", "3600", "A", "", "192.0.2.2"
  2166. };
  2167. this->checkLastAdded(rrset_added);
  2168. }
  2169. TYPED_TEST(DatabaseClientTest, addRRsetToCurrentZone) {
  2170. // Similar to the previous test, but not replacing the existing data.
  2171. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2172. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2173. this->updater_->addRRset(*this->rrset_);
  2174. // We should see both old and new data.
  2175. this->expected_rdatas_.clear();
  2176. this->expected_rdatas_.push_back("192.0.2.1");
  2177. this->expected_rdatas_.push_back("192.0.2.2");
  2178. {
  2179. SCOPED_TRACE("add RRset");
  2180. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2181. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2182. this->expected_rdatas_, this->empty_rdatas_);
  2183. }
  2184. this->updater_->commit();
  2185. {
  2186. SCOPED_TRACE("add RRset after commit");
  2187. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  2188. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2189. this->empty_rdatas_);
  2190. }
  2191. }
  2192. TYPED_TEST(DatabaseClientTest, addMultipleRRs) {
  2193. // Similar to the previous case, but the added RRset contains multiple
  2194. // RRs.
  2195. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2196. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2197. this->rrset_->getClass(),
  2198. "192.0.2.3"));
  2199. this->updater_->addRRset(*this->rrset_);
  2200. this->expected_rdatas_.clear();
  2201. this->expected_rdatas_.push_back("192.0.2.1");
  2202. this->expected_rdatas_.push_back("192.0.2.2");
  2203. this->expected_rdatas_.push_back("192.0.2.3");
  2204. {
  2205. SCOPED_TRACE("add multiple RRs");
  2206. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2207. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2208. this->expected_rdatas_, this->empty_rdatas_);
  2209. }
  2210. }
  2211. TYPED_TEST(DatabaseClientTest, addRRsetOfLargerTTL) {
  2212. // Similar to the previous one, but the TTL of the added RRset is larger
  2213. // than that of the existing record. The finder should use the smaller
  2214. // one.
  2215. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2216. this->rrset_->setTTL(RRTTL(7200));
  2217. this->updater_->addRRset(*this->rrset_);
  2218. this->expected_rdatas_.clear();
  2219. this->expected_rdatas_.push_back("192.0.2.1");
  2220. this->expected_rdatas_.push_back("192.0.2.2");
  2221. {
  2222. SCOPED_TRACE("add RRset of larger TTL");
  2223. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2224. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2225. this->expected_rdatas_, this->empty_rdatas_);
  2226. }
  2227. }
  2228. TYPED_TEST(DatabaseClientTest, addRRsetOfSmallerTTL) {
  2229. // Similar to the previous one, but the added RRset has a smaller TTL.
  2230. // The added TTL should be used by the finder.
  2231. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2232. this->rrset_->setTTL(RRTTL(1800));
  2233. this->updater_->addRRset(*this->rrset_);
  2234. this->expected_rdatas_.clear();
  2235. this->expected_rdatas_.push_back("192.0.2.1");
  2236. this->expected_rdatas_.push_back("192.0.2.2");
  2237. {
  2238. SCOPED_TRACE("add RRset of smaller TTL");
  2239. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2240. this->qtype_, RRTTL(1800), ZoneFinder::SUCCESS,
  2241. this->expected_rdatas_, this->empty_rdatas_);
  2242. }
  2243. }
  2244. TYPED_TEST(DatabaseClientTest, addSameRR) {
  2245. // Add the same RR as that is already in the data source.
  2246. // Currently the add interface doesn't try to suppress the duplicate,
  2247. // neither does the finder. We may want to revisit it in future versions.
  2248. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2249. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2250. this->rrttl_));
  2251. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2252. this->rrset_->getClass(),
  2253. "192.0.2.1"));
  2254. this->updater_->addRRset(*this->rrset_);
  2255. this->expected_rdatas_.clear();
  2256. this->expected_rdatas_.push_back("192.0.2.1");
  2257. this->expected_rdatas_.push_back("192.0.2.1");
  2258. {
  2259. SCOPED_TRACE("add same RR");
  2260. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2261. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2262. this->expected_rdatas_, this->empty_rdatas_);
  2263. }
  2264. }
  2265. TYPED_TEST(DatabaseClientTest, addDeviantRR) {
  2266. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2267. // RR class mismatch. This should be detected and rejected.
  2268. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  2269. this->rrttl_));
  2270. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2271. this->rrset_->getClass(),
  2272. "test text"));
  2273. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2274. // Out-of-zone owner name. At a higher level this should be rejected,
  2275. // but it doesn't happen in this interface.
  2276. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  2277. this->qtype_, this->rrttl_));
  2278. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2279. this->rrset_->getClass(),
  2280. "192.0.2.100"));
  2281. this->updater_->addRRset(*this->rrset_);
  2282. this->expected_rdatas_.clear();
  2283. this->expected_rdatas_.push_back("192.0.2.100");
  2284. {
  2285. // Note: with the find() implementation being more strict about
  2286. // zone cuts, this test may fail. Then the test should be updated.
  2287. SCOPED_TRACE("add out-of-zone RR");
  2288. doFindTest(this->updater_->getFinder(), Name("example.com"),
  2289. this->qtype_, this->qtype_, this->rrttl_,
  2290. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2291. this->empty_rdatas_);
  2292. }
  2293. }
  2294. TYPED_TEST(DatabaseClientTest, addEmptyRRset) {
  2295. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2296. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2297. this->rrttl_));
  2298. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2299. }
  2300. TYPED_TEST(DatabaseClientTest, addAfterCommit) {
  2301. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2302. this->updater_->commit();
  2303. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2304. }
  2305. TYPED_TEST(DatabaseClientTest, addRRsetWithRRSIG) {
  2306. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2307. this->rrset_->addRRsig(*this->rrsigset_);
  2308. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  2309. }
  2310. TYPED_TEST(DatabaseClientTest, deleteRRset) {
  2311. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2312. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2313. this->rrttl_));
  2314. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2315. this->rrset_->getClass(),
  2316. "192.0.2.1"));
  2317. // Delete one RR from an RRset
  2318. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2319. this->updater_->deleteRRset(*this->rrset_);
  2320. // Delete the only RR of a name
  2321. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  2322. RRType::CNAME(), this->rrttl_));
  2323. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2324. this->rrset_->getClass(),
  2325. "www.example.org"));
  2326. this->updater_->deleteRRset(*this->rrset_);
  2327. // The this->updater_ finder should immediately see the deleted results.
  2328. {
  2329. SCOPED_TRACE("delete RRset");
  2330. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2331. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2332. this->empty_rdatas_, this->empty_rdatas_);
  2333. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  2334. this->qtype_, this->qtype_, this->rrttl_,
  2335. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2336. this->empty_rdatas_);
  2337. }
  2338. // before committing the change, the original finder should see the
  2339. // original record.
  2340. {
  2341. SCOPED_TRACE("delete RRset before commit");
  2342. this->expected_rdatas_.push_back("192.0.2.1");
  2343. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  2344. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2345. this->empty_rdatas_);
  2346. this->expected_rdatas_.clear();
  2347. this->expected_rdatas_.push_back("www.example.org.");
  2348. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  2349. RRType::CNAME(), this->rrttl_, ZoneFinder::CNAME,
  2350. this->expected_rdatas_, this->empty_rdatas_);
  2351. }
  2352. // once committed, the record should be removed from the original finder's
  2353. // view, too.
  2354. this->updater_->commit();
  2355. {
  2356. SCOPED_TRACE("delete RRset after commit");
  2357. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  2358. this->rrttl_, ZoneFinder::NXRRSET, this->empty_rdatas_,
  2359. this->empty_rdatas_);
  2360. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  2361. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  2362. this->empty_rdatas_, this->empty_rdatas_);
  2363. }
  2364. }
  2365. TYPED_TEST(DatabaseClientTest, deleteRRsetToNXDOMAIN) {
  2366. // similar to the previous case, but it removes the only record of the
  2367. // given name. a subsequent find() should result in NXDOMAIN.
  2368. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  2369. RRType::CNAME(), this->rrttl_));
  2370. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2371. this->rrset_->getClass(),
  2372. "www.example.org"));
  2373. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2374. this->updater_->deleteRRset(*this->rrset_);
  2375. {
  2376. SCOPED_TRACE("delete RRset to NXDOMAIN");
  2377. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  2378. this->qtype_, this->qtype_, this->rrttl_,
  2379. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2380. this->empty_rdatas_);
  2381. }
  2382. }
  2383. TYPED_TEST(DatabaseClientTest, deleteMultipleRRs) {
  2384. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  2385. this->rrttl_));
  2386. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2387. this->rrset_->getClass(),
  2388. "2001:db8::1"));
  2389. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2390. this->rrset_->getClass(),
  2391. "2001:db8::2"));
  2392. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2393. this->updater_->deleteRRset(*this->rrset_);
  2394. {
  2395. SCOPED_TRACE("delete multiple RRs");
  2396. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  2397. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2398. this->empty_rdatas_, this->empty_rdatas_);
  2399. }
  2400. }
  2401. TYPED_TEST(DatabaseClientTest, partialDelete) {
  2402. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  2403. this->rrttl_));
  2404. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2405. this->rrset_->getClass(),
  2406. "2001:db8::1"));
  2407. // This does not exist in the test data source:
  2408. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2409. this->rrset_->getClass(),
  2410. "2001:db8::3"));
  2411. // deleteRRset should succeed "silently", and subsequent find() should
  2412. // find the remaining RR.
  2413. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2414. this->updater_->deleteRRset(*this->rrset_);
  2415. {
  2416. SCOPED_TRACE("partial delete");
  2417. this->expected_rdatas_.push_back("2001:db8::2");
  2418. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  2419. RRType::AAAA(), this->rrttl_, ZoneFinder::SUCCESS,
  2420. this->expected_rdatas_, this->empty_rdatas_);
  2421. }
  2422. }
  2423. TYPED_TEST(DatabaseClientTest, deleteNoMatch) {
  2424. // similar to the previous test, but there's not even a match in the
  2425. // specified RRset. Essentially there's no difference in the result.
  2426. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2427. this->updater_->deleteRRset(*this->rrset_);
  2428. {
  2429. SCOPED_TRACE("delete no match");
  2430. this->expected_rdatas_.push_back("192.0.2.1");
  2431. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2432. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2433. this->expected_rdatas_, this->empty_rdatas_);
  2434. }
  2435. }
  2436. TYPED_TEST(DatabaseClientTest, deleteWithDifferentTTL) {
  2437. // Our delete interface simply ignores TTL (may change in a future version)
  2438. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2439. RRTTL(1800)));
  2440. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2441. this->rrset_->getClass(),
  2442. "192.0.2.1"));
  2443. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2444. this->updater_->deleteRRset(*this->rrset_);
  2445. {
  2446. SCOPED_TRACE("delete RRset with a different TTL");
  2447. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2448. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2449. this->empty_rdatas_, this->empty_rdatas_);
  2450. }
  2451. }
  2452. TYPED_TEST(DatabaseClientTest, deleteDeviantRR) {
  2453. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2454. // RR class mismatch. This should be detected and rejected.
  2455. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  2456. this->rrttl_));
  2457. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2458. this->rrset_->getClass(),
  2459. "test text"));
  2460. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2461. // Out-of-zone owner name. At a higher level this should be rejected,
  2462. // but it doesn't happen in this interface.
  2463. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  2464. this->qtype_, this->rrttl_));
  2465. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2466. this->rrset_->getClass(),
  2467. "192.0.2.100"));
  2468. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->rrset_));
  2469. }
  2470. TYPED_TEST(DatabaseClientTest, deleteAfterCommit) {
  2471. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2472. this->updater_->commit();
  2473. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2474. }
  2475. TYPED_TEST(DatabaseClientTest, deleteEmptyRRset) {
  2476. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2477. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2478. this->rrttl_));
  2479. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2480. }
  2481. TYPED_TEST(DatabaseClientTest, deleteRRsetWithRRSIG) {
  2482. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2483. this->rrset_->addRRsig(*this->rrsigset_);
  2484. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2485. }
  2486. TYPED_TEST(DatabaseClientTest, compoundUpdate) {
  2487. // This test case performs an arbitrary chosen add/delete operations
  2488. // in a single update transaction. Essentially there is nothing new to
  2489. // test here, but there may be some bugs that was overlooked and can
  2490. // only happen in the compound update scenario, so we test it.
  2491. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2492. // add a new RR to an existing RRset
  2493. this->updater_->addRRset(*this->rrset_);
  2494. this->expected_rdatas_.clear();
  2495. this->expected_rdatas_.push_back("192.0.2.1");
  2496. this->expected_rdatas_.push_back("192.0.2.2");
  2497. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2498. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2499. this->expected_rdatas_, this->empty_rdatas_);
  2500. // delete an existing RR
  2501. this->rrset_.reset(new RRset(Name("www.example.org"), this->qclass_,
  2502. this->qtype_, this->rrttl_));
  2503. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2504. this->rrset_->getClass(),
  2505. "192.0.2.1"));
  2506. this->updater_->deleteRRset(*this->rrset_);
  2507. this->expected_rdatas_.clear();
  2508. this->expected_rdatas_.push_back("192.0.2.2");
  2509. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2510. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2511. this->expected_rdatas_, this->empty_rdatas_);
  2512. // re-add it
  2513. this->updater_->addRRset(*this->rrset_);
  2514. this->expected_rdatas_.push_back("192.0.2.1");
  2515. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2516. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2517. this->expected_rdatas_, this->empty_rdatas_);
  2518. // add a new RR with a new name
  2519. const Name newname("newname.example.org");
  2520. const RRType newtype(RRType::AAAA());
  2521. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2522. this->rrttl_, ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2523. this->empty_rdatas_);
  2524. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  2525. this->rrttl_));
  2526. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2527. this->rrset_->getClass(),
  2528. "2001:db8::10"));
  2529. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2530. this->rrset_->getClass(),
  2531. "2001:db8::11"));
  2532. this->updater_->addRRset(*this->rrset_);
  2533. this->expected_rdatas_.clear();
  2534. this->expected_rdatas_.push_back("2001:db8::10");
  2535. this->expected_rdatas_.push_back("2001:db8::11");
  2536. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2537. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2538. this->empty_rdatas_);
  2539. // delete one RR from the previous set
  2540. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  2541. this->rrttl_));
  2542. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2543. this->rrset_->getClass(),
  2544. "2001:db8::11"));
  2545. this->updater_->deleteRRset(*this->rrset_);
  2546. this->expected_rdatas_.clear();
  2547. this->expected_rdatas_.push_back("2001:db8::10");
  2548. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2549. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2550. this->empty_rdatas_);
  2551. // Commit the changes, confirm the entire changes applied.
  2552. this->updater_->commit();
  2553. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2554. this->expected_rdatas_.clear();
  2555. this->expected_rdatas_.push_back("192.0.2.2");
  2556. this->expected_rdatas_.push_back("192.0.2.1");
  2557. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_, this->rrttl_,
  2558. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2559. this->empty_rdatas_);
  2560. this->expected_rdatas_.clear();
  2561. this->expected_rdatas_.push_back("2001:db8::10");
  2562. doFindTest(*finder, newname, newtype, newtype, this->rrttl_,
  2563. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2564. this->empty_rdatas_);
  2565. }
  2566. TYPED_TEST(DatabaseClientTest, previous) {
  2567. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2568. EXPECT_EQ(Name("www.example.org."),
  2569. finder->findPreviousName(Name("www2.example.org.")));
  2570. // Check a name that doesn't exist there
  2571. EXPECT_EQ(Name("www.example.org."),
  2572. finder->findPreviousName(Name("www1.example.org.")));
  2573. if (this->is_mock_) { // We can't really force the DB to throw
  2574. // Check it doesn't crash or anything if the underlying DB throws
  2575. DataSourceClient::FindResult
  2576. zone(this->client_->findZone(Name("bad.example.org")));
  2577. finder =
  2578. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder);
  2579. EXPECT_THROW(finder->findPreviousName(Name("bad.example.org")),
  2580. isc::NotImplemented);
  2581. } else {
  2582. // No need to test this on mock one, because we test only that
  2583. // the exception gets through
  2584. // A name before the origin
  2585. EXPECT_THROW(finder->findPreviousName(Name("example.com")),
  2586. isc::NotImplemented);
  2587. }
  2588. }
  2589. TYPED_TEST(DatabaseClientTest, invalidRdata) {
  2590. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2591. EXPECT_THROW(finder->find(Name("invalidrdata.example.org."), RRType::A()),
  2592. DataSourceError);
  2593. EXPECT_THROW(finder->find(Name("invalidrdata2.example.org."), RRType::A()),
  2594. DataSourceError);
  2595. }
  2596. TEST_F(MockDatabaseClientTest, missingNSEC) {
  2597. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2598. /*
  2599. * FIXME: For now, we can't really distinguish this bogus input
  2600. * from not-signed zone so we can't throw. But once we can,
  2601. * enable the original test.
  2602. */
  2603. #if 0
  2604. EXPECT_THROW(finder->find(Name("badnsec2.example.org."), RRType::A(), NULL,
  2605. ZoneFinder::FIND_DNSSEC),
  2606. DataSourceError);
  2607. #endif
  2608. doFindTest(*finder, Name("badnsec2.example.org."), RRType::A(),
  2609. RRType::A(), this->rrttl_, ZoneFinder::NXDOMAIN,
  2610. this->expected_rdatas_, this->expected_sig_rdatas_);
  2611. }
  2612. TEST_F(MockDatabaseClientTest, badName) {
  2613. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2614. EXPECT_THROW(finder->findPreviousName(Name("brokenname.example.org.")),
  2615. DataSourceError);
  2616. }
  2617. /*
  2618. * Test correct use of the updater with a journal.
  2619. */
  2620. TYPED_TEST(DatabaseClientTest, journal) {
  2621. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2622. this->updater_->deleteRRset(*this->soa_);
  2623. this->updater_->deleteRRset(*this->rrset_);
  2624. this->soa_.reset(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  2625. this->rrttl_));
  2626. this->soa_->addRdata(rdata::createRdata(this->soa_->getType(),
  2627. this->soa_->getClass(),
  2628. "ns1.example.org. "
  2629. "admin.example.org. "
  2630. "1235 3600 1800 2419200 7200"));
  2631. this->updater_->addRRset(*this->soa_);
  2632. this->updater_->addRRset(*this->rrset_);
  2633. ASSERT_NO_THROW(this->updater_->commit());
  2634. std::vector<JournalEntry> expected;
  2635. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2636. DatabaseAccessor::DIFF_DELETE,
  2637. "example.org.", "SOA", "3600",
  2638. "ns1.example.org. admin.example.org. "
  2639. "1234 3600 1800 2419200 7200"));
  2640. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2641. DatabaseAccessor::DIFF_DELETE,
  2642. "www.example.org.", "A", "3600",
  2643. "192.0.2.2"));
  2644. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  2645. DatabaseAccessor::DIFF_ADD,
  2646. "example.org.", "SOA", "3600",
  2647. "ns1.example.org. admin.example.org. "
  2648. "1235 3600 1800 2419200 7200"));
  2649. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1235,
  2650. DatabaseAccessor::DIFF_ADD,
  2651. "www.example.org.", "A", "3600",
  2652. "192.0.2.2"));
  2653. this->checkJournal(expected);
  2654. }
  2655. /*
  2656. * Push multiple delete-add sequences. Checks it is allowed and all is
  2657. * saved.
  2658. */
  2659. TYPED_TEST(DatabaseClientTest, journalMultiple) {
  2660. std::vector<JournalEntry> expected;
  2661. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2662. std::string soa_rdata = "ns1.example.org. admin.example.org. "
  2663. "1234 3600 1800 2419200 7200";
  2664. for (size_t i = 1; i < 100; ++ i) {
  2665. // Remove the old SOA
  2666. this->updater_->deleteRRset(*this->soa_);
  2667. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234 + i - 1,
  2668. DatabaseAccessor::DIFF_DELETE,
  2669. "example.org.", "SOA", "3600",
  2670. soa_rdata));
  2671. // Create a new SOA
  2672. soa_rdata = "ns1.example.org. admin.example.org. " +
  2673. lexical_cast<std::string>(1234 + i) + " 3600 1800 2419200 7200";
  2674. this->soa_.reset(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  2675. this->rrttl_));
  2676. this->soa_->addRdata(rdata::createRdata(this->soa_->getType(),
  2677. this->soa_->getClass(),
  2678. soa_rdata));
  2679. // Add the new SOA
  2680. this->updater_->addRRset(*this->soa_);
  2681. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234 + i,
  2682. DatabaseAccessor::DIFF_ADD,
  2683. "example.org.", "SOA", "3600",
  2684. soa_rdata));
  2685. }
  2686. ASSERT_NO_THROW(this->updater_->commit());
  2687. // Check the journal contains everything.
  2688. this->checkJournal(expected);
  2689. }
  2690. /*
  2691. * Test passing a forbidden sequence to it and expect it to throw.
  2692. *
  2693. * Note that we implicitly test in different testcases (these for add and
  2694. * delete) that if the journaling is false, it doesn't expect the order.
  2695. *
  2696. * In this test we don't check with the real databases as this case shouldn't
  2697. * contain backend specific behavior.
  2698. */
  2699. TEST_F(MockDatabaseClientTest, journalBadSequence) {
  2700. std::vector<JournalEntry> expected;
  2701. {
  2702. SCOPED_TRACE("Delete A before SOA");
  2703. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2704. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_),
  2705. isc::BadValue);
  2706. // Make sure the journal is empty now
  2707. this->checkJournal(expected);
  2708. }
  2709. {
  2710. SCOPED_TRACE("Add before delete");
  2711. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2712. EXPECT_THROW(this->updater_->addRRset(*this->soa_), isc::BadValue);
  2713. // Make sure the journal is empty now
  2714. this->checkJournal(expected);
  2715. }
  2716. {
  2717. SCOPED_TRACE("Add A before SOA");
  2718. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2719. // So far OK
  2720. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  2721. // But we miss the add SOA here
  2722. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), isc::BadValue);
  2723. // Make sure the journal contains only the first one
  2724. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2725. DatabaseAccessor::DIFF_DELETE,
  2726. "example.org.", "SOA", "3600",
  2727. "ns1.example.org. admin.example.org. "
  2728. "1234 3600 1800 2419200 7200"));
  2729. this->checkJournal(expected);
  2730. }
  2731. {
  2732. SCOPED_TRACE("Commit before add");
  2733. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2734. // So far OK
  2735. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  2736. // Commit at the wrong time
  2737. EXPECT_THROW(updater_->commit(), isc::BadValue);
  2738. current_accessor_->checkJournal(expected);
  2739. }
  2740. {
  2741. SCOPED_TRACE("Delete two SOAs");
  2742. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2743. // So far OK
  2744. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  2745. // Delete the SOA again
  2746. EXPECT_THROW(this->updater_->deleteRRset(*this->soa_), isc::BadValue);
  2747. this->checkJournal(expected);
  2748. }
  2749. {
  2750. SCOPED_TRACE("Add two SOAs");
  2751. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2752. // So far OK
  2753. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->soa_));
  2754. // Still OK
  2755. EXPECT_NO_THROW(this->updater_->addRRset(*this->soa_));
  2756. // But this one is added again
  2757. EXPECT_THROW(this->updater_->addRRset(*this->soa_), isc::BadValue);
  2758. expected.push_back(JournalEntry(WRITABLE_ZONE_ID, 1234,
  2759. DatabaseAccessor::DIFF_ADD,
  2760. "example.org.", "SOA", "3600",
  2761. "ns1.example.org. admin.example.org. "
  2762. "1234 3600 1800 2419200 7200"));
  2763. this->checkJournal(expected);
  2764. }
  2765. }
  2766. /*
  2767. * Test it rejects to store journals when we request it together with
  2768. * erasing the whole zone.
  2769. */
  2770. TYPED_TEST(DatabaseClientTest, journalOnErase) {
  2771. EXPECT_THROW(this->client_->getUpdater(this->zname_, true, true),
  2772. isc::BadValue);
  2773. }
  2774. /*
  2775. * Check that exception is propagated when the journal is not implemented.
  2776. */
  2777. TEST_F(MockDatabaseClientTest, journalNotImplemented) {
  2778. updater_ = client_->getUpdater(Name("null.example.org"), false, true);
  2779. EXPECT_THROW(updater_->deleteRRset(*soa_), isc::NotImplemented);
  2780. soa_.reset(new RRset(zname_, qclass_, RRType::SOA(), rrttl_));
  2781. soa_->addRdata(rdata::createRdata(soa_->getType(), soa_->getClass(),
  2782. "ns1.example.org. admin.example.org. "
  2783. "1234 3600 1800 2419201 7200"));
  2784. EXPECT_THROW(updater_->addRRset(*soa_), isc::NotImplemented);
  2785. }
  2786. /*
  2787. * Test that different exceptions are propagated.
  2788. */
  2789. TEST_F(MockDatabaseClientTest, journalException) {
  2790. updater_ = client_->getUpdater(Name("bad.example.org"), false, true);
  2791. EXPECT_THROW(updater_->deleteRRset(*soa_), DataSourceError);
  2792. }
  2793. //
  2794. // Tests for the ZoneJournalReader
  2795. //
  2796. // Install a simple, commonly used diff sequence: making an update from one
  2797. // SOA to another. Return the end SOA RRset for the convenience of the caller.
  2798. ConstRRsetPtr
  2799. makeSimpleDiff(DataSourceClient& client, const Name& zname,
  2800. const RRClass& rrclass, ConstRRsetPtr begin_soa)
  2801. {
  2802. ZoneUpdaterPtr updater = client.getUpdater(zname, false, true);
  2803. updater->deleteRRset(*begin_soa);
  2804. RRsetPtr soa_end(new RRset(zname, rrclass, RRType::SOA(), RRTTL(3600)));
  2805. soa_end->addRdata(rdata::createRdata(RRType::SOA(), rrclass,
  2806. "ns1.example.org. admin.example.org. "
  2807. "1235 3600 1800 2419200 7200"));
  2808. updater->addRRset(*soa_end);
  2809. updater->commit();
  2810. return (soa_end);
  2811. }
  2812. TYPED_TEST(DatabaseClientTest, journalReader) {
  2813. // Check the simple case made by makeSimpleDiff.
  2814. ConstRRsetPtr soa_end = makeSimpleDiff(*this->client_, this->zname_,
  2815. this->qclass_, this->soa_);
  2816. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
  2817. this->client_->getJournalReader(this->zname_, 1234, 1235);
  2818. EXPECT_EQ(ZoneJournalReader::SUCCESS, result.first);
  2819. ZoneJournalReaderPtr jnl_reader = result.second;
  2820. ASSERT_TRUE(jnl_reader);
  2821. ConstRRsetPtr rrset = jnl_reader->getNextDiff();
  2822. ASSERT_TRUE(rrset);
  2823. isc::testutils::rrsetCheck(this->soa_, rrset);
  2824. rrset = jnl_reader->getNextDiff();
  2825. ASSERT_TRUE(rrset);
  2826. isc::testutils::rrsetCheck(soa_end, rrset);
  2827. rrset = jnl_reader->getNextDiff();
  2828. ASSERT_FALSE(rrset);
  2829. // Once it reaches the end of the sequence, further read attempt will
  2830. // result in exception.
  2831. EXPECT_THROW(jnl_reader->getNextDiff(), isc::InvalidOperation);
  2832. }
  2833. TYPED_TEST(DatabaseClientTest, readLargeJournal) {
  2834. // Similar to journalMultiple, but check that at a higher level.
  2835. this->updater_ = this->client_->getUpdater(this->zname_, false, true);
  2836. vector<ConstRRsetPtr> expected;
  2837. for (size_t i = 0; i < 100; ++i) {
  2838. // Create the old SOA and remove it, and record it in the expected list
  2839. RRsetPtr rrset1(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  2840. this->rrttl_));
  2841. string soa_rdata = "ns1.example.org. admin.example.org. " +
  2842. lexical_cast<std::string>(1234 + i) + " 3600 1800 2419200 7200";
  2843. rrset1->addRdata(rdata::createRdata(RRType::SOA(), this->qclass_,
  2844. soa_rdata));
  2845. this->updater_->deleteRRset(*rrset1);
  2846. expected.push_back(rrset1);
  2847. // Create a new SOA, add it, and record it.
  2848. RRsetPtr rrset2(new RRset(this->zname_, this->qclass_, RRType::SOA(),
  2849. this->rrttl_));
  2850. soa_rdata = "ns1.example.org. admin.example.org. " +
  2851. lexical_cast<std::string>(1234 + i + 1) +
  2852. " 3600 1800 2419200 7200";
  2853. rrset2->addRdata(rdata::createRdata(RRType::SOA(), this->qclass_,
  2854. soa_rdata));
  2855. this->updater_->addRRset(*rrset2);
  2856. expected.push_back(rrset2);
  2857. }
  2858. this->updater_->commit();
  2859. ZoneJournalReaderPtr jnl_reader(this->client_->getJournalReader(
  2860. this->zname_, 1234, 1334).second);
  2861. ConstRRsetPtr actual;
  2862. int i = 0;
  2863. while ((actual = jnl_reader->getNextDiff()) != NULL) {
  2864. isc::testutils::rrsetCheck(expected.at(i++), actual);
  2865. }
  2866. EXPECT_EQ(expected.size(), i); // we should have eaten all expected data
  2867. }
  2868. TYPED_TEST(DatabaseClientTest, readJournalForNoRange) {
  2869. makeSimpleDiff(*this->client_, this->zname_, this->qclass_, this->soa_);
  2870. // The specified range does not exist in the diff storage. The factory
  2871. // method should result in NO_SUCH_VERSION
  2872. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
  2873. this->client_->getJournalReader(this->zname_, 1200, 1235);
  2874. EXPECT_EQ(ZoneJournalReader::NO_SUCH_VERSION, result.first);
  2875. EXPECT_FALSE(result.second);
  2876. }
  2877. TYPED_TEST(DatabaseClientTest, journalReaderForNXZone) {
  2878. pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
  2879. this->client_->getJournalReader(Name("nosuchzone"), 0, 1);
  2880. EXPECT_EQ(ZoneJournalReader::NO_SUCH_ZONE, result.first);
  2881. EXPECT_FALSE(result.second);
  2882. }
  2883. // A helper function for journalWithBadData. It installs a simple diff
  2884. // from one serial (of 'begin') to another ('begin' + 1), tweaking a specified
  2885. // field of data with some invalid value.
  2886. void
  2887. installBadDiff(MockAccessor& accessor, uint32_t begin,
  2888. DatabaseAccessor::DiffRecordParams modify_param,
  2889. const char* const data)
  2890. {
  2891. string data1[] = {"example.org.", "SOA", "3600", "ns. root. 1 1 1 1 1"};
  2892. string data2[] = {"example.org.", "SOA", "3600", "ns. root. 2 1 1 1 1"};
  2893. data1[modify_param] = data;
  2894. accessor.addRecordDiff(READONLY_ZONE_ID, begin,
  2895. DatabaseAccessor::DIFF_DELETE, data1);
  2896. accessor.addRecordDiff(READONLY_ZONE_ID, begin + 1,
  2897. DatabaseAccessor::DIFF_ADD, data2);
  2898. }
  2899. TEST_F(MockDatabaseClientTest, journalWithBadData) {
  2900. MockAccessor& mock_accessor =
  2901. dynamic_cast<MockAccessor&>(*current_accessor_);
  2902. // One of the fields from the data source is broken as an RR parameter.
  2903. // The journal reader should still be constructed, but getNextDiff()
  2904. // should result in exception.
  2905. installBadDiff(mock_accessor, 1, DatabaseAccessor::DIFF_NAME,
  2906. "example..org");
  2907. installBadDiff(mock_accessor, 3, DatabaseAccessor::DIFF_TYPE,
  2908. "bad-rrtype");
  2909. installBadDiff(mock_accessor, 5, DatabaseAccessor::DIFF_TTL,
  2910. "bad-ttl");
  2911. installBadDiff(mock_accessor, 7, DatabaseAccessor::DIFF_RDATA,
  2912. "bad rdata");
  2913. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 1, 2).
  2914. second->getNextDiff(), DataSourceError);
  2915. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 3, 4).
  2916. second->getNextDiff(), DataSourceError);
  2917. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 5, 6).
  2918. second->getNextDiff(), DataSourceError);
  2919. EXPECT_THROW(this->client_->getJournalReader(this->zname_, 7, 8).
  2920. second->getNextDiff(), DataSourceError);
  2921. }
  2922. }