1 | 'use strict';
|
2 |
|
3 | var assert = require('assert');
|
4 | var crypto = require('crypto');
|
5 | var stream = require('stream');
|
6 | var hash = require('../index');
|
7 | var validSha1 = /^[0-9a-f]{40}$/i;
|
8 |
|
9 | describe('hash() objects with custom class names', function() {
|
10 | var builtinToString;
|
11 | beforeEach(function() {
|
12 | builtinToString = Object.prototype.toString;
|
13 | Object.prototype.toString = function() {
|
14 | if (this && typeof this.__className !== 'undefined') {
|
15 | return this.__className;
|
16 | }
|
17 |
|
18 | return builtinToString.apply(this, arguments);
|
19 | };
|
20 | });
|
21 |
|
22 | afterEach(function() {
|
23 | Object.prototype.toString = builtinToString;
|
24 | });
|
25 |
|
26 | it('should throw when trying to hash an unknown object', function() {
|
27 | assert.throws(function() {
|
28 | hash({a:1, __className: '[object Foo]'});
|
29 | }, /Unknown object type "foo"/);
|
30 |
|
31 | assert.throws(function() {
|
32 | hash({a:1, __className: 'Foo'});
|
33 | }, /Unknown object type/);
|
34 | });
|
35 |
|
36 | it('should not throw when trying to hash an unknown object with ignoreUnknown', function() {
|
37 | var opt = {ignoreUnknown: true};
|
38 |
|
39 | assert.ok(validSha1.test(hash({a:1, __className: '[object Foo]'}, opt)));
|
40 | });
|
41 |
|
42 | it('should not throw when trying to hash a weirdly-named object with ignoreUnknown', function() {
|
43 | var opt = {ignoreUnknown: true};
|
44 |
|
45 | assert.ok(validSha1.test(hash({a:1, __className: 'Foo'}, opt)));
|
46 | });
|
47 |
|
48 | it('should not delve further into a number of native types', function() {
|
49 | var nativeTypes = [
|
50 | 'domwindow',
|
51 | 'process', 'timer', 'pipe', 'tcp', 'udp', 'tty', 'statwatcher',
|
52 | 'securecontext', 'connection', 'zlib', 'context', 'nodescript',
|
53 | 'httpparser', 'dataview', 'signal', 'fsevent', 'tlswrap'
|
54 | ];
|
55 |
|
56 | for (var i = 0; i < nativeTypes.length; i++) {
|
57 | var obj = { foobar: 1, __className: '[object ' + nativeTypes[i] + ']' };
|
58 | var serialized = hash(obj, { algorithm: 'passthrough', encoding: 'utf8' });
|
59 | assert.strictEqual(serialized, nativeTypes[i]);
|
60 | }
|
61 | });
|
62 |
|
63 | it('should hash xml based on its string representation', function() {
|
64 | var obj = {
|
65 | __className: '[object xml]',
|
66 | toString: function() { return 'Bananä' }
|
67 | };
|
68 |
|
69 | var serialized = hash(obj, { algorithm: 'passthrough', encoding: 'utf8' });
|
70 | assert.strictEqual(serialized, 'xml:Bananä');
|
71 | });
|
72 |
|
73 | it('should hash URLs based on its string representation', function() {
|
74 | var obj = {
|
75 | __className: '[object url]',
|
76 | toString: function() { return 'https://example.com/' }
|
77 | };
|
78 |
|
79 | var serialized = hash(obj, { algorithm: 'passthrough', encoding: 'utf8' });
|
80 | assert.strictEqual(serialized, 'url:https://example.com/');
|
81 | });
|
82 |
|
83 | it('should not hash blobs without ignoreUnknown', function() {
|
84 | var obj = {
|
85 | __className: '[object blob]'
|
86 | };
|
87 |
|
88 | assert.throws(function() {
|
89 | hash(obj);
|
90 | }, /not supported/);
|
91 | });
|
92 |
|
93 | it('should ignore blobs with ignoreUnknown', function() {
|
94 | var obj = {
|
95 | __className: '[object blob]'
|
96 | };
|
97 |
|
98 | var serialized = hash(obj, {
|
99 | algorithm: 'passthrough',
|
100 | encoding: 'utf8',
|
101 | ignoreUnknown: true
|
102 | });
|
103 |
|
104 | assert.strictEqual(serialized, '[blob]');
|
105 | });
|
106 | });
|