github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/validator/validator_test.go (about) 1 package validator 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestRegexMatch(t *testing.T) { 9 assert.Equal(t, true, RegexMatch("abc", AlphaMatcher)) 10 assert.Equal(t, false, RegexMatch("111", AlphaMatcher)) 11 assert.Equal(t, true, RegexMatch("abc", LetterRegexMatcher)) 12 assert.Equal(t, true, RegexMatch("123", NumberRegexMatcher)) 13 assert.Equal(t, true, RegexMatch("abc.com", DnsMatcher)) 14 assert.Equal(t, false, RegexMatch("a.b.com", DnsMatcher)) 15 assert.Equal(t, true, RegexMatch("abc@xyz.com", EmailMatcher)) 16 assert.Equal(t, false, RegexMatch("a.b@@com", EmailMatcher)) 17 assert.Equal(t, true, RegexMatch("13263527980", ChineseMobileMatcher)) 18 assert.Equal(t, false, RegexMatch("434324324", ChineseMobileMatcher)) 19 assert.Equal(t, true, RegexMatch("210911192105130715", ChineseIdMatcher)) 20 assert.Equal(t, false, RegexMatch("123456", ChineseIdMatcher)) 21 assert.Equal(t, true, RegexMatch("010-32116675", ChinesePhoneMatcher)) 22 assert.Equal(t, true, RegexMatch("0731-82251545", ChinesePhoneMatcher)) 23 assert.Equal(t, false, RegexMatch("123-87562", ChinesePhoneMatcher)) 24 assert.Equal(t, true, RegexMatch("你好", ChineseMatcher)) 25 assert.Equal(t, false, RegexMatch("hello", ChineseMatcher)) 26 assert.Equal(t, true, RegexMatch("4111111111111111", CreditCardMatcher)) 27 assert.Equal(t, false, RegexMatch("123456", CreditCardMatcher)) 28 assert.Equal(t, true, RegexMatch("aGVsbG8=", Base64Matcher)) 29 assert.Equal(t, false, RegexMatch("123456", Base64Matcher)) 30 } 31 32 func TestIsAllUpper(t *testing.T) { 33 assert.Equal(t, true, IsAllUpper("ABC")) 34 assert.Equal(t, false, IsAllUpper("")) 35 assert.Equal(t, false, IsAllUpper("abc")) 36 assert.Equal(t, false, IsAllUpper("aBC")) 37 assert.Equal(t, false, IsAllUpper("1BC")) 38 assert.Equal(t, false, IsAllUpper("1bc")) 39 assert.Equal(t, false, IsAllUpper("123")) 40 assert.Equal(t, false, IsAllUpper("你好")) 41 assert.Equal(t, false, IsAllUpper("A&")) 42 assert.Equal(t, false, IsAllUpper("&@#$%^&*")) 43 } 44 45 func TestIsAllLower(t *testing.T) { 46 assert.Equal(t, true, IsAllLower("abc")) 47 assert.Equal(t, false, IsAllLower("ABC")) 48 assert.Equal(t, false, IsAllLower("")) 49 assert.Equal(t, false, IsAllLower("aBC")) 50 assert.Equal(t, false, IsAllLower("1BC")) 51 assert.Equal(t, false, IsAllLower("1bc")) 52 assert.Equal(t, false, IsAllLower("123")) 53 assert.Equal(t, false, IsAllLower("你好")) 54 assert.Equal(t, false, IsAllLower("A&")) 55 assert.Equal(t, false, IsAllLower("&@#$%^&*")) 56 } 57 58 func TestContainLower(t *testing.T) { 59 assert.Equal(t, true, ContainLower("abc")) 60 assert.Equal(t, true, ContainLower("aBC")) 61 assert.Equal(t, true, ContainLower("1bc")) 62 assert.Equal(t, true, ContainLower("a&")) 63 64 assert.Equal(t, false, ContainLower("ABC")) 65 assert.Equal(t, false, ContainLower("")) 66 assert.Equal(t, false, ContainLower("1BC")) 67 assert.Equal(t, false, ContainLower("123")) 68 assert.Equal(t, false, ContainLower("你好")) 69 assert.Equal(t, false, ContainLower("&@#$%^&*")) 70 } 71 72 func TestContainUpper(t *testing.T) { 73 assert.Equal(t, true, ContainUpper("ABC")) 74 assert.Equal(t, true, ContainUpper("aBC")) 75 assert.Equal(t, true, ContainUpper("1BC")) 76 assert.Equal(t, true, ContainUpper("A&")) 77 78 assert.Equal(t, false, ContainUpper("abc")) 79 assert.Equal(t, false, ContainUpper("")) 80 assert.Equal(t, false, ContainUpper("1bc")) 81 assert.Equal(t, false, ContainUpper("123")) 82 assert.Equal(t, false, ContainUpper("你好")) 83 assert.Equal(t, false, ContainUpper("&@#$%^&*")) 84 } 85 86 func TestIsJSON(t *testing.T) { 87 assert.Equal(t, true, IsJSON("{}")) 88 assert.Equal(t, true, IsJSON("{\"name\": \"test\"}")) 89 assert.Equal(t, true, IsJSON("[]")) 90 assert.Equal(t, true, IsJSON("123")) 91 92 assert.Equal(t, false, IsJSON("")) 93 assert.Equal(t, false, IsJSON("abc")) 94 assert.Equal(t, false, IsJSON("你好")) 95 assert.Equal(t, false, IsJSON("&@#$%^&*")) 96 } 97 98 func TestIsNumber(t *testing.T) { 99 assert.Equal(t, false, IsNumber("")) 100 assert.Equal(t, false, IsNumber("3")) 101 assert.Equal(t, true, IsNumber(0)) 102 assert.Equal(t, true, IsNumber(0.1)) 103 } 104 105 func TestIsFloat(t *testing.T) { 106 assert.Equal(t, false, IsFloat("")) 107 assert.Equal(t, false, IsFloat("3")) 108 assert.Equal(t, false, IsFloat(0)) 109 assert.Equal(t, true, IsFloat(0.1)) 110 } 111 112 func TestIsInt(t *testing.T) { 113 assert.Equal(t, false, IsInt("")) 114 assert.Equal(t, false, IsInt("3")) 115 assert.Equal(t, false, IsInt(0.1)) 116 assert.Equal(t, true, IsInt(0)) 117 assert.Equal(t, true, IsInt(-1)) 118 } 119 120 func TestIsNumberStr(t *testing.T) { 121 assert.Equal(t, true, IsNumberStr("3.")) 122 assert.Equal(t, true, IsNumberStr("+3.")) 123 assert.Equal(t, true, IsNumberStr("-3.")) 124 assert.Equal(t, true, IsNumberStr("+3e2")) 125 assert.Equal(t, false, IsNumberStr("abc")) 126 } 127 128 func TestIsFloatStr(t *testing.T) { 129 assert.Equal(t, true, IsFloatStr("3.")) 130 assert.Equal(t, true, IsFloatStr("+3.")) 131 assert.Equal(t, true, IsFloatStr("-3.")) 132 assert.Equal(t, true, IsFloatStr("12")) 133 assert.Equal(t, false, IsFloatStr("abc")) 134 } 135 136 func TestIsIntStr(t *testing.T) { 137 assert.Equal(t, true, IsIntStr("+3")) 138 assert.Equal(t, true, IsIntStr("-3")) 139 assert.Equal(t, false, IsIntStr("3.")) 140 assert.Equal(t, false, IsIntStr("abc")) 141 } 142 143 func TestIsPort(t *testing.T) { 144 assert.Equal(t, true, IsPort("1")) 145 assert.Equal(t, true, IsPort("65535")) 146 assert.Equal(t, false, IsPort("abc")) 147 assert.Equal(t, false, IsPort("123abc")) 148 assert.Equal(t, false, IsPort("")) 149 assert.Equal(t, false, IsPort("-1")) 150 assert.Equal(t, false, IsPort("65536")) 151 } 152 153 func TestIsIp(t *testing.T) { 154 assert.Equal(t, true, IsIp("127.0.0.1")) 155 assert.Equal(t, true, IsIp("::0:0:0:0:0:0:1")) 156 assert.Equal(t, false, IsIp("127.0.0")) 157 assert.Equal(t, false, IsIp("127")) 158 } 159 160 func TestIsIpV4(t *testing.T) { 161 assert.Equal(t, true, IsIpV4("127.0.0.1")) 162 assert.Equal(t, false, IsIpV4("::0:0:0:0:0:0:1")) 163 } 164 165 func TestIsIpV6(t *testing.T) { 166 assert.Equal(t, false, IsIpV6("127.0.0.1")) 167 assert.Equal(t, true, IsIpV6("::0:0:0:0:0:0:1")) 168 } 169 170 func TestIsUrl(t *testing.T) { 171 assert.Equal(t, true, IsUrl("http://abc.com")) 172 assert.Equal(t, true, IsUrl("abc.com")) 173 assert.Equal(t, true, IsUrl("a.b.com")) 174 assert.Equal(t, false, IsUrl("abc")) 175 } 176 177 func TestIsEmptyString(t *testing.T) { 178 assert.Equal(t, true, IsEmptyString("")) 179 assert.Equal(t, false, IsEmptyString("111")) 180 assert.Equal(t, false, IsEmptyString(" ")) 181 assert.Equal(t, false, IsEmptyString("\t")) 182 } 183 184 func TestIsRegexMatch(t *testing.T) { 185 assert.Equal(t, true, IsRegexMatch("abc", `^[a-zA-Z]+$`)) 186 assert.Equal(t, false, IsRegexMatch("1ab", `^[a-zA-Z]+$`)) 187 assert.Equal(t, false, IsRegexMatch("", `^[a-zA-Z]+$`)) 188 } 189 190 func TestIsStrongPassword(t *testing.T) { 191 assert.Equal(t, false, IsStrongPassword("abc", 3)) 192 assert.Equal(t, false, IsStrongPassword("abc123", 6)) 193 assert.Equal(t, false, IsStrongPassword("abcABC", 6)) 194 assert.Equal(t, false, IsStrongPassword("abc123@#$", 9)) 195 assert.Equal(t, false, IsStrongPassword("abcABC123@#$", 16)) 196 assert.Equal(t, true, IsStrongPassword("abcABC123@#$", 12)) 197 assert.Equal(t, true, IsStrongPassword("abcABC123@#$", 10)) 198 } 199 200 func TestIsWeakPassword(t *testing.T) { 201 assert.Equal(t, true, IsWeakPassword("abc")) 202 assert.Equal(t, true, IsWeakPassword("123")) 203 assert.Equal(t, true, IsWeakPassword("abc123")) 204 assert.Equal(t, true, IsWeakPassword("abcABC123")) 205 assert.Equal(t, false, IsWeakPassword("abc123@#$")) 206 } 207 208 func TestIsASCII(t *testing.T) { 209 assert.Equal(t, true, IsASCII("ABC")) 210 assert.Equal(t, true, IsASCII("123")) 211 assert.Equal(t, true, IsASCII("")) 212 assert.Equal(t, false, IsASCII("😄")) 213 assert.Equal(t, false, IsASCII("你好")) 214 } 215 216 func TestIsPrintable(t *testing.T) { 217 assert.Equal(t, true, IsPrintable("ABC")) 218 assert.Equal(t, true, IsPrintable("{id: 123}")) 219 assert.Equal(t, true, IsPrintable("")) 220 assert.Equal(t, true, IsPrintable("😄")) 221 assert.Equal(t, false, IsPrintable("\u0000")) 222 }