Skip to content

Commit

Permalink
test(types/address): add additional unit tests for address.LengthPref…
Browse files Browse the repository at this point in the history
…ix and a… (cosmos#19964)
  • Loading branch information
EmilGeorgiev authored Apr 5, 2024
1 parent c753458 commit dcec6ad
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion types/address/store_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (suite *StoreKeySuite) TestLengthPrefix() {
require := suite.Require()
addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
var addr0byte []byte
addr256byte := make([]byte, 256)

tests := []struct {
Expand All @@ -28,11 +29,11 @@ func (suite *StoreKeySuite) TestLengthPrefix() {
}{
{"10-byte address", addr10byte, append([]byte{byte(10)}, addr10byte...), false},
{"20-byte address", addr20byte, append([]byte{byte(20)}, addr20byte...), false},
{"0-byte address", addr0byte, addr0byte, false},
{"256-byte address (too long)", addr256byte, nil, true},
}

for _, tt := range tests {
tt := tt
suite.Run(tt.name, func() {
storeKey, err := address.LengthPrefix(tt.addr)
if tt.expErr {
Expand All @@ -44,3 +45,34 @@ func (suite *StoreKeySuite) TestLengthPrefix() {
})
}
}

func (suite *StoreKeySuite) TestMustLengthPrefix() {
require := suite.Require()
addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
addr256byte := make([]byte, 256)

tests := []struct {
name string
addr []byte
expStoreKey []byte
expPanic bool
}{
{"10-byte address with length prefix", addr10byte, append([]byte{byte(10)}, addr10byte...), false},
{"256-byte address triggers panic due to excessive length", addr256byte, nil, true},
}

for _, tt := range tests {
suite.Run(tt.name, func() {
defer func() {
r := recover()
if tt.expPanic {
require.NotNil(r)
} else {
require.Nil(r)
}
}()
storeKey := address.MustLengthPrefix(tt.addr)
require.Equal(tt.expStoreKey, storeKey)
})
}
}

0 comments on commit dcec6ad

Please sign in to comment.