pk_test.py 2.24 KB
Newer Older
1
# -*- coding: utf-8 -*-
poljar's avatar
poljar committed
2
3
import pytest

Hubert Chathi's avatar
Hubert Chathi committed
4
5
from olm import (PkDecryption, PkDecryptionError, PkEncryption, PkSigning,
                 ed25519_verify)
poljar's avatar
poljar committed
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


class TestClass(object):
    def test_invalid_encryption(self):
        with pytest.raises(ValueError):
            PkEncryption("")

    def test_decrytion(self):
        decryption = PkDecryption()
        encryption = PkEncryption(decryption.public_key)
        plaintext = "It's a secret to everybody."
        message = encryption.encrypt(plaintext)
        decrypted_plaintext = decryption.decrypt(message)
        isinstance(decrypted_plaintext, str)
        assert plaintext == decrypted_plaintext

    def test_invalid_decrytion(self):
        decryption = PkDecryption()
        encryption = PkEncryption(decryption.public_key)
        plaintext = "It's a secret to everybody."
        message = encryption.encrypt(plaintext)
        message.ephemeral_key = "?"
        with pytest.raises(PkDecryptionError):
            decryption.decrypt(message)

    def test_pickling(self):
        decryption = PkDecryption()
        encryption = PkEncryption(decryption.public_key)
        plaintext = "It's a secret to everybody."
        message = encryption.encrypt(plaintext)

        pickle = decryption.pickle()
        unpickled = PkDecryption.from_pickle(pickle)
        decrypted_plaintext = unpickled.decrypt(message)
        assert plaintext == decrypted_plaintext

    def test_invalid_unpickling(self):
        with pytest.raises(ValueError):
            PkDecryption.from_pickle("")

    def test_invalid_pass_pickling(self):
        decryption = PkDecryption()
        pickle = decryption.pickle("Secret")

        with pytest.raises(PkDecryptionError):
            PkDecryption.from_pickle(pickle, "Not secret")
52
53
54
55
56
57
58

    def test_signing(self):
        seed = PkSigning.generate_seed()
        signing = PkSigning(seed)
        message = "This statement is true"
        signature = signing.sign(message)
        ed25519_verify(signing.public_key, message, signature)
59
60
61
62

    def test_invalid_unicode_decrypt(self):
        decryption = PkDecryption()
        encryption = PkEncryption(decryption.public_key)
63
        message = encryption.encrypt(b"\xed")
64
        plaintext = decryption.decrypt(message)
65
        assert plaintext == u"�"