Blame view
node_modules/msgpack5/test/4-bytes-length-exts.js
2.57 KB
f7563de62
|
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 81 82 83 84 85 |
'use strict' var test = require('tape').test var msgpack = require('../') var bl = require('bl') test('encode/decode variable ext data up between 0x10000 and 0xffffffff', function (t) { var encoder = msgpack() var all = [] function MyType (size, value) { this.value = value this.size = size } function mytipeEncode (obj) { var buf = new Buffer(obj.size) buf.fill(obj.value) return buf } function mytipeDecode (data) { var result = new MyType(data.length, data.toString('utf8', 0, 1)) for (var i = 0; i < data.length; i++) { if (data.readUInt8(0) !== data.readUInt8(i)) { throw new Error('should all be the same') } } return result } encoder.register(0x52, MyType, mytipeEncode, mytipeDecode) all.push(new MyType(0x10000, 'a')) all.push(new MyType(0x10001, 'a')) all.push(new MyType(0xffffff, 'a')) all.forEach(function (orig) { t.test('encoding a custom obj of length ' + orig.size, function (t) { var buf = encoder.encode(orig) t.equal(buf.length, 6 + orig.size, 'must have the right length') t.equal(buf.readUInt8(0), 0xc9, 'must have the ext header') t.equal(buf.readUInt32BE(1), orig.size, 'must include the data length') t.equal(buf.readUInt8(5), 0x52, 'must include the custom type id') t.equal(buf.toString('utf8', 6, 7), orig.value, 'must decode correctly') t.end() }) t.test('mirror test with a custom obj of length ' + orig.size, function (t) { t.deepEqual(encoder.decode(encoder.encode(orig)), orig, 'must stay the same') t.end() }) }) t.test('decoding an incomplete variable ext data up between 0x10000 and 0xffffffff', function (t) { var obj = encoder.encode(new MyType(0xffffff, 'a')) var buf = new Buffer(obj.length) buf[0] = 0xc9 buf.writeUInt32BE(obj.length + 2, 1) // set bigger size obj.copy(buf, 5, 5, obj.length) 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() }) t.test('decoding an incomplete header of variable ext data up between 0x10000 and 0xffffffff', function (t) { var buf = new Buffer(5) buf[0] = 0xc9 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() }) t.end() }) |