-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-serializer-test.js
More file actions
179 lines (166 loc) · 5.56 KB
/
binary-serializer-test.js
File metadata and controls
179 lines (166 loc) · 5.56 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* eslint-disable func-style */
const BN = require('bn.js');
const assert = require('assert-diff');
const lib = require('../src/coretypes');
const encode = require('../src').encode;
const {binary: {makeParser, BytesList, BinarySerializer}} = lib;
const {UInt8, UInt16, UInt32, UInt64, STObject} = lib;
const {loadFixture} = require('./utils');
const fixtures = loadFixture('data-driven-tests.json');
const deliverMinTx = require('./fixtures/delivermin-tx.json');
const deliverMinTxBinary = require('./fixtures/delivermin-tx-binary.json');
const SignerListSet = {
tx: require('./fixtures/signerlistset-tx.json'),
binary: require('./fixtures/signerlistset-tx-binary.json'),
meta: require('./fixtures/signerlistset-tx-meta-binary.json')
};
const Escrow = {
create: {
tx: require('./fixtures/escrow-create-tx.json'),
binary: require('./fixtures/escrow-create-binary.json')
},
finish: {
tx: require('./fixtures/escrow-finish-tx.json'),
binary: require('./fixtures/escrow-finish-binary.json'),
meta: require('./fixtures/escrow-finish-meta-binary.json')
},
cancel: {
tx: require('./fixtures/escrow-cancel-tx.json'),
binary: require('./fixtures/escrow-cancel-binary.json')
}
}
const PaymentChannel = {
create: {
tx: require('./fixtures/payment-channel-create-tx.json'),
binary: require('./fixtures/payment-channel-create-binary.json')
},
fund: {
tx: require('./fixtures/payment-channel-fund-tx.json'),
binary: require('./fixtures/payment-channel-fund-binary.json')
},
claim: {
tx: require('./fixtures/payment-channel-claim-tx.json'),
binary: require('./fixtures/payment-channel-claim-binary.json')
}
}
function bytesListTest() {
const list = new BytesList().put([0]).put([2, 3]).put([4, 5]);
it('is an Array<Uint8Array>', function() {
assert(Array.isArray(list.arrays));
assert(list.arrays[0] instanceof Uint8Array);
});
it('keeps track of the length itself', function() {
assert.equal(list.length, 5);
});
it('can join all arrays into one via toBytes', function() {
const joined = list.toBytes();
assert(joined.length, 5);
assert.deepEqual(joined, [0, 2, 3, 4, 5]);
});
}
function assertRecycles(blob) {
const parser = makeParser(blob);
const so = parser.readType(STObject);
const out = new BytesList();
so.toBytesSink(out);
const hex = out.toHex();
assert.equal(hex, blob);
assert.notEqual(hex + ':', blob);
}
function nestedObjectTests() {
fixtures.whole_objects.forEach((f, i) => {
it(`whole_objects[${i}]: can parse blob and dump out same blob`,
/* */ () => {
assertRecycles(f.blob_with_no_signing);
});
});
}
function UIntTest() {
function check(type, n, expected) {
it(`Uint${type.width * 8} serializes ${n} as ${expected}`, function() {
const bl = new BytesList();
const serializer = new BinarySerializer(bl);
if (expected === 'throws') {
assert.throws(() => serializer.writeType(type, n));
return;
}
serializer.writeType(type, n);
assert.deepEqual(bl.toBytes(), expected);
});
}
check(UInt8, 5, [5]);
check(UInt16, 5, [0, 5]);
check(UInt32, 5, [0, 0, 0, 5]);
check(UInt32, 0xFFFFFFFF, [255, 255, 255, 255]);
check(UInt8, 0xFEFFFFFF, 'throws');
check(UInt16, 0xFEFFFFFF, 'throws');
check(UInt16, 0xFEFFFFFF, 'throws');
check(UInt64, 0xFEFFFFFF, [0, 0, 0, 0, 254, 255, 255, 255]);
check(UInt64, -1, 'throws');
check(UInt64, 0, [0, 0, 0, 0, 0, 0, 0, 0]);
check(UInt64, 1, [0, 0, 0, 0, 0, 0, 0, 1]);
check(UInt64, new BN(1), [0, 0, 0, 0, 0, 0, 0, 1]);
}
function parseLedger4320278() {
it('can parse object', done => {
this.timeout(30e3);
const json = loadFixture('as-ledger-4320278.json');
json.forEach(e => {
assertRecycles(e.binary);
});
done();
});
}
function deliverMinTest() {
it('can serialize DeliverMin', () => {
assert.strictEqual(encode(deliverMinTx), deliverMinTxBinary);
});
}
function SignerListSetTest() {
it('can serialize SignerListSet', () => {
assert.strictEqual(encode(SignerListSet.tx), SignerListSet.binary);
});
it('can serialize SignerListSet metadata', () => {
assert.strictEqual(encode(SignerListSet.tx.meta), SignerListSet.meta);
});
}
function EscrowTest() {
it('can serialize EscrowCreate', () => {
assert.strictEqual(encode(Escrow.create.tx),
Escrow.create.binary);
});
it('can serialize EscrowFinish', () => {
assert.strictEqual(encode(Escrow.finish.tx),
Escrow.finish.binary);
assert.strictEqual(encode(Escrow.finish.tx.meta),
Escrow.finish.meta);
});
it('can serialize EscrowCancel', () => {
assert.strictEqual(encode(Escrow.cancel.tx),
Escrow.cancel.binary);
});
}
function PaymentChannelTest() {
it('can serialize PaymentChannelCreate', () => {
assert.strictEqual(encode(PaymentChannel.create.tx),
PaymentChannel.create.binary);
});
it('can serialize PaymentChannelFund', () => {
assert.strictEqual(encode(PaymentChannel.fund.tx),
PaymentChannel.fund.binary);
});
it('can serialize PaymentChannelClaim', () => {
assert.strictEqual(encode(PaymentChannel.claim.tx),
PaymentChannel.claim.binary);
});
}
describe('Binary Serialization', function() {
describe.skip('parseLedger4320278', parseLedger4320278);
describe('nestedObjectTests', nestedObjectTests);
describe('UIntTest', UIntTest);
describe('BytesList', bytesListTest);
describe('DeliverMin', deliverMinTest);
describe('SignerListSet', SignerListSetTest);
describe('Escrow', EscrowTest);
describe('PaymentChannel', PaymentChannelTest);
});