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
'use strict'
 
const tap = require('tap')
const { BerWriter } = require('@ldapjs/asn1')
const Control = require('./control')
 
tap.test('constructor', t => {
  t.test('new no args', function (t) {
    t.ok(new Control())
    t.equal(Object.prototype.toString.call(new Control()), '[object LdapControl]')
    t.end()
  })
 
  t.test('new with args', function (t) {
    const c = new Control({
      type: '2.16.840.1.113730.3.4.2',
      criticality: true
    })
    t.ok(c)
    t.equal(c.type, '2.16.840.1.113730.3.4.2')
    t.ok(c.criticality)
    t.end()
  })
 
  t.end()
})
 
tap.test('pojo', t => {
  t.test('passes through _pojo', async t => {
    class Foo extends Control {
      _pojo (obj) {
        obj.foo = 'foo'
      }
    }
    const control = new Foo()
    t.strictSame(control.pojo, {
      type: '',
      value: null,
      criticality: false,
      foo: 'foo'
    })
  })
 
  t.test('returns basic object', async t => {
    const control = new Control({ type: '1.2.3', criticality: false, value: 'foo' })
    t.strictSame(control.pojo, {
      type: '1.2.3',
      value: 'foo',
      criticality: false
    })
  })
 
  t.end()
})
 
tap.test('toBer', t => {
  t.test('converts empty instance to BER', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString('')
    target.writeBoolean(false)
    target.endSequence()
 
    const control = new Control()
    const ber = control.toBer()
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.test('converts instance to BER', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString('2.16.840.1.113730.3.4.2')
    target.writeBoolean(true)
    target.writeString('foo')
    target.endSequence()
 
    const control = new Control({
      type: '2.16.840.1.113730.3.4.2',
      criticality: true,
      value: Buffer.from('foo', 'utf8')
    })
    const ber = control.toBer()
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.test('converts instance to BER (side effect manner)', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString('2.16.840.1.113730.3.4.2')
    target.writeBoolean(true)
    target.writeString('foo')
    target.endSequence()
 
    const control = new Control({
      type: '2.16.840.1.113730.3.4.2',
      criticality: true,
      value: Buffer.from('foo', 'utf8')
    })
    const ber = new BerWriter()
    control.toBer(ber)
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.test('converts instance to BER with string value', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString('2.16.840.1.113730.3.4.2')
    target.writeBoolean(true)
    target.writeString('foo')
    target.endSequence()
 
    const control = new Control({
      type: '2.16.840.1.113730.3.4.2',
      criticality: true,
      value: 'foo'
    })
    const ber = control.toBer()
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.test('ignores unrecognized value', async t => {
    const target = new BerWriter()
    target.startSequence()
    target.writeString('2.16.840.1.113730.3.4.2')
    target.writeBoolean(true)
    target.writeBoolean(false)
    target.endSequence()
 
    const control = new Control({
      type: '2.16.840.1.113730.3.4.2',
      criticality: true,
      value: false
    })
    const ber = control.toBer()
 
    t.not(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.test('passes through _toBer', async t => {
    t.plan(2)
    const target = new BerWriter()
    target.startSequence()
    target.writeString('')
    target.writeBoolean(false)
    target.endSequence()
 
    const control = new Control()
    control._toBer = (ber) => t.ok(ber)
    const ber = control.toBer()
 
    t.equal(Buffer.compare(ber.buffer, target.buffer), 0)
  })
 
  t.end()
})