[Pkg-javascript-commits] [node-asn1.js] 73/202: base: don't cache submodels

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:55 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 188b8fe56f2e7fe210baa0a635be9db55a6dc232
Author: Ilya Petrov <ilya.muromec at gmail.com>
Date:   Sat Nov 15 14:50:25 2014 +0700

    base: don't cache submodels
    
    asn1.js uses same model object for different depths of
    recursive encoding.
    
    It is possible that use-function would return different
    submodels on different calls so caching it's return value would
    break encoding.
---
 lib/asn1/base/node.js | 16 +++++++---------
 test/use-test.js      | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index d20d281..2737f40 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -379,15 +379,13 @@ Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
 Node.prototype._getUse = function _getUse(entity, obj) {
 
   var state = this._baseState;
-  if (!state.useDecoder) {
-    // Create altered use decoder if implicit is set
-    state.useDecoder = this._use(entity, obj);
-    assert(state.useDecoder._baseState.parent === null);
-    state.useDecoder = state.useDecoder._baseState.children[0];
-    if (state.implicit !== null) {
-      state.useDecoder = state.useDecoder.clone();
-      state.useDecoder._baseState.implicit = state.implicit;
-    }
+  // Create altered use decoder if implicit is set
+  state.useDecoder = this._use(entity, obj);
+  assert(state.useDecoder._baseState.parent === null);
+  state.useDecoder = state.useDecoder._baseState.children[0];
+  if (state.implicit !== state.useDecoder._baseState.implicit) {
+    state.useDecoder = state.useDecoder.clone();
+    state.useDecoder._baseState.implicit = state.implicit;
   }
   return state.useDecoder;
 };
diff --git a/test/use-test.js b/test/use-test.js
index e046f2b..e179fb4 100644
--- a/test/use-test.js
+++ b/test/use-test.js
@@ -91,6 +91,36 @@ describe('asn1.js models', function() {
 
     });
 
+    it('should support recursive submodels', function() {
+      var PlainSubModel = asn1.define('PlainSubModel', function() {
+        this.int();
+      });
+      var RecursiveModel = asn1.define('RecursiveModel', function() {
+        this.seq().obj(
+          this.key('plain').bool(),
+          this.key('content').use(function(obj) {
+            if(obj.plain) {
+              return PlainSubModel;
+            } else {
+              return RecursiveModel;
+            }
+          })
+        );
+      });
+
+      var data = {
+        'plain': false,
+        'content': {
+          'plain': true,
+          'content': 1
+        }
+      };
+      var wire = RecursiveModel.encode(data, 'der');
+      assert.equal(wire.toString('hex'), '300b01010030060101ff020101');
+      var back = RecursiveModel.decode(wire, 'der');
+      assert.deepEqual(back, data);
+    });
+
   });
 });
 

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