b62e69093fea115c40e5cc31d0cd95b8a5cc6dce
[icn/sdwan.git] /
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (c) 2020 Intel Corporation
3
4 package validation
5
6 import (
7         "bytes"
8         "testing"
9 )
10
11 func TestIsTarGz(t *testing.T) {
12
13         t.Run("Valid tar.gz", func(t *testing.T) {
14                 content := []byte{
15                         0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
16                         0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
17                         0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
18                         0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
19                         0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
20                         0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
21                         0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
22                         0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
23                         0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
24                         0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
25                         0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
26                         0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
27                         0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
28                         0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
29                         0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
30                         0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
31                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32                         0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
33                         0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
34                 }
35
36                 err := IsTarGz(bytes.NewBuffer(content))
37                 if err != nil {
38                         t.Errorf("Error reading valid tar.gz file %s", err.Error())
39                 }
40         })
41
42         t.Run("Invalid tar.gz", func(t *testing.T) {
43                 content := []byte{
44                         0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
45                         0x00, 0xff, 0xf2, 0x48, 0xcd,
46                 }
47
48                 err := IsTarGz(bytes.NewBuffer(content))
49                 if err == nil {
50                         t.Errorf("Error should NOT be nil")
51                 }
52         })
53
54         t.Run("Empty tar.gz", func(t *testing.T) {
55                 content := []byte{}
56                 err := IsTarGz(bytes.NewBuffer(content))
57                 if err == nil {
58                         t.Errorf("Error should NOT be nil")
59                 }
60         })
61 }
62
63 func TestIsValidName(t *testing.T) {
64         t.Run("Valid Names", func(t *testing.T) {
65                 validnames := []string{
66                         "abc123",
67                         "1_abc123.ONE",
68                         "0abcABC_-.5",
69                         "123456789012345678901234567890123456789012345678901234567890123", // max of 63 characters
70                 }
71                 for _, name := range validnames {
72                         errs := IsValidName(name)
73                         if len(errs) > 0 {
74                                 t.Errorf("Valid name failed to pass: %v", name)
75                         }
76                 }
77         })
78
79         t.Run("Invalid Names", func(t *testing.T) {
80                 invalidnames := []string{
81                         "",               // empty
82                         "_abc123",        // starts with non-alphanum
83                         "-abc123",        // starts with non-alphanum
84                         ".abc123",        // starts with non-alphanum
85                         "abc123-",        // ends with non-alphanum
86                         "abc123_",        // ends with non-alphanum
87                         "abc123.",        // ends with non-alphanum
88                         "1_abc-123.O=NE", // contains not allowed character
89                         "1_a/bc-123.ONE", // contains not allowed character
90                         "1234567890123456789012345678901234567890123456789012345678901234", // longer than 63 characters
91                 }
92                 for _, name := range invalidnames {
93                         errs := IsValidName(name)
94                         if len(errs) == 0 {
95                                 t.Errorf("Invalid name passed: %v", name)
96                         }
97                 }
98         })
99 }
100
101 func TestIsIpv4Cidr(t *testing.T) {
102         t.Run("Valid IPv4 Cidr", func(t *testing.T) {
103                 validipv4cidr := []string{
104                         "1.2.3.4/32",
105                         "10.11.12.0/24",
106                         "192.168.1.2/8",
107                         "255.0.0.0/16",
108                 }
109                 for _, ip := range validipv4cidr {
110                         err := IsIpv4Cidr(ip)
111                         if err != nil {
112                                 t.Errorf("Valid IPv4 CIDR string failed to pass: %v", ip)
113                         }
114                 }
115         })
116
117         t.Run("Invalid IPv4 Cidr", func(t *testing.T) {
118                 invalidipv4cidr := []string{
119                         "",
120                         "1.2.3.4.5/32",
121                         "1.2.3.415/16",
122                         "1.2.3.4/33",
123                         "2.3.4/24",
124                         "1.2.3.4",
125                         "1.2.3.4/",
126                 }
127                 for _, ip := range invalidipv4cidr {
128                         err := IsIpv4Cidr(ip)
129                         if err == nil {
130                                 t.Errorf("Invalid IPv4 Cidr passed: %v", ip)
131                         }
132                 }
133         })
134 }
135
136 func TestIsIpv4(t *testing.T) {
137         t.Run("Valid IPv4", func(t *testing.T) {
138                 validipv4 := []string{
139                         "1.2.3.42",
140                         "10.11.12.0",
141                         "192.168.1.2",
142                         "255.0.0.0",
143                         "255.255.255.255",
144                         "0.0.0.0",
145                 }
146                 for _, ip := range validipv4 {
147                         err := IsIpv4(ip)
148                         if err != nil {
149                                 t.Errorf("Valid IPv4 string failed to pass: %v", ip)
150                         }
151                 }
152         })
153
154         t.Run("Invalid IPv4", func(t *testing.T) {
155                 invalidipv4 := []string{
156                         "",
157                         "1.2.3.4.5",
158                         "1.2.3.45/32",
159                         "1.2.3.4a",
160                         "2.3.4",
161                         "1.2.3.400",
162                         "256.255.255.255",
163                         "10,11,12,13",
164                         "1.2.3.4/",
165                 }
166                 for _, ip := range invalidipv4 {
167                         err := IsIpv4(ip)
168                         if err == nil {
169                                 t.Errorf("Invalid IPv4 passed: %v", ip)
170                         }
171                 }
172         })
173 }
174
175 func TestIsMac(t *testing.T) {
176         t.Run("Valid MAC", func(t *testing.T) {
177                 validmacs := []string{
178                         "11:22:33:44:55:66",
179                         "ab-cd-ef-12-34-56",
180                         "AB-CD-EF-12-34-56",
181                 }
182                 for _, mac := range validmacs {
183                         err := IsMac(mac)
184                         if err != nil {
185                                 t.Errorf("Valid MAC string failed to pass: %v", mac)
186                         }
187                 }
188         })
189
190         t.Run("Invalid MAC", func(t *testing.T) {
191                 invalidmacs := []string{
192                         "",
193                         "1.2.3.4.5",
194                         "1.2.3.45/32",
195                         "ab:cd:ef:gh:12:34",
196                         "11:22-33-44:55:66",
197                         "11,22,33,44,55,66",
198                         "11|22|33|44|55|66",
199                         "11:22:33:44:55:66:77",
200                         "11-22-33-44-55",
201                         "11-22-33-44-55-66-77",
202                 }
203                 for _, mac := range invalidmacs {
204                         err := IsMac(mac)
205                         if err == nil {
206                                 t.Errorf("Invalid MAC passed: %v", mac)
207                         }
208                 }
209         })
210 }
211
212 func TestIsValidString(t *testing.T) {
213         t.Run("Valid Strings", func(t *testing.T) {
214                 validStrings := []struct {
215                         str    string
216                         min    int
217                         max    int
218                         format string
219                 }{
220                         {
221                                 str:    "abc123",
222                                 min:    0,
223                                 max:    16,
224                                 format: VALID_NAME_STR,
225                         },
226                         {
227                                 str:    "ab-c1_2.3",
228                                 min:    0,
229                                 max:    16,
230                                 format: VALID_NAME_STR,
231                         },
232                         {
233                                 str:    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
234                                 min:    0,
235                                 max:    62,
236                                 format: VALID_ALPHANUM_STR,
237                         },
238                         {
239                                 str:    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
240                                 min:    0,
241                                 max:    52,
242                                 format: VALID_ALPHA_STR,
243                         },
244                         {
245                                 str:    "",
246                                 min:    0,
247                                 max:    52,
248                                 format: VALID_ALPHA_STR,
249                         },
250                         {
251                                 str:    "",
252                                 min:    0,
253                                 max:    52,
254                                 format: VALID_ALPHANUM_STR,
255                         },
256                         {
257                                 str:    "dGhpcyBpcyBhCnRlc3Qgc3RyaW5nCg==",
258                                 min:    0,
259                                 max:    52,
260                                 format: VALID_BASE64_STR,
261                         },
262                         {
263                                 str:    "\t\n \n0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=,.<>/?'\"\\[]{}\n",
264                                 min:    0,
265                                 max:    256,
266                                 format: VALID_ANY_STR,
267                         },
268                 }
269                 for _, test := range validStrings {
270                         errs := IsValidString(test.str, test.min, test.max, test.format)
271                         if len(errs) > 0 {
272                                 t.Errorf("Valid string failed to pass: str:%v, min:%v, max:%v, format:%v", test.str, test.min, test.max, test.format)
273                         }
274                 }
275         })
276
277         t.Run("Invalid Strings", func(t *testing.T) {
278                 inValidStrings := []struct {
279                         str    string
280                         min    int
281                         max    int
282                         format string
283                 }{
284                         {
285                                 str:    "abc123",
286                                 min:    0,
287                                 max:    5,
288                                 format: VALID_NAME_STR,
289                         },
290                         {
291                                 str:    "",
292                                 min:    0,
293                                 max:    5,
294                                 format: VALID_NAME_STR,
295                         },
296                         {
297                                 str:    "-ab-c1_2.3",
298                                 min:    0,
299                                 max:    16,
300                                 format: VALID_NAME_STR,
301                         },
302                         {
303                                 str:    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=",
304                                 min:    0,
305                                 max:    100,
306                                 format: VALID_ALPHANUM_STR,
307                         },
308                         {
309                                 str:    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
310                                 min:    0,
311                                 max:    62,
312                                 format: VALID_ALPHA_STR,
313                         },
314                         {
315                                 str:    "",
316                                 min:    1,
317                                 max:    52,
318                                 format: VALID_ALPHA_STR,
319                         },
320                         {
321                                 str:    "abc123",
322                                 min:    1,
323                                 max:    3,
324                                 format: VALID_ALPHA_STR,
325                         },
326                         {
327                                 str:    "",
328                                 min:    1,
329                                 max:    52,
330                                 format: VALID_ALPHANUM_STR,
331                         },
332                         {
333                                 str:    "dGhpcyBpcyBhCnRlc3Qgc3RyaW5nCg===",
334                                 min:    0,
335                                 max:    52,
336                                 format: VALID_BASE64_STR,
337                         },
338                         {
339                                 str:    "dGhpcyBpcyBhCnRlc3=Qgc3RyaW5nCg==",
340                                 min:    0,
341                                 max:    52,
342                                 format: VALID_BASE64_STR,
343                         },
344                         {
345                                 str:    "dGhpcyBpcyBhCnRlc3#Qgc3RyaW5nCg==",
346                                 min:    0,
347                                 max:    52,
348                                 format: VALID_BASE64_STR,
349                         },
350                         {
351                                 str:    "\t\n \n0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=,.<>/?'\"\\[]{}\n",
352                                 min:    0,
353                                 max:    10,
354                                 format: VALID_ANY_STR,
355                         },
356                         {
357                                 str:    "abc123",
358                                 min:    0,
359                                 max:    10,
360                                 format: "unknownformat",
361                         },
362                 }
363                 for _, test := range inValidStrings {
364                         errs := IsValidString(test.str, test.min, test.max, test.format)
365                         if len(errs) == 0 {
366                                 t.Errorf("Invalid string passed: str:%v, min:%v, max:%v, format:%v", test.str, test.min, test.max, test.format)
367                         }
368                 }
369         })
370 }
371
372 func TestIsValidLabel(t *testing.T) {
373         t.Run("Valid Labels", func(t *testing.T) {
374                 validlabels := []string{
375                         "kubernetes.io/hostname=localhost",
376                         "hostname=localhost",
377                         "localhost",
378                 }
379                 for _, label := range validlabels {
380                         errs := IsValidLabel(label)
381                         if len(errs) > 0 {
382                                 t.Errorf("Valid label failed to pass: %v %v", label, errs)
383                         }
384                 }
385         })
386
387         t.Run("Invalid Labels", func(t *testing.T) {
388                 invalidlabels := []string{
389                         "",
390                         "kubernetes$.io/hostname=localhost",
391                         "hostname==localhost",
392                         "=localhost",
393                         "/hostname=localhost",
394                         ".a.b/hostname=localhost",
395                         "kubernetes.io/hostname",
396                         "kubernetes.io/hostname=",
397                         "kubernetes.io/1234567890123456789012345678901234567890123456789012345678901234=localhost",         // too long name
398                         "kubernetes.io/hostname=localhost1234567890123456789012345678901234567890123456789012345678901234", // too long value
399                         "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234/hostname=localhost", // too long prefix
400                 }
401                 for _, label := range invalidlabels {
402                         errs := IsValidLabel(label)
403                         if len(errs) == 0 {
404                                 t.Errorf("Invalid label passed: %v", label)
405                         }
406                 }
407         })
408 }
409
410 func TestIsValidNumber(t *testing.T) {
411         t.Run("Valid Number", func(t *testing.T) {
412                 validNumbers := []struct {
413                         value int
414                         min   int
415                         max   int
416                 }{
417                         {
418                                 value: 0,
419                                 min:   0,
420                                 max:   5,
421                         },
422                         {
423                                 value: 1000,
424                                 min:   0,
425                                 max:   4095,
426                         },
427                         {
428                                 value: 0,
429                                 min:   0,
430                                 max:   0,
431                         },
432                         {
433                                 value: -100,
434                                 min:   -200,
435                                 max:   -99,
436                         },
437                         {
438                                 value: 123,
439                                 min:   123,
440                                 max:   123,
441                         },
442                 }
443                 for _, test := range validNumbers {
444                         err := IsValidNumber(test.value, test.min, test.max)
445                         if len(err) > 0 {
446                                 t.Errorf("Valid number failed to pass - value:%v, min:%v, max:%v", test.value, test.min, test.max)
447                         }
448                 }
449         })
450
451         t.Run("Invalid Number", func(t *testing.T) {
452                 inValidNumbers := []struct {
453                         value int
454                         min   int
455                         max   int
456                 }{
457                         {
458                                 value: 6,
459                                 min:   0,
460                                 max:   5,
461                         },
462                         {
463                                 value: 4096,
464                                 min:   0,
465                                 max:   4095,
466                         },
467                         {
468                                 value: 11,
469                                 min:   10,
470                                 max:   10,
471                         },
472                         {
473                                 value: -100,
474                                 min:   -99,
475                                 max:   -200,
476                         },
477                         {
478                                 value: 123,
479                                 min:   223,
480                                 max:   123,
481                         },
482                 }
483                 for _, test := range inValidNumbers {
484                         err := IsValidNumber(test.value, test.min, test.max)
485                         if len(err) == 0 {
486                                 t.Errorf("Invalid number passed - value:%v, min:%v, max:%v", test.value, test.min, test.max)
487                         }
488                 }
489         })
490 }