cubicweb/test/unittest_crypto.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 16 May 2019 17:17:42 +0200
branch3.26
changeset 12615 7abe23cbfda1
child 12682 da36da3f89f1
permissions -rw-r--r--
Make crypto module python3-compatible * Remove usage of unicode() and decode the base64-encoded string in encrypt(); * Encode the string received in decrypt() as (I supposed) it should come from the result of encrypt(). Add tests for this module along the way.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12615
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     1
from unittest import TestCase
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     2
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     3
from cubicweb import crypto
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     4
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     5
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     6
class CryptoTC(TestCase):
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     7
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     8
    def test_encrypt_decrypt_roundtrip(self):
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
     9
        data = {'a': u'ah', 'b': [1, 2]}
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    10
        seed = 'ssss'
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    11
        crypted = crypto.encrypt(data, seed)
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    12
        decrypted = crypto.decrypt(crypted, seed)
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    13
        self.assertEqual(decrypted, data)
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    14
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    15
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    16
if __name__ == '__main__':
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    17
    import unittest
7abe23cbfda1 Make crypto module python3-compatible
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
diff changeset
    18
    unittest.main()