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
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
 
const { test } = require('tap')
const BerReader = require('./reader')
 
test('load library', function (t) {
  t.ok(BerReader)
  try {
    const reader = new BerReader()
    t.equal(reader, null, 'reader')
    t.fail('Should have thrown')
  } catch (e) {
    t.ok(e instanceof TypeError, 'Should have been a type error')
  }
  t.end()
})
 
test('read byte', function (t) {
  const reader = new BerReader(Buffer.from([0xde]))
  t.ok(reader)
  t.equal(reader.readByte(), 0xde, 'wrong value')
  t.end()
})
 
test('read 1 byte int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x01, 0x03]))
  t.ok(reader)
  t.equal(reader.readInt(), 0x03, 'wrong value')
  t.equal(reader.length, 0x01, 'wrong length')
  t.end()
})
 
test('read 2 byte int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x02, 0x7e, 0xde]))
  t.ok(reader)
  t.equal(reader.readInt(), 0x7ede, 'wrong value')
  t.equal(reader.length, 0x02, 'wrong length')
  t.end()
})
 
test('read 3 byte int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x03, 0x7e, 0xde, 0x03]))
  t.ok(reader)
  t.equal(reader.readInt(), 0x7ede03, 'wrong value')
  t.equal(reader.length, 0x03, 'wrong length')
  t.end()
})
 
test('read 4 byte int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x04, 0x7e, 0xde, 0x03, 0x01]))
  t.ok(reader)
  t.equal(reader.readInt(), 0x7ede0301, 'wrong value')
  t.equal(reader.length, 0x04, 'wrong length')
  t.end()
})
 
test('read 1 byte negative int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x01, 0xdc]))
  t.ok(reader)
  t.equal(reader.readInt(), -36, 'wrong value')
  t.equal(reader.length, 0x01, 'wrong length')
  t.end()
})
 
test('read 2 byte negative int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x02, 0xc0, 0x4e]))
  t.ok(reader)
  t.equal(reader.readInt(), -16306, 'wrong value')
  t.equal(reader.length, 0x02, 'wrong length')
  t.end()
})
 
test('read 3 byte negative int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x03, 0xff, 0x00, 0x19]))
  t.ok(reader)
  t.equal(reader.readInt(), -65511, 'wrong value')
  t.equal(reader.length, 0x03, 'wrong length')
  t.end()
})
 
test('read 4 byte negative int', function (t) {
  const reader = new BerReader(Buffer.from([0x02, 0x04, 0x91, 0x7c, 0x22, 0x1f]))
  t.ok(reader)
  t.equal(reader.readInt(), -1854135777, 'wrong value')
  t.equal(reader.length, 0x04, 'wrong length')
  t.end()
})
 
test('read boolean true', function (t) {
  const reader = new BerReader(Buffer.from([0x01, 0x01, 0xff]))
  t.ok(reader)
  t.equal(reader.readBoolean(), true, 'wrong value')
  t.equal(reader.length, 0x01, 'wrong length')
  t.end()
})
 
test('read boolean false', function (t) {
  const reader = new BerReader(Buffer.from([0x01, 0x01, 0x00]))
  t.ok(reader)
  t.equal(reader.readBoolean(), false, 'wrong value')
  t.equal(reader.length, 0x01, 'wrong length')
  t.end()
})
 
test('read enumeration', function (t) {
  const reader = new BerReader(Buffer.from([0x0a, 0x01, 0x20]))
  t.ok(reader)
  t.equal(reader.readEnumeration(), 0x20, 'wrong value')
  t.equal(reader.length, 0x01, 'wrong length')
  t.end()
})
 
test('read string', function (t) {
  const dn = 'cn=foo,ou=unit,o=test'
  const buf = Buffer.alloc(dn.length + 2)
  buf[0] = 0x04
  buf[1] = Buffer.byteLength(dn)
  buf.write(dn, 2)
  const reader = new BerReader(buf)
  t.ok(reader)
  t.equal(reader.readString(), dn, 'wrong value')
  t.equal(reader.length, dn.length, 'wrong length')
  t.end()
})
 
test('read sequence', function (t) {
  const reader = new BerReader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]))
  t.ok(reader)
  t.equal(reader.readSequence(), 0x30, 'wrong value')
  t.equal(reader.length, 0x03, 'wrong length')
  t.equal(reader.readBoolean(), true, 'wrong value')
  t.equal(reader.length, 0x01, 'wrong length')
  t.end()
})
 
test('anonymous LDAPv3 bind', function (t) {
  const BIND = Buffer.alloc(14)
  BIND[0] = 0x30 // Sequence
  BIND[1] = 12 // len
  BIND[2] = 0x02 // ASN.1 Integer
  BIND[3] = 1 // len
  BIND[4] = 0x04 // msgid (make up 4)
  BIND[5] = 0x60 // Bind Request
  BIND[6] = 7 // len
  BIND[7] = 0x02 // ASN.1 Integer
  BIND[8] = 1 // len
  BIND[9] = 0x03 // v3
  BIND[10] = 0x04 // String (bind dn)
  BIND[11] = 0 // len
  BIND[12] = 0x80 // ContextSpecific (choice)
  BIND[13] = 0 // simple bind
 
  // Start testing ^^
  const ber = new BerReader(BIND)
  t.equal(ber.readSequence(), 48, 'Not an ASN.1 Sequence')
  t.equal(ber.length, 12, 'Message length should be 12')
  t.equal(ber.readInt(), 4, 'Message id should have been 4')
  t.equal(ber.readSequence(), 96, 'Bind Request should have been 96')
  t.equal(ber.length, 7, 'Bind length should have been 7')
  t.equal(ber.readInt(), 3, 'LDAP version should have been 3')
  t.equal(ber.readString(), '', 'Bind DN should have been empty')
  t.equal(ber.length, 0, 'string length should have been 0')
  t.equal(ber.readByte(), 0x80, 'Should have been ContextSpecific (choice)')
  t.equal(ber.readByte(), 0, 'Should have been simple bind')
  t.equal(null, ber.readByte(), 'Should be out of data')
  t.end()
})
 
test('long string', function (t) {
  const buf = Buffer.alloc(256)
  const s =
    '2;649;CN=Red Hat CS 71GA Demo,O=Red Hat CS 71GA Demo,C=US;' +
    'CN=RHCS Agent - admin01,UID=admin01,O=redhat,C=US [1] This is ' +
    'Teena Vradmin\'s description.'
  buf[0] = 0x04
  buf[1] = 0x81
  buf[2] = 0x94
  buf.write(s, 3)
  const ber = new BerReader(buf.slice(0, 3 + s.length))
  t.equal(ber.readString(), s)
  t.end()
})