summaryrefslogtreecommitdiffstats
path: root/ffi/lang/python/tests
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-02-15 18:06:50 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-11 13:23:24 +0100
commit177835be9dba392ab10994254b67aa676be66331 (patch)
tree7682b06cd40bbec88dec091c23a18002a9bfabaa /ffi/lang/python/tests
parented7d023d5a6a2587ba218910bc1849d0d34adca7 (diff)
ffi: Add preliminary Python bindings.
- The bingings support basic manipulation of OpenPGP data, but are quite incomplete. Furthermore, the Python API is not very pythonic in some places, so expect it to break in the future.
Diffstat (limited to 'ffi/lang/python/tests')
-rw-r--r--ffi/lang/python/tests/test_armor.py71
-rw-r--r--ffi/lang/python/tests/test_fingerprint.py45
-rw-r--r--ffi/lang/python/tests/test_keyid.py63
-rw-r--r--ffi/lang/python/tests/test_packet_parser.py51
-rw-r--r--ffi/lang/python/tests/test_store.py47
-rw-r--r--ffi/lang/python/tests/test_tpk.py83
6 files changed, 360 insertions, 0 deletions
diff --git a/ffi/lang/python/tests/test_armor.py b/ffi/lang/python/tests/test_armor.py
new file mode 100644
index 00000000..e439f612
--- /dev/null
+++ b/ffi/lang/python/tests/test_armor.py
@@ -0,0 +1,71 @@
+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("org.sequoia-pgp.tests",
+ 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()))
diff --git a/ffi/lang/python/tests/test_fingerprint.py b/ffi/lang/python/tests/test_fingerprint.py
new file mode 100644
index 00000000..a5470f29
--- /dev/null
+++ b/ffi/lang/python/tests/test_fingerprint.py
@@ -0,0 +1,45 @@
+from sequoia.openpgp import Fingerprint
+
+binary = b"\x7D\xCA\x58\xB5\x4E\xB1\x43\x16\x9D\xDE\xE1\x5F\x24\x7F\x6D\xAB\xC8\x49\x14\xFE"
+hexy = "7DCA58B54EB143169DDEE15F247F6DABC84914FE"
+pretty = "7DCA 58B5 4EB1 4316 9DDE E15F 247F 6DAB C849 14FE"
+
+def test_from_bytes():
+ f = Fingerprint.from_bytes(binary)
+ assert str(f) == pretty
+ assert f.hex() == hexy
+
+def test_from_hex():
+ f = Fingerprint.from_hex(hexy)
+ assert str(f) == pretty
+ assert f.hex() == hexy
+
+def test_to_keyid():
+ f = Fingerprint.from_hex(hexy)
+ assert f.keyid().hex() == "247F6DABC84914FE"
+
+def test_bad_hex():
+ try:
+ f = Fingerprint.from_hex("bad hex")
+ except:
+ pass
+ else:
+ raise "Expected exception"
+
+def test_equals():
+ a = Fingerprint.from_hex(hexy)
+ b = Fingerprint.from_hex(hexy)
+ assert a == b
+
+def test_clone():
+ a = Fingerprint.from_hex(hexy)
+ b = a.copy()
+ del a
+ c = b.copy()
+ del b
+ assert c.hex() == hexy
+
+def test_hash():
+ a = Fingerprint.from_hex(hexy)
+ b = Fingerprint.from_hex(hexy)
+ assert hash(a) == hash(b)
diff --git a/ffi/lang/python/tests/test_keyid.py b/ffi/lang/python/tests/test_keyid.py
new file mode 100644
index 00000000..6cc95f9e
--- /dev/null
+++ b/ffi/lang/python/tests/test_keyid.py
@@ -0,0 +1,63 @@
+from sequoia.openpgp import KeyID
+
+binary = b"\x24\x7F\x6D\xAB\xC8\x49\x14\xFE"
+hexy = "247F6DABC84914FE"
+pretty = "247F 6DAB C849 14FE"
+
+def test_from_bytes():
+ k = KeyID.from_bytes(binary)
+ assert str(k) == pretty
+ assert k.hex() == hexy
+
+def test_from_hex():
+ k = KeyID.from_hex(hexy)
+ assert str(k) == pretty
+ assert k.hex() == hexy
+
+fp_hexy = "7DCA58B54EB143169DDEE15F247F6DABC84914FE"
+fp_pretty = "7DCA 58B5 4EB1 4316 9DDE E15F 247F 6DAB C849 14FE"
+def test_from_hexy_fp():
+ k = KeyID.from_bytes(binary)
+ assert k == KeyID.from_hex(fp_hexy)
+ assert k == KeyID.from_hex(fp_pretty)
+
+def test_malformed():
+ try:
+ KeyID.from_bytes(b"too few")
+ except:
+ pass
+ else:
+ raise "Expected exception"
+
+ try:
+ KeyID.from_bytes(b"way too many")
+ except:
+ pass
+ else:
+ raise "Expected exception"
+
+ try:
+ KeyID.from_hex(b"not hex chars")
+ except:
+ pass
+ else:
+ raise "Expected exception"
+
+def test_equals():
+ a = KeyID.from_hex(hexy)
+ b = KeyID.from_hex(hexy)
+ assert a == b
+
+def test_clone():
+ a = KeyID.from_hex(hexy)
+ b = a.copy()
+ del a
+ c = b.copy()
+ del b
+ assert c.hex() == hexy
+
+def test_hash():
+ a = KeyID.from_hex(hexy)
+ b = KeyID.from_hex(hexy)
+ assert hash(a) == hash(b)
+
diff --git a/ffi/lang/python/tests/test_packet_parser.py b/ffi/lang/python/tests/test_packet_parser.py
new file mode 100644
index 00000000..1c134f6f
--- /dev/null
+++ b/ffi/lang/python/tests/test_packet_parser.py
@@ -0,0 +1,51 @@
+from enum import Enum, auto
+from sequoia.core import Context, NetworkPolicy
+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("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+
+ class State(Enum):
+ Start = auto()
+ Decrypted = auto()
+ Deciphered = auto()
+ Done = auto()
+
+ state = State.Start
+ algo, key = None, None
+ ppr = PacketParser.open(ctx, pgp)
+ while True:
+ pp = ppr.packet_parser()
+ if not pp:
+ break
+
+ packet = pp.packet
+ tag = packet.tag
+ print(state, pp.recursion_depth, packet)
+
+ if state == State.Start:
+ assert pp.recursion_depth == 0
+ if tag == Tag.SKESK:
+ algo, key = packet.match().decrypt(b"123456789")
+ state = State.Decrypted
+ elif state == State.Decrypted:
+ assert pp.recursion_depth == 0
+ if tag == Tag.SEIP:
+ pp.decrypt(algo, key)
+ state = State.Deciphered
+ elif state == State.Deciphered:
+ assert pp.recursion_depth == 1
+ if tag == Tag.Literal:
+ body = pp.buffer_unread_content()
+ assert body[:].decode() == open(plain).read()
+ state = State.Done
+
+ _, ppr = pp.recurse()
+
+ assert ppr.eof().is_message()
+ assert state == State.Done
diff --git a/ffi/lang/python/tests/test_store.py b/ffi/lang/python/tests/test_store.py
new file mode 100644
index 00000000..0c86a689
--- /dev/null
+++ b/ffi/lang/python/tests/test_store.py
@@ -0,0 +1,47 @@
+from sequoia.prelude import Context, Store, Fingerprint
+
+def test_open():
+ c = Context("org.sequoia-pgp.tests", ephemeral=True)
+ Store.open(c, "default")
+
+def test_add():
+ c = Context("org.sequoia-pgp.tests", ephemeral=True)
+ s = Store.open(c, "default")
+ fp = Fingerprint.from_hex("7DCA58B54EB143169DDEE15F247F6DABC84914FE")
+ s.add("Ἀριστοτέλης", fp)
+
+def test_iterate():
+ c = Context("org.sequoia-pgp.tests", ephemeral=True)
+ s = Store.open(c, "default")
+ fp = Fingerprint.from_hex("7DCA58B54EB143169DDEE15F247F6DABC84914FE")
+ s.add("Ἀριστοτέλης", fp)
+ l = list(s.iter())
+ assert len(l) == 1
+ l = list(Store.list_keys(c))
+ assert len(l) == 1
+ fpi, key = l[0]
+ assert fpi == fp
+
+def test_logs():
+ c = Context("org.sequoia-pgp.tests", ephemeral=True)
+ s = Store.open(c, "default")
+ fp = Fingerprint.from_hex("7DCA58B54EB143169DDEE15F247F6DABC84914FE")
+ b = s.add("Ἀριστοτέλης", fp)
+ l = list(s.iter())
+ assert len(l) == 1
+
+ # global logs
+ logs = list(Store.server_log(c))
+ assert len(logs) > 0
+
+ # per store logs
+ logs = list(s.log())
+ assert len(logs) > 0
+
+ # per binding logs
+ logs = list(b.log())
+ assert len(logs) > 0
+
+ # per key logs
+ logs = list(b.key().log())
+ assert len(logs) > 0
diff --git a/ffi/lang/python/tests/test_tpk.py b/ffi/lang/python/tests/test_tpk.py
new file mode 100644
index 00000000..5960ed49
--- /dev/null
+++ b/ffi/lang/python/tests/test_tpk.py
@@ -0,0 +1,83 @@
+from os.path import join
+from tempfile import TemporaryDirectory
+
+from sequoia.core import Context, NetworkPolicy, Reader, Writer
+from sequoia.openpgp import ArmorReader, Fingerprint, TPK, PacketPile
+
+pgp = "../../../openpgp/tests/data/keys/testy.pgp"
+asc = "../../../openpgp/tests/data/keys/testy.asc"
+fp = Fingerprint.from_hex("3E8877C877274692975189F5D03F6F865226FE8B")
+
+def test_from_reader():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ r = Reader.open(ctx, pgp)
+ t = TPK.from_reader(ctx, r)
+ assert t.fingerprint() == fp
+
+def test_from_armor_reader():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ k = open(asc, "rb").read()
+ r = Reader.from_bytes(ctx, k)
+ r = ArmorReader.new(ctx, r)
+ t = TPK.from_reader(ctx, r)
+ assert t.fingerprint() == fp
+
+def test_from_file():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ t = TPK.open(ctx, pgp)
+ assert t.fingerprint() == fp
+
+def test_from_message():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ r = PacketPile.open(ctx, pgp)
+ t = TPK.from_packet_pile(ctx, r)
+ assert t.fingerprint() == fp
+
+def test_from_bytes():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ t = TPK.from_bytes(ctx, open(pgp, "rb").read())
+ assert t.fingerprint() == fp
+
+def test_from_serialize():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ with TemporaryDirectory() as tmp:
+ sink = join(tmp, "a")
+
+ t = TPK.open(ctx, pgp)
+ with Writer.open(ctx, sink) as s:
+ t.serialize(s)
+
+ t = TPK.open(ctx, sink)
+ assert t.fingerprint() == fp
+
+def test_equals():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ b = open(pgp, "rb").read()
+ t = TPK.from_bytes(ctx, b)
+ u = TPK.from_bytes(ctx, b)
+ assert t == u
+
+def test_clone():
+ ctx = Context("org.sequoia-pgp.tests",
+ network_policy=NetworkPolicy.Offline,
+ ephemeral=True)
+ a = TPK.open(ctx, pgp)
+ b = a.copy()
+ del a
+ c = b.copy()
+ del b
+ assert c.fingerprint() == fp