Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if self.number_of_bits is None:
number_of_bytes = decoder.read_length_determinant()
number_of_unused_bits = decoder.read_byte()
number_of_bytes -= 1
number_of_bits = (8 * number_of_bytes - number_of_unused_bits)
else:
number_of_bytes = (self.number_of_bits + 7) // 8
number_of_bits = self.number_of_bits
return (decoder.read_bytes(number_of_bytes), number_of_bits)
def __repr__(self):
return 'BitString({})'.format(self.name)
class OctetString(Type):
def __init__(self, name, minimum, maximum, has_extension_marker):
super(OctetString, self).__init__(name,
'OCTET STRING',
Tag.OCTET_STRING)
self.number_of_bytes = None
self.set_size_range(minimum, maximum, has_extension_marker)
def set_size_range(self, minimum, maximum, has_extension_marker):
if minimum is not None or maximum is not None:
if not has_extension_marker:
if minimum == maximum:
self.number_of_bytes = minimum
def encode(self, data, encoder):
if self.number_of_bytes is None:
def test_bmp_string(self):
foo = asn1tools.compile_string(
"Foo DEFINITIONS AUTOMATIC TAGS ::= "
"BEGIN "
"A ::= BMPString "
"B ::= SEQUENCE { "
" a BOOLEAN, "
" b BMPString "
"} "
"C ::= SEQUENCE { "
" a BMPString (SIZE(1..128)), "
" b BMPString (SIZE(1..256)) "
"} "
"END",
'uper')
datas = [
('A', '', b'\x00'),
def test_bit_string(self):
foo = asn1tools.compile_string(
"Foo DEFINITIONS AUTOMATIC TAGS ::= "
"BEGIN "
"A ::= BIT STRING "
"B ::= BIT STRING (SIZE (9)) "
"C ::= BIT STRING (SIZE (5..7)) "
"D ::= SEQUENCE { "
" a BOOLEAN, "
" b BIT STRING "
"} "
"E ::= BIT STRING { "
" a (0), "
" b (1), "
" c (2) "
"} "
"F ::= SEQUENCE { "
" a BIT STRING, "
def test_error_out_of_data(self):
foo = asn1tools.compile_string(
"Foo DEFINITIONS AUTOMATIC TAGS ::= "
"BEGIN "
"A ::= INTEGER "
"B ::= SEQUENCE { "
" a SEQUENCE { "
" b BOOLEAN, "
" c INTEGER "
" } "
"} "
"END",
'uper')
datas = [
('A', b'', 'out of data at bit offset 0 (0.0 bytes)'),
('B', b'\x00', 'a: c: out of data at bit offset 1 (0.1 bytes)'),
('B', b'\x80\x80', 'a: c: out of data at bit offset 9 (1.1 bytes)')
def decode_codecs(self, type_spec, decoded, encoded):
spec = (
"Foo DEFINITIONS AUTOMATIC TAGS ::= "
"BEGIN "
"A ::= " + type_spec + " "
+ "END"
)
for codec, encoded_message in zip(CODECS, encoded):
foo = asn1tools.compile_string(spec, codec)
decoded_message = foo.decode('A',
encoded_message,
check_constraints=True)
self.assertEqual(decoded_message, decoded)
def test_compile_error_members_backtrace(self):
for codec, module in CODECS_AND_MODULES:
foo = asn1tools.compile_string(
'Foo DEFINITIONS AUTOMATIC TAGS ::= BEGIN '
' A ::= SEQUENCE { '
' a CHOICE { '
' b INTEGER '
' } '
' } '
'END',
codec)
with self.assertRaises(asn1tools.errors.Error) as cm:
module.generate(foo, 'foo')
self.assertEqual(str(cm.exception),
"Foo.A.a.b: INTEGER has no minimum value.")
def test_sequence_of(self):
foo = asn1tools.compile_string(
"Foo DEFINITIONS ::= "
"BEGIN "
"A ::= SEQUENCE OF INTEGER "
"END",
'jer')
datas = [
('A', [], b'[]'),
('A', [1, 3], b'[1, 3]')
]
for type_name, decoded, encoded in datas:
self.assert_encode_decode_string(foo, type_name, decoded, encoded)
def test_generalized_time(self):
foo = asn1tools.compile_string(
"Foo DEFINITIONS ::= "
"BEGIN "
"A ::= GeneralizedTime "
"END",
'jer')
datas = [
('A', gt2dt('20001231235959.999'), b'"20001231235959.999"')
]
for type_name, decoded, encoded in datas:
self.assert_encode_decode_string(foo, type_name, decoded, encoded)
def test_compile_error_integer_no_minimum(self):
for codec, module in CODECS_AND_MODULES:
foo = asn1tools.compile_string(
'Foo DEFINITIONS AUTOMATIC TAGS ::= BEGIN '
' A ::= INTEGER (MIN..10) '
'END',
codec)
with self.assertRaises(asn1tools.errors.Error) as cm:
module.generate(foo, 'foo')
self.assertEqual(str(cm.exception),
"Foo.A: INTEGER has no minimum value.")
def test_sequence_of(self):
foo = asn1tools.compile_string(
"Foo DEFINITIONS AUTOMATIC TAGS ::= "
"BEGIN "
"A ::= SEQUENCE OF INTEGER "
"B ::= SEQUENCE OF A "
"C ::= SEQUENCE OF BOOLEAN "
"D ::= SEQUENCE OF E "
"E ::= BOOLEAN "
"F ::= SEQUENCE OF CHOICE { a BOOLEAN, b INTEGER } "
"G ::= SEQUENCE OF ENUMERATED { one } "
"H ::= SEQUENCE OF SEQUENCE { a INTEGER } "
"I ::= SEQUENCE OF BIT STRING "
"J ::= SEQUENCE OF OCTET STRING "
"K ::= SEQUENCE OF OBJECT IDENTIFIER "
"L ::= SEQUENCE OF SEQUENCE OF NULL "
"M ::= SEQUENCE OF SET OF NULL "
"END",