Skip to content

Commit

Permalink
Merge d8ec389 into c022823
Browse files Browse the repository at this point in the history
  • Loading branch information
shokuie committed Dec 7, 2018
2 parents c022823 + d8ec389 commit 053bd67
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ function fromTree(element, definition) {
childDefinition = definitions[definitionIdx++];
match = fromTree(child, childDefinition);

if (match === null && !childDefinition.optional) {
if (match === null && !childDefinition.optional && definition.type !== 'CHOICE') {
throw new Error('Unmatched mandatory element');
}
}

constructed[childDefinition.name] = match;
if (match !== null) {
constructed[childDefinition.name] = match;
} else {
throw new Error('No definitions found for element.');
}
});

match = constructed;
Expand All @@ -171,7 +175,12 @@ function toTree(value, definition) { // @todo optional third arg: throw exceptio
let choice = definition.elements.find((choice) => value.hasOwnProperty(choice.name));

if (choice) {
return toTree(value[choice.name], choice);
return definition.tag ? {
cls: CLS_CONTEXT_SPECIFIC,
form: FORM_CONSTRUCTED,
tagCode: definition.tag,
elements: new Array(toTree(value[choice.name], choice))
} : toTree(value[choice.name], choice);
}

throw new Error('Choice not matched');
Expand Down
98 changes: 98 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,38 @@ test('fromTree: CHOICE where element is CLS_CONTEXT_SPECIFIC and first choice ma
t.deepEqual(asn1Mapper.fromTree(tree, definition), mapped);
});

test('fromTree: CHOICE where element is CLS_CONTEXT_SPECIFIC and second choice matches', (t) => {
const tree = {
cls: CLS_CONTEXT_SPECIFIC,
form: FORM_CONSTRUCTED,
tagCode: 3,
elements: [{
cls: CLS_CONTEXT_SPECIFIC,
form: FORM_PRIMITIVE,
tagCode: 1,
value: Buffer.from([1, 2, 3])
}]
};
const definition = {
type: 'CHOICE',
tag: 3,
elements: [{
name: 'foo',
tag: 0,
type: 'NULL'
}, {
name: 'bar',
tag: 1,
type: 'OCTET STRING'
}]
};
const mapped = {
bar: Buffer.from([1, 2, 3])
};

t.deepEqual(asn1Mapper.fromTree(tree, definition), mapped);
});

test('fromTree: CHOICE where no choices match', (t) => {
const tree = {
cls: CLS_UNIVERSAL,
Expand Down Expand Up @@ -792,3 +824,69 @@ test('toTree: CHOICE where no choices match', (t) => {
asn1Mapper.toTree(mapped, definition);
});
});

test('toTree: CHOICE element with tag where first choice matches', (t) => {
const mapped = {
foo: 71
};

const definition = {
name: 'test',
type: 'CHOICE',
tag: 18,
elements: [{
name: 'foo',
type: 'INTEGER'
}, {
name: 'bar',
type: 'OCTET STRING'
}]
};

const tree = {
cls: CLS_CONTEXT_SPECIFIC,
form: FORM_CONSTRUCTED,
tagCode: 18,
elements: [{
cls: CLS_UNIVERSAL,
form: FORM_PRIMITIVE,
tagCode: TAG_INTEGER,
value: Buffer.from([71])
}]
};

t.deepEqual(asn1Mapper.toTree(mapped, definition), tree);
});

test('toTree: CHOICE element with tag where second choice matches', (t) => {
const mapped = {
bar: Buffer.from([ 1, 2, 3 ])
};

const definition = {
name: 'test',
type: 'CHOICE',
tag: 18,
elements: [{
name: 'foo',
type: 'INTEGER'
}, {
name: 'bar',
type: 'OCTET STRING'
}]
};

const tree = {
cls: CLS_CONTEXT_SPECIFIC,
form: FORM_CONSTRUCTED,
tagCode: 18,
elements: [{
cls: CLS_UNIVERSAL,
form: FORM_PRIMITIVE,
tagCode: TAG_OCTET_STRING,
value: Buffer.from([ 1, 2, 3 ])
}]
};

t.deepEqual(asn1Mapper.toTree(mapped, definition), tree);
});

0 comments on commit 053bd67

Please sign in to comment.