Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit c9be815

Browse files
committed
Use a random TCP port for testing timeouts/rejects
This fixes a test failure when the host has something listening on the LDAP TCP port. Previously, the test cases would assume that the port was free and they would not connect. For #612
1 parent e14cf69 commit c9be815

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"verror": "^1.8.1"
2828
},
2929
"devDependencies": {
30+
"get-port": "^5.1.1",
3031
"husky": "^3.0.4",
3132
"snazzy": "^8.0.0",
3233
"standard": "^14.0.2",

test/client.test.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const assert = require('assert')
55
const tap = require('tap')
66
const uuid = require('uuid')
77
const vasync = require('vasync')
8+
const getPort = require('get-port')
89
const { getSock } = require('./utils')
910
const ldap = require('../lib')
1011
const { Attribute, Change } = ldap
@@ -1495,40 +1496,44 @@ tap.test('resultError handling', function (t) {
14951496
})
14961497

14971498
tap.test('connection refused', function (t) {
1498-
const client = ldap.createClient({
1499-
url: 'ldap://0.0.0.0'
1500-
})
1499+
getPort().then(function (unusedPortNumber) {
1500+
const client = ldap.createClient({
1501+
url: `ldap://0.0.0.0:${unusedPortNumber}`
1502+
})
15011503

1502-
client.bind('cn=root', 'secret', function (err, res) {
1503-
t.true(err)
1504-
t.type(err, Error)
1505-
t.equals(err.code, 'ECONNREFUSED')
1506-
t.false(res)
1507-
t.end()
1504+
client.bind('cn=root', 'secret', function (err, res) {
1505+
t.true(err)
1506+
t.type(err, Error)
1507+
t.equals(err.code, 'ECONNREFUSED')
1508+
t.false(res)
1509+
t.end()
1510+
})
15081511
})
15091512
})
15101513

15111514
tap.test('connection timeout', function (t) {
1512-
const client = ldap.createClient({
1513-
url: 'ldap://example.org',
1514-
connectTimeout: 1,
1515-
timeout: 1
1516-
})
1515+
getPort().then(function (unusedPortNumber) {
1516+
const client = ldap.createClient({
1517+
url: `ldap://example.org:${unusedPortNumber}`,
1518+
connectTimeout: 1,
1519+
timeout: 1
1520+
})
15171521

1518-
var done = false
1522+
var done = false
15191523

1520-
setTimeout(function () {
1521-
if (!done) {
1522-
throw new Error('LDAPJS waited for the server for too long')
1523-
}
1524-
}, 2000)
1525-
1526-
client.bind('cn=root', 'secret', function (err, res) {
1527-
t.true(err)
1528-
t.type(err, Error)
1529-
t.equals(err.message, 'connection timeout')
1530-
done = true
1531-
t.false(res)
1532-
t.end()
1524+
setTimeout(function () {
1525+
if (!done) {
1526+
throw new Error('LDAPJS waited for the server for too long')
1527+
}
1528+
}, 2000)
1529+
1530+
client.bind('cn=root', 'secret', function (err, res) {
1531+
t.true(err)
1532+
t.type(err, Error)
1533+
t.equals(err.message, 'connection timeout')
1534+
done = true
1535+
t.false(res)
1536+
t.end()
1537+
})
15331538
})
15341539
})

0 commit comments

Comments
 (0)