1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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");
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");
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 }