View Javadoc
1   /*
2    * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Basic URI path prefix match tests for {@link HttpConfig}.
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 		// 2nd parameter is the match, will be normalized
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 		// 2nd parameter is the match, will be normalized
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 		// Just to make sure we don't use the PathMatchers...
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 		// Failing to normalize
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 }