-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbufferutils.js
165 lines (131 loc) · 4.64 KB
/
bufferutils.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* global describe, it */
var assert = require('assert')
var bufferutils = require('../src/bufferutils')
var fixtures = require('./fixtures/bufferutils.json')
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)
assert.strictEqual(size, f.hexPD.length / 2)
})
})
})
describe('readPushDataInt', function () {
fixtures.valid.forEach(function (f) {
if (!f.hexPD) return
it('decodes ' + f.hexPD + ' correctly', function () {
var buffer = Buffer.from(f.hexPD, 'hex')
var d = bufferutils.readPushDataInt(buffer, 0)
var fopcode = parseInt(f.hexPD.substr(0, 2), 16)
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 = Buffer.from(f.hexPD, 'hex')
var n = bufferutils.readPushDataInt(buffer, 0)
assert.strictEqual(n, null)
})
})
})
describe('readUInt64LE', function () {
fixtures.valid.forEach(function (f) {
it('decodes ' + f.hex64 + ' correctly', function () {
var buffer = Buffer.from(f.hex64, 'hex')
var number = bufferutils.readUInt64LE(buffer, 0)
assert.strictEqual(number, f.dec)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.from(f.hex64, 'hex')
assert.throws(function () {
bufferutils.readUInt64LE(buffer, 0)
}, new RegExp(f.exception))
})
})
})
describe('readVarInt', function () {
fixtures.valid.forEach(function (f) {
it('decodes ' + f.hexVI + ' correctly', function () {
var buffer = Buffer.from(f.hexVI, 'hex')
var d = bufferutils.readVarInt(buffer, 0)
assert.strictEqual(d.number, f.dec)
assert.strictEqual(d.size, buffer.length)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.from(f.hexVI, 'hex')
assert.throws(function () {
bufferutils.readVarInt(buffer, 0)
}, new RegExp(f.exception))
})
})
})
describe('varIntBuffer', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = bufferutils.varIntBuffer(f.dec)
assert.strictEqual(buffer.toString('hex'), f.hexVI)
})
})
})
describe('varIntSize', function () {
fixtures.valid.forEach(function (f) {
it('determines the varIntSize of ' + f.dec + ' correctly', function () {
var size = bufferutils.varIntSize(f.dec)
assert.strictEqual(size, f.hexVI.length / 2)
})
})
})
describe('writePushDataInt', function () {
fixtures.valid.forEach(function (f) {
if (!f.hexPD) return
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(5, 0)
var n = bufferutils.writePushDataInt(buffer, f.dec, 0)
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexPD)
})
})
})
describe('writeUInt64LE', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(8, 0)
bufferutils.writeUInt64LE(buffer, f.dec, 0)
assert.strictEqual(buffer.toString('hex'), f.hex64)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.alloc(8, 0)
assert.throws(function () {
bufferutils.writeUInt64LE(buffer, f.dec, 0)
}, new RegExp(f.exception))
})
})
})
describe('writeVarInt', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(9, 0)
var n = bufferutils.writeVarInt(buffer, f.dec, 0)
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexVI)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.alloc(9, 0)
assert.throws(function () {
bufferutils.writeVarInt(buffer, f.dec, 0)
}, new RegExp(f.exception))
})
})
})
})