Пример #1
0
def test_init_repo_other_repo():
    repo_base = asn1.Repository("""
        Base DEFINITIONS ::= BEGIN
            T1 ::= BOOLEAN
        END""")
    repo_derived = asn1.Repository(repo_base, """
        Derived DEFINITIONS ::= BEGIN
            T1 ::= SEQUENCE OF Base.T1
        END
        """)
    assert sorted(repo_derived.to_json()) == [
        [['TypeRef', 'Base', 'T1'], ['BooleanType']],
        [['TypeRef', 'Derived', 'T1'], ['SequenceOfType',
                                        ['TypeRef', 'Base', 'T1']]]]
Пример #2
0
def test_init_repo_str():
    repository = asn1.Repository("""
        Def DEFINITIONS ::= BEGIN
            T1 ::= BOOLEAN
        END""")
    assert sorted(repository.to_json()) == [
        [['TypeRef', 'Def', 'T1'], ['BooleanType']]]
Пример #3
0
def test_serialization_composite(asn1_def, encoding, name, value):
    encoder = get_encoder(asn1.Repository(asn1_def), encoding)

    encoded_value = encoder.encode('Module', name, value)
    decoded_value, remainder = encoder.decode('Module', name, encoded_value)

    assert value == decoded_value
    assert len(remainder) == 0
Пример #4
0
def test_constructed_as_octet_and_utf8_string(encoding):
    encoder = get_encoder(asn1.Repository("""
    Module DEFINITIONS ::= BEGIN
        T1 ::= SEQUENCE OF OCTET STRING
        T2 ::= OCTET STRING
        T3 ::= UTF8String
    END"""), encoding)

    entity = encoder.encode_value('Module', 'T1', [b'foo', b'bar'])
    assert encoder.decode_value('Module', 'T2', entity) == b'foobar'
    assert encoder.decode_value('Module', 'T3', entity) == 'foobar'
Пример #5
0
def test_constructed_as_bit_string(encoding):
    encoder = get_encoder(asn1.Repository("""
    Module DEFINITIONS ::= BEGIN
        T1 ::= SEQUENCE OF OCTET STRING
        T2 ::= BIT STRING
    END"""), encoding)

    entity = encoder.encode_value(
        'Module', 'T1', [bytes([0, 5]), bytes([0, 15])])
    assert encoder.decode_value('Module', 'T2', entity) == (
        [False, False, False, False, False, True, False, True] +
        [False, False, False, False, True, True, True, True])
Пример #6
0
def test_serialization_embedded_pdv(encoding, abstract, transfer):
    if isinstance(abstract, list) and transfer is None:
        return

    encoder = get_encoder(asn1.Repository("""
    Module DEFINITIONS ::= BEGIN
        T1 ::= EMBEDDED PDV
        T2 ::= SEQUENCE OF INTEGER
    END"""), encoding)

    data = encoder.encode('Module', 'T2', [1, 2, 3, 9, 8, 7])

    embedded_pdv = asn1.EmbeddedPDV(data=data,
                                    abstract=abstract,
                                    transfer=transfer)
    encoded = encoder.encode('Module', 'T1', embedded_pdv)
    decoded, rest = encoder.decode('Module', 'T1', encoded)
    assert decoded == embedded_pdv
    assert len(rest) == 0
Пример #7
0
def test_init_repo_file(tmpdir):
    directory = tmpdir.mkdir("schemas_asn1")
    file1 = directory.join("schema1.asn")
    file1.write("""
        Def1 DEFINITIONS ::= BEGIN
            T1 ::= BOOLEAN
        END
    """)
    file2 = directory.join("schema2.asn")
    file2.write("""
        Def2 DEFINITIONS ::= BEGIN
            T1 ::= BOOLEAN
        END
    """)

    repository = asn1.Repository(Path(directory))
    assert sorted(repository.to_json()) == [
        [['TypeRef', 'Def1', 'T1'], ['BooleanType']],
        [['TypeRef', 'Def2', 'T1'], ['BooleanType']]]
Пример #8
0
def test_example_docs():
    repo = asn1.Repository(r"""
        Example DEFINITIONS ::= BEGIN
            T ::= SEQUENCE OF CHOICE {
                a BOOLEAN,
                b INTEGER,
                c UTF8String
            }
        END
    """)

    encoder = asn1.Encoder(asn1.Encoding.BER, repo)

    value = [('c', '123'), ('a', True), ('a', False), ('b', 123)]

    encoded = encoder.encode('Example', 'T', value)
    decoded, rest = encoder.decode('Example', 'T', encoded)

    assert value == decoded
    assert len(rest) == 0
Пример #9
0
def test_serialization_entity(encoding, sequence_size):
    sequence_items = ', '.join([f'item{n} [APPLICATION {n}] OCTET STRING'
                                for n in range(sequence_size)])
    sequence_items = '{' + sequence_items + '}'
    definition = f"""
    Module DEFINITIONS ::= BEGIN
        T1 ::= ABSTRACT-SYNTAX.&Type
        T2 ::= SEQUENCE {sequence_items}
    END"""
    encoder = get_encoder(asn1.Repository(definition), encoding)

    value = {f'item{n}': b'0123' for n in range(sequence_size)}
    entity = encoder.encode_value('Module', 'T2', value)

    encoded = encoder.encode('Module', 'T1', entity)
    decoded_entity, rest = encoder.decode('Module', 'T1', encoded)
    assert decoded_entity == entity
    assert len(rest) == 0
    decoded_value = encoder.decode_value('Module', 'T2', decoded_entity)
    assert decoded_value == value
Пример #10
0
def test_serialization_external(encoding, data_type, direct_ref, indirect_ref):
    encoder = get_encoder(asn1.Repository("""
    Module DEFINITIONS ::= BEGIN
        T1 ::= EXTERNAL
        T2 ::= BIT STRING
    END"""), encoding)

    bits = [True, False, False, True]
    data = {
        'entity': encoder.encode_value('Module', 'T2', bits),
        'bytes': encoder.encode('Module', 'T2', bits),
        'bitstring': bits
    }[data_type]

    external = asn1.External(data=data,
                             direct_ref=direct_ref,
                             indirect_ref=indirect_ref)
    encoded = encoder.encode('Module', 'T1', external)
    decoded, rest = encoder.decode('Module', 'T1', encoded)
    assert decoded == external
    assert len(rest) == 0
Пример #11
0
 def build():
     repo = asn1.Repository(*src_paths)
     doc = repo.generate_html_doc()
     asn1_dst_dir.mkdir(parents=True, exist_ok=True)
     with open(dst_path, 'w', encoding='utf-8') as f:
         f.write(doc)
Пример #12
0
 def generate():
     repo = asn1.Repository(*src_paths)
     data = repo.to_json()
     for dst_path in dst_paths:
         json.encode_file(data, dst_path, indent=None)
Пример #13
0
def test_invalid_serialization(asn1_def, encoding, value, serialize_name,
                               deserialize_name):
    encoder = get_encoder(asn1.Repository(asn1_def), encoding)
    encoded_value = encoder.encode('Module', serialize_name, value)
    with pytest.raises(Exception):
        encoder.decode('Module', deserialize_name, encoded_value)
Пример #14
0
def test_serialization_not_supported(asn1_def, encoding, name, value):
    encoder = get_encoder(asn1.Repository(asn1_def), encoding)

    with pytest.raises(NotImplementedError):
        encoder.encode('Module', name, value)
Пример #15
0
def test_parse(asn1_def, refs_json):
    repo = asn1.Repository(asn1_def)
    assert sorted(repo.to_json()) == sorted(refs_json)