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
'use strict'
 
const tap = require('tap')
const {
  modifyRequestBytes
} = require('./_fixtures/message-byte-arrays')
const { operations } = require('@ldapjs/protocol')
const { BerReader } = require('@ldapjs/asn1')
const Attribute = require('@ldapjs/attribute')
const Change = require('@ldapjs/change')
const ModifyRequest = require('./modify-request')
 
tap.test('constructor', t => {
  t.test('with empty params', async t => {
    const req = new ModifyRequest()
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_MODIFY,
      type: 'ModifyRequest',
      object: null,
      changes: [],
      controls: []
    })
  })
 
  t.test('with invalid changes options', async t => {
    t.throws(
      () => new ModifyRequest({ changes: 'foo' }),
      Error('changes must be an array')
    )
  })
 
  t.end()
})
 
tap.test('.changes', t => {
  t.test('gets a copy of the set of changes', async t => {
    const changes = [new Change({
      modification: new Attribute()
    })]
    const req = new ModifyRequest({ changes })
    const found = req.changes
    t.not(changes, found)
    t.equal(changes.length, 1)
    t.equal(Change.isChange(changes[0]), true)
  })
 
  t.test('set throws for non-array', async t => {
    const req = new ModifyRequest()
    t.throws(
      () => {
        req.changes = 42
      },
      Error('changes must be an array')
    )
  })
 
  t.test('throws for non-change in array', async t => {
    const req = new ModifyRequest()
    t.throws(
      () => {
        req.changes = [42]
      },
      Error('change must be an instance of Change or a Change-like object')
    )
  })
 
  t.test('converts change-likes to changes', async t => {
    const req = new ModifyRequest()
    req.changes = [{
      operation: 'add',
      modification: {
        type: 'cn',
        values: ['foo']
      }
    }]
    t.equal(req.changes.length, 1)
    t.equal(Object.prototype.toString.call(req.changes[0]), '[object LdapChange]')
  })
 
  t.end()
})
 
tap.test('.object', t => {
  t.test('gets and sets', async t => {
    const req = new ModifyRequest()
    t.equal(req.object, null)
    req.object = 'foo'
    t.equal(req.object, 'foo')
    t.equal(req.dn, 'foo')
  })
 
  t.end()
})
 
tap.test('_toBer', t => {
  t.test('serializes to ber', async t => {
    const req = new ModifyRequest({
      messageId: 2,
      object: 'uid=jdoe,ou=People,dc=example,dc=com',
      changes: [
        new Change({
          operation: 'delete',
          modification: new Attribute({
            type: 'givenName',
            values: ['John']
          })
        }),
        new Change({
          operation: 'add',
          modification: new Attribute({
            type: 'givenName',
            values: ['Jonathan']
          })
        }),
        new Change({
          operation: 'replace',
          modification: new Attribute({
            type: 'cn',
            values: ['Jonathan Doe']
          })
        })
      ]
    })
    const expected = Buffer.from(modifyRequestBytes)
    const ber = req.toBer()
    t.equal(expected.compare(ber.buffer), 0)
  })
 
  t.end()
})
 
tap.test('_pojo', t => {
  t.test('serializes to plain object', async t => {
    const req = new ModifyRequest({
      object: 'foo',
      changes: [
        new Change({
          modification: new Attribute({
            type: 'cn',
            values: ['bar']
          })
        })
      ]
    })
    t.strictSame(req._pojo(), {
      object: 'foo',
      changes: [{
        operation: 'add',
        modification: {
          type: 'cn',
          values: ['bar']
        }
      }]
    })
  })
 
  t.end()
})
 
tap.test('#parseToPojo', t => {
  t.test('throws for wrong op', async t => {
    const bytes = Buffer.from(modifyRequestBytes.slice(6))
    bytes[0] = 0x61
    const reader = new BerReader(bytes)
    t.throws(
      () => ModifyRequest.parseToPojo(reader),
      Error('found wrong protocol operation: 0x61')
    )
  })
 
  t.test('parses bytes to an object', async t => {
    const bytes = Buffer.from(modifyRequestBytes.slice(6))
    const reader = new BerReader(bytes)
    const pojo = ModifyRequest.parseToPojo(reader)
    t.type(pojo, 'Object')
    t.strictSame(pojo, {
      protocolOp: operations.LDAP_REQ_MODIFY,
      object: 'uid=jdoe,ou=People,dc=example,dc=com',
      changes: [
        {
          operation: 'delete',
          modification: {
            type: 'givenName',
            values: ['John']
          }
        },
        {
          operation: 'add',
          modification: {
            type: 'givenName',
            values: ['Jonathan']
          }
        },
        {
          operation: 'replace',
          modification: {
            type: 'cn',
            values: ['Jonathan Doe']
          }
        }
      ]
    })
  })
 
  t.end()
})