react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / public-encrypt / node_modules / parse-asn1 / node_modules / asn1.js / test / der-encode-test.js
80620 viewsvar assert = require('assert');1var asn1 = require('..');2var BN = require('bn.js');34var Buffer = require('buffer').Buffer;56describe('asn1.js DER encoder', function() {7/*8* Explicit value shold be wrapped with A0 | EXPLICIT tag9* this adds two more bytes to resulting buffer.10* */11it('should code explicit tag as 0xA2', function() {12var E = asn1.define('E', function() {13this.explicit(2).octstr()14});1516var encoded = E.encode('X', 'der');1718// <Explicit tag> <wrapped len> <str tag> <len> <payload>19assert.equal(encoded.toString('hex'), 'a203040158');20assert.equal(encoded.length, 5);21})2223function test(name, model_definition, model_value, der_expected) {24it(name, function() {25var Model, der_actual;26Model = asn1.define('Model', model_definition);27der_actual = Model.encode(model_value, 'der');28assert.deepEqual(der_actual, new Buffer(der_expected,'hex'));29});30}3132test('should encode choice', function() {33this.choice({34apple: this.bool(),35});36}, { type: 'apple', value: true }, '0101ff');3738test('should encode implicit seqof', function() {39var Int = asn1.define('Int', function() {40this.int();41});42this.implicit(0).seqof(Int);43}, [ 1 ], 'A003020101' );4445test('should encode explicit seqof', function() {46var Int = asn1.define('Int', function() {47this.int();48});49this.explicit(0).seqof(Int);50}, [ 1 ], 'A0053003020101' );5152test('should encode BN(128) properly', function() {53this.int();54}, new BN(128), '02020080');5556test('should encode int 128 properly', function() {57this.int();58}, 128, '02020080');5960test('should encode 0x8011 properly', function() {61this.int();62}, 0x8011, '0203008011');6364test('should omit default value in DER', function() {65this.seq().obj(66this.key('required').def(false).bool(),67this.key('value').int()68);69}, {required: false, value: 1}, '3003020101');70});717273