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
'use strict'
 
const tap = require('tap')
const { operations } = require('@ldapjs/protocol')
const ExtensionRequest = require('./extension-request')
 
const {
  extensionCancelRequestBytes,
  extensionNameAndValueRequestBytes,
  extensionNameAndNoValueRequestBytes,
  extensionChangePasswordRequestBytes,
  extensionChangePasswordWithNewPasswordBytes
} = require('./_fixtures/message-byte-arrays')
const { BerReader, BerWriter } = require('@ldapjs/asn1')
 
tap.test('basic', t => {
  t.test('constructor no args', async t => {
    const req = new ExtensionRequest()
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_EXTENSION,
      type: 'ExtensionRequest',
      requestName: '',
      requestValue: undefined,
      controls: []
    })
  })
 
  t.test('constructor with args', async t => {
    const req = new ExtensionRequest({
      requestName: 'foo',
      requestValue: 'bar'
    })
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_EXTENSION,
      type: 'ExtensionRequest',
      requestName: 'foo',
      requestValue: 'bar',
      controls: []
    })
  })
 
  t.test('.type', async t => {
    const req = new ExtensionRequest()
    t.equal(req.type, 'ExtensionRequest')
  })
 
  t.test('.dn', async t => {
    const req = new ExtensionRequest({ requestName: 'foo' })
    t.equal(req.dn, 'foo')
  })
 
  t.test('#recognizedOIDs returns map', async t => {
    const oids = ExtensionRequest.recognizedOIDs()
    t.type(oids, 'Map')
  })
 
  t.end()
})
 
tap.test('.name', t => {
  t.test('gets and sets', async t => {
    const req = new ExtensionRequest()
    t.equal(req.requestName, '')
    req.requestName = 'foo'
    t.equal(req.requestName, 'foo')
  })
 
  t.end()
})
 
tap.test('.value', t => {
  t.test('gets and sets', async t => {
    const req = new ExtensionRequest()
    t.equal(req.requestValue, undefined)
    req.requestValue = 'foo'
    t.equal(req.requestValue, 'foo')
  })
 
  t.end()
})
 
tap.test('_toBer', t => {
  t.test('converts simple instance to BER', async t => {
    const req = new ExtensionRequest({
      requestName: '1.3.6.1.1',
      requestValue: 'foobar'
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(extensionNameAndValueRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.test('converts a simple instance with no value to BER', async t => {
    const req = new ExtensionRequest({ requestName: '1.3.6.1.1' })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(extensionNameAndNoValueRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.test('converts a cancel request', async t => {
    const req = new ExtensionRequest({
      requestName: '1.3.6.1.1.8',
      requestValue: 1
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(extensionCancelRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.test('converts a modify password request with userIdentity and oldPassword', async t => {
    const req = new ExtensionRequest({
      requestName: '1.3.6.1.4.1.4203.1.11.1',
      requestValue: {
        userIdentity: 'uid=jdoe,ou=People,dc=example,dc=com',
        oldPassword: 'secret123'
      }
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(extensionChangePasswordRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.test('converts a modify password request with newPassword', async t => {
    const req = new ExtensionRequest({
      requestName: '1.3.6.1.4.1.4203.1.11.1',
      requestValue: {
        newPassword: 'secret123'
      }
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(extensionChangePasswordWithNewPasswordBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.end()
})
 
tap.test('_pojo', t => {
  t.test('returns a pojo representation', async t => {
    const req = new ExtensionRequest({
      requestName: 'foo',
      requestValue: 'bar'
    })
    t.strictSame(req._pojo(), {
      requestName: 'foo',
      requestValue: 'bar'
    })
  })
 
  t.end()
})
 
tap.test('#parseToPojo', t => {
  t.test('throws if operation incorrect', async t => {
    const reqBuffer = Buffer.from(extensionNameAndValueRequestBytes)
    reqBuffer[5] = 0x61
 
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    t.throws(
      () => ExtensionRequest.parseToPojo(reader),
      Error('found wrong protocol operation: 0x61')
    )
  })
 
  t.test('returns a pojo for a name only operation', async t => {
    const reqBuffer = Buffer.from(extensionNameAndNoValueRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = ExtensionRequest.parseToPojo(reader)
    t.equal(pojo.requestName, '1.3.6.1.1')
    t.equal(pojo.requestValue, undefined)
  })
 
  t.test('returns a pojo for simple string name and value operation', async t => {
    const reqBuffer = Buffer.from(extensionNameAndValueRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = ExtensionRequest.parseToPojo(reader)
    t.equal(pojo.requestName, '1.3.6.1.1')
    t.equal(pojo.requestValue, 'foobar')
  })
 
  t.test('returns pojo for a cancel request', async t => {
    const reqBuffer = Buffer.from(extensionCancelRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = ExtensionRequest.parseToPojo(reader)
    t.equal(pojo.requestName, '1.3.6.1.1.8')
    t.equal(pojo.requestValue, 1)
  })
 
  t.test('returns a pojo for a modify password with user and old pass', async t => {
    const reqBuffer = Buffer.from(extensionChangePasswordRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = ExtensionRequest.parseToPojo(reader)
    t.equal(pojo.requestName, '1.3.6.1.4.1.4203.1.11.1')
    t.strictSame(pojo.requestValue, {
      userIdentity: 'uid=jdoe,ou=People,dc=example,dc=com',
      oldPassword: 'secret123',
      newPassword: undefined
    })
  })
 
  t.test('returns a pojo for a modify password new pass', async t => {
    const reqBuffer = Buffer.from(extensionChangePasswordWithNewPasswordBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = ExtensionRequest.parseToPojo(reader)
    t.equal(pojo.requestName, '1.3.6.1.4.1.4203.1.11.1')
    t.strictSame(pojo.requestValue, {
      userIdentity: undefined,
      oldPassword: undefined,
      newPassword: 'secret123'
    })
  })
 
  t.end()
})