示例#1
0
    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.")
示例#2
0
    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.")
示例#3
0
    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.")
示例#4
0
    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.")
示例#5
0
    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.")
示例#6
0
    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 ::=.")
示例#7
0
    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.")
示例#8
0
    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.")
示例#9
0
    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 '('.")
示例#10
0
    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.")
示例#11
0
    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}.")
示例#12
0
    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.")
示例#13
0
    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.")
示例#14
0
    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."
        )
示例#15
0
    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.")
示例#16
0
    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)
示例#17
0
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_)
示例#18
0
    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.")
示例#19
0
    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.")
示例#20
0
    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)
示例#21
0
    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)
示例#22
0
    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)