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
'use strict'
 
const tap = require('tap')
const { BerReader, BerWriter } = require('@ldapjs/asn1')
const { operations, resultCodes } = require('@ldapjs/protocol')
const {
  extensionDisconnectionNotificationResponseBytes
} = require('./_fixtures/message-byte-arrays')
const ExtensionResponse = require('./extension-response')
 
tap.test('basic', async t => {
  const res = new ExtensionResponse()
  t.equal(res.protocolOp, operations.LDAP_RES_EXTENSION)
  t.equal(res.type, 'ExtensionResponse')
})
 
tap.test('.responseName', t => {
  t.test('gets and sets', async t => {
    const res = new ExtensionResponse()
    t.equal(res.responseName, undefined)
    res.responseName = 'foo'
    t.equal(res.responseName, 'foo')
  })
 
  t.end()
})
 
tap.test('.responseValue', t => {
  t.test('gets and sets', async t => {
    const res = new ExtensionResponse()
    t.equal(res.responseValue, undefined)
    res.responseValue = 'foo'
    t.equal(res.responseValue, 'foo')
  })
 
  t.end()
})
 
tap.test('toBer', t => {
  tap.test('returns basic bytes', async t => {
    const res = new ExtensionResponse({
      messageId: 0,
      status: resultCodes.UNAVAILABLE,
      diagnosticMessage: 'The Directory Server is shutting down',
      responseName: '1.3.6.1.4.1.1466.20036'
    })
    const ber = res.toBer()
    const expected = Buffer.from(extensionDisconnectionNotificationResponseBytes)
    t.equal(expected.compare(ber.buffer), 0)
  })
 
  t.end()
})
 
tap.test('_writeResponse', t => {
  t.test('writes a response with a name', async t => {
    const req = new ExtensionResponse({ responseName: 'foo' })
    const expected = Buffer.from([0x8a, 0x03, 0x66, 0x6f, 0x6f])
    const writer = new BerWriter()
    req._writeResponse(writer)
    t.equal(expected.compare(writer.buffer), 0)
  })
 
  t.test('writes response with value', async t => {
    const req = new ExtensionResponse({ responseValue: 'foo' })
    const expected = Buffer.from([0x8b, 0x03, 0x66, 0x6f, 0x6f])
    const writer = new BerWriter()
    req._writeResponse(writer)
    t.equal(expected.compare(writer.buffer), 0)
  })
 
  t.end()
})
 
tap.test('#parseToPojo', t => {
  t.test('throws if protocol op is wrong', async t => {
    const bytes = extensionDisconnectionNotificationResponseBytes.slice(5)
    bytes[0] = 0x68
    const reader = new BerReader(Buffer.from(bytes))
    t.throws(
      () => ExtensionResponse.parseToPojo(reader),
      Error('found wrong protocol operation: 0x68')
    )
  })
 
  t.test('parses a basic object', async t => {
    const bytes = extensionDisconnectionNotificationResponseBytes.slice(5)
    const reader = new BerReader(Buffer.from(bytes))
    const pojo = ExtensionResponse.parseToPojo(reader)
    t.strictSame(pojo, {
      status: resultCodes.UNAVAILABLE,
      matchedDN: '',
      diagnosticMessage: 'The Directory Server is shutting down',
      referrals: [],
      responseName: '1.3.6.1.4.1.1466.20036'
    })
  })
 
  t.test('values are buffer strings', async t => {
    const {
      withGeneratedPasswordBytes
    } = require('./extension-responses/_fixtures/password-modify')
    const bytes = withGeneratedPasswordBytes.slice(5)
    const reader = new BerReader(Buffer.from(bytes))
    const pojo = ExtensionResponse.parseToPojo(reader)
    t.strictSame(pojo, {
      status: 0,
      matchedDN: '',
      diagnosticMessage: '',
      referrals: [],
      responseName: undefined,
      responseValue: '<buffer>301f801d4164617074657253746576656e736f6e477261696e506c61796d617465'
    })
  })
 
  t.end()
})