View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.util;
12  
13  import static org.eclipse.jgit.util.QuotedString.BOURNE_USER_PATH;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertFalse;
16  import static org.junit.Assert.assertNotSame;
17  
18  import org.eclipse.jgit.lib.Constants;
19  import org.junit.Test;
20  
21  public class QuotedStringBourneUserPathStyleTest {
22  	private static void assertQuote(String in, String exp) {
23  		final String r = BOURNE_USER_PATH.quote(in);
24  		assertNotSame(in, r);
25  		assertFalse(in.equals(r));
26  		assertEquals('\'' + exp + '\'', r);
27  	}
28  
29  	private static void assertDequote(String exp, String in) {
30  		final byte[] b = Constants.encode('\'' + in + '\'');
31  		final String r = BOURNE_USER_PATH.dequote(b, 0, b.length);
32  		assertEquals(exp, r);
33  	}
34  
35  	@Test
36  	public void testQuote_Empty() {
37  		assertEquals("''", BOURNE_USER_PATH.quote(""));
38  	}
39  
40  	@Test
41  	public void testDequote_Empty1() {
42  		assertEquals("", BOURNE_USER_PATH.dequote(new byte[0], 0, 0));
43  	}
44  
45  	@Test
46  	public void testDequote_Empty2() {
47  		assertEquals("", BOURNE_USER_PATH.dequote(new byte[] { '\'', '\'' }, 0,
48  				2));
49  	}
50  
51  	@Test
52  	public void testDequote_SoleSq() {
53  		assertEquals("", BOURNE_USER_PATH.dequote(new byte[] { '\'' }, 0, 1));
54  	}
55  
56  	@Test
57  	public void testQuote_BareA() {
58  		assertQuote("a", "a");
59  	}
60  
61  	@Test
62  	public void testDequote_BareA() {
63  		final String in = "a";
64  		final byte[] b = Constants.encode(in);
65  		assertEquals(in, BOURNE_USER_PATH.dequote(b, 0, b.length));
66  	}
67  
68  	@Test
69  	public void testDequote_BareABCZ_OnlyBC() {
70  		final String in = "abcz";
71  		final byte[] b = Constants.encode(in);
72  		final int p = in.indexOf('b');
73  		assertEquals("bc", BOURNE_USER_PATH.dequote(b, p, p + 2));
74  	}
75  
76  	@Test
77  	public void testDequote_LoneBackslash() {
78  		assertDequote("\\", "\\");
79  	}
80  
81  	@Test
82  	public void testQuote_NamedEscapes() {
83  		assertQuote("'", "'\\''");
84  		assertQuote("!", "'\\!'");
85  
86  		assertQuote("a'b", "a'\\''b");
87  		assertQuote("a!b", "a'\\!'b");
88  	}
89  
90  	@Test
91  	public void testDequote_NamedEscapes() {
92  		assertDequote("'", "'\\''");
93  		assertDequote("!", "'\\!'");
94  
95  		assertDequote("a'b", "a'\\''b");
96  		assertDequote("a!b", "a'\\!'b");
97  	}
98  
99  	@Test
100 	public void testQuote_User() {
101 		assertEquals("~foo/", BOURNE_USER_PATH.quote("~foo"));
102 		assertEquals("~foo/", BOURNE_USER_PATH.quote("~foo/"));
103 		assertEquals("~/", BOURNE_USER_PATH.quote("~/"));
104 
105 		assertEquals("~foo/'a'", BOURNE_USER_PATH.quote("~foo/a"));
106 		assertEquals("~/'a'", BOURNE_USER_PATH.quote("~/a"));
107 	}
108 
109 	@Test
110 	public void testDequote_User() {
111 		assertEquals("~foo", BOURNE_USER_PATH.dequote("~foo"));
112 		assertEquals("~foo/", BOURNE_USER_PATH.dequote("~foo/"));
113 		assertEquals("~/", BOURNE_USER_PATH.dequote("~/"));
114 
115 		assertEquals("~foo/a", BOURNE_USER_PATH.dequote("~foo/'a'"));
116 		assertEquals("~/a", BOURNE_USER_PATH.dequote("~/'a'"));
117 	}
118 }