[Pkg-javascript-commits] [node-asn1.js] 40/202: node: don't set missing keys to null

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:52 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 caf9e22a42b20ee5d324b95f254f1bdfbda9e655
Author: Ilya Petrov <ilya.muromec at gmail.com>
Date:   Wed Aug 6 19:39:32 2014 +0300

    node: don't set missing keys to null
    
    If key is missing from input it should not be set
    to null in resulting object because such object
    can't be but back into encoder.
---
 lib/asn1/base/node.js   |  2 +-
 test/der-decode-test.js | 24 +++++++++++++++++++++++-
 test/ping-pong-test.js  |  6 ++++++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 2a60a67..7a14cad 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -341,7 +341,7 @@ Node.prototype._decode = function decode(input) {
     result = input.leaveObject(prevObj);
 
   // Set key
-  if (state.key !== null)
+  if (state.key !== null && (result !== null || present === true))
     input.leaveKey(prevKey, state.key, result);
 
   return result;
diff --git a/test/der-decode-test.js b/test/der-decode-test.js
index 4957744..72a6d91 100644
--- a/test/der-decode-test.js
+++ b/test/der-decode-test.js
@@ -19,5 +19,27 @@ describe('asn1.js DER decoder', function() {
 
     var out = A.decode(new Buffer('300720050403313233', 'hex'), 'der');
     assert.equal(out.a.b.toString(), '123');
-  })
+  });
+
+  it('should decode optional tag to undefined key', function() {
+    var A = asn1.define('A', function() {
+      this.seq().obj(
+        this.key('key').bool(),
+        this.optional().key('opt').bool()
+      );
+    });
+    var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
+    assert.deepEqual(out, { 'key': true });
+  });
+
+  it('should decode optional tag to default value', function() {
+    var A = asn1.define('A', function() {
+      this.seq().obj(
+        this.key('key').bool(),
+        this.optional().key('opt').octstr().def('default')
+      );
+    });
+    var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
+    assert.deepEqual(out, { 'key': true, 'opt': 'default' });
+  });
 });
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index c6d9a01..cadf1cd 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -121,6 +121,12 @@ describe('asn1.js ping/pong', function() {
       );
     }, { hello: 'devs' }, { hello: 'devs', how: 'are you' });
 
+    test('optionals #3', function() {
+      this.seq().obj(
+        this.key('content').optional().int()
+      );
+    }, {}, {});
+
     test('seqof', function() {
       var S = asn1.define('S', function() {
         this.seq().obj(

-- 
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