Comparing version 0.0.2 to 0.0.3
@@ -0,1 +1,3 @@ | ||
const FORM_PRIMITIVE = 0; | ||
function decode(buffer) { | ||
@@ -103,3 +105,3 @@ let bytesRead = 0; | ||
if (element.form === 'PRIMITIVE') { | ||
if (element.form === FORM_PRIMITIVE) { | ||
writeLength(element.value.length); | ||
@@ -106,0 +108,0 @@ |
{ | ||
"name": "asn1-tree", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "ASN.1 parser and decoder", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,2 +6,3 @@ import test from 'ava'; | ||
const CLS_UNIVERSAL = 0; | ||
const CLS_CONTEXT_SPECIFIC = 2; | ||
@@ -11,2 +12,3 @@ const FORM_PRIMITIVE = 0; | ||
const TAG_INTEGER = 2; | ||
const TAG_OCTET_STRING = 4; | ||
@@ -21,4 +23,4 @@ const TAG_NULL = 5; | ||
// Return buffer of given length, with deterministic content | ||
const b = (length) => Buffer.from( | ||
// Return buffer of given length, filled with deterministic content | ||
const f = (length) => Buffer.from( | ||
Array(length) | ||
@@ -29,13 +31,11 @@ .fill(0) | ||
// Decode buffer filled with given array | ||
const d = (...a) => asn1Tree.decode( | ||
a.reduce((b, i) => Buffer.concat([ | ||
b, | ||
i instanceof Buffer ? i : Buffer.from([ i ]) | ||
]), Buffer.from([])) | ||
); | ||
// Compose buffer filled with given array | ||
const b = (...a) => a.reduce((b, i) => Buffer.concat([ | ||
b, | ||
i instanceof Buffer ? i : Buffer.from([ i ]) | ||
]), Buffer.from([])); | ||
test('decode tag 0', (t) => { | ||
test('decode: tag 0', (t) => { | ||
t.is( | ||
d(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, 0)), | ||
asn1Tree.decode(b(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, 0))), | ||
null | ||
@@ -45,11 +45,11 @@ ); | ||
test('decode tagCode 0b11111', (t) => { | ||
test('decode: tagCode 0b11111', (t) => { | ||
t.throws(() => { | ||
d(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, 0b11111)); | ||
asn1Tree.decode(b(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, 0b11111))); | ||
}, 'Extended tags are not supported'); | ||
}); | ||
test('primitive: element with length 0', (t) => { | ||
test('decode: primitive: element with length 0', (t) => { | ||
t.deepEqual( | ||
d(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_NULL), 0), | ||
asn1Tree.decode(b(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_NULL), 0)), | ||
{ | ||
@@ -64,5 +64,5 @@ cls: CLS_UNIVERSAL, | ||
test('primitive: element with short length', (t) => { | ||
test('decode: primitive: element with short length', (t) => { | ||
t.deepEqual( | ||
d(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 3, b(3)), | ||
asn1Tree.decode(b(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 3, f(3))), | ||
{ | ||
@@ -72,3 +72,3 @@ cls: CLS_UNIVERSAL, | ||
tagCode: TAG_OCTET_STRING, | ||
value: b(3) | ||
value: f(3) | ||
} | ||
@@ -78,5 +78,5 @@ ); | ||
test('primitive: element with short length = 127', (t) => { | ||
test('decode: primitive: element with short length = 127', (t) => { | ||
t.deepEqual( | ||
d(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 127, b(127)), | ||
asn1Tree.decode(b(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 127, f(127))), | ||
{ | ||
@@ -86,3 +86,3 @@ cls: CLS_UNIVERSAL, | ||
tagCode: TAG_OCTET_STRING, | ||
value: b(127) | ||
value: f(127) | ||
} | ||
@@ -92,5 +92,5 @@ ); | ||
test('primitive: element with long length', (t) => { | ||
test('decode: primitive: element with long length', (t) => { | ||
t.deepEqual( | ||
d(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 128 | 2, 5000 >> 8, 5000 & 255, b(5000)), | ||
asn1Tree.decode(b(tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 128 | 2, 5000 >> 8, 5000 & 255, f(5000))), | ||
{ | ||
@@ -100,3 +100,3 @@ cls: CLS_UNIVERSAL, | ||
tagCode: TAG_OCTET_STRING, | ||
value: b(5000) | ||
value: f(5000) | ||
} | ||
@@ -106,5 +106,9 @@ ); | ||
test('constructed: element with indefinite length', (t) => { | ||
test('decode: constructed: element with indefinite length', (t) => { | ||
t.deepEqual( | ||
d(tag(CLS_UNIVERSAL, FORM_CONSTRUCTED, TAG_SEQUENCE), 128, tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 3, b(3), 0), | ||
asn1Tree.decode(b( | ||
tag(CLS_UNIVERSAL, FORM_CONSTRUCTED, TAG_SEQUENCE), 128, | ||
tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_OCTET_STRING), 3, f(3), | ||
0 | ||
)), | ||
{ | ||
@@ -118,3 +122,3 @@ cls: CLS_UNIVERSAL, | ||
tagCode: 4, | ||
value: b(3) | ||
value: f(3) | ||
}] | ||
@@ -124,1 +128,18 @@ } | ||
}); | ||
test('encode:', (t) => { | ||
t.deepEqual( | ||
asn1Tree.encode( | ||
{ cls: CLS_UNIVERSAL, form: FORM_CONSTRUCTED, tagCode: TAG_SEQUENCE, elements: [ | ||
{ cls: CLS_CONTEXT_SPECIFIC, form: FORM_PRIMITIVE, tagCode: 0, value: f(10) }, | ||
{ cls: CLS_UNIVERSAL, form: FORM_PRIMITIVE, tagCode: TAG_INTEGER, value: f(1) } | ||
] } | ||
), | ||
b( | ||
tag(CLS_UNIVERSAL, FORM_CONSTRUCTED, TAG_SEQUENCE), 12 + 3, | ||
tag(CLS_CONTEXT_SPECIFIC, FORM_PRIMITIVE, 0), 10, f(10), // 12 | ||
tag(CLS_UNIVERSAL, FORM_PRIMITIVE, TAG_INTEGER), 1, f(1) // 3 | ||
) | ||
); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7305
217