test.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. 'use strict'
  2. const tape = require('tape')
  3. const crypto = require('crypto')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const BufferList = require('../')
  7. const { Buffer } = require('buffer')
  8. const encodings =
  9. ('hex utf8 utf-8 ascii binary base64' +
  10. (process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')
  11. require('./indexOf')
  12. require('./isBufferList')
  13. require('./convert')
  14. tape('single bytes from single buffer', function (t) {
  15. const bl = new BufferList()
  16. bl.append(Buffer.from('abcd'))
  17. t.equal(bl.length, 4)
  18. t.equal(bl.get(-1), undefined)
  19. t.equal(bl.get(0), 97)
  20. t.equal(bl.get(1), 98)
  21. t.equal(bl.get(2), 99)
  22. t.equal(bl.get(3), 100)
  23. t.equal(bl.get(4), undefined)
  24. t.end()
  25. })
  26. tape('single bytes from multiple buffers', function (t) {
  27. const bl = new BufferList()
  28. bl.append(Buffer.from('abcd'))
  29. bl.append(Buffer.from('efg'))
  30. bl.append(Buffer.from('hi'))
  31. bl.append(Buffer.from('j'))
  32. t.equal(bl.length, 10)
  33. t.equal(bl.get(0), 97)
  34. t.equal(bl.get(1), 98)
  35. t.equal(bl.get(2), 99)
  36. t.equal(bl.get(3), 100)
  37. t.equal(bl.get(4), 101)
  38. t.equal(bl.get(5), 102)
  39. t.equal(bl.get(6), 103)
  40. t.equal(bl.get(7), 104)
  41. t.equal(bl.get(8), 105)
  42. t.equal(bl.get(9), 106)
  43. t.end()
  44. })
  45. tape('multi bytes from single buffer', function (t) {
  46. const bl = new BufferList()
  47. bl.append(Buffer.from('abcd'))
  48. t.equal(bl.length, 4)
  49. t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
  50. t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
  51. t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
  52. t.equal(bl.slice(-4, -1).toString('ascii'), 'abc')
  53. t.end()
  54. })
  55. tape('multi bytes from single buffer (negative indexes)', function (t) {
  56. const bl = new BufferList()
  57. bl.append(Buffer.from('buffer'))
  58. t.equal(bl.length, 6)
  59. t.equal(bl.slice(-6, -1).toString('ascii'), 'buffe')
  60. t.equal(bl.slice(-6, -2).toString('ascii'), 'buff')
  61. t.equal(bl.slice(-5, -2).toString('ascii'), 'uff')
  62. t.end()
  63. })
  64. tape('multiple bytes from multiple buffers', function (t) {
  65. const bl = new BufferList()
  66. bl.append(Buffer.from('abcd'))
  67. bl.append(Buffer.from('efg'))
  68. bl.append(Buffer.from('hi'))
  69. bl.append(Buffer.from('j'))
  70. t.equal(bl.length, 10)
  71. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  72. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  73. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  74. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  75. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  76. t.equal(bl.slice(-7, -4).toString('ascii'), 'def')
  77. t.end()
  78. })
  79. tape('multiple bytes from multiple buffer lists', function (t) {
  80. const bl = new BufferList()
  81. bl.append(new BufferList([Buffer.from('abcd'), Buffer.from('efg')]))
  82. bl.append(new BufferList([Buffer.from('hi'), Buffer.from('j')]))
  83. t.equal(bl.length, 10)
  84. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  85. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  86. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  87. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  88. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  89. t.end()
  90. })
  91. // same data as previous test, just using nested constructors
  92. tape('multiple bytes from crazy nested buffer lists', function (t) {
  93. const bl = new BufferList()
  94. bl.append(new BufferList([
  95. new BufferList([
  96. new BufferList(Buffer.from('abc')),
  97. Buffer.from('d'),
  98. new BufferList(Buffer.from('efg'))
  99. ]),
  100. new BufferList([Buffer.from('hi')]),
  101. new BufferList(Buffer.from('j'))
  102. ]))
  103. t.equal(bl.length, 10)
  104. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  105. t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
  106. t.equal(bl.slice(3, 6).toString('ascii'), 'def')
  107. t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
  108. t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
  109. t.end()
  110. })
  111. tape('append accepts arrays of Buffers', function (t) {
  112. const bl = new BufferList()
  113. bl.append(Buffer.from('abc'))
  114. bl.append([Buffer.from('def')])
  115. bl.append([Buffer.from('ghi'), Buffer.from('jkl')])
  116. bl.append([Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz')])
  117. t.equal(bl.length, 26)
  118. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  119. t.end()
  120. })
  121. tape('append accepts arrays of Uint8Arrays', function (t) {
  122. const bl = new BufferList()
  123. bl.append(new Uint8Array([97, 98, 99]))
  124. bl.append([Uint8Array.from([100, 101, 102])])
  125. bl.append([new Uint8Array([103, 104, 105]), new Uint8Array([106, 107, 108])])
  126. bl.append([new Uint8Array([109, 110, 111, 112]), new Uint8Array([113, 114, 115, 116, 117]), new Uint8Array([118, 119, 120, 121, 122])])
  127. t.equal(bl.length, 26)
  128. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  129. t.end()
  130. })
  131. tape('append accepts arrays of BufferLists', function (t) {
  132. const bl = new BufferList()
  133. bl.append(Buffer.from('abc'))
  134. bl.append([new BufferList('def')])
  135. bl.append(new BufferList([Buffer.from('ghi'), new BufferList('jkl')]))
  136. bl.append([Buffer.from('mnop'), new BufferList([Buffer.from('qrstu'), Buffer.from('vwxyz')])])
  137. t.equal(bl.length, 26)
  138. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  139. t.end()
  140. })
  141. tape('append chainable', function (t) {
  142. const bl = new BufferList()
  143. t.ok(bl.append(Buffer.from('abcd')) === bl)
  144. t.ok(bl.append([Buffer.from('abcd')]) === bl)
  145. t.ok(bl.append(new BufferList(Buffer.from('abcd'))) === bl)
  146. t.ok(bl.append([new BufferList(Buffer.from('abcd'))]) === bl)
  147. t.end()
  148. })
  149. tape('append chainable (test results)', function (t) {
  150. const bl = new BufferList('abc')
  151. .append([new BufferList('def')])
  152. .append(new BufferList([Buffer.from('ghi'), new BufferList('jkl')]))
  153. .append([Buffer.from('mnop'), new BufferList([Buffer.from('qrstu'), Buffer.from('vwxyz')])])
  154. t.equal(bl.length, 26)
  155. t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
  156. t.end()
  157. })
  158. tape('consuming from multiple buffers', function (t) {
  159. const bl = new BufferList()
  160. bl.append(Buffer.from('abcd'))
  161. bl.append(Buffer.from('efg'))
  162. bl.append(Buffer.from('hi'))
  163. bl.append(Buffer.from('j'))
  164. t.equal(bl.length, 10)
  165. t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
  166. bl.consume(3)
  167. t.equal(bl.length, 7)
  168. t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')
  169. bl.consume(2)
  170. t.equal(bl.length, 5)
  171. t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')
  172. bl.consume(1)
  173. t.equal(bl.length, 4)
  174. t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')
  175. bl.consume(1)
  176. t.equal(bl.length, 3)
  177. t.equal(bl.slice(0, 3).toString('ascii'), 'hij')
  178. bl.consume(2)
  179. t.equal(bl.length, 1)
  180. t.equal(bl.slice(0, 1).toString('ascii'), 'j')
  181. t.end()
  182. })
  183. tape('complete consumption', function (t) {
  184. const bl = new BufferList()
  185. bl.append(Buffer.from('a'))
  186. bl.append(Buffer.from('b'))
  187. bl.consume(2)
  188. t.equal(bl.length, 0)
  189. t.equal(bl._bufs.length, 0)
  190. t.end()
  191. })
  192. tape('test readUInt8 / readInt8', function (t) {
  193. const buf1 = Buffer.alloc(1)
  194. const buf2 = Buffer.alloc(3)
  195. const buf3 = Buffer.alloc(3)
  196. const bl = new BufferList()
  197. buf1[0] = 0x1
  198. buf2[1] = 0x3
  199. buf2[2] = 0x4
  200. buf3[0] = 0x23
  201. buf3[1] = 0x42
  202. bl.append(buf1)
  203. bl.append(buf2)
  204. bl.append(buf3)
  205. t.equal(bl.readUInt8(), 0x1)
  206. t.equal(bl.readUInt8(2), 0x3)
  207. t.equal(bl.readInt8(2), 0x3)
  208. t.equal(bl.readUInt8(3), 0x4)
  209. t.equal(bl.readInt8(3), 0x4)
  210. t.equal(bl.readUInt8(4), 0x23)
  211. t.equal(bl.readInt8(4), 0x23)
  212. t.equal(bl.readUInt8(5), 0x42)
  213. t.equal(bl.readInt8(5), 0x42)
  214. t.end()
  215. })
  216. tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
  217. const buf1 = Buffer.alloc(1)
  218. const buf2 = Buffer.alloc(3)
  219. const buf3 = Buffer.alloc(3)
  220. const bl = new BufferList()
  221. buf1[0] = 0x1
  222. buf2[1] = 0x3
  223. buf2[2] = 0x4
  224. buf3[0] = 0x23
  225. buf3[1] = 0x42
  226. bl.append(buf1)
  227. bl.append(buf2)
  228. bl.append(buf3)
  229. t.equal(bl.readUInt16BE(), 0x0100)
  230. t.equal(bl.readUInt16LE(), 0x0001)
  231. t.equal(bl.readUInt16BE(2), 0x0304)
  232. t.equal(bl.readUInt16LE(2), 0x0403)
  233. t.equal(bl.readInt16BE(2), 0x0304)
  234. t.equal(bl.readInt16LE(2), 0x0403)
  235. t.equal(bl.readUInt16BE(3), 0x0423)
  236. t.equal(bl.readUInt16LE(3), 0x2304)
  237. t.equal(bl.readInt16BE(3), 0x0423)
  238. t.equal(bl.readInt16LE(3), 0x2304)
  239. t.equal(bl.readUInt16BE(4), 0x2342)
  240. t.equal(bl.readUInt16LE(4), 0x4223)
  241. t.equal(bl.readInt16BE(4), 0x2342)
  242. t.equal(bl.readInt16LE(4), 0x4223)
  243. t.end()
  244. })
  245. tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
  246. const buf1 = Buffer.alloc(1)
  247. const buf2 = Buffer.alloc(3)
  248. const buf3 = Buffer.alloc(3)
  249. const bl = new BufferList()
  250. buf1[0] = 0x1
  251. buf2[1] = 0x3
  252. buf2[2] = 0x4
  253. buf3[0] = 0x23
  254. buf3[1] = 0x42
  255. bl.append(buf1)
  256. bl.append(buf2)
  257. bl.append(buf3)
  258. t.equal(bl.readUInt32BE(), 0x01000304)
  259. t.equal(bl.readUInt32LE(), 0x04030001)
  260. t.equal(bl.readUInt32BE(2), 0x03042342)
  261. t.equal(bl.readUInt32LE(2), 0x42230403)
  262. t.equal(bl.readInt32BE(2), 0x03042342)
  263. t.equal(bl.readInt32LE(2), 0x42230403)
  264. t.end()
  265. })
  266. tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) {
  267. const buf1 = Buffer.alloc(1)
  268. const buf2 = Buffer.alloc(3)
  269. const buf3 = Buffer.alloc(3)
  270. const bl = new BufferList()
  271. buf2[0] = 0x2
  272. buf2[1] = 0x3
  273. buf2[2] = 0x4
  274. buf3[0] = 0x23
  275. buf3[1] = 0x42
  276. buf3[2] = 0x61
  277. bl.append(buf1)
  278. bl.append(buf2)
  279. bl.append(buf3)
  280. t.equal(bl.readUIntBE(1, 1), 0x02)
  281. t.equal(bl.readUIntBE(1, 2), 0x0203)
  282. t.equal(bl.readUIntBE(1, 3), 0x020304)
  283. t.equal(bl.readUIntBE(1, 4), 0x02030423)
  284. t.equal(bl.readUIntBE(1, 5), 0x0203042342)
  285. t.equal(bl.readUIntBE(1, 6), 0x020304234261)
  286. t.equal(bl.readUIntLE(1, 1), 0x02)
  287. t.equal(bl.readUIntLE(1, 2), 0x0302)
  288. t.equal(bl.readUIntLE(1, 3), 0x040302)
  289. t.equal(bl.readUIntLE(1, 4), 0x23040302)
  290. t.equal(bl.readUIntLE(1, 5), 0x4223040302)
  291. t.equal(bl.readUIntLE(1, 6), 0x614223040302)
  292. t.equal(bl.readIntBE(1, 1), 0x02)
  293. t.equal(bl.readIntBE(1, 2), 0x0203)
  294. t.equal(bl.readIntBE(1, 3), 0x020304)
  295. t.equal(bl.readIntBE(1, 4), 0x02030423)
  296. t.equal(bl.readIntBE(1, 5), 0x0203042342)
  297. t.equal(bl.readIntBE(1, 6), 0x020304234261)
  298. t.equal(bl.readIntLE(1, 1), 0x02)
  299. t.equal(bl.readIntLE(1, 2), 0x0302)
  300. t.equal(bl.readIntLE(1, 3), 0x040302)
  301. t.equal(bl.readIntLE(1, 4), 0x23040302)
  302. t.equal(bl.readIntLE(1, 5), 0x4223040302)
  303. t.equal(bl.readIntLE(1, 6), 0x614223040302)
  304. t.end()
  305. })
  306. tape('test readFloatLE / readFloatBE', function (t) {
  307. const buf1 = Buffer.alloc(1)
  308. const buf2 = Buffer.alloc(3)
  309. const buf3 = Buffer.alloc(3)
  310. const bl = new BufferList()
  311. buf1[0] = 0x01
  312. buf2[1] = 0x00
  313. buf2[2] = 0x00
  314. buf3[0] = 0x80
  315. buf3[1] = 0x3f
  316. bl.append(buf1)
  317. bl.append(buf2)
  318. bl.append(buf3)
  319. const canonical = Buffer.concat([buf1, buf2, buf3])
  320. t.equal(bl.readFloatLE(), canonical.readFloatLE())
  321. t.equal(bl.readFloatBE(), canonical.readFloatBE())
  322. t.equal(bl.readFloatLE(2), canonical.readFloatLE(2))
  323. t.equal(bl.readFloatBE(2), canonical.readFloatBE(2))
  324. t.end()
  325. })
  326. tape('test readDoubleLE / readDoubleBE', function (t) {
  327. const buf1 = Buffer.alloc(1)
  328. const buf2 = Buffer.alloc(3)
  329. const buf3 = Buffer.alloc(10)
  330. const bl = new BufferList()
  331. buf1[0] = 0x01
  332. buf2[1] = 0x55
  333. buf2[2] = 0x55
  334. buf3[0] = 0x55
  335. buf3[1] = 0x55
  336. buf3[2] = 0x55
  337. buf3[3] = 0x55
  338. buf3[4] = 0xd5
  339. buf3[5] = 0x3f
  340. bl.append(buf1)
  341. bl.append(buf2)
  342. bl.append(buf3)
  343. const canonical = Buffer.concat([buf1, buf2, buf3])
  344. t.equal(bl.readDoubleBE(), canonical.readDoubleBE())
  345. t.equal(bl.readDoubleLE(), canonical.readDoubleLE())
  346. t.equal(bl.readDoubleBE(2), canonical.readDoubleBE(2))
  347. t.equal(bl.readDoubleLE(2), canonical.readDoubleLE(2))
  348. t.end()
  349. })
  350. tape('test toString', function (t) {
  351. const bl = new BufferList()
  352. bl.append(Buffer.from('abcd'))
  353. bl.append(Buffer.from('efg'))
  354. bl.append(Buffer.from('hi'))
  355. bl.append(Buffer.from('j'))
  356. t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
  357. t.equal(bl.toString('ascii', 3, 10), 'defghij')
  358. t.equal(bl.toString('ascii', 3, 6), 'def')
  359. t.equal(bl.toString('ascii', 3, 8), 'defgh')
  360. t.equal(bl.toString('ascii', 5, 10), 'fghij')
  361. t.end()
  362. })
  363. tape('test toString encoding', function (t) {
  364. const bl = new BufferList()
  365. const b = Buffer.from('abcdefghij\xff\x00')
  366. bl.append(Buffer.from('abcd'))
  367. bl.append(Buffer.from('efg'))
  368. bl.append(Buffer.from('hi'))
  369. bl.append(Buffer.from('j'))
  370. bl.append(Buffer.from('\xff\x00'))
  371. encodings.forEach(function (enc) {
  372. t.equal(bl.toString(enc), b.toString(enc), enc)
  373. })
  374. t.end()
  375. })
  376. tape('uninitialized memory', function (t) {
  377. const secret = crypto.randomBytes(256)
  378. for (let i = 0; i < 1e6; i++) {
  379. const clone = Buffer.from(secret)
  380. const bl = new BufferList()
  381. bl.append(Buffer.from('a'))
  382. bl.consume(-1024)
  383. const buf = bl.slice(1)
  384. if (buf.indexOf(clone) !== -1) {
  385. t.fail(`Match (at ${i})`)
  386. break
  387. }
  388. }
  389. t.end()
  390. })
  391. !process.browser && tape('test stream', function (t) {
  392. const random = crypto.randomBytes(65534)
  393. const bl = new BufferList((err, buf) => {
  394. t.ok(Buffer.isBuffer(buf))
  395. t.ok(err === null)
  396. t.ok(random.equals(bl.slice()))
  397. t.ok(random.equals(buf.slice()))
  398. bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
  399. .on('close', function () {
  400. const rndhash = crypto.createHash('md5').update(random).digest('hex')
  401. const md5sum = crypto.createHash('md5')
  402. const s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
  403. s.on('data', md5sum.update.bind(md5sum))
  404. s.on('end', function () {
  405. t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
  406. t.end()
  407. })
  408. })
  409. })
  410. fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
  411. fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
  412. })
  413. tape('instantiation with Buffer', function (t) {
  414. const buf = crypto.randomBytes(1024)
  415. const buf2 = crypto.randomBytes(1024)
  416. let b = BufferList(buf)
  417. t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
  418. b = BufferList([buf, buf2])
  419. t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer')
  420. t.end()
  421. })
  422. tape('test String appendage', function (t) {
  423. const bl = new BufferList()
  424. const b = Buffer.from('abcdefghij\xff\x00')
  425. bl.append('abcd')
  426. bl.append('efg')
  427. bl.append('hi')
  428. bl.append('j')
  429. bl.append('\xff\x00')
  430. encodings.forEach(function (enc) {
  431. t.equal(bl.toString(enc), b.toString(enc))
  432. })
  433. t.end()
  434. })
  435. tape('test Number appendage', function (t) {
  436. const bl = new BufferList()
  437. const b = Buffer.from('1234567890')
  438. bl.append(1234)
  439. bl.append(567)
  440. bl.append(89)
  441. bl.append(0)
  442. encodings.forEach(function (enc) {
  443. t.equal(bl.toString(enc), b.toString(enc))
  444. })
  445. t.end()
  446. })
  447. tape('write nothing, should get empty buffer', function (t) {
  448. t.plan(3)
  449. BufferList(function (err, data) {
  450. t.notOk(err, 'no error')
  451. t.ok(Buffer.isBuffer(data), 'got a buffer')
  452. t.equal(0, data.length, 'got a zero-length buffer')
  453. t.end()
  454. }).end()
  455. })
  456. tape('unicode string', function (t) {
  457. t.plan(2)
  458. const inp1 = '\u2600'
  459. const inp2 = '\u2603'
  460. const exp = inp1 + ' and ' + inp2
  461. const bl = BufferList()
  462. bl.write(inp1)
  463. bl.write(' and ')
  464. bl.write(inp2)
  465. t.equal(exp, bl.toString())
  466. t.equal(Buffer.from(exp).toString('hex'), bl.toString('hex'))
  467. })
  468. tape('should emit finish', function (t) {
  469. const source = BufferList()
  470. const dest = BufferList()
  471. source.write('hello')
  472. source.pipe(dest)
  473. dest.on('finish', function () {
  474. t.equal(dest.toString('utf8'), 'hello')
  475. t.end()
  476. })
  477. })
  478. tape('basic copy', function (t) {
  479. const buf = crypto.randomBytes(1024)
  480. const buf2 = Buffer.alloc(1024)
  481. const b = BufferList(buf)
  482. b.copy(buf2)
  483. t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
  484. t.end()
  485. })
  486. tape('copy after many appends', function (t) {
  487. const buf = crypto.randomBytes(512)
  488. const buf2 = Buffer.alloc(1024)
  489. const b = BufferList(buf)
  490. b.append(buf)
  491. b.copy(buf2)
  492. t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
  493. t.end()
  494. })
  495. tape('copy at a precise position', function (t) {
  496. const buf = crypto.randomBytes(1004)
  497. const buf2 = Buffer.alloc(1024)
  498. const b = BufferList(buf)
  499. b.copy(buf2, 20)
  500. t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
  501. t.end()
  502. })
  503. tape('copy starting from a precise location', function (t) {
  504. const buf = crypto.randomBytes(10)
  505. const buf2 = Buffer.alloc(5)
  506. const b = BufferList(buf)
  507. b.copy(buf2, 0, 5)
  508. t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
  509. t.end()
  510. })
  511. tape('copy in an interval', function (t) {
  512. const rnd = crypto.randomBytes(10)
  513. const b = BufferList(rnd) // put the random bytes there
  514. const actual = Buffer.alloc(3)
  515. const expected = Buffer.alloc(3)
  516. rnd.copy(expected, 0, 5, 8)
  517. b.copy(actual, 0, 5, 8)
  518. t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
  519. t.end()
  520. })
  521. tape('copy an interval between two buffers', function (t) {
  522. const buf = crypto.randomBytes(10)
  523. const buf2 = Buffer.alloc(10)
  524. const b = BufferList(buf)
  525. b.append(buf)
  526. b.copy(buf2, 0, 5, 15)
  527. t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
  528. t.end()
  529. })
  530. tape('shallow slice across buffer boundaries', function (t) {
  531. const bl = new BufferList(['First', 'Second', 'Third'])
  532. t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
  533. t.end()
  534. })
  535. tape('shallow slice within single buffer', function (t) {
  536. t.plan(2)
  537. const bl = new BufferList(['First', 'Second', 'Third'])
  538. t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
  539. t.equal(bl.shallowSlice(7, 10).toString(), 'con')
  540. t.end()
  541. })
  542. tape('shallow slice single buffer', function (t) {
  543. t.plan(3)
  544. const bl = new BufferList(['First', 'Second', 'Third'])
  545. t.equal(bl.shallowSlice(0, 5).toString(), 'First')
  546. t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
  547. t.equal(bl.shallowSlice(11, 16).toString(), 'Third')
  548. })
  549. tape('shallow slice with negative or omitted indices', function (t) {
  550. t.plan(4)
  551. const bl = new BufferList(['First', 'Second', 'Third'])
  552. t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
  553. t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
  554. t.equal(bl.shallowSlice(5, -3).toString(), 'SecondTh')
  555. t.equal(bl.shallowSlice(-8).toString(), 'ondThird')
  556. })
  557. tape('shallow slice does not make a copy', function (t) {
  558. t.plan(1)
  559. const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  560. const bl = (new BufferList(buffers)).shallowSlice(5, -3)
  561. buffers[1].fill('h')
  562. buffers[2].fill('h')
  563. t.equal(bl.toString(), 'hhhhhhhh')
  564. })
  565. tape('shallow slice with 0 length', function (t) {
  566. t.plan(1)
  567. const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  568. const bl = (new BufferList(buffers)).shallowSlice(0, 0)
  569. t.equal(bl.length, 0)
  570. })
  571. tape('shallow slice with 0 length from middle', function (t) {
  572. t.plan(1)
  573. const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
  574. const bl = (new BufferList(buffers)).shallowSlice(10, 10)
  575. t.equal(bl.length, 0)
  576. })
  577. tape('duplicate', function (t) {
  578. t.plan(2)
  579. const bl = new BufferList('abcdefghij\xff\x00')
  580. const dup = bl.duplicate()
  581. t.equal(bl.prototype, dup.prototype)
  582. t.equal(bl.toString('hex'), dup.toString('hex'))
  583. })
  584. tape('destroy no pipe', function (t) {
  585. t.plan(2)
  586. const bl = new BufferList('alsdkfja;lsdkfja;lsdk')
  587. bl.destroy()
  588. t.equal(bl._bufs.length, 0)
  589. t.equal(bl.length, 0)
  590. })
  591. tape('destroy with error', function (t) {
  592. t.plan(3)
  593. const bl = new BufferList('alsdkfja;lsdkfja;lsdk')
  594. const err = new Error('kaboom')
  595. bl.destroy(err)
  596. bl.on('error', function (_err) {
  597. t.equal(_err, err)
  598. })
  599. t.equal(bl._bufs.length, 0)
  600. t.equal(bl.length, 0)
  601. })
  602. !process.browser && tape('destroy with pipe before read end', function (t) {
  603. t.plan(2)
  604. const bl = new BufferList()
  605. fs.createReadStream(path.join(__dirname, '/test.js'))
  606. .pipe(bl)
  607. bl.destroy()
  608. t.equal(bl._bufs.length, 0)
  609. t.equal(bl.length, 0)
  610. })
  611. !process.browser && tape('destroy with pipe before read end with race', function (t) {
  612. t.plan(2)
  613. const bl = new BufferList()
  614. fs.createReadStream(path.join(__dirname, '/test.js'))
  615. .pipe(bl)
  616. setTimeout(function () {
  617. bl.destroy()
  618. setTimeout(function () {
  619. t.equal(bl._bufs.length, 0)
  620. t.equal(bl.length, 0)
  621. }, 500)
  622. }, 500)
  623. })
  624. !process.browser && tape('destroy with pipe after read end', function (t) {
  625. t.plan(2)
  626. const bl = new BufferList()
  627. fs.createReadStream(path.join(__dirname, '/test.js'))
  628. .on('end', onEnd)
  629. .pipe(bl)
  630. function onEnd () {
  631. bl.destroy()
  632. t.equal(bl._bufs.length, 0)
  633. t.equal(bl.length, 0)
  634. }
  635. })
  636. !process.browser && tape('destroy with pipe while writing to a destination', function (t) {
  637. t.plan(4)
  638. const bl = new BufferList()
  639. const ds = new BufferList()
  640. fs.createReadStream(path.join(__dirname, '/test.js'))
  641. .on('end', onEnd)
  642. .pipe(bl)
  643. function onEnd () {
  644. bl.pipe(ds)
  645. setTimeout(function () {
  646. bl.destroy()
  647. t.equals(bl._bufs.length, 0)
  648. t.equals(bl.length, 0)
  649. ds.destroy()
  650. t.equals(bl._bufs.length, 0)
  651. t.equals(bl.length, 0)
  652. }, 100)
  653. }
  654. })
  655. !process.browser && tape('handle error', function (t) {
  656. t.plan(2)
  657. fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
  658. t.ok(err instanceof Error, 'has error')
  659. t.notOk(data, 'no data')
  660. }))
  661. })