-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
149 lines (121 loc) · 4.22 KB
/
test.js
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
#!/usr/bin/env node
var should = require('should');
var fs = require('fs');
var Adler32 = require('./');
var mhash = require('mhash');
var rand = require('random-buffer');
//var buf = fs.readFileSync(__filename);
var buf = rand(32768, 'adler32');
describe('Adler32', function() {
describe('.sum(buf, [adler = 1])', function() {
var sum = Adler32.sum(buf);
it('should return a number above 0 and below 4293984241', function() {
sum.should.be.above(0);
sum.should.be.below(4293984241);
});
it('should match the sum calculated by mhash', function() {
sum.should.be.exactly(parseInt(mhash('adler32', buf), 16));
});
it('should be the same if done as a running sum', function() {
var half = (buf.length / 2)<<0;
var part0 = buf.slice(0, half);
var part1 = buf.slice(half);
var partial = Adler32.sum(part0);
var sum0 = Adler32.sum(buf);
var sum1 = Adler32.sum(part1, partial);
sum1.should.be.exactly(sum0);
});
});
describe('.roll(sum, length, oldByte, [newByte = null])', function() {
it('should result in the same value as sum for each offset chunk of size 64', function() {
rollTest(64);
});
it('should result in the same value as sum for each offset chunk of size 128', function() {
rollTest(128);
});
it('should result in the same value as sum for each offset chunk of size 256', function() {
rollTest(256);
});
it('should result in the same value as sum for each offset chunk of size 512', function() {
rollTest(512);
});
it('should result in the same value as sum for each offset chunk of size 1024', function() {
rollTest(1024);
});
it('should result in the same value as sum for each offset chunk of size 2048', function() {
rollTest(2048);
});
it('should result in the same value as sum for each offset chunk of size 4096', function() {
rollTest(4096);
});
it('should result in the same value as sum for each offset chunk of size 8192', function() {
rollTest(8192);
});
it('should result in the same value as sum for each offset chunk of size 16384', function() {
rollTest(16384);
});
});
describe('.Hash', function () {
it('should hash a string', function() {
// Example taken from http://en.wikipedia.org/wiki/Adler-32.
var hash = new Adler32.Hash();
hash.update('Wikipedia');
hash.digest('hex').toLowerCase().should.be.exactly('11e60398');
});
it('should hash a string in parts', function() {
var hash = new Adler32.Hash();
hash.update('Wiki');
hash.update('pedia');
hash.digest('hex').toLowerCase().should.be.exactly('11e60398');
});
it('should work as a Transform stream', function() {
var hash = new Adler32.Hash({encoding: 'hex'});
hash.write('Wiki');
hash.write('pedia');
hash.end();
hash.read().toLowerCase().should.be.exactly('11e60398');
});
it('should throw a TypeError if update() is called after digest()', function() {
var hash = new Adler32.Hash();
hash.update('Wikipedia');
hash.digest('hex');
(function() {
hash.update('Moar!');
}).should.throw(TypeError);
});
it('should throw an Error if digest() is called more than once', function() {
var hash = new Adler32.Hash();
hash.digest('hex');
(function() {
hash.digest('hex');
}).should.throw(Error);
});
});
describe('.register()', function () {
Adler32.register();
var crypto = require('crypto');
it('should make it so crypto.getHashes() contains adler32', function () {
crypto.getHashes().indexOf('adler32').should.not.equal(-1);
});
it('should make it so crypto.createHash(algorithm) works for adler32', function () {
var hash = crypto.createHash('adler32');
should(hash).be.an.instanceOf(Adler32.Hash);
hash.update('Wikipedia');
hash.digest('hex').toLowerCase().should.be.exactly('11e60398');
});
it('should not remove other crypto hash algorithms', function () {
crypto.getHashes().indexOf('sha256').should.not.equal(-1);
should.exist(crypto.createHash('sha256'));
});
});
});
function rollTest(chunkSize)
{
var sum = Adler32.sum(buf.slice(0, chunkSize));
var i = 0;
for (; i < buf.length; ++i)
{
sum = Adler32.roll(sum, Math.min(chunkSize, buf.length - i), buf[i], buf[i + chunkSize]);
sum.should.be.exactly(Adler32.sum(buf.slice(i + 1, i + 1 + chunkSize)));
}
}