2019-08-27 21:11:49 +00:00
|
|
|
'use strict'
|
2019-08-27 18:17:33 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
const util = require('util')
|
2019-08-28 18:38:04 +00:00
|
|
|
const assert = require('assert')
|
2019-08-27 21:11:49 +00:00
|
|
|
const tap = require('tap')
|
|
|
|
const uuid = require('uuid')
|
|
|
|
const vasync = require('vasync')
|
|
|
|
const { getSock } = require('./utils')
|
|
|
|
const ldap = require('../lib')
|
|
|
|
const { Attribute, Change } = ldap
|
2019-08-27 18:17:33 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
const SUFFIX = 'dc=test'
|
|
|
|
const LDAP_CONNECT_TIMEOUT = process.env.LDAP_CONNECT_TIMEOUT || 0
|
|
|
|
const BIND_DN = 'cn=root'
|
|
|
|
const BIND_PW = 'secret'
|
2019-08-27 18:17:33 +00:00
|
|
|
|
|
|
|
tap.beforeEach((done, t) => {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.context.socketPath = getSock()
|
|
|
|
t.context.server = ldap.createServer()
|
2019-08-27 18:17:33 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
const server = t.context.server
|
2012-02-18 08:15:52 +00:00
|
|
|
server.bind(BIND_DN, function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (req.credentials !== BIND_PW) { return next(new ldap.InvalidCredentialsError('Invalid password')) }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.add(SUFFIX, function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.compare(SUFFIX, function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end(req.value === 'test')
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.del(SUFFIX, function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
// 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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.value = 'u:xxyyz@EXAMPLE.NET'
|
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.modify(SUFFIX, function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.modifyDN(SUFFIX, function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-28 18:38:04 +00:00
|
|
|
server.modifyDN('cn=issue-480', function (req, res, next) {
|
|
|
|
assert(req.newRdn.toString().length > 132)
|
|
|
|
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: {
|
2019-08-27 21:11:49 +00:00
|
|
|
you: 'wish',
|
|
|
|
this: 'was',
|
|
|
|
faster: '.'
|
2014-06-19 19:24:05 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-06-19 19:24:05 +00:00
|
|
|
setTimeout(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
next()
|
|
|
|
}, 250)
|
|
|
|
})
|
2014-06-19 19:24:05 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.search('dc=timeout', function (req, res, next) {
|
2019-08-27 18:17:33 +00:00
|
|
|
// Cause the client to timeout by not sending a response.
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2012-01-20 02:02:10 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.search(SUFFIX, function (req, res, next) {
|
2011-10-11 18:39:56 +00:00
|
|
|
if (req.dn.equals('cn=ref,' + SUFFIX)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.send(res.createSearchReference('ldap://localhost'))
|
2011-10-11 18:39:56 +00:00
|
|
|
} 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+',
|
2019-08-27 21:11:49 +00:00
|
|
|
gb18030: Buffer.from([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA]),
|
|
|
|
objectclass: 'binary'
|
2011-10-11 18:39:56 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}))
|
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
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
res.send(e)
|
|
|
|
res.send(e)
|
2011-09-27 18:49:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2014-07-24 20:41:24 +00:00
|
|
|
server.search('cn=sizelimit', function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const sizeLimit = 200
|
2019-08-27 18:17:33 +00:00
|
|
|
for (let i = 0; i < 1000; i++) {
|
2014-07-24 20:41:24 +00:00
|
|
|
if (req.sizeLimit > 0 && i >= req.sizeLimit) {
|
2019-08-27 21:11:49 +00:00
|
|
|
break
|
2014-07-24 20:41:24 +00:00
|
|
|
} else if (i > sizeLimit) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end(ldap.LDAP_SIZE_LIMIT_EXCEEDED)
|
|
|
|
return next()
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
|
|
|
res.send({
|
|
|
|
dn: util.format('o=%d, cn=sizelimit', i),
|
|
|
|
attributes: {
|
|
|
|
o: [i],
|
|
|
|
objectclass: ['pagedResult']
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
server.search('cn=paged', function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const min = 0
|
|
|
|
const max = 1000
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function sendResults (start, end) {
|
|
|
|
start = (start < min) ? min : start
|
|
|
|
end = (end > max || end < min) ? max : end
|
|
|
|
let i
|
2014-07-24 20:41:24 +00:00
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
res.send({
|
|
|
|
dn: util.format('o=%d, cn=paged', i),
|
|
|
|
attributes: {
|
|
|
|
o: [i],
|
|
|
|
objectclass: ['pagedResult']
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return i
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
let cookie = null
|
|
|
|
let pageSize = 0
|
2014-07-24 20:41:24 +00:00
|
|
|
req.controls.forEach(function (control) {
|
|
|
|
if (control.type === ldap.PagedResultsControl.OID) {
|
2019-08-27 21:11:49 +00:00
|
|
|
pageSize = control.value.size
|
|
|
|
cookie = control.value.cookie
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
if (cookie && Buffer.isBuffer(cookie)) {
|
|
|
|
// Do simple paging
|
2019-08-27 21:11:49 +00:00
|
|
|
let first = min
|
2014-07-24 20:41:24 +00:00
|
|
|
if (cookie.length !== 0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
first = parseInt(cookie.toString(), 10)
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
const last = sendResults(first, first + pageSize)
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
let resultCookie
|
2014-07-24 20:41:24 +00:00
|
|
|
if (last < max) {
|
2019-08-27 21:11:49 +00:00
|
|
|
resultCookie = Buffer.from(last.toString())
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
resultCookie = Buffer.from('')
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
|
|
|
res.controls.push(new ldap.PagedResultsControl({
|
|
|
|
value: {
|
|
|
|
size: pageSize, // correctness not required here
|
|
|
|
cookie: resultCookie
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}))
|
|
|
|
res.end()
|
|
|
|
next()
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
|
|
|
// don't allow non-paged searches for this test endpoint
|
2019-08-27 21:11:49 +00:00
|
|
|
next(new ldap.UnwillingToPerformError())
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-09-03 20:25:14 +00:00
|
|
|
server.search('cn=sssvlv', function (req, res, next) {
|
|
|
|
const min = 0
|
|
|
|
const max = 100
|
|
|
|
const results = []
|
|
|
|
let o = 'aa'
|
|
|
|
for (let i = min; i < max; i++) {
|
|
|
|
results.push({
|
|
|
|
dn: util.format('o=%s, cn=sssvlv', o),
|
|
|
|
attributes: {
|
|
|
|
o: [o],
|
|
|
|
objectclass: ['sssvlvResult']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
o = ((parseInt(o, 36) + 1).toString(36)).replace(/0/g, 'a')
|
|
|
|
}
|
|
|
|
function sendResults (start, end, sortBy, sortDesc) {
|
|
|
|
start = (start < min) ? min : start
|
|
|
|
end = (end > max || end < min) ? max : end
|
|
|
|
const sorted = results.sort((a, b) => {
|
|
|
|
if (a.attributes[sortBy][0] < b.attributes[sortBy][0]) {
|
|
|
|
return sortDesc ? 1 : -1
|
|
|
|
} else if (a.attributes[sortBy][0] > b.attributes[sortBy][0]) {
|
|
|
|
return sortDesc ? -1 : 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
res.send(sorted[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let sortBy = null
|
|
|
|
let sortDesc = null
|
|
|
|
let afterCount = null
|
|
|
|
let targetOffset = null
|
|
|
|
req.controls.forEach(function (control) {
|
|
|
|
if (control.type === ldap.ServerSideSortingRequestControl.OID) {
|
|
|
|
sortBy = control.value[0].attributeType
|
|
|
|
sortDesc = control.value[0].reverseOrder
|
|
|
|
}
|
|
|
|
if (control.type === ldap.VirtualListViewRequestControl.OID) {
|
|
|
|
afterCount = control.value.afterCount
|
|
|
|
targetOffset = control.value.targetOffset
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (sortBy) {
|
|
|
|
if (afterCount && targetOffset) {
|
|
|
|
sendResults(targetOffset - 1, (targetOffset + afterCount), sortBy, sortDesc)
|
|
|
|
} else {
|
|
|
|
sendResults(min, max, sortBy, sortDesc)
|
|
|
|
}
|
|
|
|
res.end()
|
|
|
|
next()
|
|
|
|
} else {
|
|
|
|
next(new ldap.UnwillingToPerformError())
|
|
|
|
}
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2015-10-16 02:49:34 +00:00
|
|
|
server.search('cn=pagederr', function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let cookie = null
|
2015-10-16 02:49:34 +00:00
|
|
|
req.controls.forEach(function (control) {
|
|
|
|
if (control.type === ldap.PagedResultsControl.OID) {
|
2019-08-27 21:11:49 +00:00
|
|
|
cookie = control.value.cookie
|
2015-10-16 02:49:34 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2015-10-16 02:49:34 +00:00
|
|
|
if (cookie && Buffer.isBuffer(cookie) && cookie.length === 0) {
|
|
|
|
// send first "page"
|
2019-08-27 21:11:49 +00:00
|
|
|
res.send({
|
|
|
|
dn: util.format('o=result, cn=pagederr'),
|
|
|
|
attributes: {
|
|
|
|
o: 'result',
|
|
|
|
objectclass: ['pagedResult']
|
|
|
|
}
|
|
|
|
})
|
|
|
|
res.controls.push(new ldap.PagedResultsControl({
|
|
|
|
value: {
|
|
|
|
size: 2,
|
|
|
|
cookie: Buffer.from('a')
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
res.end()
|
|
|
|
return next()
|
2015-10-16 02:49:34 +00:00
|
|
|
} else {
|
|
|
|
// send error instead of second page
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end(ldap.LDAP_SIZE_LIMIT_EXCEEDED)
|
|
|
|
return next()
|
2015-10-16 02:49:34 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2015-10-16 02:49:34 +00:00
|
|
|
|
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
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-09-30 17:48:56 +00:00
|
|
|
|
2014-07-23 19:44:15 +00:00
|
|
|
server.search('cn=busy', function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
next(new ldap.BusyError('too much to do'))
|
|
|
|
})
|
2014-07-23 19:44:15 +00:00
|
|
|
|
2015-04-26 01:53:50 +00:00
|
|
|
server.search('', function (req, res, next) {
|
|
|
|
if (req.dn.toString() === '') {
|
|
|
|
res.send({
|
|
|
|
dn: '',
|
|
|
|
attributes: {
|
|
|
|
objectclass: ['RootDSE', 'top']
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
res.end()
|
2015-04-26 01:53:50 +00:00
|
|
|
} else {
|
|
|
|
// Turn away any other requests (since '' is the fallthrough route)
|
2019-08-27 21:11:49 +00:00
|
|
|
res.errorMessage = 'No tree found for: ' + req.dn.toString()
|
|
|
|
res.end(ldap.LDAP_NO_SUCH_OBJECT)
|
2015-04-26 01:53:50 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return next()
|
|
|
|
})
|
2015-04-26 01:53:50 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
server.unbind(function (req, res, next) {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
server.listen(t.context.socketPath, function () {
|
|
|
|
const client = ldap.createClient({
|
|
|
|
connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10),
|
|
|
|
socketPath: t.context.socketPath
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
t.context.client = client
|
2019-08-27 18:17:33 +00:00
|
|
|
client.on('connect', () => done())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
tap.afterEach((done, t) => {
|
|
|
|
t.context.client.unbind((err) => {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.context.server.close(() => done())
|
2019-08-27 18:17:33 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-09-29 12:34:59 +00:00
|
|
|
tap.test('createClient', t => {
|
|
|
|
t.test('requires an options object', async t => {
|
|
|
|
const match = /options.+required/
|
|
|
|
t.throws(() => ldap.createClient(), match)
|
|
|
|
t.throws(() => ldap.createClient([]), match)
|
|
|
|
t.throws(() => ldap.createClient(''), match)
|
|
|
|
t.throws(() => ldap.createClient(42), match)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('url must be a string', async t => {
|
|
|
|
const match = /options\.url \(string\) required/
|
|
|
|
t.throws(() => ldap.createClient({ url: {} }), match)
|
|
|
|
t.throws(() => ldap.createClient({ url: [] }), match)
|
|
|
|
t.throws(() => ldap.createClient({ url: 42 }), match)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('socketPath must be a string', async t => {
|
|
|
|
const match = /options\.socketPath must be a string/
|
|
|
|
t.throws(() => ldap.createClient({ socketPath: {} }), match)
|
|
|
|
t.throws(() => ldap.createClient({ socketPath: [] }), match)
|
|
|
|
t.throws(() => ldap.createClient({ socketPath: 42 }), match)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('cannot supply both url and socketPath', async t => {
|
|
|
|
t.throws(
|
|
|
|
() => ldap.createClient({ url: 'foo', socketPath: 'bar' }),
|
|
|
|
/options\.url \^ options\.socketPath \(String\) required/
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('must supply at least url or socketPath', async t => {
|
|
|
|
t.throws(
|
|
|
|
() => ldap.createClient({}),
|
|
|
|
/options\.url \^ options\.socketPath \(String\) required/
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO: this test is really flaky. It would be better if we could validate
|
|
|
|
// the options _withouth_ having to connect to a server.
|
|
|
|
// t.test('attaches a child function to logger', async t => {
|
|
|
|
// /* eslint-disable-next-line */
|
|
|
|
// let client
|
|
|
|
// const logger = Object.create(require('abstract-logging'))
|
|
|
|
// const socketPath = getSock()
|
|
|
|
// const server = ldap.createServer()
|
|
|
|
// server.listen(socketPath, () => {})
|
|
|
|
// t.tearDown(() => {
|
|
|
|
// client.unbind(() => server.close())
|
|
|
|
// })
|
|
|
|
|
|
|
|
// client = ldap.createClient({ socketPath, log: logger })
|
|
|
|
// t.ok(logger.child)
|
|
|
|
// t.true(typeof client.log.child === 'function')
|
|
|
|
// })
|
|
|
|
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('simple bind failure', function (t) {
|
|
|
|
t.context.client.bind(BIND_DN, uuid(), function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(err)
|
|
|
|
t.notOk(res)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(err instanceof ldap.InvalidCredentialsError)
|
|
|
|
t.ok(err instanceof Error)
|
|
|
|
t.ok(err.dn)
|
|
|
|
t.ok(err.message)
|
|
|
|
t.ok(err.stack)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('simple bind success', function (t) {
|
|
|
|
t.context.client.bind(BIND_DN, BIND_PW, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2012-04-27 03:23:43 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('simple anonymous bind (empty credentials)', function (t) {
|
|
|
|
t.context.client.bind('', '', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2012-04-27 03:23:43 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('auto-bind bad credentials', function (t) {
|
|
|
|
const clt = ldap.createClient({
|
|
|
|
socketPath: t.context.socketPath,
|
2014-07-09 02:44:01 +00:00
|
|
|
bindDN: BIND_DN,
|
2019-08-27 18:17:33 +00:00
|
|
|
bindCredentials: 'totallybogus'
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-09 02:44:01 +00:00
|
|
|
clt.once('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(err.code, ldap.LDAP_INVALID_CREDENTIALS)
|
|
|
|
t.ok(clt._socket.destroyed, 'expect socket to be destroyed')
|
|
|
|
clt.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-07-09 02:44:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('auto-bind success', function (t) {
|
|
|
|
const clt = ldap.createClient({
|
|
|
|
socketPath: t.context.socketPath,
|
2014-07-09 02:44:01 +00:00
|
|
|
bindDN: BIND_DN,
|
2019-08-27 18:17:33 +00:00
|
|
|
bindCredentials: BIND_PW
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-09 02:44:01 +00:00
|
|
|
clt.once('connect', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(clt)
|
|
|
|
clt.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-07-09 02:44:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('add success', function (t) {
|
|
|
|
const attrs = [
|
2011-08-04 20:32:01 +00:00
|
|
|
new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['test']
|
|
|
|
})
|
2019-08-27 21:11:49 +00:00
|
|
|
]
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.add('cn=add, ' + SUFFIX, attrs, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('add success with object', function (t) {
|
|
|
|
const entry = {
|
2011-08-04 21:25:12 +00:00
|
|
|
cn: ['unit', 'add'],
|
|
|
|
sn: 'test'
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.add('cn=add, ' + SUFFIX, entry, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 21:25:12 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('compare success', function (t) {
|
|
|
|
t.context.client.compare('cn=compare, ' + SUFFIX, 'cn', 'test', function (err, matched, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(matched)
|
|
|
|
t.ok(res)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('compare false', function (t) {
|
|
|
|
t.context.client.compare('cn=compare, ' + SUFFIX, 'cn', 'foo', function (err, matched, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.notOk(matched)
|
|
|
|
t.ok(res)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('compare bad suffix', function (t) {
|
|
|
|
t.context.client.compare('cn=' + uuid(), 'cn', 'foo', function (err, matched, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(err)
|
|
|
|
t.ok(err instanceof ldap.NoSuchObjectError)
|
|
|
|
t.notOk(matched)
|
|
|
|
t.notOk(res)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('delete success', function (t) {
|
|
|
|
t.context.client.del('cn=delete, ' + SUFFIX, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('delete with control (GH-212)', function (t) {
|
|
|
|
const control = new ldap.Control({
|
2014-07-16 23:53:06 +00:00
|
|
|
type: '1.2.3.4',
|
|
|
|
criticality: false
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.del('cn=delete, ' + SUFFIX, control, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-07-16 23:53:06 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('exop success', function (t) {
|
|
|
|
t.context.client.exop('1.3.6.1.4.1.4203.1.11.3', function (err, value, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(value)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(value, 'u:xxyyz@EXAMPLE.NET')
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('exop invalid', function (t) {
|
|
|
|
t.context.client.exop('1.2.3.4', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(err)
|
|
|
|
t.ok(err instanceof ldap.ProtocolError)
|
|
|
|
t.notOk(res)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('bogus exop (GH-17)', function (t) {
|
|
|
|
t.context.client.exop('cn=root', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(err)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-09-23 16:00:28 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('modify success', function (t) {
|
|
|
|
const change = new Change({
|
2011-08-04 20:32:01 +00:00
|
|
|
type: 'Replace',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['test']
|
|
|
|
})
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.modify('cn=modify, ' + SUFFIX, change, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('modify change plain object success', function (t) {
|
|
|
|
const change = new Change({
|
2011-08-04 21:25:12 +00:00
|
|
|
type: 'Replace',
|
|
|
|
modification: {
|
|
|
|
cn: 'test'
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.modify('cn=modify, ' + SUFFIX, change, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 21:25:12 +00:00
|
|
|
|
2019-08-26 06:22:26 +00:00
|
|
|
// https://github.com/ldapjs/node-ldapjs/pull/435
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('can delete attributes', function (t) {
|
|
|
|
const change = new Change({
|
|
|
|
type: 'Delete',
|
|
|
|
modification: { cn: null }
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.modify('cn=modify,' + SUFFIX, change, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2019-08-26 06:22:26 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('modify array success', function (t) {
|
|
|
|
const changes = [
|
2011-08-04 20:32:01 +00:00
|
|
|
new Change({
|
|
|
|
operation: 'Replace',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'cn',
|
|
|
|
vals: ['test']
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
new Change({
|
|
|
|
operation: 'Delete',
|
|
|
|
modification: new Attribute({
|
|
|
|
type: 'sn'
|
|
|
|
})
|
|
|
|
})
|
2019-08-27 21:11:49 +00:00
|
|
|
]
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.modify('cn=modify, ' + SUFFIX, changes, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('modify change plain object success (GH-31)', function (t) {
|
|
|
|
const change = {
|
2011-11-08 21:31:00 +00:00
|
|
|
type: 'replace',
|
|
|
|
modification: {
|
|
|
|
cn: 'test',
|
|
|
|
sn: 'bar'
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.modify('cn=modify, ' + SUFFIX, change, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-11-08 21:31:00 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('modify DN new RDN only', function (t) {
|
|
|
|
t.context.client.modifyDN('cn=old, ' + SUFFIX, 'cn=new', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('modify DN new superior', function (t) {
|
|
|
|
t.context.client.modifyDN('cn=old, ' + SUFFIX, 'cn=new, dc=foo', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-28 18:38:04 +00:00
|
|
|
tap.test('modify DN excessive length (GH-480)', function (t) {
|
|
|
|
t.context.client.modifyDN('cn=issue-480', 'cn=a292979f2c86d513d48bbb9786b564b3c5228146e5ba46f404724e322544a7304a2b1049168803a5485e2d57a544c6a0d860af91330acb77e5907a9e601ad1227e80e0dc50abe963b47a004f2c90f570450d0e920d15436fdc771e3bdac0487a9735473ed3a79361d1778d7e53a7fb0e5f01f97a75ef05837d1d5496fc86968ff47fcb64', function (err, res) {
|
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search basic', function (t) {
|
|
|
|
t.context.client.search('cn=test, ' + SUFFIX, '(objectclass=*)', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
let gotEntry = 0
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(res)
|
|
|
|
t.ok(res instanceof ldap.SearchResponse)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.equal(gotEntry, 2)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search sizeLimit', function (t) {
|
2014-07-24 20:41:24 +00:00
|
|
|
t.test('over limit', function (t2) {
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('cn=sizelimit', {}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.error(err)
|
2014-07-24 20:41:24 +00:00
|
|
|
res.on('error', function (error) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.equal(error.name, 'SizeLimitExceededError')
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
t.test('under limit', function (t2) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const limit = 100
|
|
|
|
t.context.client.search('cn=sizelimit', { sizeLimit: limit }, function (err, res) {
|
|
|
|
t2.error(err)
|
|
|
|
let count = 0
|
2014-07-24 20:41:24 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +00:00
|
|
|
count++
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.pass()
|
|
|
|
t2.equal(count, limit)
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
res.on('error', t2.error.bind(t))
|
|
|
|
})
|
|
|
|
})
|
2019-08-27 13:08:00 +00:00
|
|
|
|
|
|
|
t.end()
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search paged', { timeout: 10000 }, function (t) {
|
2014-07-24 20:41:24 +00:00
|
|
|
t.test('paged - no pauses', function (t2) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let countEntries = 0
|
|
|
|
let countPages = 0
|
|
|
|
t.context.client.search('cn=paged', { paged: { pageSize: 100 } }, function (err, res) {
|
|
|
|
t2.error(err)
|
|
|
|
res.on('searchEntry', entryListener)
|
|
|
|
res.on('page', pageListener)
|
|
|
|
res.on('error', (err) => t2.error(err))
|
2014-07-24 20:41:24 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.equal(countEntries, 1000)
|
|
|
|
t2.equal(countPages, 10)
|
|
|
|
t2.end()
|
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
|
|
|
|
t2.tearDown(() => {
|
2019-08-27 21:11:49 +00:00
|
|
|
res.removeListener('searchEntry', entryListener)
|
|
|
|
res.removeListener('page', pageListener)
|
2019-08-27 18:17:33 +00:00
|
|
|
})
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function entryListener () {
|
|
|
|
countEntries += 1
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function pageListener () {
|
2019-08-27 21:11:49 +00:00
|
|
|
countPages += 1
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
t.test('paged - pauses', function (t2) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let countPages = 0
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('cn=paged', {
|
2014-07-24 20:41:24 +00:00
|
|
|
paged: {
|
|
|
|
pageSize: 100,
|
|
|
|
pagePause: true
|
|
|
|
}
|
|
|
|
}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.error(err)
|
|
|
|
res.on('page', pageListener)
|
|
|
|
res.on('error', (err) => t2.error(err))
|
2019-08-27 18:17:33 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.equal(countPages, 9)
|
|
|
|
t2.end()
|
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
|
|
|
|
function pageListener (result, cb) {
|
2019-08-27 21:11:49 +00:00
|
|
|
countPages++
|
2014-07-24 20:41:24 +00:00
|
|
|
// cancel after 9 to verify callback usage
|
|
|
|
if (countPages === 9) {
|
|
|
|
// another page should never be encountered
|
2019-08-27 18:17:33 +00:00
|
|
|
res.removeListener('page', pageListener)
|
2019-08-27 21:11:49 +00:00
|
|
|
.on('page', t2.fail.bind(null, 'unexpected page'))
|
|
|
|
return cb(new Error())
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return cb()
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2014-07-25 19:16:31 +00:00
|
|
|
t.test('paged - no support (err handled)', function (t2) {
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search(SUFFIX, {
|
2014-07-25 19:16:31 +00:00
|
|
|
paged: { pageSize: 100 }
|
|
|
|
}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.error(err)
|
|
|
|
res.on('pageError', t2.ok.bind(t2))
|
2014-07-25 19:16:31 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.pass()
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-25 19:16:31 +00:00
|
|
|
|
|
|
|
t.test('paged - no support (err not handled)', function (t2) {
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search(SUFFIX, {
|
2014-07-25 19:16:31 +00:00
|
|
|
paged: { pageSize: 100 }
|
|
|
|
}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.error(err)
|
|
|
|
res.on('end', t2.fail.bind(t2))
|
2014-07-25 19:16:31 +00:00
|
|
|
res.on('error', function (error) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.ok(error)
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-25 19:16:31 +00:00
|
|
|
|
|
|
|
t.test('paged - redundant control', function (t2) {
|
|
|
|
try {
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.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(),
|
2019-08-27 21:11:49 +00:00
|
|
|
function (err) {
|
|
|
|
t.error(err)
|
|
|
|
t2.fail()
|
|
|
|
})
|
2014-07-25 19:16:31 +00:00
|
|
|
} catch (e) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.ok(e)
|
|
|
|
t2.end()
|
2014-07-25 19:16:31 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-25 19:16:31 +00:00
|
|
|
|
2015-10-16 02:49:34 +00:00
|
|
|
t.test('paged - handle later error', function (t2) {
|
2019-08-27 21:11:49 +00:00
|
|
|
let countEntries = 0
|
|
|
|
let countPages = 0
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('cn=pagederr', {
|
2015-10-16 02:49:34 +00:00
|
|
|
paged: { pageSize: 1 }
|
|
|
|
}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.error(err)
|
2015-10-16 02:49:34 +00:00
|
|
|
res.on('searchEntry', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.ok(++countEntries)
|
|
|
|
})
|
2015-10-16 02:49:34 +00:00
|
|
|
res.on('page', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.ok(++countPages)
|
|
|
|
})
|
2015-10-16 02:49:34 +00:00
|
|
|
res.on('error', function (error) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.ok(error)
|
|
|
|
t2.equal(countEntries, 1)
|
|
|
|
t2.equal(countPages, 1)
|
|
|
|
t2.end()
|
|
|
|
})
|
2015-10-16 02:49:34 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t2.fail('should not be reached')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2015-10-16 02:49:34 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-09-03 20:25:14 +00:00
|
|
|
tap.test('search - sssvlv', { timeout: 10000 }, function (t) {
|
|
|
|
t.test('ssv - asc', function (t2) {
|
|
|
|
let preventry = null
|
|
|
|
const sssrcontrol = new ldap.ServerSideSortingRequestControl(
|
|
|
|
{
|
|
|
|
value: {
|
|
|
|
attributeType: 'o',
|
|
|
|
orderingRule: 'caseIgnoreOrderingMatch',
|
|
|
|
reverseOrder: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
t.context.client.search('cn=sssvlv', {}, sssrcontrol, function (err, res) {
|
|
|
|
t2.error(err)
|
|
|
|
res.on('searchEntry', function (entry) {
|
|
|
|
t2.ok(entry)
|
|
|
|
t2.ok(entry instanceof ldap.SearchEntry)
|
|
|
|
t2.ok(entry.attributes)
|
|
|
|
t2.ok(entry.attributes.length)
|
|
|
|
if (preventry != null) {
|
|
|
|
t2.ok(entry.attributes[0]._vals[0] >= preventry.attributes[0]._vals[0])
|
|
|
|
}
|
|
|
|
preventry = entry
|
|
|
|
})
|
|
|
|
res.on('error', (err) => t2.error(err))
|
|
|
|
res.on('end', function () {
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.test('ssv - desc', function (t2) {
|
|
|
|
let preventry = null
|
|
|
|
const sssrcontrol = new ldap.ServerSideSortingRequestControl(
|
|
|
|
{
|
|
|
|
value: {
|
|
|
|
attributeType: 'o',
|
|
|
|
orderingRule: 'caseIgnoreOrderingMatch',
|
|
|
|
reverseOrder: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
t.context.client.search('cn=sssvlv', {}, sssrcontrol, function (err, res) {
|
|
|
|
t2.error(err)
|
|
|
|
res.on('searchEntry', function (entry) {
|
|
|
|
t2.ok(entry)
|
|
|
|
t2.ok(entry instanceof ldap.SearchEntry)
|
|
|
|
t2.ok(entry.attributes)
|
|
|
|
t2.ok(entry.attributes.length)
|
|
|
|
if (preventry != null) {
|
|
|
|
t2.ok(entry.attributes[0]._vals[0] <= preventry.attributes[0]._vals[0])
|
|
|
|
}
|
|
|
|
preventry = entry
|
|
|
|
})
|
|
|
|
res.on('error', (err) => t2.error(err))
|
|
|
|
res.on('end', function () {
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('vlv - first page', function (t2) {
|
|
|
|
const sssrcontrol = new ldap.ServerSideSortingRequestControl(
|
|
|
|
{
|
|
|
|
value: {
|
|
|
|
attributeType: 'o',
|
|
|
|
orderingRule: 'caseIgnoreOrderingMatch',
|
|
|
|
reverseOrder: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const vlvrcontrol = new ldap.VirtualListViewRequestControl(
|
|
|
|
{
|
|
|
|
value: {
|
|
|
|
beforeCount: 0,
|
|
|
|
afterCount: 9,
|
|
|
|
targetOffset: 1,
|
|
|
|
contentCount: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
let count = 0
|
|
|
|
let preventry = null
|
|
|
|
t.context.client.search('cn=sssvlv', {}, [sssrcontrol, vlvrcontrol], function (err, res) {
|
|
|
|
t2.error(err)
|
|
|
|
res.on('searchEntry', function (entry) {
|
|
|
|
t2.ok(entry)
|
|
|
|
t2.ok(entry instanceof ldap.SearchEntry)
|
|
|
|
t2.ok(entry.attributes)
|
|
|
|
t2.ok(entry.attributes.length)
|
|
|
|
if (preventry != null) {
|
|
|
|
t2.ok(entry.attributes[0]._vals[0] >= preventry.attributes[0]._vals[0])
|
|
|
|
}
|
|
|
|
preventry = entry
|
|
|
|
count++
|
|
|
|
})
|
|
|
|
res.on('error', (err) => t2.error(err))
|
|
|
|
res.on('end', function (result) {
|
|
|
|
t2.equals(count, 10)
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.test('vlv - last page', function (t2) {
|
|
|
|
const sssrcontrol = new ldap.ServerSideSortingRequestControl(
|
|
|
|
{
|
|
|
|
value: {
|
|
|
|
attributeType: 'o',
|
|
|
|
orderingRule: 'caseIgnoreOrderingMatch',
|
|
|
|
reverseOrder: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const vlvrcontrol = new ldap.VirtualListViewRequestControl(
|
|
|
|
{
|
|
|
|
value: {
|
|
|
|
beforeCount: 0,
|
|
|
|
afterCount: 9,
|
|
|
|
targetOffset: 91,
|
|
|
|
contentCount: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
let count = 0
|
|
|
|
let preventry = null
|
|
|
|
t.context.client.search('cn=sssvlv', {}, [sssrcontrol, vlvrcontrol], function (err, res) {
|
|
|
|
t2.error(err)
|
|
|
|
res.on('searchEntry', function (entry) {
|
|
|
|
t2.ok(entry)
|
|
|
|
t2.ok(entry instanceof ldap.SearchEntry)
|
|
|
|
t2.ok(entry.attributes)
|
|
|
|
t2.ok(entry.attributes.length)
|
|
|
|
if (preventry != null) {
|
|
|
|
t2.ok(entry.attributes[0]._vals[0] >= preventry.attributes[0]._vals[0])
|
|
|
|
}
|
|
|
|
preventry = entry
|
|
|
|
count++
|
|
|
|
})
|
|
|
|
res.on('error', (err) => t2.error(err))
|
|
|
|
res.on('end', function (result) {
|
|
|
|
t2.equals(count, 10)
|
|
|
|
t2.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search referral', function (t) {
|
|
|
|
t.context.client.search('cn=ref, ' + SUFFIX, '(objectclass=*)', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
let gotEntry = 0
|
|
|
|
let gotReferral = false
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +00:00
|
|
|
gotEntry++
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchReference', function (referral) {
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2011-09-27 18:49:33 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search rootDSE', function (t) {
|
|
|
|
t.context.client.search('', '(objectclass=*)', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
2015-04-26 01:53:50 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(entry)
|
|
|
|
t.equal(entry.dn.toString(), '')
|
|
|
|
t.ok(entry.attributes)
|
|
|
|
t.ok(entry.object)
|
|
|
|
})
|
2015-04-26 01:53:50 +00:00
|
|
|
res.on('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2015-04-26 01:53:50 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(res)
|
|
|
|
t.ok(res instanceof ldap.SearchResponse)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2015-04-26 01:53:50 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search empty attribute', function (t) {
|
|
|
|
t.context.client.search('dc=empty', '(objectclass=*)', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
let gotEntry = 0
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(res)
|
|
|
|
t.ok(res instanceof ldap.SearchResponse)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.equal(gotEntry, 1)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2011-10-11 20:56:16 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('GH-21 binary attributes', function (t) {
|
|
|
|
t.context.client.search('cn=bin, ' + SUFFIX, '(objectclass=*)', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
let gotEntry = 0
|
|
|
|
const expect = Buffer.from('\u00bd + \u00bc = \u00be', 'utf8')
|
|
|
|
const expect2 = Buffer.from([0xB5, 0xE7, 0xCA, 0xD3, 0xBB, 0xFA])
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +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'))
|
2011-10-11 20:56:16 +00:00
|
|
|
t.equal(entry.attributes[0].buffers[0].toString('base64'),
|
2019-08-27 21:11:49 +00:00
|
|
|
expect.toString('base64'))
|
2011-10-11 22:24:00 +00:00
|
|
|
|
2019-08-27 21:11:49 +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 22:24:00 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(entry.object)
|
|
|
|
gotEntry++
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(res)
|
|
|
|
t.ok(res instanceof ldap.SearchResponse)
|
|
|
|
t.equal(res.status, 0)
|
|
|
|
t.equal(gotEntry, 1)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2011-09-30 17:48:56 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('GH-23 case insensitive attribute filtering', function (t) {
|
|
|
|
const opts = {
|
2011-10-11 17:43:27 +00:00
|
|
|
filter: '(objectclass=*)',
|
|
|
|
attributes: ['Cn']
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('cn=test, ' + SUFFIX, opts, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
let gotEntry = 0
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +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 17:43:27 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('GH-24 attribute selection of *', function (t) {
|
|
|
|
const opts = {
|
2011-10-11 18:39:56 +00:00
|
|
|
filter: '(objectclass=*)',
|
|
|
|
attributes: ['*']
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('cn=test, ' + SUFFIX, opts, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
|
|
|
let gotEntry = 0
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.fail(err)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
res.on('end', function (res) {
|
2019-08-27 21:11:49 +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
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('idle timeout', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.context.client.idleTimeout = 250
|
|
|
|
function premature () {
|
|
|
|
t.error(true)
|
2014-06-19 19:24:05 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
t.context.client.on('idle', premature)
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('dc=slow', 'objectclass=*', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
2014-06-19 19:24:05 +00:00
|
|
|
res.on('searchEntry', function (res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(res)
|
|
|
|
})
|
2014-06-19 19:24:05 +00:00
|
|
|
res.on('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
})
|
2014-06-19 19:24:05 +00:00
|
|
|
res.on('end', function () {
|
|
|
|
var late = setTimeout(function () {
|
2019-08-30 14:47:54 +00:00
|
|
|
t.fail('too late')
|
2019-08-27 21:11:49 +00:00
|
|
|
}, 500)
|
2014-06-19 19:24:05 +00:00
|
|
|
// It's ok to go idle now
|
2019-08-27 21:11:49 +00:00
|
|
|
t.context.client.removeListener('idle', premature)
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.on('idle', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
clearTimeout(late)
|
|
|
|
t.context.client.removeAllListeners('idle')
|
|
|
|
t.context.client.idleTimeout = 0
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-19 19:24:05 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('setup action', function (t) {
|
|
|
|
const setupClient = ldap.createClient({
|
2019-08-27 21:11:49 +00:00
|
|
|
connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10),
|
|
|
|
socketPath: t.context.socketPath
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
setupClient.on('setup', function (clt, cb) {
|
|
|
|
clt.bind(BIND_DN, BIND_PW, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
setupClient.search(SUFFIX, { scope: 'base' }, function (err, res) {
|
|
|
|
t.error(err)
|
|
|
|
t.ok(res)
|
2014-06-26 15:11:07 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
setupClient.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('setup reconnect', function (t) {
|
|
|
|
const rClient = ldap.createClient({
|
2019-08-27 21:11:49 +00:00
|
|
|
connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10),
|
|
|
|
socketPath: t.context.socketPath,
|
|
|
|
reconnect: true
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
rClient.on('setup', function (clt, cb) {
|
|
|
|
clt.bind(BIND_DN, BIND_PW, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function doSearch (_, cb) {
|
|
|
|
rClient.search(SUFFIX, { scope: 'base' }, function (err, res) {
|
|
|
|
t.error(err)
|
2014-06-26 15:11:07 +00:00
|
|
|
res.on('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
}
|
2019-08-27 18:17:33 +00:00
|
|
|
|
2014-06-26 15:11:07 +00:00
|
|
|
vasync.pipeline({
|
|
|
|
funcs: [
|
|
|
|
doSearch,
|
2019-08-27 21:11:49 +00:00
|
|
|
function cleanDisconnect (_, cb) {
|
|
|
|
t.ok(rClient.connected)
|
|
|
|
rClient.once('close', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
t.equal(rClient.connected, false)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
rClient.unbind()
|
2014-06-26 15:11:07 +00:00
|
|
|
},
|
|
|
|
doSearch,
|
2019-08-27 21:11:49 +00:00
|
|
|
function simulateError (_, cb) {
|
|
|
|
const msg = 'fake socket error'
|
2014-06-26 15:11:07 +00:00
|
|
|
rClient.once('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(err.message, msg)
|
|
|
|
t.ok(err)
|
|
|
|
})
|
|
|
|
rClient.once('close', function () {
|
2014-06-26 15:11:07 +00:00
|
|
|
// can't test had_err because the socket error is being faked
|
2019-08-27 21:11:49 +00:00
|
|
|
cb()
|
|
|
|
})
|
|
|
|
rClient._socket.emit('error', new Error(msg))
|
2014-06-26 15:11:07 +00:00
|
|
|
},
|
|
|
|
doSearch
|
|
|
|
]
|
2019-08-27 18:17:33 +00:00
|
|
|
}, function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
rClient.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('setup abort', function (t) {
|
|
|
|
const setupClient = ldap.createClient({
|
2019-08-27 21:11:49 +00:00
|
|
|
connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10),
|
|
|
|
socketPath: t.context.socketPath,
|
|
|
|
reconnect: true
|
|
|
|
})
|
|
|
|
const message = "It's a trap!"
|
2014-06-26 15:11:07 +00:00
|
|
|
setupClient.on('setup', function (clt, cb) {
|
|
|
|
// simulate failure
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(clt)
|
|
|
|
cb(new Error(message))
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
setupClient.on('setupError', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(true)
|
|
|
|
t.equal(err.message, message)
|
|
|
|
setupClient.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('abort reconnect', function (t) {
|
|
|
|
const abortClient = ldap.createClient({
|
|
|
|
connectTimeout: parseInt(LDAP_CONNECT_TIMEOUT, 10),
|
|
|
|
socketPath: 'an invalid path',
|
|
|
|
reconnect: true
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
var retryCount = 0
|
2014-06-24 10:33:19 +00:00
|
|
|
abortClient.on('connectError', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
++retryCount
|
|
|
|
})
|
2014-06-24 10:33:19 +00:00
|
|
|
abortClient.once('connectError', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(true)
|
2014-06-24 10:33:19 +00:00
|
|
|
abortClient.once('destroy', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(retryCount < 3)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
abortClient.destroy()
|
|
|
|
})
|
|
|
|
})
|
2014-06-24 10:33:19 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('reconnect max retries', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const RETRIES = 5
|
2019-08-27 18:17:33 +00:00
|
|
|
const rClient = ldap.createClient({
|
2014-06-26 15:11:07 +00:00
|
|
|
connectTimeout: 100,
|
2019-08-27 18:17:33 +00:00
|
|
|
socketPath: 'an invalid path',
|
2014-06-26 15:11:07 +00:00
|
|
|
reconnect: {
|
|
|
|
failAfter: RETRIES,
|
|
|
|
// Keep the test duration low
|
|
|
|
initialDelay: 10,
|
|
|
|
maxDelay: 100
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
let count = 0
|
2014-06-26 15:11:07 +00:00
|
|
|
rClient.on('connectError', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
count++
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
rClient.on('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(err)
|
|
|
|
t.equal(count, RETRIES)
|
|
|
|
rClient.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-06-26 15:11:07 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('reconnect on server close', function (t) {
|
|
|
|
const clt = ldap.createClient({
|
|
|
|
socketPath: t.context.socketPath,
|
|
|
|
reconnect: true
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
clt.on('setup', function (sclt, cb) {
|
|
|
|
sclt.bind(BIND_DN, BIND_PW, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
clt.once('connect', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(clt._socket)
|
2014-06-27 21:15:04 +00:00
|
|
|
clt.once('connect', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(true, 'successful reconnect')
|
|
|
|
clt.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
|
|
|
|
// Simulate server-side close
|
2019-08-27 21:11:49 +00:00
|
|
|
clt._socket.destroy()
|
|
|
|
})
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('no auto-reconnect on unbind', function (t) {
|
|
|
|
const clt = ldap.createClient({
|
|
|
|
socketPath: t.context.socketPath,
|
|
|
|
reconnect: true
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
clt.on('setup', function (sclt, cb) {
|
|
|
|
sclt.bind(BIND_DN, BIND_PW, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
clt.once('connect', function () {
|
|
|
|
clt.once('connect', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(new Error('client should not reconnect'))
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
clt.once('close', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(true, 'initial close')
|
2014-06-27 21:15:04 +00:00
|
|
|
setImmediate(function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.ok(!clt.connected, 'should not be connected')
|
|
|
|
t.ok(!clt.connecting, 'should not be connecting')
|
|
|
|
clt.destroy()
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
clt.unbind()
|
|
|
|
})
|
|
|
|
})
|
2014-06-27 21:15:04 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('abandon (GH-27)', function (t) {
|
2014-07-09 18:40:17 +00:00
|
|
|
// FIXME: test abandoning a real request
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.abandon(401876543, function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
2011-11-07 22:13:53 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('search timeout (GH-51)', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.context.client.timeout = 250
|
2019-08-27 18:17:33 +00:00
|
|
|
t.context.client.search('dc=timeout', 'objectclass=*', function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
2012-04-17 22:14:06 +00:00
|
|
|
res.on('error', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2011-11-08 21:10:39 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
tap.test('resultError handling', function (t) {
|
2019-08-27 21:11:49 +00:00
|
|
|
const client = t.context.client
|
2019-08-27 18:17:33 +00:00
|
|
|
vasync.pipeline({ funcs: [errSearch, cleanSearch] }, function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
|
|
|
client.removeListener('resultError', error1)
|
|
|
|
client.removeListener('resultError', error2)
|
|
|
|
t.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
function errSearch (_, cb) {
|
|
|
|
client.once('resultError', error1)
|
2019-08-27 18:17:33 +00:00
|
|
|
client.search('cn=busy', {}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
2019-08-27 18:17:33 +00:00
|
|
|
res.once('error', function (error) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(error.name, 'BusyError')
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
2014-07-23 19:44:15 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function cleanSearch (_, cb) {
|
|
|
|
client.on('resultError', error2)
|
2019-08-27 18:17:33 +00:00
|
|
|
client.search(SUFFIX, {}, function (err, res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.error(err)
|
2019-08-27 18:17:33 +00:00
|
|
|
res.once('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.pass()
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
2012-01-20 02:02:10 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
function error1 (error) {
|
2019-08-27 21:11:49 +00:00
|
|
|
t.equal(error.name, 'BusyError')
|
2019-08-27 18:17:33 +00:00
|
|
|
}
|
2012-01-20 02:02:10 +00:00
|
|
|
|
2019-08-27 18:17:33 +00:00
|
|
|
function error2 () {
|
|
|
|
t.fail('should not get error')
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|