Skip to content

Commit

Permalink
make IsValidAddr private and extend regex to account for 0 length str…
Browse files Browse the repository at this point in the history
…ing (#723)

* make IsValidAddr private and extend regex to account for 0 length strings

* add tests

* remove spaces and commas from LongString
  • Loading branch information
colin-axner authored Jan 13, 2022
1 parent d882b43 commit a4b9517
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
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

0 comments on commit a4b9517

Please sign in to comment.