diff --git a/modules/light-clients/06-solomachine/client_state_test.go b/modules/light-clients/06-solomachine/client_state_test.go index e8e1ab6d494..d70735b3b17 100644 --- a/modules/light-clients/06-solomachine/client_state_test.go +++ b/modules/light-clients/06-solomachine/client_state_test.go @@ -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" @@ -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"), }, } @@ -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()) } }) } diff --git a/modules/light-clients/06-solomachine/codec_test.go b/modules/light-clients/06-solomachine/codec_test.go index 7cf1089d59c..f683f91fd7c 100644 --- a/modules/light-clients/06-solomachine/codec_test.go +++ b/modules/light-clients/06-solomachine/codec_test.go @@ -1,6 +1,7 @@ package solomachine_test import ( + "errors" "testing" "github.com/stretchr/testify/require" @@ -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"), }, } @@ -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()) } }) } diff --git a/modules/light-clients/06-solomachine/consensus_state_test.go b/modules/light-clients/06-solomachine/consensus_state_test.go index 47abff209b9..6a385dca309 100644 --- a/modules/light-clients/06-solomachine/consensus_state_test.go +++ b/modules/light-clients/06-solomachine/consensus_state_test.go @@ -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" @@ -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", @@ -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", @@ -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", @@ -52,7 +54,7 @@ func (suite *SoloMachineTestSuite) TestConsensusStateValidateBasic() { Diversifier: sm.Diversifier, PublicKey: nil, }, - false, + errors.New("public key cannot be empty: invalid consensus state"), }, } @@ -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()) } }) } diff --git a/modules/light-clients/06-solomachine/header_test.go b/modules/light-clients/06-solomachine/header_test.go index e23e66bc2b7..29c2b6571ab 100644 --- a/modules/light-clients/06-solomachine/header_test.go +++ b/modules/light-clients/06-solomachine/header_test.go @@ -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" @@ -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", @@ -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", @@ -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", @@ -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", @@ -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"), }, } @@ -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()) } }) } diff --git a/modules/light-clients/06-solomachine/light_client_module_test.go b/modules/light-clients/06-solomachine/light_client_module_test.go index b011d130931..08c6d1f2945 100644 --- a/modules/light-clients/06-solomachine/light_client_module_test.go +++ b/modules/light-clients/06-solomachine/light_client_module_test.go @@ -1,6 +1,7 @@ package solomachine_test import ( + "errors" "fmt" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -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", @@ -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"), }, } @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/modules/light-clients/06-solomachine/misbehaviour_test.go b/modules/light-clients/06-solomachine/misbehaviour_test.go index b74023aee35..d16ff879d60 100644 --- a/modules/light-clients/06-solomachine/misbehaviour_test.go +++ b/modules/light-clients/06-solomachine/misbehaviour_test.go @@ -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" @@ -19,61 +21,61 @@ func (suite *SoloMachineTestSuite) TestMisbehaviourValidateBasic() { testCases := []struct { name string malleateMisbehaviour func(misbehaviour *solomachine.Misbehaviour) - expPass bool + expErr error }{ { "valid misbehaviour", func(*solomachine.Misbehaviour) {}, - true, + nil, }, { "sequence is zero", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.Sequence = 0 }, - false, + errors.New("sequence cannot be 0: invalid light client misbehaviour"), }, { "signature one sig is empty", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureOne.Signature = []byte{} }, - false, + errors.New("signature one failed basic validation: signature cannot be empty: invalid signature and data"), }, { "signature two sig is empty", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureTwo.Signature = []byte{} }, - false, + errors.New("signature two failed basic validation: signature cannot be empty: invalid signature and data"), }, { "signature one data is empty", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureOne.Data = nil }, - false, + errors.New("signature one failed basic validation: data for signature cannot be empty: invalid signature and data"), }, { "signature two data is empty", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureTwo.Data = []byte{} }, - false, + errors.New("signature two failed basic validation: data for signature cannot be empty: invalid signature and data"), }, { "signatures are identical", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureTwo.Signature = misbehaviour.SignatureOne.Signature }, - false, + errors.New("misbehaviour signatures cannot be equal: invalid light client misbehaviour"), }, { "data signed is identical but path differs", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureTwo.Data = misbehaviour.SignatureOne.Data }, - true, + nil, }, { "data signed and path are identical", @@ -81,31 +83,35 @@ func (suite *SoloMachineTestSuite) TestMisbehaviourValidateBasic() { misbehaviour.SignatureTwo.Path = misbehaviour.SignatureOne.Path misbehaviour.SignatureTwo.Data = misbehaviour.SignatureOne.Data }, - false, + errors.New("misbehaviour signature data must be signed over different messages: invalid light client misbehaviour"), }, { "data path for SignatureOne is unspecified", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureOne.Path = []byte{} - }, false, + }, + errors.New("signature one failed basic validation: path for signature cannot be empty: invalid signature and data"), }, { "data path for SignatureTwo is unspecified", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureTwo.Path = []byte{} - }, false, + }, + errors.New("signature two failed basic validation: path for signature cannot be empty: invalid signature and data"), }, { "timestamp for SignatureOne is zero", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureOne.Timestamp = 0 - }, false, + }, + errors.New("signature one failed basic validation: timestamp cannot be 0: invalid signature and data"), }, { "timestamp for SignatureTwo is zero", func(misbehaviour *solomachine.Misbehaviour) { misbehaviour.SignatureTwo.Timestamp = 0 - }, false, + }, + errors.New("signature two failed basic validation: timestamp cannot be 0: invalid signature and data"), }, } @@ -118,10 +124,11 @@ func (suite *SoloMachineTestSuite) TestMisbehaviourValidateBasic() { err := misbehaviour.ValidateBasic() - if tc.expPass { + if tc.expErr == nil { suite.Require().NoError(err) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) } diff --git a/modules/light-clients/06-solomachine/proof_test.go b/modules/light-clients/06-solomachine/proof_test.go index 6c382c1979a..a4fc0aba031 100644 --- a/modules/light-clients/06-solomachine/proof_test.go +++ b/modules/light-clients/06-solomachine/proof_test.go @@ -1,6 +1,8 @@ package solomachine_test import ( + "errors" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/types/tx/signing" @@ -23,31 +25,31 @@ func (suite *SoloMachineTestSuite) TestVerifySignature() { name string publicKey cryptotypes.PubKey sigData signing.SignatureData - expPass bool + expErr error }{ { "single signature with regular public key", suite.solomachine.PublicKey, singleSigData, - true, + nil, }, { "multi signature with multisig public key", suite.solomachineMulti.PublicKey, multiSigData, - true, + nil, }, { "single signature with multisig public key", suite.solomachineMulti.PublicKey, singleSigData, - false, + errors.New("invalid signature data type, expected *signing.MultiSignatureData, got *signing.MultiSignatureData: signature verification failed"), }, { "multi signature with regular public key", suite.solomachine.PublicKey, multiSigData, - false, + errors.New("invalid signature data type, expected *signing.SingleSignatureData, got *signing.SingleSignatureData: signature verification failed"), }, } @@ -57,10 +59,11 @@ func (suite *SoloMachineTestSuite) TestVerifySignature() { suite.Run(tc.name, func() { err := solomachine.VerifySignature(tc.publicKey, signBytes, tc.sigData) - if tc.expPass { + if tc.expErr == nil { suite.Require().NoError(err) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) }