def test_invalid_family(self): """ Test it rejects invalid address family. """ # Note: this produces a bad logger output, since this address # can not be converted to string, so the original message with # placeholders is output. This should not happen in practice, so # it is harmless. addr = IPAddr('0.0.0.0') addr.family = 42 self.assertRaises(ValueError, Parser(FakeCreator([])).get_socket, addr, 42, socket.SOCK_DGRAM)
def test_invalid_socktype(self): """ Test invalid socket type is rejected """ self.assertRaises(ValueError, Parser(FakeCreator([])).get_socket, IPAddr('0.0.0.0'), 42, 'RAW')
def __error(self, plan): creator = FakeCreator(plan) parser = Parser(creator) try: parser.get_socket(IPAddr('0.0.0.0'), 0, socket.SOCK_DGRAM) self.fail("Not raised") except CreatorError as ce: self.assertTrue(creator.all_used()) self.assertTrue(ce.fatal)
def setUp(self): """ Creates the socket to be tested. It also creates other useful test variables. """ Test.setUp(self) self.__address = IPAddr("192.0.2.1") self.__socket = isc.bind10.socket_cache.Socket('UDP', self.__address, 1024, 42)
def addr_parse(addr): """ Checks and parses an IP address (either IPv4 or IPv6) and returns the IPAddr object. It raises ValueError if the passed string is not valid IP address. """ try: return IPAddr(addr) except InvalidAddress: raise ValueError('Value ' + addr + ' is not valid IPv4 or IPv6 address')
def test_create_terminated(self): """ Test we can't request sockets after it was terminated. """ parser = self.__terminate() try: parser.get_socket(IPAddr('0.0.0.0'), 0, 'UDP') self.fail("Not raised") except CreatorError as ce: self.assertTrue(ce.fatal) self.assertEqual(None, ce.errno)
def setUp(self): """ Creates the cache for tests with us being the socket creator. Also creates some more variables for testing. """ Test.setUp(self) self.__cache = isc.bind10.socket_cache.Cache(self) self.__address = IPAddr("192.0.2.1") self.__socket = isc.bind10.socket_cache.Socket('UDP', self.__address, 1024, 42) self.__get_socket_called = False
def test_crash(self): """ Tests that the parser correctly raises exception when it crashes unexpectedly. """ creator = FakeCreator([('s', b'SU4\0\0\0\0\0\0'), ('r', b'')]) parser = Parser(creator) try: parser.get_socket(IPAddr('0.0.0.0'), 0, 'UDP') self.fail("Not raised") except CreatorError as ce: self.assertTrue(creator.all_used()) # Is the exception correct? self.assertTrue(ce.fatal) self.assertEqual(None, ce.errno)
def test_error(self): """ Tests that the parser correctly raises non-fatal exception when the socket can not be created. """ # We split the int to see if it can cope with data coming in # different packets intpart = struct.pack('@i', 42) creator = FakeCreator([('s', b'SU4\0\0\0\0\0\0'), ('r', b'ES' + intpart[:1]), ('r', intpart[1:])]) parser = Parser(creator) try: parser.get_socket(IPAddr('0.0.0.0'), 0, 'UDP') self.fail("Not raised") except CreatorError as ce: self.assertTrue(creator.all_used()) # Is the exception correct? self.assertFalse(ce.fatal) self.assertEqual(42, ce.errno)
def __create(self, addr, socktype, encoded): creator = FakeCreator([('s', b'S' + encoded), ('r', b'S'), ('f', 42)]) parser = Parser(creator) self.assertEqual(42, parser.get_socket(IPAddr(addr), 42, socktype))