database_unittest.cc 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  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 <boost/foreach.hpp>
  15. #include <gtest/gtest.h>
  16. #include <dns/name.h>
  17. #include <dns/rrttl.h>
  18. #include <dns/rrset.h>
  19. #include <exceptions/exceptions.h>
  20. #include <datasrc/database.h>
  21. #include <datasrc/zone.h>
  22. #include <datasrc/data_source.h>
  23. #include <datasrc/iterator.h>
  24. #include <datasrc/sqlite3_accessor.h>
  25. #include <testutils/dnsmessage_test.h>
  26. #include <map>
  27. using namespace isc::datasrc;
  28. using namespace std;
  29. using namespace boost;
  30. using namespace isc::dns;
  31. namespace {
  32. // Imaginary zone IDs used in the mock accessor below.
  33. const int READONLY_ZONE_ID = 42;
  34. const int WRITABLE_ZONE_ID = 4200;
  35. // Commonly used test data
  36. const char* const TEST_RECORDS[][5] = {
  37. // some plain data
  38. {"www.example.org.", "A", "3600", "", "192.0.2.1"},
  39. {"www.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  40. {"www.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  41. {"www.example.org.", "NSEC", "3600", "", "www2.example.org. A AAAA NSEC RRSIG"},
  42. {"www.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  43. {"www2.example.org.", "A", "3600", "", "192.0.2.1"},
  44. {"www2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  45. {"www2.example.org.", "A", "3600", "", "192.0.2.2"},
  46. {"cname.example.org.", "CNAME", "3600", "", "www.example.org."},
  47. // some DNSSEC-'signed' data
  48. {"signed1.example.org.", "A", "3600", "", "192.0.2.1"},
  49. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  50. {"signed1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  51. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  52. {"signed1.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  53. {"signed1.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  54. {"signedcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  55. {"signedcname1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  56. // special case might fail; sig is for cname, which isn't there (should be ignored)
  57. // (ignoring of 'normal' other type is done above by www.)
  58. {"acnamesig1.example.org.", "A", "3600", "", "192.0.2.1"},
  59. {"acnamesig1.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  60. {"acnamesig1.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  61. // let's pretend we have a database that is not careful
  62. // about the order in which it returns data
  63. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  64. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::2"},
  65. {"signed2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE"},
  66. {"signed2.example.org.", "A", "3600", "", "192.0.2.1"},
  67. {"signed2.example.org.", "RRSIG", "3600", "", "AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  68. {"signed2.example.org.", "AAAA", "3600", "", "2001:db8::1"},
  69. {"signedcname2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  70. {"signedcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  71. {"acnamesig2.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  72. {"acnamesig2.example.org.", "A", "3600", "", "192.0.2.1"},
  73. {"acnamesig2.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  74. {"acnamesig3.example.org.", "RRSIG", "3600", "", "CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  75. {"acnamesig3.example.org.", "RRSIG", "3600", "", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  76. {"acnamesig3.example.org.", "A", "3600", "", "192.0.2.1"},
  77. {"ttldiff1.example.org.", "A", "3600", "", "192.0.2.1"},
  78. {"ttldiff1.example.org.", "A", "360", "", "192.0.2.2"},
  79. {"ttldiff2.example.org.", "A", "360", "", "192.0.2.1"},
  80. {"ttldiff2.example.org.", "A", "3600", "", "192.0.2.2"},
  81. // also add some intentionally bad data
  82. {"badcname1.example.org.", "A", "3600", "", "192.0.2.1"},
  83. {"badcname1.example.org.", "CNAME", "3600", "", "www.example.org."},
  84. {"badcname2.example.org.", "CNAME", "3600", "", "www.example.org."},
  85. {"badcname2.example.org.", "A", "3600", "", "192.0.2.1"},
  86. {"badcname3.example.org.", "CNAME", "3600", "", "www.example.org."},
  87. {"badcname3.example.org.", "CNAME", "3600", "", "www.example2.org."},
  88. {"badrdata.example.org.", "A", "3600", "", "bad"},
  89. {"badtype.example.org.", "BAD_TYPE", "3600", "", "192.0.2.1"},
  90. {"badttl.example.org.", "A", "badttl", "", "192.0.2.1"},
  91. {"badsig.example.org.", "A", "badttl", "", "192.0.2.1"},
  92. {"badsig.example.org.", "RRSIG", "3600", "", "A 5 3 3600 somebaddata 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  93. {"badsigtype.example.org.", "A", "3600", "", "192.0.2.1"},
  94. {"badsigtype.example.org.", "RRSIG", "3600", "TXT", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  95. // Data for testing delegation (with NS and DNAME)
  96. {"delegation.example.org.", "NS", "3600", "", "ns.example.com."},
  97. {"delegation.example.org.", "NS", "3600", "",
  98. "ns.delegation.example.org."},
  99. {"delegation.example.org.", "DS", "3600", "", "1 RSAMD5 2 abcd"},
  100. {"delegation.example.org.", "RRSIG", "3600", "", "NS 5 3 3600 "
  101. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  102. {"ns.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  103. {"deep.below.delegation.example.org.", "A", "3600", "", "192.0.2.1"},
  104. {"dname.example.org.", "A", "3600", "", "192.0.2.1"},
  105. {"dname.example.org.", "DNAME", "3600", "", "dname.example.com."},
  106. {"dname.example.org.", "RRSIG", "3600", "",
  107. "DNAME 5 3 3600 20000101000000 20000201000000 12345 "
  108. "example.org. FAKEFAKEFAKE"},
  109. {"below.dname.example.org.", "A", "3600", "", "192.0.2.1"},
  110. // Broken NS
  111. {"brokenns1.example.org.", "A", "3600", "", "192.0.2.1"},
  112. {"brokenns1.example.org.", "NS", "3600", "", "ns.example.com."},
  113. {"brokenns2.example.org.", "NS", "3600", "", "ns.example.com."},
  114. {"brokenns2.example.org.", "A", "3600", "", "192.0.2.1"},
  115. // Now double DNAME, to test failure mode
  116. {"baddname.example.org.", "DNAME", "3600", "", "dname1.example.com."},
  117. {"baddname.example.org.", "DNAME", "3600", "", "dname2.example.com."},
  118. // Put some data into apex (including NS) so we can check our NS
  119. // doesn't break anything
  120. {"example.org.", "NS", "3600", "", "ns.example.com."},
  121. {"example.org.", "A", "3600", "", "192.0.2.1"},
  122. {"example.org.", "NSEC", "3600", "", "acnamesig1.example.org. NS A NSEC RRSIG"},
  123. {"example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 "
  124. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  125. {"example.org.", "RRSIG", "3600", "", "NS 5 3 3600 20000101000000 "
  126. "20000201000000 12345 example.org. FAKEFAKEFAKE"},
  127. // This is because of empty domain test
  128. {"a.b.example.org.", "A", "3600", "", "192.0.2.1"},
  129. // Something for wildcards
  130. {"*.wild.example.org.", "A", "3600", "", "192.0.2.5"},
  131. {"*.wild.example.org.", "RRSIG", "3600", "A", "A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  132. {"*.wild.example.org.", "NSEC", "3600", "", "cancel.here.wild.example.org. A NSEC RRSIG"},
  133. {"*.wild.example.org.", "RRSIG", "3600", "", "NSEC 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE"},
  134. {"cancel.here.wild.example.org.", "AAAA", "3600", "", "2001:db8::5"},
  135. {"delegatedwild.example.org.", "NS", "3600", "", "ns.example.com."},
  136. {"*.delegatedwild.example.org.", "A", "3600", "", "192.0.2.5"},
  137. {"wild.*.foo.example.org.", "A", "3600", "", "192.0.2.5"},
  138. {"wild.*.foo.*.bar.example.org.", "A", "3600", "", "192.0.2.5"},
  139. {"wild.*.foo.*.bar.example.org.", "NSEC", "3600", "",
  140. "brokenns1.example.org. A NSEC"},
  141. {"bao.example.org.", "NSEC", "3600", "", "wild.*.foo.*.bar.example.org. NSEC"},
  142. {"*.cnamewild.example.org.", "CNAME", "3600", "", "www.example.org."},
  143. {"*.dnamewild.example.org.", "DNAME", "3600", "", "dname.example.com."},
  144. {"*.nswild.example.org.", "NS", "3600", "", "ns.example.com."},
  145. // For NSEC empty non-terminal
  146. {"l.example.org.", "NSEC", "3600", "", "empty.nonterminal.example.org. NSEC"},
  147. {"empty.nonterminal.example.org.", "A", "3600", "", "192.0.2.1"},
  148. // Invalid rdata
  149. {"invalidrdata.example.org.", "A", "3600", "", "Bunch of nonsense"},
  150. {"invalidrdata2.example.org.", "A", "3600", "", "192.0.2.1"},
  151. {"invalidrdata2.example.org.", "RRSIG", "3600", "", "Nonsense"},
  152. {NULL, NULL, NULL, NULL, NULL},
  153. };
  154. /*
  155. * An accessor with minimum implementation, keeping the original
  156. * "NotImplemented" methods.
  157. */
  158. class NopAccessor : public DatabaseAccessor {
  159. public:
  160. NopAccessor() : database_name_("mock_database")
  161. { }
  162. virtual std::pair<bool, int> getZone(const std::string& name) const {
  163. if (name == "example.org.") {
  164. return (std::pair<bool, int>(true, READONLY_ZONE_ID));
  165. } else if (name == "null.example.org.") {
  166. return (std::pair<bool, int>(true, 13));
  167. } else if (name == "empty.example.org.") {
  168. return (std::pair<bool, int>(true, 0));
  169. } else if (name == "bad.example.org.") {
  170. return (std::pair<bool, int>(true, -1));
  171. } else {
  172. return (std::pair<bool, int>(false, 0));
  173. }
  174. }
  175. virtual shared_ptr<DatabaseAccessor> clone() {
  176. return (shared_ptr<DatabaseAccessor>()); // bogus data, but unused
  177. }
  178. virtual std::pair<bool, int> startUpdateZone(const std::string&, bool) {
  179. // return dummy value. unused anyway.
  180. return (pair<bool, int>(true, 0));
  181. }
  182. virtual void commitUpdateZone() {}
  183. virtual void rollbackUpdateZone() {}
  184. virtual void addRecordToZone(const string (&)[ADD_COLUMN_COUNT]) {}
  185. virtual void deleteRecordInZone(const string (&)[DEL_PARAM_COUNT]) {}
  186. virtual const std::string& getDBName() const {
  187. return (database_name_);
  188. }
  189. virtual IteratorContextPtr getRecords(const std::string&, int, bool)
  190. const
  191. {
  192. isc_throw(isc::NotImplemented,
  193. "This database datasource can't be iterated");
  194. }
  195. virtual IteratorContextPtr getAllRecords(int) const {
  196. isc_throw(isc::NotImplemented,
  197. "This database datasource can't be iterated");
  198. }
  199. virtual std::string findPreviousName(int, const std::string&) const {
  200. isc_throw(isc::NotImplemented,
  201. "This data source doesn't support DNSSEC");
  202. }
  203. private:
  204. const std::string database_name_;
  205. };
  206. /*
  207. * A virtual database accessor that pretends it contains single zone --
  208. * example.org.
  209. *
  210. * It has the same getZone method as NopConnection, but it provides
  211. * implementation of the optional functionality.
  212. */
  213. class MockAccessor : public NopAccessor {
  214. // Type of mock database "row"s. This is a map whose keys are the
  215. // own names. We internally sort them by the name comparison order.
  216. struct NameCompare : public binary_function<string, string, bool> {
  217. bool operator()(const string& n1, const string& n2) const {
  218. return (Name(n1).compare(Name(n2)).getOrder() < 0);
  219. }
  220. };
  221. typedef std::map<std::string,
  222. std::vector< std::vector<std::string> >,
  223. NameCompare > Domains;
  224. public:
  225. MockAccessor() : rollbacked_(false) {
  226. readonly_records_ = &readonly_records_master_;
  227. update_records_ = &update_records_master_;
  228. empty_records_ = &empty_records_master_;
  229. fillData();
  230. }
  231. virtual shared_ptr<DatabaseAccessor> clone() {
  232. shared_ptr<MockAccessor> cloned_accessor(new MockAccessor());
  233. cloned_accessor->readonly_records_ = &readonly_records_master_;
  234. cloned_accessor->update_records_ = &update_records_master_;
  235. cloned_accessor->empty_records_ = &empty_records_master_;
  236. latest_clone_ = cloned_accessor;
  237. return (cloned_accessor);
  238. }
  239. private:
  240. class MockNameIteratorContext : public IteratorContext {
  241. public:
  242. MockNameIteratorContext(const MockAccessor& mock_accessor, int zone_id,
  243. const std::string& name, bool subdomains) :
  244. searched_name_(name), cur_record_(0)
  245. {
  246. // 'hardcoded' names to trigger exceptions
  247. // On these names some exceptions are thrown, to test the robustness
  248. // of the find() method.
  249. if (searched_name_ == "dsexception.in.search.") {
  250. isc_throw(DataSourceError, "datasource exception on search");
  251. } else if (searched_name_ == "iscexception.in.search.") {
  252. isc_throw(isc::Exception, "isc exception on search");
  253. } else if (searched_name_ == "basicexception.in.search.") {
  254. throw std::exception();
  255. }
  256. cur_record_ = 0;
  257. const Domains& cur_records = mock_accessor.getMockRecords(zone_id);
  258. if (cur_records.count(name) > 0) {
  259. // we're not aiming for efficiency in this test, simply
  260. // copy the relevant vector from records
  261. cur_name = cur_records.find(name)->second;
  262. } else if (subdomains) {
  263. cur_name.clear();
  264. // Just walk everything and check if it is a subdomain.
  265. // If it is, just copy all data from there.
  266. for (Domains::const_iterator i(cur_records.begin());
  267. i != cur_records.end(); ++i) {
  268. const Name local(i->first);
  269. if (local.compare(Name(name)).getRelation() ==
  270. isc::dns::NameComparisonResult::SUBDOMAIN) {
  271. cur_name.insert(cur_name.end(), i->second.begin(),
  272. i->second.end());
  273. }
  274. }
  275. } else {
  276. cur_name.clear();
  277. }
  278. }
  279. virtual bool getNext(std::string (&columns)[COLUMN_COUNT]) {
  280. if (searched_name_ == "dsexception.in.getnext.") {
  281. isc_throw(DataSourceError, "datasource exception on getnextrecord");
  282. } else if (searched_name_ == "iscexception.in.getnext.") {
  283. isc_throw(isc::Exception, "isc exception on getnextrecord");
  284. } else if (searched_name_ == "basicexception.in.getnext.") {
  285. throw std::exception();
  286. }
  287. if (cur_record_ < cur_name.size()) {
  288. for (size_t i = 0; i < COLUMN_COUNT; ++i) {
  289. columns[i] = cur_name[cur_record_][i];
  290. }
  291. cur_record_++;
  292. return (true);
  293. } else {
  294. return (false);
  295. }
  296. }
  297. private:
  298. const std::string searched_name_;
  299. int cur_record_;
  300. std::vector< std::vector<std::string> > cur_name;
  301. };
  302. class MockIteratorContext : public IteratorContext {
  303. private:
  304. int step;
  305. public:
  306. MockIteratorContext() :
  307. step(0)
  308. { }
  309. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  310. switch (step ++) {
  311. case 0:
  312. data[DatabaseAccessor::NAME_COLUMN] = "example.org";
  313. data[DatabaseAccessor::TYPE_COLUMN] = "SOA";
  314. data[DatabaseAccessor::TTL_COLUMN] = "300";
  315. data[DatabaseAccessor::RDATA_COLUMN] = "ns1.example.org. admin.example.org. "
  316. "1234 3600 1800 2419200 7200";
  317. return (true);
  318. case 1:
  319. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  320. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  321. data[DatabaseAccessor::TTL_COLUMN] = "300";
  322. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  323. return (true);
  324. case 2:
  325. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  326. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  327. data[DatabaseAccessor::TTL_COLUMN] = "300";
  328. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.2";
  329. return (true);
  330. case 3:
  331. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  332. data[DatabaseAccessor::TYPE_COLUMN] = "AAAA";
  333. data[DatabaseAccessor::TTL_COLUMN] = "300";
  334. data[DatabaseAccessor::RDATA_COLUMN] = "2001:db8::1";
  335. return (true);
  336. case 4:
  337. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  338. data[DatabaseAccessor::TYPE_COLUMN] = "AAAA";
  339. data[DatabaseAccessor::TTL_COLUMN] = "300";
  340. data[DatabaseAccessor::RDATA_COLUMN] = "2001:db8::2";
  341. return (true);
  342. default:
  343. ADD_FAILURE() <<
  344. "Request past the end of iterator context";
  345. case 5:
  346. return (false);
  347. }
  348. }
  349. };
  350. class EmptyIteratorContext : public IteratorContext {
  351. public:
  352. virtual bool getNext(string(&)[COLUMN_COUNT]) {
  353. return (false);
  354. }
  355. };
  356. class BadIteratorContext : public IteratorContext {
  357. private:
  358. int step;
  359. public:
  360. BadIteratorContext() :
  361. step(0)
  362. { }
  363. virtual bool getNext(string (&data)[COLUMN_COUNT]) {
  364. switch (step ++) {
  365. case 0:
  366. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  367. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  368. data[DatabaseAccessor::TTL_COLUMN] = "300";
  369. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.1";
  370. return (true);
  371. case 1:
  372. data[DatabaseAccessor::NAME_COLUMN] = "x.example.org";
  373. data[DatabaseAccessor::TYPE_COLUMN] = "A";
  374. data[DatabaseAccessor::TTL_COLUMN] = "301";
  375. data[DatabaseAccessor::RDATA_COLUMN] = "192.0.2.2";
  376. return (true);
  377. default:
  378. ADD_FAILURE() <<
  379. "Request past the end of iterator context";
  380. case 2:
  381. return (false);
  382. }
  383. }
  384. };
  385. public:
  386. virtual IteratorContextPtr getAllRecords(int id) const {
  387. if (id == READONLY_ZONE_ID) {
  388. return (IteratorContextPtr(new MockIteratorContext()));
  389. } else if (id == 13) {
  390. return (IteratorContextPtr());
  391. } else if (id == 0) {
  392. return (IteratorContextPtr(new EmptyIteratorContext()));
  393. } else if (id == -1) {
  394. return (IteratorContextPtr(new BadIteratorContext()));
  395. } else {
  396. isc_throw(isc::Unexpected, "Unknown zone ID");
  397. }
  398. }
  399. virtual IteratorContextPtr getRecords(const std::string& name, int id,
  400. bool subdomains) const
  401. {
  402. if (id == READONLY_ZONE_ID || id == WRITABLE_ZONE_ID) {
  403. return (IteratorContextPtr(
  404. new MockNameIteratorContext(*this, id, name,
  405. subdomains)));
  406. } else {
  407. isc_throw(isc::Unexpected, "Unknown zone ID");
  408. }
  409. }
  410. virtual pair<bool, int> startUpdateZone(const std::string& zone_name,
  411. bool replace)
  412. {
  413. const pair<bool, int> zone_info = getZone(zone_name);
  414. if (!zone_info.first) {
  415. return (pair<bool, int>(false, 0));
  416. }
  417. // Prepare the record set for update. If replacing the existing one,
  418. // we use an empty set; otherwise we use a writable copy of the
  419. // original.
  420. if (replace) {
  421. update_records_->clear();
  422. } else {
  423. *update_records_ = *readonly_records_;
  424. }
  425. return (pair<bool, int>(true, WRITABLE_ZONE_ID));
  426. }
  427. virtual void commitUpdateZone() {
  428. *readonly_records_ = *update_records_;
  429. }
  430. virtual void rollbackUpdateZone() {
  431. // Special hook: if something with a name of "throw.example.org"
  432. // has been added, trigger an imaginary unexpected event with an
  433. // exception.
  434. if (update_records_->count("throw.example.org.") > 0) {
  435. isc_throw(DataSourceError, "unexpected failure in rollback");
  436. }
  437. rollbacked_ = true;
  438. }
  439. virtual void addRecordToZone(const string (&columns)[ADD_COLUMN_COUNT]) {
  440. // Copy the current value to cur_name. If it doesn't exist,
  441. // operator[] will create a new one.
  442. cur_name_ = (*update_records_)[columns[DatabaseAccessor::ADD_NAME]];
  443. vector<string> record_columns;
  444. record_columns.push_back(columns[DatabaseAccessor::ADD_TYPE]);
  445. record_columns.push_back(columns[DatabaseAccessor::ADD_TTL]);
  446. record_columns.push_back(columns[DatabaseAccessor::ADD_SIGTYPE]);
  447. record_columns.push_back(columns[DatabaseAccessor::ADD_RDATA]);
  448. record_columns.push_back(columns[DatabaseAccessor::ADD_NAME]);
  449. // copy back the added entry
  450. cur_name_.push_back(record_columns);
  451. (*update_records_)[columns[DatabaseAccessor::ADD_NAME]] = cur_name_;
  452. // remember this one so that test cases can check it.
  453. copy(columns, columns + DatabaseAccessor::ADD_COLUMN_COUNT,
  454. columns_lastadded_);
  455. }
  456. // Helper predicate class used in deleteRecordInZone().
  457. struct deleteMatch {
  458. deleteMatch(const string& type, const string& rdata) :
  459. type_(type), rdata_(rdata)
  460. {}
  461. bool operator()(const vector<string>& row) const {
  462. return (row[0] == type_ && row[3] == rdata_);
  463. }
  464. const string& type_;
  465. const string& rdata_;
  466. };
  467. virtual void deleteRecordInZone(const string (&params)[DEL_PARAM_COUNT]) {
  468. vector<vector<string> >& records =
  469. (*update_records_)[params[DatabaseAccessor::DEL_NAME]];
  470. records.erase(remove_if(records.begin(), records.end(),
  471. deleteMatch(
  472. params[DatabaseAccessor::DEL_TYPE],
  473. params[DatabaseAccessor::DEL_RDATA])),
  474. records.end());
  475. if (records.empty()) {
  476. (*update_records_).erase(params[DatabaseAccessor::DEL_NAME]);
  477. }
  478. }
  479. //
  480. // Helper methods to keep track of some update related activities
  481. //
  482. bool isRollbacked() const {
  483. return (rollbacked_);
  484. }
  485. const string* getLastAdded() const {
  486. return (columns_lastadded_);
  487. }
  488. // This allows the test code to get the accessor used in an update context
  489. shared_ptr<const MockAccessor> getLatestClone() const {
  490. return (latest_clone_);
  491. }
  492. virtual std::string findPreviousName(int id, const std::string& rname)
  493. const
  494. {
  495. if (id == -1) {
  496. isc_throw(isc::NotImplemented, "Test not implemented behaviour");
  497. } else if (id == READONLY_ZONE_ID) {
  498. // For some specific names we intentionally return broken or
  499. // unexpected result.
  500. if (rname == "org.example.badnsec2.") {
  501. return ("badnsec1.example.org.");
  502. } else if (rname == "org.example.brokenname.") {
  503. return ("brokenname...example.org.");
  504. } else if (rname == "org.example.notimplnsec." ||
  505. rname == "org.example.wild.here.") {
  506. isc_throw(isc::NotImplemented, "Not implemented in this test");
  507. }
  508. // For the general case, we search for the first name N in the
  509. // domains that meets N >= reverse(rname) using lower_bound.
  510. // The "previous name" is the name of the previous entry of N.
  511. // Note that Domains are internally sorted by the Name comparison
  512. // order. Due to the API requirement we are given a reversed
  513. // name (rname), so we need to reverse it again to convert it
  514. // to the original name.
  515. Domains::const_iterator it(readonly_records_->lower_bound(
  516. Name(rname).reverse().toText()));
  517. if (it == readonly_records_->begin()) {
  518. isc_throw(isc::Unexpected, "Unexpected name");
  519. }
  520. if (it == readonly_records_->end()) {
  521. return ((*readonly_records_->rbegin()).first);
  522. }
  523. return ((*(--it)).first);
  524. } else {
  525. isc_throw(isc::Unexpected, "Unknown zone ID");
  526. }
  527. }
  528. private:
  529. // The following member variables are storage and/or update work space
  530. // of the test zone. The "master"s are the real objects that contain
  531. // the data, and they are shared among all accessors cloned from
  532. // an initially created one. The pointer members allow the sharing.
  533. // "readonly" is for normal lookups. "update" is the workspace for
  534. // updates. When update starts it will be initialized either as an
  535. // empty set (when replacing the entire zone) or as a copy of the
  536. // "readonly" one. "empty" is a sentinel to produce negative results.
  537. Domains readonly_records_master_;
  538. Domains* readonly_records_;
  539. Domains update_records_master_;
  540. Domains* update_records_;
  541. const Domains empty_records_master_;
  542. const Domains* empty_records_;
  543. // used as temporary storage during the building of the fake data
  544. // used as temporary storage after searchForRecord() and during
  545. // getNextRecord() calls, as well as during the building of the
  546. // fake data
  547. std::vector< std::vector<std::string> > cur_name_;
  548. // The columns that were most recently added via addRecordToZone()
  549. string columns_lastadded_[ADD_COLUMN_COUNT];
  550. // Whether rollback operation has been performed for the database.
  551. // Not useful except for purely testing purpose.
  552. bool rollbacked_;
  553. // Remember the mock accessor that was last cloned
  554. boost::shared_ptr<MockAccessor> latest_clone_;
  555. const Domains& getMockRecords(int zone_id) const {
  556. if (zone_id == READONLY_ZONE_ID) {
  557. return (*readonly_records_);
  558. } else if (zone_id == WRITABLE_ZONE_ID) {
  559. return (*update_records_);
  560. }
  561. return (*empty_records_);
  562. }
  563. // Adds one record to the current name in the database
  564. // The actual data will not be added to 'records' until
  565. // addCurName() is called
  566. void addRecord(const std::string& type,
  567. const std::string& ttl,
  568. const std::string& sigtype,
  569. const std::string& rdata) {
  570. std::vector<std::string> columns;
  571. columns.push_back(type);
  572. columns.push_back(ttl);
  573. columns.push_back(sigtype);
  574. columns.push_back(rdata);
  575. cur_name_.push_back(columns);
  576. }
  577. // Adds all records we just built with calls to addRecords
  578. // to the actual fake database. This will clear cur_name_,
  579. // so we can immediately start adding new records.
  580. void addCurName(const std::string& name) {
  581. ASSERT_EQ(0, readonly_records_->count(name));
  582. // Append the name to all of them
  583. for (std::vector<std::vector<std::string> >::iterator
  584. i(cur_name_.begin()); i != cur_name_.end(); ++ i) {
  585. i->push_back(name);
  586. }
  587. (*readonly_records_)[name] = cur_name_;
  588. cur_name_.clear();
  589. }
  590. // Fills the database with zone data.
  591. // This method constructs a number of resource records (with addRecord),
  592. // which will all be added for one domain name to the fake database
  593. // (with addCurName). So for instance the first set of calls create
  594. // data for the name 'www.example.org', which will consist of one A RRset
  595. // of one record, and one AAAA RRset of two records.
  596. // The order in which they are added is the order in which getNextRecord()
  597. // will return them (so we can test whether find() etc. support data that
  598. // might not come in 'normal' order)
  599. // It shall immediately fail if you try to add the same name twice.
  600. void fillData() {
  601. const char* prev_name = NULL;
  602. for (int i = 0; TEST_RECORDS[i][0] != NULL; ++i) {
  603. if (prev_name != NULL &&
  604. strcmp(prev_name, TEST_RECORDS[i][0]) != 0) {
  605. addCurName(prev_name);
  606. }
  607. prev_name = TEST_RECORDS[i][0];
  608. addRecord(TEST_RECORDS[i][1], TEST_RECORDS[i][2],
  609. TEST_RECORDS[i][3], TEST_RECORDS[i][4]);
  610. }
  611. addCurName(prev_name);
  612. }
  613. };
  614. // This tests the default getRecords behaviour, throwing NotImplemented
  615. TEST(DatabaseConnectionTest, getRecords) {
  616. EXPECT_THROW(NopAccessor().getRecords(".", 1, false),
  617. isc::NotImplemented);
  618. }
  619. // This tests the default getAllRecords behaviour, throwing NotImplemented
  620. TEST(DatabaseConnectionTest, getAllRecords) {
  621. // The parameters don't matter
  622. EXPECT_THROW(NopAccessor().getAllRecords(1),
  623. isc::NotImplemented);
  624. }
  625. // This test fixture is templated so that we can share (most of) the test
  626. // cases with different types of data sources. Note that in test cases
  627. // we need to use 'this' to refer to member variables of the test class.
  628. template <typename ACCESSOR_TYPE>
  629. class DatabaseClientTest : public ::testing::Test {
  630. public:
  631. DatabaseClientTest() : zname_("example.org"), qname_("www.example.org"),
  632. qclass_(RRClass::IN()), qtype_(RRType::A()),
  633. rrttl_(3600)
  634. {
  635. createClient();
  636. // set up the commonly used finder.
  637. DataSourceClient::FindResult zone(client_->findZone(zname_));
  638. assert(zone.code == result::SUCCESS);
  639. finder_ = dynamic_pointer_cast<DatabaseClient::Finder>(
  640. zone.zone_finder);
  641. // Test IN/A RDATA to be added in update tests. Intentionally using
  642. // different data than the initial data configured in the MockAccessor.
  643. rrset_.reset(new RRset(qname_, qclass_, qtype_, rrttl_));
  644. rrset_->addRdata(rdata::createRdata(rrset_->getType(),
  645. rrset_->getClass(), "192.0.2.2"));
  646. // And its RRSIG. Also different from the configured one.
  647. rrsigset_.reset(new RRset(qname_, qclass_, RRType::RRSIG(),
  648. rrttl_));
  649. rrsigset_->addRdata(rdata::createRdata(rrsigset_->getType(),
  650. rrsigset_->getClass(),
  651. "A 5 3 0 20000101000000 "
  652. "20000201000000 0 example.org. "
  653. "FAKEFAKEFAKE"));
  654. }
  655. /*
  656. * We initialize the client from a function, so we can call it multiple
  657. * times per test.
  658. */
  659. void createClient() {
  660. current_accessor_ = new ACCESSOR_TYPE();
  661. is_mock_ = (dynamic_cast<MockAccessor*>(current_accessor_) != NULL);
  662. client_.reset(new DatabaseClient(qclass_,
  663. shared_ptr<ACCESSOR_TYPE>(
  664. current_accessor_)));
  665. }
  666. /**
  667. * Check the zone finder is a valid one and references the zone ID and
  668. * database available here.
  669. */
  670. void checkZoneFinder(const DataSourceClient::FindResult& zone) {
  671. ASSERT_NE(ZoneFinderPtr(), zone.zone_finder) << "No zone finder";
  672. shared_ptr<DatabaseClient::Finder> finder(
  673. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  674. ASSERT_NE(shared_ptr<DatabaseClient::Finder>(), finder) <<
  675. "Wrong type of finder";
  676. if (is_mock_) {
  677. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  678. }
  679. EXPECT_EQ(current_accessor_, &finder->getAccessor());
  680. }
  681. shared_ptr<DatabaseClient::Finder> getFinder() {
  682. DataSourceClient::FindResult zone(client_->findZone(zname_));
  683. EXPECT_EQ(result::SUCCESS, zone.code);
  684. shared_ptr<DatabaseClient::Finder> finder(
  685. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  686. if (is_mock_) {
  687. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  688. }
  689. return (finder);
  690. }
  691. // Helper methods for update tests
  692. bool isRollbacked(bool expected = false) const {
  693. if (is_mock_) {
  694. const MockAccessor& mock_accessor =
  695. dynamic_cast<const MockAccessor&>(*update_accessor_);
  696. return (mock_accessor.isRollbacked());
  697. } else {
  698. return (expected);
  699. }
  700. }
  701. void checkLastAdded(const char* const expected[]) const {
  702. if (is_mock_) {
  703. const MockAccessor* mock_accessor =
  704. dynamic_cast<const MockAccessor*>(current_accessor_);
  705. for (int i = 0; i < DatabaseAccessor::ADD_COLUMN_COUNT; ++i) {
  706. EXPECT_EQ(expected[i],
  707. mock_accessor->getLatestClone()->getLastAdded()[i]);
  708. }
  709. }
  710. }
  711. void setUpdateAccessor() {
  712. if (is_mock_) {
  713. const MockAccessor* mock_accessor =
  714. dynamic_cast<const MockAccessor*>(current_accessor_);
  715. update_accessor_ = mock_accessor->getLatestClone();
  716. }
  717. }
  718. // Some tests only work for MockAccessor. We remember whether our accessor
  719. // is of that type.
  720. bool is_mock_;
  721. // Will be deleted by client_, just keep the current value for comparison.
  722. ACCESSOR_TYPE* current_accessor_;
  723. shared_ptr<DatabaseClient> client_;
  724. const std::string database_name_;
  725. // The zone finder of the test zone commonly used in various tests.
  726. shared_ptr<DatabaseClient::Finder> finder_;
  727. // Some shortcut variables for commonly used test parameters
  728. const Name zname_; // the zone name stored in the test data source
  729. const Name qname_; // commonly used name to be found
  730. const RRClass qclass_; // commonly used RR class used with qname
  731. const RRType qtype_; // commonly used RR type used with qname
  732. const RRTTL rrttl_; // commonly used RR TTL
  733. RRsetPtr rrset_; // for adding/deleting an RRset
  734. RRsetPtr rrsigset_; // for adding/deleting an RRset
  735. // update related objects to be tested
  736. ZoneUpdaterPtr updater_;
  737. shared_ptr<const DatabaseAccessor> update_accessor_;
  738. // placeholders
  739. const std::vector<std::string> empty_rdatas_; // for NXRRSET/NXDOMAIN
  740. std::vector<std::string> expected_rdatas_;
  741. std::vector<std::string> expected_sig_rdatas_;
  742. };
  743. class TestSQLite3Accessor : public SQLite3Accessor {
  744. public:
  745. TestSQLite3Accessor() : SQLite3Accessor(
  746. TEST_DATA_BUILDDIR "/rwtest.sqlite3.copied", "IN")
  747. {
  748. startUpdateZone("example.org.", true);
  749. string columns[ADD_COLUMN_COUNT];
  750. for (int i = 0; TEST_RECORDS[i][0] != NULL; ++i) {
  751. columns[ADD_NAME] = TEST_RECORDS[i][0];
  752. columns[ADD_REV_NAME] = Name(columns[ADD_NAME]).reverse().toText();
  753. columns[ADD_TYPE] = TEST_RECORDS[i][1];
  754. columns[ADD_TTL] = TEST_RECORDS[i][2];
  755. columns[ADD_SIGTYPE] = TEST_RECORDS[i][3];
  756. columns[ADD_RDATA] = TEST_RECORDS[i][4];
  757. addRecordToZone(columns);
  758. }
  759. commitUpdateZone();
  760. }
  761. };
  762. // The following two lines instantiate test cases with concrete accessor
  763. // classes to be tested.
  764. // XXX: clang++ installed on our FreeBSD buildbot cannot complete compiling
  765. // this file, seemingly due to the size of the code. We'll consider more
  766. // complete workaround, but for a short term workaround we'll reduce the
  767. // number of tested accessor classes (thus reducing the amount of code
  768. // to be compiled) for this particular environment.
  769. #if defined(__clang__) && defined(__FreeBSD__)
  770. typedef ::testing::Types<MockAccessor> TestAccessorTypes;
  771. #else
  772. typedef ::testing::Types<MockAccessor, TestSQLite3Accessor> TestAccessorTypes;
  773. #endif
  774. TYPED_TEST_CASE(DatabaseClientTest, TestAccessorTypes);
  775. // In some cases the entire test fixture is for the mock accessor only.
  776. // We use the usual TEST_F for them with the corresponding specialized class
  777. // to make the code simpler.
  778. typedef DatabaseClientTest<MockAccessor> MockDatabaseClientTest;
  779. TYPED_TEST(DatabaseClientTest, zoneNotFound) {
  780. DataSourceClient::FindResult zone(
  781. this->client_->findZone(Name("example.com")));
  782. EXPECT_EQ(result::NOTFOUND, zone.code);
  783. }
  784. TYPED_TEST(DatabaseClientTest, exactZone) {
  785. DataSourceClient::FindResult zone(
  786. this->client_->findZone(Name("example.org")));
  787. EXPECT_EQ(result::SUCCESS, zone.code);
  788. this->checkZoneFinder(zone);
  789. }
  790. TYPED_TEST(DatabaseClientTest, superZone) {
  791. DataSourceClient::FindResult zone(this->client_->findZone(Name(
  792. "sub.example.org")));
  793. EXPECT_EQ(result::PARTIALMATCH, zone.code);
  794. this->checkZoneFinder(zone);
  795. }
  796. // This test doesn't depend on derived accessor class, so we use TEST().
  797. TEST(GenericDatabaseClientTest, noAccessorException) {
  798. // We need a dummy variable here; some compiler would regard it a mere
  799. // declaration instead of an instantiation and make the test fail.
  800. EXPECT_THROW(DatabaseClient dummy(RRClass::IN(),
  801. shared_ptr<DatabaseAccessor>()),
  802. isc::InvalidParameter);
  803. }
  804. // If the zone doesn't exist, exception is thrown
  805. TYPED_TEST(DatabaseClientTest, noZoneIterator) {
  806. EXPECT_THROW(this->client_->getIterator(Name("example.com")),
  807. DataSourceError);
  808. }
  809. // If the zone doesn't exist and iteration is not implemented, it still throws
  810. // the exception it doesn't exist
  811. TEST(GenericDatabaseClientTest, noZoneNotImplementedIterator) {
  812. EXPECT_THROW(DatabaseClient(RRClass::IN(),
  813. boost::shared_ptr<DatabaseAccessor>(
  814. new NopAccessor())).getIterator(
  815. Name("example.com")),
  816. DataSourceError);
  817. }
  818. TEST(GenericDatabaseClientTest, notImplementedIterator) {
  819. EXPECT_THROW(DatabaseClient(RRClass::IN(), shared_ptr<DatabaseAccessor>(
  820. new NopAccessor())).getIterator(Name("example.org")),
  821. isc::NotImplemented);
  822. }
  823. // Pretend a bug in the connection and pass NULL as the context
  824. // Should not crash, but gracefully throw. Works for the mock accessor only.
  825. TEST_F(MockDatabaseClientTest, nullIteratorContext) {
  826. EXPECT_THROW(this->client_->getIterator(Name("null.example.org")),
  827. isc::Unexpected);
  828. }
  829. // It doesn't crash or anything if the zone is completely empty.
  830. // Works for the mock accessor only.
  831. TEST_F(MockDatabaseClientTest, emptyIterator) {
  832. ZoneIteratorPtr it(this->client_->getIterator(Name("empty.example.org")));
  833. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  834. // This is past the end, it should throw
  835. EXPECT_THROW(it->getNextRRset(), isc::Unexpected);
  836. }
  837. // Iterate through a zone
  838. TYPED_TEST(DatabaseClientTest, iterator) {
  839. ZoneIteratorPtr it(this->client_->getIterator(Name("example.org")));
  840. ConstRRsetPtr rrset(it->getNextRRset());
  841. ASSERT_NE(ConstRRsetPtr(), rrset);
  842. // The rest of the checks work only for the mock accessor.
  843. if (!this->is_mock_) {
  844. return;
  845. }
  846. EXPECT_EQ(Name("example.org"), rrset->getName());
  847. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  848. EXPECT_EQ(RRType::SOA(), rrset->getType());
  849. EXPECT_EQ(RRTTL(300), rrset->getTTL());
  850. RdataIteratorPtr rit(rrset->getRdataIterator());
  851. ASSERT_FALSE(rit->isLast());
  852. rit->next();
  853. EXPECT_TRUE(rit->isLast());
  854. rrset = it->getNextRRset();
  855. ASSERT_NE(ConstRRsetPtr(), rrset);
  856. EXPECT_EQ(Name("x.example.org"), rrset->getName());
  857. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  858. EXPECT_EQ(RRType::A(), rrset->getType());
  859. EXPECT_EQ(RRTTL(300), rrset->getTTL());
  860. rit = rrset->getRdataIterator();
  861. ASSERT_FALSE(rit->isLast());
  862. EXPECT_EQ("192.0.2.1", rit->getCurrent().toText());
  863. rit->next();
  864. ASSERT_FALSE(rit->isLast());
  865. EXPECT_EQ("192.0.2.2", rit->getCurrent().toText());
  866. rit->next();
  867. EXPECT_TRUE(rit->isLast());
  868. rrset = it->getNextRRset();
  869. ASSERT_NE(ConstRRsetPtr(), rrset);
  870. EXPECT_EQ(Name("x.example.org"), rrset->getName());
  871. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  872. EXPECT_EQ(RRType::AAAA(), rrset->getType());
  873. EXPECT_EQ(RRTTL(300), rrset->getTTL());
  874. EXPECT_EQ(ConstRRsetPtr(), it->getNextRRset());
  875. rit = rrset->getRdataIterator();
  876. ASSERT_FALSE(rit->isLast());
  877. EXPECT_EQ("2001:db8::1", rit->getCurrent().toText());
  878. rit->next();
  879. ASSERT_FALSE(rit->isLast());
  880. EXPECT_EQ("2001:db8::2", rit->getCurrent().toText());
  881. rit->next();
  882. EXPECT_TRUE(rit->isLast());
  883. }
  884. // This has inconsistent TTL in the set (the rest, like nonsense in
  885. // the data is handled in rdata itself). Works for the mock accessor only.
  886. TEST_F(MockDatabaseClientTest, badIterator) {
  887. // It should not throw, but get the lowest one of them
  888. ZoneIteratorPtr it(this->client_->getIterator(Name("bad.example.org")));
  889. EXPECT_EQ(it->getNextRRset()->getTTL(), isc::dns::RRTTL(300));
  890. }
  891. // checks if the given rrset matches the
  892. // given name, class, type and rdatas
  893. void
  894. checkRRset(isc::dns::ConstRRsetPtr rrset,
  895. const isc::dns::Name& name,
  896. const isc::dns::RRClass& rrclass,
  897. const isc::dns::RRType& rrtype,
  898. const isc::dns::RRTTL& rrttl,
  899. const std::vector<std::string>& rdatas) {
  900. isc::dns::RRsetPtr expected_rrset(
  901. new isc::dns::RRset(name, rrclass, rrtype, rrttl));
  902. for (unsigned int i = 0; i < rdatas.size(); ++i) {
  903. expected_rrset->addRdata(
  904. isc::dns::rdata::createRdata(rrtype, rrclass,
  905. rdatas[i]));
  906. }
  907. isc::testutils::rrsetCheck(expected_rrset, rrset);
  908. }
  909. void
  910. doFindTest(ZoneFinder& finder,
  911. const isc::dns::Name& name,
  912. const isc::dns::RRType& type,
  913. const isc::dns::RRType& expected_type,
  914. const isc::dns::RRTTL expected_ttl,
  915. ZoneFinder::Result expected_result,
  916. const std::vector<std::string>& expected_rdatas,
  917. const std::vector<std::string>& expected_sig_rdatas,
  918. const isc::dns::Name& expected_name = isc::dns::Name::ROOT_NAME(),
  919. const ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT)
  920. {
  921. SCOPED_TRACE("doFindTest " + name.toText() + " " + type.toText());
  922. const ZoneFinder::FindResult result = finder.find(name, type, NULL,
  923. options);
  924. ASSERT_EQ(expected_result, result.code) << name << " " << type;
  925. if (!expected_rdatas.empty() && result.rrset) {
  926. checkRRset(result.rrset, expected_name != Name(".") ? expected_name :
  927. name, finder.getClass(), expected_type, expected_ttl,
  928. expected_rdatas);
  929. if (!expected_sig_rdatas.empty() && result.rrset->getRRsig()) {
  930. checkRRset(result.rrset->getRRsig(), expected_name != Name(".") ?
  931. expected_name : name, finder.getClass(),
  932. isc::dns::RRType::RRSIG(), expected_ttl,
  933. expected_sig_rdatas);
  934. } else if (expected_sig_rdatas.empty()) {
  935. EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset->getRRsig());
  936. } else {
  937. ADD_FAILURE() << "Missing RRSIG";
  938. }
  939. } else if (expected_rdatas.empty()) {
  940. EXPECT_EQ(isc::dns::RRsetPtr(), result.rrset);
  941. } else {
  942. ADD_FAILURE() << "Missing result";
  943. }
  944. }
  945. TYPED_TEST(DatabaseClientTest, find) {
  946. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  947. this->expected_rdatas_.clear();
  948. this->expected_sig_rdatas_.clear();
  949. this->expected_rdatas_.push_back("192.0.2.1");
  950. doFindTest(*finder, isc::dns::Name("www.example.org."),
  951. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  952. this->expected_rdatas_, this->expected_sig_rdatas_);
  953. this->expected_rdatas_.clear();
  954. this->expected_sig_rdatas_.clear();
  955. this->expected_rdatas_.push_back("192.0.2.1");
  956. this->expected_rdatas_.push_back("192.0.2.2");
  957. doFindTest(*finder, isc::dns::Name("www2.example.org."),
  958. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  959. this->expected_rdatas_, this->expected_sig_rdatas_);
  960. this->expected_rdatas_.clear();
  961. this->expected_sig_rdatas_.clear();
  962. this->expected_rdatas_.push_back("2001:db8::1");
  963. this->expected_rdatas_.push_back("2001:db8::2");
  964. doFindTest(*finder, isc::dns::Name("www.example.org."),
  965. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  966. this->rrttl_,
  967. ZoneFinder::SUCCESS,
  968. this->expected_rdatas_, this->expected_sig_rdatas_);
  969. this->expected_rdatas_.clear();
  970. this->expected_sig_rdatas_.clear();
  971. doFindTest(*finder, isc::dns::Name("www.example.org."),
  972. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(),
  973. this->rrttl_,
  974. ZoneFinder::NXRRSET,
  975. this->expected_rdatas_, this->expected_sig_rdatas_);
  976. this->expected_rdatas_.clear();
  977. this->expected_sig_rdatas_.clear();
  978. this->expected_rdatas_.push_back("www.example.org.");
  979. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  980. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  981. ZoneFinder::CNAME, this->expected_rdatas_,
  982. this->expected_sig_rdatas_);
  983. this->expected_rdatas_.clear();
  984. this->expected_sig_rdatas_.clear();
  985. this->expected_rdatas_.push_back("www.example.org.");
  986. doFindTest(*finder, isc::dns::Name("cname.example.org."),
  987. isc::dns::RRType::CNAME(), isc::dns::RRType::CNAME(),
  988. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  989. this->expected_sig_rdatas_);
  990. this->expected_rdatas_.clear();
  991. this->expected_sig_rdatas_.clear();
  992. doFindTest(*finder, isc::dns::Name("doesnotexist.example.org."),
  993. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  994. this->expected_rdatas_, this->expected_sig_rdatas_);
  995. this->expected_rdatas_.clear();
  996. this->expected_sig_rdatas_.clear();
  997. this->expected_rdatas_.push_back("192.0.2.1");
  998. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  999. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1000. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1001. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1002. this->expected_rdatas_, this->expected_sig_rdatas_);
  1003. this->expected_rdatas_.clear();
  1004. this->expected_sig_rdatas_.clear();
  1005. this->expected_rdatas_.push_back("2001:db8::1");
  1006. this->expected_rdatas_.push_back("2001:db8::2");
  1007. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1008. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1009. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1010. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1011. this->expected_sig_rdatas_);
  1012. this->expected_rdatas_.clear();
  1013. this->expected_sig_rdatas_.clear();
  1014. doFindTest(*finder, isc::dns::Name("signed1.example.org."),
  1015. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1016. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1017. this->expected_sig_rdatas_);
  1018. this->expected_rdatas_.clear();
  1019. this->expected_sig_rdatas_.clear();
  1020. this->expected_rdatas_.push_back("www.example.org.");
  1021. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1022. doFindTest(*finder, isc::dns::Name("signedcname1.example.org."),
  1023. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1024. ZoneFinder::CNAME, this->expected_rdatas_,
  1025. this->expected_sig_rdatas_);
  1026. this->expected_rdatas_.clear();
  1027. this->expected_sig_rdatas_.clear();
  1028. this->expected_rdatas_.push_back("192.0.2.1");
  1029. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1030. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12346 example.org. FAKEFAKEFAKE");
  1031. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1032. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1033. this->expected_rdatas_, this->expected_sig_rdatas_);
  1034. this->expected_rdatas_.clear();
  1035. this->expected_sig_rdatas_.clear();
  1036. this->expected_rdatas_.push_back("2001:db8::2");
  1037. this->expected_rdatas_.push_back("2001:db8::1");
  1038. this->expected_sig_rdatas_.push_back("AAAA 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1039. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1040. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1041. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1042. this->expected_sig_rdatas_);
  1043. this->expected_rdatas_.clear();
  1044. this->expected_sig_rdatas_.clear();
  1045. doFindTest(*finder, isc::dns::Name("signed2.example.org."),
  1046. isc::dns::RRType::TXT(), isc::dns::RRType::TXT(), this->rrttl_,
  1047. ZoneFinder::NXRRSET, this->expected_rdatas_,
  1048. this->expected_sig_rdatas_);
  1049. this->expected_rdatas_.clear();
  1050. this->expected_sig_rdatas_.clear();
  1051. this->expected_rdatas_.push_back("www.example.org.");
  1052. this->expected_sig_rdatas_.push_back("CNAME 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1053. doFindTest(*finder, isc::dns::Name("signedcname2.example.org."),
  1054. this->qtype_, isc::dns::RRType::CNAME(), this->rrttl_,
  1055. ZoneFinder::CNAME, this->expected_rdatas_,
  1056. this->expected_sig_rdatas_);
  1057. this->expected_rdatas_.clear();
  1058. this->expected_sig_rdatas_.clear();
  1059. this->expected_rdatas_.push_back("192.0.2.1");
  1060. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1061. doFindTest(*finder, isc::dns::Name("acnamesig1.example.org."),
  1062. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1063. this->expected_rdatas_, this->expected_sig_rdatas_);
  1064. this->expected_rdatas_.clear();
  1065. this->expected_sig_rdatas_.clear();
  1066. this->expected_rdatas_.push_back("192.0.2.1");
  1067. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1068. doFindTest(*finder, isc::dns::Name("acnamesig2.example.org."),
  1069. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1070. this->expected_rdatas_, this->expected_sig_rdatas_);
  1071. this->expected_rdatas_.clear();
  1072. this->expected_sig_rdatas_.clear();
  1073. this->expected_rdatas_.push_back("192.0.2.1");
  1074. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1075. doFindTest(*finder, isc::dns::Name("acnamesig3.example.org."),
  1076. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1077. this->expected_rdatas_, this->expected_sig_rdatas_);
  1078. this->expected_rdatas_.clear();
  1079. this->expected_sig_rdatas_.clear();
  1080. this->expected_rdatas_.push_back("192.0.2.1");
  1081. this->expected_rdatas_.push_back("192.0.2.2");
  1082. doFindTest(*finder, isc::dns::Name("ttldiff1.example.org."),
  1083. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1084. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1085. this->expected_sig_rdatas_);
  1086. this->expected_rdatas_.clear();
  1087. this->expected_sig_rdatas_.clear();
  1088. this->expected_rdatas_.push_back("192.0.2.1");
  1089. this->expected_rdatas_.push_back("192.0.2.2");
  1090. doFindTest(*finder, isc::dns::Name("ttldiff2.example.org."),
  1091. this->qtype_, this->qtype_, isc::dns::RRTTL(360),
  1092. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1093. this->expected_sig_rdatas_);
  1094. EXPECT_THROW(finder->find(isc::dns::Name("badcname1.example.org."),
  1095. this->qtype_,
  1096. NULL, ZoneFinder::FIND_DEFAULT),
  1097. DataSourceError);
  1098. EXPECT_THROW(finder->find(isc::dns::Name("badcname2.example.org."),
  1099. this->qtype_,
  1100. NULL, ZoneFinder::FIND_DEFAULT),
  1101. DataSourceError);
  1102. EXPECT_THROW(finder->find(isc::dns::Name("badcname3.example.org."),
  1103. this->qtype_,
  1104. NULL, ZoneFinder::FIND_DEFAULT),
  1105. DataSourceError);
  1106. EXPECT_THROW(finder->find(isc::dns::Name("badrdata.example.org."),
  1107. this->qtype_,
  1108. NULL, ZoneFinder::FIND_DEFAULT),
  1109. DataSourceError);
  1110. EXPECT_THROW(finder->find(isc::dns::Name("badtype.example.org."),
  1111. this->qtype_,
  1112. NULL, ZoneFinder::FIND_DEFAULT),
  1113. DataSourceError);
  1114. EXPECT_THROW(finder->find(isc::dns::Name("badttl.example.org."),
  1115. this->qtype_,
  1116. NULL, ZoneFinder::FIND_DEFAULT),
  1117. DataSourceError);
  1118. EXPECT_THROW(finder->find(isc::dns::Name("badsig.example.org."),
  1119. this->qtype_,
  1120. NULL, ZoneFinder::FIND_DEFAULT),
  1121. DataSourceError);
  1122. // Trigger the hardcoded exceptions and see if find() has cleaned up
  1123. if (this->is_mock_) {
  1124. EXPECT_THROW(finder->find(isc::dns::Name("dsexception.in.search."),
  1125. this->qtype_,
  1126. NULL, ZoneFinder::FIND_DEFAULT),
  1127. DataSourceError);
  1128. EXPECT_THROW(finder->find(isc::dns::Name("iscexception.in.search."),
  1129. this->qtype_,
  1130. NULL, ZoneFinder::FIND_DEFAULT),
  1131. isc::Exception);
  1132. EXPECT_THROW(finder->find(isc::dns::Name("basicexception.in.search."),
  1133. this->qtype_,
  1134. NULL, ZoneFinder::FIND_DEFAULT),
  1135. std::exception);
  1136. EXPECT_THROW(finder->find(isc::dns::Name("dsexception.in.getnext."),
  1137. this->qtype_,
  1138. NULL, ZoneFinder::FIND_DEFAULT),
  1139. DataSourceError);
  1140. EXPECT_THROW(finder->find(isc::dns::Name("iscexception.in.getnext."),
  1141. this->qtype_,
  1142. NULL, ZoneFinder::FIND_DEFAULT),
  1143. isc::Exception);
  1144. EXPECT_THROW(finder->find(isc::dns::Name("basicexception.in.getnext."),
  1145. this->qtype_,
  1146. NULL, ZoneFinder::FIND_DEFAULT),
  1147. std::exception);
  1148. }
  1149. // This RRSIG has the wrong sigtype field, which should be
  1150. // an error if we decide to keep using that field
  1151. // Right now the field is ignored, so it does not error
  1152. this->expected_rdatas_.clear();
  1153. this->expected_sig_rdatas_.clear();
  1154. this->expected_rdatas_.push_back("192.0.2.1");
  1155. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE");
  1156. doFindTest(*finder, isc::dns::Name("badsigtype.example.org."),
  1157. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1158. this->expected_rdatas_, this->expected_sig_rdatas_);
  1159. }
  1160. TYPED_TEST(DatabaseClientTest, findDelegation) {
  1161. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1162. // The apex should not be considered delegation point and we can access
  1163. // data
  1164. this->expected_rdatas_.clear();
  1165. this->expected_sig_rdatas_.clear();
  1166. this->expected_rdatas_.push_back("192.0.2.1");
  1167. doFindTest(*finder, isc::dns::Name("example.org."),
  1168. this->qtype_, this->qtype_,
  1169. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1170. this->expected_sig_rdatas_);
  1171. this->expected_rdatas_.clear();
  1172. this->expected_rdatas_.push_back("ns.example.com.");
  1173. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  1174. "12345 example.org. FAKEFAKEFAKE");
  1175. doFindTest(*finder, isc::dns::Name("example.org."),
  1176. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1177. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1178. this->expected_sig_rdatas_);
  1179. // Check when we ask for something below delegation point, we get the NS
  1180. // (Both when the RRset there exists and doesn't)
  1181. this->expected_rdatas_.clear();
  1182. this->expected_sig_rdatas_.clear();
  1183. this->expected_rdatas_.push_back("ns.example.com.");
  1184. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  1185. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 20000201000000 "
  1186. "12345 example.org. FAKEFAKEFAKE");
  1187. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1188. this->qtype_, isc::dns::RRType::NS(),
  1189. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1190. this->expected_sig_rdatas_,
  1191. isc::dns::Name("delegation.example.org."));
  1192. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1193. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1194. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1195. this->expected_sig_rdatas_,
  1196. isc::dns::Name("delegation.example.org."));
  1197. doFindTest(*finder, isc::dns::Name("deep.below.delegation.example.org."),
  1198. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1199. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1200. this->expected_sig_rdatas_,
  1201. isc::dns::Name("delegation.example.org."));
  1202. // Even when we check directly at the delegation point, we should get
  1203. // the NS
  1204. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1205. isc::dns::RRType::AAAA(), isc::dns::RRType::NS(),
  1206. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1207. this->expected_sig_rdatas_);
  1208. // And when we ask direcly for the NS, we should still get delegation
  1209. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1210. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1211. this->rrttl_, ZoneFinder::DELEGATION, this->expected_rdatas_,
  1212. this->expected_sig_rdatas_);
  1213. // Now test delegation. If it is below the delegation point, we should get
  1214. // the DNAME (the one with data under DNAME is invalid zone, but we test
  1215. // the behaviour anyway just to make sure)
  1216. this->expected_rdatas_.clear();
  1217. this->expected_rdatas_.push_back("dname.example.com.");
  1218. this->expected_sig_rdatas_.clear();
  1219. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  1220. "20000201000000 12345 example.org. "
  1221. "FAKEFAKEFAKE");
  1222. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1223. this->qtype_, isc::dns::RRType::DNAME(),
  1224. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1225. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1226. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1227. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1228. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1229. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1230. doFindTest(*finder, isc::dns::Name("really.deep.below.dname.example.org."),
  1231. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1232. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1233. this->expected_sig_rdatas_, isc::dns::Name("dname.example.org."));
  1234. // Asking direcly for DNAME should give SUCCESS
  1235. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1236. isc::dns::RRType::DNAME(), isc::dns::RRType::DNAME(),
  1237. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1238. this->expected_sig_rdatas_);
  1239. // But we don't delegate at DNAME point
  1240. this->expected_rdatas_.clear();
  1241. this->expected_rdatas_.push_back("192.0.2.1");
  1242. this->expected_sig_rdatas_.clear();
  1243. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1244. this->qtype_, this->qtype_,
  1245. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1246. this->expected_sig_rdatas_);
  1247. this->expected_rdatas_.clear();
  1248. doFindTest(*finder, isc::dns::Name("dname.example.org."),
  1249. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1250. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  1251. this->expected_sig_rdatas_);
  1252. // This is broken dname, it contains two targets
  1253. EXPECT_THROW(finder->find(isc::dns::Name("below.baddname.example.org."),
  1254. this->qtype_, NULL,
  1255. ZoneFinder::FIND_DEFAULT),
  1256. DataSourceError);
  1257. // Broken NS - it lives together with something else
  1258. EXPECT_THROW(finder->find(isc::dns::Name("brokenns1.example.org."),
  1259. this->qtype_, NULL,
  1260. ZoneFinder::FIND_DEFAULT),
  1261. DataSourceError);
  1262. EXPECT_THROW(finder->find(isc::dns::Name("brokenns2.example.org."),
  1263. this->qtype_, NULL,
  1264. ZoneFinder::FIND_DEFAULT),
  1265. DataSourceError);
  1266. }
  1267. TYPED_TEST(DatabaseClientTest, emptyDomain) {
  1268. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1269. // This domain doesn't exist, but a subdomain of it does.
  1270. // Therefore we should pretend the domain is there, but contains no RRsets
  1271. doFindTest(*finder, isc::dns::Name("b.example.org."), this->qtype_,
  1272. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1273. this->expected_rdatas_, this->expected_sig_rdatas_);
  1274. }
  1275. // Glue-OK mode. Just go through NS delegations down (but not through
  1276. // DNAME) and pretend it is not there.
  1277. TYPED_TEST(DatabaseClientTest, glueOK) {
  1278. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1279. this->expected_rdatas_.clear();
  1280. this->expected_sig_rdatas_.clear();
  1281. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1282. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1283. this->rrttl_, ZoneFinder::NXRRSET,
  1284. this->expected_rdatas_, this->expected_sig_rdatas_,
  1285. isc::dns::Name("ns.delegation.example.org."),
  1286. ZoneFinder::FIND_GLUE_OK);
  1287. doFindTest(*finder, isc::dns::Name("nothere.delegation.example.org."),
  1288. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1289. this->rrttl_, ZoneFinder::NXDOMAIN,
  1290. this->expected_rdatas_, this->expected_sig_rdatas_,
  1291. isc::dns::Name("nothere.delegation.example.org."),
  1292. ZoneFinder::FIND_GLUE_OK);
  1293. this->expected_rdatas_.push_back("192.0.2.1");
  1294. doFindTest(*finder, isc::dns::Name("ns.delegation.example.org."),
  1295. this->qtype_, this->qtype_,
  1296. this->rrttl_, ZoneFinder::SUCCESS,
  1297. this->expected_rdatas_, this->expected_sig_rdatas_,
  1298. isc::dns::Name("ns.delegation.example.org."),
  1299. ZoneFinder::FIND_GLUE_OK);
  1300. this->expected_rdatas_.clear();
  1301. this->expected_rdatas_.push_back("ns.example.com.");
  1302. this->expected_rdatas_.push_back("ns.delegation.example.org.");
  1303. this->expected_sig_rdatas_.clear();
  1304. this->expected_sig_rdatas_.push_back("NS 5 3 3600 20000101000000 "
  1305. "20000201000000 12345 example.org. "
  1306. "FAKEFAKEFAKE");
  1307. // When we request the NS, it should be SUCCESS, not DELEGATION
  1308. // (different in GLUE_OK)
  1309. doFindTest(*finder, isc::dns::Name("delegation.example.org."),
  1310. isc::dns::RRType::NS(), isc::dns::RRType::NS(),
  1311. this->rrttl_, ZoneFinder::SUCCESS,
  1312. this->expected_rdatas_, this->expected_sig_rdatas_,
  1313. isc::dns::Name("delegation.example.org."),
  1314. ZoneFinder::FIND_GLUE_OK);
  1315. this->expected_rdatas_.clear();
  1316. this->expected_rdatas_.push_back("dname.example.com.");
  1317. this->expected_sig_rdatas_.clear();
  1318. this->expected_sig_rdatas_.push_back("DNAME 5 3 3600 20000101000000 "
  1319. "20000201000000 12345 example.org. "
  1320. "FAKEFAKEFAKE");
  1321. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1322. this->qtype_, isc::dns::RRType::DNAME(),
  1323. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1324. this->expected_sig_rdatas_,
  1325. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  1326. doFindTest(*finder, isc::dns::Name("below.dname.example.org."),
  1327. isc::dns::RRType::AAAA(), isc::dns::RRType::DNAME(),
  1328. this->rrttl_, ZoneFinder::DNAME, this->expected_rdatas_,
  1329. this->expected_sig_rdatas_,
  1330. isc::dns::Name("dname.example.org."), ZoneFinder::FIND_GLUE_OK);
  1331. }
  1332. TYPED_TEST(DatabaseClientTest, wildcard) {
  1333. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1334. // First, simple wildcard match
  1335. // Check also that the RRSIG is added from the wildcard (not modified)
  1336. this->expected_rdatas_.push_back("192.0.2.5");
  1337. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1338. "20000201000000 12345 example.org. "
  1339. "FAKEFAKEFAKE");
  1340. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1341. this->qtype_, this->qtype_, this->rrttl_,
  1342. ZoneFinder::WILDCARD, this->expected_rdatas_,
  1343. this->expected_sig_rdatas_);
  1344. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  1345. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::WILDCARD,
  1346. this->expected_rdatas_, this->expected_sig_rdatas_);
  1347. this->expected_rdatas_.clear();
  1348. this->expected_sig_rdatas_.clear();
  1349. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1350. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1351. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1352. this->expected_rdatas_, this->expected_sig_rdatas_);
  1353. doFindTest(*finder, isc::dns::Name("b.a.wild.example.org"),
  1354. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1355. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1356. this->expected_rdatas_, this->expected_sig_rdatas_);
  1357. // Direct request for this wildcard
  1358. this->expected_rdatas_.push_back("192.0.2.5");
  1359. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1360. "20000201000000 12345 example.org. "
  1361. "FAKEFAKEFAKE");
  1362. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1363. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1364. this->expected_rdatas_, this->expected_sig_rdatas_);
  1365. this->expected_rdatas_.clear();
  1366. this->expected_sig_rdatas_.clear();
  1367. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1368. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1369. this->rrttl_, ZoneFinder::NXRRSET, this->expected_rdatas_,
  1370. this->expected_sig_rdatas_);
  1371. // This is nonsense, but check it doesn't match by some stupid accident
  1372. doFindTest(*finder, isc::dns::Name("a.*.wild.example.org"),
  1373. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1374. this->expected_rdatas_, this->expected_sig_rdatas_);
  1375. // These should be canceled, since it is below a domain which exitsts
  1376. doFindTest(*finder, isc::dns::Name("nothing.here.wild.example.org"),
  1377. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1378. this->expected_rdatas_, this->expected_sig_rdatas_);
  1379. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  1380. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1381. this->expected_rdatas_, this->expected_sig_rdatas_);
  1382. doFindTest(*finder,
  1383. isc::dns::Name("below.cancel.here.wild.example.org"),
  1384. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1385. this->expected_rdatas_, this->expected_sig_rdatas_);
  1386. // And this should be just plain empty non-terminal domain, check
  1387. // the wildcard doesn't hurt it
  1388. doFindTest(*finder, isc::dns::Name("here.wild.example.org"),
  1389. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1390. this->expected_rdatas_, this->expected_sig_rdatas_);
  1391. // Also make sure that the wildcard doesn't hurt the original data
  1392. // below the wildcard
  1393. this->expected_rdatas_.push_back("2001:db8::5");
  1394. doFindTest(*finder, isc::dns::Name("cancel.here.wild.example.org"),
  1395. isc::dns::RRType::AAAA(), isc::dns::RRType::AAAA(),
  1396. this->rrttl_, ZoneFinder::SUCCESS,
  1397. this->expected_rdatas_, this->expected_sig_rdatas_);
  1398. this->expected_rdatas_.clear();
  1399. // How wildcard go together with delegation
  1400. this->expected_rdatas_.push_back("ns.example.com.");
  1401. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  1402. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  1403. ZoneFinder::DELEGATION, this->expected_rdatas_,
  1404. this->expected_sig_rdatas_,
  1405. isc::dns::Name("delegatedwild.example.org"));
  1406. // FIXME: This doesn't look logically OK, GLUE_OK should make it transparent,
  1407. // so the match should either work or be canceled, but return NXDOMAIN
  1408. doFindTest(*finder, isc::dns::Name("below.delegatedwild.example.org"),
  1409. this->qtype_, isc::dns::RRType::NS(), this->rrttl_,
  1410. ZoneFinder::DELEGATION, this->expected_rdatas_,
  1411. this->expected_sig_rdatas_,
  1412. isc::dns::Name("delegatedwild.example.org"),
  1413. ZoneFinder::FIND_GLUE_OK);
  1414. this->expected_rdatas_.clear();
  1415. this->expected_rdatas_.push_back("192.0.2.5");
  1416. // These are direct matches
  1417. const char* positive_names[] = {
  1418. "wild.*.foo.example.org.",
  1419. "wild.*.foo.*.bar.example.org.",
  1420. NULL
  1421. };
  1422. for (const char** name(positive_names); *name != NULL; ++ name) {
  1423. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1424. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1425. this->expected_rdatas_,
  1426. this->expected_sig_rdatas_);
  1427. }
  1428. // These are wildcard matches against empty nonterminal asterisk
  1429. this->expected_rdatas_.clear();
  1430. const char* negative_names[] = {
  1431. "a.foo.example.org.",
  1432. "*.foo.example.org.",
  1433. "foo.example.org.",
  1434. "wild.bar.foo.example.org.",
  1435. "baz.foo.*.bar.example.org",
  1436. "baz.foo.baz.bar.example.org",
  1437. "*.foo.baz.bar.example.org",
  1438. "*.foo.*.bar.example.org",
  1439. "foo.*.bar.example.org",
  1440. "*.bar.example.org",
  1441. "bar.example.org",
  1442. NULL
  1443. };
  1444. // Unless FIND_DNSSEC is specified, this is no different from other
  1445. // NXRRSET case.
  1446. for (const char** name(negative_names); *name != NULL; ++ name) {
  1447. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1448. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1449. this->expected_rdatas_, this->expected_sig_rdatas_);
  1450. }
  1451. // With FIND_DNSSEC, it should result in WILDCARD_EMPTY.
  1452. const char* negative_dnssec_names[] = {
  1453. "a.bar.example.org.",
  1454. "foo.baz.bar.example.org.",
  1455. "a.foo.bar.example.org.",
  1456. NULL
  1457. };
  1458. this->expected_rdatas_.clear();
  1459. this->expected_rdatas_.push_back("wild.*.foo.*.bar.example.org. NSEC");
  1460. this->expected_sig_rdatas_.clear();
  1461. for (const char** name(negative_dnssec_names); *name != NULL; ++ name) {
  1462. doFindTest(*finder, isc::dns::Name(*name), this->qtype_,
  1463. RRType::NSEC(), this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1464. this->expected_rdatas_, this->expected_sig_rdatas_,
  1465. Name("bao.example.org."), ZoneFinder::FIND_DNSSEC);
  1466. }
  1467. // CNAME on a wildcard. Maybe not so common, but not disallowed.
  1468. this->expected_rdatas_.clear();
  1469. this->expected_rdatas_.push_back("www.example.org.");
  1470. this->expected_sig_rdatas_.clear();
  1471. doFindTest(*finder, isc::dns::Name("a.cnamewild.example.org."),
  1472. isc::dns::RRType::TXT(), isc::dns::RRType::CNAME(),
  1473. this->rrttl_, ZoneFinder::WILDCARD_CNAME,
  1474. this->expected_rdatas_, this->expected_sig_rdatas_);
  1475. // DNAME on a wildcard. In our implementation we ignore DNAMEs on a
  1476. // wildcard, but at a higher level we say the behavior is "unspecified".
  1477. // rfc2672bis strongly discourages the mixture of DNAME and wildcard
  1478. // (with SHOULD NOT).
  1479. this->expected_rdatas_.clear();
  1480. this->expected_sig_rdatas_.clear();
  1481. doFindTest(*finder, Name("a.dnamewild.example.org."),
  1482. this->qtype_, this->qtype_, this->rrttl_,
  1483. ZoneFinder::WILDCARD_NXRRSET, this->expected_rdatas_,
  1484. this->expected_sig_rdatas_);
  1485. // Some strange things in the wild node
  1486. this->expected_rdatas_.clear();
  1487. this->expected_rdatas_.push_back("ns.example.com.");
  1488. doFindTest(*finder, isc::dns::Name("a.nswild.example.org."),
  1489. isc::dns::RRType::TXT(), isc::dns::RRType::NS(),
  1490. this->rrttl_, ZoneFinder::DELEGATION,
  1491. this->expected_rdatas_, this->expected_sig_rdatas_);
  1492. }
  1493. TYPED_TEST(DatabaseClientTest, noWildcard) {
  1494. // Tests with the NO_WILDCARD flag.
  1495. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1496. // This would match *.wild.example.org, but with NO_WILDCARD should
  1497. // result in NXDOMAIN.
  1498. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A "
  1499. "NSEC RRSIG");
  1500. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1501. "20000201000000 12345 example.org. "
  1502. "FAKEFAKEFAKE");
  1503. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1504. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1505. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1506. this->expected_sig_rdatas_, Name("*.wild.example.org."),
  1507. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1508. // Should be the same without FIND_DNSSEC (but in this case no RRsets
  1509. // will be returned)
  1510. doFindTest(*finder, isc::dns::Name("a.wild.example.org"),
  1511. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1512. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1513. this->empty_rdatas_, Name::ROOT_NAME(), // name is dummy
  1514. ZoneFinder::NO_WILDCARD);
  1515. // Same for wildcard empty non terminal.
  1516. this->expected_rdatas_.clear();
  1517. this->expected_rdatas_.push_back("brokenns1.example.org. A NSEC");
  1518. doFindTest(*finder, isc::dns::Name("a.bar.example.org"),
  1519. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1520. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1521. this->empty_rdatas_, Name("wild.*.foo.*.bar.example.org"),
  1522. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1523. // Search for a wildcard name with NO_WILDCARD. There should be no
  1524. // difference. This is, for example, necessary to provide non existence
  1525. // of matching wildcard for isnx.nonterminal.example.org.
  1526. this->expected_rdatas_.clear();
  1527. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  1528. doFindTest(*finder, isc::dns::Name("*.nonterminal.example.org"),
  1529. RRType::NSEC(), RRType::NSEC(), this->rrttl_,
  1530. ZoneFinder::NXDOMAIN, this->expected_rdatas_,
  1531. this->empty_rdatas_, Name("l.example.org"),
  1532. ZoneFinder::FIND_DNSSEC | ZoneFinder::NO_WILDCARD);
  1533. // On the other hand, if there's exact match for the wildcard name
  1534. // it should be found regardless of NO_WILDCARD.
  1535. this->expected_rdatas_.clear();
  1536. this->expected_rdatas_.push_back("192.0.2.5");
  1537. this->expected_sig_rdatas_.clear();
  1538. this->expected_sig_rdatas_.push_back("A 5 3 3600 20000101000000 "
  1539. "20000201000000 12345 example.org. "
  1540. "FAKEFAKEFAKE");
  1541. doFindTest(*finder, isc::dns::Name("*.wild.example.org"),
  1542. this->qtype_, this->qtype_, this->rrttl_,
  1543. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1544. this->expected_sig_rdatas_, Name("*.wild.example.org"),
  1545. ZoneFinder::NO_WILDCARD);
  1546. }
  1547. TYPED_TEST(DatabaseClientTest, NXRRSET_NSEC) {
  1548. // The domain exists, but doesn't have this RRType
  1549. // So we should get its NSEC
  1550. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1551. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  1552. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1553. "20000201000000 12345 example.org. "
  1554. "FAKEFAKEFAKE");
  1555. doFindTest(*finder, isc::dns::Name("www.example.org."),
  1556. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1557. this->rrttl_, ZoneFinder::NXRRSET,
  1558. this->expected_rdatas_, this->expected_sig_rdatas_,
  1559. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC);
  1560. }
  1561. TYPED_TEST(DatabaseClientTest, wildcardNXRRSET_NSEC) {
  1562. // The domain exists, but doesn't have this RRType
  1563. // So we should get its NSEC
  1564. //
  1565. // The user will have to query us again to get the correct
  1566. // answer (eg. prove there's not an exact match)
  1567. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1568. this->expected_rdatas_.push_back("cancel.here.wild.example.org. A NSEC "
  1569. "RRSIG");
  1570. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1571. "20000201000000 12345 example.org. "
  1572. "FAKEFAKEFAKE");
  1573. // Note that the NSEC name should NOT be synthesized.
  1574. doFindTest(*finder, isc::dns::Name("a.wild.example.org."),
  1575. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1576. this->rrttl_, ZoneFinder::WILDCARD_NXRRSET,
  1577. this->expected_rdatas_, this->expected_sig_rdatas_,
  1578. Name("*.wild.example.org"), ZoneFinder::FIND_DNSSEC);
  1579. }
  1580. TYPED_TEST(DatabaseClientTest, NXDOMAIN_NSEC) {
  1581. // The domain doesn't exist, so we must get the right NSEC
  1582. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1583. this->expected_rdatas_.push_back("www2.example.org. A AAAA NSEC RRSIG");
  1584. this->expected_sig_rdatas_.push_back("NSEC 5 3 3600 20000101000000 "
  1585. "20000201000000 12345 example.org. "
  1586. "FAKEFAKEFAKE");
  1587. doFindTest(*finder, isc::dns::Name("www1.example.org."),
  1588. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1589. this->rrttl_, ZoneFinder::NXDOMAIN,
  1590. this->expected_rdatas_, this->expected_sig_rdatas_,
  1591. Name("www.example.org."), ZoneFinder::FIND_DNSSEC);
  1592. this->expected_rdatas_.clear();
  1593. this->expected_rdatas_.push_back("acnamesig1.example.org. NS A NSEC RRSIG");
  1594. // This tests it works correctly in apex (there was a bug, where a check
  1595. // for NS-alone was there and it would throw).
  1596. doFindTest(*finder, isc::dns::Name("aa.example.org."),
  1597. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(),
  1598. this->rrttl_, ZoneFinder::NXDOMAIN,
  1599. this->expected_rdatas_, this->expected_sig_rdatas_,
  1600. Name("example.org."), ZoneFinder::FIND_DNSSEC);
  1601. // Check that if the DB doesn't support it, the exception from there
  1602. // is not propagated and it only does not include the NSEC
  1603. if (!this->is_mock_) {
  1604. return; // We don't make the real DB to throw
  1605. }
  1606. EXPECT_NO_THROW(doFindTest(*finder,
  1607. isc::dns::Name("notimplnsec.example.org."),
  1608. isc::dns::RRType::TXT(),
  1609. isc::dns::RRType::NSEC(), this->rrttl_,
  1610. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1611. this->empty_rdatas_, Name::ROOT_NAME(),
  1612. ZoneFinder::FIND_DNSSEC));
  1613. }
  1614. TYPED_TEST(DatabaseClientTest, emptyNonterminalNSEC) {
  1615. // Same as NXDOMAIN_NSEC, but with empty non-terminal
  1616. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1617. this->expected_rdatas_.push_back("empty.nonterminal.example.org. NSEC");
  1618. doFindTest(*finder, isc::dns::Name("nonterminal.example.org."),
  1619. isc::dns::RRType::TXT(), isc::dns::RRType::NSEC(), this->rrttl_,
  1620. ZoneFinder::NXRRSET,
  1621. this->expected_rdatas_, this->expected_sig_rdatas_,
  1622. Name("l.example.org."), ZoneFinder::FIND_DNSSEC);
  1623. // Check that if the DB doesn't support it, the exception from there
  1624. // is not propagated and it only does not include the NSEC
  1625. if (!this->is_mock_) {
  1626. return; // We don't make the real DB to throw
  1627. }
  1628. EXPECT_NO_THROW(doFindTest(*finder,
  1629. isc::dns::Name("here.wild.example.org."),
  1630. isc::dns::RRType::TXT(),
  1631. isc::dns::RRType::NSEC(),
  1632. this->rrttl_, ZoneFinder::NXRRSET,
  1633. this->empty_rdatas_, this->empty_rdatas_,
  1634. Name::ROOT_NAME(), ZoneFinder::FIND_DNSSEC));
  1635. }
  1636. TYPED_TEST(DatabaseClientTest, getOrigin) {
  1637. DataSourceClient::FindResult
  1638. zone(this->client_->findZone(Name("example.org")));
  1639. ASSERT_EQ(result::SUCCESS, zone.code);
  1640. shared_ptr<DatabaseClient::Finder> finder(
  1641. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder));
  1642. if (this->is_mock_) {
  1643. EXPECT_EQ(READONLY_ZONE_ID, finder->zone_id());
  1644. }
  1645. EXPECT_EQ(this->zname_, finder->getOrigin());
  1646. }
  1647. TYPED_TEST(DatabaseClientTest, updaterFinder) {
  1648. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1649. ASSERT_TRUE(this->updater_);
  1650. // If this update isn't replacing the zone, the finder should work
  1651. // just like the normal find() case.
  1652. if (this->is_mock_) {
  1653. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  1654. this->updater_->getFinder());
  1655. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  1656. }
  1657. this->expected_rdatas_.clear();
  1658. this->expected_rdatas_.push_back("192.0.2.1");
  1659. doFindTest(this->updater_->getFinder(), this->qname_,
  1660. this->qtype_, this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1661. this->expected_rdatas_, this->empty_rdatas_);
  1662. // When replacing the zone, the updater's finder shouldn't see anything
  1663. // in the zone until something is added.
  1664. this->updater_.reset();
  1665. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1666. ASSERT_TRUE(this->updater_);
  1667. if (this->is_mock_) {
  1668. DatabaseClient::Finder& finder = dynamic_cast<DatabaseClient::Finder&>(
  1669. this->updater_->getFinder());
  1670. EXPECT_EQ(WRITABLE_ZONE_ID, finder.zone_id());
  1671. }
  1672. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1673. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1674. this->empty_rdatas_, this->empty_rdatas_);
  1675. }
  1676. TYPED_TEST(DatabaseClientTest, flushZone) {
  1677. // A simple update case: flush the entire zone
  1678. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1679. // Before update, the name exists.
  1680. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  1681. this->qtype_).code);
  1682. // start update in the replace mode. the normal finder should still
  1683. // be able to see the record, but the updater's finder shouldn't.
  1684. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1685. this->setUpdateAccessor();
  1686. EXPECT_EQ(ZoneFinder::SUCCESS,
  1687. finder->find(this->qname_, this->qtype_).code);
  1688. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1689. this->updater_->getFinder().find(this->qname_,
  1690. this->qtype_).code);
  1691. // commit the update. now the normal finder shouldn't see it.
  1692. this->updater_->commit();
  1693. EXPECT_EQ(ZoneFinder::NXDOMAIN, finder->find(this->qname_,
  1694. this->qtype_).code);
  1695. // Check rollback wasn't accidentally performed.
  1696. EXPECT_FALSE(this->isRollbacked());
  1697. }
  1698. TYPED_TEST(DatabaseClientTest, updateCancel) {
  1699. // similar to the previous test, but destruct the updater before commit.
  1700. ZoneFinderPtr finder = this->client_->findZone(this->zname_).zone_finder;
  1701. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  1702. this->qtype_).code);
  1703. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1704. this->setUpdateAccessor();
  1705. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1706. this->updater_->getFinder().find(this->qname_,
  1707. this->qtype_).code);
  1708. // DB should not have been rolled back yet.
  1709. EXPECT_FALSE(this->isRollbacked());
  1710. this->updater_.reset(); // destruct without commit
  1711. // reset() should have triggered rollback (although it doesn't affect
  1712. // anything to the mock accessor implementation except for the result of
  1713. // isRollbacked())
  1714. EXPECT_TRUE(this->isRollbacked(true));
  1715. EXPECT_EQ(ZoneFinder::SUCCESS, finder->find(this->qname_,
  1716. this->qtype_).code);
  1717. }
  1718. TYPED_TEST(DatabaseClientTest, exceptionFromRollback) {
  1719. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1720. this->rrset_.reset(new RRset(Name("throw.example.org"), this->qclass_,
  1721. this->qtype_, this->rrttl_));
  1722. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1723. this->rrset_->getClass(),
  1724. "192.0.2.1"));
  1725. this->updater_->addRRset(*this->rrset_);
  1726. // destruct without commit. The added name will result in an exception
  1727. // in the MockAccessor's rollback method. It shouldn't be propagated.
  1728. EXPECT_NO_THROW(this->updater_.reset());
  1729. }
  1730. TYPED_TEST(DatabaseClientTest, duplicateCommit) {
  1731. // duplicate commit. should result in exception.
  1732. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1733. this->updater_->commit();
  1734. EXPECT_THROW(this->updater_->commit(), DataSourceError);
  1735. }
  1736. TYPED_TEST(DatabaseClientTest, addRRsetToNewZone) {
  1737. // Add a single RRset to a fresh empty zone
  1738. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1739. this->updater_->addRRset(*this->rrset_);
  1740. this->expected_rdatas_.clear();
  1741. this->expected_rdatas_.push_back("192.0.2.2");
  1742. {
  1743. SCOPED_TRACE("add RRset");
  1744. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1745. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1746. this->expected_rdatas_, this->empty_rdatas_);
  1747. }
  1748. // Similar to the previous case, but with RRSIG
  1749. this->updater_.reset();
  1750. this->updater_ = this->client_->getUpdater(this->zname_, true);
  1751. this->updater_->addRRset(*this->rrset_);
  1752. this->updater_->addRRset(*this->rrsigset_);
  1753. // confirm the expected columns were passed to the accessor (if checkable).
  1754. const char* const rrsig_added[] = {
  1755. "www.example.org.", "org.example.www.", "3600", "RRSIG", "A",
  1756. "A 5 3 0 20000101000000 20000201000000 0 example.org. FAKEFAKEFAKE"
  1757. };
  1758. this->checkLastAdded(rrsig_added);
  1759. this->expected_sig_rdatas_.clear();
  1760. this->expected_sig_rdatas_.push_back(
  1761. rrsig_added[DatabaseAccessor::ADD_RDATA]);
  1762. {
  1763. SCOPED_TRACE("add RRset with RRSIG");
  1764. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1765. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1766. this->expected_rdatas_, this->expected_sig_rdatas_);
  1767. }
  1768. // Add the non RRSIG RRset again, to see the attempt of adding RRSIG
  1769. // causes any unexpected effect, in particular, whether the SIGTYPE
  1770. // field might remain.
  1771. this->updater_->addRRset(*this->rrset_);
  1772. const char* const rrset_added[] = {
  1773. "www.example.org.", "org.example.www.", "3600", "A", "", "192.0.2.2"
  1774. };
  1775. this->checkLastAdded(rrset_added);
  1776. }
  1777. TYPED_TEST(DatabaseClientTest, addRRsetToCurrentZone) {
  1778. // Similar to the previous test, but not replacing the existing data.
  1779. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1780. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1781. this->updater_->addRRset(*this->rrset_);
  1782. // We should see both old and new data.
  1783. this->expected_rdatas_.clear();
  1784. this->expected_rdatas_.push_back("192.0.2.1");
  1785. this->expected_rdatas_.push_back("192.0.2.2");
  1786. {
  1787. SCOPED_TRACE("add RRset");
  1788. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1789. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1790. this->expected_rdatas_, this->empty_rdatas_);
  1791. }
  1792. this->updater_->commit();
  1793. {
  1794. SCOPED_TRACE("add RRset after commit");
  1795. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  1796. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1797. this->empty_rdatas_);
  1798. }
  1799. }
  1800. TYPED_TEST(DatabaseClientTest, addMultipleRRs) {
  1801. // Similar to the previous case, but the added RRset contains multiple
  1802. // RRs.
  1803. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1804. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1805. this->rrset_->getClass(),
  1806. "192.0.2.3"));
  1807. this->updater_->addRRset(*this->rrset_);
  1808. this->expected_rdatas_.clear();
  1809. this->expected_rdatas_.push_back("192.0.2.1");
  1810. this->expected_rdatas_.push_back("192.0.2.2");
  1811. this->expected_rdatas_.push_back("192.0.2.3");
  1812. {
  1813. SCOPED_TRACE("add multiple RRs");
  1814. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1815. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1816. this->expected_rdatas_, this->empty_rdatas_);
  1817. }
  1818. }
  1819. TYPED_TEST(DatabaseClientTest, addRRsetOfLargerTTL) {
  1820. // Similar to the previous one, but the TTL of the added RRset is larger
  1821. // than that of the existing record. The finder should use the smaller
  1822. // one.
  1823. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1824. this->rrset_->setTTL(RRTTL(7200));
  1825. this->updater_->addRRset(*this->rrset_);
  1826. this->expected_rdatas_.clear();
  1827. this->expected_rdatas_.push_back("192.0.2.1");
  1828. this->expected_rdatas_.push_back("192.0.2.2");
  1829. {
  1830. SCOPED_TRACE("add RRset of larger TTL");
  1831. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1832. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1833. this->expected_rdatas_, this->empty_rdatas_);
  1834. }
  1835. }
  1836. TYPED_TEST(DatabaseClientTest, addRRsetOfSmallerTTL) {
  1837. // Similar to the previous one, but the added RRset has a smaller TTL.
  1838. // The added TTL should be used by the finder.
  1839. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1840. this->rrset_->setTTL(RRTTL(1800));
  1841. this->updater_->addRRset(*this->rrset_);
  1842. this->expected_rdatas_.clear();
  1843. this->expected_rdatas_.push_back("192.0.2.1");
  1844. this->expected_rdatas_.push_back("192.0.2.2");
  1845. {
  1846. SCOPED_TRACE("add RRset of smaller TTL");
  1847. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1848. this->qtype_, RRTTL(1800), ZoneFinder::SUCCESS,
  1849. this->expected_rdatas_, this->empty_rdatas_);
  1850. }
  1851. }
  1852. TYPED_TEST(DatabaseClientTest, addSameRR) {
  1853. // Add the same RR as that is already in the data source.
  1854. // Currently the add interface doesn't try to suppress the duplicate,
  1855. // neither does the finder. We may want to revisit it in future versions.
  1856. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1857. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  1858. this->rrttl_));
  1859. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1860. this->rrset_->getClass(),
  1861. "192.0.2.1"));
  1862. this->updater_->addRRset(*this->rrset_);
  1863. this->expected_rdatas_.clear();
  1864. this->expected_rdatas_.push_back("192.0.2.1");
  1865. this->expected_rdatas_.push_back("192.0.2.1");
  1866. {
  1867. SCOPED_TRACE("add same RR");
  1868. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1869. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  1870. this->expected_rdatas_, this->empty_rdatas_);
  1871. }
  1872. }
  1873. TYPED_TEST(DatabaseClientTest, addDeviantRR) {
  1874. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1875. // RR class mismatch. This should be detected and rejected.
  1876. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  1877. this->rrttl_));
  1878. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1879. this->rrset_->getClass(),
  1880. "test text"));
  1881. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  1882. // Out-of-zone owner name. At a higher level this should be rejected,
  1883. // but it doesn't happen in this interface.
  1884. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  1885. this->qtype_, this->rrttl_));
  1886. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1887. this->rrset_->getClass(),
  1888. "192.0.2.100"));
  1889. this->updater_->addRRset(*this->rrset_);
  1890. this->expected_rdatas_.clear();
  1891. this->expected_rdatas_.push_back("192.0.2.100");
  1892. {
  1893. // Note: with the find() implementation being more strict about
  1894. // zone cuts, this test may fail. Then the test should be updated.
  1895. SCOPED_TRACE("add out-of-zone RR");
  1896. doFindTest(this->updater_->getFinder(), Name("example.com"),
  1897. this->qtype_, this->qtype_, this->rrttl_,
  1898. ZoneFinder::SUCCESS, this->expected_rdatas_,
  1899. this->empty_rdatas_);
  1900. }
  1901. }
  1902. TYPED_TEST(DatabaseClientTest, addEmptyRRset) {
  1903. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1904. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  1905. this->rrttl_));
  1906. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  1907. }
  1908. TYPED_TEST(DatabaseClientTest, addAfterCommit) {
  1909. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1910. this->updater_->commit();
  1911. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  1912. }
  1913. TYPED_TEST(DatabaseClientTest, addRRsetWithRRSIG) {
  1914. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1915. this->rrset_->addRRsig(*this->rrsigset_);
  1916. EXPECT_THROW(this->updater_->addRRset(*this->rrset_), DataSourceError);
  1917. }
  1918. TYPED_TEST(DatabaseClientTest, deleteRRset) {
  1919. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  1920. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  1921. this->rrttl_));
  1922. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1923. this->rrset_->getClass(),
  1924. "192.0.2.1"));
  1925. // Delete one RR from an RRset
  1926. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1927. this->updater_->deleteRRset(*this->rrset_);
  1928. // Delete the only RR of a name
  1929. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  1930. RRType::CNAME(), this->rrttl_));
  1931. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1932. this->rrset_->getClass(),
  1933. "www.example.org"));
  1934. this->updater_->deleteRRset(*this->rrset_);
  1935. // The this->updater_ finder should immediately see the deleted results.
  1936. {
  1937. SCOPED_TRACE("delete RRset");
  1938. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  1939. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  1940. this->empty_rdatas_, this->empty_rdatas_);
  1941. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  1942. this->qtype_, this->qtype_, this->rrttl_,
  1943. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1944. this->empty_rdatas_);
  1945. }
  1946. // before committing the change, the original finder should see the
  1947. // original record.
  1948. {
  1949. SCOPED_TRACE("delete RRset before commit");
  1950. this->expected_rdatas_.push_back("192.0.2.1");
  1951. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  1952. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  1953. this->empty_rdatas_);
  1954. this->expected_rdatas_.clear();
  1955. this->expected_rdatas_.push_back("www.example.org.");
  1956. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  1957. RRType::CNAME(), this->rrttl_, ZoneFinder::CNAME,
  1958. this->expected_rdatas_, this->empty_rdatas_);
  1959. }
  1960. // once committed, the record should be removed from the original finder's
  1961. // view, too.
  1962. this->updater_->commit();
  1963. {
  1964. SCOPED_TRACE("delete RRset after commit");
  1965. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_,
  1966. this->rrttl_, ZoneFinder::NXRRSET, this->empty_rdatas_,
  1967. this->empty_rdatas_);
  1968. doFindTest(*finder, Name("cname.example.org"), this->qtype_,
  1969. this->qtype_, this->rrttl_, ZoneFinder::NXDOMAIN,
  1970. this->empty_rdatas_, this->empty_rdatas_);
  1971. }
  1972. }
  1973. TYPED_TEST(DatabaseClientTest, deleteRRsetToNXDOMAIN) {
  1974. // similar to the previous case, but it removes the only record of the
  1975. // given name. a subsequent find() should result in NXDOMAIN.
  1976. this->rrset_.reset(new RRset(Name("cname.example.org"), this->qclass_,
  1977. RRType::CNAME(), this->rrttl_));
  1978. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1979. this->rrset_->getClass(),
  1980. "www.example.org"));
  1981. this->updater_ = this->client_->getUpdater(this->zname_, false);
  1982. this->updater_->deleteRRset(*this->rrset_);
  1983. {
  1984. SCOPED_TRACE("delete RRset to NXDOMAIN");
  1985. doFindTest(this->updater_->getFinder(), Name("cname.example.org"),
  1986. this->qtype_, this->qtype_, this->rrttl_,
  1987. ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  1988. this->empty_rdatas_);
  1989. }
  1990. }
  1991. TYPED_TEST(DatabaseClientTest, deleteMultipleRRs) {
  1992. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  1993. this->rrttl_));
  1994. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1995. this->rrset_->getClass(),
  1996. "2001:db8::1"));
  1997. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  1998. this->rrset_->getClass(),
  1999. "2001:db8::2"));
  2000. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2001. this->updater_->deleteRRset(*this->rrset_);
  2002. {
  2003. SCOPED_TRACE("delete multiple RRs");
  2004. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  2005. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2006. this->empty_rdatas_, this->empty_rdatas_);
  2007. }
  2008. }
  2009. TYPED_TEST(DatabaseClientTest, partialDelete) {
  2010. this->rrset_.reset(new RRset(this->qname_, this->qclass_, RRType::AAAA(),
  2011. this->rrttl_));
  2012. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2013. this->rrset_->getClass(),
  2014. "2001:db8::1"));
  2015. // This does not exist in the test data source:
  2016. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2017. this->rrset_->getClass(),
  2018. "2001:db8::3"));
  2019. // deleteRRset should succeed "silently", and subsequent find() should
  2020. // find the remaining RR.
  2021. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2022. this->updater_->deleteRRset(*this->rrset_);
  2023. {
  2024. SCOPED_TRACE("partial delete");
  2025. this->expected_rdatas_.push_back("2001:db8::2");
  2026. doFindTest(this->updater_->getFinder(), this->qname_, RRType::AAAA(),
  2027. RRType::AAAA(), this->rrttl_, ZoneFinder::SUCCESS,
  2028. this->expected_rdatas_, this->empty_rdatas_);
  2029. }
  2030. }
  2031. TYPED_TEST(DatabaseClientTest, deleteNoMatch) {
  2032. // similar to the previous test, but there's not even a match in the
  2033. // specified RRset. Essentially there's no difference in the result.
  2034. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2035. this->updater_->deleteRRset(*this->rrset_);
  2036. {
  2037. SCOPED_TRACE("delete no match");
  2038. this->expected_rdatas_.push_back("192.0.2.1");
  2039. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2040. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2041. this->expected_rdatas_, this->empty_rdatas_);
  2042. }
  2043. }
  2044. TYPED_TEST(DatabaseClientTest, deleteWithDifferentTTL) {
  2045. // Our delete interface simply ignores TTL (may change in a future version)
  2046. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2047. RRTTL(1800)));
  2048. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2049. this->rrset_->getClass(),
  2050. "192.0.2.1"));
  2051. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2052. this->updater_->deleteRRset(*this->rrset_);
  2053. {
  2054. SCOPED_TRACE("delete RRset with a different TTL");
  2055. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2056. this->qtype_, this->rrttl_, ZoneFinder::NXRRSET,
  2057. this->empty_rdatas_, this->empty_rdatas_);
  2058. }
  2059. }
  2060. TYPED_TEST(DatabaseClientTest, deleteDeviantRR) {
  2061. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2062. // RR class mismatch. This should be detected and rejected.
  2063. this->rrset_.reset(new RRset(this->qname_, RRClass::CH(), RRType::TXT(),
  2064. this->rrttl_));
  2065. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2066. this->rrset_->getClass(),
  2067. "test text"));
  2068. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2069. // Out-of-zone owner name. At a higher level this should be rejected,
  2070. // but it doesn't happen in this interface.
  2071. this->rrset_.reset(new RRset(Name("example.com"), this->qclass_,
  2072. this->qtype_, this->rrttl_));
  2073. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2074. this->rrset_->getClass(),
  2075. "192.0.2.100"));
  2076. EXPECT_NO_THROW(this->updater_->deleteRRset(*this->rrset_));
  2077. }
  2078. TYPED_TEST(DatabaseClientTest, deleteAfterCommit) {
  2079. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2080. this->updater_->commit();
  2081. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2082. }
  2083. TYPED_TEST(DatabaseClientTest, deleteEmptyRRset) {
  2084. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2085. this->rrset_.reset(new RRset(this->qname_, this->qclass_, this->qtype_,
  2086. this->rrttl_));
  2087. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2088. }
  2089. TYPED_TEST(DatabaseClientTest, deleteRRsetWithRRSIG) {
  2090. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2091. this->rrset_->addRRsig(*this->rrsigset_);
  2092. EXPECT_THROW(this->updater_->deleteRRset(*this->rrset_), DataSourceError);
  2093. }
  2094. TYPED_TEST(DatabaseClientTest, compoundUpdate) {
  2095. // This test case performs an arbitrary chosen add/delete operations
  2096. // in a single update transaction. Essentially there is nothing new to
  2097. // test here, but there may be some bugs that was overlooked and can
  2098. // only happen in the compound update scenario, so we test it.
  2099. this->updater_ = this->client_->getUpdater(this->zname_, false);
  2100. // add a new RR to an existing RRset
  2101. this->updater_->addRRset(*this->rrset_);
  2102. this->expected_rdatas_.clear();
  2103. this->expected_rdatas_.push_back("192.0.2.1");
  2104. this->expected_rdatas_.push_back("192.0.2.2");
  2105. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2106. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2107. this->expected_rdatas_, this->empty_rdatas_);
  2108. // delete an existing RR
  2109. this->rrset_.reset(new RRset(Name("www.example.org"), this->qclass_,
  2110. this->qtype_, this->rrttl_));
  2111. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2112. this->rrset_->getClass(),
  2113. "192.0.2.1"));
  2114. this->updater_->deleteRRset(*this->rrset_);
  2115. this->expected_rdatas_.clear();
  2116. this->expected_rdatas_.push_back("192.0.2.2");
  2117. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2118. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2119. this->expected_rdatas_, this->empty_rdatas_);
  2120. // re-add it
  2121. this->updater_->addRRset(*this->rrset_);
  2122. this->expected_rdatas_.push_back("192.0.2.1");
  2123. doFindTest(this->updater_->getFinder(), this->qname_, this->qtype_,
  2124. this->qtype_, this->rrttl_, ZoneFinder::SUCCESS,
  2125. this->expected_rdatas_, this->empty_rdatas_);
  2126. // add a new RR with a new name
  2127. const Name newname("newname.example.org");
  2128. const RRType newtype(RRType::AAAA());
  2129. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2130. this->rrttl_, ZoneFinder::NXDOMAIN, this->empty_rdatas_,
  2131. this->empty_rdatas_);
  2132. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  2133. this->rrttl_));
  2134. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2135. this->rrset_->getClass(),
  2136. "2001:db8::10"));
  2137. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2138. this->rrset_->getClass(),
  2139. "2001:db8::11"));
  2140. this->updater_->addRRset(*this->rrset_);
  2141. this->expected_rdatas_.clear();
  2142. this->expected_rdatas_.push_back("2001:db8::10");
  2143. this->expected_rdatas_.push_back("2001:db8::11");
  2144. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2145. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2146. this->empty_rdatas_);
  2147. // delete one RR from the previous set
  2148. this->rrset_.reset(new RRset(newname, this->qclass_, newtype,
  2149. this->rrttl_));
  2150. this->rrset_->addRdata(rdata::createRdata(this->rrset_->getType(),
  2151. this->rrset_->getClass(),
  2152. "2001:db8::11"));
  2153. this->updater_->deleteRRset(*this->rrset_);
  2154. this->expected_rdatas_.clear();
  2155. this->expected_rdatas_.push_back("2001:db8::10");
  2156. doFindTest(this->updater_->getFinder(), newname, newtype, newtype,
  2157. this->rrttl_, ZoneFinder::SUCCESS, this->expected_rdatas_,
  2158. this->empty_rdatas_);
  2159. // Commit the changes, confirm the entire changes applied.
  2160. this->updater_->commit();
  2161. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2162. this->expected_rdatas_.clear();
  2163. this->expected_rdatas_.push_back("192.0.2.2");
  2164. this->expected_rdatas_.push_back("192.0.2.1");
  2165. doFindTest(*finder, this->qname_, this->qtype_, this->qtype_, this->rrttl_,
  2166. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2167. this->empty_rdatas_);
  2168. this->expected_rdatas_.clear();
  2169. this->expected_rdatas_.push_back("2001:db8::10");
  2170. doFindTest(*finder, newname, newtype, newtype, this->rrttl_,
  2171. ZoneFinder::SUCCESS, this->expected_rdatas_,
  2172. this->empty_rdatas_);
  2173. }
  2174. TYPED_TEST(DatabaseClientTest, previous) {
  2175. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2176. EXPECT_EQ(Name("www.example.org."),
  2177. finder->findPreviousName(Name("www2.example.org.")));
  2178. // Check a name that doesn't exist there
  2179. EXPECT_EQ(Name("www.example.org."),
  2180. finder->findPreviousName(Name("www1.example.org.")));
  2181. if (this->is_mock_) { // We can't really force the DB to throw
  2182. // Check it doesn't crash or anything if the underlying DB throws
  2183. DataSourceClient::FindResult
  2184. zone(this->client_->findZone(Name("bad.example.org")));
  2185. finder =
  2186. dynamic_pointer_cast<DatabaseClient::Finder>(zone.zone_finder);
  2187. EXPECT_THROW(finder->findPreviousName(Name("bad.example.org")),
  2188. isc::NotImplemented);
  2189. } else {
  2190. // No need to test this on mock one, because we test only that
  2191. // the exception gets through
  2192. // A name before the origin
  2193. EXPECT_THROW(finder->findPreviousName(Name("example.com")),
  2194. isc::NotImplemented);
  2195. }
  2196. }
  2197. TYPED_TEST(DatabaseClientTest, invalidRdata) {
  2198. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2199. EXPECT_THROW(finder->find(Name("invalidrdata.example.org."), RRType::A()),
  2200. DataSourceError);
  2201. EXPECT_THROW(finder->find(Name("invalidrdata2.example.org."), RRType::A()),
  2202. DataSourceError);
  2203. }
  2204. TEST_F(MockDatabaseClientTest, missingNSEC) {
  2205. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2206. /*
  2207. * FIXME: For now, we can't really distinguish this bogus input
  2208. * from not-signed zone so we can't throw. But once we can,
  2209. * enable the original test.
  2210. */
  2211. #if 0
  2212. EXPECT_THROW(finder->find(Name("badnsec2.example.org."), RRType::A(), NULL,
  2213. ZoneFinder::FIND_DNSSEC),
  2214. DataSourceError);
  2215. #endif
  2216. doFindTest(*finder, Name("badnsec2.example.org."), RRType::A(),
  2217. RRType::A(), this->rrttl_, ZoneFinder::NXDOMAIN,
  2218. this->expected_rdatas_, this->expected_sig_rdatas_);
  2219. }
  2220. TEST_F(MockDatabaseClientTest, badName) {
  2221. shared_ptr<DatabaseClient::Finder> finder(this->getFinder());
  2222. EXPECT_THROW(finder->findPreviousName(Name("brokenname.example.org.")),
  2223. DataSourceError);
  2224. }
  2225. }