def parse_data(self, data): """ Populates the TSRequest object with the data supplied. Need to override the default ASN1Sequence class as this structure has optional values which hasn't been implemented in the generic structure :param data: An ASN.1 data structure to be parsed """ type_byte = struct.unpack('B', data[:1])[0] if type_byte != self.type: raise AsnStructureException("Expecting %s type to be (%x), was (%x)" % (self.name, self.type, type_byte)) decoded_data, total_bytes = asn_helper.unpack_asn1(data) # Remove the bytes from the original type and length for comparison later total_bytes -= total_bytes - len(decoded_data) version_offset = asn_helper.parse_context_field(decoded_data, self['version']) new_offset = version_offset # Get the remaining values in the structure while new_offset != total_bytes: invalid_sequence = True field_data = decoded_data[new_offset:] sequence_byte = struct.unpack('B', field_data[:1])[0] for field in self.fields: field_info = self.fields[field] if sequence_byte == field_info.sequence: invalid_sequence = False value_offset = asn_helper.parse_context_field(field_data, self[field]) new_offset += value_offset if invalid_sequence: raise AsnStructureException('Unknown sequence byte (%x) in sequence' % sequence_byte)
def test_unpack_long_form_three_octets(self): test_data = hex_to_byte('02 83 01 00 00') + hex_to_byte('31') * 65536 expected_data = b'1' * 65536 expected_octets = 65541 actual_data, actual_octets = unpack_asn1(test_data) assert actual_data == expected_data assert actual_octets == expected_octets
def test_unpack_long_form_two_octets(self): test_data = hex_to_byte('02 82 01 00') + hex_to_byte('31') * 256 expected_data = b'1' * 256 expected_octets = 260 actual_data, actual_octets = unpack_asn1(test_data) assert actual_data == expected_data assert actual_octets == expected_octets
def test_unpack_long_form_one_octet(self): test_data = hex_to_byte('02 81 80') + hex_to_byte('31') * 128 expected_data = b'1' * 128 expected_octets = 131 actual_data, actual_octets = unpack_asn1(test_data) assert actual_data == expected_data assert actual_octets == expected_octets
def test_unpack_short_form(self): test_data = hex_to_byte('02 01 31') expected_data = b'1' expected_octets = 3 actual_data, actual_octets = unpack_asn1(test_data) assert actual_data == expected_data assert actual_octets == expected_octets
def test_unpack_long_form_fource_octets(self): test_data = hex_to_byte( '02 84 01 00 00 00') + hex_to_byte('31') * 16777216 expected_data = b'1' * 16777216 expected_octets = 16777222 actual_data, actual_octets = unpack_asn1(test_data) assert actual_data == expected_data assert actual_octets == expected_octets