def test_absent_option_echo_request(self): relayed_solicit_message = RelayForwardMessage( hop_count=1, link_address=IPv6Address('2001:db8:ffff:1::1'), peer_address=IPv6Address('fe80::3631:c4ff:fe3c:b2f1'), options=[ RelayMessageOption(relayed_message=SolicitMessage( transaction_id=bytes.fromhex('f350d6'), options=[ ElapsedTimeOption(elapsed_time=0), ClientIdOption(duid=LinkLayerDUID(hardware_type=1, link_layer_address=bytes.fromhex('3431c43cb2f1'))), IANAOption(iaid=bytes.fromhex('c43cb2f1')), ], )), EchoRequestOption(requested_options=[OPTION_SUBSCRIBER_ID]), UnknownOption(option_type=65535), InterfaceIdOption(interface_id=b'Fa2/3'), RemoteIdOption(enterprise_number=9, remote_id=bytes.fromhex('020023000001000a0003000100211c7d486e')), ] ) bundle = TransactionBundle(incoming_message=relayed_solicit_message, received_over_multicast=True) self.message_handler.handle(bundle, StatisticsSet()) self.assertIsInstance(bundle.outgoing_message, RelayReplyMessage) self.assertEqual(len(bundle.outgoing_message.options), 2) self.assertIsInstance(bundle.outgoing_message.options[0], InterfaceIdOption) self.assertIsInstance(bundle.outgoing_message.options[1], RelayMessageOption)
class OptionTestCase(unittest.TestCase): def setUp(self): # The following attributes must be overruled by child classes # The basics are tested with a simple UnknownOption self.option_bytes = bytes.fromhex('ffff0010') + b'0123456789abcdef' self.option_object = UnknownOption(65535, b'0123456789abcdef') self.parse_option() def parse_option(self): self.length, self.option = Option.parse(self.option_bytes) self.assertIsInstance(self.option, Option) self.option_class = type(self.option) def test_length(self): self.assertEqual(self.length, len(self.option_bytes)) def test_parse(self): self.assertEqual(self.option, self.option_object) def test_save_parsed(self): self.assertEqual(self.option_bytes, self.option.save()) def test_save_fixture(self): self.assertEqual(self.option_bytes, self.option_object.save()) def test_validate(self): # This should be ok self.option.validate() def test_overflow(self): with self.assertRaisesRegex(ValueError, 'longer than .* buffer'): self.option_class.parse(self.option_bytes, length=len(self.option_bytes) - 1) def test_load_from_wrong_buffer(self): if issubclass(self.option_class, UnknownOption): # UnknownOption accepts any parsable buffer, no point in testing that one return option = self.option_class() with self.assertRaisesRegex(ValueError, 'buffer does not contain'): option.load_from(bytes.fromhex('fffe0000'))
def setUp(self): # The following attributes must be overruled by child classes # The basics are tested with a simple UnknownOption self.option_bytes = bytes.fromhex('ffff0010') + b'0123456789abcdef' self.option_object = UnknownOption(65535, b'0123456789abcdef') self.parse_option()
class OptionTestCase(unittest.TestCase): def setUp(self): # The following attributes must be overruled by child classes # The basics are tested with a simple UnknownOption self.option_bytes = bytes.fromhex('ffff0010') + b'0123456789abcdef' self.option_object = UnknownOption(65535, b'0123456789abcdef') self.parse_option() def parse_option(self): self.length, self.option = Option.parse(self.option_bytes) self.assertIsInstance(self.option, Option) self.option_class = type(self.option) def test_length(self): self.assertEqual(self.length, len(self.option_bytes)) def test_parse(self): self.assertEqual(self.option, self.option_object) def test_save_parsed(self): self.assertEqual(self.option_bytes, self.option.save()) def test_save_fixture(self): self.assertEqual(self.option_bytes, self.option_object.save()) def test_validate(self): # This should be ok self.option.validate() def test_overflow(self): with self.assertRaisesRegex(ValueError, 'longer than .* buffer'): self.option_class.parse(self.option_bytes, length=len(self.option_bytes) - 1) def test_load_from_wrong_buffer(self): if issubclass(self.option_class, UnknownOption): # UnknownOption accepts any parseable buffer, no point in testing that one return option = self.option_class() with self.assertRaisesRegex(ValueError, 'buffer does not contain'): option.load_from(bytes.fromhex('fffe0000')) def check_unsigned_integer_property(self, property_name: str, size: int = None): """ Perform basic verification of validation of an unsigned integer :param property_name: The property under test :param size: The number of bits of this integer field """ # Do the basic integer checks setattr(self.option, property_name, 0.1) with self.assertRaisesRegex(ValueError, 'integer'): self.option.validate() setattr(self.option, property_name, 0) self.option.validate() setattr(self.option, property_name, -1) with self.assertRaisesRegex(ValueError, 'unsigned .* integer'): self.option.validate() if not size: # We can't do any further tests without knowing the size return setattr(self.option, property_name, 2**size - 1) self.option.validate() setattr(self.option, property_name, 2**size) with self.assertRaisesRegex(ValueError, 'unsigned {} bit integer'.format(size)): self.option.validate()
def setUp(self): self.option_bytes = b'\x00\xff\x00\x100123456789abcdef' self.option_object = UnknownOption(255, b'0123456789abcdef') self.parse_option()
class OptionTestCase(unittest.TestCase): def setUp(self): # The following attributes must be overruled by child classes # The basics are tested with a simple UnknownOption self.option_bytes = bytes.fromhex('ffff0010') + b'0123456789abcdef' self.option_object = UnknownOption(65535, b'0123456789abcdef') self.parse_option() def parse_option(self): self.length, self.option = Option.parse(self.option_bytes) self.assertIsInstance(self.option, Option) self.option_class = type(self.option) def test_length(self): self.assertEqual(self.length, len(self.option_bytes)) def test_parse(self): self.assertEqual(self.option, self.option_object) def test_save_parsed(self): self.assertEqual(self.option_bytes, self.option.save()) def test_save_fixture(self): self.assertEqual(self.option_bytes, self.option_object.save()) def test_validate(self): # This should be ok self.option.validate() def test_overflow(self): with self.assertRaisesRegex(ValueError, 'longer than .* buffer'): self.option_class.parse(self.option_bytes, length=len(self.option_bytes) - 1) def test_load_from_wrong_buffer(self): if issubclass(self.option_class, UnknownOption): # UnknownOption accepts any parseable buffer, no point in testing that one return option = self.option_class() with self.assertRaisesRegex(ValueError, 'buffer does not contain'): option.load_from(bytes.fromhex('fffe0000')) def check_unsigned_integer_property(self, property_name: str, size: int = None): """ Perform basic verification of validation of an unsigned integer :param property_name: The property under test :param size: The number of bits of this integer field """ # Do the basic integer checks setattr(self.option, property_name, 0.1) with self.assertRaisesRegex(ValueError, 'integer'): self.option.validate() setattr(self.option, property_name, 0) self.option.validate() setattr(self.option, property_name, -1) with self.assertRaisesRegex(ValueError, 'unsigned .* integer'): self.option.validate() if not size: # We can't do any further tests without knowing the size return setattr(self.option, property_name, 2 ** size - 1) self.option.validate() setattr(self.option, property_name, 2 ** size) with self.assertRaisesRegex(ValueError, 'unsigned {} bit integer'.format(size)): self.option.validate()