bitcoinjs-lib/test/bufferutils.js

184 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-02-23 00:36:57 +01:00
/* global describe, it */
2014-05-13 08:35:07 +02:00
var assert = require('assert')
var bufferutils = require('../src/bufferutils')
2014-05-13 08:35:07 +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 () {
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)
})
})
})
2015-02-23 00:36:57 +01:00
describe('readPushDataInt', function () {
fixtures.valid.forEach(function (f) {
if (!f.hexPD) return
2015-02-23 00:36:57 +01:00
it('decodes ' + f.hexPD + ' correctly', function () {
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)
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)
})
})
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-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
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
2015-02-23 00:36:57 +01:00
it('throws on ' + f.description, function () {
var buffer = new Buffer(f.hex64, 'hex')
2015-02-23 00:36:57 +01:00
assert.throws(function () {
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
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
2015-02-23 00:36:57 +01:00
it('throws on ' + f.description, function () {
var buffer = new Buffer(f.hexVI, 'hex')
2015-02-23 00:36:57 +01:00
assert.throws(function () {
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('reverse', function () {
fixtures.valid.forEach(function (f) {
it('reverses ' + f.hex64 + ' correctly', function () {
2014-08-17 09:24:48 +02:00
var buffer = new Buffer(f.hex64, 'hex')
var buffer2 = bufferutils.reverse(buffer)
Array.prototype.reverse.call(buffer)
assert.deepEqual(buffer, buffer2)
})
})
})
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 () {
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) {
if (!f.hexPD) return
2015-02-23 00:36:57 +01:00
it('encodes ' + f.dec + ' correctly', function () {
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)
})
})
})
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
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
2015-02-23 00:36:57 +01:00
it('throws on ' + f.description, function () {
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)
}, 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
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
2015-02-23 00:36:57 +01:00
it('throws on ' + f.description, function () {
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)
}, new RegExp(f.exception))
2014-05-13 08:35:07 +02:00
})
})
})
})