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 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_invalid_socktype(self): """ Test invalid socket type is rejected """ self.assertRaises(ValueError, Parser(FakeCreator([])).get_socket, IPAddr('0.0.0.0'), 42, 'RAW')
def test_terminate_error3(self): """ Test it reports an exception when there's error terminating the creator. This one sends data when it should have terminated. """ creator = FakeCreator([('s', b'T'), ('r', b'Extra data')]) parser = Parser(creator) self.__terminate_raises(parser)
def test_terminate_error2(self): """ Test it reports an exception when there's error terminating the creator. This one raises an error when sending data. """ creator = FakeCreator([('e', None)]) parser = Parser(creator) self.__terminate_raises(parser)
def test_terminate_error1(self): """ Test it reports an exception when there's error terminating the creator. This one raises an error when receiving the EOF. """ creator = FakeCreator([('s', b'T'), ('e', None)]) parser = Parser(creator) self.__terminate_raises(parser)
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 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 __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))
def __terminate(self): creator = FakeCreator([('s', b'T'), ('r', b'')]) parser = Parser(creator) self.assertEqual(None, parser.terminate()) self.assertTrue(creator.all_used()) return parser