ddns_test.py 61 KB

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