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