|
| 1 | +package addresses |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ethereum/go-ethereum/common" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCheckNoZeroAddresses(t *testing.T) { |
| 11 | + t.Run("no zero addresses", func(t *testing.T) { |
| 12 | + roles := SuperchainRoles{ |
| 13 | + SuperchainProxyAdminOwner: common.HexToAddress("0x1111111111111111111111111111111111111111"), |
| 14 | + SuperchainGuardian: common.HexToAddress("0x2222222222222222222222222222222222222222"), |
| 15 | + ProtocolVersionsOwner: common.HexToAddress("0x3333333333333333333333333333333333333333"), |
| 16 | + } |
| 17 | + |
| 18 | + err := CheckNoZeroAddresses(roles) |
| 19 | + require.NoError(t, err) |
| 20 | + }) |
| 21 | + |
| 22 | + t.Run("detects zero address", func(t *testing.T) { |
| 23 | + roles := SuperchainRoles{ |
| 24 | + SuperchainProxyAdminOwner: common.HexToAddress("0x1111111111111111111111111111111111111111"), |
| 25 | + ProtocolVersionsOwner: common.HexToAddress("0x3333333333333333333333333333333333333333"), |
| 26 | + } |
| 27 | + |
| 28 | + require.Equal(t, roles.SuperchainGuardian, common.HexToAddress("0x0000000000000000000000000000000000000000")) |
| 29 | + err := CheckNoZeroAddresses(roles) |
| 30 | + require.Error(t, err) |
| 31 | + require.ErrorIs(t, err, ErrZeroAddress) |
| 32 | + require.Contains(t, err.Error(), "SuperchainGuardian") |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("error for non-address fields", func(t *testing.T) { |
| 36 | + roles := struct { |
| 37 | + SuperchainProxyAdminOwner common.Address |
| 38 | + ProtocolVersionsOwner common.Address |
| 39 | + chainId uint64 |
| 40 | + }{ |
| 41 | + SuperchainProxyAdminOwner: common.HexToAddress("0x1111111111111111111111111111111111111111"), |
| 42 | + ProtocolVersionsOwner: common.HexToAddress("0x3333333333333333333333333333333333333333"), |
| 43 | + chainId: 1, |
| 44 | + } |
| 45 | + |
| 46 | + err := CheckNoZeroAddresses(roles) |
| 47 | + require.Error(t, err) |
| 48 | + require.ErrorIs(t, err, ErrNotAddressType) |
| 49 | + require.Contains(t, err.Error(), "chainId") |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("struct pointer works", func(t *testing.T) { |
| 53 | + roles := SuperchainRoles{ |
| 54 | + SuperchainProxyAdminOwner: common.HexToAddress("0x1111111111111111111111111111111111111111"), |
| 55 | + SuperchainGuardian: common.HexToAddress("0x2222222222222222222222222222222222222222"), |
| 56 | + ProtocolVersionsOwner: common.HexToAddress("0x3333333333333333333333333333333333333333"), |
| 57 | + } |
| 58 | + |
| 59 | + err := CheckNoZeroAddresses(&roles) |
| 60 | + require.NoError(t, err) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("nil struct pointer fails", func(t *testing.T) { |
| 64 | + var roles *SuperchainRoles = nil |
| 65 | + |
| 66 | + err := CheckNoZeroAddresses(roles) |
| 67 | + require.Error(t, err) |
| 68 | + require.Contains(t, err.Error(), "nil pointer provided") |
| 69 | + }) |
| 70 | +} |
0 commit comments