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
'use strict'
 
const tap = require('tap')
const Attribute = require('@ldapjs/attribute')
const { BerReader, BerWriter } = require('@ldapjs/asn1')
const { DN } = require('@ldapjs/dn')
const AddRequest = require('./add-request')
 
const { addRequestBytes } = require('./_fixtures/message-byte-arrays')
 
tap.test('basic', t => {
  t.test('constructor no args', async t => {
    const req = new AddRequest()
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: 0x68,
      type: 'AddRequest',
      entry: null,
      attributes: [],
      controls: []
    })
  })
 
  t.test('constructor with args', async t => {
    const req = new AddRequest({
      entry: 'dn=foo,dc=example,dc=com',
      attributes: [{
        type: 'cn',
        values: ['foo']
      }]
    })
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: 0x68,
      type: 'AddRequest',
      entry: 'dn=foo,dc=example,dc=com',
      attributes: [{
        type: 'cn',
        values: ['foo']
      }],
      controls: []
    })
  })
 
  t.end()
})
 
tap.test('.attributes', t => {
  t.test('returns a copy of the attributes', async t => {
    const inputAttrs = [{ type: 'cn', values: ['foo'] }]
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: inputAttrs
    })
    const attrs = req.attributes
    t.not(attrs, inputAttrs)
    t.equal(attrs.length, inputAttrs.length)
  })
 
  t.test('replaces attributes list', async t => {
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: [new Attribute({ type: 'cn', values: ['foo'] })]
    })
    t.strictSame(req.attributes[0].pojo, { type: 'cn', values: ['foo'] })
 
    req.attributes = [new Attribute({ type: 'sn', values: ['bar'] })]
    t.strictSame(req.attributes[0].pojo, { type: 'sn', values: ['bar'] })
  })
 
  t.test('throws if not an array', async t => {
    const input = { type: 'cn', values: ['foo'] }
    t.throws(
      () => new AddRequest({ entry: 'cn=foo', attributes: input }),
      Error('attrs must be an array')
    )
  })
 
  t.test('throws if attribute is invalid', async t => {
    const input = [{ type: 42, values: ['foo'] }]
    t.throws(
      () => new AddRequest({ entry: 'cn=foo', attributes: input }),
      Error('attr must be an Attribute instance or Attribute-like object')
    )
  })
 
  t.end()
})
 
tap.test('.entry', t => {
  t.test('gets and sets', async t => {
    let req = new AddRequest({ entry: 'cn=foo' })
    t.equal(req.entry.toString(), 'cn=foo')
    t.equal(req._dn.toString(), 'cn=foo')
 
    req.entry = 'sn=bar'
    t.equal(req.entry.toString(), 'sn=bar')
    t.equal(req._dn.toString(), 'sn=bar')
 
    req.entry = DN.fromString('cn=baz')
    t.equal(req.entry.toString(), 'cn=baz')
 
    req = new AddRequest()
    t.equal(req.entry, null)
  })
 
  t.test('throws for bad value', async t => {
    const req = new AddRequest()
    t.throws(
      () => {
        req.entry = { cn: 'foo' }
      },
      'entry must be a valid DN string or instance of LdapDn'
    )
  })
 
  t.end()
})
 
tap.test('addAttribute', t => {
  t.test('throws for invalid input', async t => {
    const req = new AddRequest({ entry: 'cn=foo' })
    t.throws(
      () => req.addAttribute({ type: 'foo', values: ['foo'] }),
      Error('attr must be an instance of Attribute')
    )
  })
 
  t.test('adds an attribute', async t => {
    const req = new AddRequest({ entry: 'cn=foo' })
    req.addAttribute(new Attribute({ type: 'bar', values: ['baz'] }))
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: 0x68,
      type: 'AddRequest',
      entry: 'cn=foo',
      attributes: [{
        type: 'bar',
        values: ['baz']
      }],
      controls: []
    })
  })
 
  t.end()
})
 
tap.test('attributeNames', t => {
  t.test('returns the names list', async t => {
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: [
        new Attribute({ type: 'bar' }),
        new Attribute({ type: 'baz' }),
        new Attribute({ type: 'foobar' }),
        new Attribute({ type: 'barfoo' })
      ]
    })
    t.strictSame(req.attributeNames(), [
      'bar',
      'baz',
      'foobar',
      'barfoo'
    ])
  })
 
  t.end()
})
 
tap.test('getAttribute', t => {
  t.test('throws for invalid parameter', async t => {
    const req = new AddRequest({ entry: 'cn=foo' })
    t.throws(
      () => req.getAttribute(42),
      Error('attributeName must be a string')
    )
  })
 
  t.test('returns null for not found', async t => {
    const req = new AddRequest({ entry: 'cn=foo' })
    t.equal(req.getAttribute('bar'), null)
  })
 
  t.test('returns the correct attribute', async t => {
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: [
        new Attribute({ type: 'bar' }),
        new Attribute({ type: 'baz', values: ['baz', 'baz', 'baz'] }),
        new Attribute({ type: 'foobar' }),
        new Attribute({ type: 'barfoo' })
      ]
    })
    const attr = req.getAttribute('baz')
    t.strictSame(attr.pojo, {
      type: 'baz',
      values: ['baz', 'baz', 'baz']
    })
  })
 
  t.end()
})
 
tap.test('indexOf', t => {
  t.test('throws for invalid parameter', async t => {
    const req = new AddRequest({ entry: 'cn=foo' })
    t.throws(
      () => req.indexOf(42),
      Error('attributeName must be a string')
    )
  })
 
  t.test('finds an attribute', async t => {
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: [
        new Attribute({ type: 'bar' }),
        new Attribute({ type: 'baz' }),
        new Attribute({ type: 'foobar' }),
        new Attribute({ type: 'barfoo' })
      ]
    })
    t.equal(req.indexOf('foobar'), 2)
  })
 
  t.test('returns for not found', async t => {
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: [
        new Attribute({ type: 'bar' }),
        new Attribute({ type: 'baz' }),
        new Attribute({ type: 'foobar' }),
        new Attribute({ type: 'barfoo' })
      ]
    })
    t.equal(req.indexOf('zifbang'), -1)
  })
 
  t.end()
})
 
tap.test('_toBer', t => {
  tap.test('converts instance to BER', async t => {
    const req = new AddRequest({
      entry: 'dc=example,dc=com',
      attributes: [
        new Attribute({ type: 'objectclass', values: ['top', 'domain'] }),
        new Attribute({ type: 'dc', values: ['example'] })
      ]
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(addRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.end()
})
 
tap.test('_pojo', t => {
  t.test('returns a pojo representation', async t => {
    const req = new AddRequest({
      entry: 'cn=foo',
      attributes: [{ type: 'bar', values: ['baz'] }]
    })
    t.strictSame(req._pojo(), {
      entry: 'cn=foo',
      attributes: [{
        type: 'bar',
        values: ['baz']
      }]
    })
  })
 
  t.end()
})
 
tap.test('#parseToPojo', t => {
  t.test('throws if operation incorrect', async t => {
    const reqBuffer = Buffer.from(addRequestBytes)
    reqBuffer[5] = 0x61
 
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    t.throws(
      () => AddRequest.parseToPojo(reader),
      Error('found wrong protocol operation: 0x61')
    )
  })
 
  t.test('returns a pojo representation', async t => {
    const reqBuffer = Buffer.from(addRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = AddRequest.parseToPojo(reader)
    t.equal(pojo.protocolOp, 0x68)
    t.equal(pojo.entry, 'dc=example,dc=com')
    t.strictSame(pojo.attributes[0].pojo, {
      type: 'objectclass',
      values: ['top', 'domain']
    })
    t.strictSame(pojo.attributes[1].pojo, {
      type: 'dc',
      values: ['example']
    })
  })
 
  t.end()
})