-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
100 lines (74 loc) · 5.04 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// very naive testing
var assert = require("assert"),
easyPbkdf2 = require("./easy-pbkdf2")( {"DEFAULT_HASH_ITERATIONS": 256, "SALT_SIZE": 128, "KEY_LENGTH": 1024, "MAX_PASSWORD_LENGTH": 2048} );
assert.strictEqual( easyPbkdf2.DEFAULT_HASH_ITERATIONS, 256, "DEFAULT_HASH_ITERATIONS set correctly");
assert.strictEqual( easyPbkdf2.SALT_SIZE, 128, "SALT_SIZE set correctly");
assert.strictEqual( easyPbkdf2.KEY_LENGTH, 1024, "KEY_LENGTH set correctly");
assert.strictEqual( easyPbkdf2.MAX_PASSWORD_LENGTH, 2048, "MAX_PASSWORD_LENGTH set correctly");
var salt = easyPbkdf2.generateSalt();
assert.ok( salt && salt.length > 0, "Sync salt created");
easyPbkdf2.generateSalt( function( salt ){
assert.ok( salt && salt.length > 0, "Async salt created");
assert.throws(function(){
easyPbkdf2.generateSalt("notanumber");
}, /explicitIterations must be an integer/, "generateSalt w/out a number throws");
assert.throws(function(){
easyPbkdf2.generateSalt( easyPbkdf2.DEFAULT_HASH_ITERATIONS - 1 )
}, /explicitIterations cannot be less than \d+/, "generateSalt w/ an invalid number throws");
var weakHash = easyPbkdf2.weakHash();
assert.ok(weakHash && weakHash.length, "weakHash works as expected.");
assert.strictEqual(easyPbkdf2.weakHash(["value"]), easyPbkdf2.weakHash(["value"]), "Generated weakHashes created with same seed value are identical");
var password = "password";
easyPbkdf2.secureHash( password, function( err, hashed, salt ) {
assert.ok( hashed && hashed.length > 0, "Hash created");
assert.ok( salt && salt.length > 0, "Salt created");
easyPbkdf2.verify( salt, hashed, password, function( err, valid ) {
assert.strictEqual( valid, true, "verify returns correct result for matching data" );
});
easyPbkdf2.verify( salt, hashed, "not the password", function( err, valid ) {
assert.strictEqual( valid, false, "Verify returns correct result for mismatched data" );
});
easyPbkdf2.secureHash( password, salt, function( err, secondHashed, _salt ){
assert.strictEqual( secondHashed, hashed, "Hashing with identical salt and password works as expected" );
assert.strictEqual( _salt, salt, "Salt did not change");
easyPbkdf2.secureHash( [], function(err){
assert.ok(err instanceof Error, "invalid value emits error");
assert.throws( function(){
easyPbkdf2.secureHash( "pass", "salt" );
}, Error, "Missing callback throws");
assert.throws( function(){
easyPbkdf2.secureHash( "pass", "salt", {} );
}, Error, "invliad callback throws");
assert.ok(err instanceof Error, "invalid value emits error");
var randomBytes = easyPbkdf2.random( 10 );
assert.equal( randomBytes.length, 10, "Sync random returns correct randomBytes" );
easyPbkdf2.random( 10, function( randomBytes ){
assert.equal( randomBytes.length, 10, "Async random returns correct randomBytes" );
easyPbkdf2.MAX_PASSWORD_LENGTH = 7
easyPbkdf2.secureHash( "password", "salt", function(err){
assert.ok( err instanceof Error, "Hashing a too-long password emits error" );
assert.equal( err.message, "Password exceeds maximum length of 7", "Hashing a too-long password has correct error message" );
easyPbkdf2.MAX_PASSWORD_LENGTH = 8
easyPbkdf2.secureHash( "password", "salt", function(err){
assert.ok( !err, "No error when password length is at the limit" );
assert.ok( hashed && hashed.length > 0, "Hash created when password is at the length limit");
assert.ok( salt && salt.length > 0, "Salt created when password is at the length limit");
easyPbkdf2.MAX_PASSWORD_LENGTH = 7
easyPbkdf2.verify( salt, hashed, password, function( err, valid ) {
assert.ok( err instanceof Error, "Verifying a too-long password emits error" );
assert.equal( err.message, "Password exceeds maximum length of 7", "Verifying a too-long password has correct error message" );
assert.ok( !valid, "Verify returns correct result for too-long password");
easyPbkdf2.MAX_PASSWORD_LENGTH = 8
easyPbkdf2.verify( salt, hashed, password, function( err, valid ) {
assert.ok( !err, "No error when verifying password at the limit" );
assert.ok( valid, "Verify returns correct result for password at the length limit");
console.log("done. all tests passes");
});
});
});
});
});
});
});
});
});