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.3KB

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