object-classes.js 3.1 KB

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