Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to bring us closer to 100% #80

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/ethereum/deploy_contract_prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ func TestDeployContractPrepareBadABIDefinition(t *testing.T) {

}

func TestDeployContractPrepareBadErrorABI(t *testing.T) {

ctx, c, _, done := newTestConnector(t)
defer done()

var req ffcapi.ContractDeployPrepareRequest
err := json.Unmarshal([]byte(samplePrepareDeployTX), &req)
req.Errors = []*fftypes.JSONAny{fftypes.JSONAnyPtr(`[`)}
assert.NoError(t, err)
_, reason, err := c.DeployContractPrepare(ctx, &req)

assert.Regexp(t, "FF23013", err)
assert.Equal(t, ffcapi.ErrorReasonInvalidInputs, reason)

}

func TestDeployContractPrepareEstimateNoConstructor(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
Expand Down
30 changes: 30 additions & 0 deletions internal/ethereum/event_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,36 @@ func TestFilterEnrichEthLogMethodInputsOk(t *testing.T) {

}

func TestFilterEnrichEthLogMethodInputsTxInfoWithErr(t *testing.T) {

l, mRPC, _ := newTestListener(t, true)

var abiEvent *abi.Entry
err := json.Unmarshal([]byte(abiTransferEvent), &abiEvent)
assert.NoError(t, err)

mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getBlockByHash", mock.MatchedBy(func(bh string) bool {
return bh == "0x6b012339fbb85b70c58ecfd97b31950c4a28bcef5226e12dbe551cb1abaf3b4c"
}), false).Return(nil).Run(func(args mock.Arguments) {
*args[1].(**blockInfoJSONRPC) = &blockInfoJSONRPC{
Number: ethtypes.NewHexInteger64(1024),
}
})
mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionByHash", mock.MatchedBy(func(th ethtypes.HexBytes0xPrefix) bool {
return th.String() == "0x1a1f797ee000c529b6a2dd330cedd0d081417a30d16a4eecb3f863ab4657246f"
})).Return(&rpcbackend.RPCError{Message: "pop"}).Run(func(args mock.Arguments) {
*args[1].(**txInfoJSONRPC) = &txInfoJSONRPC{
From: ethtypes.MustNewAddress("0x3968ef051b422d3d1cdc182a88bba8dd922e6fa4"),
Input: ethtypes.MustNewHexBytes0xPrefix("0xa9059cbb000000000000000000000000d0f2f5103fd050739a9fb567251bc460cc24d09100000000000000000000000000000000000000000000000000000000000003e8"),
}
}).Once()

_, ok, err := l.filterEnrichEthLog(context.Background(), l.config.filters[0], sampleTransferLog())
assert.False(t, ok)
assert.Error(t, err)

}

func TestFilterEnrichEthLogTXInfoFail(t *testing.T) {

l, mRPC, _ := newTestListener(t, true)
Expand Down
35 changes: 35 additions & 0 deletions internal/ethereum/exec_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"testing"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-evmconnect/internal/msgs"
"github.com/hyperledger/firefly-signer/pkg/ethsigner"
Expand Down Expand Up @@ -188,6 +189,40 @@ func TestExecQueryCustomErrorRevertDataBadOutput(t *testing.T) {

}

func TestExecQueryCustomErrorBadRevertData(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_call", mock.Anything, "latest").
Return(&rpcbackend.RPCError{Message: "pop", Data: "bad data"}).
Run(func(args mock.Arguments) {
*(args[1].(*ethtypes.HexBytes0xPrefix)) = ethtypes.MustNewHexBytes0xPrefix("0x053b20290000000000000000000000000000000000000000000000000000000000000020")
})

var req ffcapi.QueryInvokeRequest
err := json.Unmarshal([]byte(sampleExecQuery), &req)
assert.NoError(t, err)
_, _, err = c.QueryInvoke(ctx, &req)
assert.Error(t, err)
assert.Regexp(t, "pop", err)

}

func TestExecQueryBadErrorsABI(t *testing.T) {

ctx, c, _, done := newTestConnector(t)
defer done()

var req ffcapi.QueryInvokeRequest
err := json.Unmarshal([]byte(sampleExecQuery), &req)
req.Errors = []*fftypes.JSONAny{fftypes.JSONAnyPtr(`[`)}
assert.NoError(t, err)
_, reason, err := c.QueryInvoke(ctx, &req)
assert.Equal(t, ffcapi.ErrorReasonInvalidInputs, reason)

}

func TestExecQueryBadRevertData(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
Expand Down
24 changes: 24 additions & 0 deletions internal/ethereum/prepare_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ func TestPrepareTransactionWithEstimateFail(t *testing.T) {

}

func TestPrepareTransactionWithEstimateFailBadData(t *testing.T) {

ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("CallRPC", mock.Anything, mock.Anything, "eth_estimateGas",
mock.MatchedBy(func(tx *ethsigner.Transaction) bool {
assert.Equal(t, "0x60fe47b100000000000000000000000000000000000000000000000000000000feedbeef", tx.Data.String())
return true
})).
Return(&rpcbackend.RPCError{Message: "pop", Data: "bad data"}).
Run(func(args mock.Arguments) {
args[1].(*ethtypes.HexInteger).BigInt().SetString("12345", 10)
})

var req ffcapi.TransactionPrepareRequest
err := json.Unmarshal([]byte(samplePrepareTXEstimateGas), &req)
assert.NoError(t, err)
_, _, err = c.TransactionPrepare(ctx, &req)
assert.Error(t, err)
assert.Regexp(t, "invalid character", err)

}

func TestPrepareTransactionWithBadMethod(t *testing.T) {

ctx, c, _, done := newTestConnector(t)
Expand Down
4 changes: 3 additions & 1 deletion internal/ethereum/send_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,14 @@ func TestSendTransactionFail(t *testing.T) {
}

func TestSendErrorMapping(t *testing.T) {

assert.Equal(t, ffcapi.ErrorReasonNonceTooLow, mapError(sendRPCMethods, fmt.Errorf("nonce too low")))
assert.Equal(t, ffcapi.ErrorReasonInsufficientFunds, mapError(sendRPCMethods, fmt.Errorf("insufficient funds")))
assert.Equal(t, ffcapi.ErrorReasonTransactionUnderpriced, mapError(sendRPCMethods, fmt.Errorf("transaction underpriced")))
assert.Equal(t, ffcapi.ErrorKnownTransaction, mapError(sendRPCMethods, fmt.Errorf("known transaction")))
}

func TestCallErrorMapping(t *testing.T) {
assert.Equal(t, ffcapi.ErrorReasonTransactionReverted, mapError(callRPCMethods, fmt.Errorf("execution reverted")))
}

func TestSendTransactionBadFrom(t *testing.T) {
Expand Down