test/python_tests/test_handshake.py
changeset 2358 a40432f11e35
parent 2357 5d93322dfd2e
child 2361 6c6698ef4e34
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/python_tests/test_handshake.py	Fri Jan 05 21:54:50 2018 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +# -*- coding: utf-8 -*-
     1.5 +
     1.6 +# this file is under GNU General Public License 3.0
     1.7 +# Copyleft 2017, p≡p foundation
     1.8 +
     1.9 +
    1.10 +from transport import *
    1.11 +
    1.12 +
    1.13 +def setup_gnupg():
    1.14 +    assert os.environ["GNUPGHOME"] != ""
    1.15 +
    1.16 +
    1.17 +mydir = os.path.dirname(os.path.realpath(__file__))
    1.18 +
    1.19 +
    1.20 +class Test1:
    1.21 +
    1.22 +    def setup_method(self):
    1.23 +        arthome = "test1"
    1.24 +
    1.25 +        os.environ["HOME"] = os.path.join(mydir, arthome)
    1.26 +        os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    1.27 +
    1.28 +        os.chdir(os.path.join(mydir, arthome))
    1.29 +
    1.30 +    @property
    1.31 +    def me(self):
    1.32 +        # because of flaws of py.test these two statements are necessary
    1.33 +        setup_gnupg()   # work around a bug with initializing os.environ
    1.34 +        import pEp      # after that import pEp module, not before
    1.35 +
    1.36 +        i = pEp.Identity()
    1.37 +        i.address = "test1@peptest.ch"
    1.38 +        i.username = "Alice One"
    1.39 +        pEp.myself(i)
    1.40 +        return i
    1.41 +
    1.42 +    @property
    1.43 +    def you(self):
    1.44 +        setup_gnupg() ; import pEp
    1.45 +
    1.46 +        i = pEp.Identity()
    1.47 +        i.address = "test2@peptest.ch"
    1.48 +        i.username = "Bob Two"
    1.49 +        pEp.update_identity(i)
    1.50 +        return i
    1.51 +
    1.52 +    def test_send_and_recv_message(self):
    1.53 +        setup_gnupg() ; import pEp
    1.54 +
    1.55 +        msg = pEp.Message()
    1.56 +        msg.from_ = self.me
    1.57 +        msg.to = [self.you]
    1.58 +        msg.shortmsg = "Subject line"
    1.59 +        msg.longmsg = "Message Text\n"
    1.60 +
    1.61 +        enc = msg.encrypt()
    1.62 +        send_message("test2", str(enc))
    1.63 +
    1.64 +        txt = wait_for_message()
    1.65 +        enc = pEp.Message(txt)
    1.66 +        assert enc.from_.address == "test2@peptest.ch"
    1.67 +        inc, keys, rating, consumed, flags = enc.decrypt()
    1.68 +        assert rating == 6
    1.69 +
    1.70 +
    1.71 +class Test2:
    1.72 +
    1.73 +    def setup_method(self):
    1.74 +        arthome = "test2"
    1.75 +
    1.76 +        os.environ["HOME"] = os.path.join(mydir, arthome)
    1.77 +        os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    1.78 +
    1.79 +        os.chdir(os.path.join(mydir, arthome))
    1.80 +
    1.81 +    @property
    1.82 +    def me(self):
    1.83 +        # because of flaws of py.test these two statements are necessary
    1.84 +        setup_gnupg()   # work around a bug with initializing os.environ
    1.85 +        import pEp      # after that import pEp module, not before
    1.86 +
    1.87 +        i = pEp.Identity()
    1.88 +        i.address = "test2@peptest.ch"
    1.89 +        i.username = "Bob Two"
    1.90 +        pEp.myself(i)
    1.91 +        return i
    1.92 +
    1.93 +    @property
    1.94 +    def you(self):
    1.95 +        setup_gnupg() ; import pEp
    1.96 +
    1.97 +        i = pEp.Identity()
    1.98 +        i.address = "test1@peptest.ch"
    1.99 +        i.username = "Alice One"
   1.100 +        pEp.update_identity(i)
   1.101 +        return i
   1.102 +
   1.103 +    def test_recv_and_send_message(self):
   1.104 +        setup_gnupg() ; import pEp
   1.105 +
   1.106 +        txt = wait_for_message()
   1.107 +        msg = pEp.Message(txt)
   1.108 +        msg.decrypt()
   1.109 +        assert msg.from_.address == self.you.address
   1.110 +
   1.111 +        out = pEp.Message(1)
   1.112 +        out.from_ = self.me
   1.113 +        out.to = [self.you]
   1.114 +        out.shortmsg = "Subject Back"
   1.115 +        out.longmsg = "Text Back\n"
   1.116 +
   1.117 +        enc = out.encrypt()
   1.118 +        send_message("test1", str(enc))
   1.119 +