forked from ristik/node-guardtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
95 lines (87 loc) · 3.75 KB
/
tests.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
// informal unit-tests to test GuardTime node.js API.
// expectations:
// computer's wall clock drift < 5 minutes;
// > 40 days old, non-extended signature file cat.gif.gtts
// signed file cat.gif
// internet connectivity
// GW with public identity must be used.
// run in module build directory
var gt = require('./guardtime'),
TimeSignature = gt.TimeSignature,
assert = require('assert');
gt.loadPublications(function(err) {
assert.ok(err == null, err);
var lastpubdate = TimeSignature.verifyPublications(gt.publications.data);
var now = new Date();
assert.ok(lastpubdate.getTime() < now.getTime(), "last publication must be older than wall clock");
assert.ok(lastpubdate.getTime() + 1000*60*60*24*40 > now.getTime(), "last publication must be no older than 40 days");
gt.sign('Hello!', function(err, ts) {
assert.ok(err == null, err);
gt.verify('Hello!', ts, function(err, res){
assert.ok(err == null, err);
assert.equal(res, gt.VER_RES.PUBLIC_KEY_SIGNATURE_PRESENT +
gt.VER_RES.DOCUMENT_HASH_CHECKED +
gt.VER_RES.PUBLICATION_CHECKED);
});
var tsdate = ts.getRegisteredTime();
assert.ok(tsdate.getTime() + 1000*60*10 > now.getTime(),
"signing time is not within 5 minutes (check your wall clock?)");
assert.ok(tsdate.getTime() - 1000*60*10 < now.getTime(),
"signing time is not within 5 minutes (check your wall clock?)");
assert.ok(ts.getSignerName().match(/public/));
gt.verify('UnHello!', ts, function(err, res) {
assert.ok(err.message.match(/different document/));
assert.ok(res == null);
});
assert.throws(function(){
var invalid = gt.loadSync('cat.gif');
}, /Invalid format/i
);
gt.load('cat.gif', function(err, ts) {
assert.ok(err.message.match(/Invalid format/i));
assert.ok(ts == null);
});
assert.ok(!ts.isExtended());
assert.equal(ts.getHashAlgorithm().toUpperCase(), gt.default_hashalg.toUpperCase());
var old = gt.loadSync('cat.gif.gtts');
assert.ok(! old.isExtended(), "please make sure that testdata is not extended");
assert.equal(old.verify(), gt.VER_RES.PUBLIC_KEY_SIGNATURE_PRESENT);
assert.ok(old.isEarlierThan(ts));
assert.ok(!ts.isEarlierThan(old));
assert.ok(old.getSignerName() != null); // blank if not present
if (!old.isExtended()) {
gt.extend(old, function(err, xold) {
assert.ok(err == null, err);
assert.equal(xold.verify() | gt.VER_RES.PUBLICATION_REFERENCE_PRESENT,
gt.VER_RES.PUBLICATION_REFERENCE_PRESENT);
assert.ok(xold.isExtended());
gt.verifyFile('cat.gif', xold, function(err, res) {
assert.ok(err == null, err);
assert.equal(res | gt.VER_RES.PUBLICATION_REFERENCE_PRESENT,
gt.VER_RES.DOCUMENT_HASH_CHECKED +
gt.VER_RES.PUBLICATION_CHECKED + gt.VER_RES.PUBLICATION_REFERENCE_PRESENT);
});
});
}
gt.load('cat.gif.gtts', function(err, ts) {
gt.verifyFile('cat.gif', ts, function(err, res) {
assert.ok(err == null, err);
assert.equal(res | gt.VER_RES.PUBLICATION_REFERENCE_PRESENT,
gt.VER_RES.DOCUMENT_HASH_CHECKED +
gt.VER_RES.PUBLICATION_CHECKED + gt.VER_RES.PUBLICATION_REFERENCE_PRESENT);
});
});
});
var crypto = require('crypto');
var h = crypto.createHash('sha512');
h.update('Hi there!');
var hd = h.digest();
gt.signHash(hd, 'sha512', function(e, ts) {
assert.ok(e == null, e);
assert.equal(ts.verify(), gt.VER_RES.PUBLIC_KEY_SIGNATURE_PRESENT);
});
gt.signHash(new Buffer(hd, encoding='binary'), 'SHA512', function(e, ts) {
assert.ok(e == null, e);
assert.equal(ts.verify(), gt.VER_RES.PUBLIC_KEY_SIGNATURE_PRESENT);
});
});