index.js 1.8 KB

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