sync.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const bcrypt = require('../bcrypt')
  2. test('salt_length', () => {
  3. const salt = bcrypt.genSaltSync(13);
  4. expect(salt).toHaveLength(29);
  5. const [_, version, rounds] = salt.split('$');
  6. expect(version).toStrictEqual('2b')
  7. expect(rounds).toStrictEqual('13')
  8. })
  9. test('salt_no_params', () => {
  10. const salt = bcrypt.genSaltSync();
  11. const [_, version, rounds] = salt.split('$');
  12. expect(version).toStrictEqual('2b')
  13. expect(rounds).toStrictEqual('10')
  14. })
  15. test('salt_rounds_is_string_number', () => {
  16. expect(() => bcrypt.genSaltSync('10')).toThrowError('rounds must be a number');
  17. })
  18. test('salt_rounds_is_NaN', () => {
  19. expect(() => bcrypt.genSaltSync('b')).toThrowError("rounds must be a number");
  20. })
  21. test('salt_minor_a', () => {
  22. const salt = bcrypt.genSaltSync(10, 'a');
  23. const [_, version, rounds] = salt.split('$');
  24. expect(version).toStrictEqual('2a')
  25. expect(rounds).toStrictEqual('10')
  26. })
  27. test('salt_minor_b', () => {
  28. const salt = bcrypt.genSaltSync(10, 'b');
  29. const [_, version, rounds] = salt.split('$');
  30. expect(version).toStrictEqual('2b')
  31. expect(rounds).toStrictEqual('10')
  32. })
  33. test('hash', () => {
  34. expect(() => bcrypt.hashSync('password', bcrypt.genSaltSync(10))).not.toThrow()
  35. })
  36. test('hash_rounds', () => {
  37. const hash = bcrypt.hashSync('password', 8);
  38. expect(bcrypt.getRounds(hash)).toStrictEqual(8)
  39. })
  40. test('hash_empty_string', () => {
  41. expect(() => bcrypt.hashSync('', bcrypt.genSaltSync(10))).not.toThrow();
  42. expect(() => bcrypt.hashSync('password', '')).toThrowError('Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue');
  43. expect(() => bcrypt.hashSync('', '')).toThrowError('Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue');
  44. })
  45. test('hash_pw_no_params', () => {
  46. expect(() => bcrypt.hashSync()).toThrow('data and salt arguments required');
  47. })
  48. test('hash_pw_one_param', () => {
  49. expect(() => bcrypt.hashSync('password')).toThrow('data and salt arguments required');
  50. })
  51. test('hash_pw_not_hash_str', () => {
  52. expect(() => bcrypt.hashSync('password', {})).toThrow("data must be a string or Buffer and salt must either be a salt string or a number of rounds")
  53. })
  54. test('hash_salt_validity', () => {
  55. expect(2);
  56. expect(bcrypt.hashSync('password', '$2a$10$somesaltyvaluertsetrse')).toBeDefined()
  57. expect(() => bcrypt.hashSync('password', 'some$value')).toThrow('Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue')
  58. })
  59. test('verify_salt', () => {
  60. const salt = bcrypt.genSaltSync(10);
  61. const split_salt = salt.split('$');
  62. expect(split_salt[1]).toStrictEqual('2b')
  63. expect(split_salt[2]).toStrictEqual('10')
  64. })
  65. test('verify_salt_min_rounds', () => {
  66. const salt = bcrypt.genSaltSync(1);
  67. const split_salt = salt.split('$');
  68. expect(split_salt[1]).toStrictEqual('2b')
  69. expect(split_salt[2]).toStrictEqual('04')
  70. })
  71. test('verify_salt_max_rounds', () => {
  72. const salt = bcrypt.genSaltSync(100);
  73. const split_salt = salt.split('$');
  74. expect(split_salt[1]).toStrictEqual('2b')
  75. expect(split_salt[2]).toStrictEqual('31')
  76. })
  77. test('hash_compare', () => {
  78. const salt = bcrypt.genSaltSync(10);
  79. expect(29).toStrictEqual(salt.length)
  80. const hash = bcrypt.hashSync("test", salt);
  81. expect(bcrypt.compareSync("test", hash)).toBeDefined()
  82. expect(!(bcrypt.compareSync("blah", hash))).toBeDefined()
  83. })
  84. test('hash_compare_empty_strings', () => {
  85. expect(!(bcrypt.compareSync("", "password"))).toBeDefined()
  86. expect(!(bcrypt.compareSync("", ""))).toBeDefined()
  87. expect(!(bcrypt.compareSync("password", ""))).toBeDefined()
  88. })
  89. test('hash_compare_invalid_strings', () => {
  90. const fullString = 'envy1362987212538';
  91. const hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
  92. const wut = ':';
  93. expect(bcrypt.compareSync(fullString, hash)).toBe(true);
  94. expect(bcrypt.compareSync(fullString, wut)).toBe(false);
  95. })
  96. test('getRounds', () => {
  97. const hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
  98. expect(9).toStrictEqual(bcrypt.getRounds(hash))
  99. })
  100. test('getRounds', () => {
  101. const hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
  102. expect(9).toStrictEqual(bcrypt.getRounds(hash))
  103. expect(() => bcrypt.getRounds('')).toThrow("invalid hash provided");
  104. });