xkcdp_tests.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import unittest
  2. import subprocess
  3. import re
  4. from xkcdpass import xkcd_password
  5. WORDFILE = 'xkcdpass/static/default.txt'
  6. class XkcdPasswordTests(unittest.TestCase):
  7. def setUp(self):
  8. self.wordlist_full = xkcd_password.generate_wordlist(
  9. wordfile=WORDFILE,
  10. min_length=5,
  11. max_length=8,)
  12. self.wordlist_small = xkcd_password.generate_wordlist(
  13. wordfile='tests/test_list.txt',
  14. valid_chars='[a-z]')
  15. def test_loadwordfile(self):
  16. self.assertEquals(len(self.wordlist_full), 29612)
  17. def test_regex(self):
  18. self.assertNotIn("__$$$__", self.wordlist_small)
  19. def test_acrostic(self):
  20. word = "face"
  21. result = xkcd_password.generate_xkcdpassword(
  22. self.wordlist_small,
  23. acrostic=word)
  24. self.assertEquals("".join(map(lambda x: x[0], result.split())), word)
  25. def test_commandlineCount(self):
  26. count = 5
  27. result = subprocess.check_output(
  28. ["python", "xkcdpass/xkcd_password.py", "-w", WORDFILE,
  29. "-c", str(count)])
  30. self.assertTrue(result.count(b"\n"), count)
  31. def test_delim(self):
  32. tdelim = "_"
  33. result = xkcd_password.generate_xkcdpassword(
  34. self.wordlist_small,
  35. delimiter=tdelim)
  36. self.assertIsNotNone(re.match('([a-z]+(_|$))+', result))
  37. if __name__ == '__main__':
  38. suite = unittest.TestLoader().loadTestsFromTestCase(XkcdPasswordTests)
  39. unittest.TextTestRunner(verbosity=2).run(suite)