1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (c) 2020 Intel Corporation
11 func TestIsTarGz(t *testing.T) {
13 t.Run("Valid tar.gz", func(t *testing.T) {
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,
36 err := IsTarGz(bytes.NewBuffer(content))
38 t.Errorf("Error reading valid tar.gz file %s", err.Error())
42 t.Run("Invalid tar.gz", func(t *testing.T) {
44 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0xff, 0xf2, 0x48, 0xcd,
48 err := IsTarGz(bytes.NewBuffer(content))
50 t.Errorf("Error should NOT be nil")
54 t.Run("Empty tar.gz", func(t *testing.T) {
56 err := IsTarGz(bytes.NewBuffer(content))
58 t.Errorf("Error should NOT be nil")
63 func TestIsValidName(t *testing.T) {
64 t.Run("Valid Names", func(t *testing.T) {
65 validnames := []string{
69 "123456789012345678901234567890123456789012345678901234567890123", // max of 63 characters
71 for _, name := range validnames {
72 errs := IsValidName(name)
74 t.Errorf("Valid name failed to pass: %v", name)
79 t.Run("Invalid Names", func(t *testing.T) {
80 invalidnames := []string{
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
92 for _, name := range invalidnames {
93 errs := IsValidName(name)
95 t.Errorf("Invalid name passed: %v", name)
101 func TestIsIpv4Cidr(t *testing.T) {
102 t.Run("Valid IPv4 Cidr", func(t *testing.T) {
103 validipv4cidr := []string{
109 for _, ip := range validipv4cidr {
110 err := IsIpv4Cidr(ip)
112 t.Errorf("Valid IPv4 CIDR string failed to pass: %v", ip)
117 t.Run("Invalid IPv4 Cidr", func(t *testing.T) {
118 invalidipv4cidr := []string{
127 for _, ip := range invalidipv4cidr {
128 err := IsIpv4Cidr(ip)
130 t.Errorf("Invalid IPv4 Cidr passed: %v", ip)
136 func TestIsIpv4(t *testing.T) {
137 t.Run("Valid IPv4", func(t *testing.T) {
138 validipv4 := []string{
146 for _, ip := range validipv4 {
149 t.Errorf("Valid IPv4 string failed to pass: %v", ip)
154 t.Run("Invalid IPv4", func(t *testing.T) {
155 invalidipv4 := []string{
166 for _, ip := range invalidipv4 {
169 t.Errorf("Invalid IPv4 passed: %v", ip)
175 func TestIsMac(t *testing.T) {
176 t.Run("Valid MAC", func(t *testing.T) {
177 validmacs := []string{
182 for _, mac := range validmacs {
185 t.Errorf("Valid MAC string failed to pass: %v", mac)
190 t.Run("Invalid MAC", func(t *testing.T) {
191 invalidmacs := []string{
199 "11:22:33:44:55:66:77",
201 "11-22-33-44-55-66-77",
203 for _, mac := range invalidmacs {
206 t.Errorf("Invalid MAC passed: %v", mac)
212 func TestIsValidString(t *testing.T) {
213 t.Run("Valid Strings", func(t *testing.T) {
214 validStrings := []struct {
224 format: VALID_NAME_STR,
230 format: VALID_NAME_STR,
233 str: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
236 format: VALID_ALPHANUM_STR,
239 str: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
242 format: VALID_ALPHA_STR,
248 format: VALID_ALPHA_STR,
254 format: VALID_ALPHANUM_STR,
257 str: "dGhpcyBpcyBhCnRlc3Qgc3RyaW5nCg==",
260 format: VALID_BASE64_STR,
263 str: "\t\n \n0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=,.<>/?'\"\\[]{}\n",
266 format: VALID_ANY_STR,
269 for _, test := range validStrings {
270 errs := IsValidString(test.str, test.min, test.max, test.format)
272 t.Errorf("Valid string failed to pass: str:%v, min:%v, max:%v, format:%v", test.str, test.min, test.max, test.format)
277 t.Run("Invalid Strings", func(t *testing.T) {
278 inValidStrings := []struct {
288 format: VALID_NAME_STR,
294 format: VALID_NAME_STR,
300 format: VALID_NAME_STR,
303 str: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=",
306 format: VALID_ALPHANUM_STR,
309 str: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
312 format: VALID_ALPHA_STR,
318 format: VALID_ALPHA_STR,
324 format: VALID_ALPHA_STR,
330 format: VALID_ALPHANUM_STR,
333 str: "dGhpcyBpcyBhCnRlc3Qgc3RyaW5nCg===",
336 format: VALID_BASE64_STR,
339 str: "dGhpcyBpcyBhCnRlc3=Qgc3RyaW5nCg==",
342 format: VALID_BASE64_STR,
345 str: "dGhpcyBpcyBhCnRlc3#Qgc3RyaW5nCg==",
348 format: VALID_BASE64_STR,
351 str: "\t\n \n0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+-=,.<>/?'\"\\[]{}\n",
354 format: VALID_ANY_STR,
360 format: "unknownformat",
363 for _, test := range inValidStrings {
364 errs := IsValidString(test.str, test.min, test.max, test.format)
366 t.Errorf("Invalid string passed: str:%v, min:%v, max:%v, format:%v", test.str, test.min, test.max, test.format)
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",
379 for _, label := range validlabels {
380 errs := IsValidLabel(label)
382 t.Errorf("Valid label failed to pass: %v %v", label, errs)
387 t.Run("Invalid Labels", func(t *testing.T) {
388 invalidlabels := []string{
390 "kubernetes$.io/hostname=localhost",
391 "hostname==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
401 for _, label := range invalidlabels {
402 errs := IsValidLabel(label)
404 t.Errorf("Invalid label passed: %v", label)
410 func TestIsValidNumber(t *testing.T) {
411 t.Run("Valid Number", func(t *testing.T) {
412 validNumbers := []struct {
443 for _, test := range validNumbers {
444 err := IsValidNumber(test.value, test.min, test.max)
446 t.Errorf("Valid number failed to pass - value:%v, min:%v, max:%v", test.value, test.min, test.max)
451 t.Run("Invalid Number", func(t *testing.T) {
452 inValidNumbers := []struct {
483 for _, test := range inValidNumbers {
484 err := IsValidNumber(test.value, test.min, test.max)
486 t.Errorf("Invalid number passed - value:%v, min:%v, max:%v", test.value, test.min, test.max)