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