repetitions.test.js
1 const bcrypt = require('../bcrypt'); 2 3 const EXPECTED = 2500; //number of times to iterate these tests.) 4 const { TEST_TIMEOUT_SECONDS } = process.env; 5 let timeout = 5e3; // default test timeout 6 7 // it is necessary to increase the test timeout when emulating cross-architecture 8 // environments (i.e. arm64 from x86-64 host) which have significantly reduced performance: 9 if ( TEST_TIMEOUT_SECONDS ) 10 timeout = Number.parseInt(TEST_TIMEOUT_SECONDS, 10) * 1e3; 11 12 jest.setTimeout(timeout); 13 14 test('salt_length', () => { 15 expect.assertions(EXPECTED); 16 17 return Promise.all(Array.from({length: EXPECTED}, 18 () => bcrypt.genSalt(10) 19 .then(salt => expect(salt).toHaveLength(29)))); 20 }) 21 22 test('test_hash_length', () => { 23 expect.assertions(EXPECTED); 24 const SALT = '$2a$04$TnjywYklQbbZjdjBgBoA4e'; 25 return Promise.all(Array.from({length: EXPECTED}, 26 () => bcrypt.hash('test', SALT) 27 .then(hash => expect(hash).toHaveLength(60)))); 28 }) 29 30 test('test_compare', () => { 31 expect.assertions(EXPECTED); 32 const HASH = '$2a$04$TnjywYklQbbZjdjBgBoA4e9G7RJt9blgMgsCvUvus4Iv4TENB5nHy'; 33 return Promise.all(Array.from({length: EXPECTED}, 34 () => bcrypt.compare('test', HASH) 35 .then(match => expect(match).toEqual(true)))); 36 }) 37 38 test('test_hash_and_compare', () => { 39 expect.assertions(EXPECTED * 3); 40 const salt = bcrypt.genSaltSync(4) 41 42 return Promise.all(Array.from({length: EXPECTED}, 43 () => { 44 const password = 'secret' + Math.random(); 45 return bcrypt.hash(password, salt) 46 .then(hash => { 47 expect(hash).toHaveLength(60); 48 const goodCompare = bcrypt.compare(password, hash).then(res => expect(res).toEqual(true)); 49 const badCompare = bcrypt.compare('bad' + password, hash).then(res => expect(res).toEqual(false)); 50 51 return Promise.all([goodCompare, badCompare]); 52 }); 53 })); 54 }, timeout * 3); 55