bitcoinjs-lib/test/bufferutils.js

166 lines
4.6 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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(f.hex64, 'hex')
2014-05-13 08:35:07 +02:00
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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(f.hexVI, 'hex')
2014-05-13 08:35:07 +02:00
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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(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('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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.alloc(5, 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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.alloc(8, 0)
2014-05-13 08:35:07 +02:00
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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.alloc(8, 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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.alloc(9, 0)
2014-05-13 08:35:07 +02:00
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 () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.alloc(9, 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
})
})
})
})