Skip to content

Commit

Permalink
Added more internal method tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 1, 2020
1 parent 15f901c commit 148d0e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
3 changes: 1 addition & 2 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func parseSignature(signature string) (sig secp256k1.Signature, recID int, err e
}
r0 := sigRaw[0] - 27
recID = int(r0 & 3)
compressed := (r0 & 4) == 1
if compressed {
if (r0 & 4) == 1 {
err = fmt.Errorf("compressed type is not supported")
return
}
Expand Down
53 changes: 53 additions & 0 deletions verify_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitcoin

import (
"encoding/hex"
"fmt"
"testing"
)
Expand Down Expand Up @@ -35,6 +36,58 @@ func TestVerifyMessage(t *testing.T) {
t.Errorf("%s Failed: [%s] [%s] [%s] inputted and error was expected", t.Name(), test.inputAddress, test.inputSignature, test.inputData)
}
}

// Testing private methods
var messageTests = []struct {
inputMessage string
inputHeader string
expectedHash string
expectedError bool
}{
{"example message", hBSV, "002c483012a3c71d36349682d5ef6495926d4712b5cd2462f1e3c9f57bd4449f", false},
{"", hBSV, "80e795d4a4caadd7047af389d9f7f220562feb6196032e2131e10563352c4bcc", false},
{"example message", "", "f91e1e5a01b6aad5ec785946e4233b0613bf6183ffde8da9879949cbf7d7ca57", false},
{"4qdD3HdK7SC4R9wTgfhr4QkNqRCKunbtRFlYPRYY6lGPiTbA9wZplnscnazyK0NMAx3KtvjDwWIX4J8djkSIYZaSNFEmztekNoe8NR0MLydp21U6Ayfm97oHelvTBcI5hQYccY45oI2KKEB1gyS0V6pbxoDtgjbCAGcnQvLB2iFykNcdU7A6Yntx812tKp90KilPADcEoKfkexMddqJ1pMz262MNhpTWmC4QOFMlB3xB5iTy2fxm6DgT3QLkiesk3kwM", "", "", true},
{"", "4qdD3HdK7SC4R9wTgfhr4QkNqRCKunbtRFlYPRYY6lGPiTbA9wZplnscnazyK0NMAx3KtvjDwWIX4J8djkSIYZaSNFEmztekNoe8NR0MLydp21U6Ayfm97oHelvTBcI5hQYccY45oI2KKEB1gyS0V6pbxoDtgjbCAGcnQvLB2iFykNcdU7A6Yntx812tKp90KilPADcEoKfkexMddqJ1pMz262MNhpTWmC4QOFMlB3xB5iTy2fxm6DgT3QLkiesk3kwM", "", true},
}

// Run tests
for _, test := range messageTests {
if output, err := messageHash(test.inputMessage, test.inputHeader); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] [%s] inputted and error not expected but got: %s", t.Name(), test.inputMessage, test.inputHeader, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] [%s] inputted and error was expected", t.Name(), test.inputMessage, test.inputHeader)
} else if test.expectedHash != hex.EncodeToString(output) {
t.Errorf("%s Failed: [%s] [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputMessage, test.inputHeader, test.expectedHash, hex.EncodeToString(output))
}
}

// Testing private methods
var parseTests = []struct {
inputSignature string
expectedID int
expectedError bool
}{
{"0", 0, true},
{"1234567", 0, true},
{"Op0u5nr4CPukjekOsOojjxuyEOG1HbIAf8qEteGXjA7tlqFinprrEcvdSlJOkZ8zMb", 0, true},
{"000000000000000000000000000000000000000000000000000000000000000000", 0, true},
{"-000-0-000-0---0--00-0-0-000-0--0-000-0-00-0-00---0-0-0--000-00-0-", 0, true},
{"IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=", 1, false},
{"IBDscOd-Ov4yrd-YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS-UJD+UK2SnNMn6Z3E4na8=", 0, true},
}

// Run tests
for _, test := range parseTests {
if _, output, err := parseSignature(test.inputSignature); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputSignature, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.inputSignature)
} else if output != test.expectedID {
t.Errorf("%s Failed: [%s] inputted and [%d] expected, but got: %d", t.Name(), test.inputSignature, test.expectedID, output)
}
}

}

// ExampleVerifyMessage example using VerifyMessage()
Expand Down

0 comments on commit 148d0e0

Please sign in to comment.