react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / bl / test / basic-test.js
81146 viewsvar tape = require('tape')1, crypto = require('crypto')2, fs = require('fs')3, hash = require('hash_file')4, BufferList = require('../')56, encodings =7('hex utf8 utf-8 ascii binary base64'8+ (process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')910tape('single bytes from single buffer', function (t) {11var bl = new BufferList()12bl.append(new Buffer('abcd'))1314t.equal(bl.length, 4)1516t.equal(bl.get(0), 97)17t.equal(bl.get(1), 98)18t.equal(bl.get(2), 99)19t.equal(bl.get(3), 100)2021t.end()22})2324tape('single bytes from multiple buffers', function (t) {25var bl = new BufferList()26bl.append(new Buffer('abcd'))27bl.append(new Buffer('efg'))28bl.append(new Buffer('hi'))29bl.append(new Buffer('j'))3031t.equal(bl.length, 10)3233t.equal(bl.get(0), 97)34t.equal(bl.get(1), 98)35t.equal(bl.get(2), 99)36t.equal(bl.get(3), 100)37t.equal(bl.get(4), 101)38t.equal(bl.get(5), 102)39t.equal(bl.get(6), 103)40t.equal(bl.get(7), 104)41t.equal(bl.get(8), 105)42t.equal(bl.get(9), 106)43t.end()44})4546tape('multi bytes from single buffer', function (t) {47var bl = new BufferList()48bl.append(new Buffer('abcd'))4950t.equal(bl.length, 4)5152t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')53t.equal(bl.slice(0, 3).toString('ascii'), 'abc')54t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')5556t.end()57})5859tape('multiple bytes from multiple buffers', function (t) {60var bl = new BufferList()6162bl.append(new Buffer('abcd'))63bl.append(new Buffer('efg'))64bl.append(new Buffer('hi'))65bl.append(new Buffer('j'))6667t.equal(bl.length, 10)6869t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')70t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')71t.equal(bl.slice(3, 6).toString('ascii'), 'def')72t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')73t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')7475t.end()76})7778tape('multiple bytes from multiple buffer lists', function (t) {79var bl = new BufferList()8081bl.append(new BufferList([new Buffer('abcd'), new Buffer('efg')]))82bl.append(new BufferList([new Buffer('hi'), new Buffer('j')]))8384t.equal(bl.length, 10)8586t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')87t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')88t.equal(bl.slice(3, 6).toString('ascii'), 'def')89t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')90t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')9192t.end()93})9495tape('consuming from multiple buffers', function (t) {96var bl = new BufferList()9798bl.append(new Buffer('abcd'))99bl.append(new Buffer('efg'))100bl.append(new Buffer('hi'))101bl.append(new Buffer('j'))102103t.equal(bl.length, 10)104105t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')106107bl.consume(3)108t.equal(bl.length, 7)109t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')110111bl.consume(2)112t.equal(bl.length, 5)113t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')114115bl.consume(1)116t.equal(bl.length, 4)117t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')118119bl.consume(1)120t.equal(bl.length, 3)121t.equal(bl.slice(0, 3).toString('ascii'), 'hij')122123bl.consume(2)124t.equal(bl.length, 1)125t.equal(bl.slice(0, 1).toString('ascii'), 'j')126127t.end()128})129130tape('test readUInt8 / readInt8', function (t) {131var buf1 = new Buffer(1)132, buf2 = new Buffer(3)133, buf3 = new Buffer(3)134, bl = new BufferList()135136buf2[1] = 0x3137buf2[2] = 0x4138buf3[0] = 0x23139buf3[1] = 0x42140141bl.append(buf1)142bl.append(buf2)143bl.append(buf3)144145t.equal(bl.readUInt8(2), 0x3)146t.equal(bl.readInt8(2), 0x3)147t.equal(bl.readUInt8(3), 0x4)148t.equal(bl.readInt8(3), 0x4)149t.equal(bl.readUInt8(4), 0x23)150t.equal(bl.readInt8(4), 0x23)151t.equal(bl.readUInt8(5), 0x42)152t.equal(bl.readInt8(5), 0x42)153t.end()154})155156tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {157var buf1 = new Buffer(1)158, buf2 = new Buffer(3)159, buf3 = new Buffer(3)160, bl = new BufferList()161162buf2[1] = 0x3163buf2[2] = 0x4164buf3[0] = 0x23165buf3[1] = 0x42166167bl.append(buf1)168bl.append(buf2)169bl.append(buf3)170171t.equal(bl.readUInt16BE(2), 0x0304)172t.equal(bl.readUInt16LE(2), 0x0403)173t.equal(bl.readInt16BE(2), 0x0304)174t.equal(bl.readInt16LE(2), 0x0403)175t.equal(bl.readUInt16BE(3), 0x0423)176t.equal(bl.readUInt16LE(3), 0x2304)177t.equal(bl.readInt16BE(3), 0x0423)178t.equal(bl.readInt16LE(3), 0x2304)179t.equal(bl.readUInt16BE(4), 0x2342)180t.equal(bl.readUInt16LE(4), 0x4223)181t.equal(bl.readInt16BE(4), 0x2342)182t.equal(bl.readInt16LE(4), 0x4223)183t.end()184})185186tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {187var buf1 = new Buffer(1)188, buf2 = new Buffer(3)189, buf3 = new Buffer(3)190, bl = new BufferList()191192buf2[1] = 0x3193buf2[2] = 0x4194buf3[0] = 0x23195buf3[1] = 0x42196197bl.append(buf1)198bl.append(buf2)199bl.append(buf3)200201t.equal(bl.readUInt32BE(2), 0x03042342)202t.equal(bl.readUInt32LE(2), 0x42230403)203t.equal(bl.readInt32BE(2), 0x03042342)204t.equal(bl.readInt32LE(2), 0x42230403)205t.end()206})207208tape('test readFloatLE / readFloatBE', function (t) {209var buf1 = new Buffer(1)210, buf2 = new Buffer(3)211, buf3 = new Buffer(3)212, bl = new BufferList()213214buf2[1] = 0x00215buf2[2] = 0x00216buf3[0] = 0x80217buf3[1] = 0x3f218219bl.append(buf1)220bl.append(buf2)221bl.append(buf3)222223t.equal(bl.readFloatLE(2), 0x01)224t.end()225})226227tape('test readDoubleLE / readDoubleBE', function (t) {228var buf1 = new Buffer(1)229, buf2 = new Buffer(3)230, buf3 = new Buffer(10)231, bl = new BufferList()232233buf2[1] = 0x55234buf2[2] = 0x55235buf3[0] = 0x55236buf3[1] = 0x55237buf3[2] = 0x55238buf3[3] = 0x55239buf3[4] = 0xd5240buf3[5] = 0x3f241242bl.append(buf1)243bl.append(buf2)244bl.append(buf3)245246t.equal(bl.readDoubleLE(2), 0.3333333333333333)247t.end()248})249250tape('test toString', function (t) {251var bl = new BufferList()252253bl.append(new Buffer('abcd'))254bl.append(new Buffer('efg'))255bl.append(new Buffer('hi'))256bl.append(new Buffer('j'))257258t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')259t.equal(bl.toString('ascii', 3, 10), 'defghij')260t.equal(bl.toString('ascii', 3, 6), 'def')261t.equal(bl.toString('ascii', 3, 8), 'defgh')262t.equal(bl.toString('ascii', 5, 10), 'fghij')263264t.end()265})266267tape('test toString encoding', function (t) {268var bl = new BufferList()269, b = new Buffer('abcdefghij\xff\x00')270271bl.append(new Buffer('abcd'))272bl.append(new Buffer('efg'))273bl.append(new Buffer('hi'))274bl.append(new Buffer('j'))275bl.append(new Buffer('\xff\x00'))276277encodings.forEach(function (enc) {278t.equal(bl.toString(enc), b.toString(enc), enc)279})280281t.end()282})283284!process.browser && tape('test stream', function (t) {285var random = crypto.randomBytes(65534)286, rndhash = hash(random, 'md5')287, md5sum = crypto.createHash('md5')288, bl = new BufferList(function (err, buf) {289t.ok(Buffer.isBuffer(buf))290t.ok(err === null)291t.equal(rndhash, hash(bl.slice(), 'md5'))292t.equal(rndhash, hash(buf, 'md5'))293294bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))295.on('close', function () {296var s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')297s.on('data', md5sum.update.bind(md5sum))298s.on('end', function() {299t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')300t.end()301})302})303304})305306fs.writeFileSync('/tmp/bl_test_rnd.dat', random)307fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)308})309310tape('instantiation with Buffer', function (t) {311var buf = crypto.randomBytes(1024)312, buf2 = crypto.randomBytes(1024)313, b = BufferList(buf)314315t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')316b = BufferList([ buf, buf2 ])317t.equal(b.slice().toString('hex'), Buffer.concat([ buf, buf2 ]).toString('hex'), 'same buffer')318t.end()319})320321tape('test String appendage', function (t) {322var bl = new BufferList()323, b = new Buffer('abcdefghij\xff\x00')324325bl.append('abcd')326bl.append('efg')327bl.append('hi')328bl.append('j')329bl.append('\xff\x00')330331encodings.forEach(function (enc) {332t.equal(bl.toString(enc), b.toString(enc))333})334335t.end()336})337338tape('write nothing, should get empty buffer', function (t) {339t.plan(3)340BufferList(function (err, data) {341t.notOk(err, 'no error')342t.ok(Buffer.isBuffer(data), 'got a buffer')343t.equal(0, data.length, 'got a zero-length buffer')344t.end()345}).end()346})347348tape('unicode string', function (t) {349t.plan(2)350var inp1 = '\u2600'351, inp2 = '\u2603'352, exp = inp1 + ' and ' + inp2353, bl = BufferList()354bl.write(inp1)355bl.write(' and ')356bl.write(inp2)357t.equal(exp, bl.toString())358t.equal(new Buffer(exp).toString('hex'), bl.toString('hex'))359})360361tape('should emit finish', function (t) {362var source = BufferList()363, dest = BufferList()364365source.write('hello')366source.pipe(dest)367368dest.on('finish', function () {369t.equal(dest.toString('utf8'), 'hello')370t.end()371})372})373374tape('basic copy', function (t) {375var buf = crypto.randomBytes(1024)376, buf2 = new Buffer(1024)377, b = BufferList(buf)378379b.copy(buf2)380t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')381t.end()382})383384tape('copy after many appends', function (t) {385var buf = crypto.randomBytes(512)386, buf2 = new Buffer(1024)387, b = BufferList(buf)388389b.append(buf)390b.copy(buf2)391t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')392t.end()393})394395tape('copy at a precise position', function (t) {396var buf = crypto.randomBytes(1004)397, buf2 = new Buffer(1024)398, b = BufferList(buf)399400b.copy(buf2, 20)401t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')402t.end()403})404405tape('copy starting from a precise location', function (t) {406var buf = crypto.randomBytes(10)407, buf2 = new Buffer(5)408, b = BufferList(buf)409410b.copy(buf2, 0, 5)411t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')412t.end()413})414415tape('copy in an interval', function (t) {416var rnd = crypto.randomBytes(10)417, b = BufferList(rnd) // put the random bytes there418, actual = new Buffer(3)419, expected = new Buffer(3)420421rnd.copy(expected, 0, 5, 8)422b.copy(actual, 0, 5, 8)423424t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')425t.end()426})427428tape('copy an interval between two buffers', function (t) {429var buf = crypto.randomBytes(10)430, buf2 = new Buffer(10)431, b = BufferList(buf)432433b.append(buf)434b.copy(buf2, 0, 5, 15)435436t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')437t.end()438})439440tape('duplicate', function (t) {441t.plan(2)442443var bl = new BufferList('abcdefghij\xff\x00')444, dup = bl.duplicate()445446t.equal(bl.prototype, dup.prototype)447t.equal(bl.toString('hex'), dup.toString('hex'))448})449450tape('destroy no pipe', function (t) {451t.plan(2)452453var bl = new BufferList('alsdkfja;lsdkfja;lsdk')454bl.destroy()455456t.equal(bl._bufs.length, 0)457t.equal(bl.length, 0)458})459460!process.browser && tape('destroy with pipe before read end', function (t) {461t.plan(2)462463var bl = new BufferList()464fs.createReadStream(__dirname + '/sauce.js')465.pipe(bl)466467bl.destroy()468469t.equal(bl._bufs.length, 0)470t.equal(bl.length, 0)471472})473474!process.browser && tape('destroy with pipe before read end with race', function (t) {475t.plan(2)476477var bl = new BufferList()478fs.createReadStream(__dirname + '/sauce.js')479.pipe(bl)480481setTimeout(function () {482bl.destroy()483setTimeout(function () {484t.equal(bl._bufs.length, 0)485t.equal(bl.length, 0)486}, 500)487}, 500)488})489490!process.browser && tape('destroy with pipe after read end', function (t) {491t.plan(2)492493var bl = new BufferList()494fs.createReadStream(__dirname + '/sauce.js')495.on('end', onEnd)496.pipe(bl)497498function onEnd () {499bl.destroy()500501t.equal(bl._bufs.length, 0)502t.equal(bl.length, 0)503}504})505506!process.browser && tape('destroy with pipe while writing to a destination', function (t) {507t.plan(4)508509var bl = new BufferList()510, ds = new BufferList()511512fs.createReadStream(__dirname + '/sauce.js')513.on('end', onEnd)514.pipe(bl)515516function onEnd () {517bl.pipe(ds)518519setTimeout(function () {520bl.destroy()521522t.equals(bl._bufs.length, 0)523t.equals(bl.length, 0)524525ds.destroy()526527t.equals(bl._bufs.length, 0)528t.equals(bl.length, 0)529530}, 100)531}532})533534!process.browser && tape('handle error', function (t) {535t.plan(2)536fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {537t.ok(err instanceof Error, 'has error')538t.notOk(data, 'no data')539}))540})541542543