1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static java.nio.charset.StandardCharsets.ISO_8859_1;
14 import static org.eclipse.jgit.util.QuotedString.GIT_PATH;
15 import static org.eclipse.jgit.util.QuotedString.GIT_PATH_MINIMAL;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertNotSame;
19 import static org.junit.Assert.assertSame;
20
21 import org.eclipse.jgit.lib.Constants;
22 import org.junit.Test;
23
24 public class QuotedStringGitPathStyleTest {
25 private static void assertQuote(String exp, String in) {
26 final String r = GIT_PATH.quote(in);
27 assertNotSame(in, r);
28 assertFalse(in.equals(r));
29 assertEquals('"' + exp + '"', r);
30 }
31
32 private static void assertDequote(String exp, String in) {
33 final byte[] b = ('"' + in + '"').getBytes(ISO_8859_1);
34 final String r = GIT_PATH.dequote(b, 0, b.length);
35 assertEquals(exp, r);
36 }
37
38 private static void assertDequoteMinimal(String exp, String in) {
39 final byte[] b = ('"' + in + '"').getBytes(ISO_8859_1);
40 final String r = GIT_PATH_MINIMAL.dequote(b, 0, b.length);
41 assertEquals(exp, r);
42 }
43
44 @Test
45 public void testQuote_Empty() {
46 assertEquals("\"\"", GIT_PATH.quote(""));
47 }
48
49 @Test
50 public void testDequote_Empty1() {
51 assertEquals("", GIT_PATH.dequote(new byte[0], 0, 0));
52 }
53
54 @Test
55 public void testDequote_Empty2() {
56 assertEquals("", GIT_PATH.dequote(new byte[] { '"', '"' }, 0, 2));
57 }
58
59 @Test
60 public void testDequote_SoleDq() {
61 assertEquals("\"", GIT_PATH.dequote(new byte[] { '"' }, 0, 1));
62 }
63
64 @Test
65 public void testQuote_BareA() {
66 final String in = "a";
67 assertSame(in, GIT_PATH.quote(in));
68 }
69
70 @Test
71 public void testDequote_BareA() {
72 final String in = "a";
73 final byte[] b = Constants.encode(in);
74 assertEquals(in, GIT_PATH.dequote(b, 0, b.length));
75 }
76
77 @Test
78 public void testDequote_BareABCZ_OnlyBC() {
79 final String in = "abcz";
80 final byte[] b = Constants.encode(in);
81 final int p = in.indexOf('b');
82 assertEquals("bc", GIT_PATH.dequote(b, p, p + 2));
83 }
84
85 @Test
86 public void testDequote_LoneBackslash() {
87 assertDequote("\\", "\\");
88 }
89
90 @Test
91 public void testQuote_NamedEscapes() {
92 assertQuote("\\a", "\u0007");
93 assertQuote("\\b", "\b");
94 assertQuote("\\f", "\f");
95 assertQuote("\\n", "\n");
96 assertQuote("\\r", "\r");
97 assertQuote("\\t", "\t");
98 assertQuote("\\v", "\u000B");
99 assertQuote("\\\\", "\\");
100 assertQuote("\\\"", "\"");
101 }
102
103 @Test
104 public void testDequote_NamedEscapes() {
105 assertDequote("\u0007", "\\a");
106 assertDequote("\b", "\\b");
107 assertDequote("\f", "\\f");
108 assertDequote("\n", "\\n");
109 assertDequote("\r", "\\r");
110 assertDequote("\t", "\\t");
111 assertDequote("\u000B", "\\v");
112 assertDequote("\\", "\\\\");
113 assertDequote("\"", "\\\"");
114 }
115
116 @Test
117 public void testDequote_OctalAll() {
118 for (int i = 0; i < 127; i++) {
119 assertDequote("" + (char) i, octalEscape(i));
120 }
121 for (int i = 128; i < 256; i++) {
122 int f = 0xC0 | (i >> 6);
123 int s = 0x80 | (i & 0x3f);
124 assertDequote("" + (char) i, octalEscape(f)+octalEscape(s));
125 }
126 }
127
128 private static String octalEscape(int i) {
129 String s = Integer.toOctalString(i);
130 while (s.length() < 3) {
131 s = "0" + s;
132 }
133 return "\\"+s;
134 }
135
136 @Test
137 public void testQuote_OctalAll() {
138 assertQuote("\\001", "\1");
139 assertQuote("\\177", "\u007f");
140 assertQuote("\\303\\277", "\u00ff");
141 }
142
143 @Test
144 public void testDequote_UnknownEscapeQ() {
145 assertDequote("\\q", "\\q");
146 }
147
148 @Test
149 public void testDequote_FooTabBar() {
150 assertDequote("foo\tbar", "foo\\tbar");
151 }
152
153 @Test
154 public void testDequote_Latin1() {
155 assertDequote("\u00c5ngstr\u00f6m", "\\305ngstr\\366m");
156 }
157
158 @Test
159 public void testDequote_UTF8() {
160 assertDequote("\u00c5ngstr\u00f6m", "\\303\\205ngstr\\303\\266m");
161 }
162
163 @Test
164 public void testDequote_RawUTF8() {
165 assertDequote("\u00c5ngstr\u00f6m", "\303\205ngstr\303\266m");
166 }
167
168 @Test
169 public void testDequote_RawLatin1() {
170 assertDequote("\u00c5ngstr\u00f6m", "\305ngstr\366m");
171 }
172
173 @Test
174 public void testQuote_Ang() {
175 assertQuote("\\303\\205ngstr\\303\\266m", "\u00c5ngstr\u00f6m");
176 }
177
178 @Test
179 public void testQuoteAtAndNumber() {
180 assertSame("abc@2x.png", GIT_PATH.quote("abc@2x.png"));
181 assertDequote("abc@2x.png", "abc\\1002x.png");
182 }
183
184 @Test
185 public void testNoQuote() {
186 assertSame("\u00c5ngstr\u00f6m",
187 GIT_PATH_MINIMAL.quote("\u00c5ngstr\u00f6m"));
188 }
189
190 @Test
191 public void testQuoteMinimal() {
192 assertEquals("\"\u00c5n\\\\str\u00f6m\"",
193 GIT_PATH_MINIMAL.quote("\u00c5n\\str\u00f6m"));
194 }
195
196 @Test
197 public void testDequoteMinimal() {
198 assertEquals("\u00c5n\\str\u00f6m", GIT_PATH_MINIMAL
199 .dequote(GIT_PATH_MINIMAL.quote("\u00c5n\\str\u00f6m")));
200
201 }
202
203 @Test
204 public void testRoundtripMinimal() {
205 assertEquals("\u00c5ngstr\u00f6m", GIT_PATH_MINIMAL
206 .dequote(GIT_PATH_MINIMAL.quote("\u00c5ngstr\u00f6m")));
207
208 }
209
210 @Test
211 public void testQuoteMinimalDequoteNormal() {
212 assertEquals("\u00c5n\\str\u00f6m", GIT_PATH
213 .dequote(GIT_PATH_MINIMAL.quote("\u00c5n\\str\u00f6m")));
214
215 }
216
217 @Test
218 public void testQuoteNormalDequoteMinimal() {
219 assertEquals("\u00c5n\\str\u00f6m", GIT_PATH_MINIMAL
220 .dequote(GIT_PATH.quote("\u00c5n\\str\u00f6m")));
221
222 }
223
224 @Test
225 public void testRoundtripMinimalDequoteNormal() {
226 assertEquals("\u00c5ngstr\u00f6m",
227 GIT_PATH.dequote(GIT_PATH_MINIMAL.quote("\u00c5ngstr\u00f6m")));
228
229 }
230
231 @Test
232 public void testRoundtripNormalDequoteMinimal() {
233 assertEquals("\u00c5ngstr\u00f6m",
234 GIT_PATH_MINIMAL.dequote(GIT_PATH.quote("\u00c5ngstr\u00f6m")));
235
236 }
237
238 @Test
239 public void testDequote_UTF8_Minimal() {
240 assertDequoteMinimal("\u00c5ngstr\u00f6m",
241 "\\303\\205ngstr\\303\\266m");
242 }
243
244 @Test
245 public void testDequote_RawUTF8_Minimal() {
246 assertDequoteMinimal("\u00c5ngstr\u00f6m", "\303\205ngstr\303\266m");
247 }
248
249 @Test
250 public void testDequote_RawLatin1_Minimal() {
251 assertDequoteMinimal("\u00c5ngstr\u00f6m", "\305ngstr\366m");
252 }
253
254 }