| 1 |
'use strict'; |
| 2 |
|
| 3 |
var assert = require('assert'); |
| 4 |
var timeSafeCompare = require('../../lib/index'); |
| 5 |
|
| 6 |
process.on('error', function (e) { |
| 7 |
console.log('caught: ' + e); |
| 8 |
}); |
| 9 |
|
| 10 |
function testEqual(a, b) { |
| 11 |
assert(timeSafeCompare(a, b)); |
| 12 |
|
| 13 |
// lets also do a parity check with the strict equal to operator |
| 14 |
assert(a === b); |
| 15 |
} |
| 16 |
|
| 17 |
function testNotEqual(a, b) { |
| 18 |
assert(!timeSafeCompare(a, b)); |
| 19 |
|
| 20 |
// lets also do a parity check with the strict not equal to operator |
| 21 |
assert(a !== b); |
| 22 |
} |
| 23 |
|
| 24 |
// note: lets also make sure tsscmp can be inline replaced for any types - |
| 25 |
// just incase if anyone is interested |
| 26 |
|
| 27 |
// positive tests |
| 28 |
testEqual('127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', |
| 29 |
'127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', |
| 30 |
'test '); |
| 31 |
testEqual('a', 'a'); |
| 32 |
testEqual('', ''); |
| 33 |
testEqual(undefined, undefined); |
| 34 |
testEqual(true, true); |
| 35 |
testEqual(false, false); |
| 36 |
(function () { |
| 37 |
var a = { a: 1 }; |
| 38 |
testEqual(a, a); |
| 39 |
})(); |
| 40 |
(function () { |
| 41 |
function f1() { return 1; }; |
| 42 |
testEqual(f1, f1); |
| 43 |
})(); |
| 44 |
|
| 45 |
// negative tests |
| 46 |
testNotEqual(''); |
| 47 |
testNotEqual('a', 'b'); |
| 48 |
testNotEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); |
| 49 |
testNotEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a'); |
| 50 |
testNotEqual('alpha', 'beta'); |
| 51 |
testNotEqual(false, true); |
| 52 |
testNotEqual(false, undefined); |
| 53 |
testNotEqual(function () { }, function () { }); |
| 54 |
testNotEqual({}, {}); |
| 55 |
testNotEqual({ a: 1 }, { a: 1 }); |
| 56 |
testNotEqual({ a: 1 }, { a: 2 }); |
| 57 |
testNotEqual([1, 2], [1, 2]); |
| 58 |
testNotEqual([1, 2], [1, 2, 3]); |
| 59 |
(function () { |
| 60 |
var a = { p: 1 }; |
| 61 |
var b = { p: 1 }; |
| 62 |
testNotEqual(a, b); |
| 63 |
})(); |
| 64 |
(function () { |
| 65 |
function f1() { return 1; }; |
| 66 |
function f2() { return 1; }; |
| 67 |
testNotEqual(f1, f2); |
| 68 |
})(); |
| 69 |
console.log('Success: all tests complete.'); |