You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileBasedConfigTest.java 9.0KB

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