ddns_test.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. # Copyright (C) 2011 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 DDNS module'''
  16. from isc.ddns.session import *
  17. from isc.dns import *
  18. from isc.acl.acl import ACCEPT
  19. import isc.util.cio.socketsession
  20. from isc.cc.session import SessionTimeout, SessionError, ProtocolError
  21. from isc.datasrc import DataSourceClient
  22. from isc.config import module_spec_from_file
  23. from isc.config.config_data import ConfigData
  24. from isc.config.ccsession import create_answer, ModuleCCSessionError
  25. from isc.config.module_spec import ModuleSpecError
  26. from isc.server_common.dns_tcp import DNSTCPContext
  27. import ddns
  28. import errno
  29. import os
  30. import select
  31. import shutil
  32. import socket
  33. import unittest
  34. # Some common test parameters
  35. TESTDATA_PATH = os.environ['TESTDATA_PATH'] + os.sep
  36. READ_ZONE_DB_FILE = TESTDATA_PATH + "rwtest.sqlite3" # original, to be copied
  37. TEST_ZONE_NAME = Name('example.org')
  38. TEST_ZONE_NAME_STR = TEST_ZONE_NAME.to_text()
  39. UPDATE_RRTYPE = RRType.SOA
  40. TEST_QID = 5353 # arbitrarily chosen
  41. TEST_RRCLASS = RRClass.IN
  42. TEST_RRCLASS_STR = TEST_RRCLASS.to_text()
  43. TEST_SERVER6 = ('2001:db8::53', 53, 0, 0)
  44. TEST_CLIENT6 = ('2001:db8::1', 53000, 0, 0)
  45. TEST_SERVER4 = ('192.0.2.53', 53)
  46. TEST_CLIENT4 = ('192.0.2.1', 53534)
  47. TEST_ZONE_RECORD = Question(TEST_ZONE_NAME, TEST_RRCLASS, UPDATE_RRTYPE)
  48. TEST_ACL_CONTEXT = isc.acl.dns.RequestContext(
  49. socket.getaddrinfo("192.0.2.1", 1234, 0, socket.SOCK_DGRAM,
  50. socket.IPPROTO_UDP, socket.AI_NUMERICHOST)[0][4])
  51. # TSIG key for tests when needed. The key name is TEST_ZONE_NAME.
  52. TEST_TSIG_KEY = TSIGKey("example.org:SFuWd/q99SzF8Yzd1QbB9g==")
  53. # TSIG keyring that contains the test key
  54. TEST_TSIG_KEYRING = TSIGKeyRing()
  55. TEST_TSIG_KEYRING.add(TEST_TSIG_KEY)
  56. # Another TSIG key not in the keyring, making verification fail
  57. BAD_TSIG_KEY = TSIGKey("example.com:SFuWd/q99SzF8Yzd1QbB9g==")
  58. # Incorporate it so we can use the real default values of zonemgr config
  59. # in the tests.
  60. ZONEMGR_MODULE_SPEC = module_spec_from_file(
  61. os.environ["B10_FROM_BUILD"] + "/src/bin/zonemgr/zonemgr.spec")
  62. class FakeSocket:
  63. """
  64. A fake socket. It only provides a file number, peer name and accept method.
  65. """
  66. def __init__(self, fileno, proto=socket.IPPROTO_UDP):
  67. self.proto = proto
  68. self.__fileno = fileno
  69. self._sent_data = None
  70. self._sent_addr = None
  71. self._close_called = 0 # number of calls to close()
  72. self.__send_cc = 0 # waterline of the send buffer (can be reset)
  73. # customizable by tests; if set to True, sendto() will throw after
  74. # recording the parameters.
  75. self._raise_on_send = False
  76. self._send_buflen = None # imaginary send buffer for partial send
  77. def fileno(self):
  78. return self.__fileno
  79. def getpeername(self):
  80. if self.proto == socket.IPPROTO_UDP or \
  81. self.proto == socket.IPPROTO_TCP:
  82. return TEST_CLIENT4
  83. return "fake_unix_socket"
  84. def accept(self):
  85. return FakeSocket(self.__fileno + 1), '/dummy/path'
  86. def sendto(self, data, addr):
  87. self._sent_data = data
  88. self._sent_addr = addr
  89. if self._raise_on_send:
  90. raise socket.error('test socket failure')
  91. def send(self, data):
  92. if self._raise_on_send:
  93. raise socket.error(errno.EPIPE, 'faked connection disruption')
  94. elif self._send_buflen is None:
  95. available_space = len(data)
  96. else:
  97. available_space = self._send_buflen - self.__send_cc
  98. if available_space == 0:
  99. # if there's no space, (assuming it's nonblocking mode) raise
  100. # EAGAIN.
  101. raise socket.error(errno.EAGAIN,
  102. "Resource temporarily unavailable")
  103. # determine the sendable part of the data, record it, update "buffer".
  104. cc = min(available_space, len(data))
  105. if self._sent_data is None:
  106. self._sent_data = data[:cc]
  107. else:
  108. self._sent_data += data[:cc]
  109. self.__send_cc += cc
  110. return cc
  111. def setblocking(self, on):
  112. # We only need a faked NO-OP implementation.
  113. pass
  114. def close(self):
  115. self._close_called += 1
  116. def clear(self):
  117. '''Clear internal instrumental data.'''
  118. self._sent_data = None
  119. self._sent_addr = None
  120. def make_send_ready(self):
  121. # pretend that the accrued data has been cleared, making room in
  122. # the send buffer.
  123. self.__send_cc = 0
  124. class FakeSessionReceiver:
  125. """
  126. A fake socket session receiver, for our tests.
  127. """
  128. def __init__(self, socket):
  129. self._socket = socket
  130. def socket(self):
  131. """
  132. This method is not present in the real receiver, but we use it to
  133. inspect the socket passed to the constructor.
  134. """
  135. return self._socket
  136. class FakeUpdateSession:
  137. '''A fake update session, emulating isc.ddns.session.UpdateSession.
  138. It provides the same interfaces as UpdateSession with skipping complicated
  139. internal protocol processing and returning given faked results. This
  140. will help simplify test setups.
  141. '''
  142. def __init__(self, msg, client_addr, zone_config, faked_result):
  143. '''Faked constructor.
  144. It takes an additional faked_result parameter. It will be used
  145. as the result value of handle(). If its value is UPDATE_ERROR,
  146. get_message() will create a response message whose Rcode is
  147. REFUSED.
  148. '''
  149. self.__msg = msg
  150. self.__faked_result = faked_result
  151. def handle(self):
  152. if self.__faked_result == UPDATE_SUCCESS:
  153. return self.__faked_result, TEST_ZONE_NAME, TEST_RRCLASS
  154. return self.__faked_result, None, None
  155. def get_message(self):
  156. self.__msg.make_response()
  157. self.__msg.clear_section(SECTION_ZONE)
  158. if self.__faked_result == UPDATE_SUCCESS:
  159. self.__msg.set_rcode(Rcode.NOERROR)
  160. else:
  161. self.__msg.set_rcode(Rcode.REFUSED)
  162. return self.__msg
  163. class FakeKeyringModule:
  164. '''Fake the entire isc.server_common.tsig_keyring module.'''
  165. def init_keyring(self, cc):
  166. '''Set the instrumental attribute to True when called.
  167. It can be used for a test that confirms TSIG key initialization is
  168. surely performed. This class doesn't use any CC session, so the
  169. cc parameter will be ignored.
  170. '''
  171. self.initialized = True
  172. def get_keyring(self):
  173. '''Simply return the predefined TSIG keyring unconditionally.'''
  174. return TEST_TSIG_KEYRING
  175. class MyCCSession(isc.config.ModuleCCSession):
  176. '''Fake session with minimal interface compliance.'''
  177. # faked CC sequence used in group_send/recvmsg
  178. FAKE_SEQUENCE = 53
  179. def __init__(self):
  180. module_spec = isc.config.module_spec_from_file(
  181. ddns.SPECFILE_LOCATION)
  182. isc.config.ConfigData.__init__(self, module_spec)
  183. self._started = False
  184. self._stopped = False
  185. # Used as the return value of get_remote_config_value. Customizable.
  186. self.auth_db_file = READ_ZONE_DB_FILE
  187. # Used as the return value of get_remote_config_value. Customizable.
  188. self.auth_datasources = None
  189. # faked cc channel, providing group_send/recvmsg itself. The following
  190. # attributes are for inspection/customization in tests.
  191. self._session = self
  192. self._sent_msg = []
  193. self._recvmsg_called = 0
  194. self._answer_code = 0 # code used in answer returned via recvmsg
  195. self._sendmsg_exception = None # will be raised from sendmsg if !None
  196. self._recvmsg_exception = None # will be raised from recvmsg if !None
  197. # Attributes to handle (faked) remote configurations
  198. self.__callbacks = {} # record callbacks for updates to remote confs
  199. self._raise_mods = {} # map of module to exceptions to be triggered
  200. # on add_remote. settable by tests.
  201. self._auth_config = {} # faked auth cfg, settable by tests
  202. self._zonemgr_config = {} # faked zonemgr cfg, settable by tests
  203. def start(self):
  204. '''Called by DDNSServer initialization, but not used in tests'''
  205. self._started = True
  206. def send_stopping(self):
  207. '''Called by shutdown code'''
  208. self._stopped = True
  209. def get_socket(self):
  210. """
  211. Used to get the file number for select.
  212. """
  213. return FakeSocket(1)
  214. def add_remote_config_by_name(self, module_name, update_callback=None):
  215. # If a list of exceptions is given for the module, raise the front one,
  216. # removing that exception from the list (so the list length controls
  217. # how many (and which) exceptions should be raised on add_remote).
  218. if module_name in self._raise_mods.keys() and \
  219. len(self._raise_mods[module_name]) != 0:
  220. ex = self._raise_mods[module_name][0]
  221. self._raise_mods[module_name] = self._raise_mods[module_name][1:]
  222. raise ex('Failure requesting remote config data')
  223. if update_callback is not None:
  224. self.__callbacks[module_name] = update_callback
  225. if module_name is 'Auth':
  226. if module_name in self.__callbacks:
  227. # ddns implementation doesn't use the 2nd element, so just
  228. # setting it to None
  229. self.__callbacks[module_name](self._auth_config, None)
  230. if module_name is 'Zonemgr':
  231. if module_name in self.__callbacks:
  232. self.__callbacks[module_name](self._zonemgr_config,
  233. ConfigData(ZONEMGR_MODULE_SPEC))
  234. def get_remote_config_value(self, module_name, item):
  235. if module_name == "Auth" and item == "database_file":
  236. return self.auth_db_file, False
  237. if module_name == "Auth" and item == "datasources":
  238. if self.auth_datasources is None:
  239. return [], True # default
  240. else:
  241. return self.auth_datasources, False
  242. if module_name == 'Zonemgr' and item == 'secondary_zones':
  243. if item in self._zonemgr_config:
  244. return self._zonemgr_config[item], False
  245. else:
  246. seczone_default = \
  247. ConfigData(ZONEMGR_MODULE_SPEC).get_default_value(
  248. 'secondary_zones')
  249. return seczone_default, True
  250. def group_sendmsg(self, msg, group, instance='*', to='*',
  251. want_answer=False):
  252. # remember the passed parameter, and return dummy sequence
  253. self._sent_msg.append((msg, group))
  254. if self._sendmsg_exception is not None:
  255. raise self._sendmsg_exception
  256. return self.FAKE_SEQUENCE
  257. def group_recvmsg(self, nonblock, seq):
  258. self._recvmsg_called += 1
  259. if seq != self.FAKE_SEQUENCE:
  260. raise RuntimeError('unexpected CC sequence: ' + str(seq))
  261. if self._recvmsg_exception is not None:
  262. raise self._recvmsg_exception
  263. if self._answer_code is 0:
  264. return create_answer(0), None
  265. else:
  266. return create_answer(self._answer_code, "dummy error value"), None
  267. def clear_msg(self):
  268. '''Clear instrumental attributes related session messages.'''
  269. self._sent_msg = []
  270. self._recvmsg_called = 0
  271. self._answer_code = 0
  272. self._sendmsg_exception = None
  273. self._recvmsg_exception = None
  274. class MyDDNSServer():
  275. '''Fake DDNS server used to test the main() function'''
  276. def __init__(self):
  277. self.reset()
  278. def run(self):
  279. '''
  280. Fake the run() method of the DDNS server. This will set
  281. self._run_called to True.
  282. If self._exception is not None, this is raised as an exception
  283. '''
  284. self.run_called = True
  285. if self._exception is not None:
  286. self.exception_raised = True
  287. raise self._exception
  288. def set_exception(self, exception):
  289. '''Set an exception to be raised when run() is called'''
  290. self._exception = exception
  291. def reset(self):
  292. '''(Re)set to initial values'''
  293. self.run_called = False
  294. self.exception_raised = False
  295. self._exception = None
  296. class TestDDNSServer(unittest.TestCase):
  297. def setUp(self):
  298. cc_session = MyCCSession()
  299. self.assertFalse(cc_session._started)
  300. self.orig_tsig_keyring = isc.server_common.tsig_keyring
  301. isc.server_common.tsig_keyring = FakeKeyringModule()
  302. self.ddns_server = ddns.DDNSServer(cc_session)
  303. self.__cc_session = cc_session
  304. self.assertTrue(cc_session._started)
  305. self.__select_expected = None
  306. self.__select_answer = None
  307. self.__select_exception = None
  308. self.__hook_called = False
  309. # Because we overwrite the _listen_socket, close any existing
  310. # socket object.
  311. if self.ddns_server._listen_socket is not None:
  312. self.ddns_server._listen_socket.close()
  313. self.ddns_server._listen_socket = FakeSocket(2)
  314. ddns.select.select = self.__select
  315. # common private attributes for TCP response tests
  316. self.__tcp_sock = FakeSocket(10, socket.IPPROTO_TCP)
  317. self.__tcp_ctx = DNSTCPContext(self.__tcp_sock)
  318. self.__tcp_data = b'A' * 12 # dummy, just the same size as DNS header
  319. # some tests will override this, which will be restored in tearDown:
  320. self.__orig_add_pause = ddns.add_pause
  321. def tearDown(self):
  322. ddns.select.select = select.select
  323. ddns.isc.util.cio.socketsession.SocketSessionReceiver = \
  324. isc.util.cio.socketsession.SocketSessionReceiver
  325. isc.server_common.tsig_keyring = self.orig_tsig_keyring
  326. ddns.add_pause = self.__orig_add_pause
  327. def test_listen(self):
  328. '''
  329. Test the old socket file is removed (if any) and a new socket
  330. is created when the ddns server is created.
  331. '''
  332. # Make sure the socket does not exist now
  333. ddns.clear_socket()
  334. # Hook the call for clearing the socket
  335. orig_clear = ddns.clear_socket
  336. ddns.clear_socket = self.__hook
  337. # Create the server
  338. ddnss = ddns.DDNSServer(MyCCSession())
  339. ddns.clear_socket = orig_clear
  340. # The socket is created
  341. self.assertTrue(os.path.exists(ddns.SOCKET_FILE))
  342. self.assertTrue(isinstance(ddnss._listen_socket, socket.socket))
  343. # And deletion of the socket was requested
  344. self.assertIsNone(self.__hook_called)
  345. # Now make sure the clear_socket really works
  346. ddns.clear_socket()
  347. self.assertFalse(os.path.exists(ddns.SOCKET_FILE))
  348. # Let ddns object complete any necessary cleanup (not part of the test,
  349. # but for suppressing any warnings from the Python interpreter)
  350. ddnss.shutdown_cleanup()
  351. def test_initial_config(self):
  352. # right now, the only configuration is the zone configuration, whose
  353. # default should be an empty map.
  354. self.assertEqual({}, self.ddns_server._zone_config)
  355. def test_config_handler(self):
  356. # Update with a simple zone configuration: including an accept-all ACL
  357. new_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  358. 'class': TEST_RRCLASS_STR,
  359. 'update_acl': [{'action': 'ACCEPT'}] } ] }
  360. answer = self.ddns_server.config_handler(new_config)
  361. self.assertEqual((0, None), isc.config.parse_answer(answer))
  362. acl = self.ddns_server._zone_config[(TEST_ZONE_NAME, TEST_RRCLASS)]
  363. self.assertEqual(ACCEPT, acl.execute(TEST_ACL_CONTEXT))
  364. # Slightly more complicated one: containing multiple ACLs
  365. new_config = { 'zones': [ { 'origin': 'example.com',
  366. 'class': 'CH',
  367. 'update_acl': [{'action': 'REJECT',
  368. 'from': '2001:db8::1'}] },
  369. { 'origin': TEST_ZONE_NAME_STR,
  370. 'class': TEST_RRCLASS_STR,
  371. 'update_acl': [{'action': 'ACCEPT'}] },
  372. { 'origin': 'example.org',
  373. 'class': 'CH',
  374. 'update_acl': [{'action': 'DROP'}] } ] }
  375. answer = self.ddns_server.config_handler(new_config)
  376. self.assertEqual((0, None), isc.config.parse_answer(answer))
  377. self.assertEqual(3, len(self.ddns_server._zone_config))
  378. acl = self.ddns_server._zone_config[(TEST_ZONE_NAME, TEST_RRCLASS)]
  379. self.assertEqual(ACCEPT, acl.execute(TEST_ACL_CONTEXT))
  380. # empty zone config
  381. new_config = { 'zones': [] }
  382. answer = self.ddns_server.config_handler(new_config)
  383. self.assertEqual((0, None), isc.config.parse_answer(answer))
  384. self.assertEqual({}, self.ddns_server._zone_config)
  385. # bad zone config data: bad name. The previous config shouls be kept.
  386. bad_config = { 'zones': [ { 'origin': 'bad..example',
  387. 'class': TEST_RRCLASS_STR,
  388. 'update_acl': [{'action': 'ACCEPT'}] } ] }
  389. answer = self.ddns_server.config_handler(bad_config)
  390. self.assertEqual(1, isc.config.parse_answer(answer)[0])
  391. self.assertEqual({}, self.ddns_server._zone_config)
  392. # bad zone config data: bad class.
  393. bad_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  394. 'class': 'badclass',
  395. 'update_acl': [{'action': 'ACCEPT'}] } ] }
  396. answer = self.ddns_server.config_handler(bad_config)
  397. self.assertEqual(1, isc.config.parse_answer(answer)[0])
  398. self.assertEqual({}, self.ddns_server._zone_config)
  399. # bad zone config data: bad ACL.
  400. bad_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  401. 'class': TEST_RRCLASS_STR,
  402. 'update_acl': [{'action': 'badaction'}]}]}
  403. answer = self.ddns_server.config_handler(bad_config)
  404. self.assertEqual(1, isc.config.parse_answer(answer)[0])
  405. self.assertEqual({}, self.ddns_server._zone_config)
  406. # the first zone config is valid, but not the second. the first one
  407. # shouldn't be installed.
  408. bad_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  409. 'class': TEST_RRCLASS_STR,
  410. 'update_acl': [{'action': 'ACCEPT'}] },
  411. { 'origin': 'bad..example',
  412. 'class': TEST_RRCLASS_STR,
  413. 'update_acl': [{'action': 'ACCEPT'}] } ] }
  414. answer = self.ddns_server.config_handler(bad_config)
  415. self.assertEqual(1, isc.config.parse_answer(answer)[0])
  416. self.assertEqual({}, self.ddns_server._zone_config)
  417. # Half-broken case: 'origin, class' pair is duplicate. For now we
  418. # we accept it (the latter one will win)
  419. dup_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  420. 'class': TEST_RRCLASS_STR,
  421. 'update_acl': [{'action': 'REJECT'}] },
  422. { 'origin': TEST_ZONE_NAME_STR,
  423. 'class': TEST_RRCLASS_STR,
  424. 'update_acl': [{'action': 'ACCEPT'}] } ] }
  425. answer = self.ddns_server.config_handler(dup_config)
  426. self.assertEqual((0, None), isc.config.parse_answer(answer))
  427. acl = self.ddns_server._zone_config[(TEST_ZONE_NAME, TEST_RRCLASS)]
  428. self.assertEqual(ACCEPT, acl.execute(TEST_ACL_CONTEXT))
  429. def test_datasrc_config(self):
  430. # By default (in our faked config) it should be derived from the
  431. # test data source
  432. rrclass, datasrc_client = self.ddns_server._datasrc_info
  433. self.assertEqual(RRClass.IN, rrclass)
  434. self.assertEqual(DataSourceClient.SUCCESS,
  435. datasrc_client.find_zone(Name('example.org'))[0])
  436. # emulating an update. calling add_remote_config_by_name is a
  437. # convenient faked way to invoke the callback. We set the db file
  438. # to a bogus one; the current implementation will create an unusable
  439. # data source client.
  440. self.__cc_session.auth_db_file = './notexistentdir/somedb.sqlite3'
  441. self.__cc_session._auth_config = \
  442. {'database_file': './notexistentdir/somedb.sqlite3'}
  443. self.__cc_session.add_remote_config_by_name('Auth')
  444. rrclass, datasrc_client = self.ddns_server._datasrc_info
  445. self.assertEqual(RRClass.IN, rrclass)
  446. self.assertRaises(isc.datasrc.Error,
  447. datasrc_client.find_zone, Name('example.org'))
  448. # Check the current info isn't changed if the new config doesn't
  449. # update it.
  450. info_orig = self.ddns_server._datasrc_info
  451. self.ddns_server._datasrc_info = 42 # dummy value, should be kept.
  452. self.__cc_session._auth_config = {'other_config': 'value'}
  453. self.__cc_session.add_remote_config_by_name('Auth')
  454. self.assertEqual(42, self.ddns_server._datasrc_info)
  455. self.ddns_server._datasrc_info = info_orig
  456. def test_secondary_zones_config(self):
  457. # By default it should be an empty list
  458. self.assertEqual(set(), self.ddns_server._secondary_zones)
  459. # emulating an update.
  460. self.__cc_session._zonemgr_config = {'secondary_zones': [
  461. {'name': TEST_ZONE_NAME_STR, 'class': TEST_RRCLASS_STR}]}
  462. self.__cc_session.add_remote_config_by_name('Zonemgr')
  463. # The new set of secondary zones should be stored.
  464. self.assertEqual({(TEST_ZONE_NAME, TEST_RRCLASS)},
  465. self.ddns_server._secondary_zones)
  466. # Similar to the above, but 'class' is unspecified. The default value
  467. # should be used.
  468. self.__cc_session._zonemgr_config = {'secondary_zones': [
  469. {'name': TEST_ZONE_NAME_STR}]}
  470. self.__cc_session.add_remote_config_by_name('Zonemgr')
  471. self.assertEqual({(TEST_ZONE_NAME, TEST_RRCLASS)},
  472. self.ddns_server._secondary_zones)
  473. # The given list has a duplicate. The resulting set should unify them.
  474. self.__cc_session._zonemgr_config = {'secondary_zones': [
  475. {'name': TEST_ZONE_NAME_STR, 'class': TEST_RRCLASS_STR},
  476. {'name': TEST_ZONE_NAME_STR, 'class': TEST_RRCLASS_STR}]}
  477. self.__cc_session.add_remote_config_by_name('Zonemgr')
  478. self.assertEqual({(TEST_ZONE_NAME, TEST_RRCLASS)},
  479. self.ddns_server._secondary_zones)
  480. # Check the 2ndary zones aren't changed if the new config doesn't
  481. # update it.
  482. seczones_orig = self.ddns_server._secondary_zones
  483. self.ddns_server._secondary_zones = 42 # dummy value, should be kept.
  484. self.__cc_session._zonemgr_config = {}
  485. self.__cc_session.add_remote_config_by_name('Zonemgr')
  486. self.assertEqual(42, self.ddns_server._secondary_zones)
  487. self.ddns_server._secondary_zones = seczones_orig
  488. # If the update config is broken, the existing set should be intact.
  489. self.__cc_session._zonemgr_config = {'secondary_zones': [
  490. {'name': 'good.example', 'class': TEST_RRCLASS_STR},
  491. {'name': 'badd..example', 'class': TEST_RRCLASS_STR}]}
  492. self.__cc_session.add_remote_config_by_name('Zonemgr')
  493. self.assertEqual({(TEST_ZONE_NAME, TEST_RRCLASS)},
  494. self.ddns_server._secondary_zones)
  495. def __check_remote_config_fail(self, mod_name, num_ex, expected_ex):
  496. '''Subroutine for remote_config_fail test.'''
  497. # fake pause function for inspection and to avoid having timeouts
  498. added_pause = []
  499. ddns.add_pause = lambda sec: added_pause.append(sec)
  500. # In our current implementation, there will be up to 3 tries of
  501. # adding the module, each separated by a 1-sec pause. If all attempts
  502. # fail the exception will be propagated.
  503. exceptions = [expected_ex for i in range(0, num_ex)]
  504. self.__cc_session._raise_mods = {mod_name: exceptions}
  505. if num_ex >= 3:
  506. self.assertRaises(expected_ex, ddns.DDNSServer, self.__cc_session)
  507. else:
  508. ddns.DDNSServer(self.__cc_session)
  509. self.assertEqual([1 for i in range(0, num_ex)], added_pause)
  510. def test_remote_config_fail(self):
  511. # If getting config of Auth or Zonemgr fails on construction of
  512. # DDNServer, it should result in an exception and a few times
  513. # of retries. We test all possible cases, changing the number of
  514. # raised exceptions and the type of exceptions that can happen,
  515. # which should also cover the fatal error case.
  516. for i in range(0, 4):
  517. self.__check_remote_config_fail('Auth', i, ModuleCCSessionError)
  518. self.__check_remote_config_fail('Auth', i, ModuleSpecError)
  519. self.__check_remote_config_fail('Zonemgr', i, ModuleCCSessionError)
  520. self.__check_remote_config_fail('Zonemgr', i, ModuleSpecError)
  521. def test_shutdown_command(self):
  522. '''Test whether the shutdown command works'''
  523. self.assertFalse(self.ddns_server._shutdown)
  524. answer = self.ddns_server.command_handler('shutdown', None)
  525. self.assertEqual((0, None), isc.config.parse_answer(answer))
  526. self.assertTrue(self.ddns_server._shutdown)
  527. def test_command_handler(self):
  528. '''Test some commands.'''
  529. # this command should not exist
  530. answer = self.ddns_server.command_handler('bad_command', None)
  531. self.assertEqual((1, 'Unknown command: bad_command'),
  532. isc.config.parse_answer(answer))
  533. def test_signal_handler(self):
  534. '''Test whether signal_handler calls shutdown()'''
  535. signal_handler = ddns.create_signal_handler(self.ddns_server)
  536. self.assertFalse(self.ddns_server._shutdown)
  537. signal_handler(None, None)
  538. self.assertTrue(self.ddns_server._shutdown)
  539. def __select(self, reads, writes, exceptions, timeout=None):
  540. """
  541. A fake select. It checks it was called with the correct parameters and
  542. returns a preset answer.
  543. If there's an exception stored in __select_exception, it is raised
  544. instead and the exception is cleared.
  545. """
  546. self.assertEqual(self.__select_expected, (reads, writes, exceptions,
  547. timeout))
  548. if self.__select_exception is not None:
  549. (self.__select_exception, exception) = (None,
  550. self.__select_exception)
  551. raise exception
  552. answer = self.__select_answer
  553. self.__select_answer = None
  554. self.ddns_server._shutdown = True
  555. return answer
  556. def __hook(self, param=None):
  557. """
  558. A hook that can be installed to any nullary or unary function and see
  559. if it was really called.
  560. """
  561. self.__hook_called = param
  562. def test_accept_called(self):
  563. """
  564. Test we call the accept function when a new connection comes.
  565. """
  566. self.ddns_server.accept = self.__hook
  567. self.__select_expected = ([1, 2], [], [], None)
  568. self.__select_answer = ([2], [], [])
  569. self.__hook_called = "Not called"
  570. self.ddns_server.run()
  571. self.assertTrue(self.ddns_server._shutdown)
  572. # The answer got used
  573. self.assertIsNone(self.__select_answer)
  574. # Reset, when called without parameter
  575. self.assertIsNone(self.__hook_called)
  576. def test_check_command_called(self):
  577. """
  578. Test the check_command is called when there's something on the
  579. socket.
  580. """
  581. self.__cc_session.check_command = self.__hook
  582. self.__select_expected = ([1, 2], [], [], None)
  583. self.__select_answer = ([1], [], [])
  584. self.ddns_server.run()
  585. self.assertTrue(self.ddns_server._shutdown)
  586. # The answer got used
  587. self.assertIsNone(self.__select_answer)
  588. # And the check_command was called with true parameter (eg.
  589. # non-blocking)
  590. self.assertTrue(self.__hook_called)
  591. def test_accept(self):
  592. """
  593. Test that we can accept a new connection.
  594. """
  595. # There's nothing before the accept
  596. ddns.isc.util.cio.socketsession.SocketSessionReceiver = \
  597. FakeSessionReceiver
  598. self.assertEqual({}, self.ddns_server._socksession_receivers)
  599. self.ddns_server.accept()
  600. # Now the new socket session receiver is stored in the dict
  601. # The 3 comes from _listen_socket.accept() - _listen_socket has
  602. # fileno 2 and accept returns socket with fileno increased by one.
  603. self.assertEqual([3],
  604. list(self.ddns_server._socksession_receivers.keys()))
  605. (socket, receiver) = self.ddns_server._socksession_receivers[3]
  606. self.assertTrue(isinstance(socket, FakeSocket))
  607. self.assertEqual(3, socket.fileno())
  608. self.assertTrue(isinstance(receiver, FakeSessionReceiver))
  609. self.assertEqual(socket, receiver.socket())
  610. def test_accept_fail(self):
  611. """
  612. Test we don't crash if an accept fails and that we don't modify the
  613. internals.
  614. """
  615. # Make the accept fail
  616. def accept_failure():
  617. raise socket.error(errno.ECONNABORTED)
  618. orig = self.ddns_server._listen_socket.accept
  619. self.ddns_server._listen_socket.accept = accept_failure
  620. self.assertEqual({}, self.ddns_server._socksession_receivers)
  621. # Doesn't raise the exception
  622. self.ddns_server.accept()
  623. # And nothing is stored
  624. self.assertEqual({}, self.ddns_server._socksession_receivers)
  625. # Now make the socket receiver fail
  626. self.ddns_server._listen_socket.accept = orig
  627. def receiver_failure(sock):
  628. raise isc.util.cio.socketsession.SocketSessionError('Test error')
  629. ddns.isc.util.cio.socketsession.SocketSessionReceiver = \
  630. receiver_failure
  631. # Doesn't raise the exception
  632. self.ddns_server.accept()
  633. # And nothing is stored
  634. self.assertEqual({}, self.ddns_server._socksession_receivers)
  635. # Check we don't catch everything, so raise just an exception
  636. def unexpected_failure(sock):
  637. raise Exception('Test error')
  638. ddns.isc.util.cio.socketsession.SocketSessionReceiver = \
  639. unexpected_failure
  640. # This one gets through
  641. self.assertRaises(Exception, self.ddns_server.accept)
  642. # Nothing is stored as well
  643. self.assertEqual({}, self.ddns_server._socksession_receivers)
  644. def test_session_called(self):
  645. """
  646. Test the run calls handle_session when there's something on the
  647. socket.
  648. """
  649. socket = FakeSocket(3)
  650. self.ddns_server._socksession_receivers = \
  651. {3: (socket, FakeSessionReceiver(socket))}
  652. self.ddns_server.handle_session = self.__hook
  653. self.__select_expected = ([1, 2, 3], [], [], None)
  654. self.__select_answer = ([3], [], [])
  655. self.ddns_server.run()
  656. self.assertTrue(self.ddns_server._shutdown)
  657. self.assertTrue(self.__cc_session._stopped)
  658. self.assertIsNone(self.__select_answer)
  659. self.assertEqual(3, self.__hook_called)
  660. def test_handle_session_ok(self):
  661. """
  662. Test the handle_session pops the receiver and calls handle_request
  663. when everything is OK.
  664. """
  665. socket = FakeSocket(3)
  666. receiver = FakeSessionReceiver(socket)
  667. # It doesn't really matter what data we use here, it is only passed
  668. # through the code
  669. param = (FakeSocket(4), ('127.0.0.1', 1234), ('127.0.0.1', 1235),
  670. 'Some data')
  671. def pop():
  672. return param
  673. # Prepare data into the receiver
  674. receiver.pop = pop
  675. self.ddns_server._socksession_receivers = {3: (socket, receiver)}
  676. self.ddns_server.handle_request = self.__hook
  677. # Call it
  678. self.ddns_server.handle_session(3)
  679. # The popped data are passed into the handle_request
  680. self.assertEqual(param, self.__hook_called)
  681. # The receivers are kept the same
  682. self.assertEqual({3: (socket, receiver)},
  683. self.ddns_server._socksession_receivers)
  684. def test_handle_session_fail(self):
  685. """
  686. Test the handle_session removes (and closes) the socket and receiver
  687. when the receiver complains.
  688. """
  689. socket = FakeSocket(3)
  690. receiver = FakeSessionReceiver(socket)
  691. def pop():
  692. raise isc.util.cio.socketsession.SocketSessionError('Test error')
  693. receiver.pop = pop
  694. socket.close = self.__hook
  695. self.__hook_called = False
  696. self.ddns_server._socksession_receivers = {3: (socket, receiver)}
  697. self.ddns_server.handle_session(3)
  698. # The "dead" receiver is removed
  699. self.assertEqual({}, self.ddns_server._socksession_receivers)
  700. # Close is called with no parameter, so the default None
  701. self.assertIsNone(self.__hook_called)
  702. def test_select_exception_ignored(self):
  703. """
  704. Test that the EINTR is ignored in select.
  705. """
  706. # Prepare the EINTR exception
  707. self.__select_exception = select.error(errno.EINTR)
  708. # We reuse the test here, as it should act the same. The exception
  709. # should just get ignored.
  710. self.test_check_command_called()
  711. def test_select_exception_fatal(self):
  712. """
  713. Test that other exceptions are fatal to the run.
  714. """
  715. # Prepare a different exception
  716. self.__select_exception = select.error(errno.EBADF)
  717. self.__select_expected = ([1, 2], [], [], None)
  718. self.assertRaises(select.error, self.ddns_server.run)
  719. def __send_select_tcp(self, buflen, raise_after_select=False):
  720. '''Common subroutine for some TCP related tests below.'''
  721. fileno = self.__tcp_sock.fileno()
  722. self.ddns_server._tcp_ctxs = {fileno: (self.__tcp_ctx, TEST_CLIENT6)}
  723. # make an initial, incomplete send via the test context
  724. self.__tcp_sock._send_buflen = buflen
  725. self.assertEqual(DNSTCPContext.SENDING,
  726. self.__tcp_ctx.send(self.__tcp_data))
  727. self.assertEqual(buflen, len(self.__tcp_sock._sent_data))
  728. # clear the socket "send buffer"
  729. self.__tcp_sock.make_send_ready()
  730. # if requested, set up exception
  731. self.__tcp_sock._raise_on_send = raise_after_select
  732. # Run select
  733. self.__select_expected = ([1, 2], [fileno], [], None)
  734. self.__select_answer = ([], [fileno], [])
  735. self.ddns_server.run()
  736. def test_select_send_continued(self):
  737. '''Test continuation of sending a TCP response.'''
  738. # Common setup, with the bufsize that would make it complete after a
  739. # single select call.
  740. self.__send_select_tcp(7)
  741. # Now the send should be completed. socket should be closed,
  742. # and the context should be removed from the server.
  743. self.assertEqual(14, len(self.__tcp_sock._sent_data))
  744. self.assertEqual(1, self.__tcp_sock._close_called)
  745. self.assertEqual(0, len(self.ddns_server._tcp_ctxs))
  746. def test_select_send_continued_twice(self):
  747. '''Test continuation of sending a TCP response, still continuing.'''
  748. # This is similar to the send_continued test, but the continued
  749. # operation still won't complete the send.
  750. self.__send_select_tcp(5)
  751. # Only 10 bytes should have been transmitted, socket is still open,
  752. # and the context is still in the server (that would require select
  753. # watch it again).
  754. self.assertEqual(10, len(self.__tcp_sock._sent_data))
  755. self.assertEqual(0, self.__tcp_sock._close_called)
  756. fileno = self.__tcp_sock.fileno()
  757. self.assertEqual(self.__tcp_ctx,
  758. self.ddns_server._tcp_ctxs[fileno][0])
  759. def test_select_send_continued_failed(self):
  760. '''Test continuation of sending a TCP response, which fails.'''
  761. # Let the socket raise an exception in the second call to send().
  762. self.__send_select_tcp(5, raise_after_select=True)
  763. # Only the data before select() have been transmitted, socket is
  764. # closed due to the failure, and the context is removed from the
  765. # server.
  766. self.assertEqual(5, len(self.__tcp_sock._sent_data))
  767. self.assertEqual(1, self.__tcp_sock._close_called)
  768. self.assertEqual(0, len(self.ddns_server._tcp_ctxs))
  769. def test_select_multi_tcp(self):
  770. '''Test continuation of sending a TCP response, multiple sockets.'''
  771. # Check if the implementation still works with multiple outstanding
  772. # TCP contexts. We use three (arbitrary choice), of which two will be
  773. # writable after select and complete the send.
  774. tcp_socks = []
  775. for i in range(0, 3):
  776. # Use faked FD of 100, 101, 102 (again, arbitrary choice)
  777. s = FakeSocket(100 + i, proto=socket.IPPROTO_TCP)
  778. ctx = DNSTCPContext(s)
  779. self.ddns_server._tcp_ctxs[s.fileno()] = (ctx, TEST_CLIENT6)
  780. s._send_buflen = 7 # make sure it requires two send's
  781. self.assertEqual(DNSTCPContext.SENDING, ctx.send(self.__tcp_data))
  782. s.make_send_ready()
  783. tcp_socks.append(s)
  784. self.__select_expected = ([1, 2], [100, 101, 102], [], None)
  785. self.__select_answer = ([], [100, 102], [])
  786. self.ddns_server.run()
  787. for i in [0, 2]:
  788. self.assertEqual(14, len(tcp_socks[i]._sent_data))
  789. self.assertEqual(1, tcp_socks[i]._close_called)
  790. self.assertEqual(1, len(self.ddns_server._tcp_ctxs))
  791. def test_select_bad_writefd(self):
  792. # There's no outstanding TCP context, but select somehow returns
  793. # writable FD. It should result in an uncaught exception, killing
  794. # the server. This is okay, because it shouldn't happen and should be
  795. # an internal bug.
  796. self.__select_expected = ([1, 2], [], [], None)
  797. self.__select_answer = ([], [10], [])
  798. self.assertRaises(KeyError, self.ddns_server.run)
  799. def create_msg(opcode=Opcode.UPDATE, zones=[TEST_ZONE_RECORD], prereq=[],
  800. tsigctx=None):
  801. msg = Message(Message.RENDER)
  802. msg.set_qid(TEST_QID)
  803. msg.set_opcode(opcode)
  804. msg.set_rcode(Rcode.NOERROR)
  805. for z in zones:
  806. msg.add_question(z)
  807. for p in prereq:
  808. msg.add_rrset(SECTION_PREREQUISITE, p)
  809. renderer = MessageRenderer()
  810. msg.to_wire(renderer, tsigctx)
  811. # re-read the created data in the parse mode
  812. msg.clear(Message.PARSE)
  813. msg.from_wire(renderer.get_data())
  814. return renderer.get_data()
  815. class TestDDNSSession(unittest.TestCase):
  816. def setUp(self):
  817. self.__cc_session = MyCCSession()
  818. self.assertFalse(self.__cc_session._started)
  819. self.orig_tsig_keyring = isc.server_common.tsig_keyring
  820. isc.server_common.tsig_keyring = FakeKeyringModule()
  821. self.server = ddns.DDNSServer(self.__cc_session)
  822. # Check that start_ddns_forwarder has been called upon
  823. # initialization (before we do anything else that might
  824. # cause messages to be sent)
  825. self.check_session_start_forwarder_called()
  826. self.server._UpdateSessionClass = self.__fake_session_creator
  827. self.__faked_result = UPDATE_SUCCESS # will be returned by fake session
  828. self.__sock = FakeSocket(-1)
  829. def tearDown(self):
  830. self.assertTrue(isc.server_common.tsig_keyring.initialized)
  831. isc.server_common.tsig_keyring = self.orig_tsig_keyring
  832. def __fake_session_creator(self, req_message, client_addr, zone_config):
  833. # remember the passed message for possible inspection later.
  834. self.__req_message = req_message
  835. return FakeUpdateSession(req_message, client_addr, zone_config,
  836. self.__faked_result)
  837. def check_update_response(self, resp_wire, expected_rcode=Rcode.NOERROR,
  838. tsig_ctx=None, tcp=False):
  839. '''Check if given wire data are valid form of update response.
  840. In this implementation, zone/prerequisite/update sections should be
  841. empty in responses.
  842. If tsig_ctx (isc.dns.TSIGContext) is not None, the response should
  843. be TSIG signed and the signature should be verifiable with the context
  844. that has signed the corresponding request.
  845. if tcp is True, the wire data are expected to be prepended with
  846. a 2-byte length field.
  847. '''
  848. if tcp:
  849. data_len = resp_wire[0] * 256 + resp_wire[1]
  850. resp_wire = resp_wire[2:]
  851. self.assertEqual(len(resp_wire), data_len)
  852. msg = Message(Message.PARSE)
  853. msg.from_wire(resp_wire)
  854. if tsig_ctx is not None:
  855. tsig_record = msg.get_tsig_record()
  856. self.assertNotEqual(None, tsig_record)
  857. self.assertEqual(TSIGError.NOERROR,
  858. tsig_ctx.verify(tsig_record, resp_wire))
  859. self.assertEqual(Opcode.UPDATE, msg.get_opcode())
  860. self.assertEqual(expected_rcode, msg.get_rcode())
  861. self.assertEqual(TEST_QID, msg.get_qid())
  862. for section in [SECTION_ZONE, SECTION_PREREQUISITE, SECTION_UPDATE]:
  863. self.assertEqual(0, msg.get_rr_count(section))
  864. def check_session(self, result=UPDATE_SUCCESS, ipv6=True, tsig_key=None):
  865. # reset test parameters
  866. self.__sock.clear()
  867. self.__faked_result = result
  868. server_addr = TEST_SERVER6 if ipv6 else TEST_SERVER4
  869. client_addr = TEST_CLIENT6 if ipv6 else TEST_CLIENT4
  870. tsig = TSIGContext(tsig_key) if tsig_key is not None else None
  871. rcode = Rcode.NOERROR if result == UPDATE_SUCCESS else Rcode.REFUSED
  872. has_response = (result != UPDATE_DROP)
  873. self.assertEqual(has_response,
  874. self.server.handle_request((self.__sock,
  875. server_addr, client_addr,
  876. create_msg(tsigctx=tsig))))
  877. if has_response:
  878. self.assertEqual(client_addr, self.__sock._sent_addr)
  879. self.check_update_response(self.__sock._sent_data, rcode)
  880. else:
  881. self.assertEqual((None, None), (self.__sock._sent_addr,
  882. self.__sock._sent_data))
  883. def test_handle_request(self):
  884. '''Basic request handling without any unexpected errors.'''
  885. # Success, without TSIG
  886. self.check_session()
  887. # Update will be refused with a response.
  888. self.check_session(UPDATE_ERROR, ipv6=False)
  889. # Update will be refused and dropped
  890. self.check_session(UPDATE_DROP)
  891. # Success, with TSIG
  892. self.check_session(ipv6=False, tsig_key=TEST_TSIG_KEY)
  893. # Update will be refused with a response, with TSIG.
  894. self.check_session(UPDATE_ERROR, tsig_key=TEST_TSIG_KEY)
  895. # Update will be refused and dropped, with TSIG (doesn't matter though)
  896. self.check_session(UPDATE_DROP, ipv6=False, tsig_key=TEST_TSIG_KEY)
  897. def test_broken_request(self):
  898. # Message data too short
  899. s = self.__sock
  900. self.assertFalse(self.server.handle_request((self.__sock, None,
  901. None, b'x' * 11)))
  902. self.assertEqual((None, None), (s._sent_data, s._sent_addr))
  903. # Opcode is not UPDATE
  904. self.assertFalse(self.server.handle_request(
  905. (self.__sock, None, None, create_msg(opcode=Opcode.QUERY))))
  906. self.assertEqual((None, None), (s._sent_data, s._sent_addr))
  907. # TSIG verification error. We use UPDATE_DROP to signal check_session
  908. # that no response should be given.
  909. self.check_session(result=UPDATE_DROP, ipv6=False,
  910. tsig_key=BAD_TSIG_KEY)
  911. def test_socket_error(self):
  912. # Have the faked socket raise an exception on sendto()
  913. self.__sock._raise_on_send = True
  914. # handle_request indicates the failure
  915. self.assertFalse(self.server.handle_request((self.__sock, TEST_SERVER6,
  916. TEST_CLIENT6,
  917. create_msg())))
  918. # this check ensures sendto() was really attempted.
  919. self.check_update_response(self.__sock._sent_data, Rcode.NOERROR)
  920. def test_tcp_request(self):
  921. # A simple case using TCP: all resopnse data are sent out at once.
  922. s = self.__sock
  923. s.proto = socket.IPPROTO_TCP
  924. self.assertTrue(self.server.handle_request((s, TEST_SERVER6,
  925. TEST_CLIENT6,
  926. create_msg())))
  927. self.check_update_response(s._sent_data, Rcode.NOERROR, tcp=True)
  928. # In the current implementation, the socket should be closed
  929. # immedidately after a successful send.
  930. self.assertEqual(1, s._close_called)
  931. # TCP context shouldn't be held in the server.
  932. self.assertEqual(0, len(self.server._tcp_ctxs))
  933. def test_tcp_request_incomplete(self):
  934. # set the size of the socket "send buffer" to a small value, which
  935. # should cause partial send.
  936. s = self.__sock
  937. s.proto = socket.IPPROTO_TCP
  938. s._send_buflen = 7
  939. # before this request there should be no outstanding TCP context.
  940. self.assertEqual(0, len(self.server._tcp_ctxs))
  941. self.assertTrue(self.server.handle_request((s, TEST_SERVER6,
  942. TEST_CLIENT6,
  943. create_msg())))
  944. # Only the part of data that fit the send buffer should be transmitted.
  945. self.assertEqual(s._send_buflen, len(s._sent_data))
  946. # the socket is not yet closed.
  947. self.assertEqual(0, s._close_called)
  948. # and a new context is stored in the server.
  949. self.assertEqual(1, len(self.server._tcp_ctxs))
  950. # clear the "send buffer" of the fake socket, and continue the send
  951. # by hand. The next attempt should complete the send, and the combined
  952. # data should be the expected response.
  953. s.make_send_ready()
  954. self.assertEqual(DNSTCPContext.SEND_DONE,
  955. self.server._tcp_ctxs[s.fileno()][0].send_ready())
  956. self.check_update_response(s._sent_data, Rcode.NOERROR, tcp=True)
  957. def test_tcp_request_error(self):
  958. # initial send() on the TCP socket will fail. The request handling
  959. # will be considered failure.
  960. s = self.__sock
  961. s.proto = socket.IPPROTO_TCP
  962. s._raise_on_send = True
  963. self.assertFalse(self.server.handle_request((s, TEST_SERVER6,
  964. TEST_CLIENT6,
  965. create_msg())))
  966. # the socket should have been closed.
  967. self.assertEqual(1, s._close_called)
  968. def test_tcp_request_quota(self):
  969. '''Test'''
  970. # Originally the TCP context map should be empty.
  971. self.assertEqual(0, len(self.server._tcp_ctxs))
  972. class FakeReceiver:
  973. '''Faked SessionReceiver, just returning given param by pop()'''
  974. def __init__(self, param):
  975. self.__param = param
  976. def pop(self):
  977. return self.__param
  978. def check_tcp_ok(fd, expect_grant):
  979. '''Supplemental checker to see if TCP request is handled.'''
  980. s = FakeSocket(fd, proto=socket.IPPROTO_TCP)
  981. s._send_buflen = 7
  982. self.server._socksession_receivers[s.fileno()] = \
  983. (None, FakeReceiver((s, TEST_SERVER6, TEST_CLIENT6,
  984. create_msg())))
  985. self.assertEqual(expect_grant,
  986. self.server.handle_session(s.fileno()))
  987. self.assertEqual(0 if expect_grant else 1, s._close_called)
  988. # By default up to 10 TCP clients can coexist (use hardcode
  989. # intentionally so we can test the default value itself)
  990. for i in range(0, 10):
  991. check_tcp_ok(i, True)
  992. self.assertEqual(10, len(self.server._tcp_ctxs))
  993. # Beyond that, it should be rejected (by reset)
  994. check_tcp_ok(11, False)
  995. # If we remove one context from the server, new client can go in again.
  996. self.server._tcp_ctxs.pop(5)
  997. check_tcp_ok(12, True)
  998. def test_request_message(self):
  999. '''Test if the request message stores RRs separately.'''
  1000. # Specify 'drop' so the passed message won't be modified.
  1001. self.__faked_result = UPDATE_DROP
  1002. # Put the same RR twice in the prerequisite section. We should see
  1003. # them as separate RRs.
  1004. dummy_record = RRset(TEST_ZONE_NAME, TEST_RRCLASS, RRType.NS,
  1005. RRTTL(0))
  1006. dummy_record.add_rdata(Rdata(RRType.NS, TEST_RRCLASS, "ns.example."))
  1007. self.server.handle_request((self.__sock, TEST_SERVER6, TEST_CLIENT6,
  1008. create_msg(prereq=[dummy_record,
  1009. dummy_record])))
  1010. num_rrsets = len(self.__req_message.get_section(SECTION_PREREQUISITE))
  1011. self.assertEqual(2, num_rrsets)
  1012. def check_session_msg(self, result, expect_recv=2):
  1013. '''Check post update communication with other modules.'''
  1014. # iff the update succeeds, b10-ddns should tell interested other
  1015. # modules the information about the update zone. Possible modules
  1016. # are xfrout and auth: for xfrout, the message format should be:
  1017. # {'command': ['notify', {'zone_name': <updated_zone_name>,
  1018. # 'zone_class', <updated_zone_class>}]}
  1019. # for auth, it should be:
  1020. # {'command': ['loadzone', {'origin': <updated_zone_name>,
  1021. # 'class', <updated_zone_class>}]}
  1022. # and expect an answer by calling group_recvmsg().
  1023. #
  1024. # expect_recv indicates the expected number of calls to
  1025. # group_recvmsg(), which is normally 2, but can be 0 if send fails;
  1026. # if the message is to be sent
  1027. if result == UPDATE_SUCCESS:
  1028. expected_sentmsg = 2
  1029. self.assertEqual(expected_sentmsg,
  1030. len(self.__cc_session._sent_msg))
  1031. self.assertEqual(expect_recv, self.__cc_session._recvmsg_called)
  1032. msg_cnt = 0
  1033. sent_msg, sent_group = self.__cc_session._sent_msg[msg_cnt]
  1034. sent_cmd = sent_msg['command']
  1035. self.assertEqual('Auth', sent_group)
  1036. self.assertEqual('loadzone', sent_cmd[0])
  1037. self.assertEqual(2, len(sent_cmd[1]))
  1038. self.assertEqual(TEST_ZONE_NAME.to_text(),
  1039. sent_cmd[1]['origin'])
  1040. self.assertEqual(TEST_RRCLASS.to_text(),
  1041. sent_cmd[1]['class'])
  1042. msg_cnt += 1
  1043. sent_msg, sent_group = self.__cc_session._sent_msg[msg_cnt]
  1044. sent_cmd = sent_msg['command']
  1045. self.assertEqual('Xfrout', sent_group)
  1046. self.assertEqual('notify', sent_cmd[0])
  1047. self.assertEqual(2, len(sent_cmd[1]))
  1048. self.assertEqual(TEST_ZONE_NAME.to_text(), sent_cmd[1]['zone_name'])
  1049. self.assertEqual(TEST_RRCLASS.to_text(), sent_cmd[1]['zone_class'])
  1050. else:
  1051. # for other result cases neither send nor recvmsg should be called.
  1052. self.assertEqual([], self.__cc_session._sent_msg)
  1053. self.assertEqual(0, self.__cc_session._recvmsg_called)
  1054. def check_session_start_forwarder_called(self):
  1055. '''Check that the command 'start_ddns_forwarder' has been called
  1056. This test removes said message from the sent message queue.
  1057. '''
  1058. sent_msg, sent_group = self.__cc_session._sent_msg.pop(0)
  1059. sent_cmd = sent_msg['command']
  1060. self.assertEqual('Auth', sent_group)
  1061. self.assertEqual('start_ddns_forwarder', sent_cmd[0])
  1062. self.assertEqual(1, len(sent_cmd))
  1063. self.assertEqual(1, self.__cc_session._recvmsg_called)
  1064. # reset it for other tests
  1065. self.__cc_session._recvmsg_called = 0
  1066. def check_session_stop_forwarder_called(self):
  1067. '''Check that the command 'stop_ddns_forwarder' has been called
  1068. This test removes said message from the sent message queue.
  1069. '''
  1070. # check the last message sent
  1071. sent_msg, sent_group = self.__cc_session._sent_msg.pop()
  1072. sent_cmd = sent_msg['command']
  1073. self.assertEqual('Auth', sent_group)
  1074. self.assertEqual('stop_ddns_forwarder', sent_cmd[0])
  1075. self.assertEqual(1, len(sent_cmd))
  1076. def test_session_msg(self):
  1077. '''Test post update communication with other modules.'''
  1078. # Normal cases, confirming communication takes place iff update
  1079. # succeeds
  1080. for r in [UPDATE_SUCCESS, UPDATE_ERROR, UPDATE_DROP]:
  1081. self.__cc_session.clear_msg()
  1082. self.check_session(result=r)
  1083. self.check_session_msg(r)
  1084. # Return an error from the remote module, which should be just ignored.
  1085. self.__cc_session.clear_msg()
  1086. self.__cc_session._answer_code = 1
  1087. self.check_session()
  1088. self.check_session_msg(UPDATE_SUCCESS)
  1089. # raise some exceptions from the faked session. Expected ones are
  1090. # simply (logged and) ignored
  1091. self.__cc_session.clear_msg()
  1092. self.__cc_session._recvmsg_exception = SessionTimeout('dummy timeout')
  1093. self.check_session()
  1094. self.check_session_msg(UPDATE_SUCCESS)
  1095. self.__cc_session.clear_msg()
  1096. self.__cc_session._recvmsg_exception = SessionError('dummy error')
  1097. self.check_session()
  1098. self.check_session_msg(UPDATE_SUCCESS)
  1099. self.__cc_session.clear_msg()
  1100. self.__cc_session._recvmsg_exception = ProtocolError('dummy perror')
  1101. self.check_session()
  1102. self.check_session_msg(UPDATE_SUCCESS)
  1103. # Similar to the previous cases, but sendmsg() raises, so there should
  1104. # be no call to recvmsg().
  1105. self.__cc_session.clear_msg()
  1106. self.__cc_session._sendmsg_exception = SessionError('send error')
  1107. self.check_session()
  1108. self.check_session_msg(UPDATE_SUCCESS, expect_recv=0)
  1109. # Unexpected exception will be propagated (and will terminate the
  1110. # server)
  1111. self.__cc_session.clear_msg()
  1112. self.__cc_session._sendmsg_exception = RuntimeError('unexpected')
  1113. self.assertRaises(RuntimeError, self.check_session)
  1114. def test_session_shutdown_cleanup(self):
  1115. '''Test that the stop forwarding message is sent'''
  1116. self.server.shutdown_cleanup()
  1117. self.check_session_stop_forwarder_called()
  1118. def test_session_msg_for_auth(self):
  1119. '''Test post update communication with other modules including Auth.'''
  1120. # Let the CC session return in-memory config with sqlite3 backend.
  1121. # (The default case was covered by other tests.)
  1122. self.__cc_session.auth_datasources = \
  1123. [{'type': 'memory', 'class': 'IN', 'zones': [
  1124. {'origin': TEST_ZONE_NAME_STR, 'filetype': 'sqlite3'}]}]
  1125. self.check_session()
  1126. self.check_session_msg(UPDATE_SUCCESS)
  1127. # Let sendmsg() raise an exception. The first exception shouldn't
  1128. # stop sending the second message. There's just no recv calls.
  1129. self.__cc_session.clear_msg()
  1130. self.__cc_session._sendmsg_exception = SessionError('send error')
  1131. self.check_session()
  1132. self.check_session_msg(UPDATE_SUCCESS, expect_recv=0)
  1133. # Likewise, in the case recvmsg() raises (and there should be recv
  1134. # calls in this case)
  1135. self.__cc_session.clear_msg()
  1136. self.__cc_session._recvmsg_exception = SessionError('recv error')
  1137. self.check_session()
  1138. self.check_session_msg(UPDATE_SUCCESS)
  1139. def test_session_with_config(self):
  1140. '''Check a session with more realistic config setups.
  1141. We don't have to explore various cases in detail in this test.
  1142. We're just checking if the expected configured objects are passed
  1143. to the session object.
  1144. '''
  1145. # reset the session class to the real one
  1146. self.server._UpdateSessionClass = isc.ddns.session.UpdateSession
  1147. # install all-drop ACL
  1148. new_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  1149. 'class': TEST_RRCLASS_STR,
  1150. 'update_acl': [{'action': 'DROP'}] } ] }
  1151. answer = self.server.config_handler(new_config)
  1152. self.assertEqual((0, None), isc.config.parse_answer(answer))
  1153. # check the result
  1154. self.check_session(UPDATE_DROP)
  1155. def test_session_start_stop_forwarder_failures(self):
  1156. '''Check that we don't crash if the server reports an error
  1157. setting up or closing down the DDNS UPDATE message forwarder,
  1158. or if there is an exception from the message queue.'''
  1159. self.__cc_session._answer_code = 1
  1160. self.server._DDNSServer__notify_start_forwarder()
  1161. self.server._DDNSServer__notify_stop_forwarder()
  1162. for exc in [ SessionError("sessionerror"),
  1163. SessionTimeout("sessiontimeout"),
  1164. ProtocolError("protocolerror") ]:
  1165. self.__cc_session._recvmsg_exception = exc
  1166. self.server._DDNSServer__notify_start_forwarder()
  1167. self.server._DDNSServer__notify_stop_forwarder()
  1168. self.__cc_session._recvmsg_exception = None
  1169. self.__cc_session._sendmsg_exception = exc
  1170. self.server._DDNSServer__notify_start_forwarder()
  1171. self.server._DDNSServer__notify_stop_forwarder()
  1172. self.__cc_session._recvmsg_exception = None
  1173. def test_session_auth_started(self):
  1174. '''Check that 'start_ddns_forwarder' is sent (again) when the
  1175. notification 'auth_started' is received'''
  1176. # auth_started message should trigger it again
  1177. answer = self.server.command_handler('auth_started', None)
  1178. self.check_session_start_forwarder_called()
  1179. class TestMain(unittest.TestCase):
  1180. def setUp(self):
  1181. self._server = MyDDNSServer()
  1182. self.__orig_clear = ddns.clear_socket
  1183. ddns.clear_socket = self.__clear_socket
  1184. self.__clear_called = False
  1185. def tearDown(self):
  1186. ddns.clear_socket = self.__orig_clear
  1187. def test_main(self):
  1188. self.assertFalse(self._server.run_called)
  1189. ddns.main(self._server)
  1190. self.assertTrue(self._server.run_called)
  1191. self.assertTrue(self.__clear_called)
  1192. def __clear_socket(self):
  1193. self.__clear_called = True
  1194. # Get rid of the socket file too
  1195. self.__orig_clear()
  1196. def check_exception(self, ex):
  1197. '''Common test sequence to see if the given exception is caused.
  1198. '''
  1199. # Should technically not be necessary, but reset server to be sure
  1200. self._server.reset()
  1201. self.assertFalse(self._server.exception_raised)
  1202. self._server.set_exception(ex)
  1203. ddns.main(self._server)
  1204. self.assertTrue(self._server.exception_raised)
  1205. def test_exceptions(self):
  1206. '''
  1207. Test whether exceptions are caught in main()
  1208. These exceptions should not bubble up.
  1209. '''
  1210. self._server.set_exception(KeyboardInterrupt())
  1211. self.assertFalse(self._server.exception_raised)
  1212. ddns.main(self._server)
  1213. self.assertTrue(self._server.exception_raised)
  1214. self.check_exception(isc.cc.SessionError("error"))
  1215. self.check_exception(isc.config.ModuleCCSessionError("error"))
  1216. self.check_exception(ddns.DDNSConfigError("error"))
  1217. self.check_exception(isc.cc.SessionTimeout("error"))
  1218. # Add one that is not a subclass of Exception, and hence not
  1219. # caught. Misuse BaseException for that.
  1220. self._server.reset()
  1221. self.assertFalse(self._server.exception_raised)
  1222. self._server.set_exception(BaseException("error"))
  1223. self.assertRaises(BaseException, ddns.main, self._server)
  1224. self.assertTrue(self._server.exception_raised)
  1225. class TestConfig(unittest.TestCase):
  1226. '''Test some simple config related things that don't need server. '''
  1227. def setUp(self):
  1228. self.__ccsession = MyCCSession()
  1229. def test_file_path(self):
  1230. # Check some common paths
  1231. self.assertEqual(os.environ["B10_FROM_BUILD"] + "/ddns_socket",
  1232. ddns.SOCKET_FILE)
  1233. self.assertEqual(os.environ["B10_FROM_SOURCE"] +
  1234. "/src/bin/ddns/ddns.spec", ddns.SPECFILE_LOCATION)
  1235. if __name__== "__main__":
  1236. isc.log.resetUnitTestRootLogger()
  1237. unittest.main()