def test_parse_error_empty_string(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('') self.assertEqual(str(cm.exception), "Invalid ASN.1 syntax at line 1, column 1: '>!<': " "Expected modulereference.")
def test_parse_error_begin_missing(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= END') self.assertEqual(str(cm.exception), "Invalid ASN.1 syntax at line 1, column 19: " "'A DEFINITIONS ::= >!<END': Expected BEGIN.")
def test_parse_error_end_missing(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN') self.assertEqual(str(cm.exception), "Invalid ASN.1 syntax at line 1, column 24: " "'A DEFINITIONS ::= BEGIN>!<': Expected END.")
def test_parse_error_missing_type(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'B ::= ' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 31: 'A DEFINITIONS ::= BEGIN " "B ::= >!<END': Expected Type.")
def test_parse_error_missing_single_line_comment_end(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= \n' 'BEGIN -- END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 2, column 7: 'BEGIN >!<-- END': " "Missing newline or -- for single line comment.")
def test_parse_error_value_assignment_missing_assignment(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN a INTEGER END') self.assertEqual(str(cm.exception), "Invalid ASN.1 syntax at line 1, column 35: " "'A DEFINITIONS ::= BEGIN a INTEGER >!<END': " "Expected ::=.")
def test_parse_error_multi_line_comment_overlapping(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= \n' 'BEGIN /*/ END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 2, column 7: 'BEGIN >!</*/ END': " "Missing */ for multi line comment.")
def test_parse_error_end_missing_with_comments(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS -- g -- \n' '-- hhhh\n' '::= BEGIN ') self.assertEqual(str(cm.exception), "Invalid ASN.1 syntax at line 3, column 11: " "'::= BEGIN >!<': Expected END.")
def test_parse_error_size_constraint_missing_parentheses(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'B ::= INTEGER (SIZE 1)' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 45: \'A DEFINITIONS ::= " "BEGIN B ::= INTEGER (SIZE >!<1)END\': Expected '('.")
def test_parse_error_missing_union_member_end(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'B ::= INTEGER (SIZE (1) |)' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 39: \'A DEFINITIONS " "::= BEGIN B ::= INTEGER >!<(SIZE (1) |)END\': Expected END.")
def test_parse_error_definitive_identifier(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A {} DEFINITIONS ::= BEGIN ' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 4: 'A {>!<} DEFINITIONS " "::= BEGIN END': Expected {{identifier Suppress:(\"(\") - " "definitiveNumberForm - Suppress:(\")\")} | identifier | " "definitiveNumberForm}.")
def test_parse_error_tag_class_number_missing(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'B ::= [] INTEGER ' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 32: 'A DEFINITIONS " "::= BEGIN B ::= [>!<] INTEGER END': Expected ClassNumber.")
def test_parse_error_sequence_missing_member_name(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN' ' A ::= SEQUENCE { A } ' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 43: 'A DEFINITIONS ::= " "BEGIN A ::= SEQUENCE { >!<A } END': Expected Type.")
def test_parse_error_missing_union_member_beginning(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'B ::= INTEGER (| SIZE (1))' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 40: 'A DEFINITIONS ::= BEGIN " "B ::= INTEGER (>!<| SIZE (1))END': Expected one or more constraints." )
def test_parse_error_size_constraint_missing_size(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'B ::= INTEGER (SIZE ())' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 46: 'A DEFINITIONS ::= " "BEGIN B ::= INTEGER (SIZE (>!<))END': Expected one or more " "constraints.")
def test_parse_imports_single_value_reference(self): """Test that a value reference, in this test 'c', is not parsed as an assignmed identifier, but an imported value from 'D'. """ actual = asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'IMPORTS ' 'A FROM B ' 'c FROM D; ' 'END') expected = { 'A': { 'extensibility-implied': False, 'imports': { 'B': ['A'], 'D': ['c'] }, 'object-classes': {}, 'object-sets': {}, 'types': {}, 'values': {} } } self.assertEqual(actual, expected)
class PublicKeyCodecBase: _CodecDict = asn1tools.parse_string( """ -- Simplified public key definition based on IETF/RFC5912 -- Support RSA, DSA and ECC (**named curve only**) public keys SimplifiedPublicKey DEFINITIONS IMPLICIT TAGS ::= BEGIN PublicKey ::= SEQUENCE { algorithm SEQUENCE { algorithmIdentifier OBJECT IDENTIFIER, algorithmParameters CHOICE { rsaParams NULL, dsaParams SEQUENCE { p INTEGER, q INTEGER, g INTEGER }, ecNamedCurve OBJECT IDENTIFIER } OPTIONAL }, subjectPublicKeyInfo BIT STRING } END """ ) @classmethod def decode(cls, input_: AnyStr) -> AnyStr: return cls._Codec.decode("PublicKey", input_) @classmethod def encode(cls, input_: AnyStr) -> AnyStr: return cls._Codec.encode("PublicKey", input_)
def test_parse_error_late_extension_additions(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'Foo ::= SEQUENCE { ' 'a BOOLEAN, ' '..., ' '..., ' '[[ ' 'c BOOLEAN ' ']] ' '} ' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 63: \'A DEFINITIONS ::= " "BEGIN Foo ::= SEQUENCE { a BOOLEAN, ..., ...>!<, [[ c BOOLEAN ]] " "} END\': Expected Type.")
def test_parse_error_too_many_extension_markers(self): with self.assertRaises(asn1tools.ParseError) as cm: asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'Foo ::= SEQUENCE { ' 'a BOOLEAN, ' '..., ' '[[ ' 'b BOOLEAN ' ']], ' '[[ ' 'c BOOLEAN ' ']], ' '..., ' 'd BOOLEAN, ' '... ' '} ' 'END') self.assertEqual( str(cm.exception), "Invalid ASN.1 syntax at line 1, column 108: \'A DEFINITIONS ::= " "BEGIN Foo ::= SEQUENCE { a BOOLEAN, ..., [[ b BOOLEAN ]], [[ c " "BOOLEAN ]], ..., d BOOLEAN>!<, ... } END\': Expected Type.")
def test_parse_empty_imports(self): actual = asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'IMPORTS ; ' 'END') expected = { 'A': { 'extensibility-implied': False, 'imports': {}, 'object-classes': {}, 'object-sets': {}, 'types': {}, 'values': {} } } self.assertEqual(actual, expected)
def test_parse_keyword_in_type_name(self): actual = asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'ENDa ::= INTEGER ' 'END') expected = { 'A': { 'extensibility-implied': False, 'imports': {}, 'object-classes': {}, 'object-sets': {}, 'types': {'ENDa': {'type': 'INTEGER'}}, 'values': {} } } self.assertEqual(actual, expected)
def test_parse_imports_global_module_reference(self): actual = asn1tools.parse_string('A DEFINITIONS ::= BEGIN ' 'IMPORTS ' 'a FROM B ' 'c, d FROM E global-module-reference ' 'f, g FROM H {iso(1)}; ' 'END') expected = { 'A': { 'extensibility-implied': False, 'imports': { 'B': ['a'], 'E': ['c', 'd'], 'H': ['f', 'g'] }, 'object-classes': {}, 'object-sets': {}, 'types': {}, 'values': {} } } self.assertEqual(actual, expected)