github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/types/chain_id_test.go (about) 1 package types 2 3 import ( 4 "math/big" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestParseChainID(t *testing.T) { 12 testCases := []struct { 13 name string 14 chainID string 15 expError bool 16 expInt *big.Int 17 }{ 18 { 19 "valid chain-id, single digit", "ethermint-1", false, big.NewInt(1), 20 }, 21 { 22 "valid chain-id, multiple digits", "aragonchain-256", false, big.NewInt(256), 23 }, 24 { 25 "invalid chain-id, double dash", "aragon-chain-1", true, nil, 26 }, 27 { 28 "invalid chain-id, dash only", "-", true, nil, 29 }, 30 { 31 "invalid chain-id, undefined", "-1", true, nil, 32 }, 33 { 34 "invalid chain-id, uppercases", "ETHERMINT-1", true, nil, 35 }, 36 { 37 "invalid chain-id, mixed cases", "FBchain-1", true, nil, 38 }, 39 { 40 "invalid chain-id, special chars", "$&*#!-1", true, nil, 41 }, 42 { 43 "invalid epoch, cannot start with 0", "ethermint-001", true, nil, 44 }, 45 { 46 "invalid epoch, cannot invalid base", "ethermint-0x212", true, nil, 47 }, 48 { 49 "invalid epoch, non-integer", "ethermint-ethermint", true, nil, 50 }, 51 { 52 "invalid epoch, undefined", "ethermint-", true, nil, 53 }, 54 { 55 "blank chain ID", " ", true, nil, 56 }, 57 { 58 "empty chain ID", "", true, nil, 59 }, 60 { 61 "long chain-id", "ethermint-" + strings.Repeat("1", 40), true, nil, 62 }, 63 } 64 65 for _, tc := range testCases { 66 chainIDEpoch, err := ParseChainID(tc.chainID) 67 if tc.expError { 68 require.Error(t, err, tc.name) 69 require.Nil(t, chainIDEpoch) 70 } else { 71 require.NoError(t, err, tc.name) 72 require.Equal(t, tc.expInt, chainIDEpoch, tc.name) 73 } 74 } 75 }