-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for btc block header deserialize (#26)
* fmt Signed-off-by: Vu Vo <[email protected]> * refactor error assert in test Signed-off-by: Vu Vo <[email protected]> * fix some comments Signed-off-by: Vu Vo <[email protected]> * verify size of block header is 80 bytes Signed-off-by: Vu Vo <[email protected]> * fix testing data Signed-off-by: Vu Vo <[email protected]> * fmt Signed-off-by: Vu Vo <[email protected]> * Apply suggestions from code review Co-authored-by: sczembor <[email protected]> Signed-off-by: Vu Vo <[email protected]> * define error in btc light client Signed-off-by: Vu Vo <[email protected]> * spv errors when decode Signed-off-by: Vu Vo <[email protected]> * format remaing error Signed-off-by: Vu Vo <[email protected]> * tiny fix Signed-off-by: Vu Vo <[email protected]> * more tests Signed-off-by: Vu Vo <[email protected]> * Apply suggestions from code review Co-authored-by: sczembor <[email protected]> Signed-off-by: Vu Vo <[email protected]> * Update btc_header_bytes_test.go Co-authored-by: sczembor <[email protected]> Signed-off-by: Vu Vo <[email protected]> --------- Signed-off-by: Vu Vo <[email protected]> Co-authored-by: sczembor <[email protected]>
- Loading branch information
Showing
8 changed files
with
166 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package btclightclient | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
func TestBTCHeaderFromHex(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
headerHex string | ||
expectedBlockHash string | ||
expectedError error | ||
} | ||
|
||
run := func(t *testing.T, tc testCase) { | ||
header, err := BlockHeaderFromHex(tc.headerHex) | ||
blockHash := header.BlockHash() | ||
if tc.expectedError != nil { | ||
assert.Error(t, err, tc.expectedError.Error()) | ||
} else { | ||
assert.NilError(t, err) | ||
assert.Equal(t, tc.expectedBlockHash, blockHash.String()) | ||
} | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "happy test case", | ||
headerHex: "020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061a", | ||
expectedError: nil, | ||
expectedBlockHash: "0000000000000363d7f5f3341fb0b5b69949103e2d681591c9f737e4ea67e2a7", | ||
}, | ||
{ | ||
name: "invalid header length", | ||
headerHex: "020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061ab", | ||
expectedError: ErrInvalidHeaderSize, | ||
expectedBlockHash: "0000000000000363d7f5f3341fb0b5b69949103e2d681591c9f737e4ea67e2a7", | ||
}, | ||
{ | ||
name: "non-hex character", | ||
headerHex: "020000004cdba1415b2c6e7808c1b3c18df1374238454f7104203475bf01000000000000c17ea9d06015dc83902911cd24837a8ba4bdc0c1d72b873f906d921e06e48d2f984a8250ef75051a72a8061Q", | ||
expectedError: hex.InvalidByteError('Q'), | ||
expectedBlockHash: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
run(t, tc) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.