Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Mar 31, 2020
1 parent fc266ed commit bec3ae2
Show file tree
Hide file tree
Showing 4 changed files with 582 additions and 16 deletions.
43 changes: 31 additions & 12 deletions network/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

// TODO add test for hash fee bump transaction
func TestHashTransaction(t *testing.T) {
var txe xdr.TransactionEnvelope
err := xdr.SafeUnmarshalBase64("AAAAAGL8HQvQkbK2HA3WVjRrKmjX00fG8sLI7m0ERwJW/AX3AAAACgAAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAArqN6LeOagjxMaUP96Bzfs9e0corNZXzBWJkFoK7kvkwAAAAAO5rKAAAAAAAAAAABVvwF9wAAAEAKZ7IPj/46PuWU6ZOtyMosctNAkXRNX9WCAI5RnfRk+AyxDLoDZP/9l3NvsxQtWj9juQOuoBlFLnWu8intgxQA", &txe)
Expand All @@ -22,13 +21,11 @@ func TestHashTransaction(t *testing.T) {
0x0d, 0x78, 0x6b, 0x6e, 0x0a, 0x00, 0xfd, 0x74,
}
actual, err := HashTransactionV0(&txe.V0.Tx, TestNetworkPassphrase)
if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
assert.NoError(t, err)
assert.Equal(t, expected, actual)

_, err = HashTransactionV0(&txe.V0.Tx, "")
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "empty network passphrase")
}
assert.Contains(t, err.Error(), "empty network passphrase")

tx := xdr.Transaction{
SourceAccount: txe.SourceAccount(),
Expand All @@ -39,13 +36,35 @@ func TestHashTransaction(t *testing.T) {
TimeBounds: txe.TimeBounds(),
}
actual, err = HashTransaction(&tx, TestNetworkPassphrase)
if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
assert.NoError(t, err)
assert.Equal(t, expected, actual)

// sadpath: empty passphrase
_, err = HashTransaction(&tx, "")
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "empty network passphrase")
assert.Contains(t, err.Error(), "empty network passphrase")

feeBumpTx := xdr.FeeBumpTransaction{
Fee: 123456,
FeeSource: xdr.MustAddress("GCLOMB72ODBFUGK4E2BK7VMR3RNZ5WSTMEOGNA2YUVHFR3WMH2XBAB6H"),
InnerTx: xdr.FeeBumpTransactionInnerTx{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: tx,
Signatures: []xdr.DecoratedSignature{},
},
},
}

expected = [32]uint8{
0x4d, 0x4c, 0xe9, 0x2, 0x63, 0x72, 0x27, 0xfb,
0x8b, 0x52, 0x2a, 0xe4, 0x8c, 0xcd, 0xd6, 0x9d,
0x32, 0x51, 0x72, 0x46, 0xd9, 0xfc, 0x23, 0xff,
0x8b, 0x7a, 0x85, 0xdd, 0x4b, 0xbc, 0xef, 0x5f,
}
actual, err = HashFeeBumpTransaction(&feeBumpTx, TestNetworkPassphrase)
assert.NoError(t, err)
assert.Equal(t, expected, actual)

_, err = HashFeeBumpTransaction(&feeBumpTx, "")
assert.Contains(t, err.Error(), "empty network passphrase")
}
7 changes: 3 additions & 4 deletions xdr/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// ExampleUnmarshal shows the lowest-level process to decode a base64
// envelope encoded in base64.
func ExampleUnmarshal() {
data := "AAAAAGL8HQvQkbK2HA3WVjRrKmjX00fG8sLI7m0ERwJW/AX3AAAACgAAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAArqN6LeOagjxMaUP96Bzfs9e0corNZXzBWJkFoK7kvkwAAAAAO5rKAAAAAAAAAAABVvwF9wAAAEAKZ7IPj/46PuWU6ZOtyMosctNAkXRNX9WCAI5RnfRk+AyxDLoDZP/9l3NvsxQtWj9juQOuoBlFLnWu8intgxQA"
data := "AAAAAgAAAABi/B0L0JGythwN1lY0aypo19NHxvLCyO5tBEcCVvwF9wAAAAoAAAAAAAAAAQAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAK6jei3jmoI8TGlD/egc37PXtHKKzWV8wViZBaCu5L5MAAAAADuaygAAAAAAAAAAAVb8BfcAAABACmeyD4/+Oj7llOmTrcjKLHLTQJF0TV/VggCOUZ30ZPgMsQy6A2T//Zdzb7MULVo/Y7kDrqAZRS51rvIp7YMUAA=="

rawr := strings.NewReader(data)
b64r := base64.NewDecoder(base64.StdEncoding, rawr)
Expand All @@ -27,10 +27,9 @@ func ExampleUnmarshal() {
log.Fatal(err)
}

// TODO update example to use v1 tx
operations := tx.MustV0().Tx.Operations
operations := tx.Operations()
fmt.Printf("This tx has %d operations\n", len(operations))
// Output: read 192 bytes
// Output: read 196 bytes
// This tx has 1 operations
}

Expand Down
Loading

0 comments on commit bec3ae2

Please sign in to comment.