| [81bc7da] | 1 | var path = require("path"),
|
|---|
| 2 | fs = require("fs"),
|
|---|
| 3 | binding = require("bcrypt"),
|
|---|
| 4 | bcrypt = require(path.join(__dirname, '..', 'index.js'))/*,
|
|---|
| 5 | isaac = eval(
|
|---|
| 6 | fs.readFileSync(path.join(__dirname, "..", "src", "bcrypt", "prng", "accum.js"))+
|
|---|
| 7 | fs.readFileSync(path.join(__dirname, "..", "src", "bcrypt", "prng", "isaac.js"))+
|
|---|
| 8 | " accum.start();"+
|
|---|
| 9 | " isaac"
|
|---|
| 10 | )*/;
|
|---|
| 11 |
|
|---|
| 12 | module.exports = {
|
|---|
| 13 |
|
|---|
| 14 | "encodeBase64": function(test) {
|
|---|
| 15 | var str = bcrypt.encodeBase64([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10], 16);
|
|---|
| 16 | test.strictEqual(str, "..CA.uOD/eaGAOmJB.yMBu");
|
|---|
| 17 | test.done();
|
|---|
| 18 | },
|
|---|
| 19 |
|
|---|
| 20 | "decodeBase64": function(test) {
|
|---|
| 21 | var bytes = bcrypt.decodeBase64("..CA.uOD/eaGAOmJB.yMBv.", 16);
|
|---|
| 22 | test.deepEqual(bytes, [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]);
|
|---|
| 23 | test.done();
|
|---|
| 24 | },
|
|---|
| 25 |
|
|---|
| 26 | "genSaltSync": function(test) {
|
|---|
| 27 | var salt = bcrypt.genSaltSync(10);
|
|---|
| 28 | test.ok(salt);
|
|---|
| 29 | test.ok(typeof salt == 'string');
|
|---|
| 30 | test.ok(salt.length > 0);
|
|---|
| 31 | test.done();
|
|---|
| 32 | },
|
|---|
| 33 |
|
|---|
| 34 | "genSalt": function(test) {
|
|---|
| 35 | bcrypt.genSalt(10, function(err, salt) {
|
|---|
| 36 | test.ok(salt);
|
|---|
| 37 | test.ok(typeof salt == 'string');
|
|---|
| 38 | test.ok(salt.length > 0);
|
|---|
| 39 | test.done();
|
|---|
| 40 | });
|
|---|
| 41 | },
|
|---|
| 42 |
|
|---|
| 43 | "hashSync": function(test) {
|
|---|
| 44 | test.doesNotThrow(function() {
|
|---|
| 45 | bcrypt.hashSync("hello", 10);
|
|---|
| 46 | });
|
|---|
| 47 | test.notEqual(bcrypt.hashSync("hello", 10), bcrypt.hashSync("hello", 10));
|
|---|
| 48 | test.done();
|
|---|
| 49 | },
|
|---|
| 50 |
|
|---|
| 51 | "hash": function(test) {
|
|---|
| 52 | bcrypt.hash("hello", 10, function(err, hash) {
|
|---|
| 53 | test.notOk(err);
|
|---|
| 54 | test.ok(hash);
|
|---|
| 55 | test.done();
|
|---|
| 56 | });
|
|---|
| 57 | },
|
|---|
| 58 |
|
|---|
| 59 | "compareSync": function(test) {
|
|---|
| 60 | var salt1 = bcrypt.genSaltSync(),
|
|---|
| 61 | hash1 = bcrypt.hashSync("hello", salt1); // $2a$
|
|---|
| 62 | var salt2 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2y$"),
|
|---|
| 63 | hash2 = bcrypt.hashSync("world", salt2);
|
|---|
| 64 | var salt3 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2b$"),
|
|---|
| 65 | hash3 = bcrypt.hashSync("hello world", salt3);
|
|---|
| 66 |
|
|---|
| 67 | test.strictEqual(hash1.substring(0,4), "$2a$");
|
|---|
| 68 | test.ok(bcrypt.compareSync("hello", hash1));
|
|---|
| 69 | test.notOk(bcrypt.compareSync("hello", hash2));
|
|---|
| 70 | test.notOk(bcrypt.compareSync("hello", hash3));
|
|---|
| 71 |
|
|---|
| 72 | test.strictEqual(hash2.substring(0,4), "$2y$");
|
|---|
| 73 | test.ok(bcrypt.compareSync("world", hash2));
|
|---|
| 74 | test.notOk(bcrypt.compareSync("world", hash1));
|
|---|
| 75 | test.notOk(bcrypt.compareSync("world", hash3));
|
|---|
| 76 |
|
|---|
| 77 | test.strictEqual(hash3.substring(0,4), "$2b$");
|
|---|
| 78 | test.ok(bcrypt.compareSync("hello world", hash3));
|
|---|
| 79 | test.notOk(bcrypt.compareSync("hello world", hash1));
|
|---|
| 80 | test.notOk(bcrypt.compareSync("hello world", hash2));
|
|---|
| 81 |
|
|---|
| 82 | test.done();
|
|---|
| 83 | },
|
|---|
| 84 |
|
|---|
| 85 | "compare": function(test) {
|
|---|
| 86 | var salt1 = bcrypt.genSaltSync(),
|
|---|
| 87 | hash1 = bcrypt.hashSync("hello", salt1); // $2a$
|
|---|
| 88 | var salt2 = bcrypt.genSaltSync();
|
|---|
| 89 | salt2 = salt2.substring(0,2)+'y'+salt2.substring(3); // $2y$
|
|---|
| 90 | var hash2 = bcrypt.hashSync("world", salt2);
|
|---|
| 91 | bcrypt.compare("hello", hash1, function(err, same) {
|
|---|
| 92 | test.notOk(err);
|
|---|
| 93 | test.ok(same);
|
|---|
| 94 | bcrypt.compare("hello", hash2, function(err, same) {
|
|---|
| 95 | test.notOk(err);
|
|---|
| 96 | test.notOk(same);
|
|---|
| 97 | bcrypt.compare("world", hash2, function(err, same) {
|
|---|
| 98 | test.notOk(err);
|
|---|
| 99 | test.ok(same);
|
|---|
| 100 | bcrypt.compare("world", hash1, function(err, same) {
|
|---|
| 101 | test.notOk(err);
|
|---|
| 102 | test.notOk(same);
|
|---|
| 103 | test.done();
|
|---|
| 104 | });
|
|---|
| 105 | });
|
|---|
| 106 | });
|
|---|
| 107 | });
|
|---|
| 108 | },
|
|---|
| 109 |
|
|---|
| 110 | "getSalt": function(test) {
|
|---|
| 111 | var hash1 = bcrypt.hashSync("hello", bcrypt.genSaltSync());
|
|---|
| 112 | var salt = bcrypt.getSalt(hash1);
|
|---|
| 113 | var hash2 = bcrypt.hashSync("hello", salt);
|
|---|
| 114 | test.equal(hash1, hash2);
|
|---|
| 115 | test.done();
|
|---|
| 116 | },
|
|---|
| 117 |
|
|---|
| 118 | "getRounds": function(test) {
|
|---|
| 119 | var hash1 = bcrypt.hashSync("hello", bcrypt.genSaltSync());
|
|---|
| 120 | test.equal(bcrypt.getRounds(hash1), 10);
|
|---|
| 121 | test.done();
|
|---|
| 122 | },
|
|---|
| 123 |
|
|---|
| 124 | "progress": function(test) {
|
|---|
| 125 | bcrypt.genSalt(12, function(err, salt) {
|
|---|
| 126 | test.ok(!err);
|
|---|
| 127 | var progress = [];
|
|---|
| 128 | bcrypt.hash("hello world", salt, function(err, hash) {
|
|---|
| 129 | test.ok(!err);
|
|---|
| 130 | test.ok(typeof hash === 'string');
|
|---|
| 131 | test.ok(progress.length >= 2);
|
|---|
| 132 | test.strictEqual(progress[0], 0);
|
|---|
| 133 | test.strictEqual(progress[progress.length-1], 1);
|
|---|
| 134 | test.done();
|
|---|
| 135 | }, function(n) {
|
|---|
| 136 | progress.push(n);
|
|---|
| 137 | });
|
|---|
| 138 | });
|
|---|
| 139 | },
|
|---|
| 140 |
|
|---|
| 141 | "promise": function(test) {
|
|---|
| 142 | bcrypt.genSalt(10)
|
|---|
| 143 | .then(function(salt) {
|
|---|
| 144 | bcrypt.hash("hello", salt)
|
|---|
| 145 | .then(function(hash) {
|
|---|
| 146 | test.ok(hash);
|
|---|
| 147 | bcrypt.compare("hello", hash)
|
|---|
| 148 | .then(function(result) {
|
|---|
| 149 | test.ok(result);
|
|---|
| 150 | bcrypt.genSalt(/* no args */)
|
|---|
| 151 | .then(function(salt) {
|
|---|
| 152 | test.ok(salt);
|
|---|
| 153 | test.done();
|
|---|
| 154 | }, function(err) {
|
|---|
| 155 | test.fail(err, null, "promise rejected");
|
|---|
| 156 | });
|
|---|
| 157 | }, function(err) {
|
|---|
| 158 | test.fail(err, null, "promise rejected");
|
|---|
| 159 | });
|
|---|
| 160 | }, function(err) {
|
|---|
| 161 | test.fail(err, null, 'promise rejected');
|
|---|
| 162 | });
|
|---|
| 163 | }, function(err) {
|
|---|
| 164 | test.fail(err, null, "promise rejected");
|
|---|
| 165 | });
|
|---|
| 166 | },
|
|---|
| 167 |
|
|---|
| 168 | "compat": {
|
|---|
| 169 | "quickbrown": function(test) {
|
|---|
| 170 | var pass = fs.readFileSync(path.join(__dirname, "quickbrown.txt"))+"",
|
|---|
| 171 | salt = bcrypt.genSaltSync(),
|
|---|
| 172 | hash1 = binding.hashSync(pass, salt),
|
|---|
| 173 | hash2 = bcrypt.hashSync(pass, salt);
|
|---|
| 174 | test.equal(hash1, hash2);
|
|---|
| 175 | test.done();
|
|---|
| 176 | },
|
|---|
| 177 |
|
|---|
| 178 | "roundsOOB": function(test) {
|
|---|
| 179 | var salt1 = bcrypt.genSaltSync(0), // $10$ like not set
|
|---|
| 180 | salt2 = binding.genSaltSync(0);
|
|---|
| 181 | test.strictEqual(salt1.substring(0, 7), "$2a$10$");
|
|---|
| 182 | test.strictEqual(salt2.substring(0, 7), "$2a$10$");
|
|---|
| 183 |
|
|---|
| 184 | salt1 = bcrypt.genSaltSync(3); // $04$ is lower cap
|
|---|
| 185 | salt2 = bcrypt.genSaltSync(3);
|
|---|
| 186 | test.strictEqual(salt1.substring(0, 7), "$2a$04$");
|
|---|
| 187 | test.strictEqual(salt2.substring(0, 7), "$2a$04$");
|
|---|
| 188 |
|
|---|
| 189 | salt1 = bcrypt.genSaltSync(32); // $31$ is upper cap
|
|---|
| 190 | salt2 = bcrypt.genSaltSync(32);
|
|---|
| 191 | test.strictEqual(salt1.substring(0, 7), "$2a$31$");
|
|---|
| 192 | test.strictEqual(salt2.substring(0, 7), "$2a$31$");
|
|---|
| 193 |
|
|---|
| 194 | test.done();
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | };
|
|---|