Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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",
def encode_decode(codec):
spec = asn1tools.compile_string(
'Ber DEFINITIONS ::= BEGIN '
' A ::= SEQUENCE { '
' a BOOLEAN, '
' b INTEGER, '
# ' c REAL, '
' d NULL, '
' e BIT STRING, '
' f OCTET STRING, '
' g OBJECT IDENTIFIER, '
' h ENUMERATED {a, b}, '
' i SEQUENCE {}, '
' j SEQUENCE OF NULL, '
' k SET {}, '
' l SET OF NULL, '
' m CHOICE {a NULL}, '
' n UTF8String, '