File: test_02_packets.py

package info (click to toggle)
python-pgpy 0.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,212 kB
  • sloc: python: 8,448; makefile: 155; sh: 10
file content (80 lines) | stat: -rw-r--r-- 2,403 bytes parent folder | download | duplicates (3)
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
69
70
71
72
73
74
75
76
77
78
79
80
""" test parsing packets
"""
import pytest

import glob
import os

from pgpy.packet import Packet
from pgpy.packet import PubKeyV4, PubSubKeyV4, PrivKeyV4, PrivSubKeyV4
from pgpy.packet import Opaque

# import pgpy.packet.fields

_trailer = b'\xde\xca\xff\xba\xdd'
_pclasses = {
    (0x01, 3): 'PKESessionKeyV3',
    (0x02, 4): 'SignatureV4',
    (0x03, 4): 'SKESessionKeyV4',
    (0x04, 3): 'OnePassSignatureV3',
    (0x05, 4): 'PrivKeyV4',
    (0x06, 4): 'PubKeyV4',
    (0x07, 4): 'PrivSubKeyV4',
    0x08: 'CompressedData',
    0x09: 'SKEData',
    0x0A: 'Marker',
    0x0B: 'LiteralData',
    0x0C: 'Trust',
    0x0D: 'UserID',
    (0x0E, 4): 'PubSubKeyV4',
    0x11: 'UserAttribute',
    (0x12, 1): 'IntegrityProtectedSKEDataV1',
    0x13: 'MDC',
}


def binload(f):
    with open(f, 'rb') as ff:
        buf = bytearray(os.fstat(ff.fileno()).st_size)
        ff.readinto(buf)
        return buf


pktfiles = sorted(glob.glob('tests/testdata/packets/[0-9]*'))


class TestPacket(object):
    @pytest.mark.parametrize('packet', pktfiles, ids=[os.path.basename(f) for f in pktfiles])
    def test_load(self, packet):
        b = binload(packet) + _trailer
        _b = b[:]
        p = Packet(_b)

        # parsed all bytes
        assert _b == _trailer

        # length is computed correctly
        assert p.header.length + len(p.header) == len(p)
        if packet not in ('tests/testdata/packets/11.partial.literal',):
            assert len(p) == len(b) - len(_trailer)
            assert len(p.__bytes__()) == len(b) - len(_trailer)

            # __bytes__ output is correct
            assert p.__bytes__() == b[:-len(_trailer)]

        # instantiated class is what we expected
        if hasattr(p.header, 'version') and (p.header.tag, p.header.version) in _pclasses:
            # versioned packet
            assert p.__class__.__name__ == _pclasses[(p.header.tag, p.header.version)]

        elif (not hasattr(p.header, 'version')) and p.header.tag in _pclasses:
            # unversioned packet
            assert p.__class__.__name__ in _pclasses[p.header.tag]

        else:
            # fallback to opaque
            assert isinstance(p, Opaque)

        # if this is a key, ensure len(p.keymaterial) == len(bytes(p.keymaterial))
        if isinstance(p, (PubKeyV4, PubSubKeyV4, PrivKeyV4, PrivSubKeyV4)):
            assert len(p.keymaterial) == len(p.keymaterial.__bytes__())