Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
ce208559
Commit
ce208559
authored
Nov 30, 2016
by
xilabao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve the result of checking role name
parent
e6c57c65
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
4 deletions
+42
-4
name.go
pkg/api/validation/path/name.go
+6
-4
name_test.go
pkg/api/validation/path/name_test.go
+36
-0
No files found.
pkg/api/validation/path/name.go
View file @
ce208559
...
...
@@ -35,25 +35,27 @@ func IsValidPathSegmentName(name string) []string {
}
}
var
errors
[]
string
for
_
,
illegalContent
:=
range
NameMayNotContain
{
if
strings
.
Contains
(
name
,
illegalContent
)
{
return
[]
string
{
fmt
.
Sprintf
(
`may not contain '%s'`
,
illegalContent
)}
errors
=
append
(
errors
,
fmt
.
Sprintf
(
`may not contain '%s'`
,
illegalContent
))
}
}
return
nil
return
errors
}
// IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment
// It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid
func
IsValidPathSegmentPrefix
(
name
string
)
[]
string
{
var
errors
[]
string
for
_
,
illegalContent
:=
range
NameMayNotContain
{
if
strings
.
Contains
(
name
,
illegalContent
)
{
return
[]
string
{
fmt
.
Sprintf
(
`may not contain '%s'`
,
illegalContent
)}
errors
=
append
(
errors
,
fmt
.
Sprintf
(
`may not contain '%s'`
,
illegalContent
))
}
}
return
nil
return
errors
}
// ValidatePathSegmentName validates the name can be safely encoded as a path segment
...
...
pkg/api/validation/path/name_test.go
View file @
ce208559
...
...
@@ -130,3 +130,39 @@ func TestValidatePathSegmentName(t *testing.T) {
}
}
}
func
TestValidateWithMultiErrors
(
t
*
testing
.
T
)
{
testcases
:=
map
[
string
]
struct
{
Name
string
Prefix
bool
ExpectedMsg
[]
string
}{
"slash,percent"
:
{
Name
:
"foo//bar%"
,
Prefix
:
false
,
ExpectedMsg
:
[]
string
{
"may not contain '/'"
,
"may not contain '%'"
},
},
"slash,percent,prefix"
:
{
Name
:
"foo//bar%"
,
Prefix
:
true
,
ExpectedMsg
:
[]
string
{
"may not contain '/'"
,
"may not contain '%'"
},
},
}
for
k
,
tc
:=
range
testcases
{
msgs
:=
ValidatePathSegmentName
(
tc
.
Name
,
tc
.
Prefix
)
if
len
(
tc
.
ExpectedMsg
)
==
0
&&
len
(
msgs
)
>
0
{
t
.
Errorf
(
"%s: expected no message, got %v"
,
k
,
msgs
)
}
if
len
(
tc
.
ExpectedMsg
)
>
0
&&
len
(
msgs
)
==
0
{
t
.
Errorf
(
"%s: expected error message, got none"
,
k
)
}
if
len
(
tc
.
ExpectedMsg
)
>
0
{
for
i
:=
0
;
i
<
len
(
tc
.
ExpectedMsg
);
i
++
{
if
msgs
[
i
]
!=
tc
.
ExpectedMsg
[
i
]
{
t
.
Errorf
(
"%s: expected message containing %q, got %v"
,
k
,
tc
.
ExpectedMsg
[
i
],
msgs
[
i
])
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment