index.js 9.5 KB
Newer Older
YazhouChen's avatar
YazhouChen committed
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
'use strict';

var assert = require('assert');
var crypto = require('crypto');
var hash = require('../index');
var validSha1 = /^[0-9a-f]{40}$/i;

describe('hash', function() {
  it('throws when nothing to hash', function () {
    assert.throws(hash, 'no arguments');
    assert.throws(function() {
      hash(undefined, {algorithm: 'md5'});
    }, 'undefined');
  });

  it('throws when passed an invalid options', function() {
    assert.throws(function() {
      hash({foo: 'bar'}, {algorithm: 'shalala'});
    }, 'bad algorithm');
    assert.throws(function() {
      hash({foo: 'bar'}, {encoding: 'base16'});
    }, 'bad encoding');
  });

  it('hashes a simple object', function() {
    assert.ok(validSha1.test(hash({foo: 'bar', bar: 'baz'})), 'hash object');
  });

  if (typeof Buffer !== 'undefined') {
    it('can return buffers', function() {
      assert.ok(Buffer.isBuffer(hash({foo: 'bar', bar: 'baz'}, {encoding: 'buffer'})), 'hash object');
    });
  }

  it('hashes identical objects with different key ordering', function() {
    var hash1 = hash({foo: 'bar', bar: 'baz'});
    var hash2 = hash({bar: 'baz', foo: 'bar'});
    var hash3 = hash({bar: 'foo', foo: 'baz'});
    assert.equal(hash1, hash2, 'hashes are equal');
    assert.notEqual(hash1, hash3, 'different objects not equal');
  });

  it('respects object key ordering when unorderedObjects = false', function() {
    var hash1 = hash({foo: 'bar', bar: 'baz'}, { unorderedObjects: false });
    var hash2 = hash({bar: 'baz', foo: 'bar'}, { unorderedObjects: false });
    assert.notEqual(hash1, hash2, 'hashes are not equal');
  });

  it('hashes only object keys when excludeValues option is set', function() {
    var hash1 = hash({foo: false, bar: 'OK'}, { excludeValues: true });
    var hash2 = hash({foo: true, bar: 'NO'}, { excludeValues: true });
    var hash3 = hash({foo: true, bar: 'OK', baz: false}, { excludeValues: true });
    assert.equal(hash1, hash2, 'values not in hash digest');
    assert.notEqual(hash1, hash3, 'different keys not equal');
  });

  it('array values are hashed', function() {
    var hash1 = hash({foo: ['bar', 'baz'], bax: true });
    var hash2 = hash({foo: ['baz', 'bar'], bax: true });
    assert.notEqual(hash1, hash2, 'different array orders are unique');
  });

  it('nested object values are hashed', function() {
    var hash1 = hash({foo: {bar: true, bax: 1}});
    var hash2 = hash({foo: {bar: true, bax: 1}});
    var hash3 = hash({foo: {bar: false, bax: 1}});
    assert.equal(hash1, hash2, 'hashes are equal');
    assert.notEqual(hash1, hash3, 'different objects not equal');
  });

  it('sugar methods should be equivalent', function() {
    var obj = {foo: 'bar', baz: true};
    assert.equal(hash.keys(obj), hash(obj, {excludeValues: true}), 'keys');
    assert.equal(hash.sha1(obj), hash(obj, {algorithm: 'sha1'}), 'sha1');
    assert.equal(hash.MD5(obj), hash(obj, {algorithm: 'md5'}), 'md5');
    assert.equal(hash.keysMD5(obj),
      hash(obj, {algorithm: 'md5', excludeValues: true}), 'keys md5');
  });

  it('array of nested object values are hashed', function() {
    var hash1 = hash({foo: [ {bar: true, bax: 1}, {bar: false, bax: 2} ] });
    var hash2 = hash({foo: [ {bar: true, bax: 1}, {bar: false, bax: 2} ] });
    var hash3 = hash({foo: [ {bar: false, bax: 2} ] });
    assert.equal(hash1, hash2, 'hashes are equal');
    assert.notEqual(hash1, hash3, 'different objects not equal');
  });

  it("recursive objects don't blow up stack", function() {
    var hash1 = {foo: 'bar'};
    hash1.recursive = hash1;
    assert.doesNotThrow(function() {hash(hash1);}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception');
  });

  it("recursive arrays don't blow up stack", function() {
    var hash1 = ['foo', 'bar'];
    hash1.push(hash1);
    assert.doesNotThrow(function() {hash(hash1);}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception');
  });

  it("recursive arrays don't blow up stack with unorderedArrays", function() {
    var hash1 = ['foo', 'bar'];
    hash1.push(hash1);
    assert.doesNotThrow(function() {hash(hash1, {unorderedArrays: true});}, /Maximum call stack size exceeded/, 'Should not throw an stack size exceeded exception');
  });

  it("recursive handling tracks identity", function() {
    var hash1 = {k1: {k: 'v'}, k2: {k: 'k2'}};
    hash1.k1.r1 = hash1.k1;
    hash1.k2.r2 = hash1.k2;
    var hash2 = {k1: {k: 'v'}, k2: {k: 'k2'}};
    hash2.k1.r1 = hash2.k2;
    hash2.k2.r2 = hash2.k1;
    assert.notEqual(hash(hash1), hash(hash2), "order of recursive objects should matter");
  });

  it("object types are hashed", function() {
    var hash1 = hash({foo: 'bar'});
    var hash2 = hash(['foo', 'bar']);
    assert.notEqual(hash1, hash2, "arrays and objects should not produce identical hashes");
  });

  it("utf8 strings are hashed correctly", function() {
    var hash1 = hash('\u03c3'); // cf 83 in utf8
    var hash2 = hash('\u01c3'); // c7 83 in utf8
    assert.notEqual(hash1, hash2, "different strings with similar utf8 encodings should produce different hashes");
  });

  it("strings passed as new String are hashed correctly", function() {
    var hash1 = hash(new String('foo'));
    assert.equal(hash1, '7cd3edacc4c9dd43908177508c112608a398bb9a');
    var hash2 = hash({foo: new String('bar')});
    assert.equal(hash2, 'a75c05bdca7d704bdfcd761913e5a4e4636e956b');
  });

  it("various hashes in crypto.getHashes() should be supported", function() {
    var hashes = ['sha1', 'md5'];

    if (crypto.getHashes) {
      // take all hashes from crypto.getHashes() starting with MD or SHA
      hashes = crypto.getHashes().filter(RegExp.prototype.test.bind(/^(md|sha)/i));
    }

    var obj = {randomText: 'bananas'};

    for (var i = 0; i < hashes.length; i++) {
      assert.ok(hash(obj, {algorithm: hashes[i]}), 'Algorithm ' + hashes[i] + ' should be supported');
    }
  });

  it("null and 'Null' string produce different hashes", function() {
    var hash1 = hash({foo: null});
    var hash2 = hash({foo: 'Null'});
    assert.notEqual(hash1, hash2, "null and 'Null' should not produce identical hashes");
  });

  it('Distinguish functions based on their properties', function() {

    var a, b, c, d;
    function Foo() {}
    a = hash(Foo);

    Foo.foo = 22;
    b = hash(Foo);

    Foo.bar = "42";
    c = hash(Foo);

    Foo.foo = "22";
    d = hash(Foo);

    assert.notEqual(a,b, 'adding a property changes the hash');
    assert.notEqual(b,c, 'adding another property changes the hash');
    assert.notEqual(c,d, 'changing a property changes the hash');
  });

  it('respectFunctionProperties = false', function() {

    var a, b;
    function Foo() {}
    a = hash(Foo, {respectFunctionProperties: false});

    Foo.foo = 22;
    b = hash(Foo, {respectFunctionProperties: false});

    assert.equal(a,b, 'function properties are ignored');
  });

  it('Distinguish functions based on prototype properties', function() {

    var a, b, c, d;
    function Foo() {}
    a = hash(Foo);

    Foo.prototype.foo = 22;
    b = hash(Foo);

    Foo.prototype.bar = "42";
    c = hash(Foo);

    Foo.prototype.foo = "22";
    d = hash(Foo);

    assert.notEqual(a,b, 'adding a property to the prototype changes the hash');
    assert.notEqual(b,c, 'adding another property to the prototype changes the hash');
    assert.notEqual(c,d, 'changing a property in the prototype changes the hash');
  });

  it('Distinguish objects based on their type', function() {

    function Foo() {}
    function Bar() {}

    var f = new Foo(), b = new Bar();

    assert.notEqual(hash(Foo), hash(Bar), 'Functions with different names should produce a different Hash.');
    assert.notEqual(hash(f), hash(b), 'Objects with different constructor should have a different Hash.');
  });

  it('respectType = false', function() {
    var opt = { respectType: false };


    function Foo() {}
    function Bar() {}

    var f = new Foo(), b = new Bar();
    assert.equal(hash(f, opt), hash(b, opt), 'Hashing should disregard the different constructor');


    var ha, hb;
    function F() {}
    ha = hash(F, opt);

    F.prototype.meaningOfLife = 42;
    hb = hash(F, opt);

    assert.equal(ha, hb, 'Hashing should disregard changes in the function\'s prototype');
  });

  it('unorderedArrays = false', function() {
    var ha, hb;
    ha = hash([1, 2, 3]);
    hb = hash([3, 2, 1]);

    assert.notEqual(ha, hb, 'Hashing should respect the order of array entries');
  });

  it('unorderedArrays = true', function() {
    var opt = { unorderedArrays: true };

    var ha, hb;
    ha = hash([1, 2, 3], opt);
    hb = hash([3, 2, 1], opt);

    assert.equal(ha, hb, 'Hashing should not respect the order of array entries');

    ha = hash([{a: 1}, {a: 2}], opt);
    hb = hash([{a: 2}, {a: 1}], opt);

    assert.equal(ha, hb, 'Hashing should not respect the order of array entries');
  });

  it('excludeKeys works', function() {
    var ha, hb;
    ha = hash({a: 1, b: 4}, { excludeKeys: function(key) { return key === 'b' } });
    hb = hash({a: 1});

    assert.equal(ha, hb, 'Hashing should ignore key `b`');
  });

  if (typeof Set !== 'undefined') {
    it('unorderedSets = false', function() {
      var opt = { unorderedSets: false };

      var ha, hb;
      ha = hash(new Set([1, 2, 3]), opt);
      hb = hash(new Set([3, 2, 1]), opt);

      assert.notEqual(ha, hb, 'Hashing should respect the order of Set entries');
    });

    it('unorderedSets = true', function() {
      var ha, hb;
      ha = hash(new Set([1, 2, 3]));
      hb = hash(new Set([3, 2, 1]));

      assert.equal(ha, hb, 'Hashing should not respect the order of Set entries');
    });
  }
});