@@ -19,6 +19,7 @@ package filesystem
19
19
import (
20
20
"net"
21
21
"os"
22
+ "runtime"
22
23
"testing"
23
24
24
25
"github.com/stretchr/testify/assert"
@@ -84,3 +85,48 @@ func TestIsUnixDomainSocket(t *testing.T) {
84
85
assert .Equal (t , result , test .expectSocket , "Unexpected result from IsUnixDomainSocket: %v for %s" , result , test .label )
85
86
}
86
87
}
88
+
89
+ func TestIsCleanPath (t * testing.T ) {
90
+ type Case struct {
91
+ path string
92
+ expected bool
93
+ }
94
+
95
+ // Credits https://github.com/kubernetes/kubernetes/pull/124084/files#r1557566941
96
+ cases := []Case {
97
+ {path : "/logs" , expected : true },
98
+ {path : "/var/lib/something" , expected : true },
99
+ {path : "var/lib/something" , expected : true },
100
+ {path : "var\\ lib\\ something" , expected : true },
101
+ {path : "/" , expected : true },
102
+ {path : "." , expected : true },
103
+ {path : "/var/../something" , expected : false },
104
+ {path : "/var//lib/something" , expected : false },
105
+ {path : "/var/./lib/something" , expected : false },
106
+ }
107
+
108
+ // Additional cases applicable on Windows
109
+ if runtime .GOOS == "windows" {
110
+ cases = append (cases , []Case {
111
+ {path : "\\ " , expected : true },
112
+ {path : "C:/var/lib/something" , expected : true },
113
+ {path : "C:\\ var\\ lib\\ something" , expected : true },
114
+ {path : "C:/" , expected : true },
115
+ {path : "C:\\ " , expected : true },
116
+ {path : "C:/var//lib/something" , expected : false },
117
+ {path : "\\ var\\ \\ lib\\ something" , expected : false },
118
+ {path : "C:\\ var\\ \\ lib\\ something" , expected : false },
119
+ {path : "C:\\ var\\ ..\\ something" , expected : false },
120
+ {path : "\\ var\\ .\\ lib\\ something" , expected : false },
121
+ {path : "C:\\ var\\ .\\ lib\\ something" , expected : false },
122
+ }... )
123
+ }
124
+
125
+ for _ , tc := range cases {
126
+ actual := IsPathClean (tc .path )
127
+ if actual != tc .expected {
128
+ t .Errorf ("actual: %t, expected: %t, for path: %s\n " , actual , tc .expected , tc .path )
129
+ }
130
+ }
131
+
132
+ }
0 commit comments