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_unsupported_codec(self):
with self.assertRaises(asn1tools.CompileError) as cm:
asn1tools.compile_files('tests/files/foo.asn', 'bad_codec')
self.assertEqual(str(cm.exception), "Unsupported codec 'bad_codec'.")
def encode_decode_codecs_file(self,
filename,
type_name,
decoded,
encoded):
for codec, encoded_message in zip(ALL_CODECS, encoded):
foo = asn1tools.compile_files(filename, codec)
self.encode_decode_codec(foo,
codec,
type_name,
decoded,
encoded_message)
def test_bar(self):
"""A simple example.
"""
bar = asn1tools.compile_files('tests/files/bar.asn')
# Message 1.
decoded = {
'headerOnly': True,
'lock': False,
'acceptTypes': {
'standardTypes': [(b'\x40', 4), (b'\x80', 4)]
},
'url': b'/ses/magic/moxen.html'
}
encoded = (
b'\x60\x29\x01\x01\xff\x01\x01\x00\x61\x0a\xa0\x08\x03\x02\x04'
b'\x40\x03\x02\x04\x80\x04\x15\x2f\x73\x65\x73\x2f\x6d\x61\x67'
b'\x69\x63\x2f\x6d\x6f\x78\x65\x6e\x2e\x68\x74\x6d\x6c'
)
def test_information_object(self):
# ToDo: Fix when supported.
return
information_object = asn1tools.compile_files(
'tests/files/information_object.asn', 'per')
# Message 1 - without constraints.
decoded_message = {
'id': 0,
'value': b'\x05',
'comment': 'item 0',
'extra': 2
}
encoded_message = (
b'\x01\x00\x01\x05\x06\x69\x74\x65\x6d\x20\x30\x01\x02'
)
self.assert_encode_decode(information_object,
'ItemWithoutConstraints',
'Expected an integer between 2 and 2, but got 1.'
),
(
'C',
-1,
'Expected an integer between 0 and 1, but got -1.'
),
(
'C',
2,
'Expected an integer between 0 and 1, but got 2.'
)
]
for codec in ALL_CODECS:
foo = asn1tools.compile_files('tests/files/named_numbers.asn',
codec)
for type_name, decoded, message in datas:
with self.assertRaises(asn1tools.ConstraintsError) as cm:
foo.encode(type_name, decoded, check_constraints=True)
self.assertEqual(str(cm.exception), message)
def test_foo_indent(self):
foo = asn1tools.compile_files(['tests/files/foo.asn'], 'gser')
datas = [
('Question',
{'id': 1, 'question': 'Is 1+1=3?'},
b'question Question ::= {\n'
b' id 1,\n'
b' question "Is 1+1=3?"\n'
b'}')
]
for name, decoded, encoded in datas:
actual = foo.encode(name, decoded, indent=2)
self.assertEqual(actual, encoded)
def test_rfc1157(self):
rfc1157 = asn1tools.compile_files([
'tests/files/ietf/rfc1155.asn',
'tests/files/ietf/rfc1157.asn'
])
# First message.
decoded = {
"version": 0,
"community": b'public',
"data": (
"set-request",
{
"request-id": 60,
"error-status": 0,
"error-index": 0,
"variable-bindings": [
{
def asn1tools_encode_decode():
snmp_v1 = asn1tools.compile_files(SNMP_V1_ASN_PATHS)
def encode():
snmp_v1.encode('Message',
DECODED_MESSAGE_ASN1TOOLS,
check_types=False)
def decode():
snmp_v1.decode('Message', ENCODED_MESSAGE)
encode_time = timeit.timeit(encode, number=ITERATIONS)
decode_time = timeit.timeit(decode, number=ITERATIONS)
return encode_time, decode_time
def compile_file():
asn1tools.compile_files(RRC_8_6_0_ASN_PATH, cache_dir=CACHE_DIR)
def main():
foo_v1 = asn1tools.compile_files(FOO_V1_ASN_PATH, 'uper')
foo_v2 = asn1tools.compile_files(FOO_V2_ASN_PATH, 'uper')
foo_v2_em = asn1tools.compile_files(FOO_V2_EM_ASN_PATH, 'uper')
decoded_v1 = (
'foo',
{
'a': True,
'b': 55,
'c': 3,
'd': False,
'e': 'on'
}
)
decoded_v2 = (
'foo',
{
'a': True,