2020-07-13 20:01:45 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-06 20:56:40 +00:00
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2020-07-13 20:01:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageValidate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
storage Storage
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid storage",
|
|
|
|
Storage{
|
|
|
|
NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty storage key bytes",
|
|
|
|
Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: ethcmn.Hash{}.String()},
|
2020-07-13 20:01:45 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"duplicated storage key",
|
|
|
|
Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: ethcmn.BytesToHash([]byte{1, 2, 3}).String()},
|
|
|
|
{Key: ethcmn.BytesToHash([]byte{1, 2, 3}).String()},
|
2020-07-13 20:01:45 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
err := tc.storage.Validate()
|
|
|
|
if tc.expPass {
|
|
|
|
require.NoError(t, err, tc.name)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err, tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageCopy(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
storage Storage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"single storage",
|
|
|
|
Storage{
|
|
|
|
NewState(ethcmn.BytesToHash([]byte{1, 2, 3}), ethcmn.BytesToHash([]byte{1, 2, 3})),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty storage key value bytes",
|
|
|
|
Storage{
|
2021-01-06 20:56:40 +00:00
|
|
|
{Key: ethcmn.Hash{}.String(), Value: ethcmn.Hash{}.String()},
|
2020-07-13 20:01:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty storage",
|
|
|
|
Storage{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
require.Equal(t, tc.storage, tc.storage.Copy(), tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageString(t *testing.T) {
|
|
|
|
storage := Storage{NewState(ethcmn.BytesToHash([]byte("key")), ethcmn.BytesToHash([]byte("value")))}
|
2021-04-18 15:54:18 +00:00
|
|
|
str := "key:\"0x00000000000000000000000000000000000000000000000000000000006b6579\" value:\"0x00000000000000000000000000000000000000000000000000000076616c7565\"\n"
|
2020-07-13 20:01:45 +00:00
|
|
|
require.Equal(t, str, storage.String())
|
|
|
|
}
|