|
3 | 3 | import static org.hamcrest.CoreMatchers.hasItem;
|
4 | 4 | import static org.hamcrest.MatcherAssert.assertThat;
|
5 | 5 | import static org.hamcrest.core.IsNot.not;
|
| 6 | +import static org.junit.Assert.assertEquals; |
6 | 7 | import static org.junit.Assert.assertFalse;
|
7 | 8 | import static org.junit.Assert.assertTrue;
|
| 9 | +import static org.junit.Assume.assumeTrue; |
8 | 10 | import static org.junit.experimental.results.PrintableResult.testResult;
|
9 | 11 | import static org.junit.experimental.results.ResultMatchers.failureCountIs;
|
10 | 12 | import static org.junit.experimental.results.ResultMatchers.isSuccessful;
|
11 | 13 |
|
12 | 14 | import java.io.File;
|
13 | 15 | import java.io.IOException;
|
| 16 | +import java.lang.reflect.Array; |
| 17 | +import java.lang.reflect.InvocationTargetException; |
14 | 18 | import java.lang.reflect.Method;
|
15 | 19 | import java.util.Arrays;
|
| 20 | +import java.util.Set; |
| 21 | +import java.util.SortedSet; |
| 22 | +import java.util.TreeSet; |
16 | 23 |
|
17 | 24 | import org.junit.After;
|
| 25 | +import org.junit.AssumptionViolatedException; |
18 | 26 | import org.junit.Rule;
|
19 | 27 | import org.junit.Test;
|
20 |
| -import org.junit.rules.TemporaryFolder; |
21 | 28 |
|
22 | 29 | public class TempFolderRuleTest {
|
23 | 30 | private static File[] createdFiles = new File[20];
|
@@ -182,6 +189,34 @@ public void recursiveDeleteFolderWithZeroElements() throws IOException {
|
182 | 189 | assertFalse(folder.getRoot().exists());
|
183 | 190 | }
|
184 | 191 |
|
| 192 | + @Test |
| 193 | + public void tempFolderIsOnlyAccessibleByOwner() throws IOException { |
| 194 | + TemporaryFolder folder = new TemporaryFolder(); |
| 195 | + folder.create(); |
| 196 | + |
| 197 | + Set<String> expectedPermissions = new TreeSet<String>(Arrays.asList("OWNER_READ", "OWNER_WRITE", "OWNER_EXECUTE")); |
| 198 | + Set<String> actualPermissions = getPosixFilePermissions(folder.getRoot()); |
| 199 | + assertEquals(expectedPermissions, actualPermissions); |
| 200 | + } |
| 201 | + |
| 202 | + private Set<String> getPosixFilePermissions(File root) { |
| 203 | + try { |
| 204 | + Class<?> pathClass = Class.forName("java.nio.file.Path"); |
| 205 | + Object linkOptionArray = Array.newInstance(Class.forName("java.nio.file.LinkOption"), 0); |
| 206 | + Class<?> filesClass = Class.forName("java.nio.file.Files"); |
| 207 | + Object path = File.class.getDeclaredMethod("toPath").invoke(root); |
| 208 | + Method posixFilePermissionsMethod = filesClass.getDeclaredMethod("getPosixFilePermissions", pathClass, linkOptionArray.getClass()); |
| 209 | + Set<?> permissions = (Set<?>) posixFilePermissionsMethod.invoke(null, path, linkOptionArray); |
| 210 | + SortedSet<String> convertedPermissions = new TreeSet<String>(); |
| 211 | + for (Object item : permissions) { |
| 212 | + convertedPermissions.add(item.toString()); |
| 213 | + } |
| 214 | + return convertedPermissions; |
| 215 | + } catch (Exception e) { |
| 216 | + throw new AssumptionViolatedException("Test requires at least Java 1.7", e); |
| 217 | + } |
| 218 | + } |
| 219 | + |
185 | 220 | public static class NameClashes {
|
186 | 221 | @Rule
|
187 | 222 | public TemporaryFolder folder = new TemporaryFolder();
|
|
0 commit comments