[Pkg-javascript-commits] [node-asn1.js] 50/202: node: fix encoding of CHOICE
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Apr 20 19:18:53 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-asn1.js.
commit 053312683fd09afd1744149b243df775c7f4b5bc
Author: Ilya Petrov <ilya.muromec at gmail.com>
Date: Thu Aug 21 16:15:43 2014 +0300
node: fix encoding of CHOICE
---
lib/asn1/base/node.js | 36 +++++++++++++++++-------------------
test/der-decode-test.js | 15 +++++++++++++++
test/der-encode-test.js | 16 ++++++++++++++++
test/ping-pong-test.js | 6 ++++++
4 files changed, 54 insertions(+), 19 deletions(-)
diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 1707ea4..db68fb2 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -457,6 +457,8 @@ Node.prototype._encode = function encode(data, reporter) {
if (state.any) {
// Anything that was given is translated to buffer
result = this._createEncoderBuffer(data);
+ } else if (state.choice) {
+ result = this._encodeChoice(data, reporter);
} else if (state.children) {
content = state.children.map(function(child) {
if (child._baseState.key === null)
@@ -476,26 +478,22 @@ Node.prototype._encode = function encode(data, reporter) {
content = this._createEncoderBuffer(content);
} else {
- if (state.choice === null) {
- if (state.tag === 'seqof' || state.tag === 'setof') {
- // TODO(indutny): this should be thrown on DSL level
- if (!(state.args && state.args.length === 1))
- return reporter.error('Too many args for : ' + state.tag);
-
- if (!Array.isArray(data))
- return reporter.error('seqof/setof, but data is not Array');
-
- content = this._createEncoderBuffer(data.map(function(item) {
- return this._getUse(state.args[0])._encode(item, reporter);
- }, this));
- } else if (state.use !== null) {
- result = this._getUse(state.use)._encode(data, reporter);
- } else {
- content = this._encodePrimitive(state.tag, data);
- primitive = true;
- }
+ if (state.tag === 'seqof' || state.tag === 'setof') {
+ // TODO(indutny): this should be thrown on DSL level
+ if (!(state.args && state.args.length === 1))
+ return reporter.error('Too many args for : ' + state.tag);
+
+ if (!Array.isArray(data))
+ return reporter.error('seqof/setof, but data is not Array');
+
+ content = this._createEncoderBuffer(data.map(function(item) {
+ return this._getUse(state.args[0])._encode(item, reporter);
+ }, this));
+ } else if (state.use !== null) {
+ result = this._getUse(state.use)._encode(data, reporter);
} else {
- result = this._encodeChoice(data, reporter);
+ content = this._encodePrimitive(state.tag, data);
+ primitive = true;
}
}
diff --git a/test/der-decode-test.js b/test/der-decode-test.js
index 72a6d91..a898393 100644
--- a/test/der-decode-test.js
+++ b/test/der-decode-test.js
@@ -42,4 +42,19 @@ describe('asn1.js DER decoder', function() {
var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
assert.deepEqual(out, { 'key': true, 'opt': 'default' });
});
+
+ function test(name, model, inputHex, expected) {
+ it(name, function() {
+ var M = asn1.define('Model', model);
+ var decoded = M.decode(new Buffer(inputHex,'hex'), 'der');
+ assert.deepEqual(decoded, expected);
+ });
+ }
+
+ test('should decode choice', function() {
+ this.choice({
+ apple: this.bool(),
+ });
+ }, '0101ff', { 'type': 'apple', 'value': true });
+
});
diff --git a/test/der-encode-test.js b/test/der-encode-test.js
index f30ab65..69de4eb 100644
--- a/test/der-encode-test.js
+++ b/test/der-encode-test.js
@@ -19,4 +19,20 @@ describe('asn1.js DER encoder', function() {
assert.equal(encoded.toString('hex'), 'a203040158');
assert.equal(encoded.length, 5);
})
+
+ function test(name, model_definition, model_value, der_expected) {
+ it(name, function() {
+ var Model, der_actual;
+ Model = asn1.define('Model', model_definition);
+ der_actual = Model.encode(model_value, 'der');
+ assert.deepEqual(der_actual, new Buffer(der_expected,'hex'));
+ });
+ }
+
+ test('should encode choice', function() {
+ this.choice({
+ apple: this.bool(),
+ });
+ }, { type: 'apple', value: true }, '0101ff');
+
});
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index 5d22ff1..c9f4c2a 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -130,5 +130,11 @@ describe('asn1.js ping/pong', function() {
});
this.seqof(S);
}, [{}, { a: 'a', c: 'c' }], [{ a: 'b', c: 'd' }, { a: 'a', c: 'c' }]);
+
+ test('choice', function() {
+ this.choice({
+ apple: this.bool()
+ });
+ }, { type: 'apple', value: true });
});
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-asn1.js.git
More information about the Pkg-javascript-commits
mailing list