Skip to content

Commit

Permalink
(chore) update tests in light-clients/06-solomachine to expect specif…
Browse files Browse the repository at this point in the history
…ic error (cosmos#7659)

* update err messages

* fix lint

* update tests

---------

Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
PrathyushaLakkireddy and DimitrisJim authored Dec 11, 2024
1 parent 561c6fe commit 13ed268
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 60 deletions.
18 changes: 10 additions & 8 deletions modules/light-clients/06-solomachine/client_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package solomachine_test

import (
"bytes"
"errors"

solomachine "github.com/cosmos/ibc-go/v9/modules/light-clients/06-solomachine"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
Expand All @@ -21,37 +22,37 @@ func (suite *SoloMachineTestSuite) TestClientStateValidate() {
testCases := []struct {
name string
clientState *solomachine.ClientState
expPass bool
expErr error
}{
{
"valid client state",
sm.ClientState(),
true,
nil,
},
{
"empty ClientState",
&solomachine.ClientState{},
false,
errors.New("sequence cannot be 0: light client is invalid"),
},
{
"sequence is zero",
solomachine.NewClientState(0, &solomachine.ConsensusState{sm.ConsensusState().PublicKey, sm.Diversifier, sm.Time}),
false,
errors.New("sequence cannot be 0: light client is invalid"),
},
{
"timestamp is zero",
solomachine.NewClientState(1, &solomachine.ConsensusState{sm.ConsensusState().PublicKey, sm.Diversifier, 0}),
false,
errors.New("timestamp cannot be 0: invalid consensus state"),
},
{
"diversifier is blank",
solomachine.NewClientState(1, &solomachine.ConsensusState{sm.ConsensusState().PublicKey, " ", 1}),
false,
errors.New("diversifier cannot contain only spaces: invalid consensus state"),
},
{
"pubkey is empty",
solomachine.NewClientState(1, &solomachine.ConsensusState{nil, sm.Diversifier, sm.Time}),
false,
errors.New("public key cannot be empty: invalid consensus state"),
},
}

Expand All @@ -61,10 +62,11 @@ func (suite *SoloMachineTestSuite) TestClientStateValidate() {
suite.Run(tc.name, func() {
err := tc.clientState.Validate()

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
suite.Require().ErrorContains(err, tc.expErr.Error())
}
})
}
Expand Down
16 changes: 9 additions & 7 deletions modules/light-clients/06-solomachine/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package solomachine_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,32 +16,32 @@ func TestCodecTypeRegistration(t *testing.T) {
testCases := []struct {
name string
typeURL string
expPass bool
expErr error
}{
{
"success: ClientState",
sdk.MsgTypeURL(&solomachine.ClientState{}),
true,
nil,
},
{
"success: ConsensusState",
sdk.MsgTypeURL(&solomachine.ConsensusState{}),
true,
nil,
},
{
"success: Header",
sdk.MsgTypeURL(&solomachine.Header{}),
true,
nil,
},
{
"success: Misbehaviour",
sdk.MsgTypeURL(&solomachine.Misbehaviour{}),
true,
nil,
},
{
"type not registered on codec",
"ibc.invalid.MsgTypeURL",
false,
errors.New("unable to resolve type URL ibc.invalid.MsgTypeURL"),
},
}

Expand All @@ -51,12 +52,13 @@ func TestCodecTypeRegistration(t *testing.T) {
encodingCfg := moduletestutil.MakeTestEncodingConfig(solomachine.AppModuleBasic{})
msg, err := encodingCfg.Codec.InterfaceRegistry().Resolve(tc.typeURL)

if tc.expPass {
if tc.expErr == nil {
require.NotNil(t, msg)
require.NoError(t, err)
} else {
require.Nil(t, msg)
require.Error(t, err)
require.Contains(t, err.Error(), tc.expErr.Error())
}
})
}
Expand Down
15 changes: 9 additions & 6 deletions modules/light-clients/06-solomachine/consensus_state_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package solomachine_test

import (
"errors"

"github.com/cosmos/ibc-go/v9/modules/core/exported"
solomachine "github.com/cosmos/ibc-go/v9/modules/light-clients/06-solomachine"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
Expand All @@ -20,12 +22,12 @@ func (suite *SoloMachineTestSuite) TestConsensusStateValidateBasic() {
testCases := []struct {
name string
consensusState *solomachine.ConsensusState
expPass bool
expErr error
}{
{
"valid consensus state",
sm.ConsensusState(),
true,
nil,
},
{
"timestamp is zero",
Expand All @@ -34,7 +36,7 @@ func (suite *SoloMachineTestSuite) TestConsensusStateValidateBasic() {
Timestamp: 0,
Diversifier: sm.Diversifier,
},
false,
errors.New("timestamp cannot be 0: invalid consensus state"),
},
{
"diversifier is blank",
Expand All @@ -43,7 +45,7 @@ func (suite *SoloMachineTestSuite) TestConsensusStateValidateBasic() {
Timestamp: sm.Time,
Diversifier: " ",
},
false,
errors.New("diversifier cannot contain only spaces: invalid consensus state"),
},
{
"pubkey is nil",
Expand All @@ -52,7 +54,7 @@ func (suite *SoloMachineTestSuite) TestConsensusStateValidateBasic() {
Diversifier: sm.Diversifier,
PublicKey: nil,
},
false,
errors.New("public key cannot be empty: invalid consensus state"),
},
}

Expand All @@ -62,10 +64,11 @@ func (suite *SoloMachineTestSuite) TestConsensusStateValidateBasic() {
suite.Run(tc.name, func() {
err := tc.consensusState.ValidateBasic()

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
suite.Require().ErrorContains(err, tc.expErr.Error())
}
})
}
Expand Down
21 changes: 12 additions & 9 deletions modules/light-clients/06-solomachine/header_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package solomachine_test

import (
"errors"

"github.com/cosmos/ibc-go/v9/modules/core/exported"
solomachine "github.com/cosmos/ibc-go/v9/modules/light-clients/06-solomachine"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
Expand All @@ -13,14 +15,14 @@ func (suite *SoloMachineTestSuite) TestHeaderValidateBasic() {
header := sm.CreateHeader(sm.Diversifier)

cases := []struct {
name string
header *solomachine.Header
expPass bool
name string
header *solomachine.Header
expErr error
}{
{
"valid header",
header,
true,
nil,
},
{
"timestamp is zero",
Expand All @@ -30,7 +32,7 @@ func (suite *SoloMachineTestSuite) TestHeaderValidateBasic() {
NewPublicKey: header.NewPublicKey,
NewDiversifier: header.NewDiversifier,
},
false,
errors.New("timestamp cannot be zero: invalid client header"),
},
{
"signature is empty",
Expand All @@ -40,7 +42,7 @@ func (suite *SoloMachineTestSuite) TestHeaderValidateBasic() {
NewPublicKey: header.NewPublicKey,
NewDiversifier: header.NewDiversifier,
},
false,
errors.New("signature cannot be empty: invalid client header"),
},
{
"diversifier contains only spaces",
Expand All @@ -50,7 +52,7 @@ func (suite *SoloMachineTestSuite) TestHeaderValidateBasic() {
NewPublicKey: header.NewPublicKey,
NewDiversifier: " ",
},
false,
errors.New("diversifier cannot contain only spaces: invalid client header"),
},
{
"public key is nil",
Expand All @@ -60,7 +62,7 @@ func (suite *SoloMachineTestSuite) TestHeaderValidateBasic() {
NewPublicKey: nil,
NewDiversifier: header.NewDiversifier,
},
false,
errors.New("new public key cannot be empty: invalid client header"),
},
}

Expand All @@ -72,10 +74,11 @@ func (suite *SoloMachineTestSuite) TestHeaderValidateBasic() {
suite.Run(tc.name, func() {
err := tc.header.ValidateBasic()

if tc.expPass {
if tc.expErr == nil {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
suite.Require().ErrorContains(err, tc.expErr.Error())
}
})
}
Expand Down
19 changes: 10 additions & 9 deletions modules/light-clients/06-solomachine/light_client_module_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package solomachine_test

import (
"errors"
"fmt"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -164,7 +165,7 @@ func (suite *SoloMachineTestSuite) TestInitialize() {
"failure: invalid consensus state: Tendermint consensus state",
&ibctm.ConsensusState{},
sm.ClientState(),
fmt.Errorf("proto: wrong wireType = 0 for field TypeUrl"),
errors.New("proto: wrong wireType = 0 for field TypeUrl"),
},
{
"failure: invalid consensus state: consensus state does not match consensus state in client",
Expand All @@ -182,7 +183,7 @@ func (suite *SoloMachineTestSuite) TestInitialize() {
"failure: invalid client state: Tendermint client state",
sm.ConsensusState(),
&ibctm.ClientState{},
fmt.Errorf("proto: wrong wireType = 2 for field IsFrozen"),
errors.New("proto: wrong wireType = 2 for field IsFrozen"),
},
}

Expand Down Expand Up @@ -551,7 +552,7 @@ func (suite *SoloMachineTestSuite) TestVerifyMembership() {
path = sm.GetClientStatePath(counterpartyClientIdentifier)
proof = []byte("invalid proof")
},
fmt.Errorf("failed to unmarshal proof into type"),
errors.New("failed to unmarshal proof into type"),
},
{
"failure: consensus state timestamp is greater than signature",
Expand Down Expand Up @@ -598,7 +599,7 @@ func (suite *SoloMachineTestSuite) TestVerifyMembership() {
proof, err = suite.chainA.Codec.Marshal(signatureDoc)
suite.Require().NoError(err)
},
fmt.Errorf("failed to unmarshal proof into type"),
errors.New("failed to unmarshal proof into type"),
},
{
"failure: proof is nil",
Expand Down Expand Up @@ -771,7 +772,7 @@ func (suite *SoloMachineTestSuite) TestVerifyNonMembership() {
path = sm.GetClientStatePath(counterpartyClientIdentifier)
proof = []byte("invalid proof")
},
fmt.Errorf("failed to unmarshal proof into type"),
errors.New("failed to unmarshal proof into type"),
},
{
"failure: consensus state timestamp is greater than signature",
Expand Down Expand Up @@ -818,7 +819,7 @@ func (suite *SoloMachineTestSuite) TestVerifyNonMembership() {
proof, err = suite.chainA.Codec.Marshal(signatureDoc)
suite.Require().NoError(err)
},
fmt.Errorf("failed to unmarshal proof into type"),
errors.New("failed to unmarshal proof into type"),
},
{
"failure: proof is nil",
Expand Down Expand Up @@ -1297,7 +1298,7 @@ func (suite *SoloMachineTestSuite) TestVerifyClientMessageHeader() {
h := sm.CreateHeader(sm.Diversifier)
h.Signature = suite.GetInvalidProof()
clientMsg = h
}, fmt.Errorf("proto: wrong wireType = 0 for field Multi"),
}, errors.New("proto: wrong wireType = 0 for field Multi"),
},
{
"failure: invalid timestamp in header",
Expand Down Expand Up @@ -1469,7 +1470,7 @@ func (suite *SoloMachineTestSuite) TestVerifyClientMessageMisbehaviour() {

m.SignatureOne.Signature = suite.GetInvalidProof()
clientMsg = m
}, fmt.Errorf("proto: wrong wireType = 0 for field Multi"),
}, errors.New("proto: wrong wireType = 0 for field Multi"),
},
{
"failure: invalid SignatureTwo SignatureData",
Expand All @@ -1478,7 +1479,7 @@ func (suite *SoloMachineTestSuite) TestVerifyClientMessageMisbehaviour() {

m.SignatureTwo.Signature = suite.GetInvalidProof()
clientMsg = m
}, fmt.Errorf("proto: wrong wireType = 0 for field Multi"),
}, errors.New("proto: wrong wireType = 0 for field Multi"),
},
{
"failure: invalid SignatureOne timestamp",
Expand Down
Loading

0 comments on commit 13ed268

Please sign in to comment.