database_unittest.cc 196 KB

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