test.js 19 KB

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