2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2012-04-27 03:23:43 +00:00
|
|
|
var Logger = require('bunyan');
|
|
|
|
|
2014-07-08 19:31:57 +00:00
|
|
|
var test = require('tape').test;
|
2011-08-04 20:32:01 +00:00
|
|
|
var uuid = require('node-uuid');
|
2014-06-26 15:11:07 +00:00
|
|
|
var vasync = require('vasync');
|
2014-07-24 20:41:24 +00:00
|
|
|
var util = require('util');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var BIND_DN = 'cn=root';
|
|
|
|
var BIND_PW = 'secret';
|
|
|
|
var SOCKET = '/tmp/.' + uuid();
|
|
|
|
|
|
|
|
var SUFFIX = 'dc=test';
|
|
|
|
|
2014-06-27 21:15:04 +00:00
|
|
|
var LOG = new Logger({
|
|
|
|
name: 'ldapjs_unit_test',
|
|
|
|
stream: process.stderr,
|
|
|
|
level: (process.env.LOG_LEVEL || 'info'),
|
|
|
|
serializers: Logger.stdSerializers,
|
|
|
|
src: true
|
|
|
|
});
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
var ldap;
|
|
|
|
var Attribute;
|
|
|
|
var Change;
|
|
|
|
var client;
|
|
|
|
var server;
|
|
|
|
|
|
|
|
|
|
|
|
///--- Tests
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('setup', function (t) {
|
2011-08-04 20:32:01 +00:00
|
|
|
ldap = require('../lib/index');
|
|
|
|
t.ok(ldap);
|
|
|
|
t.ok(ldap.createClient);
|
|
|
|
t.ok(ldap.createServer);
|
|
|
|
t.ok(ldap.Attribute);
|
|
|
|
t.ok(ldap.Change);
|
|
|
|
|
|
|
|
Attribute = ldap.Attribute;
|
|
|
|
Change = ldap.Change;
|
|
|
|
|
|
|
|
server = ldap.createServer();
|
|
|
|
t.ok(server);
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.bind(BIND_DN, function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (req.credentials !== BIND_PW)
|
|
|
|
return next(new ldap.InvalidCredentialsError('Invalid password'));
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.add(SUFFIX, function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.compare(SUFFIX, function (req, res, next) {
|
2011-08-23 00:16:36 +00:00
|
|
|
res.end(req.value === 'test');
|
2011-08-04 20:32:01 +00:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.del(SUFFIX, function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// LDAP whoami
|
2012-02-18 08:15:52 +00:00
|
|
|
server.exop('1.3.6.1.4.1.4203.1.11.3', function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
res.value = 'u:xxyyz@EXAMPLE.NET';
|
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.modify(SUFFIX, function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.modifyDN(SUFFIX, function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2014-06-19 19:24:05 +00:00
|
|
|
server.search('dc=slow', function (req, res, next) {
|
|
|
|
res.send({
|
|
|
|
dn: 'dc=slow',
|
|
|
|
attributes: {
|
|
|
|
'you': 'wish',
|
|
|
|
'this': 'was',
|
|
|
|
'faster': '.'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setTimeout(function () {
|
|
|
|
res.end();
|
|
|
|
next();
|
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.search('dc=timeout', function (req, res, next) {
|
2012-01-20 02:02:10 +00:00
|
|
|
// Haha client!
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.search(SUFFIX, function (req, res, next) {
|
2011-09-27 18:49:33 +00:00
|
|
|
|
2011-10-11 18:39:56 +00:00
|
|
|
if (req.dn.equals('cn=ref,' + SUFFIX)) {
|
|
|
|
res.send(res.createSearchReference('ldap://localhost'));
|
|
|
|
} else if (req.dn.equals('cn=bin,' + SUFFIX)) {
|
|
|
|
res.send(res.createSearchEntry({
|
|
|
|
objectName: req.dn,
|
|
|
|
attributes: {
|
2011-10-11 20:56:16 +00:00
|
|
|
'foo;binary': 'wr0gKyDCvCA9IMK+',
|
2011-10-11 22:24:00 +00:00
|
|
|
'gb18030': new Buffer([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]),
|
2011-10-11 20:56:16 +00:00
|
|
|
'objectclass': 'binary'
|
2011-10-11 18:39:56 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else {
|
2011-09-27 18:49:33 +00:00
|
|
|
var e = res.createSearchEntry({
|
|
|
|
objectName: req.dn,
|
|
|
|
attributes: {
|
|
|
|
cn: ['unit', 'test'],
|
2011-10-04 17:27:43 +00:00
|
|
|
SN: 'testy'
|
2011-09-27 18:49:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
res.send(e);
|
|
|
|
res.send(e);
|
|
|
|
}
|
|
|
|
|
2011-10-11 18:39:56 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2014-07-24 20:41:24 +00:00
|
|
|
server.search('cn=sizelimit', function (req, res, next) {
|
|
|
|
var sizeLimit = 200;
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
if (req.sizeLimit > 0 && i >= req.sizeLimit) {
|
|
|
|
break;
|
|
|
|
} else if (i > sizeLimit) {
|
|
|
|
res.end(ldap.LDAP_SIZE_LIMIT_EXCEEDED);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
res.send({
|
|
|
|
dn: util.format('o=%d, cn=sizelimit', i),
|
|
|
|
attributes: {
|
|
|
|
o: [i],
|
|
|
|
objectclass: ['pagedResult']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
server.search('cn=paged', function (req, res, next) {
|
|
|
|
var min = 0;
|
|
|
|
var max = 1000;
|
|
|
|
|
|
|
|
function sendResults(start, end) {
|
|
|
|
start = (start < min) ? min : start;
|
|
|
|
end = (end > max || end < min) ? max : end;
|
|
|
|
var i;
|
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
res.send({
|
|
|
|
dn: util.format('o=%d, cn=paged', i),
|
|
|
|
attributes: {
|
|
|
|
o: [i],
|
|
|
|
objectclass: ['pagedResult']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cookie = null;
|
|
|
|
var pageSize = 0;
|
|
|
|
req.controls.forEach(function (control) {
|
|
|
|
if (control.type === ldap.PagedResultsControl.OID) {
|
|
|
|
pageSize = control.value.size;
|
|
|
|
cookie = control.value.cookie;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cookie && Buffer.isBuffer(cookie)) {
|
|
|
|
// Do simple paging
|
|
|
|
var first = min;
|
|
|
|
if (cookie.length !== 0) {
|
|
|
|
first = parseInt(cookie.toString(), 10);
|
|
|
|
}
|
|
|
|
var last = sendResults(first, first + pageSize);
|
|
|
|
|
|
|
|
var resultCookie;
|
|
|
|
if (last < max) {
|
|
|
|
resultCookie = new Buffer(last.toString());
|
|
|
|
} else {
|
|
|
|
resultCookie = new Buffer('');
|
|
|
|
}
|
|
|
|
res.controls.push(new ldap.PagedResultsControl({
|
|
|
|
value: {
|
|
|
|
size: pageSize, // correctness not required here
|
|
|
|
cookie: resultCookie
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
res.end();
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
// don't allow non-paged searches for this test endpoint
|
|
|
|
next(new ldap.UnwillingToPerformError());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.search('dc=empty', function (req, res, next) {
|
2011-09-30 17:48:56 +00:00
|
|
|
res.send({
|
|
|
|
dn: 'dc=empty',
|
|
|
|
attributes: {
|
|
|
|
member: [],
|
2011-10-17 23:37:09 +00:00
|
|
|
'member;range=0-1': ['cn=user1, dc=empty', 'cn=user2, dc=empty']
|
2011-09-30 17:48:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2014-07-23 19:44:15 +00:00
|
|
|
server.search('cn=busy', function (req, res, next) {
|
|
|
|
next(new ldap.BusyError('too much to do'));
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.unbind(function (req, res, next) {
|
2011-08-04 20:32:01 +00:00
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.listen(SOCKET, function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
client = ldap.createClient({
|
2012-07-09 13:00:51 +00:00
|
|
|
connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10),
|
2011-09-26 23:45:49 +00:00
|
|
|
socketPath: SOCKET,
|
2014-06-27 21:15:04 +00:00
|
|
|
log: LOG
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
t.ok(client);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('simple bind failure', function (t) {
|
|
|
|
client.bind(BIND_DN, uuid(), function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(err);
|
|
|
|
t.notOk(res);
|
|
|
|
|
|
|
|
t.ok(err instanceof ldap.InvalidCredentialsError);
|
|
|
|
t.ok(err instanceof Error);
|
|
|
|
t.ok(err.dn);
|
|
|
|
t.ok(err.message);
|
|
|
|
t.ok(err.stack);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-04-27 03:23:43 +00:00
|
|
|
test('simple bind success', function (t) {
|
|
|
|
client.bind(BIND_DN, BIND_PW, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-07-09 02:44:01 +00:00
|
|
|
test('auto-bind bad credentials', function (t) {
|
|
|
|
var clt = ldap.createClient({
|
|
|
|
socketPath: SOCKET,
|
|
|
|
bindDN: BIND_DN,
|
|
|
|
bindCredentials: 'totallybogus',
|
|
|
|
log: LOG
|
|
|
|
});
|
|
|
|
clt.once('error', function (err) {
|
|
|
|
t.equal(err.code, ldap.LDAP_INVALID_CREDENTIALS);
|
|
|
|
clt.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('auto-bind success', function (t) {
|
|
|
|
var clt = ldap.createClient({
|
|
|
|
socketPath: SOCKET,
|
|
|
|
bindDN: BIND_DN,
|
|
|
|
bindCredentials: BIND_PW,
|
|
|
|
log: LOG
|
|
|
|
});
|
|
|
|
clt.once('connect', function () {
|
|
|
|
t.ok(clt);
|
|
|
|
clt.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('add success', function (t) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var attrs = [
|
|
|
|
new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['test']
|
|
|
|
})
|
|
|
|
];
|
2012-02-18 08:15:52 +00:00
|
|
|
client.add('cn=add, ' + SUFFIX, attrs, function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('add success with object', function (t) {
|
2011-08-04 21:25:12 +00:00
|
|
|
var entry = {
|
|
|
|
cn: ['unit', 'add'],
|
|
|
|
sn: 'test'
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
client.add('cn=add, ' + SUFFIX, entry, function (err, res) {
|
2011-08-04 21:25:12 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('compare success', function (t) {
|
|
|
|
client.compare('cn=compare, ' + SUFFIX, 'cn', 'test', function (err,
|
2011-08-04 20:32:01 +00:00
|
|
|
matched,
|
|
|
|
res) {
|
|
|
|
t.ifError(err);
|
|
|
|
t.ok(matched);
|
|
|
|
t.ok(res);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('compare false', function (t) {
|
|
|
|
client.compare('cn=compare, ' + SUFFIX, 'cn', 'foo', function (err,
|
2011-08-04 20:32:01 +00:00
|
|
|
matched,
|
|
|
|
res) {
|
|
|
|
t.ifError(err);
|
|
|
|
t.notOk(matched);
|
|
|
|
t.ok(res);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('compare bad suffix', function (t) {
|
|
|
|
client.compare('cn=' + uuid(), 'cn', 'foo', function (err,
|
2011-08-04 20:32:01 +00:00
|
|
|
matched,
|
|
|
|
res) {
|
|
|
|
t.ok(err);
|
|
|
|
t.ok(err instanceof ldap.NoSuchObjectError);
|
|
|
|
t.notOk(matched);
|
|
|
|
t.notOk(res);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('delete success', function (t) {
|
|
|
|
client.del('cn=delete, ' + SUFFIX, function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-07-16 23:53:06 +00:00
|
|
|
test('delete with control (GH-212)', function (t) {
|
|
|
|
var control = new ldap.Control({
|
|
|
|
type: '1.2.3.4',
|
|
|
|
criticality: false
|
|
|
|
});
|
|
|
|
client.del('cn=delete, ' + SUFFIX, control, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('exop success', function (t) {
|
|
|
|
client.exop('1.3.6.1.4.1.4203.1.11.3', function (err, value, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(value);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(value, 'u:xxyyz@EXAMPLE.NET');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('exop invalid', function (t) {
|
|
|
|
client.exop('1.2.3.4', function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(err);
|
|
|
|
t.ok(err instanceof ldap.ProtocolError);
|
|
|
|
t.notOk(res);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('bogus exop (GH-17)', function (t) {
|
|
|
|
client.exop('cn=root', function (err, value) {
|
2011-09-23 16:00:28 +00:00
|
|
|
t.ok(err);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('modify success', function (t) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var change = new Change({
|
|
|
|
type: 'Replace',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['test']
|
|
|
|
})
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
client.modify('cn=modify, ' + SUFFIX, change, function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('modify change plain object success', function (t) {
|
2011-08-04 21:25:12 +00:00
|
|
|
var change = new Change({
|
|
|
|
type: 'Replace',
|
|
|
|
modification: {
|
|
|
|
cn: 'test'
|
|
|
|
}
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
client.modify('cn=modify, ' + SUFFIX, change, function (err, res) {
|
2011-08-04 21:25:12 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('modify array success', function (t) {
|
2011-08-04 20:32:01 +00:00
|
|
|
var changes = [
|
|
|
|
new Change({
|
|
|
|
operation: 'Replace',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['test']
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
new Change({
|
|
|
|
operation: 'Delete',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'sn'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
];
|
2012-02-18 08:15:52 +00:00
|
|
|
client.modify('cn=modify, ' + SUFFIX, changes, function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('modify change plain object success (GH-31)', function (t) {
|
2011-11-08 21:31:00 +00:00
|
|
|
var change = {
|
|
|
|
type: 'replace',
|
|
|
|
modification: {
|
|
|
|
cn: 'test',
|
|
|
|
sn: 'bar'
|
|
|
|
}
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
client.modify('cn=modify, ' + SUFFIX, change, function (err, res) {
|
2011-11-08 21:31:00 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('modify DN new RDN only', function (t) {
|
|
|
|
client.modifyDN('cn=old, ' + SUFFIX, 'cn=new', function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('modify DN new superior', function (t) {
|
|
|
|
client.modifyDN('cn=old, ' + SUFFIX, 'cn=new, dc=foo', function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('search basic', function (t) {
|
|
|
|
client.search('cn=test, ' + SUFFIX, '(objectclass=*)', function (err, res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
var gotEntry = 0;
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(entry);
|
|
|
|
t.ok(entry instanceof ldap.SearchEntry);
|
|
|
|
t.equal(entry.dn.toString(), 'cn=test, ' + SUFFIX);
|
|
|
|
t.ok(entry.attributes);
|
|
|
|
t.ok(entry.attributes.length);
|
2011-10-04 17:27:43 +00:00
|
|
|
t.equal(entry.attributes[0].type, 'cn');
|
|
|
|
t.equal(entry.attributes[1].type, 'SN');
|
2011-08-04 21:25:12 +00:00
|
|
|
t.ok(entry.object);
|
2011-08-04 20:32:01 +00:00
|
|
|
gotEntry++;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.fail(err);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2011-08-04 20:32:01 +00:00
|
|
|
t.ok(res);
|
|
|
|
t.ok(res instanceof ldap.SearchResponse);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.equal(gotEntry, 2);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-07-24 20:41:24 +00:00
|
|
|
test('search sizeLimit', function (t) {
|
|
|
|
t.test('over limit', function (t2) {
|
|
|
|
client.search('cn=sizelimit', {}, function (err, res) {
|
|
|
|
t2.ifError(err);
|
|
|
|
res.on('error', function (error) {
|
|
|
|
t2.equal(error.name, 'SizeLimitExceededError');
|
|
|
|
t2.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test('under limit', function (t2) {
|
|
|
|
var limit = 100;
|
|
|
|
client.search('cn=sizelimit', {sizeLimit: limit}, function (err, res) {
|
|
|
|
t2.ifError(err);
|
|
|
|
var count = 0;
|
|
|
|
res.on('searchEntry', function (entry) {
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
res.on('end', function () {
|
|
|
|
t2.pass();
|
|
|
|
t2.equal(count, limit);
|
|
|
|
t2.end();
|
|
|
|
});
|
|
|
|
res.on('error', t2.ifError.bind(t));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('search paged', function (t) {
|
|
|
|
t.test('paged - no pauses', function (t2) {
|
|
|
|
var countEntries = 0;
|
|
|
|
var countPages = 0;
|
|
|
|
client.search('cn=paged', {paged: {pageSize: 100}}, function (err, res) {
|
|
|
|
t2.ifError(err);
|
|
|
|
res.on('searchEntry', function () {
|
|
|
|
countEntries++;
|
|
|
|
});
|
|
|
|
res.on('page', function () {
|
|
|
|
countPages++;
|
|
|
|
});
|
|
|
|
res.on('error', t2.ifError.bind(t2));
|
|
|
|
res.on('end', function () {
|
|
|
|
t2.equal(countEntries, 1000);
|
|
|
|
t2.equal(countPages, 10);
|
|
|
|
t2.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test('paged - pauses', function (t2) {
|
|
|
|
var countPages = 0;
|
|
|
|
client.search('cn=paged', {
|
|
|
|
paged: {
|
|
|
|
pageSize: 100,
|
|
|
|
pagePause: true
|
|
|
|
}
|
|
|
|
}, function (err, res) {
|
|
|
|
t2.ifError(err);
|
|
|
|
res.on('page', function (result, cb) {
|
|
|
|
countPages++;
|
|
|
|
// cancel after 9 to verify callback usage
|
|
|
|
if (countPages === 9) {
|
|
|
|
// another page should never be encountered
|
|
|
|
res.removeAllListeners('page')
|
|
|
|
.on('page', t2.fail.bind(null, 'unexpected page'));
|
|
|
|
return cb(new Error());
|
|
|
|
}
|
|
|
|
return cb();
|
|
|
|
});
|
|
|
|
res.on('error', t2.ifError.bind(t2));
|
|
|
|
res.on('end', function () {
|
|
|
|
t2.equal(countPages, 9);
|
|
|
|
t2.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-25 19:16:31 +00:00
|
|
|
t.test('paged - no support (err handled)', function (t2) {
|
|
|
|
client.search(SUFFIX, {
|
|
|
|
paged: { pageSize: 100 }
|
|
|
|
}, function (err, res) {
|
|
|
|
t2.ifError(err);
|
|
|
|
res.on('pageError', t2.ok.bind(t2));
|
|
|
|
res.on('end', function () {
|
|
|
|
t2.pass();
|
|
|
|
t2.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test('paged - no support (err not handled)', function (t2) {
|
|
|
|
client.search(SUFFIX, {
|
|
|
|
paged: { pageSize: 100 }
|
|
|
|
}, function (err, res) {
|
|
|
|
t2.ifError(err);
|
|
|
|
res.on('end', t2.fail.bind(t2));
|
|
|
|
res.on('error', function (error) {
|
|
|
|
t2.ok(error);
|
|
|
|
t2.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test('paged - redundant control', function (t2) {
|
|
|
|
try {
|
|
|
|
client.search(SUFFIX, {
|
2014-07-29 15:21:57 +00:00
|
|
|
paged: { pageSize: 100 }
|
2014-07-25 19:16:31 +00:00
|
|
|
}, new ldap.PagedResultsControl(),
|
|
|
|
function (err, res) {
|
|
|
|
t2.fail();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
t2.ok(e);
|
|
|
|
t2.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-07-24 20:41:24 +00:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('search referral', function (t) {
|
|
|
|
client.search('cn=ref, ' + SUFFIX, '(objectclass=*)', function (err, res) {
|
2011-09-27 18:49:33 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
var gotEntry = 0;
|
|
|
|
var gotReferral = false;
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-09-27 18:49:33 +00:00
|
|
|
gotEntry++;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchReference', function (referral) {
|
2011-09-27 18:49:33 +00:00
|
|
|
gotReferral = true;
|
|
|
|
t.ok(referral);
|
|
|
|
t.ok(referral instanceof ldap.SearchReference);
|
|
|
|
t.ok(referral.uris);
|
|
|
|
t.ok(referral.uris.length);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2011-09-27 18:49:33 +00:00
|
|
|
t.fail(err);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2011-09-27 18:49:33 +00:00
|
|
|
t.ok(res);
|
|
|
|
t.ok(res instanceof ldap.SearchResponse);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.equal(gotEntry, 0);
|
|
|
|
t.ok(gotReferral);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('search empty attribute', function (t) {
|
|
|
|
client.search('dc=empty', '(objectclass=*)', function (err, res) {
|
2011-09-30 17:48:56 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
var gotEntry = 0;
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-09-30 17:48:56 +00:00
|
|
|
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++;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2011-09-30 17:48:56 +00:00
|
|
|
t.fail(err);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2011-10-11 20:56:16 +00:00
|
|
|
t.ok(res);
|
|
|
|
t.ok(res instanceof ldap.SearchResponse);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.equal(gotEntry, 1);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('GH-21 binary attributes', function (t) {
|
|
|
|
client.search('cn=bin, ' + SUFFIX, '(objectclass=*)', function (err, res) {
|
2011-10-11 20:56:16 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
var gotEntry = 0;
|
|
|
|
var expect = new Buffer('\u00bd + \u00bc = \u00be', 'utf8');
|
2011-10-11 22:24:00 +00:00
|
|
|
var expect2 = new Buffer([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]);
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-10-11 20:56:16 +00:00
|
|
|
t.ok(entry);
|
|
|
|
t.ok(entry instanceof ldap.SearchEntry);
|
|
|
|
t.equal(entry.dn.toString(), 'cn=bin, ' + SUFFIX);
|
|
|
|
t.ok(entry.attributes);
|
|
|
|
t.ok(entry.attributes.length);
|
|
|
|
t.equal(entry.attributes[0].type, 'foo;binary');
|
|
|
|
t.equal(entry.attributes[0].vals[0], expect.toString('base64'));
|
|
|
|
t.equal(entry.attributes[0].buffers[0].toString('base64'),
|
|
|
|
expect.toString('base64'));
|
2011-10-11 22:24:00 +00:00
|
|
|
|
|
|
|
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]);
|
|
|
|
|
2011-10-11 20:56:16 +00:00
|
|
|
t.ok(entry.object);
|
|
|
|
gotEntry++;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2011-10-11 20:56:16 +00:00
|
|
|
t.fail(err);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2011-09-30 17:48:56 +00:00
|
|
|
t.ok(res);
|
|
|
|
t.ok(res instanceof ldap.SearchResponse);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.equal(gotEntry, 1);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('GH-23 case insensitive attribute filtering', function (t) {
|
2011-10-11 17:43:27 +00:00
|
|
|
var opts = {
|
|
|
|
filter: '(objectclass=*)',
|
|
|
|
attributes: ['Cn']
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
client.search('cn=test, ' + SUFFIX, opts, function (err, res) {
|
2011-10-11 17:43:27 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
var gotEntry = 0;
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-10-11 17:43:27 +00:00
|
|
|
t.ok(entry);
|
|
|
|
t.ok(entry instanceof ldap.SearchEntry);
|
|
|
|
t.equal(entry.dn.toString(), 'cn=test, ' + SUFFIX);
|
|
|
|
t.ok(entry.attributes);
|
|
|
|
t.ok(entry.attributes.length);
|
|
|
|
t.equal(entry.attributes[0].type, 'cn');
|
|
|
|
t.ok(entry.object);
|
|
|
|
gotEntry++;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2011-10-11 17:43:27 +00:00
|
|
|
t.fail(err);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2011-10-11 17:43:27 +00:00
|
|
|
t.ok(res);
|
|
|
|
t.ok(res instanceof ldap.SearchResponse);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.equal(gotEntry, 2);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-11 18:39:56 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('GH-24 attribute selection of *', function (t) {
|
2011-10-11 18:39:56 +00:00
|
|
|
var opts = {
|
|
|
|
filter: '(objectclass=*)',
|
|
|
|
attributes: ['*']
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
client.search('cn=test, ' + SUFFIX, opts, function (err, res) {
|
2011-10-11 18:39:56 +00:00
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
var gotEntry = 0;
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-10-11 18:39:56 +00:00
|
|
|
t.ok(entry);
|
|
|
|
t.ok(entry instanceof ldap.SearchEntry);
|
|
|
|
t.equal(entry.dn.toString(), 'cn=test, ' + SUFFIX);
|
|
|
|
t.ok(entry.attributes);
|
|
|
|
t.ok(entry.attributes.length);
|
|
|
|
t.equal(entry.attributes[0].type, 'cn');
|
|
|
|
t.equal(entry.attributes[1].type, 'SN');
|
|
|
|
t.ok(entry.object);
|
|
|
|
gotEntry++;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2011-10-11 18:39:56 +00:00
|
|
|
t.fail(err);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2011-10-11 18:39:56 +00:00
|
|
|
t.ok(res);
|
|
|
|
t.ok(res instanceof ldap.SearchResponse);
|
|
|
|
t.equal(res.status, 0);
|
|
|
|
t.equal(gotEntry, 2);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-19 19:24:05 +00:00
|
|
|
test('idle timeout', function (t) {
|
|
|
|
client.idleTimeout = 250;
|
|
|
|
function premature() {
|
|
|
|
t.ifError(true);
|
|
|
|
}
|
|
|
|
client.on('idle', premature);
|
|
|
|
client.search('dc=slow', 'objectclass=*', function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
res.on('searchEntry', function (res) {
|
|
|
|
t.ok(res);
|
|
|
|
});
|
|
|
|
res.on('error', function (err) {
|
|
|
|
t.ifError(err);
|
|
|
|
});
|
|
|
|
res.on('end', function () {
|
|
|
|
var late = setTimeout(function () {
|
|
|
|
t.ifError(false, 'too late');
|
|
|
|
}, 500);
|
|
|
|
// It's ok to go idle now
|
|
|
|
client.removeListener('idle', premature);
|
|
|
|
client.on('idle', function () {
|
|
|
|
clearTimeout(late);
|
|
|
|
client.removeAllListeners('idle');
|
|
|
|
client.idleTimeout = 0;
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-26 15:11:07 +00:00
|
|
|
test('setup action', function (t) {
|
|
|
|
var setupClient = ldap.createClient({
|
|
|
|
connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10),
|
|
|
|
socketPath: SOCKET,
|
2014-06-27 21:15:04 +00:00
|
|
|
log: LOG
|
2014-06-26 15:11:07 +00:00
|
|
|
});
|
|
|
|
setupClient.on('setup', function (clt, cb) {
|
|
|
|
clt.bind(BIND_DN, BIND_PW, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setupClient.search(SUFFIX, {scope: 'base'}, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
t.ok(res);
|
|
|
|
res.on('end', function () {
|
|
|
|
setupClient.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('setup reconnect', function (t) {
|
|
|
|
var rClient = ldap.createClient({
|
|
|
|
connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10),
|
|
|
|
socketPath: SOCKET,
|
|
|
|
reconnect: true,
|
2014-06-27 21:15:04 +00:00
|
|
|
log: LOG
|
2014-06-26 15:11:07 +00:00
|
|
|
});
|
|
|
|
rClient.on('setup', function (clt, cb) {
|
|
|
|
clt.bind(BIND_DN, BIND_PW, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function doSearch(_, cb) {
|
|
|
|
rClient.search(SUFFIX, {scope: 'base'}, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
res.on('end', function () {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
vasync.pipeline({
|
|
|
|
funcs: [
|
|
|
|
doSearch,
|
|
|
|
function cleanDisconnect(_, cb) {
|
|
|
|
t.ok(rClient.connected);
|
|
|
|
rClient.once('close', function (had_err) {
|
|
|
|
t.ifError(had_err);
|
|
|
|
t.equal(rClient.connected, false);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
rClient.unbind();
|
|
|
|
},
|
|
|
|
doSearch,
|
|
|
|
function simulateError(_, cb) {
|
|
|
|
var msg = 'fake socket error';
|
|
|
|
rClient.once('error', function (err) {
|
|
|
|
t.equal(err.message, msg);
|
|
|
|
t.ok(err);
|
|
|
|
});
|
|
|
|
rClient.once('close', function (had_err) {
|
|
|
|
// can't test had_err because the socket error is being faked
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
rClient.socket.emit('error', new Error(msg));
|
|
|
|
},
|
|
|
|
doSearch
|
|
|
|
]
|
|
|
|
}, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
rClient.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('setup abort', function (t) {
|
|
|
|
var setupClient = ldap.createClient({
|
|
|
|
connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10),
|
|
|
|
socketPath: SOCKET,
|
|
|
|
reconnect: true,
|
2014-06-27 21:15:04 +00:00
|
|
|
log: LOG
|
2014-06-26 15:11:07 +00:00
|
|
|
});
|
|
|
|
var message = 'It\'s a trap!';
|
|
|
|
setupClient.on('setup', function (clt, cb) {
|
|
|
|
// simulate failure
|
|
|
|
t.ok(clt);
|
|
|
|
cb(new Error(message));
|
|
|
|
});
|
|
|
|
setupClient.on('setupError', function (err) {
|
|
|
|
t.ok(true);
|
|
|
|
t.equal(err.message, message);
|
|
|
|
setupClient.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-24 10:33:19 +00:00
|
|
|
test('abort reconnect', function (t) {
|
|
|
|
var abortClient = ldap.createClient({
|
|
|
|
connectTimeout: parseInt(process.env.LDAP_CONNECT_TIMEOUT || 0, 10),
|
|
|
|
socketPath: '/dev/null',
|
2014-06-26 15:11:07 +00:00
|
|
|
reconnect: true,
|
2014-06-27 21:15:04 +00:00
|
|
|
log: LOG
|
2014-06-24 10:33:19 +00:00
|
|
|
});
|
|
|
|
var retryCount = 0;
|
|
|
|
abortClient.on('connectError', function () {
|
|
|
|
++retryCount;
|
|
|
|
});
|
|
|
|
abortClient.once('connectError', function () {
|
|
|
|
t.ok(true);
|
|
|
|
abortClient.once('destroy', function () {
|
|
|
|
t.ok(retryCount < 3);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
abortClient.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-26 15:11:07 +00:00
|
|
|
test('reconnect max retries', function (t) {
|
|
|
|
var RETRIES = 5;
|
|
|
|
var rClient = ldap.createClient({
|
|
|
|
connectTimeout: 100,
|
|
|
|
socketPath: '/dev/null',
|
|
|
|
reconnect: {
|
|
|
|
failAfter: RETRIES,
|
|
|
|
// Keep the test duration low
|
|
|
|
initialDelay: 10,
|
|
|
|
maxDelay: 100
|
|
|
|
},
|
2014-06-27 21:15:04 +00:00
|
|
|
log: LOG
|
2014-06-26 15:11:07 +00:00
|
|
|
});
|
|
|
|
var count = 0;
|
|
|
|
rClient.on('connectError', function () {
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
rClient.on('error', function (err) {
|
|
|
|
t.equal(count, RETRIES);
|
|
|
|
rClient.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-06-27 21:15:04 +00:00
|
|
|
test('reconnect on server close', function (t) {
|
|
|
|
var clt = ldap.createClient({
|
|
|
|
socketPath: SOCKET,
|
|
|
|
reconnect: true,
|
|
|
|
log: LOG
|
|
|
|
});
|
|
|
|
clt.on('setup', function (sclt, cb) {
|
|
|
|
sclt.bind(BIND_DN, BIND_PW, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
clt.once('connect', function () {
|
|
|
|
t.ok(clt.socket);
|
|
|
|
clt.once('connect', function () {
|
|
|
|
t.ok(true, 'successful reconnect');
|
|
|
|
clt.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Simulate server-side close
|
|
|
|
clt.socket.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('no auto-reconnect on unbind', function (t) {
|
|
|
|
var clt = ldap.createClient({
|
|
|
|
socketPath: SOCKET,
|
|
|
|
reconnect: true,
|
|
|
|
log: LOG
|
|
|
|
});
|
|
|
|
clt.on('setup', function (sclt, cb) {
|
|
|
|
sclt.bind(BIND_DN, BIND_PW, function (err, res) {
|
|
|
|
t.ifError(err);
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
clt.once('connect', function () {
|
|
|
|
clt.once('connect', function () {
|
|
|
|
t.ifError(new Error('client should not reconnect'));
|
|
|
|
});
|
|
|
|
clt.once('close', function () {
|
|
|
|
t.ok(true, 'initial close');
|
|
|
|
setImmediate(function () {
|
|
|
|
t.ok(!clt.connected, 'should not be connected');
|
|
|
|
t.ok(!clt.connecting, 'should not be connecting');
|
|
|
|
clt.destroy();
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
clt.unbind();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-07-09 18:40:17 +00:00
|
|
|
test('abandon (GH-27)', function (t) {
|
|
|
|
// FIXME: test abandoning a real request
|
|
|
|
client.abandon(401876543, function (err) {
|
|
|
|
t.ifError(err);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
2011-11-07 22:13:53 +00:00
|
|
|
|
|
|
|
|
2012-04-17 22:14:06 +00:00
|
|
|
test('search timeout (GH-51)', function (t) {
|
|
|
|
client.timeout = 250;
|
|
|
|
client.search('dc=timeout', 'objectclass=*', function (err, res) {
|
2011-11-08 21:10:39 +00:00
|
|
|
t.ifError(err);
|
2012-04-17 22:14:06 +00:00
|
|
|
res.on('error', function () {
|
|
|
|
t.end();
|
|
|
|
});
|
2011-11-08 21:10:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-04-27 03:23:43 +00:00
|
|
|
|
2014-07-23 19:44:15 +00:00
|
|
|
test('resultError handling', function (t) {
|
|
|
|
t.plan(3);
|
|
|
|
vasync.pipeline({
|
|
|
|
funcs: [
|
|
|
|
function errSearch(_, cb) {
|
|
|
|
client.once('resultError', function (error) {
|
|
|
|
t.equal(error.name, 'BusyError');
|
|
|
|
});
|
|
|
|
client.search('cn=busy', {}, function (err, res) {
|
|
|
|
res.once('error', function (error) {
|
|
|
|
t.equal(error.name, 'BusyError');
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function cleanSearch(_, cb) {
|
|
|
|
client.on('resultError', t.ifError.bind(null));
|
|
|
|
client.search(SUFFIX, {}, function (err, res) {
|
|
|
|
res.once('end', function () {
|
|
|
|
t.ok(true);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, function (err, res) {
|
|
|
|
client.removeAllListeners('resultError');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-04-17 22:14:06 +00:00
|
|
|
test('unbind (GH-30)', function (t) {
|
|
|
|
client.unbind(function (err) {
|
|
|
|
t.ifError(err);
|
2012-01-20 02:02:10 +00:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
test('shutdown', function (t) {
|
2012-04-17 22:14:06 +00:00
|
|
|
server.on('close', function () {
|
|
|
|
t.end();
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
2012-04-17 22:14:06 +00:00
|
|
|
server.close();
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|