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.

FSTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (C) 2012-2013, Robin Rosenberg <robin.rosenberg@dewire.com>
  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.util;
  44. import static java.time.Instant.EPOCH;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assume.assumeNoException;
  49. import static org.junit.Assume.assumeTrue;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.nio.charset.Charset;
  53. import java.nio.file.Files;
  54. import java.nio.file.InvalidPathException;
  55. import java.nio.file.Path;
  56. import java.nio.file.attribute.FileTime;
  57. import java.nio.file.attribute.PosixFileAttributeView;
  58. import java.nio.file.attribute.PosixFilePermission;
  59. import java.time.Duration;
  60. import java.time.ZoneId;
  61. import java.time.format.DateTimeFormatter;
  62. import java.util.Locale;
  63. import java.util.Set;
  64. import java.util.concurrent.TimeUnit;
  65. import org.eclipse.jgit.errors.CommandFailedException;
  66. import org.eclipse.jgit.junit.MockSystemReader;
  67. import org.eclipse.jgit.junit.RepositoryTestCase;
  68. import org.eclipse.jgit.lib.RepositoryCache;
  69. import org.junit.After;
  70. import org.junit.Assume;
  71. import org.junit.Before;
  72. import org.junit.Test;
  73. public class FSTest {
  74. private File trash;
  75. @Before
  76. public void setUp() throws Exception {
  77. SystemReader.setInstance(new MockSystemReader());
  78. trash = File.createTempFile("tmp_", "");
  79. trash.delete();
  80. assertTrue("mkdir " + trash, trash.mkdir());
  81. }
  82. @After
  83. public void tearDown() throws Exception {
  84. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  85. }
  86. /**
  87. * The old File methods traverse symbolic links and look at the targets.
  88. * With symbolic links we usually want to modify/look at the link. For some
  89. * reason the executable attribute seems to always look at the target, but
  90. * for the other attributes like lastModified, hidden and exists we must
  91. * differ between the link and the target.
  92. *
  93. * @throws IOException
  94. * @throws InterruptedException
  95. */
  96. @Test
  97. public void testSymlinkAttributes() throws IOException, InterruptedException {
  98. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  99. FS fs = FS.DETECTED;
  100. File link = new File(trash, "a");
  101. File target = new File(trash, "b");
  102. fs.createSymLink(link, "b");
  103. assertTrue(fs.exists(link));
  104. String targetName = fs.readSymLink(link);
  105. assertEquals("b", targetName);
  106. assertTrue(fs.lastModifiedInstant(link).compareTo(EPOCH) > 0);
  107. assertTrue(fs.exists(link));
  108. assertFalse(fs.canExecute(link));
  109. // The length of a symbolic link is a length of the target file path.
  110. assertEquals(1, fs.length(link));
  111. assertFalse(fs.exists(target));
  112. assertFalse(fs.isFile(target));
  113. assertFalse(fs.isDirectory(target));
  114. assertFalse(fs.canExecute(target));
  115. RepositoryTestCase.fsTick(link);
  116. // Now create the link target
  117. FileUtils.createNewFile(target);
  118. assertTrue(fs.exists(link));
  119. assertTrue(fs.lastModifiedInstant(link).compareTo(EPOCH) > 0);
  120. assertTrue(fs.lastModifiedInstant(target)
  121. .compareTo(fs.lastModifiedInstant(link)) > 0);
  122. assertFalse(fs.canExecute(link));
  123. fs.setExecute(target, true);
  124. assertFalse(fs.canExecute(link));
  125. assumeTrue(fs.supportsExecute());
  126. assertTrue(fs.canExecute(target));
  127. }
  128. @Test
  129. public void testUnicodeFilePath() throws IOException {
  130. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  131. FS fs = FS.DETECTED;
  132. File link = new File(trash, "ä");
  133. File target = new File(trash, "å");
  134. try {
  135. // Check if the runtime can support Unicode file paths.
  136. link.toPath();
  137. target.toPath();
  138. } catch (InvalidPathException e) {
  139. // When executing a test with LANG environment variable set to non
  140. // UTF-8 encoding, it seems that JRE cannot handle Unicode file
  141. // paths. This happens when this test is executed in Bazel as it
  142. // unsets LANG
  143. // (https://docs.bazel.build/versions/master/test-encyclopedia.html#initial-conditions).
  144. // Skip the test if the runtime cannot handle Unicode characters.
  145. assumeNoException(e);
  146. }
  147. fs.createSymLink(link, "å");
  148. assertTrue(fs.exists(link));
  149. assertEquals("å", fs.readSymLink(link));
  150. }
  151. @Test
  152. public void testExecutableAttributes() throws Exception {
  153. FS fs = FS.DETECTED.newInstance();
  154. // If this assumption fails the test is halted and ignored.
  155. assumeTrue(fs instanceof FS_POSIX);
  156. ((FS_POSIX) fs).setUmask(0022);
  157. File f = new File(trash, "bla");
  158. assertTrue(f.createNewFile());
  159. assertFalse(fs.canExecute(f));
  160. Set<PosixFilePermission> permissions = readPermissions(f);
  161. assertTrue(!permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  162. assertTrue(!permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  163. assertTrue(!permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  164. fs.setExecute(f, true);
  165. permissions = readPermissions(f);
  166. assertTrue("'owner' execute permission not set",
  167. permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  168. assertTrue("'group' execute permission not set",
  169. permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  170. assertTrue("'others' execute permission not set",
  171. permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  172. ((FS_POSIX) fs).setUmask(0033);
  173. fs.setExecute(f, false);
  174. assertFalse(fs.canExecute(f));
  175. fs.setExecute(f, true);
  176. permissions = readPermissions(f);
  177. assertTrue("'owner' execute permission not set",
  178. permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  179. assertFalse("'group' execute permission set",
  180. permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  181. assertFalse("'others' execute permission set",
  182. permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  183. }
  184. private Set<PosixFilePermission> readPermissions(File f) throws IOException {
  185. return Files
  186. .getFileAttributeView(f.toPath(), PosixFileAttributeView.class)
  187. .readAttributes().permissions();
  188. }
  189. @Test(expected = CommandFailedException.class)
  190. public void testReadPipePosixCommandFailure()
  191. throws CommandFailedException {
  192. FS fs = FS.DETECTED.newInstance();
  193. assumeTrue(fs instanceof FS_POSIX);
  194. FS.readPipe(fs.userHome(),
  195. new String[] { "/bin/sh", "-c", "exit 1" },
  196. Charset.defaultCharset().name());
  197. }
  198. @Test(expected = CommandFailedException.class)
  199. public void testReadPipeCommandStartFailure()
  200. throws CommandFailedException {
  201. FS fs = FS.DETECTED.newInstance();
  202. FS.readPipe(fs.userHome(),
  203. new String[] { "this-command-does-not-exist" },
  204. Charset.defaultCharset().name());
  205. }
  206. @Test
  207. public void testFsTimestampResolution() throws Exception {
  208. DateTimeFormatter formatter = DateTimeFormatter
  209. .ofPattern("uuuu-MMM-dd HH:mm:ss.nnnnnnnnn", Locale.ENGLISH)
  210. .withZone(ZoneId.systemDefault());
  211. Path dir = Files.createTempDirectory("probe-filesystem");
  212. Duration resolution = FS.getFileStoreAttributes(dir)
  213. .getFsTimestampResolution();
  214. long resolutionNs = resolution.toNanos();
  215. assertTrue(resolutionNs > 0);
  216. for (int i = 0; i < 10; i++) {
  217. Path f = null;
  218. try {
  219. f = dir.resolve("testTimestampResolution" + i);
  220. Files.createFile(f);
  221. FileUtils.touch(f);
  222. FileTime t1 = Files.getLastModifiedTime(f);
  223. TimeUnit.NANOSECONDS.sleep(resolutionNs);
  224. FileUtils.touch(f);
  225. FileTime t2 = Files.getLastModifiedTime(f);
  226. assertTrue(String.format(
  227. "expected t2=%s to be larger than t1=%s\nsince file timestamp resolution was measured to be %,d ns",
  228. formatter.format(t2.toInstant()),
  229. formatter.format(t1.toInstant()),
  230. Long.valueOf(resolutionNs)), t2.compareTo(t1) > 0);
  231. } finally {
  232. Files.delete(f);
  233. }
  234. }
  235. }
  236. // bug 548682
  237. @Test
  238. public void testRepoCacheRelativePathUnbornRepo() {
  239. assertFalse(RepositoryCache.FileKey
  240. .isGitRepository(new File("repo.git"), FS.DETECTED));
  241. }
  242. }