test_crypto_engine.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute iut and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import unittest
  20. import os
  21. import time
  22. import string
  23. from pwman.util.callback import Callback
  24. from pwman.util.crypto_engine import (CryptoEngine, CryptoException,
  25. generate_password)
  26. # set cls_timout to negative number (e.g. -1) to disable
  27. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  28. 'cls_timeout': '5'
  29. },
  30. 'Database': {'type': 'SQLite',
  31. 'filename': os.path.join("tests", "pwman.db")},
  32. 'Encryption': {'algorithm': 'AES'},
  33. 'Readline': {'history': os.path.join("tests",
  34. "history")}
  35. }
  36. give_key = lambda msg: b"12345"
  37. give_wrong_key = lambda msg: b"verywrongtkey"
  38. salt = b'cUDHNMJdTRxiIDPXuT163UMvi4fd2pXz/bRg2Zm8ajE='
  39. digest = b'9eaec7dc1ee647338406739c54dbf9c4881c74702008eb978622811cfc46a07f'
  40. class DummyCallback(Callback):
  41. def getinput(self, question):
  42. return b'12345'
  43. def getsecret(self, question):
  44. return b'12345'
  45. def getnewsecret(self, question):
  46. return b'12345'
  47. class TestPassGenerator(unittest.TestCase):
  48. def test_len(self):
  49. self.assertEqual(13, len(generate_password(pass_len=13)))
  50. def test_has_no_lower(self):
  51. password = generate_password(uppercase=True, lowercase=False)
  52. lower = set(string.ascii_lowercase)
  53. it = lower.intersection(set(password))
  54. print(it)
  55. self.assertTrue(len(it) == 0)
  56. def test_has_digits(self):
  57. password = generate_password(uppercase=True, lowercase=False)
  58. digits = set(string.digits)
  59. it = digits.intersection(password)
  60. print(it)
  61. try:
  62. self.assertTrue(len(it) >= 0)
  63. except unittest.AssetionError:
  64. print(it)
  65. def test_has_no_digits(self):
  66. password = generate_password(uppercase=True, digits=False,
  67. lowercase=False)
  68. digits = set(string.digits)
  69. it = digits.intersection(password)
  70. print(it)
  71. try:
  72. self.assertTrue(len(it) == 0)
  73. except unittest.AssetionError:
  74. print(it)
  75. class CryptoEngineTest(unittest.TestCase):
  76. def test4_d_get_crypto(self):
  77. ce = CryptoEngine.get()
  78. ce.callback = DummyCallback()
  79. secret2 = ce.changepassword(reader=give_key)
  80. secret1 = ce.changepassword(reader=give_key)
  81. # althouth the same secret key is used,
  82. # the secret hash is not the same, because a
  83. # different random seed is used when calling
  84. # CryptoEngine._get_digest
  85. self.assertNotEqual(secret1, secret2)
  86. def test5_e_authenticate(self):
  87. ce = CryptoEngine.get()
  88. ce._reader = give_key
  89. self.assertFalse(ce.authenticate(b'verywrong'))
  90. self.assertTrue(ce.authenticate(b'12345'))
  91. ce._expires_at = int(time.time()) + 600
  92. self.assertTrue(ce._is_authenticated())
  93. def test6_is_timedout(self):
  94. ce = CryptoEngine.get()
  95. ce._expires_at = time.time() - 2
  96. time.sleep(1.1)
  97. self.assertTrue(ce._is_timedout())
  98. self.assertIsNone(ce._cipher)
  99. self.assertFalse(ce._is_authenticated())
  100. def test_f_encrypt_decrypt(self):
  101. ce = CryptoEngine.get()
  102. ce._reader = give_key
  103. if not ce._salt:
  104. ce._salt = salt
  105. secret = ce.encrypt(b"topsecret")
  106. decrypt = ce.decrypt(secret)
  107. self.assertEqual(decrypt.decode(), "topsecret")
  108. ce._cipher = None
  109. secret = ce.encrypt(b"topsecret")
  110. decrypt = ce.decrypt(secret)
  111. self.assertEqual(decrypt.decode(), "topsecret")
  112. def test_g_encrypt_decrypt_wrong_pass(self):
  113. ce = CryptoEngine.get()
  114. ce._cipher = None
  115. ce._getsecret = give_wrong_key
  116. self.assertRaises(CryptoException, ce.encrypt, b"secret")
  117. ce._getsecret = lambda x: b'12345'
  118. secret = ce.encrypt(b"topsecret")
  119. decrypt = ce.decrypt(secret)
  120. self.assertEqual(decrypt.decode(), "topsecret")
  121. if __name__ == '__main__':
  122. unittest.main(verbosity=2, failfast=True)