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
'use strict'
 
const tap = require('tap')
const warning = require('./deprecations')
const { BerReader } = require('@ldapjs/asn1')
const RDN = require('./rdn')
 
// Silence the standard warning logs. We will test the messages explicitly.
process.removeAllListeners('warning')
 
tap.test('equals', t => {
  t.test('false for non-rdn object', async t => {
    const rdn = new RDN()
    t.equal(rdn.equals({}), false)
  })
 
  t.test('false for size mis-match', async t => {
    const rdn1 = new RDN({ cn: 'foo' })
    const rdn2 = new RDN({ cn: 'foo', sn: 'bar' })
    t.equal(rdn1.equals(rdn2), false)
  })
 
  t.test('false for keys mis-match', async t => {
    const rdn1 = new RDN({ cn: 'foo' })
    const rdn2 = new RDN({ sn: 'bar' })
    t.equal(rdn1.equals(rdn2), false)
  })
 
  t.test('false for value mis-match', async t => {
    const rdn1 = new RDN({ cn: 'foo' })
    const rdn2 = new RDN({ cn: 'bar' })
    t.equal(rdn1.equals(rdn2), false)
  })
 
  t.test('true for match', async t => {
    const rdn1 = new RDN({ cn: 'foo' })
    const rdn2 = new RDN({ cn: 'foo' })
    t.equal(rdn1.equals(rdn2), true)
  })
 
  t.end()
})
 
tap.test('setAttribute', async t => {
  t.test('throws for bad name', async t => {
    const rdn = new RDN()
    t.throws(
      () => rdn.setAttribute({ name: 42 }),
      Error('name must be a string')
    )
 
    t.throws(
      () => rdn.setAttribute({ name: '3cn', value: 'foo' }),
      Error('attribute name must start with an ASCII alpha character or be a numeric OID')
    )
  })
 
  t.test('throws for bad value', async t => {
    const rdn = new RDN()
    t.throws(
      () => rdn.setAttribute({ name: 'cn', value: 42 }),
      Error('value must be a string')
    )
  })
 
  t.test('throws for options', async t => {
    const rdn = new RDN()
    t.throws(
      () => rdn.setAttribute({ name: 'cn', value: 'foo', options: 42 }),
      Error('options must be an object')
    )
  })
 
  t.test('sets an attribute with value', async t => {
    const rdn = new RDN()
    rdn.setAttribute({ name: 'cn', value: 'foo' })
    t.equal(rdn.getValue('cn'), 'foo')
  })
 
  t.test('options generates warning', t => {
    process.on('warning', handler)
    t.teardown(async () => {
      process.removeListener('warning', handler)
      warning.emitted.set('LDAP_DN_DEP_001', false)
    })
 
    const rdn = new RDN()
    rdn.setAttribute({ name: 'cn', value: 'foo', options: { foo: 'bar' } })
 
    function handler (error) {
      t.equal(error.message, 'attribute options is deprecated and are ignored')
      t.end()
    }
  })
 
  t.end()
})
 
tap.test('toString', t => {
  t.test('basic single value', async t => {
    const rdn = new RDN({ cn: 'foo' })
    t.equal(rdn.toString(), 'cn=foo')
  })
 
  t.test('escaped single value', async t => {
    const rdn = new RDN({ cn: ' foo, bar\n' })
    t.equal(rdn.toString(), 'cn=\\20foo\\2c bar\\0a')
  })
 
  t.test('basic multi-value', async t => {
    const rdn = new RDN({ cn: 'foo', sn: 'bar' })
    t.equal(rdn.toString(), 'cn=foo+sn=bar')
  })
 
  t.test('escaped multi-value', async t => {
    const rdn = new RDN({ cn: '#foo', sn: 'bar' })
    t.equal(rdn.toString(), 'cn=\\23foo+sn=bar')
  })
 
  t.test('recognizes encoded string values', async t => {
    const rdn = new RDN({
      cn: '#foo',
      '1.3.6.1.4.1.1466.0': '#04024869'
    })
    t.equal(rdn.toString(), 'cn=\\23foo+1.3.6.1.4.1.1466.0=#04024869')
  })
 
  t.test('encodes BerReader instances', async t => {
    const rdn = new RDN({
      cn: new BerReader(Buffer.from([0x04, 0x03, 0x66, 0x6f, 0x6f]))
    })
    t.equal(rdn.toString(), 'cn=#0403666f6f')
  })
 
  t.test('honors unescaped options', async t => {
    const rdn = new RDN({
      ou: '研发二组'
    })
    t.equal(rdn.toString({ unescaped: true }), 'ou=研发二组')
  })
 
  t.end()
})
 
tap.test('deprecations', t => {
  t.test('format', t => {
    process.on('warning', handler)
    t.teardown(async () => {
      process.removeListener('warning', handler)
      warning.emitted.set('LDAP_DN_DEP_002', false)
    })
 
    const rdn = new RDN({ cn: 'foo' })
    t.equal(rdn.format(), 'cn=foo')
 
    function handler (error) {
      t.equal(error.message, '.format() is deprecated. Use .toString() instead')
      t.end()
    }
  })
 
  t.test('set', t => {
    process.on('warning', handler)
    t.teardown(async () => {
      process.removeListener('warning', handler)
      warning.emitted.set('LDAP_DN_DEP_002', false)
    })
 
    const rdn = new RDN()
    rdn.set('cn', 'foo', { value: 'ignored' })
 
    function handler (error) {
      t.equal(error.message, '.set() is deprecated. Use .setAttribute() instead')
      t.end()
    }
  })
 
  t.end()
})
 
tap.test('#isRdn', t => {
  t.test('true for instance', async t => {
    const rdn = new RDN()
    t.equal(RDN.isRdn(rdn), true)
  })
 
  t.test('false for non-object', async t => {
    t.equal(RDN.isRdn(42), false)
  })
 
  t.test('false for bad object', async t => {
    const input = { bad: 'rdn', 'non-string-value': 42 }
    t.equal(RDN.isRdn(input), false)
  })
 
  t.test('true for rdn-like with name+value keys', async t => {
    const input = { name: 'cn', value: 'foo' }
    t.equal(RDN.isRdn(input), true)
  })
 
  t.test('true for pojo representation', async t => {
    const input = { cn: 'foo', sn: 'bar' }
    t.equal(RDN.isRdn(input), true)
  })
 
  t.test('true for pojo with BerReader', async t => {
    const input = {
      foo: new BerReader(Buffer.from([0x04, 0x03, 0x66, 0x6f, 0x6f]))
    }
    t.equal(RDN.isRdn(input), true)
  })
 
  t.end()
})