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

index.test.js 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const assert = require("assert");
  2. const { renderPassword } = require("../src/index");
  3. test("render password use remainder of long division between entropy and set of chars length as an index", () => {
  4. const options = {
  5. length: 16,
  6. lowercase: true,
  7. uppercase: true,
  8. digits: true,
  9. symbols: true
  10. };
  11. assert.equal(
  12. "W",
  13. renderPassword(
  14. "dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e",
  15. options
  16. )[0]
  17. );
  18. });
  19. test("render password use quotient as second entropy recursively", () => {
  20. const options = {
  21. length: 16,
  22. lowercase: true,
  23. uppercase: true,
  24. digits: true,
  25. symbols: true
  26. };
  27. assert.equal(
  28. "H",
  29. renderPassword(
  30. "dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e",
  31. options
  32. )[1]
  33. );
  34. });
  35. test("render password has default length of 16", () => {
  36. const options = {
  37. length: 16,
  38. lowercase: true,
  39. uppercase: true,
  40. digits: true,
  41. symbols: true
  42. };
  43. const password = renderPassword(
  44. "dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e",
  45. options
  46. );
  47. assert.equal(16, password.length);
  48. });
  49. test("render password can specify length", () => {
  50. const options = {
  51. length: 20,
  52. lowercase: true,
  53. uppercase: true,
  54. digits: true,
  55. symbols: true
  56. };
  57. const password = renderPassword(
  58. "dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e",
  59. options
  60. );
  61. assert.equal(20, password.length);
  62. });
  63. test("render password return at least one char in every characters set", () => {
  64. const entropy =
  65. "dc33d431bce2b01182c613382483ccdb0e2f66482cbba5e9d07dab34acc7eb1e";
  66. const passwordProfile = {
  67. length: 6,
  68. lowercase: true,
  69. uppercase: true,
  70. digits: true,
  71. symbols: true
  72. };
  73. const generatedPassword = renderPassword(entropy, passwordProfile);
  74. let passwordLength = generatedPassword.length;
  75. let lowercaseOk = false;
  76. let uppercaseOk = false;
  77. let digitsOk = false;
  78. let symbolsOk = false;
  79. for (passwordLength; passwordLength > 0; passwordLength -= 1) {
  80. if (
  81. "abcdefghijklmnopqrstuvwxyz".indexOf(
  82. generatedPassword[passwordLength - 1]
  83. ) !== -1
  84. ) {
  85. lowercaseOk = true;
  86. }
  87. if (
  88. "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(
  89. generatedPassword[passwordLength - 1]
  90. ) !== -1
  91. ) {
  92. uppercaseOk = true;
  93. }
  94. if ("0123456789".indexOf(generatedPassword[passwordLength - 1]) !== -1) {
  95. digitsOk = true;
  96. }
  97. if (
  98. "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".indexOf(
  99. generatedPassword[passwordLength - 1]
  100. ) !== -1
  101. ) {
  102. symbolsOk = true;
  103. }
  104. }
  105. assert.equal(6, generatedPassword.length);
  106. assert(
  107. lowercaseOk && uppercaseOk && digitsOk && symbolsOk,
  108. "there is no at least one char in every characters set"
  109. );
  110. });