writer.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. 'use strict'
  2. const tap = require('tap')
  3. const { Writable } = require('stream')
  4. const BerWriter = require('./writer')
  5. tap.test('has toStringTag', async t => {
  6. const writer = new BerWriter()
  7. t.equal(Object.prototype.toString.call(writer), '[object BerWriter]')
  8. })
  9. tap.test('#ensureBufferCapacity', t => {
  10. t.test('does not change buffer size if unnecessary', async t => {
  11. const writer = new BerWriter({ size: 1 })
  12. t.equal(writer.size, 1)
  13. writer.writeByte(0x01)
  14. t.equal(writer.size, 1)
  15. })
  16. t.test('expands buffer to accomodate write skipping growth factor', async t => {
  17. const writer = new BerWriter({ size: 0 })
  18. t.equal(writer.size, 0)
  19. writer.writeByte(0x01)
  20. t.equal(writer.size, 1)
  21. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x01])), 0)
  22. })
  23. t.test('expands buffer to accomodate write with growth factor', async t => {
  24. const writer = new BerWriter({ size: 1 })
  25. t.equal(writer.size, 1)
  26. writer.writeByte(0x01)
  27. writer.writeByte(0x02)
  28. t.equal(writer.size, 8)
  29. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x01, 0x02])), 0)
  30. })
  31. t.end()
  32. })
  33. tap.test('appendBuffer', t => {
  34. t.test('throws if input not a buffer', async t => {
  35. const writer = new BerWriter()
  36. t.throws(
  37. () => writer.appendBuffer('foo'),
  38. Error('buffer must be an instance of Buffer')
  39. )
  40. })
  41. t.test('appendBuffer appends a buffer', async t => {
  42. const expected = Buffer.from([0x04, 0x03, 0x66, 0x6f, 0x6f, 0x66, 0x6f, 0x6f])
  43. const writer = new BerWriter()
  44. writer.writeString('foo')
  45. writer.appendBuffer(Buffer.from('foo'))
  46. t.equal(Buffer.compare(writer.buffer, expected), 0)
  47. })
  48. t.end()
  49. })
  50. tap.test('endSequence', t => {
  51. t.test('ends a sequence', async t => {
  52. const writer = new BerWriter({ size: 25 })
  53. writer.startSequence()
  54. writer.writeString('hello world')
  55. writer.endSequence()
  56. const ber = writer.buffer
  57. const expected = Buffer.from([
  58. 0x30, 0x0d, // sequence; 13 bytes
  59. 0x04, 0x0b, // string; 11 bytes
  60. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, // 'hello '
  61. 0x77, 0x6f, 0x72, 0x6c, 0x64 // 'world'
  62. ])
  63. t.equal(Buffer.compare(ber, expected), 0)
  64. })
  65. t.test('ends sequence of two byte length', async t => {
  66. const value = Buffer.alloc(0x81, 0x01)
  67. const writer = new BerWriter()
  68. writer.startSequence()
  69. writer.writeBuffer(value, 0x04)
  70. writer.endSequence()
  71. const ber = writer.buffer
  72. t.equal(
  73. Buffer.from([0x30, 0x81, 0x84, 0x04, 0x81, value.length])
  74. .compare(ber.subarray(0, 6)),
  75. 0
  76. )
  77. })
  78. t.test('ends sequence of three byte length', async t => {
  79. const value = Buffer.alloc(0xfe, 0x01)
  80. const writer = new BerWriter()
  81. writer.startSequence()
  82. writer.writeBuffer(value, 0x04)
  83. writer.endSequence()
  84. const ber = writer.buffer
  85. t.equal(
  86. Buffer.from([0x30, 0x82, 0x01, 0x01, 0x04, 0x81, value.length])
  87. .compare(ber.subarray(0, 7)),
  88. 0
  89. )
  90. })
  91. t.test('ends sequence of four byte length', async t => {
  92. const value = Buffer.alloc(0xaaaaaa, 0x01)
  93. const writer = new BerWriter()
  94. writer.startSequence()
  95. writer.writeBuffer(value, 0x04)
  96. writer.endSequence()
  97. const ber = writer.buffer
  98. t.equal(
  99. Buffer.from([0x30, 0x83, 0xaa, 0xaa, 0xaf, 0x04, 0x83, value.length])
  100. .compare(ber.subarray(0, 8)),
  101. 0
  102. )
  103. })
  104. t.test('throws if sequence too long', async t => {
  105. const value = Buffer.alloc(0xaffffff, 0x01)
  106. const writer = new BerWriter()
  107. writer.startSequence()
  108. writer.writeByte(0x04)
  109. // We can't write the length because it is too long. However, this
  110. // still gives us enough data to generate the error we want to generate.
  111. writer.appendBuffer(value)
  112. t.throws(
  113. () => writer.endSequence(),
  114. Error('sequence too long')
  115. )
  116. })
  117. t.end()
  118. })
  119. tap.test('startSequence', t => {
  120. t.test('throws if tag not a number', async t => {
  121. const writer = new BerWriter()
  122. t.throws(
  123. () => writer.startSequence('30'),
  124. Error('tag must be a Number')
  125. )
  126. })
  127. t.test('starts a sequence', async t => {
  128. const writer = new BerWriter({ size: 1 })
  129. writer.startSequence()
  130. t.equal(writer.size, 8)
  131. const expected = Buffer.from([0x30, 0x00, 0x00, 0x00])
  132. t.equal(Buffer.compare(writer.buffer, expected), 0)
  133. })
  134. t.end()
  135. })
  136. tap.test('toHexDump', t => {
  137. t.test('dumps buffer', t => {
  138. const writer = new BerWriter()
  139. writer.appendBuffer(Buffer.from([0x00, 0x01, 0x02, 0x03]))
  140. const expected = '00010203'
  141. let found = ''
  142. const destination = new Writable({
  143. write (chunk, encoding, callback) {
  144. found += chunk.toString()
  145. callback()
  146. }
  147. })
  148. destination.on('finish', () => {
  149. t.equal(found, expected)
  150. t.end()
  151. })
  152. writer.toHexDump({
  153. destination,
  154. closeDestination: true
  155. })
  156. })
  157. t.end()
  158. })
  159. tap.test('writeBoolean', t => {
  160. t.test('throws if input not a boolean', async t => {
  161. const writer = new BerWriter()
  162. t.throws(
  163. () => writer.writeBoolean(1),
  164. Error('boolValue must be a Boolean')
  165. )
  166. })
  167. t.test('throws if tag not a number', async t => {
  168. const writer = new BerWriter()
  169. t.throws(
  170. () => writer.writeBoolean(true, '5'),
  171. Error('tag must be a Number')
  172. )
  173. })
  174. t.test('writes true', async t => {
  175. const writer = new BerWriter({ size: 1 })
  176. writer.writeBoolean(true)
  177. t.equal(writer.size, 8)
  178. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x01, 0x01, 0xff])), 0)
  179. })
  180. t.test('writes false', async t => {
  181. const writer = new BerWriter({ size: 1 })
  182. writer.writeBoolean(false)
  183. t.equal(writer.size, 8)
  184. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x01, 0x01, 0x00])), 0)
  185. })
  186. t.test('writes with custom tag', async t => {
  187. const writer = new BerWriter({ size: 1 })
  188. writer.writeBoolean(true, 0xff)
  189. t.equal(writer.size, 8)
  190. t.equal(Buffer.compare(writer.buffer, Buffer.from([0xff, 0x01, 0xff])), 0)
  191. })
  192. // Original test
  193. t.test('write boolean', async t => {
  194. const writer = new BerWriter()
  195. writer.writeBoolean(true)
  196. writer.writeBoolean(false)
  197. const ber = writer.buffer
  198. t.equal(ber.length, 6, 'Wrong length')
  199. t.equal(ber[0], 0x01, 'tag wrong')
  200. t.equal(ber[1], 0x01, 'length wrong')
  201. t.equal(ber[2], 0xff, 'value wrong')
  202. t.equal(ber[3], 0x01, 'tag wrong')
  203. t.equal(ber[4], 0x01, 'length wrong')
  204. t.equal(ber[5], 0x00, 'value wrong')
  205. })
  206. t.end()
  207. })
  208. tap.test('writeBuffer', t => {
  209. t.test('throws if tag not a number', async t => {
  210. const writer = new BerWriter()
  211. t.throws(
  212. () => writer.writeBuffer(Buffer.alloc(0), '1'),
  213. Error('tag must be a Number')
  214. )
  215. })
  216. t.test('throws if buffer not a Buffer', async t => {
  217. const writer = new BerWriter()
  218. t.throws(
  219. () => writer.writeBuffer([0x00], 0x01),
  220. Error('buffer must be an instance of Buffer')
  221. )
  222. })
  223. t.test('write buffer', async t => {
  224. const writer = new BerWriter()
  225. // write some stuff to start with
  226. writer.writeString('hello world')
  227. let ber = writer.buffer
  228. const buf = Buffer.from([0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01, 0x01,
  229. 0xff, 0x01, 0x01, 0xff])
  230. writer.writeBuffer(buf.subarray(2, buf.length), 0x04)
  231. ber = writer.buffer
  232. t.equal(ber.length, 26, 'wrong length')
  233. t.equal(ber[0], 0x04, 'wrong tag')
  234. t.equal(ber[1], 11, 'wrong length')
  235. t.equal(ber.slice(2, 13).toString('utf8'), 'hello world', 'wrong value')
  236. t.equal(ber[13], buf[0], 'wrong tag')
  237. t.equal(ber[14], buf[1], 'wrong length')
  238. for (let i = 13, j = 0; i < ber.length && j < buf.length; i++, j++) {
  239. t.equal(ber[i], buf[j], 'buffer contents not identical')
  240. }
  241. })
  242. t.end()
  243. })
  244. tap.test('writeByte', t => {
  245. t.test('throws if input not a number', async t => {
  246. const writer = new BerWriter()
  247. t.equal(writer.size, 1024)
  248. t.throws(
  249. () => writer.writeByte('1'),
  250. Error('argument must be a Number')
  251. )
  252. })
  253. t.test('writes a byte to the backing buffer', async t => {
  254. const writer = new BerWriter()
  255. writer.writeByte(0x01)
  256. const buffer = writer.buffer
  257. t.equal(buffer.length, 1)
  258. t.equal(Buffer.compare(buffer, Buffer.from([0x01])), 0)
  259. })
  260. t.end()
  261. })
  262. tap.test('writeEnumeration', async t => {
  263. t.test('throws if value not a number', async t => {
  264. const writer = new BerWriter()
  265. t.throws(
  266. () => writer.writeEnumeration('1'),
  267. Error('value must be a Number')
  268. )
  269. })
  270. t.test('throws if tag not a number', async t => {
  271. const writer = new BerWriter()
  272. t.throws(
  273. () => writer.writeEnumeration(1, '1'),
  274. Error('tag must be a Number')
  275. )
  276. })
  277. t.test('writes an enumeration', async t => {
  278. const writer = new BerWriter({ size: 1 })
  279. writer.writeEnumeration(0x01)
  280. t.equal(writer.size, 8)
  281. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x0a, 0x01, 0x01])), 0)
  282. })
  283. t.test('writes an enumeration with custom tag', async t => {
  284. const writer = new BerWriter({ size: 1 })
  285. writer.writeEnumeration(0x01, 0xff)
  286. t.equal(writer.size, 8)
  287. t.equal(Buffer.compare(writer.buffer, Buffer.from([0xff, 0x01, 0x01])), 0)
  288. })
  289. t.end()
  290. })
  291. tap.test('writeInt', t => {
  292. t.test('throws if int not a number', async t => {
  293. const writer = new BerWriter()
  294. t.throws(
  295. () => writer.writeInt('1'),
  296. Error('intToWrite must be a Number')
  297. )
  298. })
  299. t.test('throws if tag not a number', async t => {
  300. const writer = new BerWriter()
  301. t.throws(
  302. () => writer.writeInt(1, '1'),
  303. Error('tag must be a Number')
  304. )
  305. })
  306. t.test('write 1 byte int', async t => {
  307. const writer = new BerWriter()
  308. writer.writeInt(0x7f)
  309. const ber = writer.buffer
  310. t.equal(ber.length, 3, 'Wrong length for an int: ' + ber.length)
  311. t.equal(ber[0], 0x02, 'ASN.1 tag wrong (2) -> ' + ber[0])
  312. t.equal(ber[1], 0x01, 'length wrong(1) -> ' + ber[1])
  313. t.equal(ber[2], 0x7f, 'value wrong(3) -> ' + ber[2])
  314. })
  315. t.test('write 2 byte int', async t => {
  316. const writer = new BerWriter()
  317. writer.writeInt(0x7ffe)
  318. const ber = writer.buffer
  319. t.equal(ber.length, 4, 'Wrong length for an int')
  320. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  321. t.equal(ber[1], 0x02, 'length wrong')
  322. t.equal(ber[2], 0x7f, 'value wrong (byte 1)')
  323. t.equal(ber[3], 0xfe, 'value wrong (byte 2)')
  324. })
  325. t.test('write 3 byte int', async t => {
  326. const writer = new BerWriter()
  327. writer.writeInt(0x7ffffe)
  328. const ber = writer.buffer
  329. t.equal(ber.length, 5, 'Wrong length for an int')
  330. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  331. t.equal(ber[1], 0x03, 'length wrong')
  332. t.equal(ber[2], 0x7f, 'value wrong (byte 1)')
  333. t.equal(ber[3], 0xff, 'value wrong (byte 2)')
  334. t.equal(ber[4], 0xfe, 'value wrong (byte 3)')
  335. })
  336. t.test('write 4 byte int', async t => {
  337. const writer = new BerWriter()
  338. writer.writeInt(0x7ffffffe)
  339. const ber = writer.buffer
  340. t.equal(ber.length, 6, 'Wrong length for an int')
  341. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  342. t.equal(ber[1], 0x04, 'length wrong')
  343. t.equal(ber[2], 0x7f, 'value wrong (byte 1)')
  344. t.equal(ber[3], 0xff, 'value wrong (byte 2)')
  345. t.equal(ber[4], 0xff, 'value wrong (byte 3)')
  346. t.equal(ber[5], 0xfe, 'value wrong (byte 4)')
  347. })
  348. t.test('write 1 byte negative int', async t => {
  349. const writer = new BerWriter()
  350. writer.writeInt(-128)
  351. const ber = writer.buffer
  352. t.equal(ber.length, 3, 'Wrong length for an int')
  353. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  354. t.equal(ber[1], 0x01, 'length wrong')
  355. t.equal(ber[2], 0x80, 'value wrong (byte 1)')
  356. })
  357. t.test('write 2 byte negative int', async t => {
  358. const writer = new BerWriter()
  359. writer.writeInt(-22400)
  360. const ber = writer.buffer
  361. t.equal(ber.length, 4, 'Wrong length for an int')
  362. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  363. t.equal(ber[1], 0x02, 'length wrong')
  364. t.equal(ber[2], 0xa8, 'value wrong (byte 1)')
  365. t.equal(ber[3], 0x80, 'value wrong (byte 2)')
  366. })
  367. t.test('write 3 byte negative int', async t => {
  368. const writer = new BerWriter()
  369. writer.writeInt(-481653)
  370. const ber = writer.buffer
  371. t.equal(ber.length, 5, 'Wrong length for an int')
  372. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  373. t.equal(ber[1], 0x03, 'length wrong')
  374. t.equal(ber[2], 0xf8, 'value wrong (byte 1)')
  375. t.equal(ber[3], 0xa6, 'value wrong (byte 2)')
  376. t.equal(ber[4], 0x8b, 'value wrong (byte 3)')
  377. })
  378. t.test('write 4 byte negative int', async t => {
  379. const writer = new BerWriter()
  380. writer.writeInt(-1522904131)
  381. const ber = writer.buffer
  382. t.equal(ber.length, 6, 'Wrong length for an int')
  383. t.equal(ber[0], 0x02, 'ASN.1 tag wrong')
  384. t.equal(ber[1], 0x04, 'length wrong')
  385. t.equal(ber[2], 0xa5, 'value wrong (byte 1)')
  386. t.equal(ber[3], 0x3a, 'value wrong (byte 2)')
  387. t.equal(ber[4], 0x53, 'value wrong (byte 3)')
  388. t.equal(ber[5], 0xbd, 'value wrong (byte 4)')
  389. })
  390. t.test('throws for > 4 byte integer', { skip: true }, async t => {
  391. const writer = new BerWriter()
  392. t.throws(
  393. () => writer.writeInt(0xffffffffff),
  394. Error('BER ints cannot be > 0xffffffff')
  395. )
  396. })
  397. t.end()
  398. })
  399. tap.test('writeLength', t => {
  400. t.test('throws if length not a number', async t => {
  401. const writer = new BerWriter()
  402. t.throws(
  403. () => writer.writeLength('1'),
  404. Error('argument must be a Number')
  405. )
  406. })
  407. t.test('writes a single byte length', async t => {
  408. const writer = new BerWriter({ size: 4 })
  409. writer.writeLength(0x7f)
  410. t.equal(writer.buffer.length, 1)
  411. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x7f])), 0)
  412. })
  413. t.test('writes a two byte length', async t => {
  414. const writer = new BerWriter({ size: 4 })
  415. writer.writeLength(0xff)
  416. t.equal(writer.buffer.length, 2)
  417. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x81, 0xff])), 0)
  418. })
  419. t.test('writes a three byte length', async t => {
  420. const writer = new BerWriter({ size: 4 })
  421. writer.writeLength(0xffff)
  422. t.equal(writer.buffer.length, 3)
  423. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x82, 0xff, 0xff])), 0)
  424. })
  425. t.test('writes a four byte length', async t => {
  426. const writer = new BerWriter({ size: 4 })
  427. writer.writeLength(0xffffff)
  428. t.equal(writer.buffer.length, 4)
  429. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x83, 0xff, 0xff, 0xff])), 0)
  430. })
  431. t.test('throw if byte length is too long', async t => {
  432. const writer = new BerWriter({ size: 4 })
  433. t.throws(
  434. () => writer.writeLength(0xffffffffff),
  435. Error('length too long (> 4 bytes)')
  436. )
  437. })
  438. t.end()
  439. })
  440. tap.test('writeNull', t => {
  441. t.test('writeNull', async t => {
  442. const writer = new BerWriter({ size: 2 })
  443. writer.writeNull()
  444. t.equal(writer.size, 2)
  445. t.equal(Buffer.compare(writer.buffer, Buffer.from([0x05, 0x00])), 0)
  446. })
  447. t.end()
  448. })
  449. tap.test('writeOID', t => {
  450. t.test('throws if OID not a string', async t => {
  451. const writer = new BerWriter()
  452. t.throws(
  453. () => writer.writeOID(42),
  454. Error('oidString must be a string')
  455. )
  456. })
  457. t.test('throws if tag not a number', async t => {
  458. const writer = new BerWriter()
  459. t.throws(
  460. () => writer.writeOID('1.2.3', '1'),
  461. Error('tag must be a Number')
  462. )
  463. })
  464. t.test('throws if OID not a valid OID string', async t => {
  465. const writer = new BerWriter()
  466. t.throws(
  467. () => writer.writeOID('foo'),
  468. Error('oidString is not a valid OID string')
  469. )
  470. })
  471. t.test('writes an OID', async t => {
  472. const oid = '1.2.840.113549.1.1.1'
  473. const writer = new BerWriter()
  474. writer.writeOID(oid)
  475. const expected = Buffer.from([0x06, 0x09, 0x2a, 0x86,
  476. 0x48, 0x86, 0xf7, 0x0d,
  477. 0x01, 0x01, 0x01])
  478. const ber = writer.buffer
  479. t.equal(ber.compare(expected), 0)
  480. })
  481. t.test('writes OID covering all octet encodings', async t => {
  482. const oid = '1.2.200.17000.2100100.270100100'
  483. const writer = new BerWriter()
  484. writer.writeOID(oid)
  485. const expected = Buffer.from([
  486. 0x06, 0x0f,
  487. 0x2a, 0x81, 0x48, 0x81,
  488. 0x84, 0x68, 0x81, 0x80,
  489. 0x97, 0x04, 0x81, 0x80,
  490. 0xe5, 0xcd, 0x04
  491. ])
  492. const ber = writer.buffer
  493. t.equal(ber.compare(expected), 0)
  494. })
  495. t.end()
  496. })
  497. tap.test('writeString', t => {
  498. t.test('throws if non-string supplied', async t => {
  499. const writer = new BerWriter()
  500. t.throws(
  501. () => writer.writeString(42),
  502. Error('stringToWrite must be a string')
  503. )
  504. })
  505. t.test('throws if tag not a number', async t => {
  506. const writer = new BerWriter()
  507. t.throws(
  508. () => writer.writeString('foo', '1'),
  509. Error('tag must be a number')
  510. )
  511. })
  512. t.test('writes an empty string', async t => {
  513. const writer = new BerWriter()
  514. writer.writeString('')
  515. const expected = Buffer.from([0x04, 0x00])
  516. t.equal(Buffer.compare(writer.buffer, expected), 0)
  517. })
  518. t.test('writes a string', async t => {
  519. const writer = new BerWriter({ size: 1 })
  520. writer.writeString('foo')
  521. const expected = Buffer.from([0x04, 0x03, 0x66, 0x6f, 0x6f])
  522. t.equal(Buffer.compare(writer.buffer, expected), 0)
  523. t.equal(writer.size, 8)
  524. })
  525. t.end()
  526. })
  527. tap.test('writeString', t => {
  528. t.test('throws if non-array supplied', async t => {
  529. const writer = new BerWriter()
  530. t.throws(
  531. () => writer.writeStringArray(42),
  532. Error('strings must be an instance of Array')
  533. )
  534. })
  535. t.test('write string array', async t => {
  536. const writer = new BerWriter()
  537. writer.writeStringArray(['hello world', 'fubar!'])
  538. const ber = writer.buffer
  539. t.equal(ber.length, 21, 'wrong length')
  540. t.equal(ber[0], 0x04, 'wrong tag')
  541. t.equal(ber[1], 11, 'wrong length')
  542. t.equal(ber.subarray(2, 13).toString('utf8'), 'hello world', 'wrong value')
  543. t.equal(ber[13], 0x04, 'wrong tag')
  544. t.equal(ber[14], 6, 'wrong length')
  545. t.equal(ber.subarray(15).toString('utf8'), 'fubar!', 'wrong value')
  546. })
  547. t.end()
  548. })
  549. tap.test('original tests', t => {
  550. t.test('resize internal buffer', async t => {
  551. const writer = new BerWriter({ size: 2 })
  552. writer.writeString('hello world')
  553. const ber = writer.buffer
  554. t.equal(ber.length, 13, 'wrong length')
  555. t.equal(ber[0], 0x04, 'wrong tag')
  556. t.equal(ber[1], 11, 'wrong length')
  557. t.equal(ber.subarray(2).toString('utf8'), 'hello world', 'wrong value')
  558. })
  559. t.test('sequence', async t => {
  560. const writer = new BerWriter({ size: 25 })
  561. writer.startSequence()
  562. writer.writeString('hello world')
  563. writer.endSequence()
  564. const ber = writer.buffer
  565. t.equal(ber.length, 15, 'wrong length')
  566. t.equal(ber[0], 0x30, 'wrong tag')
  567. t.equal(ber[1], 13, 'wrong length')
  568. t.equal(ber[2], 0x04, 'wrong tag')
  569. t.equal(ber[3], 11, 'wrong length')
  570. t.equal(ber.subarray(4).toString('utf8'), 'hello world', 'wrong value')
  571. })
  572. t.test('nested sequence', async t => {
  573. const writer = new BerWriter({ size: 25 })
  574. writer.startSequence()
  575. writer.writeString('hello world')
  576. writer.startSequence()
  577. writer.writeString('hello world')
  578. writer.endSequence()
  579. writer.endSequence()
  580. const ber = writer.buffer
  581. t.equal(ber.length, 30, 'wrong length')
  582. t.equal(ber[0], 0x30, 'wrong tag')
  583. t.equal(ber[1], 28, 'wrong length')
  584. t.equal(ber[2], 0x04, 'wrong tag')
  585. t.equal(ber[3], 11, 'wrong length')
  586. t.equal(ber.subarray(4, 15).toString('utf8'), 'hello world', 'wrong value')
  587. t.equal(ber[15], 0x30, 'wrong tag')
  588. t.equal(ber[16], 13, 'wrong length')
  589. t.equal(ber[17], 0x04, 'wrong tag')
  590. t.equal(ber[18], 11, 'wrong length')
  591. t.equal(ber.subarray(19, 30).toString('utf8'), 'hello world', 'wrong value')
  592. })
  593. t.test('LDAP bind message', async t => {
  594. const dn = 'cn=foo,ou=unit,o=test'
  595. const writer = new BerWriter()
  596. writer.startSequence()
  597. writer.writeInt(3) // msgid = 3
  598. writer.startSequence(0x60) // ldap bind
  599. writer.writeInt(3) // ldap v3
  600. writer.writeString(dn)
  601. writer.writeByte(0x80)
  602. writer.writeByte(0x00)
  603. writer.endSequence()
  604. writer.endSequence()
  605. const ber = writer.buffer
  606. t.equal(ber.length, 35, 'wrong length (buffer)')
  607. t.equal(ber[0], 0x30, 'wrong tag')
  608. t.equal(ber[1], 33, 'wrong length')
  609. t.equal(ber[2], 0x02, 'wrong tag')
  610. t.equal(ber[3], 1, 'wrong length')
  611. t.equal(ber[4], 0x03, 'wrong value')
  612. t.equal(ber[5], 0x60, 'wrong tag')
  613. t.equal(ber[6], 28, 'wrong length')
  614. t.equal(ber[7], 0x02, 'wrong tag')
  615. t.equal(ber[8], 1, 'wrong length')
  616. t.equal(ber[9], 0x03, 'wrong value')
  617. t.equal(ber[10], 0x04, 'wrong tag')
  618. t.equal(ber[11], dn.length, 'wrong length')
  619. t.equal(ber.subarray(12, 33).toString('utf8'), dn, 'wrong value')
  620. t.equal(ber[33], 0x80, 'wrong tag')
  621. t.equal(ber[34], 0x00, 'wrong len')
  622. })
  623. t.end()
  624. })