test(types/address): add additional unit tests for address.LengthPrefix and a… (#19964)

This commit is contained in:
Emil Georgiev 2024-04-05 16:12:50 +03:00 committed by GitHub
parent c7534584dd
commit dcec6adaf5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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 {
@ -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 {
@ -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)
})
}
}