1 var tape = require('tape')
2 , crypto = require('crypto')
4 , hash = require('hash_file')
5 , BufferList = require('../')
8 ('hex utf8 utf-8 ascii binary base64'
9 + (process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')
11 tape('single bytes from single buffer', function (t) {
12 var bl = new BufferList()
13 bl.append(new Buffer('abcd'))
17 t.equal(bl.get(0), 97)
18 t.equal(bl.get(1), 98)
19 t.equal(bl.get(2), 99)
20 t.equal(bl.get(3), 100)
25 tape('single bytes from multiple buffers', function (t) {
26 var bl = new BufferList()
27 bl.append(new Buffer('abcd'))
28 bl.append(new Buffer('efg'))
29 bl.append(new Buffer('hi'))
30 bl.append(new Buffer('j'))
32 t.equal(bl.length, 10)
34 t.equal(bl.get(0), 97)
35 t.equal(bl.get(1), 98)
36 t.equal(bl.get(2), 99)
37 t.equal(bl.get(3), 100)
38 t.equal(bl.get(4), 101)
39 t.equal(bl.get(5), 102)
40 t.equal(bl.get(6), 103)
41 t.equal(bl.get(7), 104)
42 t.equal(bl.get(8), 105)
43 t.equal(bl.get(9), 106)
47 tape('multi bytes from single buffer', function (t) {
48 var bl = new BufferList()
49 bl.append(new Buffer('abcd'))
53 t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
54 t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
55 t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
60 tape('multiple bytes from multiple buffers', function (t) {
61 var bl = new BufferList()
63 bl.append(new Buffer('abcd'))
64 bl.append(new Buffer('efg'))
65 bl.append(new Buffer('hi'))
66 bl.append(new Buffer('j'))
68 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')
79 tape('multiple bytes from multiple buffer lists', function (t) {
80 var bl = new BufferList()
82 bl.append(new BufferList([new Buffer('abcd'), new Buffer('efg')]))
83 bl.append(new BufferList([new Buffer('hi'), new Buffer('j')]))
85 t.equal(bl.length, 10)
87 t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
88 t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
89 t.equal(bl.slice(3, 6).toString('ascii'), 'def')
90 t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
91 t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
96 tape('consuming from multiple buffers', function (t) {
97 var bl = new BufferList()
99 bl.append(new Buffer('abcd'))
100 bl.append(new Buffer('efg'))
101 bl.append(new Buffer('hi'))
102 bl.append(new Buffer('j'))
104 t.equal(bl.length, 10)
106 t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
109 t.equal(bl.length, 7)
110 t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')
113 t.equal(bl.length, 5)
114 t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')
117 t.equal(bl.length, 4)
118 t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')
121 t.equal(bl.length, 3)
122 t.equal(bl.slice(0, 3).toString('ascii'), 'hij')
125 t.equal(bl.length, 1)
126 t.equal(bl.slice(0, 1).toString('ascii'), 'j')
131 tape('complete consumption', function (t) {
132 var bl = new BufferList()
134 bl.append(new Buffer('a'))
135 bl.append(new Buffer('b'))
139 t.equal(bl.length, 0)
140 t.equal(bl._bufs.length, 0)
145 tape('test readUInt8 / readInt8', function (t) {
146 var buf1 = new Buffer(1)
147 , buf2 = new Buffer(3)
148 , buf3 = new Buffer(3)
149 , bl = new BufferList()
160 t.equal(bl.readUInt8(2), 0x3)
161 t.equal(bl.readInt8(2), 0x3)
162 t.equal(bl.readUInt8(3), 0x4)
163 t.equal(bl.readInt8(3), 0x4)
164 t.equal(bl.readUInt8(4), 0x23)
165 t.equal(bl.readInt8(4), 0x23)
166 t.equal(bl.readUInt8(5), 0x42)
167 t.equal(bl.readInt8(5), 0x42)
171 tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
172 var buf1 = new Buffer(1)
173 , buf2 = new Buffer(3)
174 , buf3 = new Buffer(3)
175 , bl = new BufferList()
186 t.equal(bl.readUInt16BE(2), 0x0304)
187 t.equal(bl.readUInt16LE(2), 0x0403)
188 t.equal(bl.readInt16BE(2), 0x0304)
189 t.equal(bl.readInt16LE(2), 0x0403)
190 t.equal(bl.readUInt16BE(3), 0x0423)
191 t.equal(bl.readUInt16LE(3), 0x2304)
192 t.equal(bl.readInt16BE(3), 0x0423)
193 t.equal(bl.readInt16LE(3), 0x2304)
194 t.equal(bl.readUInt16BE(4), 0x2342)
195 t.equal(bl.readUInt16LE(4), 0x4223)
196 t.equal(bl.readInt16BE(4), 0x2342)
197 t.equal(bl.readInt16LE(4), 0x4223)
201 tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
202 var buf1 = new Buffer(1)
203 , buf2 = new Buffer(3)
204 , buf3 = new Buffer(3)
205 , bl = new BufferList()
216 t.equal(bl.readUInt32BE(2), 0x03042342)
217 t.equal(bl.readUInt32LE(2), 0x42230403)
218 t.equal(bl.readInt32BE(2), 0x03042342)
219 t.equal(bl.readInt32LE(2), 0x42230403)
223 tape('test readFloatLE / readFloatBE', function (t) {
224 var buf1 = new Buffer(1)
225 , buf2 = new Buffer(3)
226 , buf3 = new Buffer(3)
227 , bl = new BufferList()
238 t.equal(bl.readFloatLE(2), 0x01)
242 tape('test readDoubleLE / readDoubleBE', function (t) {
243 var buf1 = new Buffer(1)
244 , buf2 = new Buffer(3)
245 , buf3 = new Buffer(10)
246 , bl = new BufferList()
261 t.equal(bl.readDoubleLE(2), 0.3333333333333333)
265 tape('test toString', function (t) {
266 var bl = new BufferList()
268 bl.append(new Buffer('abcd'))
269 bl.append(new Buffer('efg'))
270 bl.append(new Buffer('hi'))
271 bl.append(new Buffer('j'))
273 t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
274 t.equal(bl.toString('ascii', 3, 10), 'defghij')
275 t.equal(bl.toString('ascii', 3, 6), 'def')
276 t.equal(bl.toString('ascii', 3, 8), 'defgh')
277 t.equal(bl.toString('ascii', 5, 10), 'fghij')
282 tape('test toString encoding', function (t) {
283 var bl = new BufferList()
284 , b = new Buffer('abcdefghij\xff\x00')
286 bl.append(new Buffer('abcd'))
287 bl.append(new Buffer('efg'))
288 bl.append(new Buffer('hi'))
289 bl.append(new Buffer('j'))
290 bl.append(new Buffer('\xff\x00'))
292 encodings.forEach(function (enc) {
293 t.equal(bl.toString(enc), b.toString(enc), enc)
299 !process.browser && tape('test stream', function (t) {
300 var random = crypto.randomBytes(65534)
301 , rndhash = hash(random, 'md5')
302 , md5sum = crypto.createHash('md5')
303 , bl = new BufferList(function (err, buf) {
304 t.ok(Buffer.isBuffer(buf))
306 t.equal(rndhash, hash(bl.slice(), 'md5'))
307 t.equal(rndhash, hash(buf, 'md5'))
309 bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
310 .on('close', function () {
311 var s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
312 s.on('data', md5sum.update.bind(md5sum))
313 s.on('end', function() {
314 t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
321 fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
322 fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
325 tape('instantiation with Buffer', function (t) {
326 var buf = crypto.randomBytes(1024)
327 , buf2 = crypto.randomBytes(1024)
328 , b = BufferList(buf)
330 t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
331 b = BufferList([ buf, buf2 ])
332 t.equal(b.slice().toString('hex'), Buffer.concat([ buf, buf2 ]).toString('hex'), 'same buffer')
336 tape('test String appendage', function (t) {
337 var bl = new BufferList()
338 , b = new Buffer('abcdefghij\xff\x00')
344 bl.append('\xff\x00')
346 encodings.forEach(function (enc) {
347 t.equal(bl.toString(enc), b.toString(enc))
353 tape('test Number appendage', function (t) {
354 var bl = new BufferList()
355 , b = new Buffer('1234567890')
362 encodings.forEach(function (enc) {
363 t.equal(bl.toString(enc), b.toString(enc))
369 tape('write nothing, should get empty buffer', function (t) {
371 BufferList(function (err, data) {
372 t.notOk(err, 'no error')
373 t.ok(Buffer.isBuffer(data), 'got a buffer')
374 t.equal(0, data.length, 'got a zero-length buffer')
379 tape('unicode string', function (t) {
383 , exp = inp1 + ' and ' + inp2
388 t.equal(exp, bl.toString())
389 t.equal(new Buffer(exp).toString('hex'), bl.toString('hex'))
392 tape('should emit finish', function (t) {
393 var source = BufferList()
394 , dest = BufferList()
396 source.write('hello')
399 dest.on('finish', function () {
400 t.equal(dest.toString('utf8'), 'hello')
405 tape('basic copy', function (t) {
406 var buf = crypto.randomBytes(1024)
407 , buf2 = new Buffer(1024)
408 , b = BufferList(buf)
411 t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
415 tape('copy after many appends', function (t) {
416 var buf = crypto.randomBytes(512)
417 , buf2 = new Buffer(1024)
418 , b = BufferList(buf)
422 t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
426 tape('copy at a precise position', function (t) {
427 var buf = crypto.randomBytes(1004)
428 , buf2 = new Buffer(1024)
429 , b = BufferList(buf)
432 t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
436 tape('copy starting from a precise location', function (t) {
437 var buf = crypto.randomBytes(10)
438 , buf2 = new Buffer(5)
439 , b = BufferList(buf)
442 t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
446 tape('copy in an interval', function (t) {
447 var rnd = crypto.randomBytes(10)
448 , b = BufferList(rnd) // put the random bytes there
449 , actual = new Buffer(3)
450 , expected = new Buffer(3)
452 rnd.copy(expected, 0, 5, 8)
453 b.copy(actual, 0, 5, 8)
455 t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
459 tape('copy an interval between two buffers', function (t) {
460 var buf = crypto.randomBytes(10)
461 , buf2 = new Buffer(10)
462 , b = BufferList(buf)
465 b.copy(buf2, 0, 5, 15)
467 t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
471 tape('duplicate', function (t) {
474 var bl = new BufferList('abcdefghij\xff\x00')
475 , dup = bl.duplicate()
477 t.equal(bl.prototype, dup.prototype)
478 t.equal(bl.toString('hex'), dup.toString('hex'))
481 tape('destroy no pipe', function (t) {
484 var bl = new BufferList('alsdkfja;lsdkfja;lsdk')
487 t.equal(bl._bufs.length, 0)
488 t.equal(bl.length, 0)
491 !process.browser && tape('destroy with pipe before read end', function (t) {
494 var bl = new BufferList()
495 fs.createReadStream(__dirname + '/test.js')
500 t.equal(bl._bufs.length, 0)
501 t.equal(bl.length, 0)
505 !process.browser && tape('destroy with pipe before read end with race', function (t) {
508 var bl = new BufferList()
509 fs.createReadStream(__dirname + '/test.js')
512 setTimeout(function () {
514 setTimeout(function () {
515 t.equal(bl._bufs.length, 0)
516 t.equal(bl.length, 0)
521 !process.browser && tape('destroy with pipe after read end', function (t) {
524 var bl = new BufferList()
525 fs.createReadStream(__dirname + '/test.js')
532 t.equal(bl._bufs.length, 0)
533 t.equal(bl.length, 0)
537 !process.browser && tape('destroy with pipe while writing to a destination', function (t) {
540 var bl = new BufferList()
541 , ds = new BufferList()
543 fs.createReadStream(__dirname + '/test.js')
550 setTimeout(function () {
553 t.equals(bl._bufs.length, 0)
554 t.equals(bl.length, 0)
558 t.equals(bl._bufs.length, 0)
559 t.equals(bl.length, 0)
565 !process.browser && tape('handle error', function (t) {
567 fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
568 t.ok(err instanceof Error, 'has error')
569 t.notOk(data, 'no data')