Skip to content

Commit

Permalink
wire: add unit test covering TxWitness JSON (un)marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Nov 3, 2023
1 parent d8df0c2 commit f96021d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions wire/msgtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package wire

import (
"bytes"
"encoding/hex"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -779,6 +780,56 @@ func TestTxWitnessSize(t *testing.T) {
}
}

// hexToBytes converts the passed hex string into bytes and will panic if there
// is an error. This is only provided for the hard-coded constants so errors in
// the source code can be detected. It will only (and must only) be called with
// hard-coded values.
func hexToBytes(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic("invalid hex in source file: " + s)
}
return b
}

// TestTxWitnessJSON performs tests to ensure that the TxWitness datatype can be
// correctly marshalled to JSON and back.
func TestTxWitnessJSON(t *testing.T) {
testCases := []struct {
// witness is the witness to encode and decode.
witness TxWitness
}{
{
witness: TxWitness{
hexToBytes(
"3045022100ee9fe8f9487afa9776647ebcf0883ce0cd37454d7ce198" +
"89d34ba2c99ce5a9f402200341cb469d0efd3955acb9e46f568d" +
"7e2cc10f9084aaff94ced6dc50a59134ad01",
),
hexToBytes(
"03f0000d0639a22bfaf217e4c94289c2b0cc7fa1036f7fd5d9f61a9d" +
"6ec153100e",
),
}},
}

t.Logf("Running %d tests", len(testCases))
for i, test := range testCases {
// Marshall to JSON.
jsonData, err := test.witness.MarshalJSON()
require.NoError(t, err)

// Unmarshall from JSON.
var unmarshalledWitness TxWitness
err = unmarshalledWitness.UnmarshalJSON(jsonData)
require.NoError(t, err)

// Ensure that the witness data is the same before and after the JSON
// marshalling/unmarshalling process.
require.Equal(t, test.witness, unmarshalledWitness, "Test %d", i)
}
}

// TestTxOutPointFromString performs tests to ensure that the outpoint string
// parser works as expected.
func TestTxOutPointFromString(t *testing.T) {
Expand Down

0 comments on commit f96021d

Please sign in to comment.