diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-02-15 18:06:50 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-11 13:23:24 +0100 |
commit | 177835be9dba392ab10994254b67aa676be66331 (patch) | |
tree | 7682b06cd40bbec88dec091c23a18002a9bfabaa /ffi/lang/python/tests/test_packet_parser.py | |
parent | ed7d023d5a6a2587ba218910bc1849d0d34adca7 (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/test_packet_parser.py')
-rw-r--r-- | ffi/lang/python/tests/test_packet_parser.py | 51 |
1 files changed, 51 insertions, 0 deletions
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 |