xfrout_test.py.in 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. '''Tests for the XfroutSession and UnixSockServer classes '''
  16. import unittest
  17. import os
  18. from isc.testutils.tsigctx_mock import MockTSIGContext
  19. from isc.testutils.ccsession_mock import MockModuleCCSession
  20. from isc.cc.session import *
  21. import isc.config
  22. from isc.dns import *
  23. from isc.testutils.rrset_utils import *
  24. from xfrout import *
  25. import xfrout
  26. import isc.log
  27. import isc.acl.dns
  28. TESTDATA_SRCDIR = os.getenv("TESTDATASRCDIR")
  29. TSIG_KEY = TSIGKey("example.com:SFuWd/q99SzF8Yzd1QbB9g==")
  30. #
  31. # Commonly used (mostly constant) test parameters
  32. #
  33. TEST_ZONE_NAME_STR = "example.com."
  34. TEST_ZONE_NAME = Name(TEST_ZONE_NAME_STR)
  35. TEST_RRCLASS = RRClass.IN()
  36. IXFR_OK_VERSION = 2011111802
  37. IXFR_NG_VERSION = 2011111803
  38. SOA_CURRENT_VERSION = 2011112001
  39. # our fake socket, where we can read and insert messages
  40. class MySocket():
  41. def __init__(self, family, type):
  42. self.family = family
  43. self.type = type
  44. self.sendqueue = bytearray()
  45. def connect(self, to):
  46. pass
  47. def close(self):
  48. pass
  49. def send(self, data):
  50. self.sendqueue.extend(data);
  51. return len(data)
  52. def readsent(self):
  53. if len(self.sendqueue) >= 2:
  54. size = 2 + struct.unpack("!H", self.sendqueue[:2])[0]
  55. else:
  56. size = 0
  57. result = self.sendqueue[:size]
  58. self.sendqueue = self.sendqueue[size:]
  59. return result
  60. def read_msg(self, parse_options=Message.PARSE_DEFAULT, need_len=False):
  61. sent_data = self.readsent()
  62. get_msg = Message(Message.PARSE)
  63. get_msg.from_wire(bytes(sent_data[2:]), parse_options)
  64. if need_len:
  65. return (get_msg, len(sent_data) - 2)
  66. return get_msg
  67. def clear_send(self):
  68. del self.sendqueue[:]
  69. class MockDataSrcClient:
  70. def __init__(self, type, config):
  71. pass
  72. def find_zone(self, zone_name):
  73. '''Mock version of find_zone().
  74. It returns itself (subsequently acting as a mock ZoneFinder) for
  75. some test zone names. For a special name it returns NOTFOUND to
  76. emulate the condition where the specified zone doen't exist.
  77. '''
  78. self._zone_name = zone_name
  79. if zone_name == Name('notauth.example.com'):
  80. return (isc.datasrc.DataSourceClient.NOTFOUND, None)
  81. return (isc.datasrc.DataSourceClient.SUCCESS, self)
  82. def find(self, name, rrtype, options=ZoneFinder.FIND_DEFAULT):
  83. '''Mock ZoneFinder.find().
  84. (At the moment) this method only handles query for type SOA.
  85. By default it returns a normal SOA RR(set) whose owner name is
  86. the query name It also emulates some unusual cases for special
  87. zone names.
  88. '''
  89. if name == Name('nosoa.example.com') and rrtype == RRType.SOA():
  90. return (ZoneFinder.NXDOMAIN, None, 0)
  91. elif name == Name('multisoa.example.com') and rrtype == RRType.SOA():
  92. soa_rrset = create_soa(SOA_CURRENT_VERSION)
  93. soa_rrset.add_rdata(soa_rrset.get_rdata()[0])
  94. return (ZoneFinder.SUCCESS, soa_rrset, 0)
  95. elif name == Name('maxserial.example.com'):
  96. soa_rrset = create_soa(0xffffffff)
  97. return (ZoneFinder.SUCCESS, soa_rrset, 0)
  98. elif rrtype == RRType.SOA():
  99. return (ZoneFinder.SUCCESS, create_soa(SOA_CURRENT_VERSION), 0)
  100. raise ValueError('Unexpected input to mock finder: bug in test case?')
  101. def get_iterator(self, zone_name, adjust_ttl=False):
  102. if zone_name == Name('notauth.example.com'):
  103. raise isc.datasrc.Error('no such zone')
  104. self._zone_name = zone_name
  105. return self
  106. def get_soa(self): # emulate ZoneIterator.get_soa()
  107. if self._zone_name == Name('nosoa.example.com'):
  108. return None
  109. soa_rrset = create_soa(SOA_CURRENT_VERSION)
  110. if self._zone_name == Name('multisoa.example.com'):
  111. soa_rrset.add_rdata(soa_rrset.get_rdata()[0])
  112. return soa_rrset
  113. def get_journal_reader(self, zone_name, begin_serial, end_serial):
  114. if zone_name == Name('notauth2.example.com'):
  115. return isc.datasrc.ZoneJournalReader.NO_SUCH_ZONE, None
  116. if zone_name == Name('nojournal.example.com'):
  117. raise isc.datasrc.NotImplemented('journaling not supported')
  118. if begin_serial == IXFR_NG_VERSION:
  119. return isc.datasrc.ZoneJournalReader.NO_SUCH_VERSION, None
  120. return isc.datasrc.ZoneJournalReader.SUCCESS, self
  121. class MyCCSession(isc.config.ConfigData):
  122. def __init__(self):
  123. module_spec = isc.config.module_spec_from_file(
  124. xfrout.SPECFILE_LOCATION)
  125. ConfigData.__init__(self, module_spec)
  126. def get_remote_config_value(self, module_name, identifier):
  127. if module_name == "Auth" and identifier == "database_file":
  128. return "initdb.file", False
  129. else:
  130. return "unknown", False
  131. # This constant dictionary stores all default configuration parameters
  132. # defined in the xfrout spec file.
  133. DEFAULT_CONFIG = MyCCSession().get_full_config()
  134. # We subclass the Session class we're testing here, only overriding a few
  135. # methods
  136. class MyXfroutSession(XfroutSession):
  137. def _handle(self):
  138. pass
  139. def _close_socket(self):
  140. pass
  141. def _send_data(self, sock, data):
  142. size = len(data)
  143. total_count = 0
  144. while total_count < size:
  145. count = sock.send(data[total_count:])
  146. total_count += count
  147. class Dbserver:
  148. def __init__(self):
  149. self._shutdown_event = threading.Event()
  150. self.transfer_counter = 0
  151. self._max_transfers_out = DEFAULT_CONFIG['transfers_out']
  152. def get_db_file(self):
  153. return 'test.sqlite3'
  154. def increase_transfers_counter(self):
  155. self.transfer_counter += 1
  156. return True
  157. def decrease_transfers_counter(self):
  158. self.transfer_counter -= 1
  159. class TestXfroutSessionBase(unittest.TestCase):
  160. '''Base classs for tests related to xfrout sessions
  161. This class defines common setup/teadown and utility methods. Actual
  162. tests are delegated to subclasses.
  163. '''
  164. def getmsg(self):
  165. msg = Message(Message.PARSE)
  166. msg.from_wire(self.mdata)
  167. return msg
  168. def create_mock_tsig_ctx(self, error):
  169. # This helper function creates a MockTSIGContext for a given key
  170. # and TSIG error to be used as a result of verify (normally faked
  171. # one)
  172. mock_ctx = MockTSIGContext(TSIG_KEY)
  173. mock_ctx.error = error
  174. return mock_ctx
  175. def message_has_tsig(self, msg):
  176. return msg.get_tsig_record() is not None
  177. def create_request_data(self, with_question=True, with_tsig=False,
  178. ixfr=None, qtype=None, zone_name=TEST_ZONE_NAME,
  179. soa_class=TEST_RRCLASS, num_soa=1):
  180. '''Create a commonly used XFR request data.
  181. By default the request type is AXFR; if 'ixfr' is an integer,
  182. the request type will be IXFR and an SOA with the serial being
  183. the value of the parameter will be included in the authority
  184. section.
  185. This method has various minor parameters only for creating bad
  186. format requests for testing purposes:
  187. qtype: the RR type of the question section. By default automatically
  188. determined by the value of ixfr, but could be an invalid type
  189. for testing.
  190. zone_name: the query (zone) name. for IXFR, it's also used as
  191. the owner name of the SOA in the authority section.
  192. soa_class: IXFR only. The RR class of the SOA RR in the authority
  193. section.
  194. num_soa: IXFR only. The number of SOA RDATAs in the authority
  195. section.
  196. '''
  197. msg = Message(Message.RENDER)
  198. query_id = 0x1035
  199. msg.set_qid(query_id)
  200. msg.set_opcode(Opcode.QUERY())
  201. msg.set_rcode(Rcode.NOERROR())
  202. req_type = RRType.AXFR() if ixfr is None else RRType.IXFR()
  203. if with_question:
  204. msg.add_question(Question(zone_name, RRClass.IN(),
  205. req_type if qtype is None else qtype))
  206. if req_type == RRType.IXFR():
  207. soa = RRset(zone_name, soa_class, RRType.SOA(), RRTTL(0))
  208. # In the RDATA only the serial matters.
  209. for i in range(0, num_soa):
  210. soa.add_rdata(Rdata(RRType.SOA(), soa_class,
  211. 'm r ' + str(ixfr) + ' 1 1 1 1'))
  212. msg.add_rrset(Message.SECTION_AUTHORITY, soa)
  213. renderer = MessageRenderer()
  214. if with_tsig:
  215. tsig_ctx = MockTSIGContext(TSIG_KEY)
  216. msg.to_wire(renderer, tsig_ctx)
  217. else:
  218. msg.to_wire(renderer)
  219. request_data = renderer.get_data()
  220. return request_data
  221. def set_request_type(self, type):
  222. self.xfrsess._request_type = type
  223. if type == RRType.AXFR():
  224. self.xfrsess._request_typestr = 'AXFR'
  225. else:
  226. self.xfrsess._request_typestr = 'IXFR'
  227. def setUp(self):
  228. self.sock = MySocket(socket.AF_INET,socket.SOCK_STREAM)
  229. self.xfrsess = MyXfroutSession(self.sock, None, Dbserver(),
  230. TSIGKeyRing(),
  231. (socket.AF_INET, socket.SOCK_STREAM,
  232. ('127.0.0.1', 12345)),
  233. # When not testing ACLs, simply accept
  234. isc.acl.dns.REQUEST_LOADER.load(
  235. [{"action": "ACCEPT"}]),
  236. {})
  237. self.set_request_type(RRType.AXFR()) # test AXFR by default
  238. self.mdata = self.create_request_data()
  239. self.soa_rrset = create_soa(SOA_CURRENT_VERSION)
  240. # some test replaces a module-wide function. We should ensure the
  241. # original is used elsewhere.
  242. self.orig_get_rrset_len = xfrout.get_rrset_len
  243. def tearDown(self):
  244. xfrout.get_rrset_len = self.orig_get_rrset_len
  245. # transfer_counter must be always be reset no matter happens within
  246. # the XfroutSession object. We check the condition here.
  247. self.assertEqual(0, self.xfrsess._server.transfer_counter)
  248. class TestXfroutSession(TestXfroutSessionBase):
  249. def test_quota_error(self):
  250. '''Emulating the server being too busy.
  251. '''
  252. self.xfrsess._request_data = self.mdata
  253. self.xfrsess._server.increase_transfers_counter = lambda : False
  254. XfroutSession._handle(self.xfrsess)
  255. self.assertEqual(self.sock.read_msg().get_rcode(), Rcode.REFUSED())
  256. def test_quota_ok(self):
  257. '''The default case in terms of the xfrout quota.
  258. '''
  259. # set up a bogus request, which should result in FORMERR. (it only
  260. # has to be something that is different from the previous case)
  261. self.xfrsess._request_data = \
  262. self.create_request_data(ixfr=IXFR_OK_VERSION, num_soa=2)
  263. # Replace the data source client to avoid datasrc related exceptions
  264. self.xfrsess.ClientClass = MockDataSrcClient
  265. XfroutSession._handle(self.xfrsess)
  266. self.assertEqual(self.sock.read_msg().get_rcode(), Rcode.FORMERR())
  267. def test_exception_from_session(self):
  268. '''Test the case where the main processing raises an exception.
  269. We just check it doesn't any unexpected disruption and (in tearDown)
  270. transfer_counter is correctly reset to 0.
  271. '''
  272. def dns_xfrout_start(fd, msg, quota):
  273. raise ValueError('fake exception')
  274. self.xfrsess.dns_xfrout_start = dns_xfrout_start
  275. XfroutSession._handle(self.xfrsess)
  276. def test_parse_query_message(self):
  277. # Valid AXFR
  278. [get_rcode, get_msg] = self.xfrsess._parse_query_message(self.mdata)
  279. self.assertEqual(RRType.AXFR(), self.xfrsess._request_type)
  280. self.assertEqual(get_rcode.to_text(), "NOERROR")
  281. # Valid IXFR
  282. request_data = self.create_request_data(ixfr=2011111801)
  283. rcode, msg = self.xfrsess._parse_query_message(request_data)
  284. self.assertEqual(RRType.IXFR(), self.xfrsess._request_type)
  285. self.assertEqual(Rcode.NOERROR(), rcode)
  286. # Broken request: no question
  287. self.assertRaises(RuntimeError, self.xfrsess._parse_query_message,
  288. self.create_request_data(with_question=False))
  289. # Broken request: invalid RR type (neither AXFR nor IXFR)
  290. self.assertRaises(RuntimeError, self.xfrsess._parse_query_message,
  291. self.create_request_data(qtype=RRType.A()))
  292. # NOERROR
  293. request_data = self.create_request_data(ixfr=IXFR_OK_VERSION)
  294. rcode, msg = self.xfrsess._parse_query_message(request_data)
  295. self.assertEqual(rcode.to_text(), "NOERROR")
  296. # tsig signed query message
  297. request_data = self.create_request_data(with_tsig=True)
  298. # BADKEY
  299. [rcode, msg] = self.xfrsess._parse_query_message(request_data)
  300. self.assertEqual(rcode.to_text(), "NOTAUTH")
  301. self.assertTrue(self.xfrsess._tsig_ctx is not None)
  302. # NOERROR
  303. self.assertEqual(TSIGKeyRing.SUCCESS,
  304. self.xfrsess._tsig_key_ring.add(TSIG_KEY))
  305. [rcode, msg] = self.xfrsess._parse_query_message(request_data)
  306. self.assertEqual(rcode.to_text(), "NOERROR")
  307. self.assertTrue(self.xfrsess._tsig_ctx is not None)
  308. def check_transfer_acl(self, acl_setter):
  309. # ACL checks, put some ACL inside
  310. acl_setter(isc.acl.dns.REQUEST_LOADER.load([
  311. {
  312. "from": "127.0.0.1",
  313. "action": "ACCEPT"
  314. },
  315. {
  316. "from": "192.0.2.1",
  317. "action": "DROP"
  318. }
  319. ]))
  320. # Localhost (the default in this test) is accepted
  321. rcode, msg = self.xfrsess._parse_query_message(self.mdata)
  322. self.assertEqual(rcode.to_text(), "NOERROR")
  323. # This should be dropped completely, therefore returning None
  324. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  325. ('192.0.2.1', 12345))
  326. rcode, msg = self.xfrsess._parse_query_message(self.mdata)
  327. self.assertEqual(None, rcode)
  328. # This should be refused, therefore REFUSED
  329. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  330. ('192.0.2.2', 12345))
  331. rcode, msg = self.xfrsess._parse_query_message(self.mdata)
  332. self.assertEqual(rcode.to_text(), "REFUSED")
  333. # TSIG signed request
  334. request_data = self.create_request_data(with_tsig=True)
  335. # If the TSIG check fails, it should not check ACL
  336. # (If it checked ACL as well, it would just drop the request)
  337. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  338. ('192.0.2.1', 12345))
  339. self.xfrsess._tsig_key_ring = TSIGKeyRing()
  340. rcode, msg = self.xfrsess._parse_query_message(request_data)
  341. self.assertEqual(rcode.to_text(), "NOTAUTH")
  342. self.assertTrue(self.xfrsess._tsig_ctx is not None)
  343. # ACL using TSIG: successful case
  344. acl_setter(isc.acl.dns.REQUEST_LOADER.load([
  345. {"key": "example.com", "action": "ACCEPT"}, {"action": "REJECT"}
  346. ]))
  347. self.assertEqual(TSIGKeyRing.SUCCESS,
  348. self.xfrsess._tsig_key_ring.add(TSIG_KEY))
  349. [rcode, msg] = self.xfrsess._parse_query_message(request_data)
  350. self.assertEqual(rcode.to_text(), "NOERROR")
  351. # ACL using TSIG: key name doesn't match; should be rejected
  352. acl_setter(isc.acl.dns.REQUEST_LOADER.load([
  353. {"key": "example.org", "action": "ACCEPT"}, {"action": "REJECT"}
  354. ]))
  355. [rcode, msg] = self.xfrsess._parse_query_message(request_data)
  356. self.assertEqual(rcode.to_text(), "REFUSED")
  357. # ACL using TSIG: no TSIG; should be rejected
  358. acl_setter(isc.acl.dns.REQUEST_LOADER.load([
  359. {"key": "example.org", "action": "ACCEPT"}, {"action": "REJECT"}
  360. ]))
  361. [rcode, msg] = self.xfrsess._parse_query_message(self.mdata)
  362. self.assertEqual(rcode.to_text(), "REFUSED")
  363. #
  364. # ACL using IP + TSIG: both should match
  365. #
  366. acl_setter(isc.acl.dns.REQUEST_LOADER.load([
  367. {"ALL": [{"key": "example.com"}, {"from": "192.0.2.1"}],
  368. "action": "ACCEPT"},
  369. {"action": "REJECT"}
  370. ]))
  371. # both matches
  372. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  373. ('192.0.2.1', 12345))
  374. [rcode, msg] = self.xfrsess._parse_query_message(request_data)
  375. self.assertEqual(rcode.to_text(), "NOERROR")
  376. # TSIG matches, but address doesn't
  377. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  378. ('192.0.2.2', 12345))
  379. [rcode, msg] = self.xfrsess._parse_query_message(request_data)
  380. self.assertEqual(rcode.to_text(), "REFUSED")
  381. # Address matches, but TSIG doesn't (not included)
  382. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  383. ('192.0.2.1', 12345))
  384. [rcode, msg] = self.xfrsess._parse_query_message(self.mdata)
  385. self.assertEqual(rcode.to_text(), "REFUSED")
  386. # Neither address nor TSIG matches
  387. self.xfrsess._remote = (socket.AF_INET, socket.SOCK_STREAM,
  388. ('192.0.2.2', 12345))
  389. [rcode, msg] = self.xfrsess._parse_query_message(self.mdata)
  390. self.assertEqual(rcode.to_text(), "REFUSED")
  391. def test_transfer_acl(self):
  392. # ACL checks only with the default ACL
  393. def acl_setter(acl):
  394. self.xfrsess._acl = acl
  395. self.check_transfer_acl(acl_setter)
  396. def test_transfer_zoneacl(self):
  397. # ACL check with a per zone ACL + default ACL. The per zone ACL
  398. # should match the queryied zone, so it should be used.
  399. def acl_setter(acl):
  400. zone_key = ('IN', 'example.com.')
  401. self.xfrsess._zone_config[zone_key] = {}
  402. self.xfrsess._zone_config[zone_key]['transfer_acl'] = acl
  403. self.xfrsess._acl = isc.acl.dns.REQUEST_LOADER.load([
  404. {"from": "127.0.0.1", "action": "DROP"}])
  405. self.check_transfer_acl(acl_setter)
  406. def test_transfer_zoneacl_nomatch(self):
  407. # similar to the previous one, but the per zone doesn't match the
  408. # query. The default should be used.
  409. def acl_setter(acl):
  410. zone_key = ('IN', 'example.org.')
  411. self.xfrsess._zone_config[zone_key] = {}
  412. self.xfrsess._zone_config[zone_key]['transfer_acl'] = \
  413. isc.acl.dns.REQUEST_LOADER.load([
  414. {"from": "127.0.0.1", "action": "DROP"}])
  415. self.xfrsess._acl = acl
  416. self.check_transfer_acl(acl_setter)
  417. def test_get_transfer_acl(self):
  418. # set the default ACL. If there's no specific zone ACL, this one
  419. # should be used.
  420. self.xfrsess._acl = isc.acl.dns.REQUEST_LOADER.load([
  421. {"from": "127.0.0.1", "action": "ACCEPT"}])
  422. acl = self.xfrsess._get_transfer_acl(Name('example.com'), RRClass.IN())
  423. self.assertEqual(acl, self.xfrsess._acl)
  424. # install a per zone config with transfer ACL for example.com. Then
  425. # that ACL will be used for example.com; for others the default ACL
  426. # will still be used.
  427. com_acl = isc.acl.dns.REQUEST_LOADER.load([
  428. {"from": "127.0.0.1", "action": "REJECT"}])
  429. self.xfrsess._zone_config[('IN', 'example.com.')] = {}
  430. self.xfrsess._zone_config[('IN', 'example.com.')]['transfer_acl'] = \
  431. com_acl
  432. self.assertEqual(com_acl,
  433. self.xfrsess._get_transfer_acl(Name('example.com'),
  434. RRClass.IN()))
  435. self.assertEqual(self.xfrsess._acl,
  436. self.xfrsess._get_transfer_acl(Name('example.org'),
  437. RRClass.IN()))
  438. # Name matching should be case insensitive.
  439. self.assertEqual(com_acl,
  440. self.xfrsess._get_transfer_acl(Name('EXAMPLE.COM'),
  441. RRClass.IN()))
  442. def test_send_data(self):
  443. self.xfrsess._send_data(self.sock, self.mdata)
  444. senddata = self.sock.readsent()
  445. self.assertEqual(senddata, self.mdata)
  446. def test_reply_xfrout_query_with_error_rcode(self):
  447. msg = self.getmsg()
  448. self.xfrsess._reply_query_with_error_rcode(msg, self.sock, Rcode(3))
  449. get_msg = self.sock.read_msg()
  450. self.assertEqual(get_msg.get_rcode().to_text(), "NXDOMAIN")
  451. # tsig signed message
  452. msg = self.getmsg()
  453. self.xfrsess._tsig_ctx = self.create_mock_tsig_ctx(TSIGError.NOERROR)
  454. self.xfrsess._reply_query_with_error_rcode(msg, self.sock, Rcode(3))
  455. get_msg = self.sock.read_msg()
  456. self.assertEqual(get_msg.get_rcode().to_text(), "NXDOMAIN")
  457. self.assertTrue(self.message_has_tsig(get_msg))
  458. def test_send_message(self):
  459. msg = self.getmsg()
  460. msg.make_response()
  461. # SOA record data with different cases
  462. soa_rrset = RRset(Name('Example.com.'), RRClass.IN(), RRType.SOA(),
  463. RRTTL(3600))
  464. soa_rrset.add_rdata(Rdata(RRType.SOA(), RRClass.IN(),
  465. 'master.Example.com. admin.exAmple.com. ' +
  466. '2011112001 3600 1800 2419200 7200'))
  467. msg.add_rrset(Message.SECTION_ANSWER, soa_rrset)
  468. self.xfrsess._send_message(self.sock, msg)
  469. send_out_data = self.sock.readsent()[2:]
  470. # CASE_INSENSITIVE compression mode
  471. render = MessageRenderer();
  472. render.set_length_limit(XFROUT_MAX_MESSAGE_SIZE)
  473. msg.to_wire(render)
  474. self.assertNotEqual(render.get_data(), send_out_data)
  475. # CASE_SENSITIVE compression mode
  476. render.clear()
  477. render.set_compress_mode(MessageRenderer.CASE_SENSITIVE)
  478. render.set_length_limit(XFROUT_MAX_MESSAGE_SIZE)
  479. msg.to_wire(render)
  480. self.assertEqual(render.get_data(), send_out_data)
  481. def test_clear_message(self):
  482. msg = self.getmsg()
  483. qid = msg.get_qid()
  484. opcode = msg.get_opcode()
  485. rcode = msg.get_rcode()
  486. self.xfrsess._clear_message(msg)
  487. self.assertEqual(msg.get_qid(), qid)
  488. self.assertEqual(msg.get_opcode(), opcode)
  489. self.assertEqual(msg.get_rcode(), rcode)
  490. self.assertTrue(msg.get_header_flag(Message.HEADERFLAG_AA))
  491. def test_send_message_with_last_soa(self):
  492. msg = self.getmsg()
  493. msg.make_response()
  494. self.xfrsess._send_message_with_last_soa(msg, self.sock,
  495. self.soa_rrset, 0)
  496. get_msg = self.sock.read_msg()
  497. # tsig context does not exist
  498. self.assertFalse(self.message_has_tsig(get_msg))
  499. self.assertEqual(get_msg.get_rr_count(Message.SECTION_QUESTION), 1)
  500. self.assertEqual(get_msg.get_rr_count(Message.SECTION_ANSWER), 1)
  501. self.assertEqual(get_msg.get_rr_count(Message.SECTION_AUTHORITY), 0)
  502. answer = get_msg.get_section(Message.SECTION_ANSWER)[0]
  503. self.assertEqual(answer.get_name().to_text(), "example.com.")
  504. self.assertEqual(answer.get_class(), RRClass("IN"))
  505. self.assertEqual(answer.get_type().to_text(), "SOA")
  506. rdata = answer.get_rdata()
  507. self.assertEqual(rdata[0], self.soa_rrset.get_rdata()[0])
  508. # Sending the message with last soa together
  509. self.xfrsess._send_message_with_last_soa(msg, self.sock,
  510. self.soa_rrset, 0)
  511. get_msg = self.sock.read_msg()
  512. # tsig context does not exist
  513. self.assertFalse(self.message_has_tsig(get_msg))
  514. def test_send_message_with_last_soa_with_tsig(self):
  515. # create tsig context
  516. self.xfrsess._tsig_ctx = self.create_mock_tsig_ctx(TSIGError.NOERROR)
  517. msg = self.getmsg()
  518. msg.make_response()
  519. # Sending the message with last soa together
  520. self.xfrsess._send_message_with_last_soa(msg, self.sock,
  521. self.soa_rrset, 0)
  522. get_msg = self.sock.read_msg()
  523. self.assertTrue(self.message_has_tsig(get_msg))
  524. self.assertEqual(get_msg.get_rr_count(Message.SECTION_QUESTION), 1)
  525. self.assertEqual(get_msg.get_rr_count(Message.SECTION_ANSWER), 1)
  526. self.assertEqual(get_msg.get_rr_count(Message.SECTION_AUTHORITY), 0)
  527. def test_trigger_send_message_with_last_soa(self):
  528. rrset_a = RRset(Name("example.com"), RRClass.IN(), RRType.A(), RRTTL(3600))
  529. rrset_a.add_rdata(Rdata(RRType.A(), RRClass.IN(), "192.0.2.1"))
  530. msg = self.getmsg()
  531. msg.make_response()
  532. msg.add_rrset(Message.SECTION_ANSWER, rrset_a)
  533. # length larger than MAX-len(rrset)
  534. length_need_split = xfrout.XFROUT_MAX_MESSAGE_SIZE - \
  535. get_rrset_len(self.soa_rrset) + 1
  536. # give the function a value that is larger than MAX-len(rrset)
  537. # this should have triggered the sending of two messages
  538. # (1 with the rrset we added manually, and 1 that triggered
  539. # the sending in _with_last_soa)
  540. self.xfrsess._send_message_with_last_soa(msg, self.sock,
  541. self.soa_rrset,
  542. length_need_split)
  543. get_msg = self.sock.read_msg()
  544. self.assertFalse(self.message_has_tsig(get_msg))
  545. self.assertEqual(get_msg.get_rr_count(Message.SECTION_QUESTION), 1)
  546. self.assertEqual(get_msg.get_rr_count(Message.SECTION_ANSWER), 1)
  547. self.assertEqual(get_msg.get_rr_count(Message.SECTION_AUTHORITY), 0)
  548. answer = get_msg.get_section(Message.SECTION_ANSWER)[0]
  549. self.assertEqual(answer.get_name().to_text(), "example.com.")
  550. self.assertEqual(answer.get_class(), RRClass("IN"))
  551. self.assertEqual(answer.get_type().to_text(), "A")
  552. rdata = answer.get_rdata()
  553. self.assertEqual(rdata[0].to_text(), "192.0.2.1")
  554. get_msg = self.sock.read_msg()
  555. self.assertFalse(self.message_has_tsig(get_msg))
  556. self.assertEqual(get_msg.get_rr_count(Message.SECTION_QUESTION), 0)
  557. self.assertEqual(get_msg.get_rr_count(Message.SECTION_ANSWER), 1)
  558. self.assertEqual(get_msg.get_rr_count(Message.SECTION_AUTHORITY), 0)
  559. answer = get_msg.get_section(Message.SECTION_ANSWER)[0]
  560. self.assertEqual(answer.get_name().to_text(), "example.com.")
  561. self.assertEqual(answer.get_class(), RRClass("IN"))
  562. self.assertEqual(answer.get_type().to_text(), "SOA")
  563. rdata = answer.get_rdata()
  564. self.assertEqual(rdata[0], self.soa_rrset.get_rdata()[0])
  565. # and it should not have sent anything else
  566. self.assertEqual(0, len(self.sock.sendqueue))
  567. def test_trigger_send_message_with_last_soa_with_tsig(self):
  568. self.xfrsess._tsig_ctx = self.create_mock_tsig_ctx(TSIGError.NOERROR)
  569. msg = self.getmsg()
  570. msg.make_response()
  571. msg.add_rrset(Message.SECTION_ANSWER, self.soa_rrset)
  572. # length larger than MAX-len(rrset)
  573. length_need_split = xfrout.XFROUT_MAX_MESSAGE_SIZE - \
  574. get_rrset_len(self.soa_rrset) + 1
  575. # give the function a value that is larger than MAX-len(rrset)
  576. # this should have triggered the sending of two messages
  577. # (1 with the rrset we added manually, and 1 that triggered
  578. # the sending in _with_last_soa)
  579. self.xfrsess._send_message_with_last_soa(msg, self.sock,
  580. self.soa_rrset,
  581. length_need_split)
  582. # Both messages should have TSIG RRs
  583. get_msg = self.sock.read_msg()
  584. self.assertTrue(self.message_has_tsig(get_msg))
  585. get_msg = self.sock.read_msg()
  586. self.assertTrue(self.message_has_tsig(get_msg))
  587. # and it should not have sent anything else
  588. self.assertEqual(0, len(self.sock.sendqueue))
  589. def test_get_rrset_len(self):
  590. self.assertEqual(82, get_rrset_len(self.soa_rrset))
  591. def test_xfrout_axfr_setup(self):
  592. self.xfrsess.ClientClass = MockDataSrcClient
  593. # Successful case. A zone iterator should be set up.
  594. self.assertEqual(self.xfrsess._xfrout_setup(
  595. self.getmsg(), TEST_ZONE_NAME, TEST_RRCLASS), Rcode.NOERROR())
  596. self.assertNotEqual(None, self.xfrsess._iterator)
  597. # Failure cases
  598. self.assertEqual(self.xfrsess._xfrout_setup(
  599. self.getmsg(), Name('notauth.example.com'), TEST_RRCLASS),
  600. Rcode.NOTAUTH())
  601. self.assertEqual(self.xfrsess._xfrout_setup(
  602. self.getmsg(), Name('nosoa.example.com'), TEST_RRCLASS),
  603. Rcode.SERVFAIL())
  604. self.assertEqual(self.xfrsess._xfrout_setup(
  605. self.getmsg(), Name('multisoa.example.com'), TEST_RRCLASS),
  606. Rcode.SERVFAIL())
  607. def test_xfrout_ixfr_setup(self):
  608. self.xfrsess.ClientClass = MockDataSrcClient
  609. self.set_request_type(RRType.IXFR())
  610. # Successful case of pure IXFR. A zone journal reader should be set
  611. # up.
  612. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION)
  613. self.assertEqual(self.xfrsess._xfrout_setup(
  614. self.getmsg(), TEST_ZONE_NAME, TEST_RRCLASS), Rcode.NOERROR())
  615. self.assertNotEqual(None, self.xfrsess._jnl_reader)
  616. # Successful case, but as a result of falling back to AXFR-style
  617. # IXFR. A zone iterator should be set up instead of a journal reader.
  618. self.mdata = self.create_request_data(ixfr=IXFR_NG_VERSION)
  619. self.assertEqual(self.xfrsess._xfrout_setup(
  620. self.getmsg(), TEST_ZONE_NAME, TEST_RRCLASS), Rcode.NOERROR())
  621. self.assertNotEqual(None, self.xfrsess._iterator)
  622. self.assertEqual(None, self.xfrsess._jnl_reader)
  623. # Successful case, but the requested SOA serial is greater than that of
  624. # the local SOA. Both iterator and jnl_reader should be None,
  625. # indicating that the response will contain just one SOA.
  626. self.mdata = self.create_request_data(ixfr=SOA_CURRENT_VERSION+1)
  627. self.assertEqual(self.xfrsess._xfrout_setup(
  628. self.getmsg(), TEST_ZONE_NAME, TEST_RRCLASS), Rcode.NOERROR())
  629. self.assertEqual(None, self.xfrsess._iterator)
  630. self.assertEqual(None, self.xfrsess._jnl_reader)
  631. # Similar to the previous case, but the requested serial is equal to
  632. # the local SOA.
  633. self.mdata = self.create_request_data(ixfr=SOA_CURRENT_VERSION)
  634. self.assertEqual(self.xfrsess._xfrout_setup(
  635. self.getmsg(), TEST_ZONE_NAME, TEST_RRCLASS), Rcode.NOERROR())
  636. self.assertEqual(None, self.xfrsess._iterator)
  637. self.assertEqual(None, self.xfrsess._jnl_reader)
  638. # Similar to the previous case, but the comparison should be done
  639. # based on serial number arithmetic, not as integers.
  640. zone_name = Name('maxserial.example.com') # whose SOA is 0xffffffff
  641. self.mdata = self.create_request_data(ixfr=1, zone_name=zone_name)
  642. self.assertEqual(self.xfrsess._xfrout_setup(
  643. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.NOERROR())
  644. self.assertEqual(None, self.xfrsess._iterator)
  645. self.assertEqual(None, self.xfrsess._jnl_reader)
  646. # The data source doesn't support journaling. Should fallback to AXFR.
  647. zone_name = Name('nojournal.example.com')
  648. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  649. zone_name=zone_name)
  650. self.assertEqual(self.xfrsess._xfrout_setup(
  651. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.NOERROR())
  652. self.assertNotEqual(None, self.xfrsess._iterator)
  653. # Failure cases
  654. zone_name = Name('notauth.example.com')
  655. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  656. zone_name=zone_name)
  657. self.assertEqual(self.xfrsess._xfrout_setup(
  658. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.NOTAUTH())
  659. # this is a strange case: zone's SOA will be found but the journal
  660. # reader won't be created due to 'no such zone'.
  661. zone_name = Name('notauth2.example.com')
  662. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  663. zone_name=zone_name)
  664. self.assertEqual(self.xfrsess._xfrout_setup(
  665. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.NOTAUTH())
  666. zone_name = Name('nosoa.example.com')
  667. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  668. zone_name=zone_name)
  669. self.assertEqual(self.xfrsess._xfrout_setup(
  670. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.SERVFAIL())
  671. zone_name = Name('multisoa.example.com')
  672. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  673. zone_name=zone_name)
  674. self.assertEqual(self.xfrsess._xfrout_setup(
  675. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.SERVFAIL())
  676. # query name doesn't match the SOA's owner
  677. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION)
  678. self.assertEqual(self.xfrsess._xfrout_setup(
  679. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.FORMERR())
  680. # query's RR class doesn't match the SOA's class
  681. zone_name = TEST_ZONE_NAME # make sure the name matches this time
  682. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  683. soa_class=RRClass.CH())
  684. self.assertEqual(self.xfrsess._xfrout_setup(
  685. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.FORMERR())
  686. # multiple SOA RRs
  687. self.mdata = self.create_request_data(ixfr=IXFR_OK_VERSION,
  688. num_soa=2)
  689. self.assertEqual(self.xfrsess._xfrout_setup(
  690. self.getmsg(), zone_name, TEST_RRCLASS), Rcode.FORMERR())
  691. def test_dns_xfrout_start_formerror(self):
  692. # formerror
  693. self.xfrsess.dns_xfrout_start(self.sock, b"\xd6=\x00\x00\x00\x01\x00")
  694. sent_data = self.sock.readsent()
  695. self.assertEqual(len(sent_data), 0)
  696. def test_dns_xfrout_start_notauth(self):
  697. def notauth(msg, name, rrclass):
  698. return Rcode.NOTAUTH()
  699. self.xfrsess._xfrout_setup = notauth
  700. self.xfrsess.dns_xfrout_start(self.sock, self.mdata)
  701. get_msg = self.sock.read_msg()
  702. self.assertEqual(get_msg.get_rcode().to_text(), "NOTAUTH")
  703. def test_dns_xfrout_start_datasrc_servfail(self):
  704. def internal_raise(x, y):
  705. raise isc.datasrc.Error('exception for the sake of test')
  706. self.xfrsess.ClientClass = internal_raise
  707. self.xfrsess.dns_xfrout_start(self.sock, self.mdata)
  708. self.assertEqual(self.sock.read_msg().get_rcode(), Rcode.SERVFAIL())
  709. def test_dns_xfrout_start_noerror(self):
  710. def noerror(msg, name, rrclass):
  711. return Rcode.NOERROR()
  712. self.xfrsess._xfrout_setup = noerror
  713. def myreply(msg, sock):
  714. self.sock.send(b"success")
  715. self.xfrsess._reply_xfrout_query = myreply
  716. self.xfrsess.dns_xfrout_start(self.sock, self.mdata)
  717. self.assertEqual(self.sock.readsent(), b"success")
  718. def test_reply_xfrout_query_axfr(self):
  719. self.xfrsess._soa = self.soa_rrset
  720. self.xfrsess._iterator = [self.soa_rrset]
  721. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  722. reply_msg = self.sock.read_msg()
  723. self.assertEqual(reply_msg.get_rr_count(Message.SECTION_ANSWER), 2)
  724. def test_reply_xfrout_query_axfr_with_tsig(self):
  725. rrset = RRset(Name('a.example.com'), RRClass.IN(), RRType.A(),
  726. RRTTL(3600))
  727. rrset.add_rdata(Rdata(RRType.A(), RRClass.IN(), '192.0.2.1'))
  728. global xfrout
  729. def get_rrset_len(rrset):
  730. return 65520
  731. self.xfrsess._soa = self.soa_rrset
  732. self.xfrsess._iterator = [rrset for i in range(0, 100)]
  733. xfrout.get_rrset_len = get_rrset_len
  734. self.xfrsess._tsig_ctx = self.create_mock_tsig_ctx(TSIGError.NOERROR)
  735. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  736. # All messages must have TSIG as we don't support the feature of
  737. # skipping intermediate TSIG records (with bulk signing).
  738. for i in range(0, 102): # 102 = all 100 RRs from iterator and 2 SOAs
  739. reply_msg = self.sock.read_msg()
  740. # With the hack of get_rrset_len() above, every message must have
  741. # exactly one RR in the answer section.
  742. self.assertEqual(reply_msg.get_rr_count(Message.SECTION_ANSWER), 1)
  743. self.assertTrue(self.message_has_tsig(reply_msg))
  744. # and it should not have sent anything else
  745. self.assertEqual(0, len(self.sock.sendqueue))
  746. def test_reply_xfrout_query_ixfr(self):
  747. # Creating a pure (incremental) IXFR response. Intermediate SOA
  748. # RRs won't be skipped.
  749. self.xfrsess._soa = create_soa(SOA_CURRENT_VERSION)
  750. self.xfrsess._iterator = [create_soa(IXFR_OK_VERSION),
  751. create_a(Name('a.example.com'), '192.0.2.2'),
  752. create_soa(SOA_CURRENT_VERSION),
  753. create_aaaa(Name('a.example.com'),
  754. '2001:db8::1')]
  755. self.xfrsess._jnl_reader = self.xfrsess._iterator
  756. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  757. reply_msg = self.sock.read_msg(Message.PRESERVE_ORDER)
  758. actual_records = reply_msg.get_section(Message.SECTION_ANSWER)
  759. expected_records = self.xfrsess._iterator[:]
  760. expected_records.insert(0, create_soa(SOA_CURRENT_VERSION))
  761. expected_records.append(create_soa(SOA_CURRENT_VERSION))
  762. self.assertEqual(len(expected_records), len(actual_records))
  763. for (expected_rr, actual_rr) in zip(expected_records, actual_records):
  764. self.assertTrue(rrsets_equal(expected_rr, actual_rr))
  765. def test_reply_xfrout_query_axfr_maxlen(self):
  766. # The test RR(set) has the length of 65535 - 12 (size of hdr) bytes:
  767. # owner name = 1 (root), fixed fields (type,class,TTL,RDLEN) = 10
  768. # RDATA = 65512 (= 65535 - 12 - 1 - 10)
  769. self.xfrsess._soa = self.soa_rrset
  770. test_rr = create_generic(Name('.'), 65512)
  771. self.xfrsess._iterator = [self.soa_rrset, test_rr]
  772. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  773. # The first message should contain the beginning SOA, and only that RR
  774. r = self.sock.read_msg()
  775. self.assertEqual(1, r.get_rr_count(Message.SECTION_ANSWER))
  776. self.assertTrue(rrsets_equal(self.soa_rrset,
  777. r.get_section(Message.SECTION_ANSWER)[0]))
  778. # The second message should contain the beginning SOA, and only that RR
  779. # The wire format data should have the possible maximum size.
  780. r, rlen = self.sock.read_msg(need_len=True)
  781. self.assertEqual(65535, rlen)
  782. self.assertEqual(1, r.get_rr_count(Message.SECTION_ANSWER))
  783. self.assertTrue(rrsets_equal(test_rr,
  784. r.get_section(Message.SECTION_ANSWER)[0]))
  785. # The third message should contain the ending SOA, and only that RR
  786. r = self.sock.read_msg()
  787. self.assertEqual(1, r.get_rr_count(Message.SECTION_ANSWER))
  788. self.assertTrue(rrsets_equal(self.soa_rrset,
  789. r.get_section(Message.SECTION_ANSWER)[0]))
  790. # there should be no more message
  791. self.assertEqual(0, len(self.sock.sendqueue))
  792. def maxlen_test_common_setup(self, tsig=False):
  793. '''Common initialization for some of the tests below
  794. For those tests we use '.' for all owner names and names in RDATA
  795. to avoid having unexpected results due to compression. It returns
  796. the created SOA for convenience.
  797. If tsig is True, also setup TSIG (mock) context. In our test cases
  798. the size of the TSIG RR is 81 bytes (key name = example.com,
  799. algorithm = hmac-md5)
  800. '''
  801. soa = RRset(Name('.'), RRClass.IN(), RRType.SOA(), RRTTL(3600))
  802. soa.add_rdata(Rdata(RRType.SOA(), RRClass.IN(), '. . 0 0 0 0 0'))
  803. self.mdata = self.create_request_data(zone_name=Name('.'))
  804. self.xfrsess._soa = soa
  805. if tsig:
  806. self.xfrsess._tsig_ctx = \
  807. self.create_mock_tsig_ctx(TSIGError.NOERROR)
  808. self.xfrsess._tsig_len = 81
  809. return soa
  810. def maxlen_test_common_checks(self, soa_rr, test_rr, expected_n_rr):
  811. '''A set of common assertion checks for some tests below.
  812. In all cases two AXFR response messages should have been created.
  813. expected_n_rr is a list of two elements, each specifies the expected
  814. number of answer RRs for each message: expected_n_rr[0] is the expected
  815. number of the first answer RRs; expected_n_rr[1] is the expected number
  816. of the second answer RRs. The message that contains two RRs should
  817. have the maximum possible wire length (65535 bytes). And, in all
  818. cases, the resulting RRs should be in the order of SOA, another RR,
  819. SOA.
  820. '''
  821. # Check the first message
  822. r, rlen = self.sock.read_msg(need_len=True)
  823. if expected_n_rr[0] == 2:
  824. self.assertEqual(65535, rlen)
  825. self.assertEqual(expected_n_rr[0],
  826. r.get_rr_count(Message.SECTION_ANSWER))
  827. actual_rrs = r.get_section(Message.SECTION_ANSWER)[:]
  828. # Check the second message
  829. r, rlen = self.sock.read_msg(need_len=True)
  830. if expected_n_rr[1] == 2:
  831. self.assertEqual(65535, rlen)
  832. self.assertEqual(expected_n_rr[1],
  833. r.get_rr_count(Message.SECTION_ANSWER))
  834. actual_rrs.extend(r.get_section(Message.SECTION_ANSWER))
  835. for (expected_rr, actual_rr) in zip([soa_rr, test_rr, soa_rr],
  836. actual_rrs):
  837. self.assertTrue(rrsets_equal(expected_rr, actual_rr))
  838. # there should be no more message
  839. self.assertEqual(0, len(self.sock.sendqueue))
  840. def test_reply_xfrout_query_axfr_maxlen_with_soa(self):
  841. # Similar to the 'maxlen' test, but the first message should be
  842. # able to contain both SOA and the large RR.
  843. soa = self.maxlen_test_common_setup()
  844. # The first message will contain the question (5 bytes), so the
  845. # test RDATA should allow a room for that.
  846. test_rr = create_generic(Name('.'), 65512 - 5 - get_rrset_len(soa))
  847. self.xfrsess._iterator = [soa, test_rr]
  848. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  849. self.maxlen_test_common_checks(soa, test_rr, [2, 1])
  850. def test_reply_xfrout_query_axfr_maxlen_with_soa_with_tsig(self):
  851. # Similar to the previous case, but with TSIG (whose size is 81 bytes).
  852. soa = self.maxlen_test_common_setup(True)
  853. test_rr = create_generic(Name('.'), 65512 - 5 - 81 -
  854. get_rrset_len(soa))
  855. self.xfrsess._iterator = [soa, test_rr]
  856. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  857. self.maxlen_test_common_checks(soa, test_rr, [2, 1])
  858. def test_reply_xfrout_query_axfr_maxlen_with_endsoa(self):
  859. # Similar to the max w/ soa test, but the first message cannot contain
  860. # both SOA and the long RR due to the question section. The second
  861. # message should be able to contain both.
  862. soa = self.maxlen_test_common_setup()
  863. test_rr = create_generic(Name('.'), 65512 - get_rrset_len(soa))
  864. self.xfrsess._iterator = [soa, test_rr]
  865. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  866. self.maxlen_test_common_checks(soa, test_rr, [1, 2])
  867. def test_reply_xfrout_query_axfr_maxlen_with_endsoa_with_tsig(self):
  868. # Similar to the previous case, but with TSIG.
  869. soa = self.maxlen_test_common_setup(True)
  870. test_rr = create_generic(Name('.'), 65512 - 81 - get_rrset_len(soa))
  871. self.xfrsess._iterator = [soa, test_rr]
  872. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  873. self.maxlen_test_common_checks(soa, test_rr, [1, 2])
  874. def test_reply_xfrout_query_axfr_toobigdata(self):
  875. # Similar to the 'maxlen' test, but the RR doesn't even fit in a
  876. # single message.
  877. self.xfrsess._soa = self.soa_rrset
  878. test_rr = create_generic(Name('.'), 65513) # 1 byte larger than 'max'
  879. self.xfrsess._iterator = [self.soa_rrset, test_rr]
  880. # the reply method should fail with exception
  881. self.assertRaises(XfroutSessionError, self.xfrsess._reply_xfrout_query,
  882. self.getmsg(), self.sock)
  883. # The first message should still have been sent and contain the
  884. # beginning SOA, and only that RR
  885. r = self.sock.read_msg()
  886. self.assertEqual(1, r.get_rr_count(Message.SECTION_ANSWER))
  887. self.assertTrue(rrsets_equal(self.soa_rrset,
  888. r.get_section(Message.SECTION_ANSWER)[0]))
  889. # And there should have been no other messages sent
  890. self.assertEqual(0, len(self.sock.sendqueue))
  891. def test_reply_xfrout_query_ixfr_soa_only(self):
  892. # Creating an IXFR response that contains only one RR, which is the
  893. # SOA of the current version.
  894. self.xfrsess._soa = create_soa(SOA_CURRENT_VERSION)
  895. self.xfrsess._iterator = None
  896. self.xfrsess._jnl_reader = None
  897. self.xfrsess._reply_xfrout_query(self.getmsg(), self.sock)
  898. reply_msg = self.sock.read_msg(Message.PRESERVE_ORDER)
  899. answer = reply_msg.get_section(Message.SECTION_ANSWER)
  900. self.assertEqual(1, len(answer))
  901. self.assertTrue(rrsets_equal(create_soa(SOA_CURRENT_VERSION),
  902. answer[0]))
  903. class TestXfroutSessionWithSQLite3(TestXfroutSessionBase):
  904. '''Tests for XFR-out sessions using an SQLite3 DB.
  905. These are provided mainly to confirm the implementation actually works
  906. in an environment closer to actual operational environments. So we
  907. only check a few common cases; other details are tested using mock
  908. data sources.
  909. '''
  910. def setUp(self):
  911. super().setUp()
  912. self.xfrsess._request_data = self.mdata
  913. self.xfrsess._server.get_db_file = lambda : TESTDATA_SRCDIR + \
  914. 'test.sqlite3'
  915. self.ns_name = 'a.dns.example.com'
  916. def check_axfr_stream(self, response):
  917. '''Common checks for AXFR(-style) response for the test zone.
  918. '''
  919. # This zone contains two A RRs for the same name with different TTLs.
  920. # These TTLs should be preseved in the AXFR stream.
  921. actual_records = response.get_section(Message.SECTION_ANSWER)
  922. self.assertEqual(5, len(actual_records))
  923. # The first and last RR should be the expected SOA
  924. expected_soa = create_soa(2011112001)
  925. self.assertTrue(rrsets_equal(expected_soa, actual_records[0]))
  926. self.assertTrue(rrsets_equal(expected_soa, actual_records[-1]))
  927. # The ordering of the intermediate RRs can differ depending on the
  928. # internal details of the SQLite3 library, so we sort them by a simple
  929. # rule sufficient for the purpose here, and then compare them.
  930. expected_others = [create_ns(self.ns_name),
  931. create_a(Name(self.ns_name), '192.0.2.1', 3600),
  932. create_a(Name(self.ns_name), '192.0.2.2', 7200)]
  933. keyfn = lambda x: (x.get_type(), x.get_ttl())
  934. for (expected_rr, actual_rr) in zip(sorted(expected_others, key=keyfn),
  935. sorted(actual_records[1:4],
  936. key=keyfn)):
  937. self.assertTrue(rrsets_equal(expected_rr, actual_rr))
  938. def test_axfr_normal_session(self):
  939. XfroutSession._handle(self.xfrsess)
  940. response = self.sock.read_msg(Message.PRESERVE_ORDER);
  941. self.assertEqual(Rcode.NOERROR(), response.get_rcode())
  942. self.check_axfr_stream(response)
  943. def test_ixfr_to_axfr(self):
  944. self.xfrsess._request_data = \
  945. self.create_request_data(ixfr=IXFR_NG_VERSION)
  946. XfroutSession._handle(self.xfrsess)
  947. response = self.sock.read_msg(Message.PRESERVE_ORDER);
  948. self.assertEqual(Rcode.NOERROR(), response.get_rcode())
  949. # This is an AXFR-style IXFR. So the question section should indicate
  950. # that it's an IXFR resposne.
  951. self.assertEqual(RRType.IXFR(), response.get_question()[0].get_type())
  952. self.check_axfr_stream(response)
  953. def test_ixfr_normal_session(self):
  954. # See testdata/creatediff.py. There are 8 changes between two
  955. # versions. So the answer section should contain all of these and
  956. # two beginning and trailing SOAs.
  957. self.xfrsess._request_data = \
  958. self.create_request_data(ixfr=IXFR_OK_VERSION)
  959. XfroutSession._handle(self.xfrsess)
  960. response = self.sock.read_msg(Message.PRESERVE_ORDER)
  961. actual_records = response.get_section(Message.SECTION_ANSWER)
  962. expected_records = [create_soa(2011112001), create_soa(2011111802),
  963. create_soa(2011111900),
  964. create_a(Name(self.ns_name), '192.0.2.2', 7200),
  965. create_soa(2011111900),
  966. create_a(Name(self.ns_name), '192.0.2.53'),
  967. create_aaaa(Name(self.ns_name), '2001:db8::1'),
  968. create_soa(2011112001),
  969. create_a(Name(self.ns_name), '192.0.2.1'),
  970. create_soa(2011112001)]
  971. self.assertEqual(len(expected_records), len(actual_records))
  972. for (expected_rr, actual_rr) in zip(expected_records, actual_records):
  973. self.assertTrue(rrsets_equal(expected_rr, actual_rr))
  974. def ixfr_soa_only_common_checks(self, request_serial):
  975. self.xfrsess._request_data = \
  976. self.create_request_data(ixfr=request_serial)
  977. XfroutSession._handle(self.xfrsess)
  978. response = self.sock.read_msg(Message.PRESERVE_ORDER);
  979. answers = response.get_section(Message.SECTION_ANSWER)
  980. self.assertEqual(1, len(answers))
  981. self.assertTrue(rrsets_equal(create_soa(SOA_CURRENT_VERSION),
  982. answers[0]))
  983. def test_ixfr_soa_only(self):
  984. # The requested SOA serial is the latest one. The response should
  985. # contain exactly one SOA of that serial.
  986. self.ixfr_soa_only_common_checks(SOA_CURRENT_VERSION)
  987. def test_ixfr_soa_only2(self):
  988. # Similar to the previous test, but the requested SOA is larger than
  989. # the current.
  990. self.ixfr_soa_only_common_checks(SOA_CURRENT_VERSION + 1)
  991. class MyUnixSockServer(UnixSockServer):
  992. def __init__(self):
  993. self._shutdown_event = threading.Event()
  994. self._common_init()
  995. self._cc = MyCCSession()
  996. self.update_config_data(self._cc.get_full_config())
  997. class TestUnixSockServer(unittest.TestCase):
  998. def setUp(self):
  999. self.write_sock, self.read_sock = socket.socketpair()
  1000. self.unix = MyUnixSockServer()
  1001. def test_guess_remote(self):
  1002. """Test we can guess the remote endpoint when we have only the
  1003. file descriptor. This is needed, because we get only that one
  1004. from auth."""
  1005. # We test with UDP, as it can be "connected" without other
  1006. # endpoint. Note that in the current implementation _guess_remote()
  1007. # unconditionally returns SOCK_STREAM.
  1008. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  1009. sock.connect(('127.0.0.1', 12345))
  1010. self.assertEqual((socket.AF_INET, socket.SOCK_STREAM,
  1011. ('127.0.0.1', 12345)),
  1012. self.unix._guess_remote(sock.fileno()))
  1013. if socket.has_ipv6:
  1014. # Don't check IPv6 address on hosts not supporting them
  1015. sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
  1016. sock.connect(('::1', 12345))
  1017. self.assertEqual((socket.AF_INET6, socket.SOCK_STREAM,
  1018. ('::1', 12345, 0, 0)),
  1019. self.unix._guess_remote(sock.fileno()))
  1020. # Try when pretending there's no IPv6 support
  1021. # (No need to pretend when there's really no IPv6)
  1022. xfrout.socket.has_ipv6 = False
  1023. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  1024. sock.connect(('127.0.0.1', 12345))
  1025. self.assertEqual((socket.AF_INET, socket.SOCK_STREAM,
  1026. ('127.0.0.1', 12345)),
  1027. self.unix._guess_remote(sock.fileno()))
  1028. # Return it back
  1029. xfrout.socket.has_ipv6 = True
  1030. def test_receive_query_message(self):
  1031. send_msg = b"\xd6=\x00\x00\x00\x01\x00"
  1032. msg_len = struct.pack('H', socket.htons(len(send_msg)))
  1033. self.write_sock.send(msg_len)
  1034. self.write_sock.send(send_msg)
  1035. recv_msg = self.unix._receive_query_message(self.read_sock)
  1036. self.assertEqual(recv_msg, send_msg)
  1037. def check_default_ACL(self):
  1038. context = isc.acl.dns.RequestContext(socket.getaddrinfo("127.0.0.1",
  1039. 1234, 0, socket.SOCK_DGRAM,
  1040. socket.IPPROTO_UDP,
  1041. socket.AI_NUMERICHOST)[0][4])
  1042. self.assertEqual(isc.acl.acl.ACCEPT, self.unix._acl.execute(context))
  1043. def check_loaded_ACL(self, acl):
  1044. context = isc.acl.dns.RequestContext(socket.getaddrinfo("127.0.0.1",
  1045. 1234, 0, socket.SOCK_DGRAM,
  1046. socket.IPPROTO_UDP,
  1047. socket.AI_NUMERICHOST)[0][4])
  1048. self.assertEqual(isc.acl.acl.ACCEPT, acl.execute(context))
  1049. context = isc.acl.dns.RequestContext(socket.getaddrinfo("192.0.2.1",
  1050. 1234, 0, socket.SOCK_DGRAM,
  1051. socket.IPPROTO_UDP,
  1052. socket.AI_NUMERICHOST)[0][4])
  1053. self.assertEqual(isc.acl.acl.REJECT, acl.execute(context))
  1054. def test_update_config_data(self):
  1055. self.check_default_ACL()
  1056. tsig_key_str = 'example.com:SFuWd/q99SzF8Yzd1QbB9g=='
  1057. tsig_key_list = [tsig_key_str]
  1058. bad_key_list = ['bad..example.com:SFuWd/q99SzF8Yzd1QbB9g==']
  1059. self.unix.update_config_data({'transfers_out':10 })
  1060. self.assertEqual(self.unix._max_transfers_out, 10)
  1061. self.assertTrue(self.unix.tsig_key_ring is not None)
  1062. self.check_default_ACL()
  1063. self.unix.update_config_data({'transfers_out':9,
  1064. 'tsig_key_ring':tsig_key_list})
  1065. self.assertEqual(self.unix._max_transfers_out, 9)
  1066. self.assertEqual(self.unix.tsig_key_ring.size(), 1)
  1067. self.unix.tsig_key_ring.remove(Name("example.com."))
  1068. self.assertEqual(self.unix.tsig_key_ring.size(), 0)
  1069. # bad tsig key
  1070. config_data = {'transfers_out':9, 'tsig_key_ring': bad_key_list}
  1071. self.assertRaises(None, self.unix.update_config_data(config_data))
  1072. self.assertEqual(self.unix.tsig_key_ring.size(), 0)
  1073. # Load the ACL
  1074. self.unix.update_config_data({'transfer_acl': [{'from': '127.0.0.1',
  1075. 'action': 'ACCEPT'}]})
  1076. self.check_loaded_ACL(self.unix._acl)
  1077. # Pass a wrong data there and check it does not replace the old one
  1078. self.assertRaises(XfroutConfigError,
  1079. self.unix.update_config_data,
  1080. {'transfer_acl': ['Something bad']})
  1081. self.check_loaded_ACL(self.unix._acl)
  1082. def test_zone_config_data(self):
  1083. # By default, there's no specific zone config
  1084. self.assertEqual({}, self.unix._zone_config)
  1085. # Adding config for a specific zone. The config is empty unless
  1086. # explicitly specified.
  1087. self.unix.update_config_data({'zone_config':
  1088. [{'origin': 'example.com',
  1089. 'class': 'IN'}]})
  1090. self.assertEqual({}, self.unix._zone_config[('IN', 'example.com.')])
  1091. # zone class can be omitted
  1092. self.unix.update_config_data({'zone_config':
  1093. [{'origin': 'example.com'}]})
  1094. self.assertEqual({}, self.unix._zone_config[('IN', 'example.com.')])
  1095. # zone class, name are stored in the "normalized" form. class
  1096. # strings are upper cased, names are down cased.
  1097. self.unix.update_config_data({'zone_config':
  1098. [{'origin': 'EXAMPLE.com'}]})
  1099. self.assertEqual({}, self.unix._zone_config[('IN', 'example.com.')])
  1100. # invalid zone class, name will result in exceptions
  1101. self.assertRaises(EmptyLabel,
  1102. self.unix.update_config_data,
  1103. {'zone_config': [{'origin': 'bad..example'}]})
  1104. self.assertRaises(InvalidRRClass,
  1105. self.unix.update_config_data,
  1106. {'zone_config': [{'origin': 'example.com',
  1107. 'class': 'badclass'}]})
  1108. # Configuring a couple of more zones
  1109. self.unix.update_config_data({'zone_config':
  1110. [{'origin': 'example.com'},
  1111. {'origin': 'example.com',
  1112. 'class': 'CH'},
  1113. {'origin': 'example.org'}]})
  1114. self.assertEqual({}, self.unix._zone_config[('IN', 'example.com.')])
  1115. self.assertEqual({}, self.unix._zone_config[('CH', 'example.com.')])
  1116. self.assertEqual({}, self.unix._zone_config[('IN', 'example.org.')])
  1117. # Duplicate data: should be rejected with an exception
  1118. self.assertRaises(XfroutConfigError,
  1119. self.unix.update_config_data,
  1120. {'zone_config': [{'origin': 'example.com'},
  1121. {'origin': 'example.org'},
  1122. {'origin': 'example.com'}]})
  1123. def test_zone_config_data_with_acl(self):
  1124. # Similar to the previous test, but with transfer_acl config
  1125. self.unix.update_config_data({'zone_config':
  1126. [{'origin': 'example.com',
  1127. 'transfer_acl':
  1128. [{'from': '127.0.0.1',
  1129. 'action': 'ACCEPT'}]}]})
  1130. acl = self.unix._zone_config[('IN', 'example.com.')]['transfer_acl']
  1131. self.check_loaded_ACL(acl)
  1132. # invalid ACL syntax will be rejected with exception
  1133. self.assertRaises(XfroutConfigError,
  1134. self.unix.update_config_data,
  1135. {'zone_config': [{'origin': 'example.com',
  1136. 'transfer_acl':
  1137. [{'action': 'BADACTION'}]}]})
  1138. def test_get_db_file(self):
  1139. self.assertEqual(self.unix.get_db_file(), "initdb.file")
  1140. def test_increase_transfers_counter(self):
  1141. self.unix._max_transfers_out = 10
  1142. count = self.unix._transfers_counter
  1143. self.assertEqual(self.unix.increase_transfers_counter(), True)
  1144. self.assertEqual(count + 1, self.unix._transfers_counter)
  1145. self.unix._max_transfers_out = 0
  1146. count = self.unix._transfers_counter
  1147. self.assertEqual(self.unix.increase_transfers_counter(), False)
  1148. self.assertEqual(count, self.unix._transfers_counter)
  1149. def test_decrease_transfers_counter(self):
  1150. count = self.unix._transfers_counter
  1151. self.unix.decrease_transfers_counter()
  1152. self.assertEqual(count - 1, self.unix._transfers_counter)
  1153. def _remove_file(self, sock_file):
  1154. try:
  1155. os.remove(sock_file)
  1156. except OSError:
  1157. pass
  1158. def test_sock_file_in_use_file_exist(self):
  1159. sock_file = 'temp.sock.file'
  1160. self._remove_file(sock_file)
  1161. self.assertFalse(self.unix._sock_file_in_use(sock_file))
  1162. self.assertFalse(os.path.exists(sock_file))
  1163. def test_sock_file_in_use_file_not_exist(self):
  1164. self.assertFalse(self.unix._sock_file_in_use('temp.sock.file'))
  1165. def _start_unix_sock_server(self, sock_file):
  1166. serv = ThreadingUnixStreamServer(sock_file, BaseRequestHandler)
  1167. serv_thread = threading.Thread(target=serv.serve_forever)
  1168. serv_thread.setDaemon(True)
  1169. serv_thread.start()
  1170. def test_sock_file_in_use(self):
  1171. sock_file = 'temp.sock.file'
  1172. self._remove_file(sock_file)
  1173. self.assertFalse(self.unix._sock_file_in_use(sock_file))
  1174. self._start_unix_sock_server(sock_file)
  1175. old_stdout = sys.stdout
  1176. sys.stdout = open(os.devnull, 'w')
  1177. self.assertTrue(self.unix._sock_file_in_use(sock_file))
  1178. sys.stdout = old_stdout
  1179. def test_remove_unused_sock_file_in_use(self):
  1180. sock_file = 'temp.sock.file'
  1181. self._remove_file(sock_file)
  1182. self.assertFalse(self.unix._sock_file_in_use(sock_file))
  1183. self._start_unix_sock_server(sock_file)
  1184. old_stdout = sys.stdout
  1185. sys.stdout = open(os.devnull, 'w')
  1186. try:
  1187. self.unix._remove_unused_sock_file(sock_file)
  1188. except SystemExit:
  1189. pass
  1190. else:
  1191. # This should never happen
  1192. self.assertTrue(False)
  1193. sys.stdout = old_stdout
  1194. def test_remove_unused_sock_file_dir(self):
  1195. import tempfile
  1196. dir_name = tempfile.mkdtemp()
  1197. old_stdout = sys.stdout
  1198. sys.stdout = open(os.devnull, 'w')
  1199. try:
  1200. self.unix._remove_unused_sock_file(dir_name)
  1201. except SystemExit:
  1202. pass
  1203. else:
  1204. # This should never happen
  1205. self.assertTrue(False)
  1206. sys.stdout = old_stdout
  1207. os.rmdir(dir_name)
  1208. class TestInitialization(unittest.TestCase):
  1209. def setEnv(self, name, value):
  1210. if value is None:
  1211. if name in os.environ:
  1212. del os.environ[name]
  1213. else:
  1214. os.environ[name] = value
  1215. def setUp(self):
  1216. self._oldSocket = os.getenv("BIND10_XFROUT_SOCKET_FILE")
  1217. self._oldFromBuild = os.getenv("B10_FROM_BUILD")
  1218. def tearDown(self):
  1219. self.setEnv("B10_FROM_BUILD", self._oldFromBuild)
  1220. self.setEnv("BIND10_XFROUT_SOCKET_FILE", self._oldSocket)
  1221. # Make sure even the computed values are back
  1222. xfrout.init_paths()
  1223. def testNoEnv(self):
  1224. self.setEnv("B10_FROM_BUILD", None)
  1225. self.setEnv("BIND10_XFROUT_SOCKET_FILE", None)
  1226. xfrout.init_paths()
  1227. self.assertEqual(xfrout.UNIX_SOCKET_FILE,
  1228. "@@LOCALSTATEDIR@@/@PACKAGE_NAME@/auth_xfrout_conn")
  1229. def testProvidedSocket(self):
  1230. self.setEnv("B10_FROM_BUILD", None)
  1231. self.setEnv("BIND10_XFROUT_SOCKET_FILE", "The/Socket/File")
  1232. xfrout.init_paths()
  1233. self.assertEqual(xfrout.UNIX_SOCKET_FILE, "The/Socket/File")
  1234. class MyNotifier():
  1235. def __init__(self):
  1236. self.shutdown_called = False
  1237. def shutdown(self):
  1238. self.shutdown_called = True
  1239. class MyXfroutServer(XfroutServer):
  1240. def __init__(self):
  1241. self._cc = MockModuleCCSession()
  1242. self._shutdown_event = threading.Event()
  1243. self._notifier = MyNotifier()
  1244. self._unix_socket_server = None
  1245. # Disable the wait for threads
  1246. self._wait_for_threads = lambda : None
  1247. class TestXfroutServer(unittest.TestCase):
  1248. def setUp(self):
  1249. self.xfrout_server = MyXfroutServer()
  1250. def test_shutdown(self):
  1251. self.xfrout_server.shutdown()
  1252. self.assertTrue(self.xfrout_server._notifier.shutdown_called)
  1253. self.assertTrue(self.xfrout_server._cc.stopped)
  1254. if __name__== "__main__":
  1255. isc.log.resetUnitTestRootLogger()
  1256. unittest.main()