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