diff options
author | Azul <azul@riseup.net> | 2021-02-03 10:49:29 +0100 |
---|---|---|
committer | Azul <azul@riseup.net> | 2021-02-08 14:54:46 +0100 |
commit | 280796ac92d25ada9d6fd79d03aea7676b83ded7 (patch) | |
tree | 3991b0f7abcbf24e6a1f63f51775f6185a745f18 /ffi | |
parent | 65f9d716c56c809b5cca59f964963b5ee062e261 (diff) |
ffi: Move NetworkPolicy into net for python.
- See #665.
- Also add test for keyserver to make sure the fix is correct.
Diffstat (limited to 'ffi')
-rw-r--r-- | ffi/lang/python/sequoia/core.py | 8 | ||||
-rw-r--r-- | ffi/lang/python/sequoia/net.py | 27 | ||||
-rw-r--r-- | ffi/lang/python/sequoia/store.py | 7 | ||||
-rw-r--r-- | ffi/lang/python/tests/test_armor.py | 5 | ||||
-rw-r--r-- | ffi/lang/python/tests/test_cert.py | 26 | ||||
-rw-r--r-- | ffi/lang/python/tests/test_keyserver.py | 10 | ||||
-rw-r--r-- | ffi/lang/python/tests/test_packet_parser.py | 5 |
7 files changed, 48 insertions, 40 deletions
diff --git a/ffi/lang/python/sequoia/core.py b/ffi/lang/python/sequoia/core.py index b985247b..5f301c26 100644 --- a/ffi/lang/python/sequoia/core.py +++ b/ffi/lang/python/sequoia/core.py @@ -5,12 +5,6 @@ from _sequoia import ffi, lib from .error import Error from .glue import SQObject, invoke -class NetworkPolicy(Enum): - Offline = lib.SQ_NETWORK_POLICY_OFFLINE - Anonymized = lib.SQ_NETWORK_POLICY_ANONYMIZED - Encrypted = lib.SQ_NETWORK_POLICY_ENCRYPTED - Insecure = lib.SQ_NETWORK_POLICY_INSECURE - class IPCPolicy(Enum): External = lib.SQ_IPC_POLICY_EXTERNAL Internal = lib.SQ_IPC_POLICY_INTERNAL @@ -20,13 +14,11 @@ class Context(SQObject): _del = lib.sq_context_free def __init__(self, home=None, - network_policy=NetworkPolicy.Encrypted, ipc_policy=IPCPolicy.Robust, ephemeral=False): cfg = lib.sq_context_configure() if home: lib.sq_config_home(cfg, home.encode()) - lib.sq_config_network_policy(cfg, network_policy.value) lib.sq_config_ipc_policy(cfg, ipc_policy.value) if ephemeral: lib.sq_config_ephemeral(cfg) diff --git a/ffi/lang/python/sequoia/net.py b/ffi/lang/python/sequoia/net.py index de7a77fc..bc5de9eb 100644 --- a/ffi/lang/python/sequoia/net.py +++ b/ffi/lang/python/sequoia/net.py @@ -1,27 +1,40 @@ -from _sequoia import ffi, lib +from enum import Enum +from _sequoia import ffi, lib from .openpgp import Cert from .error import Error from .glue import SQObject +class NetworkPolicy(Enum): + Offline = lib.SQ_NETWORK_POLICY_OFFLINE + Anonymized = lib.SQ_NETWORK_POLICY_ANONYMIZED + Encrypted = lib.SQ_NETWORK_POLICY_ENCRYPTED + Insecure = lib.SQ_NETWORK_POLICY_INSECURE + class KeyServer(SQObject): _del = lib.sq_keyserver_free @classmethod - def new(cls, ctx, uri, cert=None): + def new(cls, ctx, uri, network_policy=NetworkPolicy.Encrypted, cert=None): if not cert: - ks = lib.sq_keyserver_new(ctx.ref(), uri.encode()) + ks = lib.sq_keyserver_new( + ctx.ref(), + network_policy.value, + uri.encode()) else: ks = lib.sq_keyserver_with_cert( - ctx.ref(), uri.encode(), + ctx.ref(), + network_policy.value, + uri.encode(), ffi.cast("uint8_t *", ffi.from_buffer(cert)), len(cert)) return KeyServer(ks, context=ctx) @classmethod - def keys_openpgp_org(cls, ctx): - return KeyServer(lib.sq_keyserver_keys_openpgp_org(ctx.ref()), - context=ctx) + def keys_openpgp_org(cls, ctx, network_policy=NetworkPolicy.Encrypted): + ks = lib.sq_keyserver_keys_openpgp_org(ctx.ref(), + network_policy.value) + return KeyServer(ks, context=ctx) def get(self, keyid): return Cert(lib.sq_keyserver_get(self.context().ref(), diff --git a/ffi/lang/python/sequoia/store.py b/ffi/lang/python/sequoia/store.py index 97c601ec..57d1b18f 100644 --- a/ffi/lang/python/sequoia/store.py +++ b/ffi/lang/python/sequoia/store.py @@ -2,6 +2,7 @@ from _sequoia import ffi, lib from .error import Error from .glue import _str, _static_str, SQObject, sq_iterator, sq_time +from .net import NetworkPolicy from .openpgp import Fingerprint, Cert class Store(object): @@ -41,8 +42,10 @@ class Mapping(SQObject): REALM_SOFTWARE_UPDATES = _static_str(lib.SQ_REALM_SOFTWARE_UPDATES) @classmethod - def open(cls, ctx, realm=REALM_CONTACTS, name="default"): - return Mapping(lib.sq_mapping_open(ctx.ref(), realm.encode(), name.encode()), context=ctx) + def open(cls, ctx, network_policy=NetworkPolicy.Encrypted, realm=REALM_CONTACTS, name="default"): + return Mapping(lib.sq_mapping_open(ctx.ref(), network_policy.value, + realm.encode(), name.encode()), + context=ctx) def add(self, label, fingerprint): diff --git a/ffi/lang/python/tests/test_armor.py b/ffi/lang/python/tests/test_armor.py index 93486703..2d1815f6 100644 --- a/ffi/lang/python/tests/test_armor.py +++ b/ffi/lang/python/tests/test_armor.py @@ -2,13 +2,12 @@ import os from os.path import join from tempfile import TemporaryDirectory -from sequoia.core import Context, NetworkPolicy, Reader, Writer +from sequoia.core import Context, 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) +ctx = Context(ephemeral=True) def fn_bin(t): return "../../../openpgp/tests/data/armor/test-{}.bin".format(t) diff --git a/ffi/lang/python/tests/test_cert.py b/ffi/lang/python/tests/test_cert.py index 8c38f205..98222529 100644 --- a/ffi/lang/python/tests/test_cert.py +++ b/ffi/lang/python/tests/test_cert.py @@ -1,7 +1,7 @@ from os.path import join from tempfile import TemporaryDirectory -from sequoia.core import Context, NetworkPolicy, Reader, Writer +from sequoia.core import Context, Reader, Writer from sequoia.openpgp import ArmorReader, Fingerprint, Cert, PacketPile pgp = "../../../openpgp/tests/data/keys/testy.pgp" @@ -9,15 +9,13 @@ asc = "../../../openpgp/tests/data/keys/testy.asc" fp = Fingerprint.from_hex("3E8877C877274692975189F5D03F6F865226FE8B") def test_from_reader(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) r = Reader.open(ctx, pgp) t = Cert.from_reader(ctx, r) assert t.fingerprint() == fp def test_from_armor_reader(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) k = open(asc, "rb").read() r = Reader.from_bytes(ctx, k) r = ArmorReader.new(ctx, r) @@ -25,27 +23,23 @@ def test_from_armor_reader(): assert t.fingerprint() == fp def test_from_file(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) t = Cert.open(ctx, pgp) assert t.fingerprint() == fp def test_from_packet_pile(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) r = PacketPile.open(ctx, pgp) t = Cert.from_packet_pile(ctx, r) assert t.fingerprint() == fp def test_from_bytes(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) t = Cert.from_bytes(ctx, open(pgp, "rb").read()) assert t.fingerprint() == fp def test_from_serialize(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) with TemporaryDirectory() as tmp: sink = join(tmp, "a") @@ -57,16 +51,14 @@ def test_from_serialize(): assert t.fingerprint() == fp def test_equals(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) b = open(pgp, "rb").read() t = Cert.from_bytes(ctx, b) u = Cert.from_bytes(ctx, b) assert t == u def test_clone(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) a = Cert.open(ctx, pgp) b = a.copy() del a diff --git a/ffi/lang/python/tests/test_keyserver.py b/ffi/lang/python/tests/test_keyserver.py new file mode 100644 index 00000000..30d237fa --- /dev/null +++ b/ffi/lang/python/tests/test_keyserver.py @@ -0,0 +1,10 @@ +from sequoia.prelude import Context +from sequoia.net import KeyServer + +def test_keys_openpgp_org(): + c = Context(ephemeral=True) + KeyServer.keys_openpgp_org(c) + +def test_new(): + c = Context(ephemeral=True) + KeyServer.new(c, "hkps://keys.domain.example") diff --git a/ffi/lang/python/tests/test_packet_parser.py b/ffi/lang/python/tests/test_packet_parser.py index e50a58f5..abb0ab24 100644 --- a/ffi/lang/python/tests/test_packet_parser.py +++ b/ffi/lang/python/tests/test_packet_parser.py @@ -1,13 +1,12 @@ from enum import Enum -from sequoia.core import Context, NetworkPolicy +from sequoia.core import Context from sequoia.openpgp import Tag, PacketParser pgp = "../../../openpgp/tests/data/messages/encrypted-aes128-password-123456789.gpg" plain = "../../../openpgp/tests/data/messages/a-cypherpunks-manifesto.txt" def test_decryption(): - ctx = Context(network_policy=NetworkPolicy.Offline, - ephemeral=True) + ctx = Context(ephemeral=True) class State(Enum): # XXX: In Python 3.6, we can use enum.auto() to assign values. |