|
| 1 | +"use strict"; |
| 2 | +const assert = require('assert'); |
| 3 | +const crypto = require('crypto'); |
| 4 | +const execFile = require('child_process').execFile; |
| 5 | +const sps = require('..'); |
| 6 | + |
| 7 | +Promise.all([ |
| 8 | + (function truncatedHashTest() { |
| 9 | + const testPassword = crypto.randomBytes(3).toString('hex'); |
| 10 | + return sps.PasswordStorage.createHash(testPassword) |
| 11 | + .then(hash => |
| 12 | + sps.PasswordStorage.verifyPassword(testPassword, hash.slice(0, hash.length - 1))) |
| 13 | + .then(accepted => assert(false, 'Should not have accepted password')) |
| 14 | + .catch(reason => { |
| 15 | + if (!(reason instanceof sps.InvalidHashException)) |
| 16 | + throw reason; |
| 17 | + }); |
| 18 | + })(), |
| 19 | + (function basicTests() { |
| 20 | + const testPassword = crypto.randomBytes(3).toString('hex'); |
| 21 | + const anotherPassword = crypto.randomBytes(3).toString('hex'); |
| 22 | + |
| 23 | + return Promise.all([ |
| 24 | + sps.PasswordStorage.createHash(testPassword), |
| 25 | + sps.PasswordStorage.createHash(testPassword), |
| 26 | + ]).then(hashes => { |
| 27 | + assert.notStrictEqual(hashes[0], hashes[1], 'Two hashes are equal'); |
| 28 | + return Promise.all([ |
| 29 | + sps.PasswordStorage.verifyPassword(anotherPassword, hashes[0]), |
| 30 | + sps.PasswordStorage.verifyPassword(testPassword, hashes[0]) |
| 31 | + ]); |
| 32 | + }).then(accepted => { |
| 33 | + assert.strictEqual(accepted[0], false, 'Wrong password accepted'); |
| 34 | + assert.strictEqual(accepted[1], true, 'Good password not accepted'); |
| 35 | + }); |
| 36 | + })(), |
| 37 | + (function testHashFunctionChecking() { |
| 38 | + const testPassword = crypto.randomBytes(3).toString('hex'); |
| 39 | + return sps.PasswordStorage.createHash(testPassword) |
| 40 | + .then(hash => |
| 41 | + sps.PasswordStorage.verifyPassword(testPassword, hash.replace(/^sha1/, 'md5'))) |
| 42 | + .then(accepted => assert.strictEqual(accepted, false, |
| 43 | + 'Should not have accepted password')); |
| 44 | + })(), |
| 45 | + (function testGoodHashInPhp() { |
| 46 | + const testPassword = crypto.randomBytes(3).toString('hex'); |
| 47 | + return sps.PasswordStorage.createHash(testPassword) |
| 48 | + .then(hash => phpVerify(testPassword, hash)); |
| 49 | + })(), |
| 50 | + (function testBadHashInPhp() { |
| 51 | + const testPassword = crypto.randomBytes(3).toString('hex'); |
| 52 | + const errorOccurred = Symbol(); |
| 53 | + return sps.PasswordStorage.createHash(testPassword) |
| 54 | + .then(hash => phpVerify(testPassword, hash.slice(0, hash.length - 1))) |
| 55 | + .catch(reason => { |
| 56 | + // Swallow this error, it is expected |
| 57 | + return errorOccurred; |
| 58 | + }) |
| 59 | + .then(result => assert.strictEqual(result, errorOccurred, |
| 60 | + 'Should not have accepted password')); |
| 61 | + })(), |
| 62 | + (function testHashFromPhp() { |
| 63 | + return phpHashMaker() |
| 64 | + .then(pair => sps.PasswordStorage.verifyPassword(pair.password, pair.hash)) |
| 65 | + .then(accepted => assert.strictEqual(accepted, true, |
| 66 | + 'Should have accepted password')); |
| 67 | + })(), |
| 68 | + (function testHashFromPhpFailsWithWrongPassword() { |
| 69 | + const testPassword = crypto.randomBytes(3).toString('hex'); |
| 70 | + return phpHashMaker() |
| 71 | + .then(pair => sps.PasswordStorage.verifyPassword(testPassword, pair.hash)) |
| 72 | + .then(accepted => assert.strictEqual(accepted, false, |
| 73 | + 'Should not have accepted password')); |
| 74 | + })(), |
| 75 | +]) |
| 76 | +.then(results => { |
| 77 | + // Test cases can be disabled by NOT immediately invoking their function |
| 78 | + const testCount = results.filter(x=>typeof x !== 'function').length; |
| 79 | + console.log(`✔ ${testCount} Passed`); |
| 80 | +}) |
| 81 | +.catch(reason => { |
| 82 | + if(reason.name === 'AssertionError') |
| 83 | + console.error('AssertionError:', |
| 84 | + reason.actual, reason.operator, reason.expected); |
| 85 | + |
| 86 | + console.error(reason.stack); |
| 87 | + process.exit(1); |
| 88 | +}); |
| 89 | + |
| 90 | +function phpVerify(password, hash) { |
| 91 | + return new Promise((resolve, reject) => { |
| 92 | + execFile('php', [ 'tests/phpVerify.php', password, hash ], |
| 93 | + (error, stdout, stderr) => { |
| 94 | + if(error) reject(error); |
| 95 | + else resolve(stdout); |
| 96 | + }); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function phpHashMaker(password, hash) { |
| 101 | + return new Promise((resolve, reject) => { |
| 102 | + execFile('php', [ 'tests/phpHashMaker.php' ], |
| 103 | + (error, stdout, stderr) => { |
| 104 | + if(error) reject(error); |
| 105 | + else { |
| 106 | + const hashPair = stdout.trim().split(' '); |
| 107 | + if (hashPair[1].length !== parseInt(hashPair[0], 10)) |
| 108 | + reject(new Error('Unicode test is invalid')); |
| 109 | + else |
| 110 | + resolve({ password: hashPair[1], hash: hashPair[2] }); |
| 111 | + } |
| 112 | + }); |
| 113 | + }); |
| 114 | +} |
0 commit comments