Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 views
1
var assert = require('assert');
2
var asn1 = require('..');
3
4
var Buffer = require('buffer').Buffer;
5
6
describe('asn1.js DER decoder', function() {
7
it('should propagate implicit tag', function() {
8
var B = asn1.define('B', function() {
9
this.seq().obj(
10
this.key('b').octstr()
11
);
12
});
13
14
var A = asn1.define('Bug', function() {
15
this.seq().obj(
16
this.key('a').implicit(0).use(B)
17
);
18
});
19
20
var out = A.decode(new Buffer('300720050403313233', 'hex'), 'der');
21
assert.equal(out.a.b.toString(), '123');
22
});
23
24
it('should decode optional tag to undefined key', function() {
25
var A = asn1.define('A', function() {
26
this.seq().obj(
27
this.key('key').bool(),
28
this.optional().key('opt').bool()
29
);
30
});
31
var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
32
assert.deepEqual(out, { 'key': true });
33
});
34
35
it('should decode optional tag to default value', function() {
36
var A = asn1.define('A', function() {
37
this.seq().obj(
38
this.key('key').bool(),
39
this.optional().key('opt').octstr().def('default')
40
);
41
});
42
var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
43
assert.deepEqual(out, { 'key': true, 'opt': 'default' });
44
});
45
46
function test(name, model, inputHex, expected) {
47
it(name, function() {
48
var M = asn1.define('Model', model);
49
var decoded = M.decode(new Buffer(inputHex,'hex'), 'der');
50
assert.deepEqual(decoded, expected);
51
});
52
}
53
54
test('should decode choice', function() {
55
this.choice({
56
apple: this.bool(),
57
});
58
}, '0101ff', { 'type': 'apple', 'value': true });
59
60
});
61
62