Debian Codegraph @

pkg/internal/third_party/govalidator/validator_test.go

raw
1 package govalidator
2
3 import (
4 "testing"
5 )
6
7 func TestIsRequestURI(t *testing.T) {
8 t.Parallel()
9
10 var tests = []struct {
11 param string
12 expected bool
13 }{
14 {"", false},
15 {"http://foo.bar/#com", true},
16 {"http://foobar.com", true},
17 {"https://foobar.com", true},
18 {"foobar.com", false},
19 {"http://foobar.coffee/", true},
20 {"http://foobar.中文网/", true},
21 {"http://foobar.org/", true},
22 {"http://foobar.org:8080/", true},
23 {"ftp://foobar.ru/", true},
24 {"http://user:[email protected]/", true},
25 {"http://127.0.0.1/", true},
26 {"http://duckduckgo.com/?q=%2F", true},
27 {"http://localhost:3000/", true},
28 {"http://foobar.com/?foo=bar#baz=qux", true},
29 {"http://foobar.com?foo=bar", true},
30 {"http://www.xn--froschgrn-x9a.net/", true},
31 {"xyz://foobar.com", true},
32 {"invalid.", false},
33 {".com", false},
34 {"rtmp://foobar.com", true},
35 {"http://www.foo_bar.com/", true},
36 {"http://localhost:3000/", true},
37 {"http://foobar.com/#baz=qux", true},
38 {"http://foobar.com/t$-_.+!*\\'(),", true},
39 {"http://www.foobar.com/~foobar", true},
40 {"http://www.-foobar.com/", true},
41 {"http://www.foo---bar.com/", true},
42 {"mailto:[email protected]", true},
43 {"irc://irc.server.org/channel", true},
44 {"/abs/test/dir", true},
45 {"./rel/test/dir", false},
46 }
47 for _, test := range tests {
48 actual := IsRequestURI(test.param)
49 if actual != test.expected {
50 t.Errorf("Expected IsRequestURI(%q) to be %v, got %v", test.param, test.expected, actual)
51 }
52 }
53 }
54
55 func TestIsHexcolor(t *testing.T) {
56 t.Parallel()
57
58 var tests = []struct {
59 param string
60 expected bool
61 }{
62 {"", false},
63 {"#ff", false},
64 {"fff0", false},
65 {"#ff12FG", false},
66 {"CCccCC", true},
67 {"fff", true},
68 {"#f00", true},
69 }
70 for _, test := range tests {
71 actual := IsHexcolor(test.param)
72 if actual != test.expected {
73 t.Errorf("Expected IsHexcolor(%q) to be %v, got %v", test.param, test.expected, actual)
74 }
75 }
76 }
77
78 func TestIsRGBcolor(t *testing.T) {
79 t.Parallel()
80
81 var tests = []struct {
82 param string
83 expected bool
84 }{
85 {"", false},
86 {"rgb(0,31,255)", true},
87 {"rgb(1,349,275)", false},
88 {"rgb(01,31,255)", false},
89 {"rgb(0.6,31,255)", false},
90 {"rgba(0,31,255)", false},
91 {"rgb(0, 31, 255)", true},
92 }
93 for _, test := range tests {
94 actual := IsRGBcolor(test.param)
95 if actual != test.expected {
96 t.Errorf("Expected IsRGBcolor(%q) to be %v, got %v", test.param, test.expected, actual)
97 }
98 }
99 }
100
101 func TestIsCreditCard(t *testing.T) {
102 t.Parallel()
103 tests := []struct {
104 name string
105 number string
106 want bool
107 }{
108 {"empty", "", false},
109 {"not numbers", "credit card", false},
110 {"invalid luhn algorithm", "4220855426213389", false},
111
112 {"visa", "4220855426222389", true},
113 {"visa spaces", "4220 8554 2622 2389", true},
114 {"visa dashes", "4220-8554-2622-2389", true},
115 {"mastercard", "5139288802098206", true},
116 {"american express", "374953669708156", true},
117 {"discover", "6011464355444102", true},
118 {"jcb", "3548209662790989", true},
119
120 // below should be valid, do they respect international standards?
121 // is our validator logic not correct?
122 {"diners club international", "30190239451016", false},
123 {"rupay", "6521674451993089", false},
124 {"mir", "2204151414444676", false},
125 {"china unionPay", "624356436327468104", false},
126 }
127 for _, tt := range tests {
128 t.Run(tt.name, func(t *testing.T) {
129 if got := IsCreditCard(tt.number); got != tt.want {
130 t.Errorf("IsCreditCard(%v) = %v, want %v", tt.number, got, tt.want)
131 }
132 })
133 }
134 }
135
136 func TestIsISBN(t *testing.T) {
137 t.Parallel()
138
139 // Without version
140 var tests = []struct {
141 param string
142 expected bool
143 }{
144 {"", false},
145 {"foo", false},
146 {"3836221195", true},
147 {"1-61729-085-8", true},
148 {"3 423 21412 0", true},
149 {"3 401 01319 X", true},
150 {"9784873113685", true},
151 {"978-4-87311-368-5", true},
152 {"978 3401013190", true},
153 {"978-3-8362-2119-1", true},
154 }
155 for _, test := range tests {
156 actual := IsISBN(test.param, -1)
157 if actual != test.expected {
158 t.Errorf("Expected IsISBN(%q, -1) to be %v, got %v", test.param, test.expected, actual)
159 }
160 }
161
162 // ISBN 10
163 tests = []struct {
164 param string
165 expected bool
166 }{
167 {"", false},
168 {"foo", false},
169 {"3423214121", false},
170 {"978-3836221191", false},
171 {"3-423-21412-1", false},
172 {"3 423 21412 1", false},
173 {"3836221195", true},
174 {"1-61729-085-8", true},
175 {"3 423 21412 0", true},
176 {"3 401 01319 X", true},
177 }
178 for _, test := range tests {
179 actual := IsISBN10(test.param)
180 if actual != test.expected {
181 t.Errorf("Expected IsISBN10(%q) to be %v, got %v", test.param, test.expected, actual)
182 }
183 }
184
185 // ISBN 13
186 tests = []struct {
187 param string
188 expected bool
189 }{
190 {"", false},
191 {"foo", false},
192 {"3-8362-2119-5", false},
193 {"01234567890ab", false},
194 {"978 3 8362 2119 0", false},
195 {"9784873113685", true},
196 {"978-4-87311-368-5", true},
197 {"978 3401013190", true},
198 {"978-3-8362-2119-1", true},
199 }
200 for _, test := range tests {
201 actual := IsISBN13(test.param)
202 if actual != test.expected {
203 t.Errorf("Expected IsISBN13(%q) to be %v, got %v", test.param, test.expected, actual)
204 }
205 }
206 }
207
208 func TestIsBase64(t *testing.T) {
209 t.Parallel()
210
211 var tests = []struct {
212 param string
213 expected bool
214 }{
215 {"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4=", true},
216 {"Vml2YW11cyBmZXJtZW50dW0gc2VtcGVyIHBvcnRhLg==", true},
217 {"U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw==", true},
218 {"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMPNS1Ufof9EW/M98FNw" +
219 "UAKrwflsqVxaxQjBQnHQmiI7Vac40t8x7pIb8gLGV6wL7sBTJiPovJ0V7y7oc0Ye" +
220 "rhKh0Rm4skP2z/jHwwZICgGzBvA0rH8xlhUiTvcwDCJ0kc+fh35hNt8srZQM4619" +
221 "FTgB66Xmp4EtVyhpQV+t02g6NzK72oZI0vnAvqhpkxLeLiMCyrI416wHm5Tkukhx" +
222 "QmcL2a6hNOyu0ixX/x2kSFXApEnVrJ+/IxGyfyw8kf4N2IZpW5nEP847lpfj0SZZ" +
223 "Fwrd1mnfnDbYohX2zRptLy2ZUn06Qo9pkG5ntvFEPo9bfZeULtjYzIl6K8gJ2uGZ" + "HQIDAQAB", true},
224 {"12345", false},
225 {"", false},
226 {"Vml2YW11cyBmZXJtZtesting123", false},
227 }
228 for _, test := range tests {
229 actual := IsBase64(test.param)
230 if actual != test.expected {
231 t.Errorf("Expected IsBase64(%q) to be %v, got %v", test.param, test.expected, actual)
232 }
233 }
234 }
235
236 func TestIIsIPv6(t *testing.T) {
237 t.Parallel()
238
239 // IPv6
240 var tests = []struct {
241 param string
242 expected bool
243 }{
244 {"", false},
245 {"127.0.0.1", false},
246 {"0.0.0.0", false},
247 {"255.255.255.255", false},
248 {"1.2.3.4", false},
249 {"::1", true},
250 {"2001:db8:0000:1:1:1:1:1", true},
251 {"300.0.0.0", false},
252 }
253 for _, test := range tests {
254 actual := IsIPv6(test.param)
255 if actual != test.expected {
256 t.Errorf("Expected IsIPv6(%q) to be %v, got %v", test.param, test.expected, actual)
257 }
258 }
259 }
260
261 func TestIsMAC(t *testing.T) {
262 t.Parallel()
263
264 var tests = []struct {
265 param string
266 expected bool
267 }{
268 {"3D:F2:C9:A6:B3:4F", true},
269 {"3D-F2-C9-A6-B3:4F", false},
270 {"123", false},
271 {"", false},
272 {"abacaba", false},
273 }
274 for _, test := range tests {
275 actual := IsMAC(test.param)
276 if actual != test.expected {
277 t.Errorf("Expected IsMAC(%q) to be %v, got %v", test.param, test.expected, actual)
278 }
279 }
280 }
281
282 func TestIsSSN(t *testing.T) {
283 t.Parallel()
284
285 var tests = []struct {
286 param string
287 expected bool
288 }{
289 {"", false},
290 {"00-90-8787", false},
291 {"66690-76", false},
292 {"191 60 2869", true},
293 {"191-60-2869", true},
294 }
295 for _, test := range tests {
296 actual := IsSSN(test.param)
297 if actual != test.expected {
298 t.Errorf("Expected IsSSN(%q) to be %v, got %v", test.param, test.expected, actual)
299 }
300 }
301 }
302
303 func TestIsInt(t *testing.T) {
304 t.Parallel()
305
306 var tests = []struct {
307 param string
308 expected bool
309 }{
310 {"-2147483648", true}, //Signed 32 Bit Min Int
311 {"2147483647", true}, //Signed 32 Bit Max Int
312 {"-2147483649", true}, //Signed 32 Bit Min Int - 1
313 {"2147483648", true}, //Signed 32 Bit Max Int + 1
314 {"4294967295", true}, //Unsigned 32 Bit Max Int
315 {"4294967296", true}, //Unsigned 32 Bit Max Int + 1
316 {"-9223372036854775808", true}, //Signed 64 Bit Min Int
317 {"9223372036854775807", true}, //Signed 64 Bit Max Int
318 {"-9223372036854775809", true}, //Signed 64 Bit Min Int - 1
319 {"9223372036854775808", true}, //Signed 64 Bit Max Int + 1
320 {"18446744073709551615", true}, //Unsigned 64 Bit Max Int
321 {"18446744073709551616", true}, //Unsigned 64 Bit Max Int + 1
322 {"", true},
323 {"123", true},
324 {"0", true},
325 {"-0", true},
326 {"+0", true},
327 {"01", false},
328 {"123.123", false},
329 {" ", false},
330 {"000", false},
331 }
332 for _, test := range tests {
333 actual := IsInt(test.param)
334 if actual != test.expected {
335 t.Errorf("Expected IsInt(%q) to be %v, got %v", test.param, test.expected, actual)
336 }
337 }
338 }
339
340 func TestIsNull(t *testing.T) {
341 t.Parallel()
342
343 var tests = []struct {
344 param string
345 expected bool
346 }{
347 {"abacaba", false},
348 {"", true},
349 }
350 for _, test := range tests {
351 actual := IsNull(test.param)
352 if actual != test.expected {
353 t.Errorf("Expected IsNull(%q) to be %v, got %v", test.param, test.expected, actual)
354 }
355 }
356 }