| 1 |
package org.jboss.modules; |
| 2 |
|
| 3 |
import org.junit.Assert; |
| 4 |
import org.junit.Assume; |
| 5 |
import org.junit.Before; |
| 6 |
import org.junit.Rule; |
| 7 |
import org.junit.Test; |
| 8 |
import org.junit.rules.TemporaryFolder; |
| 9 |
|
| 10 |
import java.io.File; |
| 11 |
import java.io.IOException; |
| 12 |
import java.nio.file.FileSystems; |
| 13 |
import java.nio.file.Files; |
| 14 |
import java.nio.file.attribute.PosixFilePermission; |
| 15 |
import java.nio.file.attribute.PosixFilePermissions; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.List; |
| 18 |
import java.util.Set; |
| 19 |
|
| 20 |
public class LayeredModulePathFactoryTest { |
| 21 |
|
| 22 |
private static final String OVERLAYS = ".overlays"; |
| 23 |
private static final String OVERLAY_NAME = "overlay-1"; |
| 24 |
|
| 25 |
@Rule |
| 26 |
public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 27 |
|
| 28 |
private File layeringRoot; |
| 29 |
private File overlaysDir; |
| 30 |
private File metadataFile; |
| 31 |
private File overlayRoot; |
| 32 |
private final List<File> discoveredPaths = new ArrayList<>(); |
| 33 |
|
| 34 |
@Before |
| 35 |
public void setup() throws IOException { |
| 36 |
boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); |
| 37 |
Assume.assumeTrue("This test requires POSIX compatible OS", isPosix); |
| 38 |
|
| 39 |
layeringRoot = temporaryFolder.getRoot(); |
| 40 |
|
| 41 |
// layeringRoot/.overlays/ |
| 42 |
overlaysDir = new File(layeringRoot, OVERLAYS); |
| 43 |
Assert.assertTrue(overlaysDir.mkdir()); |
| 44 |
|
| 45 |
// layeringRoot/.overlays/.overlays file |
| 46 |
metadataFile = new File(overlaysDir, OVERLAYS); |
| 47 |
Assert.assertTrue(metadataFile.createNewFile()); |
| 48 |
writeRefsFile(metadataFile); |
| 49 |
|
| 50 |
// layeringRoot/.overlays/overlay-1/ |
| 51 |
overlayRoot = new File(overlaysDir, OVERLAY_NAME); |
| 52 |
Assert.assertTrue(overlayRoot.mkdir()); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void testReadable() { |
| 57 |
LayeredModulePathFactory.loadOverlays(layeringRoot, discoveredPaths); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void testUnreadableOverlays() throws IOException { |
| 62 |
boolean expectedFailure = false; |
| 63 |
Set<PosixFilePermission> origPermissions = Files.getPosixFilePermissions(overlaysDir.toPath()); |
| 64 |
try { |
| 65 |
// make directory non-readable |
| 66 |
Set<PosixFilePermission> testPermissions = PosixFilePermissions.fromString("-w-------"); |
| 67 |
Files.setPosixFilePermissions(overlaysDir.toPath(), testPermissions); |
| 68 |
|
| 69 |
LayeredModulePathFactory.loadOverlays(layeringRoot, discoveredPaths); |
| 70 |
} catch (IllegalStateException ise) { |
| 71 |
expectedFailure = ise.getMessage().startsWith("Overlays directory exists but is not readable: "); |
| 72 |
} finally { |
| 73 |
Files.setPosixFilePermissions(overlaysDir.toPath(), origPermissions); |
| 74 |
} |
| 75 |
|
| 76 |
Assert.assertTrue(expectedFailure); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void testUnreadableOverlaysMetadataFile() throws IOException { |
| 81 |
boolean expectedFailure = false; |
| 82 |
Set<PosixFilePermission> origPermissions = Files.getPosixFilePermissions(overlaysDir.toPath()); |
| 83 |
try { |
| 84 |
// make directory non-readable |
| 85 |
Set<PosixFilePermission> testPermissions = PosixFilePermissions.fromString("-w-------"); |
| 86 |
Files.setPosixFilePermissions(metadataFile.toPath(), testPermissions); |
| 87 |
|
| 88 |
try { |
| 89 |
LayeredModulePathFactory.loadOverlays(layeringRoot, discoveredPaths); |
| 90 |
} catch (IllegalStateException ise) { |
| 91 |
expectedFailure = ise.getMessage().startsWith("Overlays metadata file exists but is not readable: "); |
| 92 |
} |
| 93 |
} finally { |
| 94 |
Files.setPosixFilePermissions(overlaysDir.toPath(), origPermissions); |
| 95 |
} |
| 96 |
|
| 97 |
Assert.assertTrue(expectedFailure); |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void testUnreadableOverlayRoot() throws IOException { |
| 102 |
boolean expectedFailure = false; |
| 103 |
Set<PosixFilePermission> origPermissions = Files.getPosixFilePermissions(overlaysDir.toPath()); |
| 104 |
try { |
| 105 |
// make directory non-readable |
| 106 |
Set<PosixFilePermission> testPermissions = PosixFilePermissions.fromString("-w-------"); |
| 107 |
Files.setPosixFilePermissions(overlayRoot.toPath(), testPermissions); |
| 108 |
|
| 109 |
LayeredModulePathFactory.loadOverlays(layeringRoot, discoveredPaths); |
| 110 |
} catch (IllegalStateException ise) { |
| 111 |
expectedFailure = ise.getMessage().startsWith("Overlay root directory doesn't exists or is not readable: "); |
| 112 |
} finally { |
| 113 |
Files.setPosixFilePermissions(overlaysDir.toPath(), origPermissions); |
| 114 |
} |
| 115 |
|
| 116 |
Assert.assertTrue(expectedFailure); |
| 117 |
} |
| 118 |
|
| 119 |
private static void writeRefsFile(File file) throws IOException { |
| 120 |
Files.write(file.toPath(), (OVERLAY_NAME + '\n').getBytes()); |
| 121 |
} |
| 122 |
|
| 123 |
} |