This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathldap-message.test.js
315 lines (264 loc) · 7.23 KB
/
ldap-message.test.js
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
'use strict'
const tap = require('tap')
const { BerReader } = require('@ldapjs/asn1')
const warning = require('./deprecations')
const { Control } = require('@ldapjs/controls')
const LdapMessage = require('./ldap-message')
// Silence the standard warning logs. We will test the messages explicitly.
process.removeAllListeners('warning')
const {
abandonRequestBytes,
bindRequestBytes,
deleteRequestBytes
} = require('./messages/_fixtures/message-byte-arrays')
tap.test('constructor', t => {
t.test('no args', async t => {
const message = new LdapMessage()
t.strictSame(message.pojo, {
messageId: 1,
protocolOp: undefined,
type: 'LdapMessage',
controls: []
})
})
t.test('all options supplied', t => {
process.on('warning', handler)
t.teardown(async () => {
process.removeListener('warning', handler)
warning.emitted.set('LDAP_MESSAGE_DEP_001', false)
})
const message = new LdapMessage({
messageID: 10,
protocolOp: 0x01,
controls: [new Control({ type: 'foo', value: 'foo' })]
})
t.strictSame(message.pojo, {
messageId: 10,
protocolOp: 0x01,
type: 'LdapMessage',
controls: [{
type: 'foo',
value: 'foo',
criticality: false
}]
})
function handler (error) {
t.equal(error.message, 'messageID is deprecated. Use messageId instead.')
t.end()
}
})
t.end()
})
tap.test('misc', t => {
t.test('toStringTag is correct', async t => {
const message = new LdapMessage()
t.equal(Object.prototype.toString.call(message), '[object LdapMessage]')
})
t.test('dn returns _dn', async t => {
class Foo extends LdapMessage {
get _dn () {
return 'foo'
}
}
const message = new Foo()
t.equal(message.dn, 'foo')
})
t.test('protocolOp returns code', async t => {
const message = new LdapMessage({ protocolOp: 1 })
t.equal(message.protocolOp, 1)
})
t.test('json emits warning', t => {
process.on('warning', handler)
t.teardown(async () => {
process.removeListener('warning', handler)
warning.emitted.set('LDAP_MESSAGE_DEP_002', false)
})
const message = new LdapMessage()
t.ok(message.json)
function handler (error) {
t.equal(
error.message,
'The .json property is deprecated. Use .pojo instead.'
)
t.end()
}
})
t.test('toString returns JSON', async t => {
const message = new LdapMessage()
const expected = JSON.stringify(message.pojo)
t.equal(message.toString(), expected)
})
t.end()
})
tap.test('.controls', t => {
t.test('sets/gets', async t => {
const req = new LdapMessage()
t.strictSame(req.controls, [])
req.controls = [new Control()]
t.strictSame(req.pojo, {
messageId: 1,
protocolOp: undefined,
type: 'LdapMessage',
controls: [{
type: '',
value: null,
criticality: false
}]
})
})
t.test('rejects for non-array', async t => {
const req = new LdapMessage()
t.throws(
() => {
req.controls = {}
},
'controls must be an array'
)
})
t.test('rejects if array item is not a control', async t => {
const req = new LdapMessage()
t.throws(
() => {
req.controls = ['foo']
},
'control must be an instance of LdapControl'
)
})
t.end()
})
tap.test('.id', t => {
t.test('sets/gets', async t => {
const req = new LdapMessage()
t.equal(req.id, 1)
req.id = 2
t.equal(req.id, 2)
t.equal(req.messageId, 2)
req.messageId = 3
t.equal(req.id, 3)
})
t.test('throws if not an integer', async t => {
const req = new LdapMessage()
t.throws(
() => {
req.id = 1.5
},
'id must be an integer'
)
})
t.test('get messageID is deprecated', t => {
process.on('warning', handler)
t.teardown(async () => {
process.removeListener('warning', handler)
warning.emitted.set('LDAP_MESSAGE_DEP_001', false)
})
const message = new LdapMessage()
t.ok(message.messageID)
function handler (error) {
t.equal(
error.message,
'messageID is deprecated. Use messageId instead.'
)
t.end()
}
})
t.test('set messageID is deprecated', t => {
process.on('warning', handler)
t.teardown(async () => {
process.removeListener('warning', handler)
warning.emitted.set('LDAP_MESSAGE_DEP_001', false)
})
const message = new LdapMessage()
message.messageID = 2
function handler (error) {
t.equal(
error.message,
'messageID is deprecated. Use messageId instead.'
)
t.end()
}
})
t.end()
})
tap.test('toBer', t => {
t.test('throws for bad subclass', async t => {
class Foo extends LdapMessage {
}
const message = new Foo()
t.throws(
() => message.toBer(),
Error('LdapMessage does not implement _toBer')
)
})
t.test('converts BindRequest to BER', async t => {
const reqBuffer = Buffer.from(bindRequestBytes)
const reader = new BerReader(reqBuffer)
const message = LdapMessage.parse(reader)
const ber = message.toBer()
t.equal('[object BerReader]', Object.prototype.toString.call(ber))
t.equal(reqBuffer.compare(ber.buffer), 0)
})
t.test('converts DeleteRequest to BER', async t => {
const reqBuffer = Buffer.from(deleteRequestBytes)
const reader = new BerReader(reqBuffer)
const message = LdapMessage.parse(reader)
const ber = message.toBer()
t.equal(reqBuffer.compare(ber.buffer), 0)
})
t.end()
})
tap.test('#parse', t => {
t.test('parses an abandon request', async t => {
const reader = new BerReader(Buffer.from(abandonRequestBytes))
const message = LdapMessage.parse(reader)
t.strictSame(message.pojo, {
messageId: 6,
protocolOp: 0x50,
type: 'AbandonRequest',
abandonId: 5,
controls: []
})
})
t.test('parses a bind request', async t => {
const reader = new BerReader(Buffer.from(bindRequestBytes))
const message = LdapMessage.parse(reader)
t.strictSame(message.pojo, {
messageId: 1,
protocolOp: 0x60,
type: 'BindRequest',
version: 3,
name: 'uid=admin,ou=system',
authenticationType: 'simple',
credentials: 'secret',
controls: []
})
t.equal(message.name, 'uid=admin,ou=system')
})
t.test('parses a delete request with controls', async t => {
const reader = new BerReader(Buffer.from(deleteRequestBytes))
const message = LdapMessage.parse(reader)
// We need to parse the JSON representation because stringSame will return
// false when comparing a plain object to an instance of Control.
t.strictSame(JSON.parse(message.toString()), {
messageId: 5,
protocolOp: 0x4a,
type: 'DeleteRequest',
entry: 'dc=example,dc=com',
controls: [{
type: '1.2.840.113556.1.4.805',
criticality: true,
value: null
}]
})
})
t.end()
})
tap.test('#parseToPojo', t => {
t.test('throws because not implemented', async t => {
const expected = Error('Use LdapMessage.parse, or a specific message type\'s parseToPojo, instead.')
t.throws(
() => LdapMessage.parseToPojo(),
expected
)
})
t.end()
})