database_unittest.cc 162 KB

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