Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 views
1
var assert = require('assert');
2
var asn1 = require('..');
3
var fixtures = require('./fixtures');
4
var jsonEqual = fixtures.jsonEqual;
5
6
var Buffer = require('buffer').Buffer;
7
8
describe('asn1.js ping/pong', function() {
9
function test(name, model, input, expected) {
10
it('should support ' + name, function() {
11
var M = asn1.define('TestModel', model);
12
13
var encoded = M.encode(input, 'der');
14
var decoded = M.decode(encoded, 'der');
15
jsonEqual(decoded, expected !== undefined ? expected : input);
16
});
17
}
18
19
describe('primitives', function() {
20
test('bigint', function() {
21
this.int();
22
}, new asn1.bignum('0102030405060708', 16));
23
24
test('enum', function() {
25
this.enum({ 0: 'hello', 1: 'world' });
26
}, 'world');
27
28
test('octstr', function() {
29
this.octstr();
30
}, new Buffer('hello'));
31
32
test('bitstr', function() {
33
this.bitstr();
34
}, { unused: 4, data: new Buffer('hello!') });
35
36
test('ia5str', function() {
37
this.ia5str();
38
}, 'hello');
39
40
test('gentime', function() {
41
this.gentime();
42
}, 1385921175000);
43
44
test('utctime', function() {
45
this.utctime();
46
}, 1385921175000);
47
48
test('utctime regression', function() {
49
this.utctime();
50
}, 1414454400000);
51
52
test('null', function() {
53
this.null_();
54
}, null);
55
56
test('objid', function() {
57
this.objid({
58
'1 3 6 1 5 5 7 48 1 1': 'id-pkix-ocsp-basic'
59
});
60
}, 'id-pkix-ocsp-basic');
61
62
test('true', function() {
63
this.bool();
64
}, true);
65
66
test('false', function() {
67
this.bool();
68
}, false);
69
70
test('any', function() {
71
this.any();
72
}, new Buffer('02210081347a0d3d674aeeb563061d94a3aea5f6a7' +
73
'c6dc153ea90a42c1ca41929ac1b9', 'hex'));
74
75
test('default explicit', function() {
76
this.seq().obj(
77
this.key('version').def('v1').explicit(0).int({
78
0: 'v1',
79
1: 'v2'
80
})
81
);
82
}, {}, {'version': 'v1'});
83
84
test('implicit', function() {
85
this.implicit(0).int({
86
0: 'v1',
87
1: 'v2'
88
});
89
}, 'v2', 'v2');
90
});
91
92
describe('composite', function() {
93
test('2x int', function() {
94
this.seq().obj(
95
this.key('hello').int(),
96
this.key('world').int()
97
);
98
}, { hello: 4, world: 2 });
99
100
test('enum', function() {
101
this.seq().obj(
102
this.key('hello').enum({ 0: 'world', 1: 'devs' })
103
);
104
}, { hello: 'devs' });
105
106
test('optionals', function() {
107
this.seq().obj(
108
this.key('hello').enum({ 0: 'world', 1: 'devs' }),
109
this.key('how').optional().def('are you').enum({
110
0: 'are you',
111
1: 'are we?!'
112
})
113
);
114
}, { hello: 'devs', how: 'are we?!' });
115
116
test('optionals #2', function() {
117
this.seq().obj(
118
this.key('hello').enum({ 0: 'world', 1: 'devs' }),
119
this.key('how').optional().def('are you').enum({
120
0: 'are you',
121
1: 'are we?!'
122
})
123
);
124
}, { hello: 'devs' }, { hello: 'devs', how: 'are you' });
125
126
test('optionals #3', function() {
127
this.seq().obj(
128
this.key('content').optional().int()
129
);
130
}, {}, {});
131
132
test('optional + any', function() {
133
this.seq().obj(
134
this.key('content').optional().any()
135
);
136
}, { content: new Buffer('0500', 'hex') });
137
138
test('seqof', function() {
139
var S = asn1.define('S', function() {
140
this.seq().obj(
141
this.key('a').def('b').int({ 0: 'a', 1: 'b' }),
142
this.key('c').def('d').int({ 2: 'c', 3: 'd' })
143
);
144
});
145
this.seqof(S);
146
}, [{}, { a: 'a', c: 'c' }], [{ a: 'b', c: 'd' }, { a: 'a', c: 'c' }]);
147
148
test('choice', function() {
149
this.choice({
150
apple: this.bool()
151
});
152
}, { type: 'apple', value: true });
153
});
154
});
155
156