| 1 |
package ch.systemsx.cisd.base.unix; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.lang.reflect.InvocationTargetException; |
| 6 |
import java.lang.reflect.Method; |
| 7 |
import java.nio.charset.Charset; |
| 8 |
|
| 9 |
import org.apache.commons.io.FileUtils; |
| 10 |
import org.testng.annotations.Test; |
| 11 |
|
| 12 |
import ch.systemsx.cisd.base.BuildAndEnvironmentInfo; |
| 13 |
import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase; |
| 14 |
import ch.systemsx.cisd.base.unix.Unix.Stat; |
| 15 |
|
| 16 |
/** |
| 17 |
* Tests of the {@link Unix} class that can only be performed as user root. |
| 18 |
*/ |
| 19 |
public class UnixRootTests extends AbstractFileSystemTestCase |
| 20 |
{ |
| 21 |
@Test(groups = |
| 22 |
{ "requires_unix" }) |
| 23 |
public void testChown() throws IOException |
| 24 |
{ |
| 25 |
if (Unix.getUid() != 0) |
| 26 |
{ |
| 27 |
System.out.println("Skipping test as we are not root."); |
| 28 |
return; |
| 29 |
} |
| 30 |
final short accessMode = (short) 0777; |
| 31 |
final String content = "someText\n"; |
| 32 |
final File f = new File(workingDirectory, "someFile"); |
| 33 |
final File s = new File(workingDirectory, "MyLink"); |
| 34 |
FileUtils.writeStringToFile(f, content, Charset.defaultCharset()); |
| 35 |
Unix.setAccessMode(f.getAbsolutePath(), accessMode); |
| 36 |
final Stat info = Unix.getLinkInfo(f.getAbsolutePath()); |
| 37 |
Unix.setOwner(f.getAbsolutePath(), info.getUid(), info.getGid()); |
| 38 |
assertEquals(1, info.getNumberOfHardLinks()); |
| 39 |
assertEquals(content.length(), info.getSize()); |
| 40 |
assertEquals(accessMode, info.getPermissions()); |
| 41 |
final Unix.Password nobody = Unix.tryGetUserByName("nobody"); |
| 42 |
assertNotNull(nobody); |
| 43 |
final Unix.Password daemon = Unix.tryGetUserByName("daemon"); |
| 44 |
assertNotNull(daemon); |
| 45 |
Unix.setOwner(f.getAbsolutePath(), nobody); |
| 46 |
Unix.createSymbolicLink(f.getAbsolutePath(), s.getAbsolutePath()); |
| 47 |
Unix.setLinkOwner(s.getAbsolutePath(), daemon); |
| 48 |
|
| 49 |
final Unix.Stat fileInfo = Unix.getFileInfo(s.getAbsolutePath()); |
| 50 |
assertEquals(nobody.getUid(), fileInfo.getUid()); |
| 51 |
assertEquals(nobody.getGid(), fileInfo.getGid()); |
| 52 |
|
| 53 |
final Unix.Stat linkInfo = Unix.getLinkInfo(s.getAbsolutePath()); |
| 54 |
assertEquals(daemon.getUid(), linkInfo.getUid()); |
| 55 |
assertEquals(daemon.getGid(), linkInfo.getGid()); |
| 56 |
} |
| 57 |
|
| 58 |
public static void main(String[] args) throws Throwable |
| 59 |
{ |
| 60 |
System.out.println(BuildAndEnvironmentInfo.INSTANCE); |
| 61 |
System.out.println("Test class: " + UnixRootTests.class.getSimpleName()); |
| 62 |
System.out.println(); |
| 63 |
if (Unix.isOperational() == false) |
| 64 |
{ |
| 65 |
System.err.println("No unix library found."); |
| 66 |
System.exit(1); |
| 67 |
} |
| 68 |
final UnixRootTests test = new UnixRootTests(); |
| 69 |
try |
| 70 |
{ |
| 71 |
for (Method m : UnixRootTests.class.getMethods()) |
| 72 |
{ |
| 73 |
final Test testAnnotation = m.getAnnotation(Test.class); |
| 74 |
if (testAnnotation == null) |
| 75 |
{ |
| 76 |
continue; |
| 77 |
} |
| 78 |
System.out.println("Running " + m.getName()); |
| 79 |
test.setUp(); |
| 80 |
try |
| 81 |
{ |
| 82 |
m.invoke(test); |
| 83 |
} catch (InvocationTargetException wrapperThrowable) |
| 84 |
{ |
| 85 |
final Throwable th = wrapperThrowable.getCause(); |
| 86 |
boolean exceptionFound = false; |
| 87 |
for (Class<?> expectedExClazz : testAnnotation.expectedExceptions()) |
| 88 |
{ |
| 89 |
if (expectedExClazz == th.getClass()) |
| 90 |
{ |
| 91 |
exceptionFound = true; |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
if (exceptionFound == false) |
| 96 |
{ |
| 97 |
throw th; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
System.out.println("Tests OK!"); |
| 102 |
} finally |
| 103 |
{ |
| 104 |
test.afterClass(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
} |