1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.transport;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNull;
48
49 import org.junit.Test;
50
51
52
53
54 public class HttpConfigUriPathTest {
55
56 @Test
57 public void testNormalizationEmptyPaths() {
58 assertEquals("/", HttpConfig.normalize(""));
59 assertEquals("/", HttpConfig.normalize("/"));
60 }
61
62 @Test
63 public void testNormalization() {
64 assertEquals("/f", HttpConfig.normalize("f"));
65 assertEquals("/f", HttpConfig.normalize("/f"));
66 assertEquals("/f/", HttpConfig.normalize("/f/"));
67 assertEquals("/foo", HttpConfig.normalize("foo"));
68 assertEquals("/foo", HttpConfig.normalize("/foo"));
69 assertEquals("/foo/", HttpConfig.normalize("/foo/"));
70 assertEquals("/foo/bar", HttpConfig.normalize("foo/bar"));
71 assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar"));
72 assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/"));
73 }
74
75 @Test
76 public void testNormalizationWithDot() {
77 assertEquals("/", HttpConfig.normalize("."));
78 assertEquals("/", HttpConfig.normalize("/."));
79 assertEquals("/", HttpConfig.normalize("/./"));
80 assertEquals("/foo", HttpConfig.normalize("foo/."));
81 assertEquals("/foo/bar", HttpConfig.normalize("/foo/./bar"));
82 assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar/."));
83 assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/./"));
84 assertEquals("/foo/bar", HttpConfig.normalize("/foo/./././bar"));
85 assertEquals("/foo/bar/", HttpConfig.normalize("/foo/./././bar/"));
86 assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar/././."));
87 assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/./././"));
88 assertEquals("/foo/bar/.baz/bam",
89 HttpConfig.normalize("/foo/bar/.baz/bam"));
90 assertEquals("/foo/bar/.baz/bam/",
91 HttpConfig.normalize("/foo/bar/.baz/bam/"));
92 }
93
94 @Test
95 public void testNormalizationWithDotDot() {
96 assertEquals("/", HttpConfig.normalize("foo/.."));
97 assertEquals("/", HttpConfig.normalize("/foo/.."));
98 assertEquals("/", HttpConfig.normalize("/foo/../bar/.."));
99 assertEquals("/", HttpConfig.normalize("/foo/.././bar/.."));
100 assertEquals("/bar", HttpConfig.normalize("foo/../bar"));
101 assertEquals("/bar", HttpConfig.normalize("/foo/../bar"));
102 assertEquals("/bar", HttpConfig.normalize("/foo/./.././bar"));
103 assertEquals("/bar/", HttpConfig.normalize("/foo/../bar/"));
104 assertEquals("/bar/", HttpConfig.normalize("/foo/./.././bar/"));
105 assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar/baz/.."));
106 assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/baz/../"));
107 assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/../.."));
108 assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/../.."));
109 assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/.././.."));
110 assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/../././.."));
111 assertEquals("/foo/baz", HttpConfig.normalize("/foo/bar/../baz"));
112 assertEquals("/foo/baz/", HttpConfig.normalize("/foo/bar/../baz/"));
113 assertEquals("/foo/baz", HttpConfig.normalize("/foo/bar/../baz/."));
114 assertEquals("/foo/baz/", HttpConfig.normalize("/foo/bar/../baz/./"));
115 assertEquals("/foo", HttpConfig.normalize("/foo/bar/../baz/.."));
116 assertEquals("/foo/", HttpConfig.normalize("/foo/bar/../baz/../"));
117 assertEquals("/baz", HttpConfig.normalize("/foo/bar/../../baz"));
118 assertEquals("/baz/", HttpConfig.normalize("/foo/bar/../../baz/"));
119 assertEquals("/foo/.b/bar", HttpConfig.normalize("/foo/.b/bar"));
120 assertEquals("/.f/foo/.b/bar/", HttpConfig.normalize(".f/foo/.b/bar/"));
121 assertEquals("/foo/bar/..baz/bam",
122 HttpConfig.normalize("/foo/bar/..baz/bam"));
123 assertEquals("/foo/bar/..baz/bam/",
124 HttpConfig.normalize("/foo/bar/..baz/bam/"));
125 assertEquals("/foo/bar/.../baz/bam",
126 HttpConfig.normalize("/foo/bar/.../baz/bam"));
127 assertEquals("/foo/bar/.../baz/bam/",
128 HttpConfig.normalize("/foo/bar/.../baz/bam/"));
129 }
130
131 @Test
132 public void testNormalizationWithDoubleSlash() {
133 assertEquals("/", HttpConfig.normalize("//"));
134 assertEquals("/foo/", HttpConfig.normalize("///foo//"));
135 assertEquals("/foo", HttpConfig.normalize("///foo//."));
136 assertEquals("/foo/", HttpConfig.normalize("///foo//.////"));
137 assertEquals("/foo/bar", HttpConfig.normalize("/foo//bar"));
138 assertEquals("/foo/bar", HttpConfig.normalize("/foo//bar//."));
139 assertEquals("/foo/bar/", HttpConfig.normalize("/foo//bar//./"));
140 }
141
142 @Test
143 public void testNormalizationWithDotDotFailing() {
144 assertNull(HttpConfig.normalize(".."));
145 assertNull(HttpConfig.normalize("/.."));
146 assertNull(HttpConfig.normalize("/../"));
147 assertNull(HttpConfig.normalize("/../foo"));
148 assertNull(HttpConfig.normalize("./../foo"));
149 assertNull(HttpConfig.normalize("/./../foo"));
150 assertNull(HttpConfig.normalize("/foo/./.././.."));
151 assertNull(HttpConfig.normalize("/foo/../bar/../.."));
152 assertNull(HttpConfig.normalize("/foo/../bar/../../baz"));
153 }
154
155 @Test
156 public void testSegmentCompare() {
157
158 assertSuccess("/foo", "");
159 assertSuccess("/foo", "/");
160 assertSuccess("/foo", "//");
161 assertSuccess("/foo", "foo");
162 assertSuccess("/foo", "/foo");
163 assertSuccess("/foo/", "foo");
164 assertSuccess("/foo/", "/foo");
165 assertSuccess("/foo/", "foo/");
166 assertSuccess("/foo/", "/foo/");
167 assertSuccess("/foo/bar", "foo");
168 assertSuccess("/foo/bar", "foo/");
169 assertSuccess("/foo/bar", "foo/bar");
170 assertSuccess("/foo/bar/", "foo/bar");
171 assertSuccess("/foo/bar/", "foo/bar/");
172 assertSuccess("/foo/bar", "/foo/bar");
173 assertSuccess("/foo/bar/", "/foo/bar");
174 assertSuccess("/foo/bar/", "/foo/bar/");
175 assertSuccess("/foo/bar", "/foo/bar/..");
176 assertSuccess("/foo/bar/", "/foo/bar/..");
177 assertSuccess("/foo/bar/", "/foo/bar/../");
178 assertSuccess("/foo/bar", "/foo/./bar");
179 assertSuccess("/foo/bar/", "/foo/./bar/");
180 assertSuccess("/some/repo/.git", "/some/repo");
181 assertSuccess("/some/repo/bare.git", "/some/repo");
182 assertSuccess("/some/repo/.git", "/some/repo/.git");
183 assertSuccess("/some/repo/bare.git", "/some/repo/bare.git");
184 }
185
186 @Test
187 public void testSegmentCompareFailing() {
188
189 assertEquals(-1, HttpConfig.segmentCompare("/foo", "foo/"));
190 assertEquals(-1, HttpConfig.segmentCompare("/foo", "/foo/"));
191 assertEquals(-1, HttpConfig.segmentCompare("/foobar", "foo"));
192 assertEquals(-1, HttpConfig.segmentCompare("/foobar", "/foo"));
193 assertEquals(-1,
194 HttpConfig.segmentCompare("/foo/barbar/baz", "foo/bar"));
195 assertEquals(-1, HttpConfig.segmentCompare("/foo/barbar", "/foo/bar"));
196 assertEquals(-1,
197 HttpConfig.segmentCompare("/some/repo.git", "/some/repo"));
198 assertEquals(-1,
199 HttpConfig.segmentCompare("/some/repo.git", "/some/repo.g"));
200 assertEquals(-1, HttpConfig.segmentCompare("/some/repo/bare.git",
201 "/some/repo/bar"));
202 assertSuccess("/some/repo/bare.git", "/some/repo");
203
204 assertEquals(-1, HttpConfig.segmentCompare("/foo/barbar/baz", "**"));
205 assertEquals(-1,
206 HttpConfig.segmentCompare("/foo/barbar/baz", "**/foo"));
207 assertEquals(-1,
208 HttpConfig.segmentCompare("/foo/barbar/baz", "/*/barbar/**"));
209 assertEquals(-1, HttpConfig.segmentCompare("/foo", "/*"));
210 assertEquals(-1, HttpConfig.segmentCompare("/foo", "/???"));
211 assertEquals(-1, HttpConfig.segmentCompare("/foo/bar/baz", "bar"));
212
213 assertEquals(-1,
214 HttpConfig.segmentCompare("/foo/bar/baz", "bar/../.."));
215 }
216
217 private void assertSuccess(String uri, String match) {
218 String normalized = HttpConfig.normalize(match);
219 assertEquals(normalized.length(),
220 HttpConfig.segmentCompare(uri, match));
221 }
222 }