Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

222 řádky
7.1 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_set_of_characters_without_rule(self):
  118. self.assertEqual(
  119. password._get_set_of_characters(),
  120. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  121. )
  122. def test_get_set_of_characters_with_single_rule(self):
  123. self.assertEqual(
  124. password._get_set_of_characters(["lowercase"]), "abcdefghijklmnopqrstuvwxyz"
  125. )
  126. self.assertEqual(
  127. password._get_set_of_characters(["uppercase"]), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  128. )
  129. self.assertEqual(password._get_set_of_characters(["digits"]), "0123456789")
  130. self.assertEqual(
  131. password._get_set_of_characters(["symbols"]),
  132. "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  133. )
  134. def test_get_set_of_characters_with_several_rules(self):
  135. self.assertEqual(
  136. password._get_set_of_characters(["lowercase", "digits"]),
  137. "abcdefghijklmnopqrstuvwxyz0123456789",
  138. )
  139. def test_get_set_of_characters_with_several_rules_and_exclude(self):
  140. self.assertEqual(
  141. password._get_set_of_characters(["lowercase", "digits"], 'iy4!'),
  142. "abcdefghjklmnopqrstuvwxz012356789",
  143. )
  144. def test_consume_entropy(self):
  145. entropy = b"dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e"
  146. password_value, password_entropy = password._consume_entropy(
  147. generated_password="",
  148. quotient=int(entropy, 16),
  149. set_of_characters="abcdefghijklmnopqrstuvwxyz0123456789",
  150. max_length=12,
  151. )
  152. self.assertEqual(password_value, "gsrwvjl3d0sn")
  153. self.assertEqual(
  154. password_entropy,
  155. 21019920789038193790619410818194537836313158091882651458040,
  156. )
  157. def test_get_one_char_per_rule_without_rules(self):
  158. self.assertListEqual(
  159. password._get_one_char_per_rule(
  160. entropy=21019920789038193790619410818194537836313158091882651458040,
  161. rules=[],
  162. ),
  163. ["", 21019920789038193790619410818194537836313158091882651458040],
  164. )
  165. def test_get_one_char_per_rule_with_several_rules(self):
  166. self.assertListEqual(
  167. password._get_one_char_per_rule(
  168. entropy=21019920789038193790619410818194537836313158091882651458040,
  169. rules=["lowercase", "digits"],
  170. ),
  171. ["a0", 80845849188608437656228503146902068601204454199548659454],
  172. )
  173. def test_insert_string_pseudo_randomly(self):
  174. self.assertEqual(
  175. password._insert_string_pseudo_randomly(
  176. generated_password="gsrwvjl3d0sn",
  177. entropy=80845849188608437656228503146902068601204454199548659454,
  178. string="a0",
  179. ),
  180. "gsrwvjl03d0asn",
  181. )
  182. def test_render_password(self):
  183. password_profile = {
  184. "site": "example.org",
  185. "login": "contact@example.org",
  186. "digits": True,
  187. "lowercase": True,
  188. "length": 14,
  189. "counter": 1,
  190. }
  191. master_password = "password"
  192. entropy = password._calc_entropy(password_profile, master_password)
  193. self.assertEqual(
  194. password._render_password(entropy, password_profile), "gsrwvjl03d0asn"
  195. )