From 01a6cfb77e9e351c2410ef1bbd29a2d2a46e2871 Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Fri, 30 Sep 2011 10:48:56 -0700 Subject: [PATCH] GH-14: Support null/empty attribute values --- lib/attribute.js | 8 +++----- lib/messages/search_entry.js | 10 ++++++++- lib/server.js | 4 +++- package.json | 2 +- tst/client.test.js | 40 ++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/attribute.js b/lib/attribute.js index 5a75217..9a006e4 100644 --- a/lib/attribute.js +++ b/lib/attribute.js @@ -88,11 +88,9 @@ Attribute.prototype.toBer = function(ber) { ber.startSequence(); ber.writeString(this.type); - if (this.vals && this.vals.length) { - ber.startSequence(Protocol.LBER_SET); - ber.writeStringArray(this.vals); - ber.endSequence(); - } + ber.startSequence(Protocol.LBER_SET); + ber.writeStringArray(this.vals && this.vals.length ? this.vals : []); + ber.endSequence(); ber.endSequence(); return ber; diff --git a/lib/messages/search_entry.js b/lib/messages/search_entry.js index 71e4091..3934767 100644 --- a/lib/messages/search_entry.js +++ b/lib/messages/search_entry.js @@ -43,7 +43,15 @@ function SearchEntry(options) { dn: self.dn.toString() }; self.attributes.forEach(function(a) { - obj[a.type] = a.vals.length > 1 ? a.vals.slice() : a.vals[0]; + if (a.vals && a.vals.length) { + if (a.vals.length > 1) { + obj[a.type] = a.vals.slice(); + } else { + obj[a.type] = a.vals[0]; + } + } else { + obj[a.type] = []; + } }); return obj; }); diff --git a/lib/server.js b/lib/server.js index 1116128..f1d4351 100644 --- a/lib/server.js +++ b/lib/server.js @@ -241,7 +241,9 @@ function Server(options) { EventEmitter.call(this, options); - this.log = options.log4js.getLogger('Server'); + this.log4js = options.log4js; + this.log = this.log4js.getLogger('Server'); + var log = this.log; function setupConnection(c) { diff --git a/package.json b/package.json index 1315346..44723f8 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "name": "ldapjs", "homepage": "http://ldapjs.org", "description": "LDAP client and server APIs", - "version": "0.1.8", + "version": "0.1.9", "repository": { "type": "git", "url": "git://github.com/mcavage/node-ldapjs.git" diff --git a/tst/client.test.js b/tst/client.test.js index dffae16..6fc2fdf 100644 --- a/tst/client.test.js +++ b/tst/client.test.js @@ -95,6 +95,18 @@ test('setup', function(t) { return next(); }); + server.search('dc=empty', function(req, res, next) { + res.send({ + dn: 'dc=empty', + attributes: { + member: [], + 'member;range=0-1': ['cn=user1, dc=empty','cn=user2, dc=empty'] + } + }); + res.end(); + return next(); + }); + server.unbind(function(req, res, next) { res.end(); return next(); @@ -381,6 +393,34 @@ test('search referral', function(t) { }); +test('search empty attribute', function(t) { + client.search('dc=empty', '(objectclass=*)', function(err, res) { + t.ifError(err); + t.ok(res); + var gotEntry = 0; + res.on('searchEntry', function(entry) { + var obj = entry.toObject(); + t.equal('dc=empty', obj.dn); + t.ok(obj.member); + t.equal(obj.member.length, 0); + t.ok(obj['member;range=0-1']); + t.ok(obj['member;range=0-1'].length); + gotEntry++; + }); + res.on('error', function(err) { + t.fail(err); + }); + res.on('end', function(res) { + t.ok(res); + t.ok(res instanceof ldap.SearchResponse); + t.equal(res.status, 0); + t.equal(gotEntry, 1); + t.end(); + }); + }); +}); + + test('shutdown', function(t) { client.unbind(function() { server.on('close', function() {