diff --git a/types/address/store_key_test.go b/types/address/store_key_test.go index ac28f814cc3..c68f9273e55 100644 --- a/types/address/store_key_test.go +++ b/types/address/store_key_test.go @@ -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) + }) + } +}