def test_big_negative_numbers(self, value: int) -> None: encoder = asn1.Encoder() encoder.write(value, asn1.Number.Integer) encoded_bytes = encoder.output() decoder = asn1.Decoder(encoded_bytes) tag, val = decoder.read() assert val == value
def test_integer(self, buf: bytes, result: int) -> None: dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (asn1.Number.Integer, asn1.Type.Primitive, asn1.Class.Universal) tag, val = dec.read() assert isinstance(val, int) assert val == result
def test_time_ticks(self) -> None: buf = b"\x43\x03\x54\xa5\xb0" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (3, asn1.Type.Primitive, asn1.Class.Application) tag, val = dec.read() assert val == 5547440
def test_no_such_instance(self) -> None: buf = b"\x81\x00" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (1, asn1.Type.Primitive, asn1.Class.Context) tag, val = dec.read() assert val is None
def test_end_of_mib_view(self) -> None: buf = b"\x82\x00" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (2, asn1.Type.Primitive, asn1.Class.Context) tag, val = dec.read() assert val is None
def test_ipaddress(self) -> None: buf = b"\x40\x04\x7f\x00\x00\x01" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (0, asn1.Type.Primitive, asn1.Class.Application) tag, val = dec.read() assert val == ipaddress.IPv4Address("127.0.0.1")
def test_counter32(self) -> None: buf = b"\x41\x01\x2a" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (1, asn1.Type.Primitive, asn1.Class.Application) tag, val = dec.read() assert val == 42
def test_null(self) -> None: buf = b"\x05\x00" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (asn1.Number.Null, asn1.Type.Primitive, asn1.Class.Universal) tag, val = dec.read() assert val is None
def test_skip_primitive(self) -> None: buf = b"\x02\x01\x01\x02\x01\x02" dec = asn1.Decoder(buf) dec.read() tag, val = dec.read() assert val == 2 assert dec.eof()
def test_skip_constructed(self) -> None: buf = b"\x30\x06\x02\x01\x01\x02\x01\x02\x02\x01\x03" dec = asn1.Decoder(buf) dec.read() tag, val = dec.read() assert val == 3 assert dec.eof()
def test_gauge32(self) -> None: buf = b"\x42\x01\x02" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (2, asn1.Type.Primitive, asn1.Class.Application) tag, val = dec.read() assert val == 2
def test_private(self) -> None: buf = b"\xe1\x03\x02\x01\x01" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (1, asn1.Type.Constructed, asn1.Class.Private) with dec.enter(): tag, val = dec.read() assert val == 1
def test_application(self) -> None: buf = b"\x61\x03\x02\x01\x01" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (1, asn1.Type.Constructed, asn1.Class.Application) with dec.enter(): tag, val = dec.read() assert val == 1
def test_read_multiple(self) -> None: buf = b"\x02\x01\x01\x02\x01\x02" dec = asn1.Decoder(buf) tag, val = dec.read() assert val == 1 tag, val = dec.read() assert val == 2 assert dec.eof()
def test_long_tag_id(self) -> None: buf = b"\x3f\x83\xff\x7f\x03\x02\x01\x01" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (0xFFFF, asn1.Type.Constructed, asn1.Class.Universal) with dec.enter(): tag, val = dec.read() assert val == 1
def test_big_negative_integer(self) -> None: buf = ( b"\x02\x10\xff\x7f\x2b\x3a\x4d\xea\x48\x1e\x1f\x37\x7b\xa8\xbd\x7f\xb0\x16" ) dec = asn1.Decoder(buf) tag, val = dec.read() assert val == -668929531791034950848739021124816874 assert dec.eof()
def test_set_of(self) -> None: buf = b"\x31\x06\x02\x01\x01\x02\x01\x02" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == (asn1.Number.Set, asn1.Type.Constructed, asn1.Class.Universal) with dec.enter(): tag, val = dec.read() assert val == 1 tag, val = dec.read() assert val == 2
def test_octet_string(self) -> None: buf = b"\x04\x03foo" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == ( asn1.Number.OctetString, asn1.Type.Primitive, asn1.Class.Universal, ) tag, val = dec.read() assert val == b"foo"
def test_unicode_printable_string(self) -> None: buf = b"\x13\x05\x66\x6f\x6f\xc3\xa9" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == ( asn1.Number.PrintableString, asn1.Type.Primitive, asn1.Class.Universal, ) tag, val = dec.read() assert val == "fooé"
def test_object_identifier(self) -> None: buf = b"\x06\x02\x2a\x03" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == ( asn1.Number.ObjectIdentifier, asn1.Type.Primitive, asn1.Class.Universal, ) tag, val = dec.read() assert val == ".1.2.3"
def test_enumerated(self) -> None: buf = b"\x0a\x01\x01" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == ( asn1.Number.Enumerated, asn1.Type.Primitive, asn1.Class.Universal, ) tag, val = dec.read() assert isinstance(val, int) assert val == 1
def test_sequence(self) -> None: buf = b"\x30\x08\x02\x01\x01\x04\x03foo" dec = asn1.Decoder(buf) tag = dec.peek() assert tag == ( asn1.Number.Sequence, asn1.Type.Constructed, asn1.Class.Universal, ) with dec.enter(): tag, val = dec.read() assert val == 1 tag, val = dec.read() assert val == b"foo"
def test_no_input(self) -> None: dec = asn1.Decoder(b"") with pytest.raises(asn1.Error): dec.peek()
def test_error_non_normalised_object_identifier(self) -> None: buf = b"\x06\x02\x80\x01" dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.read()
def test_error_object_identifier_with_too_large_first_component( self) -> None: buf = b"\x06\x02\x8c\x40" dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.read()
def test_error_missing_value_bytes(self) -> None: buf = b"\x02\x02\x01" dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.read()
def test_error_too_many_length_bytes(self) -> None: buf = b"\x04\xff" + b"\xff" * 0x7F dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.read()
def test_error_missing_length_bytes(self) -> None: buf = b"\x04\x82\xff" dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.read()
def test_error_no_length_bytes(self) -> None: buf = b"\x02" dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.read()
def test_error_missing_tag_bytes(self, buf: bytes) -> None: dec = asn1.Decoder(buf) with pytest.raises(asn1.Error): dec.peek()