[Pkg-javascript-commits] [node-asn1.js] 99/202: der: always return bn.js integers

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:58 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 77e11171ffe717d98d6981b306f491af576a1bc9
Author: Fedor Indutny <fedor at indutny.com>
Date:   Tue May 19 23:05:39 2015 +0200

    der: always return bn.js integers
---
 lib/asn1/decoders/der.js | 15 ++-------------
 test/error-test.js       |  9 ++++++---
 test/fixtures.js         |  7 +++++++
 test/ping-pong-test.js   |  8 +++-----
 test/use-test.js         | 24 +++++++++++++-----------
 5 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 559600d..c794660 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -205,23 +205,12 @@ DERNode.prototype._decodeBool = function decodeBool(buffer) {
 };
 
 DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
-  var res = 0;
-
   // Bigint, return as it is (assume big endian)
   var raw = buffer.raw();
-  if (raw.length > 3)
-    return new bignum(raw);
-
-  while (!buffer.isEmpty()) {
-    res <<= 8;
-    var i = buffer.readUInt8();
-    if (buffer.isError(i))
-      return i;
-    res |= i;
-  }
+  var res = new bignum(raw);
 
   if (values)
-    res = values[res] || res;
+    res = values[res.toString(10)] || res;
 
   return res;
 };
diff --git a/test/error-test.js b/test/error-test.js
index 3535482..8bda36a 100644
--- a/test/error-test.js
+++ b/test/error-test.js
@@ -1,5 +1,8 @@
 var assert = require('assert');
 var asn1 = require('..');
+var bn = asn1.bignum;
+var fixtures = require('./fixtures');
+var jsonEqual = fixtures.jsonEqual;
 
 var Buffer = require('buffer').Buffer;
 
@@ -159,7 +162,7 @@ describe('asn1.js error', function() {
           partial: true
         });
 
-        assert.deepEqual(decoded.result, expectedObj);
+        jsonEqual(decoded.result, expectedObj);
 
         assert.equal(decoded.errors.length, expectedErrs.length);
         expectedErrs.forEach(function(expected, i) {
@@ -179,7 +182,7 @@ describe('asn1.js error', function() {
           this.key('d').int()
         )
       );
-    }, '30073005300022012e', { a: { b: {}, d: 46 } }, [
+    }, '30073005300022012e', { a: { b: {}, d: new bn(46) } }, [
       /"int" at: \["a"\]\["b"\]\["c"\]/
     ]);
 
@@ -192,7 +195,7 @@ describe('asn1.js error', function() {
           this.key('d').int()
         )
       );
-    }, '30073005300322012e', { a: { b: { c: 46 } } }, [
+    }, '30073005300322012e', { a: { b: { c: new bn(46) } } }, [
       /"int" at: \["a"\]\["d"\]/
     ]);
   });
diff --git a/test/fixtures.js b/test/fixtures.js
new file mode 100644
index 0000000..9ee4db6
--- /dev/null
+++ b/test/fixtures.js
@@ -0,0 +1,7 @@
+var assert = require('assert');
+
+function jsonEqual(a, b) {
+  assert.deepEqual(JSON.parse(JSON.stringify(a)),
+                   JSON.parse(JSON.stringify(b)));
+}
+exports.jsonEqual = jsonEqual;
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index 1044168..10557b8 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -1,5 +1,7 @@
 var assert = require('assert');
 var asn1 = require('..');
+var fixtures = require('./fixtures');
+var jsonEqual = fixtures.jsonEqual;
 
 var Buffer = require('buffer').Buffer;
 
@@ -10,15 +12,11 @@ describe('asn1.js ping/pong', function() {
 
       var encoded = M.encode(input, 'der');
       var decoded = M.decode(encoded, 'der');
-      assert.deepEqual(decoded, expected !== undefined ? expected : input);
+      jsonEqual(decoded, expected !== undefined ? expected : input);
     });
   }
 
   describe('primitives', function() {
-    test('int', function() {
-      this.int();
-    }, 0);
-
     test('bigint', function() {
       this.int();
     }, new asn1.bignum('0102030405060708', 16));
diff --git a/test/use-test.js b/test/use-test.js
index e179fb4..93e088a 100644
--- a/test/use-test.js
+++ b/test/use-test.js
@@ -1,5 +1,8 @@
 var assert = require('assert');
 var asn1 = require('..');
+var bn = asn1.bignum;
+var fixtures = require('./fixtures');
+var jsonEqual = fixtures.jsonEqual;
 
 var Buffer = require('buffer').Buffer;
 
@@ -18,11 +21,11 @@ describe('asn1.js models', function() {
         );
       });
 
-      var data = {a: 1, sub: {b: new Buffer("XXX")}};
+      var data = {a: new bn(1), sub: {b: new Buffer("XXX")}};
       var wire = Model.encode(data, 'der');
       assert.equal(wire.toString('hex'), '300a02010130050403585858');
       var back = Model.decode(wire, 'der');
-      assert.deepEqual(back, data);
+      jsonEqual(back, data);
     });
 
     it('should honour implicit tag from parent', function() {
@@ -38,12 +41,11 @@ describe('asn1.js models', function() {
         );
       });
 
-      var data = {a: 1, sub: {x: new Buffer("123")}};
+      var data = {a: new bn(1), sub: {x: new Buffer("123")}};
       var wire = Model.encode(data, 'der');
       assert.equal(wire.toString('hex'), '300a020101a0050403313233');
       var back = Model.decode(wire, 'der');
-      assert.deepEqual(back, data);
-
+      jsonEqual(back, data);
     });
 
     it('should honour explicit tag from parent', function() {
@@ -59,11 +61,11 @@ describe('asn1.js models', function() {
         );
       });
 
-      var data = {a: 1, sub: {x: new Buffer("123")}};
+      var data = {a: new bn(1), sub: {x: new Buffer("123")}};
       var wire = Model.encode(data, 'der');
       assert.equal(wire.toString('hex'), '300c020101a00730050403313233');
       var back = Model.decode(wire, 'der');
-      assert.deepEqual(back, data);
+      jsonEqual(back, data);
 
     });
 
@@ -83,11 +85,11 @@ describe('asn1.js models', function() {
         );
       });
 
-      var data = {a: 1, sub: {x: new Buffer("123")}};
+      var data = {a: new bn(1), sub: {x: new Buffer("123")}};
       var wire = Model.encode(data, 'der');
       assert.equal(wire.toString('hex'), '300a02010130050403313233');
       var back = Model.decode(wire, 'der');
-      assert.deepEqual(back, data);
+      jsonEqual(back, data);
 
     });
 
@@ -112,13 +114,13 @@ describe('asn1.js models', function() {
         'plain': false,
         'content': {
           'plain': true,
-          'content': 1
+          'content': new bn(1)
         }
       };
       var wire = RecursiveModel.encode(data, 'der');
       assert.equal(wire.toString('hex'), '300b01010030060101ff020101');
       var back = RecursiveModel.decode(wire, 'der');
-      assert.deepEqual(back, data);
+      jsonEqual(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