Skip to content

Commit

Permalink
CR's fixes: test only public API
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed May 3, 2024
1 parent 770e0ba commit e749630
Showing 1 changed file with 112 additions and 99 deletions.
211 changes: 112 additions & 99 deletions x/evm/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,118 @@ func TestParamsValidate(t *testing.T) {
}
}

func TestEnabledPrecompilesOrderInBytesRepr(t *testing.T) {
addr1 := "0x1000000000000000000000000000000000000000"
addr2 := "0x2000000000000000000000000000000000000000"

// NOTE: we sort in bytes representation, so proper order will be []string{mixedCaseAddr, upperCaseAddr},
// and it differs from lexicographically sorted strings
upperCaseAddr := "0xAB00000000000000000000000000000000000000"
mixedCaseAddr := "0xaA00000000000000000000000000000000000000"

testCases := []struct {
name string
getParams func() Params
errorMsg string
}{
{
name: "success: addresses are sorted",
getParams: func() Params {
params := DefaultParams()
params.EnabledPrecompiles = []string{addr1, addr2}
return params
},
errorMsg: "",
},
{
name: "failure: addresses are in reverse order",
getParams: func() Params {
params := DefaultParams()
params.EnabledPrecompiles = []string{addr2, addr1}
return params
},
errorMsg: "enabled precompiles are not sorted",
},
{
name: "success: addresses are sorted in bytes representation",
getParams: func() Params {
params := DefaultParams()
params.EnabledPrecompiles = []string{mixedCaseAddr, upperCaseAddr}
return params
},
errorMsg: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.getParams().Validate()

if tc.errorMsg == "" {
require.NoError(t, err, tc.name)
} else {
require.ErrorContains(t, err, tc.errorMsg, tc.name)
}
})
}
}

func TestEnabledPrecompilesUniquenessInBytesRepr(t *testing.T) {
addr1 := "0x1000000000000000000000000000000000000000"
addr2 := "0x2000000000000000000000000000000000000000"

// NOTE: we check uniqueness in bytes representation, so lowerCaseAddr and mixedCaseAddr are the same,
// despite it differs in string representation
lowerCaseAddr := "0xab00000000000000000000000000000000000000"
mixedCaseAddr := "0xAb00000000000000000000000000000000000000"

testCases := []struct {
name string
getParams func() Params
errorMsg string
}{
{
name: "success: addresses are unique",
getParams: func() Params {
params := DefaultParams()
params.EnabledPrecompiles = []string{addr1, addr2}
return params
},
errorMsg: "",
},
{
name: "failure: addresses are not unique",
getParams: func() Params {
params := DefaultParams()
params.EnabledPrecompiles = []string{addr1, addr1}
return params
},
errorMsg: "enabled precompiles are not unique",
},
{
name: "failure: addresses are not unique in bytes representation",
getParams: func() Params {
params := DefaultParams()
params.EnabledPrecompiles = []string{lowerCaseAddr, mixedCaseAddr}
return params
},
errorMsg: "enabled precompiles are not unique",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.getParams().Validate()

if tc.errorMsg == "" {
require.NoError(t, err, tc.name)
} else {
require.ErrorContains(t, err, tc.errorMsg, tc.name)
}
})
}
}

func TestParamsEIPs(t *testing.T) {
extraEips := []int64{2929, 1884, 1344}
params := NewParams("ara", false, true, true, DefaultChainConfig(), extraEips, []EIP712AllowedMsg{})
Expand All @@ -113,17 +225,6 @@ func TestParamsValidatePriv(t *testing.T) {
require.NoError(t, validateEnabledPrecompiles(nil))
require.NoError(t, validateEnabledPrecompiles([]string{}))
require.NoError(t, validateEnabledPrecompiles([]string{validEthAddress}))

addr1 := "0x1000000000000000000000000000000000000000"
addr2 := "0x2000000000000000000000000000000000000000"

// check if sorted
require.NoError(t, validateEnabledPrecompiles([]string{addr1, addr2}))
require.Error(t, validateEnabledPrecompiles([]string{addr2, addr1}))

// check if unique
require.NoError(t, validateEnabledPrecompiles([]string{addr1, addr2}))
require.Error(t, validateEnabledPrecompiles([]string{addr1, addr1}))
}

func TestValidateChainConfig(t *testing.T) {
Expand Down Expand Up @@ -244,91 +345,3 @@ func TestCheckIfEnabledPrecompilesAreRegistered(t *testing.T) {
})
}
}

func TestCheckIfSortedInBytesRepr(t *testing.T) {
addr1 := common.HexToAddress("0x1000000000000000000000000000000000000000")
addr2 := common.HexToAddress("0x2000000000000000000000000000000000000000")

// NOTE: we sort in bytes representation, so proper order will be []string{mixedCaseAddr, upperCaseAddr},
// and it differs from lexicographically sorted strings
upperCaseAddr := common.HexToAddress("0xAB00000000000000000000000000000000000000")
mixedCaseAddr := common.HexToAddress("0xaA00000000000000000000000000000000000000")

testCases := []struct {
name string
addrs []common.Address
sorted bool
}{
{
name: "success: addresses are sorted",
addrs: []common.Address{addr1, addr2},
sorted: true,
},
{
name: "failure: addresses are in reverse order",
addrs: []common.Address{addr2, addr1},
sorted: false,
},
{
name: "success: addresses are sorted in bytes representation",
addrs: []common.Address{mixedCaseAddr, upperCaseAddr},
sorted: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := checkIfSortedInBytesRepr(tc.addrs)

if tc.sorted {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
}
})
}
}

func TestCheckIfUniqueInBytesRepr(t *testing.T) {
addr1 := common.HexToAddress("0x1000000000000000000000000000000000000000")
addr2 := common.HexToAddress("0x2000000000000000000000000000000000000000")

// NOTE: we check uniqueness in bytes representation, so lowerCaseAddr and mixedCaseAddr are the same,
// despite it differs in string representation
lowerCaseAddr := common.HexToAddress("0xab00000000000000000000000000000000000000")
mixedCaseAddr := common.HexToAddress("0xAb00000000000000000000000000000000000000")

testCases := []struct {
name string
addrs []common.Address
unique bool
}{
{
name: "success: addresses are unique",
addrs: []common.Address{addr1, addr2},
unique: true,
},
{
name: "failure: addresses are not unique",
addrs: []common.Address{addr1, addr1},
unique: false,
},
{
name: "failure: addresses are not unique in bytes representation",
addrs: []common.Address{lowerCaseAddr, mixedCaseAddr},
unique: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := checkIfUniqueInBytesRepr(tc.addrs)

if tc.unique {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
}
})
}
}

0 comments on commit e749630

Please sign in to comment.