fingerprint_spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. describe("Fingerprint", function(){
  2. beforeEach(function() {
  3. this.addMatchers({
  4. toBeInstanceOf : function(expected) {
  5. return this.actual instanceof expected && this.actual.length > 0;
  6. },
  7. toBeA: function(expected) {
  8. return typeof this.actual === expected;
  9. }
  10. });
  11. });
  12. describe("new Fingerprint", function(){
  13. it("Creates a new instance of Fingerprint", function(){
  14. expect(new Fingerprint()).not.toBeNull();
  15. });
  16. it("Accepts a custom hashing function as argument", function(){
  17. var hasher = function(){return 31;};
  18. expect(new Fingerprint(hasher)).not.toBeNull();
  19. });
  20. it("Accepts a custom hasing function as options argument", function(){
  21. var hasher = function(){return 31;};
  22. expect(new Fingerprint({hasher: hasher})).not.toBeNull();
  23. });
  24. });
  25. describe("#get", function(){
  26. it("Calculates fingerprint with built-in hashing if no custom hashing is given", function(){
  27. var fingerprint = new Fingerprint();
  28. spyOn(fingerprint, 'murmurhash3_32_gc');
  29. fingerprint.get();
  30. expect(fingerprint.murmurhash3_32_gc).toHaveBeenCalled();
  31. });
  32. it("Calculates fingerprint with custom hashing if it is given as an argument", function(){
  33. var hasher = function(){return 'abcdef';};
  34. var fingerprint = new Fingerprint(hasher);
  35. expect(fingerprint.get()).toEqual('abcdef');
  36. });
  37. it("Calculates fingerprint with custom hashing if it is given as an options argument", function(){
  38. var hasher = function(){return 'abcdef';};
  39. var fingerprint = new Fingerprint({hasher: hasher});
  40. expect(fingerprint.get()).toEqual('abcdef');
  41. });
  42. it('Calculates fingerprint with canvas fingerprinting if it is said to do so', function(){
  43. var fp = new Fingerprint({canvas: true});
  44. spyOn(fp, 'getCanvasFingerprint');
  45. fp.get();
  46. expect(fp.getCanvasFingerprint).toHaveBeenCalled();
  47. });
  48. it('Does not try to use canvas fingerprinting when not told to(version 1)', function(){
  49. var fp = new Fingerprint({canvas: false});
  50. spyOn(fp, 'getCanvasFingerprint');
  51. fp.get();
  52. expect(fp.getCanvasFingerprint).not.toHaveBeenCalled();
  53. });
  54. it('Does not try to use canvas fingerprinting when not told to(version 2)', function(){
  55. var fp = new Fingerprint();
  56. spyOn(fp, 'getCanvasFingerprint');
  57. fp.get();
  58. expect(fp.getCanvasFingerprint).not.toHaveBeenCalled();
  59. });
  60. it('Calculates fingerprint with ActiveX fingerprinting if it is said to do so', function(){
  61. var fp = new Fingerprint({ie_activex: true});
  62. spyOn(fp, 'isIE').andReturn(true);
  63. spyOn(fp, 'getIEPluginsString');
  64. fp.get();
  65. expect(fp.getIEPluginsString).toHaveBeenCalled();
  66. });
  67. it('Does not try to use ActiveX fingerprinting when not told to(version 1)', function(){
  68. var fp = new Fingerprint({ie_activex: false});
  69. spyOn(fp, 'getIEPluginsString');
  70. fp.get();
  71. expect(fp.getIEPluginsString).not.toHaveBeenCalled();
  72. });
  73. it('Does not try to use ActiveX fingerprinting when not told to(version 2)', function(){
  74. var fp = new Fingerprint();
  75. spyOn(fp, 'getIEPluginsString');
  76. fp.get();
  77. expect(fp.getIEPluginsString).not.toHaveBeenCalled();
  78. });
  79. it('Calculates fingerprint accessing screen resolution if it is said to do so', function(){
  80. var fp = new Fingerprint({screen_resolution: true});
  81. spyOn(fp, 'getScreenResolution');
  82. fp.get();
  83. expect(fp.getScreenResolution).toHaveBeenCalled();
  84. });
  85. it('Does not try to use screen resolution when not told to', function(){
  86. var fp = new Fingerprint();
  87. spyOn(fp, 'getScreenResolution');
  88. fp.get();
  89. expect(fp.getScreenResolution).not.toHaveBeenCalled();
  90. });
  91. it("Returns a number as a fingerprint value when used with a built-in hashing function", function(){
  92. var fingerprint = new Fingerprint();
  93. expect(fingerprint.get()).toBeA('number');
  94. });
  95. it('Returns a string from getCanvasFingerprint function', function(){
  96. var fp = new Fingerprint({canvas: true});
  97. expect(fp.getCanvasFingerprint()).toBeA('string');
  98. });
  99. it('Does not fail when document.body is null', function(){
  100. var body = document.body.parentNode.removeChild(document.body);
  101. var fingerprint = new Fingerprint();
  102. expect(fingerprint.get()).toBeA('number');
  103. document.body = body;
  104. });
  105. });
  106. });