tests: separate templates and script tests
This commit is contained in:
parent
b2a2a2a136
commit
ff68116ee9
4 changed files with 718 additions and 605 deletions
test
|
@ -5,28 +5,28 @@ var bcrypto = require('../src/crypto')
|
|||
var bscript = require('../src/script')
|
||||
var ops = require('../src/opcodes')
|
||||
|
||||
var fixtures = require('./fixtures/script.json')
|
||||
var fixtures = require('./fixtures/templates.json')
|
||||
|
||||
describe('script-templates', function () {
|
||||
describe('classifyInput', function () {
|
||||
fixtures.valid.forEach(function (f) {
|
||||
if (!f.scriptSig) return
|
||||
if (!f.input) return
|
||||
|
||||
it('classifies ' + f.scriptSig + ' as ' + f.type, function () {
|
||||
var scriptSig = bscript.fromASM(f.scriptSig)
|
||||
var type = bscript.classifyInput(scriptSig)
|
||||
it('classifies ' + f.input + ' as ' + f.type, function () {
|
||||
var input = bscript.fromASM(f.input)
|
||||
var type = bscript.classifyInput(input)
|
||||
|
||||
assert.strictEqual(type, f.type)
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.valid.forEach(function (f) {
|
||||
if (!f.scriptSig) return
|
||||
if (!f.input) return
|
||||
if (!f.typeIncomplete) return
|
||||
|
||||
it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () {
|
||||
var scriptSig = bscript.fromASM(f.scriptSig)
|
||||
var type = bscript.classifyInput(scriptSig, true)
|
||||
it('classifies incomplete ' + f.input + ' as ' + f.typeIncomplete, function () {
|
||||
var input = bscript.fromASM(f.input)
|
||||
var type = bscript.classifyInput(input, true)
|
||||
|
||||
assert.strictEqual(type, f.typeIncomplete)
|
||||
})
|
||||
|
@ -35,11 +35,11 @@ describe('script-templates', function () {
|
|||
|
||||
describe('classifyOutput', function () {
|
||||
fixtures.valid.forEach(function (f) {
|
||||
if (!f.scriptPubKey) return
|
||||
if (!f.output) return
|
||||
|
||||
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () {
|
||||
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
||||
var type = bscript.classifyOutput(scriptPubKey)
|
||||
it('classifies ' + f.output + ' as ' + f.type, function () {
|
||||
var output = bscript.fromASM(f.output)
|
||||
var type = bscript.classifyOutput(output)
|
||||
|
||||
assert.strictEqual(type, f.type)
|
||||
})
|
||||
|
@ -62,18 +62,18 @@ describe('script-templates', function () {
|
|||
fixtures.valid.forEach(function (f) {
|
||||
var expected = name.toLowerCase() === f.type
|
||||
|
||||
if (inputType && f.scriptSig) {
|
||||
var scriptSig = bscript.fromASM(f.scriptSig)
|
||||
if (inputType && f.input) {
|
||||
var input = bscript.fromASM(f.input)
|
||||
|
||||
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
||||
assert.strictEqual(inputType.check(scriptSig), expected)
|
||||
it('returns ' + expected + ' for ' + f.input, function () {
|
||||
assert.strictEqual(inputType.check(input), expected)
|
||||
})
|
||||
|
||||
if (f.typeIncomplete) {
|
||||
var expectedIncomplete = name.toLowerCase() === f.typeIncomplete
|
||||
|
||||
it('returns ' + expected + ' for ' + f.scriptSig, function () {
|
||||
assert.strictEqual(inputType.check(scriptSig, true), expectedIncomplete)
|
||||
it('returns ' + expected + ' for ' + f.input, function () {
|
||||
assert.strictEqual(inputType.check(input, true), expectedIncomplete)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -82,18 +82,18 @@ describe('script-templates', function () {
|
|||
if (!(fixtures.invalid[name])) return
|
||||
|
||||
fixtures.invalid[name].inputs.forEach(function (f) {
|
||||
if (!f.scriptSig && !f.scriptSigHex) return
|
||||
if (!f.input && !f.inputHex) return
|
||||
|
||||
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
|
||||
var scriptSig
|
||||
it('returns false for ' + f.description + ' (' + (f.input || f.inputHex) + ')', function () {
|
||||
var input
|
||||
|
||||
if (f.scriptSig) {
|
||||
scriptSig = bscript.fromASM(f.scriptSig)
|
||||
if (f.input) {
|
||||
input = bscript.fromASM(f.input)
|
||||
} else {
|
||||
scriptSig = new Buffer(f.scriptSigHex, 'hex')
|
||||
input = new Buffer(f.inputHex, 'hex')
|
||||
}
|
||||
|
||||
assert.strictEqual(inputType.check(scriptSig), false)
|
||||
assert.strictEqual(inputType.check(input), false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -102,11 +102,11 @@ describe('script-templates', function () {
|
|||
fixtures.valid.forEach(function (f) {
|
||||
var expected = name.toLowerCase() === f.type
|
||||
|
||||
if (outputType && f.scriptPubKey) {
|
||||
it('returns ' + expected + ' for ' + f.scriptPubKey, function () {
|
||||
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
||||
if (outputType && f.output) {
|
||||
it('returns ' + expected + ' for ' + f.output, function () {
|
||||
var output = bscript.fromASM(f.output)
|
||||
|
||||
assert.strictEqual(outputType.check(scriptPubKey), expected)
|
||||
assert.strictEqual(outputType.check(output), expected)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -114,18 +114,18 @@ describe('script-templates', function () {
|
|||
if (!(fixtures.invalid[name])) return
|
||||
|
||||
fixtures.invalid[name].outputs.forEach(function (f) {
|
||||
if (!f.scriptPubKey && !f.scriptPubKeyHex) return
|
||||
if (!f.output && !f.outputHex) return
|
||||
|
||||
it('returns false for ' + f.description + ' (' + (f.scriptPubKey || f.scriptPubKeyHex) + ')', function () {
|
||||
var scriptPubKey
|
||||
it('returns false for ' + f.description + ' (' + (f.output || f.outputHex) + ')', function () {
|
||||
var output
|
||||
|
||||
if (f.scriptPubKey) {
|
||||
scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
||||
if (f.output) {
|
||||
output = bscript.fromASM(f.output)
|
||||
} else {
|
||||
scriptPubKey = new Buffer(f.scriptPubKeyHex, 'hex')
|
||||
output = new Buffer(f.outputHex, 'hex')
|
||||
}
|
||||
|
||||
assert.strictEqual(outputType.check(scriptPubKey), false)
|
||||
assert.strictEqual(outputType.check(output), false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -136,14 +136,14 @@ describe('script-templates', function () {
|
|||
if (f.type !== 'pubkey') return
|
||||
|
||||
var signature = new Buffer(f.signature, 'hex')
|
||||
var scriptSig = bscript.pubKey.input.encode(signature)
|
||||
var input = bscript.pubKey.input.encode(signature)
|
||||
|
||||
it('encodes to ' + f.scriptSig, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
||||
it('encodes to ' + f.input, function () {
|
||||
assert.strictEqual(bscript.toASM(input), f.input)
|
||||
})
|
||||
|
||||
it('decodes to ' + f.signature, function () {
|
||||
assert.deepEqual(bscript.pubKey.input.decode(scriptSig), signature)
|
||||
assert.deepEqual(bscript.pubKey.input.decode(input), signature)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -153,14 +153,14 @@ describe('script-templates', function () {
|
|||
if (f.type !== 'pubkey') return
|
||||
|
||||
var pubKey = new Buffer(f.pubKey, 'hex')
|
||||
var scriptPubKey = bscript.pubKey.output.encode(pubKey)
|
||||
var output = bscript.pubKey.output.encode(pubKey)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes to ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to ' + f.pubKey, function () {
|
||||
assert.deepEqual(bscript.pubKey.output.decode(scriptPubKey), pubKey)
|
||||
assert.deepEqual(bscript.pubKey.output.decode(output), pubKey)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -171,14 +171,14 @@ describe('script-templates', function () {
|
|||
|
||||
var pubKey = new Buffer(f.pubKey, 'hex')
|
||||
var signature = new Buffer(f.signature, 'hex')
|
||||
var scriptSig = bscript.pubKeyHash.input.encode(signature, pubKey)
|
||||
var input = bscript.pubKeyHash.input.encode(signature, pubKey)
|
||||
|
||||
it('encodes to ' + f.scriptSig, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
||||
it('encodes to ' + f.input, function () {
|
||||
assert.strictEqual(bscript.toASM(input), f.input)
|
||||
})
|
||||
|
||||
it('decodes to original arguments', function () {
|
||||
assert.deepEqual(bscript.pubKeyHash.input.decode(scriptSig), {
|
||||
assert.deepEqual(bscript.pubKeyHash.input.decode(input), {
|
||||
signature: signature,
|
||||
pubKey: pubKey
|
||||
})
|
||||
|
@ -192,14 +192,14 @@ describe('script-templates', function () {
|
|||
|
||||
var pubKey = new Buffer(f.pubKey, 'hex')
|
||||
var pubKeyHash = bcrypto.hash160(pubKey)
|
||||
var scriptPubKey = bscript.pubKeyHash.output.encode(pubKeyHash)
|
||||
var output = bscript.pubKeyHash.output.encode(pubKeyHash)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes to ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to ' + pubKeyHash.toString('hex'), function () {
|
||||
assert.deepEqual(bscript.pubKeyHash.output.decode(scriptPubKey), pubKeyHash)
|
||||
assert.deepEqual(bscript.pubKeyHash.output.decode(output), pubKeyHash)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -224,20 +224,20 @@ describe('script-templates', function () {
|
|||
return signature ? new Buffer(signature, 'hex') : ops.OP_0
|
||||
})
|
||||
|
||||
var scriptSig = bscript.multisig.input.encode(signatures)
|
||||
var input = bscript.multisig.input.encode(signatures)
|
||||
|
||||
it('encodes to ' + f.scriptSig, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
||||
it('encodes to ' + f.input, function () {
|
||||
assert.strictEqual(bscript.toASM(input), f.input)
|
||||
})
|
||||
|
||||
it('decodes to ' + signatures.map(function (x) { return x === ops.OP_0 ? 'OP_0' : x.toString('hex') }), function () {
|
||||
assert.deepEqual(bscript.multisig.input.decode(scriptSig, allowIncomplete), signatures)
|
||||
assert.deepEqual(bscript.multisig.input.decode(input, allowIncomplete), signatures)
|
||||
})
|
||||
})
|
||||
|
||||
fixtures.invalid.multisig.inputs.forEach(function (f) {
|
||||
if (!f.scriptPubKey) return
|
||||
var scriptPubKey = bscript.fromASM(f.scriptPubKey)
|
||||
if (!f.output) return
|
||||
var output = bscript.fromASM(f.output)
|
||||
|
||||
it('throws on ' + f.exception, function () {
|
||||
var signatures = f.signatures.map(function (signature) {
|
||||
|
@ -245,7 +245,7 @@ describe('script-templates', function () {
|
|||
})
|
||||
|
||||
assert.throws(function () {
|
||||
bscript.multisig.input.encode(signatures, scriptPubKey)
|
||||
bscript.multisig.input.encode(signatures, output)
|
||||
}, new RegExp(f.exception))
|
||||
})
|
||||
})
|
||||
|
@ -258,14 +258,14 @@ describe('script-templates', function () {
|
|||
var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') })
|
||||
var m = pubKeys.length
|
||||
|
||||
var scriptPubKey = bscript.multisig.output.encode(m, pubKeys)
|
||||
var output = bscript.multisig.output.encode(m, pubKeys)
|
||||
|
||||
it('encodes ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to original arguments', function () {
|
||||
assert.deepEqual(bscript.multisig.output.decode(scriptPubKey), {
|
||||
assert.deepEqual(bscript.multisig.output.decode(output), {
|
||||
m: m,
|
||||
pubKeys: pubKeys
|
||||
})
|
||||
|
@ -292,18 +292,18 @@ describe('script-templates', function () {
|
|||
|
||||
var redeemScript = bscript.fromASM(f.redeemScript)
|
||||
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
|
||||
var scriptSig = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript)
|
||||
var input = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
if (f.scriptSig) {
|
||||
assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig)
|
||||
it('encodes to ' + f.output, function () {
|
||||
if (f.input) {
|
||||
assert.strictEqual(bscript.toASM(input), f.input)
|
||||
} else {
|
||||
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
|
||||
assert.strictEqual(input.toString('hex'), f.inputHex)
|
||||
}
|
||||
})
|
||||
|
||||
it('decodes to original arguments', function () {
|
||||
assert.deepEqual(bscript.scriptHash.input.decode(scriptSig), {
|
||||
assert.deepEqual(bscript.scriptHash.input.decode(input), {
|
||||
redeemScriptSig: redeemScriptSig,
|
||||
redeemScript: redeemScript
|
||||
})
|
||||
|
@ -314,18 +314,18 @@ describe('script-templates', function () {
|
|||
describe('scriptHash.output', function () {
|
||||
fixtures.valid.forEach(function (f) {
|
||||
if (f.type !== 'scripthash') return
|
||||
if (!f.scriptPubKey) return
|
||||
if (!f.output) return
|
||||
|
||||
var redeemScript = bscript.fromASM(f.redeemScript)
|
||||
var scriptHash = bcrypto.hash160(redeemScript)
|
||||
var scriptPubKey = bscript.scriptHash.output.encode(scriptHash)
|
||||
var output = bscript.scriptHash.output.encode(scriptHash)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes to ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to ' + scriptHash.toString('hex'), function () {
|
||||
assert.deepEqual(bscript.scriptHash.output.decode(scriptPubKey), scriptHash)
|
||||
assert.deepEqual(bscript.scriptHash.output.decode(output), scriptHash)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -344,18 +344,18 @@ describe('script-templates', function () {
|
|||
describe('witnessPubKeyHash.output', function () {
|
||||
fixtures.valid.forEach(function (f) {
|
||||
if (f.type !== 'witnesspubkeyhash') return
|
||||
if (!f.scriptPubKey) return
|
||||
if (!f.output) return
|
||||
|
||||
var pubKey = new Buffer(f.pubKey, 'hex')
|
||||
var pubKeyHash = bcrypto.hash160(pubKey)
|
||||
var scriptPubKey = bscript.witnessPubKeyHash.output.encode(pubKeyHash)
|
||||
var output = bscript.witnessPubKeyHash.output.encode(pubKeyHash)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes to ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to ' + pubKeyHash.toString('hex'), function () {
|
||||
assert.deepEqual(bscript.witnessPubKeyHash.output.decode(scriptPubKey), pubKeyHash)
|
||||
assert.deepEqual(bscript.witnessPubKeyHash.output.decode(output), pubKeyHash)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -374,18 +374,18 @@ describe('script-templates', function () {
|
|||
describe('witnessScriptHash.output', function () {
|
||||
fixtures.valid.forEach(function (f) {
|
||||
if (f.type !== 'witnessscripthash') return
|
||||
if (!f.scriptPubKey) return
|
||||
if (!f.output) return
|
||||
|
||||
var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey)
|
||||
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
|
||||
var scriptPubKey = bscript.witnessScriptHash.output.encode(scriptHash)
|
||||
var output = bscript.witnessScriptHash.output.encode(scriptHash)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes to ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to ' + scriptHash.toString('hex'), function () {
|
||||
assert.deepEqual(bscript.witnessScriptHash.output.decode(scriptPubKey), scriptHash)
|
||||
assert.deepEqual(bscript.witnessScriptHash.output.decode(output), scriptHash)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -406,14 +406,14 @@ describe('script-templates', function () {
|
|||
if (f.type !== 'nulldata') return
|
||||
|
||||
var data = new Buffer(f.data, 'hex')
|
||||
var scriptPubKey = bscript.nullData.output.encode(data)
|
||||
var output = bscript.nullData.output.encode(data)
|
||||
|
||||
it('encodes to ' + f.scriptPubKey, function () {
|
||||
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
|
||||
it('encodes to ' + f.output, function () {
|
||||
assert.strictEqual(bscript.toASM(output), f.output)
|
||||
})
|
||||
|
||||
it('decodes to ' + f.data, function () {
|
||||
assert.deepEqual(bscript.nullData.output.decode(scriptPubKey), data)
|
||||
assert.deepEqual(bscript.nullData.output.decode(output), data)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue