react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / public-encrypt / node_modules / parse-asn1 / node_modules / asn1.js / test / ping-pong-test.js
80620 viewsvar assert = require('assert');1var asn1 = require('..');2var fixtures = require('./fixtures');3var jsonEqual = fixtures.jsonEqual;45var Buffer = require('buffer').Buffer;67describe('asn1.js ping/pong', function() {8function test(name, model, input, expected) {9it('should support ' + name, function() {10var M = asn1.define('TestModel', model);1112var encoded = M.encode(input, 'der');13var decoded = M.decode(encoded, 'der');14jsonEqual(decoded, expected !== undefined ? expected : input);15});16}1718describe('primitives', function() {19test('bigint', function() {20this.int();21}, new asn1.bignum('0102030405060708', 16));2223test('enum', function() {24this.enum({ 0: 'hello', 1: 'world' });25}, 'world');2627test('octstr', function() {28this.octstr();29}, new Buffer('hello'));3031test('bitstr', function() {32this.bitstr();33}, { unused: 4, data: new Buffer('hello!') });3435test('ia5str', function() {36this.ia5str();37}, 'hello');3839test('gentime', function() {40this.gentime();41}, 1385921175000);4243test('utctime', function() {44this.utctime();45}, 1385921175000);4647test('utctime regression', function() {48this.utctime();49}, 1414454400000);5051test('null', function() {52this.null_();53}, null);5455test('objid', function() {56this.objid({57'1 3 6 1 5 5 7 48 1 1': 'id-pkix-ocsp-basic'58});59}, 'id-pkix-ocsp-basic');6061test('true', function() {62this.bool();63}, true);6465test('false', function() {66this.bool();67}, false);6869test('any', function() {70this.any();71}, new Buffer('02210081347a0d3d674aeeb563061d94a3aea5f6a7' +72'c6dc153ea90a42c1ca41929ac1b9', 'hex'));7374test('default explicit', function() {75this.seq().obj(76this.key('version').def('v1').explicit(0).int({770: 'v1',781: 'v2'79})80);81}, {}, {'version': 'v1'});8283test('implicit', function() {84this.implicit(0).int({850: 'v1',861: 'v2'87});88}, 'v2', 'v2');89});9091describe('composite', function() {92test('2x int', function() {93this.seq().obj(94this.key('hello').int(),95this.key('world').int()96);97}, { hello: 4, world: 2 });9899test('enum', function() {100this.seq().obj(101this.key('hello').enum({ 0: 'world', 1: 'devs' })102);103}, { hello: 'devs' });104105test('optionals', function() {106this.seq().obj(107this.key('hello').enum({ 0: 'world', 1: 'devs' }),108this.key('how').optional().def('are you').enum({1090: 'are you',1101: 'are we?!'111})112);113}, { hello: 'devs', how: 'are we?!' });114115test('optionals #2', function() {116this.seq().obj(117this.key('hello').enum({ 0: 'world', 1: 'devs' }),118this.key('how').optional().def('are you').enum({1190: 'are you',1201: 'are we?!'121})122);123}, { hello: 'devs' }, { hello: 'devs', how: 'are you' });124125test('optionals #3', function() {126this.seq().obj(127this.key('content').optional().int()128);129}, {}, {});130131test('optional + any', function() {132this.seq().obj(133this.key('content').optional().any()134);135}, { content: new Buffer('0500', 'hex') });136137test('seqof', function() {138var S = asn1.define('S', function() {139this.seq().obj(140this.key('a').def('b').int({ 0: 'a', 1: 'b' }),141this.key('c').def('d').int({ 2: 'c', 3: 'd' })142);143});144this.seqof(S);145}, [{}, { a: 'a', c: 'c' }], [{ a: 'b', c: 'd' }, { a: 'a', c: 'c' }]);146147test('choice', function() {148this.choice({149apple: this.bool()150});151}, { type: 'apple', value: true });152});153});154155156