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