Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 views
1
var assert = require('assert');
2
var asn1 = require('..');
3
var bn = asn1.bignum;
4
var fixtures = require('./fixtures');
5
var jsonEqual = fixtures.jsonEqual;
6
7
var Buffer = require('buffer').Buffer;
8
9
describe('asn1.js error', function() {
10
describe('encoder', function() {
11
function test(name, model, input, expected) {
12
it('should support ' + name, function() {
13
var M = asn1.define('TestModel', model);
14
15
var error;
16
assert.throws(function() {
17
try {
18
var encoded = M.encode(input, 'der');
19
} catch (e) {
20
error = e;
21
throw e;
22
}
23
});
24
25
assert(expected.test(error.stack),
26
'Failed to match, expected: ' + expected + ' got: ' +
27
JSON.stringify(error.stack));
28
});
29
}
30
31
describe('primitives', function() {
32
test('int', function() {
33
this.int();
34
}, 'hello', /no values map/i);
35
36
test('enum', function() {
37
this.enum({ 0: 'hello', 1: 'world' });
38
}, 'gosh', /contain: "gosh"/);
39
40
test('objid', function() {
41
this.objid();
42
}, 1, /objid\(\) should be either array or string, got: 1/);
43
});
44
45
describe('composite', function() {
46
test('shallow', function() {
47
this.seq().obj(
48
this.key('key').int()
49
);
50
}, { key: 'hello' } , /map at: \["key"\]/i);
51
52
test('deep and empty', function() {
53
this.seq().obj(
54
this.key('a').seq().obj(
55
this.key('b').seq().obj(
56
this.key('c').int()
57
)
58
)
59
);
60
}, { } , /input is not object at: \["a"\]\["b"\]/i);
61
62
test('deep', function() {
63
this.seq().obj(
64
this.key('a').seq().obj(
65
this.key('b').seq().obj(
66
this.key('c').int()
67
)
68
)
69
);
70
}, { a: { b: { c: 'hello' } } } , /map at: \["a"\]\["b"\]\["c"\]/i);
71
72
test('use', function() {
73
var S = asn1.define('S', function() {
74
this.seq().obj(
75
this.key('x').int()
76
);
77
});
78
79
this.seq().obj(
80
this.key('a').seq().obj(
81
this.key('b').use(S)
82
)
83
);
84
}, { a: { b: { x: 'hello' } } } , /map at: \["a"\]\["b"\]\["x"\]/i);
85
});
86
});
87
88
describe('decoder', function() {
89
function test(name, model, input, expected) {
90
it('should support ' + name, function() {
91
var M = asn1.define('TestModel', model);
92
93
var error;
94
assert.throws(function() {
95
try {
96
var decoded = M.decode(new Buffer(input, 'hex'), 'der');
97
} catch (e) {
98
error = e;
99
throw e;
100
}
101
});
102
var partial = M.decode(new Buffer(input, 'hex'), 'der', {
103
partial: true
104
});
105
106
assert(expected.test(error.stack),
107
'Failed to match, expected: ' + expected + ' got: ' +
108
JSON.stringify(error.stack));
109
110
assert.equal(partial.errors.length, 1);
111
assert(expected.test(partial.errors[0].stack),
112
'Failed to match, expected: ' + expected + ' got: ' +
113
JSON.stringify(partial.errors[0].stack));
114
});
115
}
116
117
describe('primitive', function() {
118
test('int', function() {
119
this.int();
120
}, '2201', /body of: "int"/);
121
122
test('int', function() {
123
this.int();
124
}, '', /tag of "int"/);
125
});
126
127
describe('composite', function() {
128
test('shallow', function() {
129
this.seq().obj(
130
this.key('a').seq().obj()
131
);
132
}, '30', /length of "seq"/);
133
134
test('deep and empty', function() {
135
this.seq().obj(
136
this.key('a').seq().obj(
137
this.key('b').seq().obj(
138
this.key('c').int()
139
)
140
)
141
);
142
}, '300430023000', /tag of "int" at: \["a"\]\["b"\]\["c"\]/);
143
144
test('deep and incomplete', function() {
145
this.seq().obj(
146
this.key('a').seq().obj(
147
this.key('b').seq().obj(
148
this.key('c').int()
149
)
150
)
151
);
152
}, '30053003300122', /length of "int" at: \["a"\]\["b"\]\["c"\]/);
153
});
154
});
155
156
describe('partial decoder', function() {
157
function test(name, model, input, expectedObj, expectedErrs) {
158
it('should support ' + name, function() {
159
var M = asn1.define('TestModel', model);
160
161
var decoded = M.decode(new Buffer(input, 'hex'), 'der', {
162
partial: true
163
});
164
165
jsonEqual(decoded.result, expectedObj);
166
167
assert.equal(decoded.errors.length, expectedErrs.length);
168
expectedErrs.forEach(function(expected, i) {
169
assert(expected.test(decoded.errors[i].stack),
170
'Failed to match, expected: ' + expected + ' got: ' +
171
JSON.stringify(decoded.errors[i].stack));
172
});
173
});
174
}
175
176
test('last key not present', function() {
177
this.seq().obj(
178
this.key('a').seq().obj(
179
this.key('b').seq().obj(
180
this.key('c').int()
181
),
182
this.key('d').int()
183
)
184
);
185
}, '30073005300022012e', { a: { b: {}, d: new bn(46) } }, [
186
/"int" at: \["a"\]\["b"\]\["c"\]/
187
]);
188
189
test('first key not present', function() {
190
this.seq().obj(
191
this.key('a').seq().obj(
192
this.key('b').seq().obj(
193
this.key('c').int()
194
),
195
this.key('d').int()
196
)
197
);
198
}, '30073005300322012e', { a: { b: { c: new bn(46) } } }, [
199
/"int" at: \["a"\]\["d"\]/
200
]);
201
});
202
});
203
204