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

make IsValidAddr private and extend regex to account for 0 length string #723

Merged
merged 6 commits into from
Jan 13, 2022
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
8 changes: 4 additions & 4 deletions modules/apps/27-interchain-accounts/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var (
// DefaultMaxAddrLength defines the default maximum character length used in validation of addresses
var DefaultMaxAddrLength = 128

// IsValidAddr defines a regular expression to check if the provided string consists of
// strictly alphanumeric characters
var IsValidAddr = regexp.MustCompile("^[a-zA-Z0-9]*$").MatchString
// isValidAddr defines a regular expression to check if the provided string consists of
// strictly alphanumeric characters and is non empty.
var isValidAddr = regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString

// InterchainAccountI wraps the authtypes.AccountI interface
type InterchainAccountI interface {
Expand All @@ -48,7 +48,7 @@ func GenerateAddress(moduleAccAddr sdk.AccAddress, portID string) sdk.AccAddress
// ValidateAccountAddress performs basic validation of interchain account addresses, enforcing constraints
// on address length and character set
func ValidateAccountAddress(addr string) error {
if !IsValidAddr(addr) || len(addr) == 0 || len(addr) > DefaultMaxAddrLength {
if !isValidAddr(addr) || len(addr) > DefaultMaxAddrLength {
return sdkerrors.Wrapf(
ErrInvalidAccountAddress,
"address must contain strictly alphanumeric characters, not exceeding %d characters in length",
Expand Down
46 changes: 46 additions & 0 deletions modules/apps/27-interchain-accounts/types/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,52 @@ func (suite *TypesTestSuite) TestGenerateAddress() {
suite.Require().NotEmpty(accAddr)
}

func (suite *TypesTestSuite) TestValidateAccountAddress() {
testCases := []struct {
name string
address string
expPass bool
}{
{
"success",
TestOwnerAddress,
true,
},
{
"success with single character",
"a",
true,
},
{
"empty string",
"",
false,
},
{
"only spaces",
" ",
false,
},
{
"address is too long",
ibctesting.LongString,
false,
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
err := types.ValidateAccountAddress(tc.address)

if tc.expPass {
suite.Require().NoError(err, tc.name)
} else {
suite.Require().Error(err, tc.name)
}
})
}
}

func (suite *TypesTestSuite) TestInterchainAccount() {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
Expand Down
2 changes: 2 additions & 0 deletions testing/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
// used for testing proposals
Title = "title"
Description = "description"

LongString = "LoremipsumdolorsitameconsecteturadipiscingeliseddoeiusmodtemporincididuntutlaboreetdoloremagnaaliquUtenimadminimveniamquisnostrudexercitationullamcolaborisnisiutaliquipexeacommodoconsequDuisauteiruredolorinreprehenderitinvoluptateelitsseillumoloreufugiatnullaariaturEcepteurintoccaectupidatatonroidentuntnulpauifficiaeseruntmollitanimidestlaborum"
)

var (
Expand Down