database_unittest.cc 182 KB

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