2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-05-13 08:35:07 +02:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var bufferutils = require('../src/bufferutils')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-31 05:19:46 +02:00
|
|
|
var fixtures = require('./fixtures/bufferutils.json')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('bufferutils', function () {
|
|
|
|
describe('pushDataSize', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('determines the pushDataSize of ' + f.dec + ' correctly', function () {
|
2014-05-28 11:34:15 +02:00
|
|
|
if (!f.hexPD) return
|
|
|
|
|
|
|
|
var size = bufferutils.pushDataSize(f.dec)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(size, f.hexPD.length / 2)
|
2014-05-28 11:34:15 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('readPushDataInt', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-05-29 08:26:13 +02:00
|
|
|
if (!f.hexPD) return
|
2014-05-28 11:34:15 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('decodes ' + f.hexPD + ' correctly', function () {
|
2014-05-28 11:34:15 +02:00
|
|
|
var buffer = new Buffer(f.hexPD, 'hex')
|
|
|
|
var d = bufferutils.readPushDataInt(buffer, 0)
|
2014-05-30 08:28:13 +02:00
|
|
|
var fopcode = parseInt(f.hexPD.substr(0, 2), 16)
|
2014-05-28 11:34:15 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(d.opcode, fopcode)
|
|
|
|
assert.strictEqual(d.number, f.dec)
|
|
|
|
assert.strictEqual(d.size, buffer.length)
|
2014-05-28 11:34:15 +02:00
|
|
|
})
|
|
|
|
})
|
2015-03-04 10:32:08 +01:00
|
|
|
|
|
|
|
fixtures.invalid.readPushDataInt.forEach(function (f) {
|
|
|
|
if (!f.hexPD) return
|
|
|
|
|
|
|
|
it('decodes ' + f.hexPD + ' as null', function () {
|
|
|
|
var buffer = new Buffer(f.hexPD, 'hex')
|
|
|
|
|
|
|
|
var n = bufferutils.readPushDataInt(buffer, 0)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(n, null)
|
2015-03-04 10:32:08 +01:00
|
|
|
})
|
|
|
|
})
|
2014-05-28 11:34:15 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('readUInt64LE', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('decodes ' + f.hex64 + ' correctly', function () {
|
2014-05-13 08:35:07 +02:00
|
|
|
var buffer = new Buffer(f.hex64, 'hex')
|
|
|
|
var number = bufferutils.readUInt64LE(buffer, 0)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(number, f.dec)
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
2014-05-31 05:18:57 +02:00
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
fixtures.invalid.readUInt64LE.forEach(function (f) {
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on ' + f.description, function () {
|
2014-05-31 05:18:57 +02:00
|
|
|
var buffer = new Buffer(f.hex64, 'hex')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-05-31 05:18:57 +02:00
|
|
|
bufferutils.readUInt64LE(buffer, 0)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('readVarInt', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('decodes ' + f.hexVI + ' correctly', function () {
|
2014-05-13 08:35:07 +02:00
|
|
|
var buffer = new Buffer(f.hexVI, 'hex')
|
|
|
|
var d = bufferutils.readVarInt(buffer, 0)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(d.number, f.dec)
|
|
|
|
assert.strictEqual(d.size, buffer.length)
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
2014-05-31 05:18:57 +02:00
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
fixtures.invalid.readUInt64LE.forEach(function (f) {
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on ' + f.description, function () {
|
2014-05-31 05:18:57 +02:00
|
|
|
var buffer = new Buffer(f.hexVI, 'hex')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-05-31 05:18:57 +02:00
|
|
|
bufferutils.readVarInt(buffer, 0)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('varIntBuffer', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('encodes ' + f.dec + ' correctly', function () {
|
2014-10-17 04:07:14 +02:00
|
|
|
var buffer = bufferutils.varIntBuffer(f.dec)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(buffer.toString('hex'), f.hexVI)
|
2014-10-17 04:07:14 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('varIntSize', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('determines the varIntSize of ' + f.dec + ' correctly', function () {
|
2014-05-28 11:34:15 +02:00
|
|
|
var size = bufferutils.varIntSize(f.dec)
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(size, f.hexVI.length / 2)
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('writePushDataInt', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-05-29 08:26:13 +02:00
|
|
|
if (!f.hexPD) return
|
2014-05-28 11:34:15 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('encodes ' + f.dec + ' correctly', function () {
|
2014-05-28 11:34:15 +02:00
|
|
|
var buffer = new Buffer(5)
|
|
|
|
buffer.fill(0)
|
|
|
|
|
|
|
|
var n = bufferutils.writePushDataInt(buffer, f.dec, 0)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexPD)
|
2014-05-28 11:34:15 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('writeUInt64LE', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('encodes ' + f.dec + ' correctly', function () {
|
2014-05-13 08:35:07 +02:00
|
|
|
var buffer = new Buffer(8)
|
|
|
|
buffer.fill(0)
|
|
|
|
|
|
|
|
bufferutils.writeUInt64LE(buffer, f.dec, 0)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(buffer.toString('hex'), f.hex64)
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
fixtures.invalid.readUInt64LE.forEach(function (f) {
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on ' + f.description, function () {
|
2014-05-28 11:34:15 +02:00
|
|
|
var buffer = new Buffer(8)
|
|
|
|
buffer.fill(0)
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-05-13 08:35:07 +02:00
|
|
|
bufferutils.writeUInt64LE(buffer, f.dec, 0)
|
2014-05-31 05:18:57 +02:00
|
|
|
}, new RegExp(f.exception))
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('writeVarInt', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('encodes ' + f.dec + ' correctly', function () {
|
2014-05-13 08:35:07 +02:00
|
|
|
var buffer = new Buffer(9)
|
|
|
|
buffer.fill(0)
|
|
|
|
|
|
|
|
var n = bufferutils.writeVarInt(buffer, f.dec, 0)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexVI)
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
fixtures.invalid.readUInt64LE.forEach(function (f) {
|
2015-02-23 00:36:57 +01:00
|
|
|
it('throws on ' + f.description, function () {
|
2014-05-28 11:34:15 +02:00
|
|
|
var buffer = new Buffer(9)
|
|
|
|
buffer.fill(0)
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-05-13 08:35:07 +02:00
|
|
|
bufferutils.writeVarInt(buffer, f.dec, 0)
|
2014-05-31 05:18:57 +02:00
|
|
|
}, new RegExp(f.exception))
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|