ddns_test.py 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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. from isc.notify import notify_out
  28. import ddns
  29. import errno
  30. import os
  31. import select
  32. import shutil
  33. import socket
  34. import unittest
  35. # Some common test parameters
  36. TESTDATA_PATH = os.environ['TESTDATA_PATH'] + os.sep
  37. READ_ZONE_DB_FILE = TESTDATA_PATH + "rwtest.sqlite3" # original, to be copied
  38. TEST_ZONE_NAME = Name('example.org')
  39. TEST_ZONE_NAME_STR = TEST_ZONE_NAME.to_text()
  40. UPDATE_RRTYPE = RRType.SOA()
  41. TEST_QID = 5353 # arbitrary chosen
  42. TEST_RRCLASS = RRClass.IN()
  43. TEST_RRCLASS_STR = TEST_RRCLASS.to_text()
  44. TEST_SERVER6 = ('2001:db8::53', 53, 0, 0)
  45. TEST_CLIENT6 = ('2001:db8::1', 53000, 0, 0)
  46. TEST_SERVER4 = ('192.0.2.53', 53)
  47. TEST_CLIENT4 = ('192.0.2.1', 53534)
  48. TEST_ZONE_RECORD = Question(TEST_ZONE_NAME, TEST_RRCLASS, UPDATE_RRTYPE)
  49. TEST_ACL_CONTEXT = isc.acl.dns.RequestContext(
  50. socket.getaddrinfo("192.0.2.1", 1234, 0, socket.SOCK_DGRAM,
  51. socket.IPPROTO_UDP, socket.AI_NUMERICHOST)[0][4])
  52. # TSIG key for tests when needed. The key name is TEST_ZONE_NAME.
  53. TEST_TSIG_KEY = TSIGKey("example.org:SFuWd/q99SzF8Yzd1QbB9g==")
  54. # TSIG keyring that contanins the test key
  55. TEST_TSIG_KEYRING = TSIGKeyRing()
  56. TEST_TSIG_KEYRING.add(TEST_TSIG_KEY)
  57. # Another TSIG key not in the keyring, making verification fail
  58. BAD_TSIG_KEY = TSIGKey("example.com:SFuWd/q99SzF8Yzd1QbB9g==")
  59. # Incorporate it so we can use the real default values of zonemgr config
  60. # in the tests.
  61. ZONEMGR_MODULE_SPEC = module_spec_from_file(
  62. os.environ["B10_FROM_BUILD"] + "/src/bin/zonemgr/zonemgr.spec")
  63. class FakeSocket:
  64. """
  65. A fake socket. It only provides a file number, peer name and accept method.
  66. """
  67. def __init__(self, fileno, proto=socket.IPPROTO_UDP):
  68. self.proto = proto
  69. self.__fileno = fileno
  70. self._sent_data = None
  71. self._sent_addr = None
  72. self._close_called = 0 # number of calls to close()
  73. self.__send_cc = 0 # waterline of the send buffer (can be reset)
  74. # customizable by tests; if set to True, sendto() will throw after
  75. # recording the parameters.
  76. self._raise_on_send = False
  77. self._send_buflen = None # imaginary send buffer for partial send
  78. def fileno(self):
  79. return self.__fileno
  80. def getpeername(self):
  81. if self.proto == socket.IPPROTO_UDP or \
  82. self.proto == socket.IPPROTO_TCP:
  83. return TEST_CLIENT4
  84. return "fake_unix_socket"
  85. def accept(self):
  86. return FakeSocket(self.__fileno + 1), '/dummy/path'
  87. def sendto(self, data, addr):
  88. self._sent_data = data
  89. self._sent_addr = addr
  90. if self._raise_on_send:
  91. raise socket.error('test socket failure')
  92. def send(self, data):
  93. if self._raise_on_send:
  94. raise socket.error(errno.EPIPE, 'faked connection disruption')
  95. elif self._send_buflen is None:
  96. available_space = len(data)
  97. else:
  98. available_space = self._send_buflen - self.__send_cc
  99. if available_space == 0:
  100. # if there's no space, (assuming it's nonblocking mode) raise
  101. # EAGAIN.
  102. raise socket.error(errno.EAGAIN,
  103. "Resource temporarily unavailable")
  104. # determine the sendable part of the data, record it, update "buffer".
  105. cc = min(available_space, len(data))
  106. if self._sent_data is None:
  107. self._sent_data = data[:cc]
  108. else:
  109. self._sent_data += data[:cc]
  110. self.__send_cc += cc
  111. return cc
  112. def setblocking(self, on):
  113. # We only need a faked NO-OP implementation.
  114. pass
  115. def close(self):
  116. self._close_called += 1
  117. def clear(self):
  118. '''Clear internal instrumental data.'''
  119. self._sent_data = None
  120. self._sent_addr = None
  121. def make_send_ready(self):
  122. # pretend that the accrued data has been cleared, making room in
  123. # the send buffer.
  124. self.__send_cc = 0
  125. class FakeSessionReceiver:
  126. """
  127. A fake socket session receiver, for our tests.
  128. """
  129. def __init__(self, socket):
  130. self._socket = socket
  131. def socket(self):
  132. """
  133. This method is not present in the real receiver, but we use it to
  134. inspect the socket passed to the constructor.
  135. """
  136. return self._socket
  137. class FakeUpdateSession:
  138. '''A fake update session, emulating isc.ddns.session.UpdateSession.
  139. It provides the same interfaces as UpdateSession with skipping complicated
  140. internal protocol processing and returning given faked results. This
  141. will help simplify test setups.
  142. '''
  143. def __init__(self, msg, client_addr, zone_config, faked_result):
  144. '''Faked constructor.
  145. It takes an additional faked_result parameter. It will be used
  146. as the result value of handle(). If its value is UPDATE_ERROR,
  147. get_message() will create a response message whose Rcode is
  148. REFUSED.
  149. '''
  150. self.__msg = msg
  151. self.__faked_result = faked_result
  152. def handle(self):
  153. if self.__faked_result == UPDATE_SUCCESS:
  154. return self.__faked_result, TEST_ZONE_NAME, TEST_RRCLASS
  155. return self.__faked_result, None, None
  156. def get_message(self):
  157. self.__msg.make_response()
  158. self.__msg.clear_section(SECTION_ZONE)
  159. if self.__faked_result == UPDATE_SUCCESS:
  160. self.__msg.set_rcode(Rcode.NOERROR())
  161. else:
  162. self.__msg.set_rcode(Rcode.REFUSED())
  163. return self.__msg
  164. class FakeKeyringModule:
  165. '''Fake the entire isc.server_common.tsig_keyring module.'''
  166. def init_keyring(self, cc):
  167. '''Set the instrumental attribute to True when called.
  168. It can be used for a test that confirms TSIG key initialization is
  169. surely performed. This class doesn't use any CC session, so the
  170. cc parameter will be ignored.
  171. '''
  172. self.initialized = True
  173. def get_keyring(self):
  174. '''Simply return the predefined TSIG keyring unconditionally.'''
  175. return TEST_TSIG_KEYRING
  176. class MyCCSession(isc.config.ConfigData):
  177. '''Fake session with minimal interface compliance.'''
  178. # faked CC sequence used in group_send/recvmsg
  179. FAKE_SEQUENCE = 53
  180. def __init__(self):
  181. module_spec = isc.config.module_spec_from_file(
  182. ddns.SPECFILE_LOCATION)
  183. isc.config.ConfigData.__init__(self, module_spec)
  184. self._started = False
  185. self._stopped = False
  186. # Used as the return value of get_remote_config_value. Customizable.
  187. self.auth_db_file = READ_ZONE_DB_FILE
  188. # Used as the return value of get_remote_config_value. Customizable.
  189. self.auth_datasources = None
  190. # faked cc channel, providing group_send/recvmsg itself. The following
  191. # attributes are for inspection/customization in tests.
  192. self._session = self
  193. self._sent_msg = []
  194. self._recvmsg_called = 0
  195. self._answer_code = 0 # code used in answer returned via recvmsg
  196. self._sendmsg_exception = None # will be raised from sendmsg if !None
  197. self._recvmsg_exception = None # will be raised from recvmsg if !None
  198. # Attributes to handle (faked) remote configurations
  199. self.__callbacks = {} # record callbacks for updates to remote confs
  200. self._raise_mods = {} # map of module to exceptions to be triggered
  201. # on add_remote. settable by tests.
  202. self._auth_config = {} # faked auth cfg, settable by tests
  203. self._zonemgr_config = {} # faked zonemgr cfg, settable by tests
  204. def start(self):
  205. '''Called by DDNSServer initialization, but not used in tests'''
  206. self._started = True
  207. def send_stopping(self):
  208. '''Called by shutdown code'''
  209. self._stopped = True
  210. def get_socket(self):
  211. """
  212. Used to get the file number for select.
  213. """
  214. return FakeSocket(1)
  215. def add_remote_config_by_name(self, module_name, update_callback=None):
  216. # If a list of exceptions is given for the module, raise the front one,
  217. # removing that exception from the list (so the list length controls
  218. # how many (and which) exceptions should be raised on add_remote).
  219. if module_name in self._raise_mods.keys() and \
  220. len(self._raise_mods[module_name]) != 0:
  221. ex = self._raise_mods[module_name][0]
  222. self._raise_mods[module_name] = self._raise_mods[module_name][1:]
  223. raise ex('Failure requesting remote config data')
  224. if update_callback is not None:
  225. self.__callbacks[module_name] = update_callback
  226. if module_name is 'Auth':
  227. if module_name in self.__callbacks:
  228. # ddns implementation doesn't use the 2nd element, so just
  229. # setting it to None
  230. self.__callbacks[module_name](self._auth_config, None)
  231. if module_name is 'Zonemgr':
  232. if module_name in self.__callbacks:
  233. self.__callbacks[module_name](self._zonemgr_config,
  234. ConfigData(ZONEMGR_MODULE_SPEC))
  235. def get_remote_config_value(self, module_name, item):
  236. if module_name == "Auth" and item == "database_file":
  237. return self.auth_db_file, False
  238. if module_name == "Auth" and item == "datasources":
  239. if self.auth_datasources is None:
  240. return [], True # default
  241. else:
  242. return self.auth_datasources, False
  243. if module_name == 'Zonemgr' and item == 'secondary_zones':
  244. if item in self._zonemgr_config:
  245. return self._zonemgr_config[item], False
  246. else:
  247. seczone_default = \
  248. ConfigData(ZONEMGR_MODULE_SPEC).get_default_value(
  249. 'secondary_zones')
  250. return seczone_default, True
  251. def group_sendmsg(self, msg, group):
  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 cofig 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 (arbitray 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. if tsigctx is not None:
  811. msg.to_wire(renderer, tsigctx)
  812. else:
  813. msg.to_wire(renderer)
  814. # re-read the created data in the parse mode
  815. msg.clear(Message.PARSE)
  816. msg.from_wire(renderer.get_data())
  817. return renderer.get_data()
  818. class TestDDNSSession(unittest.TestCase):
  819. def setUp(self):
  820. self.__cc_session = MyCCSession()
  821. self.assertFalse(self.__cc_session._started)
  822. self.orig_tsig_keyring = isc.server_common.tsig_keyring
  823. isc.server_common.tsig_keyring = FakeKeyringModule()
  824. self.server = ddns.DDNSServer(self.__cc_session)
  825. self.server._UpdateSessionClass = self.__fake_session_creator
  826. self.__faked_result = UPDATE_SUCCESS # will be returned by fake session
  827. self.__sock = FakeSocket(-1)
  828. def tearDown(self):
  829. self.assertTrue(isc.server_common.tsig_keyring.initialized)
  830. isc.server_common.tsig_keyring = self.orig_tsig_keyring
  831. def __fake_session_creator(self, req_message, client_addr, zone_config):
  832. # remember the passed message for possible inspection later.
  833. self.__req_message = req_message
  834. return FakeUpdateSession(req_message, client_addr, zone_config,
  835. self.__faked_result)
  836. def check_update_response(self, resp_wire, expected_rcode=Rcode.NOERROR(),
  837. tsig_ctx=None, tcp=False):
  838. '''Check if given wire data are valid form of update response.
  839. In this implementation, zone/prerequisite/update sections should be
  840. empty in responses.
  841. If tsig_ctx (isc.dns.TSIGContext) is not None, the response should
  842. be TSIG signed and the signature should be verifiable with the context
  843. that has signed the corresponding request.
  844. if tcp is True, the wire data are expected to be prepended with
  845. a 2-byte length field.
  846. '''
  847. if tcp:
  848. data_len = resp_wire[0] * 256 + resp_wire[1]
  849. resp_wire = resp_wire[2:]
  850. self.assertEqual(len(resp_wire), data_len)
  851. msg = Message(Message.PARSE)
  852. msg.from_wire(resp_wire)
  853. if tsig_ctx is not None:
  854. tsig_record = msg.get_tsig_record()
  855. self.assertNotEqual(None, tsig_record)
  856. self.assertEqual(TSIGError.NOERROR,
  857. tsig_ctx.verify(tsig_record, resp_wire))
  858. self.assertEqual(Opcode.UPDATE(), msg.get_opcode())
  859. self.assertEqual(expected_rcode, msg.get_rcode())
  860. self.assertEqual(TEST_QID, msg.get_qid())
  861. for section in [SECTION_ZONE, SECTION_PREREQUISITE, SECTION_UPDATE]:
  862. self.assertEqual(0, msg.get_rr_count(section))
  863. def check_session(self, result=UPDATE_SUCCESS, ipv6=True, tsig_key=None):
  864. # reset test parameters
  865. self.__sock.clear()
  866. self.__faked_result = result
  867. server_addr = TEST_SERVER6 if ipv6 else TEST_SERVER4
  868. client_addr = TEST_CLIENT6 if ipv6 else TEST_CLIENT4
  869. tsig = TSIGContext(tsig_key) if tsig_key is not None else None
  870. rcode = Rcode.NOERROR() if result == UPDATE_SUCCESS else Rcode.REFUSED()
  871. has_response = (result != UPDATE_DROP)
  872. self.assertEqual(has_response,
  873. self.server.handle_request((self.__sock,
  874. server_addr, client_addr,
  875. create_msg(tsigctx=tsig))))
  876. if has_response:
  877. self.assertEqual(client_addr, self.__sock._sent_addr)
  878. self.check_update_response(self.__sock._sent_data, rcode)
  879. else:
  880. self.assertEqual((None, None), (self.__sock._sent_addr,
  881. self.__sock._sent_data))
  882. def test_handle_request(self):
  883. '''Basic request handling without any unexpected errors.'''
  884. # Success, without TSIG
  885. self.check_session()
  886. # Update will be refused with a response.
  887. self.check_session(UPDATE_ERROR, ipv6=False)
  888. # Update will be refused and dropped
  889. self.check_session(UPDATE_DROP)
  890. # Success, with TSIG
  891. self.check_session(ipv6=False, tsig_key=TEST_TSIG_KEY)
  892. # Update will be refused with a response, with TSIG.
  893. self.check_session(UPDATE_ERROR, tsig_key=TEST_TSIG_KEY)
  894. # Update will be refused and dropped, with TSIG (doesn't matter though)
  895. self.check_session(UPDATE_DROP, ipv6=False, tsig_key=TEST_TSIG_KEY)
  896. def test_broken_request(self):
  897. # Message data too short
  898. s = self.__sock
  899. self.assertFalse(self.server.handle_request((self.__sock, None,
  900. None, b'x' * 11)))
  901. self.assertEqual((None, None), (s._sent_data, s._sent_addr))
  902. # Opcode is not UPDATE
  903. self.assertFalse(self.server.handle_request(
  904. (self.__sock, None, None, create_msg(opcode=Opcode.QUERY()))))
  905. self.assertEqual((None, None), (s._sent_data, s._sent_addr))
  906. # TSIG verification error. We use UPDATE_DROP to signal check_session
  907. # that no response should be given.
  908. self.check_session(result=UPDATE_DROP, ipv6=False,
  909. tsig_key=BAD_TSIG_KEY)
  910. def test_socket_error(self):
  911. # Have the faked socket raise an exception on sendto()
  912. self.__sock._raise_on_send = True
  913. # handle_request indicates the failure
  914. self.assertFalse(self.server.handle_request((self.__sock, TEST_SERVER6,
  915. TEST_CLIENT6,
  916. create_msg())))
  917. # this check ensures sendto() was really attempted.
  918. self.check_update_response(self.__sock._sent_data, Rcode.NOERROR())
  919. def test_tcp_request(self):
  920. # A simple case using TCP: all resopnse data are sent out at once.
  921. s = self.__sock
  922. s.proto = socket.IPPROTO_TCP
  923. self.assertTrue(self.server.handle_request((s, TEST_SERVER6,
  924. TEST_CLIENT6,
  925. create_msg())))
  926. self.check_update_response(s._sent_data, Rcode.NOERROR(), tcp=True)
  927. # In the current implementation, the socket should be closed
  928. # immedidately after a successful send.
  929. self.assertEqual(1, s._close_called)
  930. # TCP context shouldn't be held in the server.
  931. self.assertEqual(0, len(self.server._tcp_ctxs))
  932. def test_tcp_request_incomplete(self):
  933. # set the size of the socket "send buffer" to a small value, which
  934. # should cause partial send.
  935. s = self.__sock
  936. s.proto = socket.IPPROTO_TCP
  937. s._send_buflen = 7
  938. # before this request there should be no outstanding TCP context.
  939. self.assertEqual(0, len(self.server._tcp_ctxs))
  940. self.assertTrue(self.server.handle_request((s, TEST_SERVER6,
  941. TEST_CLIENT6,
  942. create_msg())))
  943. # Only the part of data that fit the send buffer should be transmitted.
  944. self.assertEqual(s._send_buflen, len(s._sent_data))
  945. # the socket is not yet closed.
  946. self.assertEqual(0, s._close_called)
  947. # and a new context is stored in the server.
  948. self.assertEqual(1, len(self.server._tcp_ctxs))
  949. # clear the "send buffer" of the fake socket, and continue the send
  950. # by hand. The next attempt should complete the send, and the combined
  951. # data should be the expected response.
  952. s.make_send_ready()
  953. self.assertEqual(DNSTCPContext.SEND_DONE,
  954. self.server._tcp_ctxs[s.fileno()][0].send_ready())
  955. self.check_update_response(s._sent_data, Rcode.NOERROR(), tcp=True)
  956. def test_tcp_request_error(self):
  957. # initial send() on the TCP socket will fail. The request handling
  958. # will be considered failure.
  959. s = self.__sock
  960. s.proto = socket.IPPROTO_TCP
  961. s._raise_on_send = True
  962. self.assertFalse(self.server.handle_request((s, TEST_SERVER6,
  963. TEST_CLIENT6,
  964. create_msg())))
  965. # the socket should have been closed.
  966. self.assertEqual(1, s._close_called)
  967. def test_tcp_request_quota(self):
  968. '''Test'''
  969. # Originally the TCP context map should be empty.
  970. self.assertEqual(0, len(self.server._tcp_ctxs))
  971. class FakeReceiver:
  972. '''Faked SessionReceiver, just returning given param by pop()'''
  973. def __init__(self, param):
  974. self.__param = param
  975. def pop(self):
  976. return self.__param
  977. def check_tcp_ok(fd, expect_grant):
  978. '''Supplemental checker to see if TCP request is handled.'''
  979. s = FakeSocket(fd, proto=socket.IPPROTO_TCP)
  980. s._send_buflen = 7
  981. self.server._socksession_receivers[s.fileno()] = \
  982. (None, FakeReceiver((s, TEST_SERVER6, TEST_CLIENT6,
  983. create_msg())))
  984. self.assertEqual(expect_grant,
  985. self.server.handle_session(s.fileno()))
  986. self.assertEqual(0 if expect_grant else 1, s._close_called)
  987. # By default up to 10 TCP clients can coexist (use hardcode
  988. # intentionally so we can test the default value itself)
  989. for i in range(0, 10):
  990. check_tcp_ok(i, True)
  991. self.assertEqual(10, len(self.server._tcp_ctxs))
  992. # Beyond that, it should be rejected (by reset)
  993. check_tcp_ok(11, False)
  994. # If we remove one context from the server, new client can go in again.
  995. self.server._tcp_ctxs.pop(5)
  996. check_tcp_ok(12, True)
  997. def test_request_message(self):
  998. '''Test if the request message stores RRs separately.'''
  999. # Specify 'drop' so the passed message won't be modified.
  1000. self.__faked_result = UPDATE_DROP
  1001. # Put the same RR twice in the prerequisite section. We should see
  1002. # them as separate RRs.
  1003. dummy_record = RRset(TEST_ZONE_NAME, TEST_RRCLASS, RRType.NS(),
  1004. RRTTL(0))
  1005. dummy_record.add_rdata(Rdata(RRType.NS(), TEST_RRCLASS, "ns.example"))
  1006. self.server.handle_request((self.__sock, TEST_SERVER6, TEST_CLIENT6,
  1007. create_msg(prereq=[dummy_record,
  1008. dummy_record])))
  1009. num_rrsets = len(self.__req_message.get_section(SECTION_PREREQUISITE))
  1010. self.assertEqual(2, num_rrsets)
  1011. def check_session_msg(self, result, expect_recv=1, notify_auth=False):
  1012. '''Check post update communication with other modules.'''
  1013. # iff the update succeeds, b10-ddns should tell interested other
  1014. # modules the information about the update zone. Possible modules
  1015. # are xfrout and auth: for xfrout, the message format should be:
  1016. # {'command': ['notify', {'zone_name': <updated_zone_name>,
  1017. # 'zone_class', <updated_zone_class>}]}
  1018. # for auth, it should be:
  1019. # {'command': ['loadzone', {'origin': <updated_zone_name>,
  1020. # 'class', <updated_zone_class>,
  1021. # 'datasrc', <datasrc type, should be
  1022. # "memory" in practice>}]}
  1023. # and expect an answer by calling group_recvmsg().
  1024. #
  1025. # expect_recv indicates the expected number of calls to
  1026. # group_recvmsg(), which is normally 1, but can be 0 if send fails;
  1027. # if the message is to be sent
  1028. if result == UPDATE_SUCCESS:
  1029. expected_sentmsg = 2 if notify_auth else 1
  1030. self.assertEqual(expected_sentmsg,
  1031. len(self.__cc_session._sent_msg))
  1032. self.assertEqual(expect_recv, self.__cc_session._recvmsg_called)
  1033. msg_cnt = 0
  1034. if notify_auth:
  1035. sent_msg, sent_group = self.__cc_session._sent_msg[msg_cnt]
  1036. sent_cmd = sent_msg['command']
  1037. self.assertEqual('Auth', sent_group)
  1038. self.assertEqual('loadzone', sent_cmd[0])
  1039. self.assertEqual(3, len(sent_cmd[1]))
  1040. self.assertEqual(TEST_ZONE_NAME.to_text(),
  1041. sent_cmd[1]['origin'])
  1042. self.assertEqual(TEST_RRCLASS.to_text(),
  1043. sent_cmd[1]['class'])
  1044. self.assertEqual('memory', sent_cmd[1]['datasrc'])
  1045. msg_cnt += 1
  1046. sent_msg, sent_group = self.__cc_session._sent_msg[msg_cnt]
  1047. sent_cmd = sent_msg['command']
  1048. self.assertEqual('Xfrout', sent_group)
  1049. self.assertEqual(notify_out.ZONE_NEW_DATA_READY_CMD, sent_cmd[0])
  1050. self.assertEqual(2, len(sent_cmd[1]))
  1051. self.assertEqual(TEST_ZONE_NAME.to_text(), sent_cmd[1]['zone_name'])
  1052. self.assertEqual(TEST_RRCLASS.to_text(), sent_cmd[1]['zone_class'])
  1053. else:
  1054. # for other result cases neither send nor recvmsg should be called.
  1055. self.assertEqual([], self.__cc_session._sent_msg)
  1056. self.assertEqual(0, self.__cc_session._recvmsg_called)
  1057. def test_session_msg(self):
  1058. '''Test post update communication with other modules.'''
  1059. # Normal cases, confirming communication takes place iff update
  1060. # succeeds
  1061. for r in [UPDATE_SUCCESS, UPDATE_ERROR, UPDATE_DROP]:
  1062. self.__cc_session.clear_msg()
  1063. self.check_session(result=r)
  1064. self.check_session_msg(r)
  1065. # Return an error from the remote module, which should be just ignored.
  1066. self.__cc_session.clear_msg()
  1067. self.__cc_session._answer_code = 1
  1068. self.check_session()
  1069. self.check_session_msg(UPDATE_SUCCESS)
  1070. # raise some exceptions from the faked session. Expected ones are
  1071. # simply (logged and) ignored
  1072. self.__cc_session.clear_msg()
  1073. self.__cc_session._recvmsg_exception = SessionTimeout('dummy timeout')
  1074. self.check_session()
  1075. self.check_session_msg(UPDATE_SUCCESS)
  1076. self.__cc_session.clear_msg()
  1077. self.__cc_session._recvmsg_exception = SessionError('dummy error')
  1078. self.check_session()
  1079. self.check_session_msg(UPDATE_SUCCESS)
  1080. self.__cc_session.clear_msg()
  1081. self.__cc_session._recvmsg_exception = ProtocolError('dummy perror')
  1082. self.check_session()
  1083. self.check_session_msg(UPDATE_SUCCESS)
  1084. # Similar to the previous cases, but sendmsg() raises, so there should
  1085. # be no call to recvmsg().
  1086. self.__cc_session.clear_msg()
  1087. self.__cc_session._sendmsg_exception = SessionError('send error')
  1088. self.check_session()
  1089. self.check_session_msg(UPDATE_SUCCESS, expect_recv=0)
  1090. # Unexpected exception will be propagated (and will terminate the
  1091. # server)
  1092. self.__cc_session.clear_msg()
  1093. self.__cc_session._sendmsg_exception = RuntimeError('unexpected')
  1094. self.assertRaises(RuntimeError, self.check_session)
  1095. def test_session_msg_for_auth(self):
  1096. '''Test post update communication with other modules including Auth.'''
  1097. # Let the CC session return in-memory config with sqlite3 backend.
  1098. # (The default case was covered by other tests.)
  1099. self.__cc_session.auth_datasources = \
  1100. [{'type': 'memory', 'class': 'IN', 'zones': [
  1101. {'origin': TEST_ZONE_NAME_STR, 'filetype': 'sqlite3'}]}]
  1102. self.check_session()
  1103. self.check_session_msg(UPDATE_SUCCESS, expect_recv=2, notify_auth=True)
  1104. # Let sendmsg() raise an exception. The first exception shouldn't
  1105. # stop sending the second message. There's just no recv calls.
  1106. self.__cc_session.clear_msg()
  1107. self.__cc_session._sendmsg_exception = SessionError('send error')
  1108. self.check_session()
  1109. self.check_session_msg(UPDATE_SUCCESS, expect_recv=0, notify_auth=True)
  1110. # Likewise, in the case recvmsg() raises (and there should be recv
  1111. # calls in this case)
  1112. self.__cc_session.clear_msg()
  1113. self.__cc_session._recvmsg_exception = SessionError('recv error')
  1114. self.check_session()
  1115. self.check_session_msg(UPDATE_SUCCESS, expect_recv=2, notify_auth=True)
  1116. def test_session_with_config(self):
  1117. '''Check a session with more realistic config setups.
  1118. We don't have to explore various cases in detail in this test.
  1119. We're just checking if the expected configured objects are passed
  1120. to the session object.
  1121. '''
  1122. # reset the session class to the real one
  1123. self.server._UpdateSessionClass = isc.ddns.session.UpdateSession
  1124. # install all-drop ACL
  1125. new_config = { 'zones': [ { 'origin': TEST_ZONE_NAME_STR,
  1126. 'class': TEST_RRCLASS_STR,
  1127. 'update_acl': [{'action': 'DROP'}] } ] }
  1128. answer = self.server.config_handler(new_config)
  1129. self.assertEqual((0, None), isc.config.parse_answer(answer))
  1130. # check the result
  1131. self.check_session(UPDATE_DROP)
  1132. class TestMain(unittest.TestCase):
  1133. def setUp(self):
  1134. self._server = MyDDNSServer()
  1135. self.__orig_clear = ddns.clear_socket
  1136. ddns.clear_socket = self.__clear_socket
  1137. self.__clear_called = False
  1138. def tearDown(self):
  1139. ddns.clear_socket = self.__orig_clear
  1140. def test_main(self):
  1141. self.assertFalse(self._server.run_called)
  1142. ddns.main(self._server)
  1143. self.assertTrue(self._server.run_called)
  1144. self.assertTrue(self.__clear_called)
  1145. def __clear_socket(self):
  1146. self.__clear_called = True
  1147. # Get rid of the socket file too
  1148. self.__orig_clear()
  1149. def check_exception(self, ex):
  1150. '''Common test sequence to see if the given exception is caused.
  1151. '''
  1152. # Should technically not be necessary, but reset server to be sure
  1153. self._server.reset()
  1154. self.assertFalse(self._server.exception_raised)
  1155. self._server.set_exception(ex)
  1156. ddns.main(self._server)
  1157. self.assertTrue(self._server.exception_raised)
  1158. def test_exceptions(self):
  1159. '''
  1160. Test whether exceptions are caught in main()
  1161. These exceptions should not bubble up.
  1162. '''
  1163. self._server.set_exception(KeyboardInterrupt())
  1164. self.assertFalse(self._server.exception_raised)
  1165. ddns.main(self._server)
  1166. self.assertTrue(self._server.exception_raised)
  1167. self.check_exception(isc.cc.SessionError("error"))
  1168. self.check_exception(isc.config.ModuleCCSessionError("error"))
  1169. self.check_exception(ddns.DDNSConfigError("error"))
  1170. self.check_exception(isc.cc.SessionTimeout("error"))
  1171. self.check_exception(Exception("error"))
  1172. # Add one that is not a subclass of Exception, and hence not
  1173. # caught. Misuse BaseException for that.
  1174. self._server.reset()
  1175. self.assertFalse(self._server.exception_raised)
  1176. self._server.set_exception(BaseException("error"))
  1177. self.assertRaises(BaseException, ddns.main, self._server)
  1178. self.assertTrue(self._server.exception_raised)
  1179. class TestConfig(unittest.TestCase):
  1180. '''Test some simple config related things that don't need server. '''
  1181. def setUp(self):
  1182. self.__ccsession = MyCCSession()
  1183. def test_file_path(self):
  1184. # Check some common paths
  1185. self.assertEqual(os.environ["B10_FROM_BUILD"] + "/ddns_socket",
  1186. ddns.SOCKET_FILE)
  1187. self.assertEqual(os.environ["B10_FROM_SOURCE"] +
  1188. "/src/bin/ddns/ddns.spec", ddns.SPECFILE_LOCATION)
  1189. if __name__== "__main__":
  1190. isc.log.resetUnitTestRootLogger()
  1191. unittest.main()