File: test_encoding.py

package info (click to toggle)
python-cryptography 46.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,684 kB
  • sloc: python: 52,706; java: 319; makefile: 160
file content (68 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

import pytest

import cryptography.hazmat.asn1 as asn1


class TestEncodeInteger:
    @pytest.mark.parametrize(
        "value,expected",
        [
            (0, b"\x02\x01\x00"),
            (1, b"\x02\x01\x01"),
            (42, b"\x02\x01\x2a"),
            (127, b"\x02\x01\x7f"),
            (128, b"\x02\x02\x00\x80"),
            (255, b"\x02\x02\x00\xff"),
            (256, b"\x02\x02\x01\x00"),
            (-1, b"\x02\x01\xff"),
            (-128, b"\x02\x01\x80"),
            (-129, b"\x02\x02\xff\x7f"),
        ],
    )
    def test_encode_int(self, value: int, expected: bytes) -> None:
        encoded = asn1.encode_der(value)
        assert encoded == expected, (
            f"Failed for {value}: got {encoded.hex()}, expected"
            f"{expected.hex()}"
        )


class TestEncodeSequence:
    def test_encode_ok_sequence_single_field(self) -> None:
        @asn1.sequence
        class Example:
            foo: int

        value = Example(foo=9)

        encoded = asn1.encode_der(value)
        assert encoded == b"\x30\x03\x02\x01\x09"

    def test_encode_ok_sequence_multiple_fields(self) -> None:
        @asn1.sequence
        class Example:
            foo: int
            bar: int

        value = Example(foo=9, bar=6)

        encoded = asn1.encode_der(value)
        assert encoded == b"\x30\x06\x02\x01\x09\x02\x01\x06"

    def test_encode_ok_nested_sequence(self) -> None:
        @asn1.sequence
        class Child:
            foo: int

        @asn1.sequence
        class Parent:
            foo: Child

        value = Parent(foo=Child(foo=9))

        encoded = asn1.encode_der(value)
        assert encoded == b"\x30\x05\x30\x03\x02\x01\x09"