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-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/buffer.json')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
|
|
|
describe('bufferutils', function() {
|
2014-05-28 11:34:15 +02:00
|
|
|
describe('pushDataSize', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
assert.equal(size, f.hexPD.length / 2)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('readPushDataInt', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
if (!f.hexPD) return
|
2014-05-28 11:34:15 +02:00
|
|
|
|
2014-05-29 08:26:13 +02: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)
|
|
|
|
|
|
|
|
assert.equal(d.number, f.dec)
|
|
|
|
assert.equal(d.size, buffer.length)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-13 08:35:07 +02:00
|
|
|
describe('readUInt64LE', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
assert.equal(number, f.dec)
|
|
|
|
})
|
|
|
|
})
|
2014-05-31 05:18:57 +02:00
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) {
|
|
|
|
it('throws on ' + f.description, function() {
|
|
|
|
var buffer = new Buffer(f.hex64, 'hex')
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
bufferutils.readUInt64LE(buffer, 0)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('readVarInt', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
assert.equal(d.number, f.dec)
|
|
|
|
assert.equal(d.size, buffer.length)
|
|
|
|
})
|
|
|
|
})
|
2014-05-31 05:18:57 +02:00
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) {
|
|
|
|
it('throws on ' + f.description, function() {
|
|
|
|
var buffer = new Buffer(f.hexVI, 'hex')
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
bufferutils.readVarInt(buffer, 0)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-05-13 08:35:07 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('varIntSize', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
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
|
|
|
|
|
|
|
assert.equal(size, f.hexVI.length / 2)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-28 11:34:15 +02:00
|
|
|
describe('writePushDataInt', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
fixtures.valid.forEach(function(f, i) {
|
|
|
|
if (!f.hexPD) return
|
2014-05-28 11:34:15 +02:00
|
|
|
|
2014-05-29 08:26:13 +02: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)
|
|
|
|
assert.equal(buffer.slice(0, n).toString('hex'), f.hexPD)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-13 08:35:07 +02:00
|
|
|
describe('writeUInt64LE', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
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)
|
|
|
|
assert.equal(buffer.toString('hex'), f.hex64)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) {
|
|
|
|
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
|
|
|
|
2014-05-28 11:34:15 +02: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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('writeVarInt', function() {
|
2014-05-29 08:26:13 +02:00
|
|
|
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)
|
|
|
|
assert.equal(buffer.slice(0, n).toString('hex'), f.hexVI)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) {
|
|
|
|
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
|
|
|
|
2014-05-28 11:34:15 +02: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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|