Merge pull request #1617 from bitcoincoretech/issue_1
Add more unit tests, improve test coverage
This commit is contained in:
commit
cd4577432f
9 changed files with 154 additions and 1 deletions
|
@ -42,7 +42,7 @@ describe('bufferutils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
fixtures.invalid.readUInt64LE.forEach(f => {
|
||||
fixtures.invalid.writeUInt64LE.forEach(f => {
|
||||
it('throws on ' + f.description, () => {
|
||||
const buffer = Buffer.alloc(8, 0);
|
||||
|
||||
|
|
|
@ -146,6 +146,13 @@ describe('ECPair', () => {
|
|||
assert.strictEqual(result, f.WIF);
|
||||
});
|
||||
});
|
||||
it('throws if no private key is found', () => {
|
||||
assert.throws(() => {
|
||||
const keyPair = ECPair.makeRandom();
|
||||
delete (keyPair as any).__D;
|
||||
keyPair.toWIF();
|
||||
}, /Missing private key/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('makeRandom', () => {
|
||||
|
|
8
test/fixtures/address.json
vendored
8
test/fixtures/address.json
vendored
|
@ -189,6 +189,14 @@
|
|||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"
|
||||
},
|
||||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"
|
||||
},
|
||||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "bc1qqqqqqqqqqv9qus"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
26
test/fixtures/bufferutils.json
vendored
26
test/fixtures/bufferutils.json
vendored
|
@ -71,6 +71,32 @@
|
|||
"hex": "0100000000002000",
|
||||
"dec": 9007199254740993
|
||||
}
|
||||
],
|
||||
"writeUInt64LE": [
|
||||
{
|
||||
"description": "n === 2^53",
|
||||
"exception": "RangeError: value out of range",
|
||||
"hex": "0000000000002000",
|
||||
"dec": 9007199254740992
|
||||
},
|
||||
{
|
||||
"description": "n > 2^53",
|
||||
"exception": "RangeError: value out of range",
|
||||
"hex": "0100000000002000",
|
||||
"dec": 9007199254740993
|
||||
},
|
||||
{
|
||||
"description": "n < 0",
|
||||
"exception": "specified a negative value for writing an unsigned value",
|
||||
"hex": "",
|
||||
"dec": -1
|
||||
},
|
||||
{
|
||||
"description": "0 < n < 1",
|
||||
"exception": "value has a fractional component",
|
||||
"hex": "",
|
||||
"dec": 0.1
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
30
test/fixtures/embed.json
vendored
30
test/fixtures/embed.json
vendored
|
@ -44,6 +44,36 @@
|
|||
"arguments": {
|
||||
"output": "OP_1 OP_2 OP_ADD"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Return value and data do not match",
|
||||
"exception": "Data mismatch",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4",
|
||||
"data": [
|
||||
"a3b147dbe4a85579fc4b5a1855555555555555555555555555555555555555555555555555555555"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Script length incorrect",
|
||||
"exception": "Data mismatch",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN a3b1 47db",
|
||||
"data": [
|
||||
"a3b1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Return data is not buffer",
|
||||
"exception": "Output is invalid",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN OP_1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"dynamic": {
|
||||
|
|
20
test/fixtures/p2sh.json
vendored
20
test/fixtures/p2sh.json
vendored
|
@ -323,6 +323,26 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Input and witness provided",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "OP_0",
|
||||
"witness": [
|
||||
"030000000000000000000000000000000000000000000000000000000000000001"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Non push-only scriptSig",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "OP_RETURN",
|
||||
"output": "OP_1"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Redeem.output too short",
|
||||
"arguments": {
|
||||
|
|
10
test/fixtures/p2wpkh.json
vendored
10
test/fixtures/p2wpkh.json
vendored
|
@ -141,6 +141,16 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Signature mismatch",
|
||||
"arguments": {
|
||||
"signature": "300602010002010002",
|
||||
"witness": [
|
||||
"300602010002010001",
|
||||
"030000000000000000000000000000000000000000000000000000000000000001"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid prefix or Network mismatch",
|
||||
"arguments": {
|
||||
|
|
|
@ -41,6 +41,21 @@ describe('script', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('toASM', () => {
|
||||
const OP_RETURN = bscript.OPS.OP_RETURN;
|
||||
it('encodes empty buffer as OP_0', () => {
|
||||
const chunks = [OP_RETURN, Buffer.from([])];
|
||||
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
|
||||
});
|
||||
|
||||
for (let i = 1; i <= 16; i++) {
|
||||
it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
|
||||
const chunks = [OP_RETURN, Buffer.from([i])];
|
||||
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('fromASM/toASM (templates)', () => {
|
||||
fixtures2.valid.forEach(f => {
|
||||
if (f.inputHex) {
|
||||
|
|
|
@ -54,4 +54,41 @@ describe('types', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('UInt31', () => {
|
||||
const UINT31_MAX = Math.pow(2, 31) - 1;
|
||||
it('return true for valid values', () => {
|
||||
assert.strictEqual(types.UInt31(0), true);
|
||||
assert.strictEqual(types.UInt31(1000), true);
|
||||
assert.strictEqual(types.UInt31(UINT31_MAX), true);
|
||||
});
|
||||
|
||||
it('return false for negative values', () => {
|
||||
assert.strictEqual(types.UInt31(-1), false);
|
||||
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
|
||||
});
|
||||
|
||||
it(`return false for value > ${UINT31_MAX}`, () => {
|
||||
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BIP32Path', () => {
|
||||
it('return true for valid paths', () => {
|
||||
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
|
||||
});
|
||||
|
||||
it('return false for invalid paths', () => {
|
||||
assert.strictEqual(types.BIP32Path('m'), false);
|
||||
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
|
||||
});
|
||||
|
||||
it('return "BIP32 derivation path" for JSON.strigify()', () => {
|
||||
const toJsonValue = JSON.stringify(types.BIP32Path);
|
||||
assert.equal(toJsonValue, '"BIP32 derivation path"');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue