txscript: Correct JSON float conversions in tests.
This modifies the conversion of the output index from the JSON-based test data for valid and invalid transactions as well as the signature hash type for signature hash tests to first convert to a signed int and then to an unsigned int. This is necessary because the result of a direct conversion of a float to an unsigned int is implementation dependent and doesn't result in the expected value on all platforms. Also, while here, change the function names in the error prints to match the actual names. Fixes #600.
This commit is contained in:
parent
3c2c858888
commit
c7e6c1e88f
1 changed files with 22 additions and 13 deletions
|
@ -309,20 +309,31 @@ func TestScriptValidTests(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// testVecF64ToUint32 properly handles conversion of float64s read from the JSON
|
||||||
|
// test data to unsigned 32-bit integers. This is necessary because some of the
|
||||||
|
// test data uses -1 as a shortcut to mean max uint32 and direct conversion of a
|
||||||
|
// negative float to an unsigned int is implementation dependent and therefore
|
||||||
|
// doesn't result in the expected value on all platforms. This function woks
|
||||||
|
// around that limitation by converting to a 32-bit signed integer first and
|
||||||
|
// then to a 32-bit unsigned integer which results in the expected behavior on
|
||||||
|
// all platforms.
|
||||||
|
func testVecF64ToUint32(f float64) uint32 {
|
||||||
|
return uint32(int32(f))
|
||||||
|
}
|
||||||
|
|
||||||
// TestTxInvalidTests ensures all of the tests in tx_invalid.json fail as
|
// TestTxInvalidTests ensures all of the tests in tx_invalid.json fail as
|
||||||
// expected.
|
// expected.
|
||||||
func TestTxInvalidTests(t *testing.T) {
|
func TestTxInvalidTests(t *testing.T) {
|
||||||
file, err := ioutil.ReadFile("data/tx_invalid.json")
|
file, err := ioutil.ReadFile("data/tx_invalid.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestBitcoindInvalidTests: %v\n", err)
|
t.Errorf("TestTxInvalidTests: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var tests [][]interface{}
|
var tests [][]interface{}
|
||||||
err = json.Unmarshal(file, &tests)
|
err = json.Unmarshal(file, &tests)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestBitcoindInvalidTests couldn't Unmarshal: %v\n",
|
t.Errorf("TestTxInvalidTests couldn't Unmarshal: %v\n", err)
|
||||||
err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -409,8 +420,7 @@ testloop:
|
||||||
"%d: %v", j, i, test)
|
"%d: %v", j, i, test)
|
||||||
continue testloop
|
continue testloop
|
||||||
}
|
}
|
||||||
|
idx := testVecF64ToUint32(idxf)
|
||||||
idx := uint32(idxf) // (floor(idxf) == idxf?)
|
|
||||||
|
|
||||||
oscript, ok := input[2].(string)
|
oscript, ok := input[2].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -459,15 +469,14 @@ testloop:
|
||||||
func TestTxValidTests(t *testing.T) {
|
func TestTxValidTests(t *testing.T) {
|
||||||
file, err := ioutil.ReadFile("data/tx_valid.json")
|
file, err := ioutil.ReadFile("data/tx_valid.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestBitcoindInvalidTests: %v\n", err)
|
t.Errorf("TestTxValidTests: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var tests [][]interface{}
|
var tests [][]interface{}
|
||||||
err = json.Unmarshal(file, &tests)
|
err = json.Unmarshal(file, &tests)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestBitcoindInvalidTests couldn't Unmarshal: %v\n",
|
t.Errorf("TestTxValidTests couldn't Unmarshal: %v\n", err)
|
||||||
err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -553,8 +562,7 @@ testloop:
|
||||||
"%d: %v", j, i, test)
|
"%d: %v", j, i, test)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
idx := testVecF64ToUint32(idxf)
|
||||||
idx := uint32(idxf) // (floor(idxf) == idxf?)
|
|
||||||
|
|
||||||
oscript, ok := input[2].(string)
|
oscript, ok := input[2].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -640,9 +648,10 @@ func TestCalcSignatureHash(t *testing.T) {
|
||||||
"Failed to parse sub-script: %v", i, err)
|
"Failed to parse sub-script: %v", i, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
hash := TstCalcSignatureHash(parsedScript,
|
|
||||||
SigHashType(test[3].(float64)),
|
hashType := SigHashType(testVecF64ToUint32(test[3].(float64)))
|
||||||
tx, int(test[2].(float64)))
|
hash := TstCalcSignatureHash(parsedScript, hashType, tx,
|
||||||
|
int(test[2].(float64)))
|
||||||
|
|
||||||
expectedHash, _ := wire.NewShaHashFromStr(test[4].(string))
|
expectedHash, _ := wire.NewShaHashFromStr(test[4].(string))
|
||||||
if !bytes.Equal(hash, expectedHash.Bytes()) {
|
if !bytes.Equal(hash, expectedHash.Bytes()) {
|
||||||
|
|
Loading…
Reference in a new issue