View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc.
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.util;
45  
46  import static java.nio.charset.StandardCharsets.ISO_8859_1;
47  import static org.eclipse.jgit.util.QuotedString.GIT_PATH;
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertFalse;
50  import static org.junit.Assert.assertNotSame;
51  import static org.junit.Assert.assertSame;
52  
53  import org.eclipse.jgit.lib.Constants;
54  import org.junit.Test;
55  
56  public class QuotedStringGitPathStyleTest {
57  	private static void assertQuote(String exp, String in) {
58  		final String r = GIT_PATH.quote(in);
59  		assertNotSame(in, r);
60  		assertFalse(in.equals(r));
61  		assertEquals('"' + exp + '"', r);
62  	}
63  
64  	private static void assertDequote(String exp, String in) {
65  		final byte[] b = ('"' + in + '"').getBytes(ISO_8859_1);
66  		final String r = GIT_PATH.dequote(b, 0, b.length);
67  		assertEquals(exp, r);
68  	}
69  
70  	@Test
71  	public void testQuote_Empty() {
72  		assertEquals("\"\"", GIT_PATH.quote(""));
73  	}
74  
75  	@Test
76  	public void testDequote_Empty1() {
77  		assertEquals("", GIT_PATH.dequote(new byte[0], 0, 0));
78  	}
79  
80  	@Test
81  	public void testDequote_Empty2() {
82  		assertEquals("", GIT_PATH.dequote(new byte[] { '"', '"' }, 0, 2));
83  	}
84  
85  	@Test
86  	public void testDequote_SoleDq() {
87  		assertEquals("\"", GIT_PATH.dequote(new byte[] { '"' }, 0, 1));
88  	}
89  
90  	@Test
91  	public void testQuote_BareA() {
92  		final String in = "a";
93  		assertSame(in, GIT_PATH.quote(in));
94  	}
95  
96  	@Test
97  	public void testDequote_BareA() {
98  		final String in = "a";
99  		final byte[] b = Constants.encode(in);
100 		assertEquals(in, GIT_PATH.dequote(b, 0, b.length));
101 	}
102 
103 	@Test
104 	public void testDequote_BareABCZ_OnlyBC() {
105 		final String in = "abcz";
106 		final byte[] b = Constants.encode(in);
107 		final int p = in.indexOf('b');
108 		assertEquals("bc", GIT_PATH.dequote(b, p, p + 2));
109 	}
110 
111 	@Test
112 	public void testDequote_LoneBackslash() {
113 		assertDequote("\\", "\\");
114 	}
115 
116 	@Test
117 	public void testQuote_NamedEscapes() {
118 		assertQuote("\\a", "\u0007");
119 		assertQuote("\\b", "\b");
120 		assertQuote("\\f", "\f");
121 		assertQuote("\\n", "\n");
122 		assertQuote("\\r", "\r");
123 		assertQuote("\\t", "\t");
124 		assertQuote("\\v", "\u000B");
125 		assertQuote("\\\\", "\\");
126 		assertQuote("\\\"", "\"");
127 	}
128 
129 	@Test
130 	public void testDequote_NamedEscapes() {
131 		assertDequote("\u0007", "\\a");
132 		assertDequote("\b", "\\b");
133 		assertDequote("\f", "\\f");
134 		assertDequote("\n", "\\n");
135 		assertDequote("\r", "\\r");
136 		assertDequote("\t", "\\t");
137 		assertDequote("\u000B", "\\v");
138 		assertDequote("\\", "\\\\");
139 		assertDequote("\"", "\\\"");
140 	}
141 
142 	@Test
143 	public void testDequote_OctalAll() {
144 		for (int i = 0; i < 127; i++) {
145 			assertDequote("" + (char) i, octalEscape(i));
146 		}
147 		for (int i = 128; i < 256; i++) {
148 			int f = 0xC0 | (i >> 6);
149 			int s = 0x80 | (i & 0x3f);
150 			assertDequote("" + (char) i, octalEscape(f)+octalEscape(s));
151 		}
152 	}
153 
154 	private static String octalEscape(int i) {
155 		String s = Integer.toOctalString(i);
156 		while (s.length() < 3) {
157 			s = "0" + s;
158 		}
159 		return "\\"+s;
160 	}
161 
162 	@Test
163 	public void testQuote_OctalAll() {
164 		assertQuote("\\001", "\1");
165 		assertQuote("\\177", "\u007f");
166 		assertQuote("\\303\\277", "\u00ff"); // \u00ff in UTF-8
167 	}
168 
169 	@Test
170 	public void testDequote_UnknownEscapeQ() {
171 		assertDequote("\\q", "\\q");
172 	}
173 
174 	@Test
175 	public void testDequote_FooTabBar() {
176 		assertDequote("foo\tbar", "foo\\tbar");
177 	}
178 
179 	@Test
180 	public void testDequote_Latin1() {
181 		assertDequote("\u00c5ngstr\u00f6m", "\\305ngstr\\366m"); // Latin1
182 	}
183 
184 	@Test
185 	public void testDequote_UTF8() {
186 		assertDequote("\u00c5ngstr\u00f6m", "\\303\\205ngstr\\303\\266m");
187 	}
188 
189 	@Test
190 	public void testDequote_RawUTF8() {
191 		assertDequote("\u00c5ngstr\u00f6m", "\303\205ngstr\303\266m");
192 	}
193 
194 	@Test
195 	public void testDequote_RawLatin1() {
196 		assertDequote("\u00c5ngstr\u00f6m", "\305ngstr\366m");
197 	}
198 
199 	@Test
200 	public void testQuote_Ang() {
201 		assertQuote("\\303\\205ngstr\\303\\266m", "\u00c5ngstr\u00f6m");
202 	}
203 
204 	@Test
205 	public void testQuoteAtAndNumber() {
206 		assertSame("abc@2x.png", GIT_PATH.quote("abc@2x.png"));
207 		assertDequote("abc@2x.png", "abc\\1002x.png");
208 	}
209 }