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
'use strict'
 
const tap = require('tap')
const { operations } = require('@ldapjs/protocol')
const { BerReader, BerWriter } = require('@ldapjs/asn1')
const { DN } = require('@ldapjs/dn')
const CompareRequest = require('./compare-request')
 
const { compareRequestBytes } = require('./_fixtures/message-byte-arrays')
 
tap.test('basic', t => {
  t.test('constructor no args', async t => {
    const req = new CompareRequest()
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_COMPARE,
      type: 'CompareRequest',
      entry: null,
      attribute: '',
      value: '',
      controls: []
    })
  })
 
  t.test('constructor with args', async t => {
    const req = new CompareRequest({
      entry: 'dn=foo,dc=example,dc=com',
      attribute: 'foo',
      value: 'bar'
    })
    t.strictSame(req.pojo, {
      messageId: 1,
      protocolOp: operations.LDAP_REQ_COMPARE,
      type: 'CompareRequest',
      entry: 'dn=foo,dc=example,dc=com',
      attribute: 'foo',
      value: 'bar',
      controls: []
    })
  })
 
  t.test('.type', async t => {
    const req = new CompareRequest()
    t.equal(req.type, 'CompareRequest')
  })
 
  t.test('.dn', async t => {
    const req = new CompareRequest({ entry: 'dn=foo,dc=example,dc=com' })
    t.equal(req.dn.toString(), 'dn=foo,dc=example,dc=com')
  })
 
  t.end()
})
 
tap.test('.attribute', t => {
  t.test('gets and sets', async t => {
    const req = new CompareRequest()
    t.equal(req.attribute, '')
    req.attribute = 'foo'
    t.equal(req.attribute, 'foo')
  })
 
  t.end()
})
 
tap.test('.entry', t => {
  t.test('gets and sets', async t => {
    const req = new CompareRequest()
    t.equal(req.entry, null)
 
    req.entry = 'cn=foo'
    t.equal(req.entry.toString(), 'cn=foo')
 
    req.entry = DN.fromString('sn=bar')
    t.equal(req.entry.toString(), 'sn=bar')
  })
 
  t.test('throws for bad value', async t => {
    const req = new CompareRequest()
    t.throws(
      () => {
        req.entry = { cn: 'foo' }
      },
      'entry must be a valid DN string or instance of LdapDn'
    )
  })
 
  t.end()
})
 
tap.test('.value', t => {
  t.test('gets and sets', async t => {
    const req = new CompareRequest()
    t.equal(req.value, '')
    req.value = 'foo'
    t.equal(req.value, 'foo')
  })
 
  t.end()
})
 
tap.test('_toBer', t => {
  t.test('converts instance to BER', async t => {
    const req = new CompareRequest({
      messageId: 2,
      entry: 'uid=jdoe,ou=People,dc=example,dc=com',
      attribute: 'employeeType',
      value: 'salaried'
    })
    const writer = new BerWriter()
    req._toBer(writer)
 
    t.equal(
      Buffer.from(compareRequestBytes.slice(5)).compare(writer.buffer),
      0
    )
  })
 
  t.end()
})
 
tap.test('_pojo', t => {
  t.test('returns a pojo representation', async t => {
    const req = new CompareRequest({
      entry: 'cn=foo',
      attribute: 'bar',
      value: 'baz'
    })
    t.strictSame(req._pojo(), {
      entry: 'cn=foo',
      attribute: 'bar',
      value: 'baz'
    })
  })
 
  t.end()
})
 
tap.test('#parseToPojo', t => {
  t.test('throws if operation incorrect', async t => {
    const reqBuffer = Buffer.from(compareRequestBytes)
    reqBuffer[5] = 0x61
 
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    t.throws(
      () => CompareRequest.parseToPojo(reader),
      Error('found wrong protocol operation: 0x61')
    )
  })
 
  t.test('returns a pojo representation', async t => {
    const reqBuffer = Buffer.from(compareRequestBytes)
    const reader = new BerReader(reqBuffer)
    reader.readSequence()
    reader.readInt()
 
    const pojo = CompareRequest.parseToPojo(reader)
    t.equal(pojo.protocolOp, operations.LDAP_REQ_COMPARE)
    t.equal(pojo.entry, 'uid=jdoe,ou=People,dc=example,dc=com')
    t.equal(pojo.attribute, 'employeeType')
    t.equal(pojo.value, 'salaried')
  })
 
  t.end()
})