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 package org.eclipse.jgit.storage.file;
44
45 import static java.nio.charset.StandardCharsets.UTF_8;
46 import static org.eclipse.jgit.util.FileUtils.pathToString;
47 import static org.junit.Assert.assertArrayEquals;
48 import static org.junit.Assert.assertEquals;
49
50 import java.io.ByteArrayOutputStream;
51 import java.io.IOException;
52 import java.io.OutputStream;
53 import java.nio.file.Files;
54 import java.nio.file.Path;
55 import java.nio.file.StandardOpenOption;
56
57 import org.eclipse.jgit.errors.ConfigInvalidException;
58 import org.eclipse.jgit.junit.MockSystemReader;
59 import org.eclipse.jgit.util.FS;
60 import org.eclipse.jgit.util.FileUtils;
61 import org.eclipse.jgit.util.IO;
62 import org.eclipse.jgit.util.SystemReader;
63 import org.junit.After;
64 import org.junit.Before;
65 import org.junit.Test;
66
67 public class FileBasedConfigTest {
68
69 private static final String USER = "user";
70
71 private static final String NAME = "name";
72
73 private static final String ALICE = "Alice";
74
75 private static final String BOB = "Bob";
76
77 private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = "
78 + ALICE + "\n";
79
80 private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
81 + BOB + "\n";
82
83 private Path trash;
84
85 @Before
86 public void setUp() throws Exception {
87 SystemReader.setInstance(new MockSystemReader());
88 trash = Files.createTempDirectory("tmp_");
89 FS.getFileStoreAttributes(trash.getParent());
90 }
91
92 @After
93 public void tearDown() throws Exception {
94 FileUtils.delete(trash.toFile(),
95 FileUtils.RECURSIVE | FileUtils.SKIP_MISSING | FileUtils.RETRY);
96 }
97
98 @Test
99 public void testSystemEncoding() throws IOException, ConfigInvalidException {
100 final Path file = createFile(CONTENT1.getBytes());
101 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
102 FS.DETECTED);
103 config.load();
104 assertEquals(ALICE, config.getString(USER, null, NAME));
105
106 config.setString(USER, null, NAME, BOB);
107 config.save();
108 assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file.toFile()));
109 }
110
111 @Test
112 public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
113 final Path file = createFile(CONTENT1.getBytes(UTF_8));
114 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
115 FS.DETECTED);
116 config.load();
117 assertEquals(ALICE, config.getString(USER, null, NAME));
118
119 config.setString(USER, null, NAME, BOB);
120 config.save();
121 assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file.toFile()));
122 }
123
124 @Test
125 public void testUTF8withBOM() throws IOException, ConfigInvalidException {
126 final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
127 bos1.write(0xEF);
128 bos1.write(0xBB);
129 bos1.write(0xBF);
130 bos1.write(CONTENT1.getBytes(UTF_8));
131
132 final Path file = createFile(bos1.toByteArray());
133 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
134 FS.DETECTED);
135 config.load();
136 assertEquals(ALICE, config.getString(USER, null, NAME));
137
138 config.setString(USER, null, NAME, BOB);
139 config.save();
140
141 final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
142 bos2.write(0xEF);
143 bos2.write(0xBB);
144 bos2.write(0xBF);
145 bos2.write(CONTENT2.getBytes(UTF_8));
146 assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
147 }
148
149 @Test
150 public void testLeadingWhitespaces() throws IOException, ConfigInvalidException {
151 final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
152 bos1.write(" \n\t".getBytes());
153 bos1.write(CONTENT1.getBytes());
154
155 final Path file = createFile(bos1.toByteArray());
156 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
157 FS.DETECTED);
158 config.load();
159 assertEquals(ALICE, config.getString(USER, null, NAME));
160
161 config.setString(USER, null, NAME, BOB);
162 config.save();
163
164 final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
165 bos2.write(" \n\t".getBytes());
166 bos2.write(CONTENT2.getBytes());
167 assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
168 }
169
170 @Test
171 public void testIncludeAbsolute()
172 throws IOException, ConfigInvalidException {
173 final Path includedFile = createFile(CONTENT1.getBytes());
174 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
175 bos.write("[include]\npath=".getBytes());
176 bos.write(pathToString(includedFile.toFile()).getBytes());
177
178 final Path file = createFile(bos.toByteArray());
179 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
180 FS.DETECTED);
181 config.load();
182 assertEquals(ALICE, config.getString(USER, null, NAME));
183 }
184
185 @Test
186 public void testIncludeRelativeDot()
187 throws IOException, ConfigInvalidException {
188 final Path includedFile = createFile(CONTENT1.getBytes(), "dir1");
189 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
190 bos.write("[include]\npath=".getBytes());
191 bos.write(("./" + includedFile.getFileName()).getBytes());
192
193 final Path file = createFile(bos.toByteArray(), "dir1");
194 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
195 FS.DETECTED);
196 config.load();
197 assertEquals(ALICE, config.getString(USER, null, NAME));
198 }
199
200 @Test
201 public void testIncludeRelativeDotDot()
202 throws IOException, ConfigInvalidException {
203 final Path includedFile = createFile(CONTENT1.getBytes(), "dir1");
204 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
205 bos.write("[include]\npath=".getBytes());
206 bos.write(("../" + includedFile.getParent().getFileName() + "/"
207 + includedFile.getFileName()).getBytes());
208
209 final Path file = createFile(bos.toByteArray(), "dir2");
210 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
211 FS.DETECTED);
212 config.load();
213 assertEquals(ALICE, config.getString(USER, null, NAME));
214 }
215
216 @Test
217 public void testIncludeRelativeDotDotNotFound()
218 throws IOException, ConfigInvalidException {
219 final Path includedFile = createFile(CONTENT1.getBytes());
220 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
221 bos.write("[include]\npath=".getBytes());
222 bos.write(("../" + includedFile.getFileName()).getBytes());
223
224 final Path file = createFile(bos.toByteArray());
225 final FileBasedConfig config = new FileBasedConfig(file.toFile(),
226 FS.DETECTED);
227 config.load();
228 assertEquals(null, config.getString(USER, null, NAME));
229 }
230
231 @Test
232 public void testIncludeWithTilde()
233 throws IOException, ConfigInvalidException {
234 final Path includedFile = createFile(CONTENT1.getBytes(), "home");
235 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
236 bos.write("[include]\npath=".getBytes());
237 bos.write(("~/" + includedFile.getFileName()).getBytes());
238
239 final Path file = createFile(bos.toByteArray(), "repo");
240 final FS fs = FS.DETECTED.newInstance();
241 fs.setUserHome(includedFile.getParent().toFile());
242
243 final FileBasedConfig config = new FileBasedConfig(file.toFile(), fs);
244 config.load();
245 assertEquals(ALICE, config.getString(USER, null, NAME));
246 }
247
248 private Path createFile(byte[] content) throws IOException {
249 return createFile(content, null);
250 }
251
252 private Path createFile(byte[] content, String subdir) throws IOException {
253 Path dir = subdir != null ? trash.resolve(subdir) : trash;
254 Files.createDirectories(dir);
255
256 Path f = Files.createTempFile(dir, getClass().getName(), null);
257 try (OutputStream os = Files.newOutputStream(f,
258 StandardOpenOption.APPEND)) {
259 os.write(content);
260 }
261 return f;
262 }
263 }