25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
6.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_calc_entropy(self):
  79. password_profile = {
  80. "site": "example.org",
  81. "login": "contact@example.org",
  82. "counter": 1,
  83. }
  84. master_password = "password"
  85. self.assertEqual(
  86. password._calc_entropy(password_profile, master_password),
  87. b"dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e",
  88. )
  89. def test_get_configured_rules_empty_when_no_rules_in_profile(self):
  90. password_profile = {}
  91. self.assertListEqual(password._get_configured_rules(password_profile), [])
  92. def test_get_configured_rules_ignore_disable_rules(self):
  93. password_profile = {
  94. "lowercase": False,
  95. "uppercase": True,
  96. "digits": False,
  97. "symbols": True,
  98. }
  99. self.assertListEqual(
  100. password._get_configured_rules(password_profile), ["uppercase", "symbols"]
  101. )
  102. def test_get_set_of_characters_without_rule(self):
  103. self.assertEqual(
  104. password._get_set_of_characters(),
  105. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  106. )
  107. def test_get_set_of_characters_with_single_rule(self):
  108. self.assertEqual(
  109. password._get_set_of_characters(["lowercase"]), "abcdefghijklmnopqrstuvwxyz"
  110. )
  111. self.assertEqual(
  112. password._get_set_of_characters(["uppercase"]), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  113. )
  114. self.assertEqual(password._get_set_of_characters(["digits"]), "0123456789")
  115. self.assertEqual(
  116. password._get_set_of_characters(["symbols"]),
  117. "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  118. )
  119. def test_get_set_of_characters_with_several_rules(self):
  120. self.assertEqual(
  121. password._get_set_of_characters(["lowercase", "digits"]),
  122. "abcdefghijklmnopqrstuvwxyz0123456789",
  123. )
  124. def test_consume_entropy(self):
  125. entropy = b"dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e"
  126. password_value, password_entropy = password._consume_entropy(
  127. generated_password="",
  128. quotient=int(entropy, 16),
  129. set_of_characters="abcdefghijklmnopqrstuvwxyz0123456789",
  130. max_length=12,
  131. )
  132. self.assertEqual(password_value, "gsrwvjl3d0sn")
  133. self.assertEqual(
  134. password_entropy,
  135. 21019920789038193790619410818194537836313158091882651458040,
  136. )
  137. def test_get_one_char_per_rule_without_rules(self):
  138. self.assertListEqual(
  139. password._get_one_char_per_rule(
  140. entropy=21019920789038193790619410818194537836313158091882651458040,
  141. rules=[],
  142. ),
  143. ["", 21019920789038193790619410818194537836313158091882651458040],
  144. )
  145. def test_get_one_char_per_rule_with_several_rules(self):
  146. self.assertListEqual(
  147. password._get_one_char_per_rule(
  148. entropy=21019920789038193790619410818194537836313158091882651458040,
  149. rules=["lowercase", "digits"],
  150. ),
  151. ["a0", 80845849188608437656228503146902068601204454199548659454],
  152. )
  153. def test_insert_string_pseudo_randomly(self):
  154. self.assertEqual(
  155. password._insert_string_pseudo_randomly(
  156. generated_password="gsrwvjl3d0sn",
  157. entropy=80845849188608437656228503146902068601204454199548659454,
  158. string="a0",
  159. ),
  160. "gsrwvjl03d0asn",
  161. )
  162. def test_render_password(self):
  163. password_profile = {
  164. "site": "example.org",
  165. "login": "contact@example.org",
  166. "digits": True,
  167. "lowercase": True,
  168. "length": 14,
  169. "counter": 1,
  170. }
  171. master_password = "password"
  172. entropy = password._calc_entropy(password_profile, master_password)
  173. self.assertEqual(
  174. password._render_password(entropy, password_profile), "gsrwvjl03d0asn"
  175. )