crypto_tests.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import pwman.util.config as config
  2. import os
  3. from pwman.util.crypto_engine import (CryptoEngine, CryptoException)
  4. # set cls_timout to negative number (e.g. -1) to disable
  5. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  6. 'cls_timeout': '5'
  7. },
  8. 'Database': {'type': 'SQLite',
  9. 'filename': os.path.join("tests", "pwman.db")},
  10. 'Encryption': {'algorithm': 'AES'},
  11. 'Readline': {'history': os.path.join("tests",
  12. "history")}
  13. }
  14. config.set_defaults(default_config)
  15. import unittest
  16. class CryptoTest(unittest.TestCase):
  17. def test_no_algorithm(self):
  18. CryptoEngine._instance_new = None
  19. config.set_value('Encryption', 'algorithm', '')
  20. self.assertRaises((CryptoException,), CryptoEngine.get)
  21. config.set_value('Encryption', 'algorithm', 'AES')
  22. def test_getcipher(self):
  23. crypto = CryptoEngine()
  24. self.assertRaises((CryptoNoCallbackException,), crypto._getcipher)
  25. def test_prepare_data(self):
  26. obj = 'dummy_data'
  27. self.assertTrue(True)
  28. def test_get(self):
  29. CryptoEngine._instance_new = None
  30. old_engine = CryptoEngine.get(0.4)
  31. self.assertIsInstance(old_engine, CryptoEngineOld)
  32. CryptoEngine._instance = None
  33. new_engine = CryptoEngine.get(dbver=0.5)
  34. self.assertIsInstance(new_engine, CryptoEngine)
  35. self.assertFalse(isinstance(new_engine, CryptoEngineOld))
  36. CryptoEngine._instance = None
  37. old_engine = CryptoEngine.get()
  38. def test_alive(self):
  39. old_engine = CryptoEngine.get()
  40. self.assertTrue(old_engine.alive())
  41. old_engine._cipher = None
  42. self.assertFalse(old_engine.alive())
  43. CryptoEngine.get()
  44. if __name__ == '__main__':
  45. unittest.main()