react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-sign / node_modules / parse-asn1 / node_modules / asn1.js / test / error-test.js
80621 viewsvar assert = require('assert');1var asn1 = require('..');2var bn = asn1.bignum;3var fixtures = require('./fixtures');4var jsonEqual = fixtures.jsonEqual;56var Buffer = require('buffer').Buffer;78describe('asn1.js error', function() {9describe('encoder', function() {10function test(name, model, input, expected) {11it('should support ' + name, function() {12var M = asn1.define('TestModel', model);1314var error;15assert.throws(function() {16try {17var encoded = M.encode(input, 'der');18} catch (e) {19error = e;20throw e;21}22});2324assert(expected.test(error.stack),25'Failed to match, expected: ' + expected + ' got: ' +26JSON.stringify(error.stack));27});28}2930describe('primitives', function() {31test('int', function() {32this.int();33}, 'hello', /no values map/i);3435test('enum', function() {36this.enum({ 0: 'hello', 1: 'world' });37}, 'gosh', /contain: "gosh"/);3839test('objid', function() {40this.objid();41}, 1, /objid\(\) should be either array or string, got: 1/);42});4344describe('composite', function() {45test('shallow', function() {46this.seq().obj(47this.key('key').int()48);49}, { key: 'hello' } , /map at: \["key"\]/i);5051test('deep and empty', function() {52this.seq().obj(53this.key('a').seq().obj(54this.key('b').seq().obj(55this.key('c').int()56)57)58);59}, { } , /input is not object at: \["a"\]\["b"\]/i);6061test('deep', function() {62this.seq().obj(63this.key('a').seq().obj(64this.key('b').seq().obj(65this.key('c').int()66)67)68);69}, { a: { b: { c: 'hello' } } } , /map at: \["a"\]\["b"\]\["c"\]/i);7071test('use', function() {72var S = asn1.define('S', function() {73this.seq().obj(74this.key('x').int()75);76});7778this.seq().obj(79this.key('a').seq().obj(80this.key('b').use(S)81)82);83}, { a: { b: { x: 'hello' } } } , /map at: \["a"\]\["b"\]\["x"\]/i);84});85});8687describe('decoder', function() {88function test(name, model, input, expected) {89it('should support ' + name, function() {90var M = asn1.define('TestModel', model);9192var error;93assert.throws(function() {94try {95var decoded = M.decode(new Buffer(input, 'hex'), 'der');96} catch (e) {97error = e;98throw e;99}100});101var partial = M.decode(new Buffer(input, 'hex'), 'der', {102partial: true103});104105assert(expected.test(error.stack),106'Failed to match, expected: ' + expected + ' got: ' +107JSON.stringify(error.stack));108109assert.equal(partial.errors.length, 1);110assert(expected.test(partial.errors[0].stack),111'Failed to match, expected: ' + expected + ' got: ' +112JSON.stringify(partial.errors[0].stack));113});114}115116describe('primitive', function() {117test('int', function() {118this.int();119}, '2201', /body of: "int"/);120121test('int', function() {122this.int();123}, '', /tag of "int"/);124});125126describe('composite', function() {127test('shallow', function() {128this.seq().obj(129this.key('a').seq().obj()130);131}, '30', /length of "seq"/);132133test('deep and empty', function() {134this.seq().obj(135this.key('a').seq().obj(136this.key('b').seq().obj(137this.key('c').int()138)139)140);141}, '300430023000', /tag of "int" at: \["a"\]\["b"\]\["c"\]/);142143test('deep and incomplete', function() {144this.seq().obj(145this.key('a').seq().obj(146this.key('b').seq().obj(147this.key('c').int()148)149)150);151}, '30053003300122', /length of "int" at: \["a"\]\["b"\]\["c"\]/);152});153});154155describe('partial decoder', function() {156function test(name, model, input, expectedObj, expectedErrs) {157it('should support ' + name, function() {158var M = asn1.define('TestModel', model);159160var decoded = M.decode(new Buffer(input, 'hex'), 'der', {161partial: true162});163164jsonEqual(decoded.result, expectedObj);165166assert.equal(decoded.errors.length, expectedErrs.length);167expectedErrs.forEach(function(expected, i) {168assert(expected.test(decoded.errors[i].stack),169'Failed to match, expected: ' + expected + ' got: ' +170JSON.stringify(decoded.errors[i].stack));171});172});173}174175test('last key not present', function() {176this.seq().obj(177this.key('a').seq().obj(178this.key('b').seq().obj(179this.key('c').int()180),181this.key('d').int()182)183);184}, '30073005300022012e', { a: { b: {}, d: new bn(46) } }, [185/"int" at: \["a"\]\["b"\]\["c"\]/186]);187188test('first key not present', function() {189this.seq().obj(190this.key('a').seq().obj(191this.key('b').seq().obj(192this.key('c').int()193),194this.key('d').int()195)196);197}, '30073005300322012e', { a: { b: { c: new bn(46) } } }, [198/"int" at: \["a"\]\["d"\]/199]);200});201});202203204