From b87e4bb288c224ff8e4efc27e47f2aa61c1adae3 Mon Sep 17 00:00:00 2001 From: dododedodonl Date: Tue, 27 Aug 2019 14:04:45 +0200 Subject: [PATCH] store buffer on extended request --- lib/messages/ext_request.js | 34 ++++++++-- test/messages/ext_request.test.js | 100 ++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/lib/messages/ext_request.js b/lib/messages/ext_request.js index 35ac072..3dc18d2 100644 --- a/lib/messages/ext_request.js +++ b/lib/messages/ext_request.js @@ -24,6 +24,12 @@ function ExtendedRequest(options) { this.requestName = options.requestName || ''; this.requestValue = options.requestValue; + + if(Buffer.isBuffer(this.requestValue)) { + this.requestValueBuffer = this.requestValue; + } else { + this.requestValueBuffer = new Buffer(this.requestValue || '', 'utf8'); + } } util.inherits(ExtendedRequest, LDAPMessage); Object.defineProperties(ExtendedRequest.prototype, { @@ -49,9 +55,27 @@ Object.defineProperties(ExtendedRequest.prototype, { if (!(Buffer.isBuffer(val) || typeof (val) === 'string')) throw new TypeError('value must be a buffer or a string'); + if(Buffer.isBuffer(val)) { + this.requestValueBuffer = val; + } else { + this.requestValueBuffer = new Buffer(val, 'utf8'); + } + this.requestValue = val; }, configurable: false + }, + valueBuffer: { + get: function getValueBuffer() { + return this.requestValueBuffer; + }, + set: function setValueBuffer(val) { + if(!Buffer.isBuffer(val)) + throw new TypeError('valueBuffer must be a buffer'); + + this.value = val; + }, + configurable: false } }); @@ -59,12 +83,10 @@ ExtendedRequest.prototype._parse = function (ber) { assert.ok(ber); this.requestName = ber.readString(0x80); - if (ber.peek() === 0x81) - try { - this.requestValue = ber.readString(0x81); - } catch (e) { - this.requestValue = ber.readBuffer(0x81); - } + if (ber.peek() === 0x81) { + this.requestValueBuffer = ber.readString(0x81, true); + this.requestValue = this.requestValueBuffer.toString('utf8'); + } return true; }; diff --git a/test/messages/ext_request.test.js b/test/messages/ext_request.test.js index 5056a71..5c2903e 100644 --- a/test/messages/ext_request.test.js +++ b/test/messages/ext_request.test.js @@ -4,6 +4,30 @@ var test = require('tape').test; var asn1 = require('asn1'); +var Buffer = require('buffer').Buffer; + +// Buffer.compare was only added on node 0.11.13 +var areBuffersEqual = function areBuffersEqual(bufA, bufB) { + if(!(Buffer.isBuffer(bufA) && Buffer.isBuffer(bufB))) { + return false; + } + + if(typeof Buffer.compare === 'function') { + return Buffer.compare(bufA, bufB) === 0; + } + + var len = bufA.length; + if (len !== bufB.length) { + return false; + } + for (var i = 0; i < len; i++) { + if (bufA.readUInt8(i) !== bufB.readUInt8(i)) { + return false; + } + } + return true; +}; + ///--- Globals @@ -37,6 +61,58 @@ test('new with args', function (t) { t.ok(req); t.equal(req.requestName, '1.2.3.4'); t.equal(req.requestValue, 'test'); + t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.equal(req.value, 'test'); + t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.end(); +}); + + +test('new with buffer args', function (t) { + var req = new ExtendedRequest({ + requestName: '1.2.3.4', + requestValue: new Buffer('test', 'utf8') + }); + t.ok(req); + t.equal(req.requestName, '1.2.3.4'); + t.equal(req.requestValue, req.requestValueBuffer); + t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.equal(req.value, req.valueBuffer); + t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.end(); +}); + + +test('new no args set args', function (t) { + var req = new ExtendedRequest(); + t.ok(req); + + req.name = '1.2.3.4'; + t.equal(req.requestName, '1.2.3.4'); + + req.value = 'test'; + t.equal(req.requestValue, 'test'); + t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.equal(req.value, 'test'); + t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + + t.end(); +}); + + +test('new no args set args buffer', function (t) { + var req = new ExtendedRequest(); + t.ok(req); + + req.name = '1.2.3.4'; + t.equal(req.requestName, '1.2.3.4'); + + req.value = new Buffer('test', 'utf8'); + t.equal(req.requestValue, req.requestValueBuffer); + t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.equal(req.value, req.valueBuffer); + t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); + t.end(); }); @@ -51,6 +127,9 @@ test('parse', function (t) { t.ok(req._parse(new BerReader(ber.buffer))); t.equal(req.requestName, '1.2.3.4'); t.equal(req.requestValue, 'test'); + t.ok(areBuffersEqual(req.requestValueBuffer, new Buffer('test', 'utf8'))); + t.equal(req.value, 'test'); + t.ok(areBuffersEqual(req.valueBuffer, new Buffer('test', 'utf8'))); t.end(); }); @@ -74,3 +153,24 @@ test('toBer', function (t) { t.end(); }); + + +test('toBer from buffer', function (t) { + var req = new ExtendedRequest({ + messageID: 123, + requestName: '1.2.3.4', + requestValue: new Buffer('test', 'utf8') + }); + + t.ok(req); + + var ber = new BerReader(req.toBer()); + t.ok(ber); + t.equal(ber.readSequence(), 0x30); + t.equal(ber.readInt(), 123); + t.equal(ber.readSequence(), 0x77); + t.equal(ber.readString(0x80), '1.2.3.4'); + t.equal(ber.readString(0x81), 'test'); + + t.end(); +});