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