| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | # ============================================================================# This file is part of Pwman3.## Pwman3 is free software; you can redistribute iut and/or modify# it under the terms of the GNU General Public License, version 2# as published by the Free Software Foundation;## Pwman3 is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with Pwman3; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA# ============================================================================# Copyright (C) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com># ============================================================================import unittestimport osimport timeimport stringfrom pwman.util.callback import Callbackfrom pwman.util.crypto_engine import (CryptoEngine, CryptoException,                                      generate_password)# set cls_timout to negative number (e.g. -1) to disabledefault_config = {'Global': {'umask': '0100', 'colors': 'yes',                             'cls_timeout': '5'                             },                  'Database': {'type': 'SQLite',                               'filename': os.path.join("tests", "pwman.db")},                  'Encryption': {'algorithm': 'AES'},                  'Readline': {'history': os.path.join("tests",                                                       "history")}                  }give_key = lambda msg: "12345"give_wrong_key = lambda msg: "verywrongtkey"salt = b'cUDHNMJdTRxiIDPXuT163UMvi4fd2pXz/bRg2Zm8ajE='digest = b'9eaec7dc1ee647338406739c54dbf9c4881c74702008eb978622811cfc46a07f'class DummyCallback(Callback):    def getinput(self, question):        return u'12345'    def getsecret(self, question):        return u'12345'    def getnewsecret(self, question):        return u'12345'class TestPassGenerator(unittest.TestCase):    def test_len(self):        self.assertEqual(13, len(generate_password(pass_len=13)))    def test_has_upper(self):        password = generate_password(uppercase=True, lowercase=False)        lower = set(string.ascii_lowercase)        it = lower.intersection(set(password))        print(it)        self.assertTrue(len(it) == 0)    def test_has_digits(self):        password = generate_password(uppercase=True, lowercase=False)        digits = set(string.digits)        it = digits.intersection(password)        print(it)        try:            self.assertTrue(len(it) >= 0)        except unittest.AssetionError:            print(it)    def test_has_no_digits(self):        password = generate_password(uppercase=True, digits=False,                                     lowercase=False)        digits = set(string.digits)        it = digits.intersection(password)        print(it)        try:            self.assertTrue(len(it) == 0)        except unittest.AssetionError:            print(it)class CryptoEngineTest(unittest.TestCase):    def test4_d_get_crypto(self):        ce = CryptoEngine.get()        ce.callback = DummyCallback()        secret2 = ce.changepassword(reader=give_key)        secret1 = ce.changepassword(reader=give_key)        # althouth the same secret key is used,        # the secret hash is not the same, because a        # different random seed is used when calling        # CryptoEngine._get_digest        self.assertNotEqual(secret1, secret2)    def test5_e_authenticate(self):        ce = CryptoEngine.get()        ce._reader = give_key        self.assertFalse(ce.authenticate('verywrong'))        self.assertTrue(ce.authenticate('12345'))        ce._timeout = -1        self.assertTrue(ce._is_authenticated())    def test6_is_timedout(self):        ce = CryptoEngine.get()        ce._timeout = 1        time.sleep(1.1)        self.assertTrue(ce._is_timedout())        self.assertIsNone(ce._cipher)        self.assertFalse(ce._is_authenticated())    def test_f_encrypt_decrypt(self):        ce = CryptoEngine.get()        ce._reader = give_key        if not ce._salt:            ce._salt = salt        secret = ce.encrypt("topsecret")        decrypt = ce.decrypt(secret)        self.assertEqual(decrypt.decode(), "topsecret")        ce._cipher = None        secret = ce.encrypt("topsecret")        decrypt = ce.decrypt(secret)        self.assertEqual(decrypt.decode(), "topsecret")    def test_g_encrypt_decrypt_wrong_pass(self):        ce = CryptoEngine.get()        ce._cipher = None        ce._getsecret = give_wrong_key        self.assertRaises(CryptoException, ce.encrypt, "secret")        ce._getsecret = lambda x: u'12345'        secret = ce.encrypt(u"topsecret")        decrypt = ce.decrypt(secret)        self.assertEqual(decrypt.decode(), "topsecret")if __name__ == '__main__':    unittest.main(verbosity=2, failfast=True)
 |