react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / public-encrypt / node_modules / parse-asn1 / node_modules / asn1.js / test / der-decode-test.js
80620 viewsvar assert = require('assert');1var asn1 = require('..');23var Buffer = require('buffer').Buffer;45describe('asn1.js DER decoder', function() {6it('should propagate implicit tag', function() {7var B = asn1.define('B', function() {8this.seq().obj(9this.key('b').octstr()10);11});1213var A = asn1.define('Bug', function() {14this.seq().obj(15this.key('a').implicit(0).use(B)16);17});1819var out = A.decode(new Buffer('300720050403313233', 'hex'), 'der');20assert.equal(out.a.b.toString(), '123');21});2223it('should decode optional tag to undefined key', function() {24var A = asn1.define('A', function() {25this.seq().obj(26this.key('key').bool(),27this.optional().key('opt').bool()28);29});30var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');31assert.deepEqual(out, { 'key': true });32});3334it('should decode optional tag to default value', function() {35var A = asn1.define('A', function() {36this.seq().obj(37this.key('key').bool(),38this.optional().key('opt').octstr().def('default')39);40});41var out = A.decode(new Buffer('30030101ff', 'hex'), 'der');42assert.deepEqual(out, { 'key': true, 'opt': 'default' });43});4445function test(name, model, inputHex, expected) {46it(name, function() {47var M = asn1.define('Model', model);48var decoded = M.decode(new Buffer(inputHex,'hex'), 'der');49assert.deepEqual(decoded, expected);50});51}5253test('should decode choice', function() {54this.choice({55apple: this.bool(),56});57}, '0101ff', { 'type': 'apple', 'value': true });5859});606162