Blame view

node_modules/msgpack5/test/1-byte-length-buffers.js 2.25 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  'use strict'
  
  var test = require('tape').test
  var msgpack = require('../')
  var bl = require('bl')
  
  function build (size) {
    var buf
  
    buf = new Buffer(size)
    buf.fill('a')
  
    return buf
  }
  
  test('encode/decode 2^8-1 bytes buffers', function (t) {
    var encoder = msgpack()
    var all = []
  
    all.push(build(Math.pow(2, 8) - 1))
    all.push(build(Math.pow(2, 6) + 1))
    all.push(build(1))
    all.push(new Buffer(0))
  
    all.forEach(function (orig) {
      t.test('encoding a buffer of length ' + orig.length, function (t) {
        var buf = encoder.encode(orig)
        t.equal(buf.length, 2 + orig.length, 'must have the right length')
        t.equal(buf.readUInt8(0), 0xc4, 'must have the proper header')
        t.equal(buf.readUInt8(1), orig.length, 'must include the buf length')
        t.equal(buf.toString('utf8', 2), orig.toString('utf8'), 'must decode correctly')
        t.end()
      })
  
      t.test('decoding a buffer of length ' + orig.length, function (t) {
        var buf = new Buffer(2 + orig.length)
        buf[0] = 0xc4
        buf[1] = orig.length
        orig.copy(buf, 2)
        t.equal(encoder.decode(buf).toString('utf8'), orig.toString('utf8'), 'must decode correctly')
        t.end()
      })
  
      t.test('mirror test a buffer of length ' + orig.length, function (t) {
        t.equal(encoder.decode(encoder.encode(orig)).toString(), orig.toString(), 'must stay the same')
        t.end()
      })
    })
  
    t.end()
  })
  
  test('decoding a chopped 2^8-1 bytes buffer', function (t) {
    var encoder = msgpack()
    var orig = build(Math.pow(2, 6))
    var buf = new Buffer(2 + orig.length)
    buf[0] = 0xc4
    buf[1] = Math.pow(2, 8) - 1 // set bigger size
    orig.copy(buf, 2)
    buf = bl().append(buf)
    var origLength = buf.length
    t.throws(function () {
      encoder.decode(buf)
    }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError')
    t.equals(buf.length, origLength, 'must not consume any byte')
    t.end()
  })
  
  test('decoding an incomplete header of 2^8-1 bytes buffer', function (t) {
    var encoder = msgpack()
    var buf = new Buffer(1)
    buf[0] = 0xc4
    buf = bl().append(buf)
    var origLength = buf.length
    t.throws(function () {
      encoder.decode(buf)
    }, encoder.IncompleteBufferError, 'must throw IncompleteBufferError')
    t.equals(buf.length, origLength, 'must not consume any byte')
    t.end()
  })