-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathtest-counter.js
75 lines (60 loc) · 3.51 KB
/
test-counter.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
var nodeunit = require('nodeunit');
var aes = require('../index');
function bufferEquals(a, b) {
if (a.length != b.length) { return false; }
for (var i = 0; i < a.length; i++) {
if (a[i] != b[i]) { return false; }
}
return true;
}
function makeTest (options) {
return function(test) {
var result = Buffer.from(options.incrementResult, 'hex');
if (options.hasOwnProperty('nullish')) {
var counter = new aes.Counter(options.nullish);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to initialize with a nullish thing")
}
if (options.hasOwnProperty('number')) {
var counter = new aes.Counter(options.number);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to initialize with a number")
counter.setValue(options.number);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to reset to a number")
counter = new aes.Counter();
counter.setValue(options.number);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to reset to a number")
}
if (options.bytes) {
var bytes = Buffer.from(options.bytes, 'hex');
var counter = new aes.Counter(bytes);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to initialize with bytes")
counter.setBytes(bytes);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to reset with bytes")
counter = new aes.Counter();
counter.setBytes(bytes);
counter.increment();
test.ok(bufferEquals(counter._counter, result), "counter failed to reset with bytes")
}
test.done();
};
}
module.exports = {
'test-counter-nullish-null': makeTest({nullish: null, incrementResult: "00000000000000000000000000000002"}),
'test-counter-nullish-undefined': makeTest({nullish: undefined, incrementResult: "00000000000000000000000000000002"}),
'test-counter-number-0': makeTest({number: 0, incrementResult: "00000000000000000000000000000001"}),
'test-counter-number-1': makeTest({number: 1, incrementResult: "00000000000000000000000000000002"}),
'test-counter-number-254': makeTest({number: 254, incrementResult: "000000000000000000000000000000ff"}),
'test-counter-number-255': makeTest({number: 255, incrementResult: "00000000000000000000000000000100"}),
'test-counter-number-256': makeTest({number: 256, incrementResult: "00000000000000000000000000000101"}),
'test-counter-number-large': makeTest({number: 1099511627774, incrementResult: "0000000000000000000000ffffffffff"}),
'test-counter-number-max': makeTest({number: 9007199254740991, incrementResult: "00000000000000000020000000000000"}),
'test-counter-bytes-0000': makeTest({bytes: "00000000000000000000000000000000", incrementResult: "00000000000000000000000000000001"}),
'test-counter-bytes-00ff': makeTest({bytes: "000000000000000000000000000000ff", incrementResult: "00000000000000000000000000000100"}),
'test-counter-bytes-ffff': makeTest({bytes: "ffffffffffffffffffffffffffffffff", incrementResult: "00000000000000000000000000000000"}),
'test-counter-bytes-dead': makeTest({bytes: "deadbeefdeadbeefdeadbeefdeadbeef", incrementResult: "deadbeefdeadbeefdeadbeefdeadbef0"}),
};