github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/24-host/validate_test.go (about) 1 package host 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 // 195 characters 12 var longId = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eros neque, ultricies vel ligula ac, convallis porttitor elit. Maecenas tincidunt turpis elit, vel faucibus nisl pellentesque sodales" 13 14 type testCase struct { 15 msg string 16 id string 17 expPass bool 18 } 19 20 func TestDefaultIdentifierValidator(t *testing.T) { 21 testCases := []testCase{ 22 {"valid lowercase", "lowercaseid", true}, 23 {"valid id special chars", "._+-#[]<>._+-#[]<>", true}, 24 {"valid id lower and special chars", "lower._+-#[]<>", true}, 25 {"numeric id", "1234567890", true}, 26 {"uppercase id", "NOTLOWERCASE", true}, 27 {"numeric id", "1234567890", true}, 28 {"blank id", " ", false}, 29 {"id length out of range", "1", false}, 30 {"id is too long", "this identifier is too long to be used as a valid identifier", false}, 31 {"path-like id", "lower/case/id", false}, 32 {"invalid id", "(clientid)", false}, 33 {"empty string", "", false}, 34 } 35 36 for _, tc := range testCases { 37 38 err := ClientIdentifierValidator(tc.id) 39 err1 := ConnectionIdentifierValidator(tc.id) 40 err2 := ChannelIdentifierValidator(tc.id) 41 err3 := PortIdentifierValidator(tc.id) 42 if tc.expPass { 43 require.NoError(t, err, tc.msg) 44 require.NoError(t, err1, tc.msg) 45 require.NoError(t, err2, tc.msg) 46 require.NoError(t, err3, tc.msg) 47 } else { 48 require.Error(t, err, tc.msg) 49 require.Error(t, err1, tc.msg) 50 require.Error(t, err2, tc.msg) 51 require.Error(t, err3, tc.msg) 52 } 53 } 54 } 55 56 func TestPortIdentifierValidator(t *testing.T) { 57 testCases := []testCase{ 58 {"valid lowercase", "transfer", true}, 59 {"valid id special chars", "._+-#[]<>._+-#[]<>", true}, 60 {"valid id lower and special chars", "lower._+-#[]<>", true}, 61 {"numeric id", "1234567890", true}, 62 {"uppercase id", "NOTLOWERCASE", true}, 63 {"numeric id", "1234567890", true}, 64 {"blank id", " ", false}, 65 {"id length out of range", "1", false}, 66 {"id is too long", longId, false}, 67 {"path-like id", "lower/case/id", false}, 68 {"invalid id", "(clientid)", false}, 69 {"empty string", "", false}, 70 } 71 72 for _, tc := range testCases { 73 74 err := PortIdentifierValidator(tc.id) 75 if tc.expPass { 76 require.NoError(t, err, tc.msg) 77 } else { 78 require.Error(t, err, tc.msg) 79 } 80 } 81 } 82 83 func TestPathValidator(t *testing.T) { 84 testCases := []testCase{ 85 {"valid lowercase", "p/lowercaseid", true}, 86 {"numeric path", "p/239123", true}, 87 {"valid id special chars", "p/._+-#[]<>._+-#[]<>", true}, 88 {"valid id lower and special chars", "lower/._+-#[]<>", true}, 89 {"id length out of range", "p/l", true}, 90 {"uppercase id", "p/NOTLOWERCASE", true}, 91 {"invalid path", "lowercaseid", false}, 92 {"blank id", "p/ ", false}, 93 {"id length out of range", "p/12345678901234567890123456789012345678901234567890123456789012345", false}, 94 {"invalid id", "p/(clientid)", false}, 95 {"empty string", "", false}, 96 {"separators only", "////", false}, 97 {"just separator", "/", false}, 98 {"begins with separator", "/id", false}, 99 {"blank before separator", " /id", false}, 100 {"ends with separator", "id/", false}, 101 {"blank after separator", "id/ ", false}, 102 {"blanks with separator", " / ", false}, 103 } 104 105 for _, tc := range testCases { 106 f := NewPathValidator(func(path string) error { 107 return nil 108 }) 109 110 err := f(tc.id) 111 112 if tc.expPass { 113 seps := strings.Count(tc.id, "/") 114 require.Equal(t, 1, seps) 115 require.NoError(t, err, tc.msg) 116 } else { 117 require.Error(t, err, tc.msg) 118 } 119 } 120 } 121 122 func TestCustomPathValidator(t *testing.T) { 123 validateFn := NewPathValidator(func(path string) error { 124 if !strings.HasPrefix(path, "id_") { 125 return fmt.Errorf("identifier %s must start with 'id_", path) 126 } 127 return nil 128 }) 129 130 testCases := []testCase{ 131 {"valid custom path", "id_client/id_one", true}, 132 {"invalid path", "client", false}, 133 {"invalid custom path", "id_one/client", false}, 134 {"invalid identifier", "id_client/id_1234567890123456789012345678901234567890123457890123456789012345", false}, 135 {"separators only", "////", false}, 136 {"just separator", "/", false}, 137 {"ends with separator", "id_client/id_one/", false}, 138 {"beings with separator", "/id_client/id_one", false}, 139 } 140 141 for _, tc := range testCases { 142 err := validateFn(tc.id) 143 if tc.expPass { 144 require.NoError(t, err, tc.msg) 145 } else { 146 require.Error(t, err, tc.msg) 147 } 148 } 149 }