Skip to content

Commit cd45774

Browse files
authored
Merge pull request bitcoinjs#1617 from bitcoincoretech/issue_1
Add more unit tests, improve test coverage
2 parents 68fb3cd + bae1d36 commit cd45774

File tree

9 files changed

+154
-1
lines changed

9 files changed

+154
-1
lines changed

test/bufferutils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('bufferutils', () => {
4242
});
4343
});
4444

45-
fixtures.invalid.readUInt64LE.forEach(f => {
45+
fixtures.invalid.writeUInt64LE.forEach(f => {
4646
it('throws on ' + f.description, () => {
4747
const buffer = Buffer.alloc(8, 0);
4848

test/ecpair.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ describe('ECPair', () => {
146146
assert.strictEqual(result, f.WIF);
147147
});
148148
});
149+
it('throws if no private key is found', () => {
150+
assert.throws(() => {
151+
const keyPair = ECPair.makeRandom();
152+
delete (keyPair as any).__D;
153+
keyPair.toWIF();
154+
}, /Missing private key/);
155+
});
149156
});
150157

151158
describe('makeRandom', () => {

test/fixtures/address.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@
189189
{
190190
"exception": "has no matching Script",
191191
"address": "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"
192+
},
193+
{
194+
"exception": "has no matching Script",
195+
"address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"
196+
},
197+
{
198+
"exception": "has no matching Script",
199+
"address": "bc1qqqqqqqqqqv9qus"
192200
}
193201
]
194202
}

test/fixtures/bufferutils.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@
7171
"hex": "0100000000002000",
7272
"dec": 9007199254740993
7373
}
74+
],
75+
"writeUInt64LE": [
76+
{
77+
"description": "n === 2^53",
78+
"exception": "RangeError: value out of range",
79+
"hex": "0000000000002000",
80+
"dec": 9007199254740992
81+
},
82+
{
83+
"description": "n > 2^53",
84+
"exception": "RangeError: value out of range",
85+
"hex": "0100000000002000",
86+
"dec": 9007199254740993
87+
},
88+
{
89+
"description": "n < 0",
90+
"exception": "specified a negative value for writing an unsigned value",
91+
"hex": "",
92+
"dec": -1
93+
},
94+
{
95+
"description": "0 < n < 1",
96+
"exception": "value has a fractional component",
97+
"hex": "",
98+
"dec": 0.1
99+
}
74100
]
75101
}
76102
}

test/fixtures/embed.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@
4444
"arguments": {
4545
"output": "OP_1 OP_2 OP_ADD"
4646
}
47+
},
48+
{
49+
"description": "Return value and data do not match",
50+
"exception": "Data mismatch",
51+
"options": {},
52+
"arguments": {
53+
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4",
54+
"data": [
55+
"a3b147dbe4a85579fc4b5a1855555555555555555555555555555555555555555555555555555555"
56+
]
57+
}
58+
},
59+
{
60+
"description": "Script length incorrect",
61+
"exception": "Data mismatch",
62+
"options": {},
63+
"arguments": {
64+
"output": "OP_RETURN a3b1 47db",
65+
"data": [
66+
"a3b1"
67+
]
68+
}
69+
},
70+
{
71+
"description": "Return data is not buffer",
72+
"exception": "Output is invalid",
73+
"options": {},
74+
"arguments": {
75+
"output": "OP_RETURN OP_1"
76+
}
4777
}
4878
],
4979
"dynamic": {

test/fixtures/p2sh.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,26 @@
323323
}
324324
}
325325
},
326+
{
327+
"exception": "Input and witness provided",
328+
"arguments": {
329+
"redeem": {
330+
"input": "OP_0",
331+
"witness": [
332+
"030000000000000000000000000000000000000000000000000000000000000001"
333+
]
334+
}
335+
}
336+
},
337+
{
338+
"exception": "Non push-only scriptSig",
339+
"arguments": {
340+
"redeem": {
341+
"input": "OP_RETURN",
342+
"output": "OP_1"
343+
}
344+
}
345+
},
326346
{
327347
"exception": "Redeem.output too short",
328348
"arguments": {

test/fixtures/p2wpkh.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@
141141
]
142142
}
143143
},
144+
{
145+
"exception": "Signature mismatch",
146+
"arguments": {
147+
"signature": "300602010002010002",
148+
"witness": [
149+
"300602010002010001",
150+
"030000000000000000000000000000000000000000000000000000000000000001"
151+
]
152+
}
153+
},
144154
{
145155
"exception": "Invalid prefix or Network mismatch",
146156
"arguments": {

test/script.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ describe('script', () => {
4141
});
4242
});
4343

44+
describe('toASM', () => {
45+
const OP_RETURN = bscript.OPS.OP_RETURN;
46+
it('encodes empty buffer as OP_0', () => {
47+
const chunks = [OP_RETURN, Buffer.from([])];
48+
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
49+
});
50+
51+
for (let i = 1; i <= 16; i++) {
52+
it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
53+
const chunks = [OP_RETURN, Buffer.from([i])];
54+
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
55+
});
56+
}
57+
});
58+
4459
describe('fromASM/toASM (templates)', () => {
4560
fixtures2.valid.forEach(f => {
4661
if (f.inputHex) {

test/types.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,41 @@ describe('types', () => {
5454
});
5555
});
5656
});
57+
58+
describe('UInt31', () => {
59+
const UINT31_MAX = Math.pow(2, 31) - 1;
60+
it('return true for valid values', () => {
61+
assert.strictEqual(types.UInt31(0), true);
62+
assert.strictEqual(types.UInt31(1000), true);
63+
assert.strictEqual(types.UInt31(UINT31_MAX), true);
64+
});
65+
66+
it('return false for negative values', () => {
67+
assert.strictEqual(types.UInt31(-1), false);
68+
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
69+
});
70+
71+
it(`return false for value > ${UINT31_MAX}`, () => {
72+
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
73+
});
74+
});
75+
76+
describe('BIP32Path', () => {
77+
it('return true for valid paths', () => {
78+
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
79+
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
80+
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
81+
});
82+
83+
it('return false for invalid paths', () => {
84+
assert.strictEqual(types.BIP32Path('m'), false);
85+
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
86+
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
87+
});
88+
89+
it('return "BIP32 derivation path" for JSON.strigify()', () => {
90+
const toJsonValue = JSON.stringify(types.BIP32Path);
91+
assert.equal(toJsonValue, '"BIP32 derivation path"');
92+
});
93+
});
5794
});

0 commit comments

Comments
 (0)