View Javadoc
1   /*
2    * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5    * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
6    * Copyright (C) 2015, Patrick Steinhardt <ps@pks.im>
7    * and other copyright owners as documented in the project's IP log.
8    *
9    * This program and the accompanying materials are made available
10   * under the terms of the Eclipse Distribution License v1.0 which
11   * accompanies this distribution, is reproduced below, and is
12   * available at http://www.eclipse.org/org/documents/edl-v10.php
13   *
14   * All rights reserved.
15   *
16   * Redistribution and use in source and binary forms, with or
17   * without modification, are permitted provided that the following
18   * conditions are met:
19   *
20   * - Redistributions of source code must retain the above copyright
21   *   notice, this list of conditions and the following disclaimer.
22   *
23   * - Redistributions in binary form must reproduce the above
24   *   copyright notice, this list of conditions and the following
25   *   disclaimer in the documentation and/or other materials provided
26   *   with the distribution.
27   *
28   * - Neither the name of the Eclipse Foundation, Inc. nor the
29   *   names of its contributors may be used to endorse or promote
30   *   products derived from this software without specific prior
31   *   written permission.
32   *
33   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
34   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
35   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
38   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46   */
47  
48  package org.eclipse.jgit.transport;
49  
50  import static org.junit.Assert.assertEquals;
51  import static org.junit.Assert.assertFalse;
52  import static org.junit.Assert.assertNull;
53  import static org.junit.Assert.assertTrue;
54  
55  import java.io.File;
56  import java.io.IOException;
57  import java.net.URISyntaxException;
58  
59  import org.junit.Ignore;
60  import org.junit.Test;
61  
62  public class URIishTest {
63  
64  	private static final String GIT_SCHEME = "git://";
65  
66  	@SuppressWarnings("unused")
67  	@Test(expected = URISyntaxException.class)
68  	public void shouldRaiseErrorOnEmptyURI() throws Exception {
69  		new URIish("");
70  	}
71  
72  	@SuppressWarnings("unused")
73  	@Test(expected = URISyntaxException.class)
74  	public void shouldRaiseErrorOnNullURI() throws Exception {
75  		new URIish((String) null);
76  	}
77  
78  	@Test
79  	public void testUnixFile() throws Exception {
80  		final String str = "/home/m y";
81  		URIish u = new URIish(str);
82  		assertNull(u.getScheme());
83  		assertFalse(u.isRemote());
84  		assertEquals(str, u.getRawPath());
85  		assertEquals(str, u.getPath());
86  		assertEquals(str, u.toString());
87  		assertEquals(str, u.toASCIIString());
88  		assertEquals(u, new URIish(str));
89  	}
90  
91  	@Test
92  	public void testWindowsFile() throws Exception {
93  		final String str = "D:/m y";
94  		URIish u = new URIish(str);
95  		assertNull(u.getScheme());
96  		assertFalse(u.isRemote());
97  		assertEquals(str, u.getRawPath());
98  		assertEquals(str, u.getPath());
99  		assertEquals(str, u.toString());
100 		assertEquals(str, u.toASCIIString());
101 		assertEquals(u, new URIish(str));
102 	}
103 
104 	@Test
105 	public void testWindowsFile2() throws Exception {
106 		final String str = "D:\\m y";
107 		URIish u = new URIish(str);
108 		assertNull(u.getScheme());
109 		assertFalse(u.isRemote());
110 		assertEquals("D:\\m y", u.getRawPath());
111 		assertEquals("D:\\m y", u.getPath());
112 		assertEquals("D:\\m y", u.toString());
113 		assertEquals("D:\\m y", u.toASCIIString());
114 		assertEquals(u, new URIish(str));
115 	}
116 
117 	@Test
118 	public void testRelativePath() throws Exception {
119 		final String str = "../../foo/bar";
120 		URIish u = new URIish(str);
121 		assertNull(u.getScheme());
122 		assertFalse(u.isRemote());
123 		assertEquals(str, u.getRawPath());
124 		assertEquals(str, u.getPath());
125 		assertEquals(str, u.toString());
126 		assertEquals(str, u.toASCIIString());
127 		assertEquals(u, new URIish(str));
128 	}
129 
130 	@Test
131 	public void testUNC() throws Exception {
132 		final String str = "\\\\some\\place";
133 		URIish u = new URIish(str);
134 		assertNull(u.getScheme());
135 		assertFalse(u.isRemote());
136 		assertEquals("\\\\some\\place", u.getRawPath());
137 		assertEquals("\\\\some\\place", u.getPath());
138 		assertEquals("\\\\some\\place", u.toString());
139 		assertEquals("\\\\some\\place", u.toASCIIString());
140 		assertEquals(u, new URIish(str));
141 	}
142 
143 	@Test
144 	public void testFileProtoUnix() throws Exception {
145 		final String str = "file:///home/m y";
146 		URIish u = new URIish(str);
147 		assertEquals("file", u.getScheme());
148 		assertFalse(u.isRemote());
149 		assertEquals("/home/m y", u.getRawPath());
150 		assertEquals("/home/m y", u.getPath());
151 		assertEquals("file:///home/m y", u.toString());
152 		assertEquals("file:///home/m%20y", u.toASCIIString());
153 		assertEquals(u, new URIish(str));
154 	}
155 
156 	@Test
157 	public void testURIEncode_00() throws Exception {
158 		final String str = "file:///home/m%00y";
159 		URIish u = new URIish(str);
160 		assertEquals("file", u.getScheme());
161 		assertFalse(u.isRemote());
162 		assertEquals("/home/m%00y", u.getRawPath());
163 		assertEquals("/home/m\u0000y", u.getPath());
164 		assertEquals("file:///home/m%00y", u.toString());
165 		assertEquals("file:///home/m%00y", u.toASCIIString());
166 		assertEquals(u, new URIish(str));
167 	}
168 
169 	@Test
170 	public void testURIEncode_0a() throws Exception {
171 		final String str = "file:///home/m%0ay";
172 		URIish u = new URIish(str);
173 		assertEquals("file", u.getScheme());
174 		assertFalse(u.isRemote());
175 		assertEquals("/home/m%0ay", u.getRawPath());
176 		assertEquals("/home/m\ny", u.getPath());
177 		assertEquals("file:///home/m%0ay", u.toString());
178 		assertEquals("file:///home/m%0ay", u.toASCIIString());
179 		assertEquals(u, new URIish(str));
180 	}
181 
182 	@Test
183 	public void testURIEncode_unicode() throws Exception {
184 		final String str = "file:///home/m%c3%a5y";
185 		URIish u = new URIish(str);
186 		assertEquals("file", u.getScheme());
187 		assertFalse(u.isRemote());
188 		assertEquals("/home/m%c3%a5y", u.getRawPath());
189 		assertEquals("/home/m\u00e5y", u.getPath());
190 		assertEquals("file:///home/m%c3%a5y", u.toString());
191 		assertEquals("file:///home/m%c3%a5y", u.toASCIIString());
192 		assertEquals(u, new URIish(str));
193 	}
194 
195 	@Test
196 	public void testFileProtoWindows() throws Exception {
197 		final String str = "file:///D:/m y";
198 		URIish u = new URIish(str);
199 		assertEquals("file", u.getScheme());
200 		assertFalse(u.isRemote());
201 		assertEquals(null, u.getHost());
202 		assertEquals(-1, u.getPort());
203 		assertEquals(null, u.getUser());
204 		assertEquals(null, u.getPass());
205 		assertEquals("D:/m y", u.getRawPath());
206 		assertEquals("D:/m y", u.getPath());
207 		assertEquals("file:///D:/m y", u.toString());
208 		assertEquals("file:///D:/m%20y", u.toASCIIString());
209 		assertEquals(u, new URIish(str));
210 	}
211 
212 	@Test
213 	public void testFileProtoWindowsWithHost() throws Exception {
214 		final String str = "file://localhost/D:/m y";
215 		URIish u = new URIish(str);
216 		assertEquals("file", u.getScheme());
217 		assertTrue(u.isRemote());
218 		assertEquals("localhost", u.getHost());
219 		assertEquals(-1, u.getPort());
220 		assertEquals(null, u.getUser());
221 		assertEquals(null, u.getPass());
222 		assertEquals("D:/m y", u.getRawPath());
223 		assertEquals("D:/m y", u.getPath());
224 		assertEquals("file://localhost/D:/m y", u.toString());
225 		assertEquals("file://localhost/D:/m%20y", u.toASCIIString());
226 		assertEquals(u, new URIish(str));
227 	}
228 
229 	@Test
230 	public void testFileProtoWindowsWithHostAndPort() throws Exception {
231 		final String str = "file://localhost:80/D:/m y";
232 		URIish u = new URIish(str);
233 		assertEquals("file", u.getScheme());
234 		assertTrue(u.isRemote());
235 		assertEquals("localhost", u.getHost());
236 		assertEquals(80, u.getPort());
237 		assertEquals(null, u.getUser());
238 		assertEquals(null, u.getPass());
239 		assertEquals("D:/m y", u.getRawPath());
240 		assertEquals("D:/m y", u.getPath());
241 		assertEquals("file://localhost:80/D:/m y", u.toString());
242 		assertEquals("file://localhost:80/D:/m%20y", u.toASCIIString());
243 		assertEquals(u, new URIish(str));
244 	}
245 
246 	@Test
247 	public void testFileProtoWindowsWithHostAndEmptyPortIsAmbiguous()
248 			throws Exception {
249 		final String str = "file://localhost:/D:/m y";
250 		URIish u = new URIish(str);
251 		assertEquals("file", u.getScheme());
252 		assertFalse(u.isRemote());
253 		assertEquals(null, u.getHost());
254 		assertEquals(-1, u.getPort());
255 		assertEquals(null, u.getUser());
256 		assertEquals(null, u.getPass());
257 		assertEquals("localhost:/D:/m y", u.getRawPath());
258 		assertEquals("localhost:/D:/m y", u.getPath());
259 		assertEquals("file:///localhost:/D:/m y", u.toString());
260 		assertEquals("file:///localhost:/D:/m%20y", u.toASCIIString());
261 		assertEquals(u, new URIish(str));
262 	}
263 
264 	@Test
265 	public void testFileProtoWindowsMissingHostSlash() throws Exception {
266 		final String str = "file://D:/m y";
267 		URIish u = new URIish(str);
268 		assertEquals("file", u.getScheme());
269 		assertFalse(u.isRemote());
270 		assertEquals("D:/m y", u.getRawPath());
271 		assertEquals("D:/m y", u.getPath());
272 		assertEquals("file:///D:/m y", u.toString());
273 		assertEquals("file:///D:/m%20y", u.toASCIIString());
274 		assertEquals(u, new URIish(str));
275 	}
276 
277 	@Test
278 	public void testFileProtoWindowsMissingHostSlash2() throws Exception {
279 		final String str = "file://D: /m y";
280 		URIish u = new URIish(str);
281 		assertEquals("file", u.getScheme());
282 		assertFalse(u.isRemote());
283 		assertEquals("D: /m y", u.getRawPath());
284 		assertEquals("D: /m y", u.getPath());
285 		assertEquals("file:///D: /m y", u.toString());
286 		assertEquals("file:///D:%20/m%20y", u.toASCIIString());
287 		assertEquals(u, new URIish(str));
288 	}
289 
290 	@Test
291 	public void testGitProtoUnix() throws Exception {
292 		final String str = "git://example.com/home/m y";
293 		URIish u = new URIish(str);
294 		assertEquals("git", u.getScheme());
295 		assertTrue(u.isRemote());
296 		assertEquals("example.com", u.getHost());
297 		assertEquals("/home/m y", u.getRawPath());
298 		assertEquals("/home/m y", u.getPath());
299 		assertEquals("git://example.com/home/m y", u.toString());
300 		assertEquals("git://example.com/home/m%20y", u.toASCIIString());
301 		assertEquals(u, new URIish(str));
302 	}
303 
304 	@Test
305 	public void testGitProtoUnixPort() throws Exception {
306 		final String str = "git://example.com:333/home/m y";
307 		URIish u = new URIish(str);
308 		assertEquals("git", u.getScheme());
309 		assertTrue(u.isRemote());
310 		assertEquals("example.com", u.getHost());
311 		assertEquals("/home/m y", u.getRawPath());
312 		assertEquals("/home/m y", u.getPath());
313 		assertEquals(333, u.getPort());
314 		assertEquals("git://example.com:333/home/m y", u.toString());
315 		assertEquals("git://example.com:333/home/m%20y", u.toASCIIString());
316 		assertEquals(u, new URIish(str));
317 	}
318 
319 	@Test
320 	public void testGitProtoWindowsPort() throws Exception {
321 		final String str = "git://example.com:338/D:/m y";
322 		URIish u = new URIish(str);
323 		assertEquals("git", u.getScheme());
324 		assertTrue(u.isRemote());
325 		assertEquals("D:/m y", u.getRawPath());
326 		assertEquals("D:/m y", u.getPath());
327 		assertEquals(338, u.getPort());
328 		assertEquals("example.com", u.getHost());
329 		assertEquals("git://example.com:338/D:/m y", u.toString());
330 		assertEquals("git://example.com:338/D:/m%20y", u.toASCIIString());
331 		assertEquals(u, new URIish(str));
332 	}
333 
334 	@Test
335 	public void testGitProtoWindows() throws Exception {
336 		final String str = "git://example.com/D:/m y";
337 		URIish u = new URIish(str);
338 		assertEquals("git", u.getScheme());
339 		assertTrue(u.isRemote());
340 		assertEquals("D:/m y", u.getRawPath());
341 		assertEquals("D:/m y", u.getPath());
342 		assertEquals("example.com", u.getHost());
343 		assertEquals(-1, u.getPort());
344 		assertEquals("git://example.com/D:/m y", u.toString());
345 		assertEquals("git://example.com/D:/m%20y", u.toASCIIString());
346 		assertEquals(u, new URIish(str));
347 	}
348 
349 	@Test
350 	public void testScpStyleNoURIDecoding() throws Exception {
351 		final String str = "example.com:some/p%20ath";
352 		URIish u = new URIish(str);
353 		assertNull(u.getScheme());
354 		assertTrue(u.isRemote());
355 		assertEquals("some/p%20ath", u.getRawPath());
356 		assertEquals("some/p%20ath", u.getPath());
357 		assertEquals("example.com", u.getHost());
358 		assertEquals(-1, u.getPort());
359 		assertEquals(str, u.toString());
360 		assertEquals(str, u.toASCIIString());
361 		assertEquals(u, new URIish(str));
362 	}
363 
364 	@Test
365 	public void testScpStyleWithoutUserRelativePath() throws Exception {
366 		final String str = "example.com:some/p ath";
367 		URIish u = new URIish(str);
368 		assertNull(u.getScheme());
369 		assertTrue(u.isRemote());
370 		assertEquals("some/p ath", u.getRawPath());
371 		assertEquals("some/p ath", u.getPath());
372 		assertEquals("example.com", u.getHost());
373 		assertEquals(-1, u.getPort());
374 		assertEquals(str, u.toString());
375 		assertEquals(str, u.toASCIIString());
376 		assertEquals(u, new URIish(str));
377 	}
378 
379 	@Test
380 	public void testScpStyleWithoutUserAbsolutePath() throws Exception {
381 		final String str = "example.com:/some/p ath";
382 		URIish u = new URIish(str);
383 		assertNull(u.getScheme());
384 		assertTrue(u.isRemote());
385 		assertEquals("/some/p ath", u.getRawPath());
386 		assertEquals("/some/p ath", u.getPath());
387 		assertEquals("example.com", u.getHost());
388 		assertEquals(-1, u.getPort());
389 		assertEquals(str, u.toString());
390 		assertEquals(str, u.toASCIIString());
391 		assertEquals(u, new URIish(str));
392 	}
393 
394 	@Test
395 	public void testScpStyleWithUser() throws Exception {
396 		final String str = "user@example.com:some/p ath";
397 		URIish u = new URIish(str);
398 		assertNull(u.getScheme());
399 		assertTrue(u.isRemote());
400 		assertEquals("some/p ath", u.getRawPath());
401 		assertEquals("some/p ath", u.getPath());
402 		assertEquals("user", u.getUser());
403 		assertEquals("example.com", u.getHost());
404 		assertEquals(-1, u.getPort());
405 		assertEquals(str, u.toString());
406 		assertEquals(str, u.toASCIIString());
407 		assertEquals(u, new URIish(str));
408 	}
409 
410 	@Test
411 	public void testGitSshProto() throws Exception {
412 		final String str = "git+ssh://example.com/some/p ath";
413 		URIish u = new URIish(str);
414 		assertEquals("git+ssh", u.getScheme());
415 		assertTrue(u.isRemote());
416 		assertEquals("/some/p ath", u.getRawPath());
417 		assertEquals("/some/p ath", u.getPath());
418 		assertEquals("example.com", u.getHost());
419 		assertEquals(-1, u.getPort());
420 		assertEquals("git+ssh://example.com/some/p ath", u.toString());
421 		assertEquals("git+ssh://example.com/some/p%20ath", u.toASCIIString());
422 		assertEquals(u, new URIish(str));
423 	}
424 
425 	@Test
426 	public void testSshGitProto() throws Exception {
427 		final String str = "ssh+git://example.com/some/p ath";
428 		URIish u = new URIish(str);
429 		assertEquals("ssh+git", u.getScheme());
430 		assertTrue(u.isRemote());
431 		assertEquals("/some/p ath", u.getRawPath());
432 		assertEquals("/some/p ath", u.getPath());
433 		assertEquals("example.com", u.getHost());
434 		assertEquals(-1, u.getPort());
435 		assertEquals("ssh+git://example.com/some/p ath", u.toString());
436 		assertEquals("ssh+git://example.com/some/p%20ath", u.toASCIIString());
437 		assertEquals(u, new URIish(str));
438 	}
439 
440 	@Test
441 	public void testSshProto() throws Exception {
442 		final String str = "ssh://example.com/some/p ath";
443 		URIish u = new URIish(str);
444 		assertEquals("ssh", u.getScheme());
445 		assertTrue(u.isRemote());
446 		assertEquals("/some/p ath", u.getRawPath());
447 		assertEquals("/some/p ath", u.getPath());
448 		assertEquals("example.com", u.getHost());
449 		assertEquals(-1, u.getPort());
450 		assertEquals("ssh://example.com/some/p ath", u.toString());
451 		assertEquals("ssh://example.com/some/p%20ath", u.toASCIIString());
452 		assertEquals(u, new URIish(str));
453 	}
454 
455 	@Test
456 	public void testSshProtoHostOnly() throws Exception {
457 		final String str = "ssh://example.com/";
458 		URIish u = new URIish(str);
459 		assertEquals("ssh", u.getScheme());
460 		assertTrue(u.isRemote());
461 		assertEquals("/", u.getRawPath());
462 		assertEquals("/", u.getPath());
463 		assertEquals("example.com", u.getHost());
464 		assertEquals(-1, u.getPort());
465 		assertEquals("ssh://example.com/", u.toString());
466 		assertEquals("ssh://example.com/", u.toASCIIString());
467 		assertEquals("example.com", u.getHumanishName());
468 		assertEquals(u, new URIish(str));
469 	}
470 
471 	@Test
472 	public void testSshProtoHostWithAuthentication() throws Exception {
473 		final String str = "ssh://user:secret@pass@example.com/";
474 		URIish u = new URIish(str);
475 		assertEquals("ssh", u.getScheme());
476 		assertTrue(u.isRemote());
477 		assertEquals("/", u.getRawPath());
478 		assertEquals("/", u.getPath());
479 		assertEquals("example.com", u.getHost());
480 		assertEquals(-1, u.getPort());
481 		assertEquals("ssh://user@example.com/", u.toString());
482 		assertEquals("ssh://user@example.com/", u.toASCIIString());
483 		assertEquals("example.com", u.getHumanishName());
484 		assertEquals("user", u.getUser());
485 		assertEquals("secret@pass", u.getPass());
486 		assertEquals(u, new URIish(str));
487 	}
488 
489 	@Test
490 	public void testSshProtoHostWithPort() throws Exception {
491 		final String str = "ssh://example.com:2222/";
492 		URIish u = new URIish(str);
493 		assertEquals("ssh", u.getScheme());
494 		assertTrue(u.isRemote());
495 		assertEquals("/", u.getRawPath());
496 		assertEquals("/", u.getPath());
497 		assertEquals("example.com", u.getHost());
498 		assertEquals(2222, u.getPort());
499 		assertEquals("ssh://example.com:2222/", u.toString());
500 		assertEquals("ssh://example.com:2222/", u.toASCIIString());
501 		assertEquals("example.com", u.getHumanishName());
502 		assertEquals(u, new URIish(str));
503 	}
504 
505 	@Test
506 	public void testSshProtoHostWithEmptyPortAndPath() throws Exception {
507 		final String str = "ssh://example.com:/path";
508 		URIish u = new URIish(str);
509 		assertEquals("ssh", u.getScheme());
510 		assertTrue(u.isRemote());
511 		assertEquals("/path", u.getRawPath());
512 		assertEquals("/path", u.getPath());
513 		assertEquals("example.com", u.getHost());
514 		assertEquals(-1, u.getPort());
515 		assertEquals("ssh://example.com/path", u.toString());
516 		assertEquals("ssh://example.com/path", u.toASCIIString());
517 		assertEquals(u, new URIish(str));
518 		assertEquals(u, new URIish("ssh://example.com/path"));
519 	}
520 
521 	@Test
522 	public void testSshProtoWithUserAndPort() throws Exception {
523 		final String str = "ssh://user@example.com:33/some/p ath";
524 		URIish u = new URIish(str);
525 		assertEquals("ssh", u.getScheme());
526 		assertTrue(u.isRemote());
527 		assertEquals("/some/p ath", u.getRawPath());
528 		assertEquals("/some/p ath", u.getPath());
529 		assertEquals("example.com", u.getHost());
530 		assertEquals("user", u.getUser());
531 		assertNull(u.getPass());
532 		assertEquals(33, u.getPort());
533 		assertEquals("ssh://user@example.com:33/some/p ath", u.toString());
534 		assertEquals("ssh://user@example.com:33/some/p%20ath",
535 				u.toASCIIString());
536 		assertEquals(u, new URIish(str));
537 	}
538 
539 	@Test
540 	public void testSshProtoWithUserPassAndPort() throws Exception {
541 		final String str = "ssh://user:pass@example.com:33/some/p ath";
542 		URIish u = new URIish(str);
543 		assertEquals("ssh", u.getScheme());
544 		assertTrue(u.isRemote());
545 		assertEquals("/some/p ath", u.getRawPath());
546 		assertEquals("/some/p ath", u.getPath());
547 		assertEquals("example.com", u.getHost());
548 		assertEquals("user", u.getUser());
549 		assertEquals("pass", u.getPass());
550 		assertEquals(33, u.getPort());
551 		assertEquals("ssh://user:pass@example.com:33/some/p ath",
552 				u.toPrivateString());
553 		assertEquals("ssh://user:pass@example.com:33/some/p%20ath",
554 				u.toPrivateASCIIString());
555 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
556 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
557 		assertEquals(u, new URIish(str));
558 	}
559 
560 	@Test
561 	public void testSshProtoWithEmailUserAndPort() throws Exception {
562 		final String str = "ssh://user.name@email.com@example.com:33/some/p ath";
563 		URIish u = new URIish(str);
564 		assertEquals("ssh", u.getScheme());
565 		assertTrue(u.isRemote());
566 		assertEquals("/some/p ath", u.getRawPath());
567 		assertEquals("/some/p ath", u.getPath());
568 		assertEquals("example.com", u.getHost());
569 		assertEquals("user.name@email.com", u.getUser());
570 		assertNull(u.getPass());
571 		assertEquals(33, u.getPort());
572 		assertEquals("ssh://user.name%40email.com@example.com:33/some/p ath",
573 				u.toPrivateString());
574 		assertEquals("ssh://user.name%40email.com@example.com:33/some/p%20ath",
575 				u.toPrivateASCIIString());
576 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
577 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
578 		assertEquals(u, new URIish(str));
579 	}
580 
581 	@Test
582 	public void testSshProtoWithEmailUserPassAndPort() throws Exception {
583 		final String str = "ssh://user.name@email.com:pass@wor:d@example.com:33/some/p ath";
584 		URIish u = new URIish(str);
585 		assertEquals("ssh", u.getScheme());
586 		assertTrue(u.isRemote());
587 		assertEquals("/some/p ath", u.getRawPath());
588 		assertEquals("/some/p ath", u.getPath());
589 		assertEquals("example.com", u.getHost());
590 		assertEquals("user.name@email.com", u.getUser());
591 		assertEquals("pass@wor:d", u.getPass());
592 		assertEquals(33, u.getPort());
593 		assertEquals("ssh://user.name%40email.com:pass%40wor%3ad@example.com:33/some/p ath",
594 				u.toPrivateString());
595 		assertEquals("ssh://user.name%40email.com:pass%40wor%3ad@example.com:33/some/p%20ath",
596 				u.toPrivateASCIIString());
597 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
598 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
599 		assertEquals(u, new URIish(str));
600 	}
601 
602 	@Test
603 	public void testSshProtoWithADUserPassAndPort() throws Exception {
604 		final String str = "ssh://DOMAIN\\user:pass@example.com:33/some/p ath";
605 		URIish u = new URIish(str);
606 		assertEquals("ssh", u.getScheme());
607 		assertTrue(u.isRemote());
608 		assertEquals("/some/p ath", u.getRawPath());
609 		assertEquals("/some/p ath", u.getPath());
610 		assertEquals("example.com", u.getHost());
611 		assertEquals("DOMAIN\\user", u.getUser());
612 		assertEquals("pass", u.getPass());
613 		assertEquals(33, u.getPort());
614 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p ath",
615 				u.toPrivateString());
616 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p%20ath",
617 				u.toPrivateASCIIString());
618 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
619 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
620 		assertEquals(u, new URIish(str));
621 	}
622 
623 	@Test
624 	public void testSshProtoWithEscapedADUserPassAndPort() throws Exception {
625 		final String str = "ssh://DOMAIN%5c\u00fcser:pass@example.com:33/some/p ath";
626 		URIish u = new URIish(str);
627 		assertEquals("ssh", u.getScheme());
628 		assertTrue(u.isRemote());
629 		assertEquals("/some/p ath", u.getRawPath());
630 		assertEquals("/some/p ath", u.getPath());
631 		assertEquals("example.com", u.getHost());
632 		assertEquals("DOMAIN\\\u00fcser", u.getUser());
633 		assertEquals("pass", u.getPass());
634 		assertEquals(33, u.getPort());
635 		assertEquals("ssh://DOMAIN\\\u00fcser:pass@example.com:33/some/p ath",
636 				u.toPrivateString());
637 		assertEquals(
638 				"ssh://DOMAIN\\%c3%bcser:pass@example.com:33/some/p%20ath",
639 				u.toPrivateASCIIString());
640 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
641 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
642 		assertEquals(u, new URIish(str));
643 	}
644 
645 	@Test
646 	public void testURIEncodeDecode() throws Exception {
647 		final String str = "ssh://%3ax%25:%40%41x@example.com:33/some%c3%a5/p%20a th";
648 		URIish u = new URIish(str);
649 		assertEquals("ssh", u.getScheme());
650 		assertTrue(u.isRemote());
651 		assertEquals("/some%c3%a5/p%20a th", u.getRawPath());
652 		assertEquals("/some\u00e5/p a th", u.getPath());
653 		assertEquals("example.com", u.getHost());
654 		assertEquals(":x%", u.getUser());
655 		assertEquals("@Ax", u.getPass());
656 		assertEquals(33, u.getPort());
657 		assertEquals("ssh://%3ax%25:%40Ax@example.com:33/some%c3%a5/p%20a th",
658 				u.toPrivateString());
659 		assertEquals(
660 				"ssh://%3ax%25:%40Ax@example.com:33/some%c3%a5/p%20a%20th",
661 				u.toPrivateASCIIString());
662 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
663 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
664 		assertEquals(u, new URIish(str));
665 	}
666 
667 	@Test
668 	public void testGitWithUserHome() throws Exception {
669 		final String str = "git://example.com/~some/p ath";
670 		URIish u = new URIish(str);
671 		assertEquals("git", u.getScheme());
672 		assertTrue(u.isRemote());
673 		assertEquals("~some/p ath", u.getRawPath());
674 		assertEquals("~some/p ath", u.getPath());
675 		assertEquals("example.com", u.getHost());
676 		assertNull(u.getUser());
677 		assertNull(u.getPass());
678 		assertEquals(-1, u.getPort());
679 		assertEquals("git://example.com/~some/p ath", u.toPrivateString());
680 		assertEquals("git://example.com/~some/p%20ath",
681 				u.toPrivateASCIIString());
682 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
683 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
684 		assertEquals(u, new URIish(str));
685 	}
686 
687 	@Test
688 	@Ignore("Resolving ~user is beyond standard Java API and need more support")
689 	public void testFileWithUserHome() throws Exception {
690 		final String str = "~some/p ath";
691 		URIish u = new URIish(str);
692 		assertEquals("git", u.getScheme());
693 		assertTrue(u.isRemote());
694 		assertEquals("~some/p ath", u.getRawPath());
695 		assertEquals("~some/p ath", u.getPath());
696 		assertEquals("example.com", u.getHost());
697 		assertNull(u.getUser());
698 		assertNull(u.getPass());
699 		assertEquals(-1, u.getPort());
700 		assertEquals(str, u.toPrivateString());
701 		assertEquals(str, u.toPrivateASCIIString());
702 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
703 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
704 		assertEquals(u, new URIish(str));
705 	}
706 
707 	@Test
708 	public void testFileWithNoneUserHomeWithTilde() throws Exception {
709 		final String str = "/~some/p ath";
710 		URIish u = new URIish(str);
711 		assertNull(u.getScheme());
712 		assertFalse(u.isRemote());
713 		assertEquals("/~some/p ath", u.getRawPath());
714 		assertEquals("/~some/p ath", u.getPath());
715 		assertNull(u.getHost());
716 		assertNull(u.getUser());
717 		assertNull(u.getPass());
718 		assertEquals(-1, u.getPort());
719 		assertEquals(str, u.toPrivateString());
720 		assertEquals(str, u.toPrivateASCIIString());
721 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
722 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
723 		assertEquals(u, new URIish(str));
724 	}
725 
726 	@Test(expected = IllegalArgumentException.class)
727 	public void testGetNullHumanishName() {
728 		new URIish().getHumanishName();
729 	}
730 
731 	@Test(expected = IllegalArgumentException.class)
732 	public void testGetEmptyHumanishName() throws URISyntaxException {
733 		new URIish(GIT_SCHEME).getHumanishName();
734 	}
735 
736 	@Test(expected = IllegalArgumentException.class)
737 	public void testGetAbsEmptyHumanishName() {
738 		new URIish().getHumanishName();
739 	}
740 
741 	@Test
742 	public void testGetSet() throws Exception {
743 		final String str = "ssh://DOMAIN\\user:pass@example.com:33/some/p ath%20";
744 		URIish u = new URIish(str);
745 		u = u.setHost(u.getHost());
746 		u = u.setPass(u.getPass());
747 		u = u.setPort(u.getPort());
748 		assertEquals("ssh", u.getScheme());
749 		assertTrue(u.isRemote());
750 		u = u.setRawPath(u.getRawPath());
751 		assertEquals("/some/p ath%20", u.getRawPath());
752 		u = u.setPath(u.getPath());
753 		assertEquals("/some/p ath ", u.getRawPath());
754 		assertEquals("/some/p ath ", u.getPath());
755 		assertEquals("example.com", u.getHost());
756 		assertEquals("DOMAIN\\user", u.getUser());
757 		assertEquals("pass", u.getPass());
758 		assertEquals(33, u.getPort());
759 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p ath ",
760 				u.toPrivateString());
761 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p%20ath%20",
762 				u.toPrivateASCIIString());
763 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
764 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
765 		assertEquals(u, new URIish(str));
766 	}
767 
768 	@Test
769 	public void testGetValidWithEmptySlashDotGitHumanishName()
770 			throws IllegalArgumentException, URISyntaxException {
771 		String humanishName = new URIish("/a/b/.git").getHumanishName();
772 		assertEquals("b", humanishName);
773 	}
774 
775 	@Test
776 	public void testGetWithSlashDotGitHumanishName() throws URISyntaxException {
777 		assertEquals("", new URIish("/.git").getHumanishName());
778 	}
779 
780 	@Test
781 	public void testGetTwoSlashesDotGitHumanishName() throws URISyntaxException {
782 		assertEquals("", new URIish("//.git").getHumanishName());
783 	}
784 
785 	@Test
786 	public void testGetValidHumanishName() throws IllegalArgumentException,
787 			URISyntaxException {
788 		String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
789 		assertEquals("abc", humanishName);
790 	}
791 
792 	@Test
793 	public void testGetEmptyHumanishNameWithAuthorityOnly() throws IllegalArgumentException,
794 			URISyntaxException {
795 		String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
796 		assertEquals("abc", humanishName);
797 	}
798 
799 	@Test
800 	public void testGetValidSlashHumanishName()
801 			throws IllegalArgumentException, URISyntaxException {
802 		String humanishName = new URIish(GIT_SCHEME + "host/abc/")
803 				.getHumanishName();
804 		assertEquals("abc", humanishName);
805 	}
806 
807 	@Test
808 	public void testGetSlashValidSlashHumanishName()
809 			throws IllegalArgumentException, URISyntaxException {
810 		String humanishName = new URIish("/abc/").getHumanishName();
811 		assertEquals("abc", humanishName);
812 	}
813 
814 	@Test
815 	public void testGetSlashValidSlashDotGitSlashHumanishName()
816 			throws IllegalArgumentException, URISyntaxException {
817 		String humanishName = new URIish("/abc/.git").getHumanishName();
818 		assertEquals("abc", humanishName);
819 	}
820 
821 	@Test
822 	public void testGetSlashSlashDotGitSlashHumanishName()
823 			throws IllegalArgumentException, URISyntaxException {
824 		final String humanishName = new URIish(GIT_SCHEME + "/.git")
825 				.getHumanishName();
826 		assertEquals("may return an empty humanish name", "", humanishName);
827 	}
828 
829 	@Test
830 	public void testGetSlashesValidSlashHumanishName()
831 			throws IllegalArgumentException, URISyntaxException {
832 		String humanishName = new URIish("/a/b/c/").getHumanishName();
833 		assertEquals("c", humanishName);
834 	}
835 
836 	@Test
837 	public void testGetValidDotGitHumanishName()
838 			throws IllegalArgumentException, URISyntaxException {
839 		String humanishName = new URIish(GIT_SCHEME + "abc.git")
840 				.getHumanishName();
841 		assertEquals("abc", humanishName);
842 	}
843 
844 	@Test
845 	public void testGetValidDotGitSlashHumanishName()
846 			throws IllegalArgumentException, URISyntaxException {
847 		String humanishName = new URIish(GIT_SCHEME + "host.xy/abc.git/")
848 				.getHumanishName();
849 		assertEquals("abc", humanishName);
850 	}
851 
852 	@Test
853 	public void testGetValidWithSlashDotGitHumanishName()
854 			throws IllegalArgumentException, URISyntaxException {
855 		String humanishName = new URIish("/abc.git").getHumanishName();
856 		assertEquals("abc", humanishName);
857 	}
858 
859 	@Test
860 	public void testGetValidWithSlashDotGitSlashHumanishName()
861 			throws IllegalArgumentException, URISyntaxException {
862 		String humanishName = new URIish("/abc.git/").getHumanishName();
863 		assertEquals("abc", humanishName);
864 	}
865 
866 	@Test
867 	public void testGetValidWithSlashesDotGitHumanishName()
868 			throws IllegalArgumentException, URISyntaxException {
869 		String humanishName = new URIish("/a/b/c.git").getHumanishName();
870 		assertEquals("c", humanishName);
871 	}
872 
873 	@Test
874 	public void testGetValidWithSlashesDotGitSlashHumanishName()
875 			throws IllegalArgumentException, URISyntaxException {
876 		String humanishName = new URIish("/a/b/c.git/").getHumanishName();
877 		assertEquals("c", humanishName);
878 	}
879 
880 	@Test
881 	public void testGetValidLocalWithTwoSlashesHumanishName()
882 			throws IllegalArgumentException, URISyntaxException {
883 		String humanishName = new URIish("/a/b/c//").getHumanishName();
884 		assertEquals("c", humanishName);
885 	}
886 
887 	@Test
888 	public void testGetValidGitSchemeWithTwoSlashesHumanishName()
889 			throws IllegalArgumentException, URISyntaxException {
890 		String humanishName = new URIish(GIT_SCHEME + "/a/b/c//")
891 				.getHumanishName();
892 		assertEquals("c", humanishName);
893 	}
894 
895 	@Test
896 	public void testGetWindowsPathHumanishName()
897 			throws IllegalArgumentException,
898 			URISyntaxException {
899 		if (File.separatorChar == '\\') {
900 			String humanishName = new URIish("file:///C\\a\\b\\c.git/")
901 					.getHumanishName();
902 			assertEquals("c", humanishName);
903 		}
904 	}
905 
906 	@Test
907 	public void testUserPasswordAndPort() throws URISyntaxException {
908 		String str = "http://user:secret@host.xy:80/some/path";
909 		URIish u = new URIish(str);
910 		assertEquals("http", u.getScheme());
911 		assertTrue(u.isRemote());
912 		assertEquals("/some/path", u.getRawPath());
913 		assertEquals("/some/path", u.getPath());
914 		assertEquals("host.xy", u.getHost());
915 		assertEquals(80, u.getPort());
916 		assertEquals("user", u.getUser());
917 		assertEquals("secret", u.getPass());
918 		assertEquals(u, new URIish(str));
919 
920 		str = "http://user:secret@pass@host.xy:80/some/path";
921 		u = new URIish(str);
922 		assertEquals("http", u.getScheme());
923 		assertTrue(u.isRemote());
924 		assertEquals("/some/path", u.getPath());
925 		assertEquals("host.xy", u.getHost());
926 		assertEquals(80, u.getPort());
927 		assertEquals("user", u.getUser());
928 		assertEquals("secret@pass", u.getPass());
929 		assertEquals(u, new URIish(str));
930 	}
931 
932 	/**
933 	 * Exemplify what happens with the special case of encoding '/' as %2F. Web
934 	 * services in general parse path components before decoding the characters.
935 	 *
936 	 * @throws URISyntaxException
937 	 */
938 	@Test
939 	public void testPathSeparator() throws URISyntaxException {
940 		String str = "http://user:secret@host.xy:80/some%2Fpath";
941 		URIish u = new URIish(str);
942 		assertEquals("http", u.getScheme());
943 		assertTrue(u.isRemote());
944 		assertEquals("/some%2Fpath", u.getRawPath());
945 		assertEquals("/some/path", u.getPath());
946 		assertEquals("host.xy", u.getHost());
947 		assertEquals(80, u.getPort());
948 		assertEquals("user", u.getUser());
949 		assertEquals("secret", u.getPass());
950 		assertEquals(u, new URIish(str));
951 	}
952 
953 	@Test
954 	public void testFileProtocol() throws IllegalArgumentException,
955 			URISyntaxException, IOException {
956 		// as defined by git docu
957 		URIish u = new URIish("file:///a/b.txt");
958 		assertEquals("file", u.getScheme());
959 		assertFalse(u.isRemote());
960 		assertNull(u.getHost());
961 		assertNull(u.getPass());
962 		assertEquals("/a/b.txt", u.getRawPath());
963 		assertEquals("/a/b.txt", u.getPath());
964 		assertEquals(-1, u.getPort());
965 		assertNull(u.getUser());
966 		assertEquals("b.txt", u.getHumanishName());
967 
968 		File tmp = File.createTempFile("jgitUnitTest", ".tmp");
969 		u = new URIish(tmp.toURI().toString());
970 		assertEquals("file", u.getScheme());
971 		assertFalse(u.isRemote());
972 		assertNull(u.getHost());
973 		assertNull(u.getPass());
974 		assertTrue(u.getPath().contains("jgitUnitTest"));
975 		assertEquals(-1, u.getPort());
976 		assertNull(u.getUser());
977 		assertTrue(u.getHumanishName().startsWith("jgitUnitTest"));
978 
979 		u = new URIish("file:/a/b.txt");
980 		assertEquals("file", u.getScheme());
981 		assertFalse(u.isRemote());
982 		assertNull(u.getHost());
983 		assertNull(u.getPass());
984 		assertEquals("/a/b.txt", u.getRawPath());
985 		assertEquals("/a/b.txt", u.getPath());
986 		assertEquals(-1, u.getPort());
987 		assertNull(u.getUser());
988 		assertEquals("b.txt", u.getHumanishName());
989 	}
990 
991 	@Test
992 	public void testALot() throws URISyntaxException {
993 		// user pass host port path
994 		// 1 2 3 4 5
995 		String[][] tests = {
996 				new String[] { "%1$s://%2$s:%3$s@%4$s:%5$s/%6$s", "%1$s",
997 						"%2$s", "%3$s", "%4$s", "%5$s", "%6$s" },
998 				new String[] { "%1$s://%2$s@%4$s:%5$s/%6$s", "%1$s", "%2$s",
999 						null, "%4$s", "%5$s", "%6$s" },
1000 				new String[] { "%1$s://%2$s@%4$s/%6$s", "%1$s", "%2$s", null,
1001 						"%4$s", null, "%6$s" },
1002 				new String[] { "%1$s://%4$s/%6$s", "%1$s", null, null, "%4$s",
1003 						null, "%6$s" }, };
1004 		String[] schemes = new String[] { "ssh", "ssh+git", "http", "https" };
1005 		String[] users = new String[] { "me", "l usr\\example.com",
1006 				"lusr\\example" };
1007 		String[] passes = new String[] { "wtf", };
1008 		String[] hosts = new String[] { "example.com", "1.2.3.4", "[::1]" };
1009 		String[] ports = new String[] { "1234", "80" };
1010 		String[] paths = new String[] { "/", "/abc", "D:/x", "D:\\x" };
1011 		for (String[] test : tests) {
1012 			String fmt = test[0];
1013 			for (String scheme : schemes) {
1014 				for (String user : users) {
1015 					for (String pass : passes) {
1016 						for (String host : hosts) {
1017 							for (String port : ports) {
1018 								for (String path : paths) {
1019 									String url = String.format(fmt, scheme,
1020 											user, pass, host, port, path);
1021 									String[] expect = new String[test.length];
1022 									for (int i = 1; i < expect.length; ++i)
1023 										if (test[i] != null)
1024 											expect[i] = String.format(test[i],
1025 													scheme, user, pass, host,
1026 													port, path);
1027 									URIish urIish = new URIish(url);
1028 									assertEquals(url, expect[1],
1029 											urIish.getScheme());
1030 									assertEquals(url, expect[2],
1031 											urIish.getUser());
1032 								}
1033 							}
1034 						}
1035 					}
1036 				}
1037 			}
1038 		}
1039 	}
1040 
1041 	@Test
1042 	public void testStringConstructor() throws Exception {
1043 		String str = "http://example.com/";
1044 		URIish u = new URIish(str);
1045 		assertEquals("example.com", u.getHost());
1046 		assertEquals("/", u.getPath());
1047 		assertEquals(str, u.toString());
1048 
1049 		str = "http://example.com";
1050 		u = new URIish(str);
1051 		assertEquals("example.com", u.getHost());
1052 		assertEquals("", u.getPath());
1053 		assertEquals(str, u.toString());
1054 	}
1055 
1056 	@Test
1057 	public void testEqualsHashcode() throws Exception
1058 	{
1059 		String[] urls = { "http://user:pass@example.com:8081/path", "../x",
1060 				"ssh://x.y:23/z", "ssh://example.com:/path", "D:\\m y",
1061 				"\\\\some\\place", "http://localhost:1234",
1062 				"user@example.com:some/p ath", "a",
1063 				"http://user:pwd@example.com:8081/path",
1064 				"http://user:pass@another.com:8081/path",
1065 				"http://user:pass@example.com:8083/path" };
1066 		URIish w = new URIish("http://user:pass@example.com:8081/path/x");
1067 		for (String s : urls) {
1068 			URIish u = new URIish(s);
1069 			URIish v = new URIish(s);
1070 			assertTrue(u.equals(v));
1071 			assertTrue(v.equals(u));
1072 
1073 			assertFalse(u.equals(null));
1074 			assertFalse(u.equals(new Object()));
1075 			assertFalse(new Object().equals(u));
1076 			assertFalse(u.equals(w));
1077 			assertFalse(w.equals(u));
1078 
1079 			assertTrue(u.hashCode() == v.hashCode());
1080 			assertFalse(u.hashCode() == new Object().hashCode());
1081 		}
1082 	}
1083 }