wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict'
 
const tap = require('tap')
const { getSock, uuid } = require('./utils')
const { SearchResultEntry } = require('@ldapjs/messages')
const Attribute = require('@ldapjs/attribute')
const ldap = require('../lib')
 
function search (t, options, callback) {
  t.context.client.search(t.context.suffix, options, function (err, res) {
    t.error(err)
    t.ok(res)
    let found = false
    res.on('searchEntry', function (entry) {
      t.ok(entry)
      found = true
    })
    res.on('end', function () {
      t.ok(found)
      if (callback) return callback()
      return t.end()
    })
  })
}
 
tap.beforeEach((t) => {
  return new Promise((resolve, reject) => {
    const suffix = `dc=${uuid()}`
    const server = ldap.createServer()
 
    t.context.server = server
    t.context.socketPath = getSock()
    t.context.suffix = suffix
 
    server.on('error', err => {
      server.close(() => reject(err))
    })
 
    server.bind('cn=root', function (req, res, next) {
      res.end()
      return next()
    })
 
    server.search(suffix, function (req, res) {
      const entry = new SearchResultEntry({
        entry: 'cn=foo,' + suffix,
        attributes: Attribute.fromObject({
          objectclass: ['person', 'top'],
          cn: 'Pogo Stick',
          sn: 'Stick',
          givenname: 'ogo',
          mail: uuid() + '@pogostick.org'
        })
      })
 
      if (req.filter.matches(entry.attributes)) {
        res.send(entry)
      }
 
      res.end()
    })
 
    server.listen(t.context.socketPath, function () {
      t.context.client = ldap.createClient({
        socketPath: t.context.socketPath
      })
 
      t.context.client.on('error', (err) => {
        t.context.server.close(() => reject(err))
      })
      t.context.client.on('connectError', (err) => {
        t.context.server.close(() => reject(err))
      })
      t.context.client.on('connect', (socket) => {
        t.context.socket = socket
        resolve()
      })
    })
  })
})
 
tap.afterEach((t) => {
  return new Promise((resolve, reject) => {
    if (!t.context.client) return resolve()
    t.context.client.unbind(() => {
      t.context.server.close((err) => {
        if (err) return reject(err)
        resolve()
      })
    })
  })
})
 
tap.test('Evolution search filter (GH-3)', function (t) {
  // This is what Evolution sends, when searching for a contact 'ogo'. Wow.
  const filter =
    '(|(cn=ogo*)(givenname=ogo*)(sn=ogo*)(mail=ogo*)(member=ogo*)' +
    '(primaryphone=ogo*)(telephonenumber=ogo*)(homephone=ogo*)(mobile=ogo*)' +
    '(carphone=ogo*)(facsimiletelephonenumber=ogo*)' +
    '(homefacsimiletelephonenumber=ogo*)(otherphone=ogo*)' +
    '(otherfacsimiletelephonenumber=ogo*)(internationalisdnnumber=ogo*)' +
    '(pager=ogo*)(radio=ogo*)(telex=ogo*)(assistantphone=ogo*)' +
    '(companyphone=ogo*)(callbackphone=ogo*)(tty=ogo*)(o=ogo*)(ou=ogo*)' +
    '(roomnumber=ogo*)(title=ogo*)(businessrole=ogo*)(managername=ogo*)' +
    '(assistantname=ogo*)(postaladdress=ogo*)(l=ogo*)(st=ogo*)' +
    '(postofficebox=ogo*)(postalcode=ogo*)(c=ogo*)(homepostaladdress=ogo*)' +
    '(mozillahomelocalityname=ogo*)(mozillahomestate=ogo*)' +
    '(mozillahomepostalcode=ogo*)(mozillahomecountryname=ogo*)' +
    '(otherpostaladdress=ogo*)(jpegphoto=ogo*)(usercertificate=ogo*)' +
    '(labeleduri=ogo*)(displayname=ogo*)(spousename=ogo*)(note=ogo*)' +
    '(anniversary=ogo*)(birthdate=ogo*)(mailer=ogo*)(fileas=ogo*)' +
    '(category=ogo*)(calcaluri=ogo*)(calfburl=ogo*)(icscalendar=ogo*))'
 
  return search(t, filter)
})
 
tap.test('GH-49 Client errors on bad attributes', function (t) {
  const searchOpts = {
    filter: 'cn=*ogo*',
    scope: 'one',
    attributes: 'dn'
  }
  return search(t, searchOpts)
})
 
tap.test('GH-55 Client emits connect multiple times', function (t) {
  const c = ldap.createClient({
    socketPath: t.context.socketPath
  })
 
  let count = 0
  c.on('connect', function (socket) {
    t.ok(socket)
    count++
    c.bind('cn=root', 'secret', function (err) {
      t.error(err)
      c.unbind(function () {
        t.equal(count, 1)
        t.end()
      })
    })
  })
})