From 2a41abbff8dc202d53c7d4655cbad4f4fc1d81a6 Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Tue, 11 Oct 2011 15:24:00 -0700 Subject: [PATCH] GH-21: Write attribute values as raw binary, rather than as strings --- lib/attribute.js | 16 ++++++++++------ tst/client.test.js | 9 +++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/attribute.js b/lib/attribute.js index d1ba081..8054f0b 100644 --- a/lib/attribute.js +++ b/lib/attribute.js @@ -118,15 +118,19 @@ Attribute.prototype.parse = function(ber) { Attribute.prototype.toBer = function(ber) { assert.ok(ber); - var vals = []; - this._vals.forEach(function(v) { - vals.push(v.toString('utf8')); - }); - ber.startSequence(); ber.writeString(this.type); ber.startSequence(Protocol.LBER_SET); - ber.writeStringArray(vals); + if (this._vals.length) { + this._vals.forEach(function(b) { + ber.writeByte(asn1.Ber.OctetString); + ber.writeLength(b.length); + for (var i = 0; i < b.length; i++) + ber.writeByte(b[i]); + }); + } else { + ber.writeStringArray([]); + } ber.endSequence(); ber.endSequence(); diff --git a/tst/client.test.js b/tst/client.test.js index a208dc6..9ad00aa 100644 --- a/tst/client.test.js +++ b/tst/client.test.js @@ -84,6 +84,7 @@ test('setup', function(t) { objectName: req.dn, attributes: { 'foo;binary': 'wr0gKyDCvCA9IMK+', + 'gb18030': new Buffer([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]), 'objectclass': 'binary' } })); @@ -438,6 +439,7 @@ test('GH-21 binary attributes', function(t) { t.ok(res); var gotEntry = 0; var expect = new Buffer('\u00bd + \u00bc = \u00be', 'utf8'); + var expect2 = new Buffer([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]); res.on('searchEntry', function(entry) { t.ok(entry); t.ok(entry instanceof ldap.SearchEntry); @@ -448,6 +450,13 @@ test('GH-21 binary attributes', function(t) { t.equal(entry.attributes[0].vals[0], expect.toString('base64')); t.equal(entry.attributes[0].buffers[0].toString('base64'), expect.toString('base64')); + + t.ok(entry.attributes[1].type, 'gb18030'); + t.equal(entry.attributes[1].buffers.length, 1); + t.equal(expect2.length, entry.attributes[1].buffers[0].length); + for (var i = 0; i < expect2.length; i++) + t.equal(expect2[i], entry.attributes[1].buffers[0][i]); + t.ok(entry.object); gotEntry++; });