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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
'use strict'
 
const tap = require('tap')
const { operations } = require('@ldapjs/protocol')
const filter = require('@ldapjs/filter')
const SearchRequest = require('./search-request')
const { DN } = require('@ldapjs/dn')
const { BerReader, BerWriter } = require('@ldapjs/asn1')
const warning = require('../deprecations')
 
// Silence the standard warning logs. We will test the messages explicitly.
process.removeAllListeners('warning')
 
const {
  searchRequestBytes
} = require('./_fixtures/message-byte-arrays')
 
tap.test('basic', t => {
  t.test('constructor no args', async t => {
    const req = new SearchRequest()
    const pojo = req.pojo
    t.strictSame(pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_SEARCH,
      type: 'SearchRequest',
      baseObject: '',
      scope: 'base',
      derefAliases: SearchRequest.DEREF_ALIASES_NEVER,
      sizeLimit: 0,
      timeLimit: 0,
      typesOnly: false,
      filter: '(objectclass=*)',
      attributes: [],
      controls: []
    })
 
    t.equal(req.type, 'SearchRequest')
  })
 
  t.test('constructor with args', async t => {
    const req = new SearchRequest({
      baseObject: 'cn=foo,dc=example,dc=com',
      scope: SearchRequest.SCOPE_SUBTREE,
      derefAliases: SearchRequest.DEREF_BASE_OBJECT,
      sizeLimit: 1,
      timeLimit: 1,
      typesOnly: true,
      filter: new filter.EqualityFilter({ attribute: 'cn', value: 'foo' }),
      attributes: ['*']
    })
    const pojo = req.pojo
    t.strictSame(pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_SEARCH,
      type: 'SearchRequest',
      baseObject: 'cn=foo,dc=example,dc=com',
      scope: 'subtree',
      derefAliases: SearchRequest.DEREF_BASE_OBJECT,
      sizeLimit: 1,
      timeLimit: 1,
      typesOnly: true,
      filter: '(cn=foo)',
      attributes: ['*'],
      controls: []
    })
  })
 
  t.end()
})
 
tap.test('.attributes', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.strictSame(req.attributes, [])
 
    req.attributes = ['*']
    t.strictSame(req.attributes, ['*'])
  })
 
  t.test('set overwrites current list', async t => {
    const req = new SearchRequest({
      attributes: ['1.1']
    })
    req.attributes = ['1.1', '*', '@foo3-bar.2', 'cn', 'sn;lang-en']
 
    t.strictSame(req.attributes, ['1.1', '*', '@foo3-bar.2', 'cn', 'sn;lang-en'])
  })
 
  t.test('throws if not an array', async t => {
    t.throws(
      () => new SearchRequest({ attributes: '*' }),
      'attributes must be an array of attribute strings'
    )
  })
 
  t.test('throws if array contains non-attribute', async t => {
    const input = [
      '*',
      'not allowed'
    ]
    t.throws(
      () => new SearchRequest({ attributes: input }),
      'attribute must be a valid string'
    )
  })
 
  t.test('supports single character names (issue #2)', async t => {
    const req = new SearchRequest({
      attributes: ['a']
    })
    t.strictSame(req.attributes, ['a'])
  })
 
  t.test('supports multiple attribute options', async t => {
    const req = new SearchRequest({
      attributes: ['abc;lang-en;lang-es']
    })
    t.strictSame(req.attributes, ['abc;lang-en;lang-es'])
  })
 
  t.test('supports range options', async t => {
    const req = new SearchRequest({
      attributes: [
        'a;range=0-*',
        'abc;range=100-200',
        'def;range=0-5;lang-en',
        'ghi;lang-en;range=6-10',
        'jkl;lang-en;range=11-15;lang-es'
      ]
    })
    t.strictSame(req.attributes, [
      'a;range=0-*',
      'abc;range=100-200',
      'def;range=0-5;lang-en',
      'ghi;lang-en;range=6-10',
      'jkl;lang-en;range=11-15;lang-es'
    ])
  })
 
  t.test('throws if array contains an invalid range', async t => {
    const input = ['a;range=*-100']
    t.throws(
      () => new SearchRequest({ attributes: input }),
      'attribute must be a valid string'
    )
  })
 
  t.test('skip empty attribute name', async t => {
    process.on('warning', handler)
    t.teardown(async () => {
      process.removeListener('warning', handler)
      warning.emitted.set('LDAP_ATTRIBUTE_SPEC_ERR_001', false)
    })
 
    const req = new SearchRequest({
      attributes: ['abc', '']
    })
    t.strictSame(req.attributes, ['abc'])
 
    function handler (error) {
      t.equal(error.message, 'received attempt to define attribute with an empty name: attribute skipped.')
      t.end()
    }
  })
 
  t.end()
})
 
tap.test('.baseObject', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.baseObject.toString(), '')
 
    req.baseObject = 'dc=example,dc=com'
    t.equal(req.baseObject.toString(), 'dc=example,dc=com')
 
    req.baseObject = DN.fromString('dc=example,dc=net')
    t.equal(req.baseObject.toString(), 'dc=example,dc=net')
    t.equal(req._dn.toString(), 'dc=example,dc=net')
  })
 
  t.test('throws for non-DN object', async t => {
    const req = new SearchRequest()
    t.throws(
      () => {
        req.baseObject = ['foo']
      },
      'baseObject must be a DN string or DN instance'
    )
  })
 
  t.end()
})
 
tap.test('.derefAliases', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.derefAliases, SearchRequest.DEREF_ALIASES_NEVER)
 
    req.derefAliases = SearchRequest.DEREF_ALWAYS
    t.equal(req.derefAliases, SearchRequest.DEREF_ALWAYS)
  })
 
  t.test('throws for bad value', async t => {
    const req = new SearchRequest()
    t.throws(
      () => {
        req.derefAliases = '0'
      },
      'derefAliases must be set to an integer'
    )
  })
 
  t.end()
})
 
tap.test('.filter', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.filter.toString(), '(objectclass=*)')
 
    req.filter = '(cn=foo)'
    t.equal(req.filter.toString(), '(cn=foo)')
 
    req.filter = new filter.EqualityFilter({ attribute: 'sn', value: 'bar' })
    t.equal(req.filter.toString(), '(sn=bar)')
  })
 
  t.test('throws for bad value', async t => {
    const expected = 'filter must be a string or a FilterString instance'
    const req = new SearchRequest()
    t.throws(
      () => {
        req.filter = ['foo']
      },
      expected
    )
    t.throws(
      () => {
        req.filter = { attribute: 'cn', value: 'foo' }
      },
      expected
    )
  })
 
  t.end()
})
 
tap.test('.scope', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.scopeName, 'base')
    t.equal(req.scope, 0)
 
    req.scope = SearchRequest.SCOPE_SINGLE
    t.equal(req.scopeName, 'single')
    t.equal(req.scope, 1)
 
    req.scope = SearchRequest.SCOPE_SUBTREE
    t.equal(req.scopeName, 'subtree')
    t.equal(req.scope, 2)
 
    req.scope = 'SUB'
    t.equal(req.scopeName, 'subtree')
    t.equal(req.scope, 2)
 
    req.scope = 'base'
    t.equal(req.scopeName, 'base')
    t.equal(req.scope, 0)
  })
 
  t.test('throws for invalid value', async t => {
    const expected = ' is an invalid search scope'
    const req = new SearchRequest()
 
    t.throws(
      () => {
        req.scope = 'nested'
      },
      'nested' + expected
    )
 
    t.throws(
      () => {
        req.scope = 42
      },
      42 + expected
    )
  })
 
  t.end()
})
 
tap.test('.sizeLimit', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.sizeLimit, 0)
 
    req.sizeLimit = 15
    t.equal(req.sizeLimit, 15)
  })
 
  t.test('throws for bad value', async t => {
    const req = new SearchRequest()
    t.throws(
      () => {
        req.sizeLimit = 15.5
      },
      'sizeLimit must be an integer'
    )
  })
 
  t.end()
})
 
tap.test('.timeLimit', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.timeLimit, 0)
 
    req.timeLimit = 15
    t.equal(req.timeLimit, 15)
  })
 
  t.test('throws for bad value', async t => {
    const req = new SearchRequest()
    t.throws(
      () => {
        req.timeLimit = 15.5
      },
      'timeLimit must be an integer'
    )
  })
 
  t.end()
})
 
tap.test('.typesOnly', t => {
  t.test('sets/gets', async t => {
    const req = new SearchRequest()
    t.equal(req.typesOnly, false)
 
    req.typesOnly = true
    t.equal(req.typesOnly, true)
  })
 
  t.test('throws for bad value', async t => {
    const req = new SearchRequest()
    t.throws(
      () => {
        req.typesOnly = 'true'
      },
      'typesOnly must be set to a boolean value'
    )
  })
 
  t.end()
})
 
tap.test('_toBer', t => {
  tap.test('converts instance to BER', async t => {
    const req = new SearchRequest({
      messageId: 2,
      baseObject: 'dc=example,dc=com',
      scope: 'subtree',
      derefAliases: SearchRequest.DEREF_ALIASES_NEVER,
      sizeLimit: 1000,
      timeLimit: 30,
      typesOnly: false,
      filter: '(&(objectClass=person)(uid=jdoe))',
 
      attributes: ['*', '+']
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(searchRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.end()
})
 
tap.test('_pojo', t => {
  t.test('returns a pojo representation', async t => {
    const req = new SearchRequest()
    t.strictSame(req._pojo(), {
      baseObject: '',
      scope: 'base',
      derefAliases: 0,
      sizeLimit: 0,
      timeLimit: 0,
      typesOnly: false,
      filter: '(objectclass=*)',
      attributes: []
    })
  })
 
  t.end()
})
 
tap.test('#parseToPojo', t => {
  t.test('throws if operation incorrect', async t => {
    const reqBuffer = Buffer.from(searchRequestBytes)
    reqBuffer[5] = 0x61
 
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    t.throws(
      () => SearchRequest.parseToPojo(reader),
      Error('found wrong protocol operation: 0x61')
    )
  })
 
  t.test('returns a pojo representation', async t => {
    const reqBuffer = Buffer.from(searchRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = SearchRequest.parseToPojo(reader)
    t.equal(pojo.protocolOp, operations.LDAP_REQ_SEARCH)
    t.equal(pojo.baseObject, 'dc=example,dc=com')
    t.equal(pojo.scope, SearchRequest.SCOPE_SUBTREE)
    t.equal(pojo.derefAliases, SearchRequest.DEREF_ALIASES_NEVER)
    t.equal(pojo.sizeLimit, 1000)
    t.equal(pojo.timeLimit, 30)
    t.equal(pojo.typesOnly, false)
    t.equal(pojo.filter, '(&(objectClass=person)(uid=jdoe))')
    t.strictSame(pojo.attributes, ['*', '+'])
  })
 
  t.end()
})