summaryrefslogtreecommitdiffstats
path: root/ffi/lang/python/tests/test_armor.py
blob: 934867038082d38f845c68d849339a1cdc3eb48c (plain)
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
import os
from os.path import join
from tempfile import TemporaryDirectory

from sequoia.core import Context, NetworkPolicy, Reader, Writer
from sequoia.openpgp import ArmorReader, ArmorWriter, Kind

TEST_VECTORS = [0, 1, 2, 3, 47, 48, 49, 50, 51]

ctx = Context(network_policy=NetworkPolicy.Offline,
              ephemeral=True)

def fn_bin(t):
    return "../../../openpgp/tests/data/armor/test-{}.bin".format(t)

def fn_asc(t):
    return "../../../openpgp/tests/data/armor/test-{}.asc".format(t)

def test_dearmor_file():
    for t in TEST_VECTORS:
        bin = open(fn_bin(t), "rb").read()
        ar = ArmorReader.new(ctx, Reader.open(ctx, fn_asc(t)))
        assert(bin == ar.read())

def test_dearmor_fd():
    for t in TEST_VECTORS:
        bin = open(fn_bin(t), "rb").read()
        fd = os.open(fn_asc(t), os.O_RDONLY)
        ar = ArmorReader.new(ctx, Reader.from_fd(ctx, fd))
        assert(bin == ar.read())

def test_dearmor_bytes():
    for t in TEST_VECTORS:
        bin = open(fn_bin(t), "rb").read()
        asc = open(fn_asc(t), "rb").read()
        ar = ArmorReader.new(ctx, Reader.from_bytes(ctx, asc))
        assert(bin == ar.read())

def test_enarmor_file():
    for t in TEST_VECTORS:
        with TemporaryDirectory() as tmp:
            bin = open(fn_bin(t), "rb").read()
            sink = join(tmp, "a")
            ar = ArmorWriter.new(ctx, Writer.open(ctx, sink), Kind.File)
            ar.write(bin)
            ar.close()

            assert(open(fn_asc(t)).read() == open(sink).read())

def test_enarmor_fd():
    for t in TEST_VECTORS:
        with TemporaryDirectory() as tmp:
            bin = open(fn_bin(t), "rb").read()
            sink = join(tmp, "a")
            fd = os.open(sink, os.O_WRONLY|os.O_CREAT)
            ar = ArmorWriter.new(ctx, Writer.from_fd(ctx, fd), Kind.File)
            ar.write(bin)
            ar.close()

            assert(open(fn_asc(t)).read() == open(sink).read())

def test_enarmor_bytes():
    for t in TEST_VECTORS:
        bin = open(fn_bin(t), "rb").read()
        sink = bytearray(141)
        ar = ArmorWriter.new(ctx, Writer.from_bytes(ctx, sink), Kind.File)
        ar.write(bin)
        ar.close()

        assert(sink.startswith(open(fn_asc(t), "rb").read()))