1 // SPDX-License-Identifier: Apache-2.0
\r
2 // Copyright (c) 2020 Intel Corporation
\r
11 func TestIsTarGz(t *testing.T) {
\r
13 t.Run("Valid tar.gz", func(t *testing.T) {
\r
15 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
\r
16 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
\r
17 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
\r
18 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
\r
19 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
\r
20 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
\r
21 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
\r
22 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
\r
23 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
\r
24 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
\r
25 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
\r
26 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
\r
27 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
\r
28 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
\r
29 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
\r
30 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
\r
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
\r
32 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
\r
33 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
\r
36 err := IsTarGz(bytes.NewBuffer(content))
\r
38 t.Errorf("Error reading valid tar.gz file %s", err.Error())
\r
42 t.Run("Invalid tar.gz", func(t *testing.T) {
\r
44 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
\r
45 0x00, 0xff, 0xf2, 0x48, 0xcd,
\r
48 err := IsTarGz(bytes.NewBuffer(content))
\r
50 t.Errorf("Error should NOT be nil")
\r
54 t.Run("Empty tar.gz", func(t *testing.T) {
\r
56 err := IsTarGz(bytes.NewBuffer(content))
\r
58 t.Errorf("Error should NOT be nil")
\r
63 func TestIsValidName(t *testing.T) {
\r
64 t.Run("Valid Names", func(t *testing.T) {
\r
65 validnames := []string{
\r
69 "123456789012345678901234567890123456789012345678901234567890123", // max of 63 characters
\r
71 for _, name := range validnames {
\r
72 errs := IsValidName(name)
\r
74 t.Errorf("Valid name failed to pass: %v", name)
\r
79 t.Run("Invalid Names", func(t *testing.T) {
\r
80 invalidnames := []string{
\r
82 "_abc123", // starts with non-alphanum
\r
83 "-abc123", // starts with non-alphanum
\r
84 ".abc123", // starts with non-alphanum
\r
85 "abc123-", // ends with non-alphanum
\r
86 "abc123_", // ends with non-alphanum
\r
87 "abc123.", // ends with non-alphanum
\r
88 "1_abc-123.O=NE", // contains not allowed character
\r
89 "1_a/bc-123.ONE", // contains not allowed character
\r
90 "1234567890123456789012345678901234567890123456789012345678901234", // longer than 63 characters
\r
92 for _, name := range invalidnames {
\r
93 errs := IsValidName(name)
\r
95 t.Errorf("Invalid name passed: %v", name)
\r
101 func TestIsIpv4Cidr(t *testing.T) {
\r
102 t.Run("Valid IPv4 Cidr", func(t *testing.T) {
\r
103 validipv4cidr := []string{
\r
109 for _, ip := range validipv4cidr {
\r
110 err := IsIpv4Cidr(ip)
\r
112 t.Errorf("Valid IPv4 CIDR string failed to pass: %v", ip)
\r
117 t.Run("Invalid IPv4 Cidr", func(t *testing.T) {
\r
118 invalidipv4cidr := []string{
\r
127 for _, ip := range invalidipv4cidr {
\r
128 err := IsIpv4Cidr(ip)
\r
130 t.Errorf("Invalid IPv4 Cidr passed: %v", ip)
\r
136 func TestIsIpv4(t *testing.T) {
\r
137 t.Run("Valid IPv4", func(t *testing.T) {
\r
138 validipv4 := []string{
\r
146 for _, ip := range validipv4 {
\r
149 t.Errorf("Valid IPv4 string failed to pass: %v", ip)
\r
154 t.Run("Invalid IPv4", func(t *testing.T) {
\r
155 invalidipv4 := []string{
\r
166 for _, ip := range invalidipv4 {
\r
169 t.Errorf("Invalid IPv4 passed: %v", ip)
\r
175 func TestIsMac(t *testing.T) {
\r
176 t.Run("Valid MAC", func(t *testing.T) {
\r
177 validmacs := []string{
\r
178 "11:22:33:44:55:66",
\r
179 "ab-cd-ef-12-34-56",
\r
180 "AB-CD-EF-12-34-56",
\r
182 for _, mac := range validmacs {
\r
185 t.Errorf("Valid MAC string failed to pass: %v", mac)
\r
190 t.Run("Invalid MAC", func(t *testing.T) {
\r
191 invalidmacs := []string{
\r
195 "ab:cd:ef:gh:12:34",
\r
196 "11:22-33-44:55:66",
\r
197 "11,22,33,44,55,66",
\r
198 "11|22|33|44|55|66",
\r
199 "11:22:33:44:55:66:77",
\r
201 "11-22-33-44-55-66-77",
\r
203 for _, mac := range invalidmacs {
\r
206 t.Errorf("Invalid MAC passed: %v", mac)
\r
212 func TestIsValidString(t *testing.T) {
\r
213 t.Run("Valid Strings", func(t *testing.T) {
\r
214 validStrings := []struct {
\r
224 format: VALID_NAME_STR,
\r
230 format: VALID_NAME_STR,
\r
233 str: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
\r
236 format: VALID_ALPHANUM_STR,
\r
239 str: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
\r
242 format: VALID_ALPHA_STR,
\r
248 format: VALID_ALPHA_STR,
\r
254 format: VALID_ALPHANUM_STR,
\r
257 str: "dGhpcyBpcyBhCnRlc3Qgc3RyaW5nCg==",
\r
260 format: VALID_BASE64_STR,
\r
263 str: "\t\n \n0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=,.<>/?'\"\\[]{}\n",
\r
266 format: VALID_ANY_STR,
\r
269 for _, test := range validStrings {
\r
270 errs := IsValidString(test.str, test.min, test.max, test.format)
\r
272 t.Errorf("Valid string failed to pass: str:%v, min:%v, max:%v, format:%v", test.str, test.min, test.max, test.format)
\r
277 t.Run("Invalid Strings", func(t *testing.T) {
\r
278 inValidStrings := []struct {
\r
288 format: VALID_NAME_STR,
\r
294 format: VALID_NAME_STR,
\r
300 format: VALID_NAME_STR,
\r
303 str: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=",
\r
306 format: VALID_ALPHANUM_STR,
\r
309 str: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
\r
312 format: VALID_ALPHA_STR,
\r
318 format: VALID_ALPHA_STR,
\r
324 format: VALID_ALPHA_STR,
\r
330 format: VALID_ALPHANUM_STR,
\r
333 str: "dGhpcyBpcyBhCnRlc3Qgc3RyaW5nCg===",
\r
336 format: VALID_BASE64_STR,
\r
339 str: "dGhpcyBpcyBhCnRlc3=Qgc3RyaW5nCg==",
\r
342 format: VALID_BASE64_STR,
\r
345 str: "dGhpcyBpcyBhCnRlc3#Qgc3RyaW5nCg==",
\r
348 format: VALID_BASE64_STR,
\r
351 str: "\t\n \n0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=,.<>/?'\"\\[]{}\n",
\r
354 format: VALID_ANY_STR,
\r
360 format: "unknownformat",
\r
363 for _, test := range inValidStrings {
\r
364 errs := IsValidString(test.str, test.min, test.max, test.format)
\r
365 if len(errs) == 0 {
\r
366 t.Errorf("Invalid string passed: str:%v, min:%v, max:%v, format:%v", test.str, test.min, test.max, test.format)
\r
372 func TestIsValidLabel(t *testing.T) {
\r
373 t.Run("Valid Labels", func(t *testing.T) {
\r
374 validlabels := []string{
\r
375 "kubernetes.io/hostname=localhost",
\r
376 "hostname=localhost",
\r
379 for _, label := range validlabels {
\r
380 errs := IsValidLabel(label)
\r
382 t.Errorf("Valid label failed to pass: %v %v", label, errs)
\r
387 t.Run("Invalid Labels", func(t *testing.T) {
\r
388 invalidlabels := []string{
\r
390 "kubernetes$.io/hostname=localhost",
\r
391 "hostname==localhost",
\r
393 "/hostname=localhost",
\r
394 ".a.b/hostname=localhost",
\r
395 "kubernetes.io/hostname",
\r
396 "kubernetes.io/hostname=",
\r
397 "kubernetes.io/1234567890123456789012345678901234567890123456789012345678901234=localhost", // too long name
\r
398 "kubernetes.io/hostname=localhost1234567890123456789012345678901234567890123456789012345678901234", // too long value
\r
399 "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234/hostname=localhost", // too long prefix
\r
401 for _, label := range invalidlabels {
\r
402 errs := IsValidLabel(label)
\r
403 if len(errs) == 0 {
\r
404 t.Errorf("Invalid label passed: %v", label)
\r
410 func TestIsValidNumber(t *testing.T) {
\r
411 t.Run("Valid Number", func(t *testing.T) {
\r
412 validNumbers := []struct {
\r
443 for _, test := range validNumbers {
\r
444 err := IsValidNumber(test.value, test.min, test.max)
\r
446 t.Errorf("Valid number failed to pass - value:%v, min:%v, max:%v", test.value, test.min, test.max)
\r
451 t.Run("Invalid Number", func(t *testing.T) {
\r
452 inValidNumbers := []struct {
\r
483 for _, test := range inValidNumbers {
\r
484 err := IsValidNumber(test.value, test.min, test.max)
\r
486 t.Errorf("Invalid number passed - value:%v, min:%v, max:%v", test.value, test.min, test.max)
\r