test.js 18 KB

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