aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/HttpConfigUriPathTest.java
blob: 3dd16dd63ab24b5dc5fbf08e130ec1ac71396a46 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.transport;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;

/**
 * Basic URI path prefix match tests for {@link HttpConfig}.
 */
public class HttpConfigUriPathTest {

	@Test
	public void testNormalizationEmptyPaths() {
		assertEquals("/", HttpConfig.normalize(""));
		assertEquals("/", HttpConfig.normalize("/"));
	}

	@Test
	public void testNormalization() {
		assertEquals("/f", HttpConfig.normalize("f"));
		assertEquals("/f", HttpConfig.normalize("/f"));
		assertEquals("/f/", HttpConfig.normalize("/f/"));
		assertEquals("/foo", HttpConfig.normalize("foo"));
		assertEquals("/foo", HttpConfig.normalize("/foo"));
		assertEquals("/foo/", HttpConfig.normalize("/foo/"));
		assertEquals("/foo/bar", HttpConfig.normalize("foo/bar"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar"));
		assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/"));
	}

	@Test
	public void testNormalizationWithDot() {
		assertEquals("/", HttpConfig.normalize("."));
		assertEquals("/", HttpConfig.normalize("/."));
		assertEquals("/", HttpConfig.normalize("/./"));
		assertEquals("/foo", HttpConfig.normalize("foo/."));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo/./bar"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar/."));
		assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/./"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo/./././bar"));
		assertEquals("/foo/bar/", HttpConfig.normalize("/foo/./././bar/"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar/././."));
		assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/./././"));
		assertEquals("/foo/bar/.baz/bam",
				HttpConfig.normalize("/foo/bar/.baz/bam"));
		assertEquals("/foo/bar/.baz/bam/",
				HttpConfig.normalize("/foo/bar/.baz/bam/"));
	}

	@Test
	public void testNormalizationWithDotDot() {
		assertEquals("/", HttpConfig.normalize("foo/.."));
		assertEquals("/", HttpConfig.normalize("/foo/.."));
		assertEquals("/", HttpConfig.normalize("/foo/../bar/.."));
		assertEquals("/", HttpConfig.normalize("/foo/.././bar/.."));
		assertEquals("/bar", HttpConfig.normalize("foo/../bar"));
		assertEquals("/bar", HttpConfig.normalize("/foo/../bar"));
		assertEquals("/bar", HttpConfig.normalize("/foo/./.././bar"));
		assertEquals("/bar/", HttpConfig.normalize("/foo/../bar/"));
		assertEquals("/bar/", HttpConfig.normalize("/foo/./.././bar/"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo/bar/baz/.."));
		assertEquals("/foo/bar/", HttpConfig.normalize("/foo/bar/baz/../"));
		assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/../.."));
		assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/../.."));
		assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/.././.."));
		assertEquals("/foo", HttpConfig.normalize("/foo/bar/baz/../././.."));
		assertEquals("/foo/baz", HttpConfig.normalize("/foo/bar/../baz"));
		assertEquals("/foo/baz/", HttpConfig.normalize("/foo/bar/../baz/"));
		assertEquals("/foo/baz", HttpConfig.normalize("/foo/bar/../baz/."));
		assertEquals("/foo/baz/", HttpConfig.normalize("/foo/bar/../baz/./"));
		assertEquals("/foo", HttpConfig.normalize("/foo/bar/../baz/.."));
		assertEquals("/foo/", HttpConfig.normalize("/foo/bar/../baz/../"));
		assertEquals("/baz", HttpConfig.normalize("/foo/bar/../../baz"));
		assertEquals("/baz/", HttpConfig.normalize("/foo/bar/../../baz/"));
		assertEquals("/foo/.b/bar", HttpConfig.normalize("/foo/.b/bar"));
		assertEquals("/.f/foo/.b/bar/", HttpConfig.normalize(".f/foo/.b/bar/"));
		assertEquals("/foo/bar/..baz/bam",
				HttpConfig.normalize("/foo/bar/..baz/bam"));
		assertEquals("/foo/bar/..baz/bam/",
				HttpConfig.normalize("/foo/bar/..baz/bam/"));
		assertEquals("/foo/bar/.../baz/bam",
				HttpConfig.normalize("/foo/bar/.../baz/bam"));
		assertEquals("/foo/bar/.../baz/bam/",
				HttpConfig.normalize("/foo/bar/.../baz/bam/"));
	}

	@Test
	public void testNormalizationWithDoubleSlash() {
		assertEquals("/", HttpConfig.normalize("//"));
		assertEquals("/foo/", HttpConfig.normalize("///foo//"));
		assertEquals("/foo", HttpConfig.normalize("///foo//."));
		assertEquals("/foo/", HttpConfig.normalize("///foo//.////"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo//bar"));
		assertEquals("/foo/bar", HttpConfig.normalize("/foo//bar//."));
		assertEquals("/foo/bar/", HttpConfig.normalize("/foo//bar//./"));
	}

	@Test
	public void testNormalizationWithDotDotFailing() {
		assertNull(HttpConfig.normalize(".."));
		assertNull(HttpConfig.normalize("/.."));
		assertNull(HttpConfig.normalize("/../"));
		assertNull(HttpConfig.normalize("/../foo"));
		assertNull(HttpConfig.normalize("./../foo"));
		assertNull(HttpConfig.normalize("/./../foo"));
		assertNull(HttpConfig.normalize("/foo/./.././.."));
		assertNull(HttpConfig.normalize("/foo/../bar/../.."));
		assertNull(HttpConfig.normalize("/foo/../bar/../../baz"));
	}

	@Test
	public void testSegmentCompare() {
		// 2nd parameter is the match, will be normalized
		assertSuccess("/foo", "");
		assertSuccess("/foo", "/");
		assertSuccess("/foo", "//");
		assertSuccess("/foo", "foo");
		assertSuccess("/foo", "/foo");
		assertSuccess("/foo/", "foo");
		assertSuccess("/foo/", "/foo");
		assertSuccess("/foo/", "foo/");
		assertSuccess("/foo/", "/foo/");
		assertSuccess("/foo/bar", "foo");
		assertSuccess("/foo/bar", "foo/");
		assertSuccess("/foo/bar", "foo/bar");
		assertSuccess("/foo/bar/", "foo/bar");
		assertSuccess("/foo/bar/", "foo/bar/");
		assertSuccess("/foo/bar", "/foo/bar");
		assertSuccess("/foo/bar/", "/foo/bar");
		assertSuccess("/foo/bar/", "/foo/bar/");
		assertSuccess("/foo/bar", "/foo/bar/..");
		assertSuccess("/foo/bar/", "/foo/bar/..");
		assertSuccess("/foo/bar/", "/foo/bar/../");
		assertSuccess("/foo/bar", "/foo/./bar");
		assertSuccess("/foo/bar/", "/foo/./bar/");
		assertSuccess("/some/repo/.git", "/some/repo");
		assertSuccess("/some/repo/bare.git", "/some/repo");
		assertSuccess("/some/repo/.git", "/some/repo/.git");
		assertSuccess("/some/repo/bare.git", "/some/repo/bare.git");
	}

	@Test
	public void testSegmentCompareFailing() {
		// 2nd parameter is the match, will be normalized
		assertEquals(-1, HttpConfig.segmentCompare("/foo", "foo/"));
		assertEquals(-1, HttpConfig.segmentCompare("/foo", "/foo/"));
		assertEquals(-1, HttpConfig.segmentCompare("/foobar", "foo"));
		assertEquals(-1, HttpConfig.segmentCompare("/foobar", "/foo"));
		assertEquals(-1,
				HttpConfig.segmentCompare("/foo/barbar/baz", "foo/bar"));
		assertEquals(-1, HttpConfig.segmentCompare("/foo/barbar", "/foo/bar"));
		assertEquals(-1,
				HttpConfig.segmentCompare("/some/repo.git", "/some/repo"));
		assertEquals(-1,
				HttpConfig.segmentCompare("/some/repo.git", "/some/repo.g"));
		assertEquals(-1, HttpConfig.segmentCompare("/some/repo/bare.git",
				"/some/repo/bar"));
		assertSuccess("/some/repo/bare.git", "/some/repo");
		// Just to make sure we don't use the PathMatchers...
		assertEquals(-1, HttpConfig.segmentCompare("/foo/barbar/baz", "**"));
		assertEquals(-1,
				HttpConfig.segmentCompare("/foo/barbar/baz", "**/foo"));
		assertEquals(-1,
				HttpConfig.segmentCompare("/foo/barbar/baz", "/*/barbar/**"));
		assertEquals(-1, HttpConfig.segmentCompare("/foo", "/*"));
		assertEquals(-1, HttpConfig.segmentCompare("/foo", "/???"));
		assertEquals(-1, HttpConfig.segmentCompare("/foo/bar/baz", "bar"));
		// Failing to normalize
		assertEquals(-1,
				HttpConfig.segmentCompare("/foo/bar/baz", "bar/../.."));
	}

	private void assertSuccess(String uri, String match) {
		String normalized = HttpConfig.normalize(match);
		assertEquals(normalized.length(),
				HttpConfig.segmentCompare(uri, match));
	}
}