You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

234 rivejä
7.4 KiB

  1. import unittest
  2. from lesspass.password import generate_password
  3. from lesspass import password
  4. class TestPassword(unittest.TestCase):
  5. def test_generate_password(self):
  6. profile = {
  7. "site": "example.org",
  8. "login": "contact@example.org",
  9. "lowercase": True,
  10. "uppercase": True,
  11. "digits": True,
  12. "symbols": True,
  13. "length": 16,
  14. "counter": 1,
  15. }
  16. master_password = "password"
  17. self.assertEqual(
  18. generate_password(profile, master_password), "WHLpUL)e00[iHR+w"
  19. )
  20. def test_generate_password_2(self):
  21. profile = {
  22. "site": "example.org",
  23. "login": "contact@example.org",
  24. "lowercase": True,
  25. "uppercase": True,
  26. "digits": True,
  27. "symbols": False,
  28. "length": 14,
  29. "counter": 2,
  30. }
  31. master_password = "password"
  32. self.assertEqual(generate_password(profile, master_password), "MBAsB7b1Prt8Sl")
  33. def test_generate_password_3(self):
  34. profile = {
  35. "site": "example.org",
  36. "login": "contact@example.org",
  37. "lowercase": False,
  38. "uppercase": False,
  39. "digits": True,
  40. "symbols": False,
  41. "length": 16,
  42. "counter": 1,
  43. }
  44. master_password = "password"
  45. self.assertEqual(
  46. generate_password(profile, master_password), "8742368585200667"
  47. )
  48. def test_generate_password_4(self):
  49. profile = {
  50. "site": "example.org",
  51. "login": "contact@example.org",
  52. "lowercase": True,
  53. "uppercase": True,
  54. "digits": False,
  55. "symbols": True,
  56. "length": 16,
  57. "counter": 1,
  58. }
  59. master_password = "password"
  60. self.assertEqual(
  61. generate_password(profile, master_password), "s>{F}RwkN/-fmM.X"
  62. )
  63. def test_generate_password_nrt_328(self):
  64. profile = {
  65. "site": "site",
  66. "login": "login",
  67. "lowercase": True,
  68. "uppercase": True,
  69. "digits": True,
  70. "symbols": True,
  71. "length": 16,
  72. "counter": 10,
  73. }
  74. master_password = "test"
  75. self.assertEqual(
  76. generate_password(profile, master_password), "XFt0F*,r619:+}[."
  77. )
  78. def test_generate_password_unicode(self):
  79. profile = {
  80. "site": "♥ LessPass ♥",
  81. "login": "test@example.org",
  82. "lowercase": True,
  83. "uppercase": True,
  84. "digits": True,
  85. "symbols": True,
  86. "length": 16,
  87. "counter": 1,
  88. }
  89. master_password = "password"
  90. self.assertEqual(
  91. generate_password(profile, master_password), "BH$>U5Lj7v9A1wB/"
  92. )
  93. def test_calc_entropy(self):
  94. password_profile = {
  95. "site": "example.org",
  96. "login": "contact@example.org",
  97. "counter": 1,
  98. }
  99. master_password = "password"
  100. self.assertEqual(
  101. password._calc_entropy(password_profile, master_password),
  102. 99600400399777174105034830393873797761817714609490038944205586760025858632478,
  103. )
  104. def test_get_configured_rules_empty_when_no_rules_in_profile(self):
  105. password_profile = {}
  106. self.assertListEqual(password._get_configured_rules(password_profile), [])
  107. def test_get_configured_rules_ignore_disable_rules(self):
  108. password_profile = {
  109. "lowercase": False,
  110. "uppercase": True,
  111. "digits": False,
  112. "symbols": True,
  113. }
  114. self.assertListEqual(
  115. password._get_configured_rules(password_profile), ["uppercase", "symbols"]
  116. )
  117. def test_get_configured_rules_use_numbers_as_digits(self):
  118. password_profile = {
  119. "lowercase": False,
  120. "uppercase": False,
  121. "numbers": True,
  122. "symbols": False,
  123. }
  124. self.assertListEqual(
  125. password._get_configured_rules(password_profile), ["digits"]
  126. )
  127. def test_get_set_of_characters_without_rule(self):
  128. self.assertEqual(
  129. password._get_set_of_characters(),
  130. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  131. )
  132. def test_get_set_of_characters_with_single_rule(self):
  133. self.assertEqual(
  134. password._get_set_of_characters(["lowercase"]), "abcdefghijklmnopqrstuvwxyz"
  135. )
  136. self.assertEqual(
  137. password._get_set_of_characters(["uppercase"]), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  138. )
  139. self.assertEqual(password._get_set_of_characters(["digits"]), "0123456789")
  140. self.assertEqual(
  141. password._get_set_of_characters(["symbols"]),
  142. "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  143. )
  144. def test_get_set_of_characters_with_several_rules(self):
  145. self.assertEqual(
  146. password._get_set_of_characters(["lowercase", "digits"]),
  147. "abcdefghijklmnopqrstuvwxyz0123456789",
  148. )
  149. def test_get_set_of_characters_with_several_rules_and_exclude(self):
  150. self.assertEqual(
  151. password._get_set_of_characters(["lowercase", "digits"], "iy4!"),
  152. "abcdefghjklmnopqrstuvwxz012356789",
  153. )
  154. def test_consume_entropy(self):
  155. entropy = b"dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e"
  156. password_value, password_entropy = password._consume_entropy(
  157. generated_password="",
  158. quotient=int(entropy, 16),
  159. set_of_characters="abcdefghijklmnopqrstuvwxyz0123456789",
  160. max_length=12,
  161. )
  162. self.assertEqual(password_value, "gsrwvjl3d0sn")
  163. self.assertEqual(
  164. password_entropy,
  165. 21019920789038193790619410818194537836313158091882651458040,
  166. )
  167. def test_get_one_char_per_rule_without_rules(self):
  168. self.assertListEqual(
  169. password._get_one_char_per_rule(
  170. entropy=21019920789038193790619410818194537836313158091882651458040,
  171. rules=[],
  172. ),
  173. ["", 21019920789038193790619410818194537836313158091882651458040],
  174. )
  175. def test_get_one_char_per_rule_with_several_rules(self):
  176. self.assertListEqual(
  177. password._get_one_char_per_rule(
  178. entropy=21019920789038193790619410818194537836313158091882651458040,
  179. rules=["lowercase", "digits"],
  180. ),
  181. ["a0", 80845849188608437656228503146902068601204454199548659454],
  182. )
  183. def test_insert_string_pseudo_randomly(self):
  184. self.assertEqual(
  185. password._insert_string_pseudo_randomly(
  186. generated_password="gsrwvjl3d0sn",
  187. entropy=80845849188608437656228503146902068601204454199548659454,
  188. string="a0",
  189. ),
  190. "gsrwvjl03d0asn",
  191. )
  192. def test_render_password(self):
  193. password_profile = {
  194. "site": "example.org",
  195. "login": "contact@example.org",
  196. "digits": True,
  197. "lowercase": True,
  198. "length": 14,
  199. "counter": 1,
  200. }
  201. master_password = "password"
  202. entropy = password._calc_entropy(password_profile, master_password)
  203. self.assertEqual(
  204. password._render_password(entropy, password_profile), "gsrwvjl03d0asn"
  205. )