forked from cosmos/ibc-go
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor validategrpc into an internal shared package (cosmos#…
…6080) * Refactor validateGRPCRequest * Refactor test * Linter * Remove duplicated import * Linter (again) * Refactor last instance of validateGRPCRequest --------- Co-authored-by: Carlos Rodriguez <[email protected]>
- Loading branch information
1 parent
b465156
commit f1b5244
Showing
5 changed files
with
94 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package validate | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
host "github.com/cosmos/ibc-go/v8/modules/core/24-host" | ||
) | ||
|
||
// GRPCRequest validates that the portID and channelID of a gRPC Request are valid identifiers. | ||
func GRPCRequest(portID, channelID string) error { | ||
if err := host.PortIdentifierValidator(portID); err != nil { | ||
return status.Error(codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
if err := host.ChannelIdentifierValidator(channelID); err != nil { | ||
return status.Error(codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package validate_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/ibc-go/v8/internal/validate" | ||
) | ||
|
||
func TestGRPCRequest(t *testing.T) { | ||
const ( | ||
validID = "validIdentifier" | ||
invalidID = "" | ||
) | ||
testCases := []struct { | ||
msg string | ||
portID string | ||
channelID string | ||
expPass bool | ||
}{ | ||
{ | ||
"success", | ||
validID, | ||
validID, | ||
true, | ||
}, | ||
{ | ||
"invalid portID", | ||
invalidID, | ||
validID, | ||
false, | ||
}, | ||
{ | ||
"invalid channelID", | ||
validID, | ||
invalidID, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("Case %s", tc.msg), func(t *testing.T) { | ||
err := validate.GRPCRequest(tc.portID, tc.channelID) | ||
if tc.expPass { | ||
require.NoError(t, err, tc.msg) | ||
} else { | ||
require.Error(t, err, tc.msg) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters