1/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package path
18
19import (
20	"strings"
21	"testing"
22)
23
24func TestValidatePathSegmentName(t *testing.T) {
25	testcases := map[string]struct {
26		Name        string
27		Prefix      bool
28		ExpectedMsg string
29	}{
30		"empty": {
31			Name:        "",
32			Prefix:      false,
33			ExpectedMsg: "",
34		},
35		"empty,prefix": {
36			Name:        "",
37			Prefix:      true,
38			ExpectedMsg: "",
39		},
40
41		"valid": {
42			Name:        "foo.bar.baz",
43			Prefix:      false,
44			ExpectedMsg: "",
45		},
46		"valid,prefix": {
47			Name:        "foo.bar.baz",
48			Prefix:      true,
49			ExpectedMsg: "",
50		},
51
52		// Make sure mixed case, non DNS subdomain characters are tolerated
53		"valid complex": {
54			Name:        "sha256:ABCDEF012345@ABCDEF012345",
55			Prefix:      false,
56			ExpectedMsg: "",
57		},
58		// Make sure non-ascii characters are tolerated
59		"valid extended charset": {
60			Name:        "Iñtërnâtiônàlizætiøn",
61			Prefix:      false,
62			ExpectedMsg: "",
63		},
64
65		"dot": {
66			Name:        ".",
67			Prefix:      false,
68			ExpectedMsg: ".",
69		},
70		"dot leading": {
71			Name:        ".test",
72			Prefix:      false,
73			ExpectedMsg: "",
74		},
75		"dot,prefix": {
76			Name:        ".",
77			Prefix:      true,
78			ExpectedMsg: "",
79		},
80
81		"dot dot": {
82			Name:        "..",
83			Prefix:      false,
84			ExpectedMsg: "..",
85		},
86		"dot dot leading": {
87			Name:        "..test",
88			Prefix:      false,
89			ExpectedMsg: "",
90		},
91		"dot dot,prefix": {
92			Name:        "..",
93			Prefix:      true,
94			ExpectedMsg: "",
95		},
96
97		"slash": {
98			Name:        "foo/bar",
99			Prefix:      false,
100			ExpectedMsg: "/",
101		},
102		"slash,prefix": {
103			Name:        "foo/bar",
104			Prefix:      true,
105			ExpectedMsg: "/",
106		},
107
108		"percent": {
109			Name:        "foo%bar",
110			Prefix:      false,
111			ExpectedMsg: "%",
112		},
113		"percent,prefix": {
114			Name:        "foo%bar",
115			Prefix:      true,
116			ExpectedMsg: "%",
117		},
118	}
119
120	for k, tc := range testcases {
121		msgs := ValidatePathSegmentName(tc.Name, tc.Prefix)
122		if len(tc.ExpectedMsg) == 0 && len(msgs) > 0 {
123			t.Errorf("%s: expected no message, got %v", k, msgs)
124		}
125		if len(tc.ExpectedMsg) > 0 && len(msgs) == 0 {
126			t.Errorf("%s: expected error message, got none", k)
127		}
128		if len(tc.ExpectedMsg) > 0 && !strings.Contains(msgs[0], tc.ExpectedMsg) {
129			t.Errorf("%s: expected message containing %q, got %v", k, tc.ExpectedMsg, msgs[0])
130		}
131	}
132}
133
134func TestValidateWithMultiErrors(t *testing.T) {
135	testcases := map[string]struct {
136		Name        string
137		Prefix      bool
138		ExpectedMsg []string
139	}{
140		"slash,percent": {
141			Name:        "foo//bar%",
142			Prefix:      false,
143			ExpectedMsg: []string{"may not contain '/'", "may not contain '%'"},
144		},
145		"slash,percent,prefix": {
146			Name:        "foo//bar%",
147			Prefix:      true,
148			ExpectedMsg: []string{"may not contain '/'", "may not contain '%'"},
149		},
150	}
151
152	for k, tc := range testcases {
153		msgs := ValidatePathSegmentName(tc.Name, tc.Prefix)
154		if len(tc.ExpectedMsg) == 0 && len(msgs) > 0 {
155			t.Errorf("%s: expected no message, got %v", k, msgs)
156		}
157		if len(tc.ExpectedMsg) > 0 && len(msgs) == 0 {
158			t.Errorf("%s: expected error message, got none", k)
159		}
160		if len(tc.ExpectedMsg) > 0 {
161			for i := 0; i < len(tc.ExpectedMsg); i++ {
162				if msgs[i] != tc.ExpectedMsg[i] {
163					t.Errorf("%s: expected message containing %q, got %v", k, tc.ExpectedMsg[i], msgs[i])
164				}
165			}
166		}
167	}
168}
169