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
'use strict'
 
const tap = require('tap')
const { operations } = require('@ldapjs/protocol')
const Attribute = require('@ldapjs/attribute')
const { DN } = require('@ldapjs/dn')
const { BerWriter, BerReader } = require('@ldapjs/asn1')
const SearchResultEntry = require('./search-result-entry')
 
const {
  searchResultEntryBytes,
  searchResultEntryNoValuesBytes
} = require('./_fixtures/message-byte-arrays')
 
tap.test('basic', t => {
  t.test('constructor no args', async t => {
    const res = new SearchResultEntry()
    t.strictSame(res.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_RES_SEARCH_ENTRY,
      type: 'SearchResultEntry',
      objectName: '',
      attributes: [],
      controls: []
    })
 
    t.equal(res.type, 'SearchResultEntry')
  })
 
  t.test('constructor with args', async t => {
    const res = new SearchResultEntry({
      objectName: 'dc=example,dc=com',
      attributes: [{ type: 'cn', values: ['foo'] }]
    })
    t.strictSame(res.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_RES_SEARCH_ENTRY,
      type: 'SearchResultEntry',
      objectName: 'dc=example,dc=com',
      attributes: [{ type: 'cn', values: ['foo'] }],
      controls: []
    })
  })
 
  t.end()
})
 
tap.test('.attributes', t => {
  t.test('sets/gets', async t => {
    const res = new SearchResultEntry()
    t.strictSame(res.attributes, [])
 
    res.attributes = [new Attribute({ type: 'cn', values: 'foo' })]
    t.strictSame(res.attributes, [new Attribute({ type: 'cn', values: 'foo' })])
  })
 
  t.test('rejects non-array', async t => {
    const res = new SearchResultEntry()
    t.throws(
      () => {
        res.attributes = { type: 'cn', values: ['foo'] }
      },
      'attrs must be an array'
    )
  })
 
  t.test('rejects non-attribute objects', async t => {
    const res = new SearchResultEntry()
    t.throws(
      () => {
        res.attributes = [{ foo: 'bar' }]
      },
      'attr must be an Attribute instance or Attribute-like object'
    )
  })
 
  t.end()
})
 
tap.test('.objectName', t => {
  t.test('sets/gets', async t => {
    const res = new SearchResultEntry()
    t.equal(res.objectName.toString(), '')
    t.equal(res._dn.toString(), '')
 
    res.objectName = 'cn=foo'
    t.equal(res.objectName.toString(), 'cn=foo')
 
    res.objectName = DN.fromString('sn=bar')
    t.equal(res.objectName.toString(), 'sn=bar')
  })
 
  t.test('throws for invalid value', async t => {
    const res = new SearchResultEntry()
    t.throws(
      () => {
        res.objectName = ['invalid input']
      },
      'objectName must be a DN string or instance of LdapDn'
    )
  })
 
  t.end()
})
 
tap.test('_toBer', t => {
  t.test('converts instance to BER', async t => {
    let res = new SearchResultEntry({
      objectName: 'dc=example,dc=com',
      attributes: [
        { type: 'objectClass', values: ['top', 'domain'] },
        { type: 'dc', values: ['example'] }
      ]
    })
    let writer = new BerWriter()
    res._toBer(writer)
 
    t.equal(
      Buffer.from(searchResultEntryBytes.slice(5)).compare(writer.buffer),
      0
    )
 
    res = new SearchResultEntry({
      objectName: 'dc=example,dc=com',
      attributes: [
        { type: 'objectClass', values: [] },
        { type: 'dc', values: [] }
      ]
    })
    writer = new BerWriter()
    res._toBer(writer)
 
    t.equal(
      Buffer.from(searchResultEntryNoValuesBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.end()
})
 
tap.test('_pojo', t => {
  t.test('returns a pojo representation', async t => {
    const req = new SearchResultEntry({
      objectName: 'cn=foo',
      attributes: [{ type: 'bar', values: ['baz'] }]
    })
    t.strictSame(req._pojo(), {
      objectName: '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(searchResultEntryBytes)
    reqBuffer[5] = 0x61
 
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    t.throws(
      () => SearchResultEntry.parseToPojo(reader),
      Error('found wrong protocol operation: 0x61')
    )
  })
 
  t.test('returns a pojo representation', async t => {
    const reqBuffer = Buffer.from(searchResultEntryBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = SearchResultEntry.parseToPojo(reader)
    t.equal(pojo.protocolOp, operations.LDAP_RES_SEARCH_ENTRY)
    t.equal(pojo.objectName, '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()
})