|
28 | 28 |
|
29 | 29 | import org.apache.commons.logging.Log; |
30 | 30 | import org.apache.commons.logging.LogFactory; |
| 31 | +import org.apache.hadoop.conf.Configuration; |
31 | 32 | import org.apache.hadoop.fs.FileUtil; |
| 33 | +import org.apache.hadoop.fs.FileSystem; |
| 34 | +import org.apache.hadoop.fs.Path; |
| 35 | +import org.apache.hadoop.fs.permission.FsPermission; |
32 | 36 | import org.apache.hadoop.util.NativeCodeLoader; |
33 | 37 |
|
34 | 38 | public class TestNativeIO { |
@@ -134,4 +138,34 @@ public void testFDDoesntLeak() throws IOException { |
134 | 138 | } |
135 | 139 | } |
136 | 140 |
|
| 141 | + /** |
| 142 | + * Test basic chmod operation |
| 143 | + */ |
| 144 | + @Test |
| 145 | + public void testChmod() throws Exception { |
| 146 | + try { |
| 147 | + NativeIO.chmod("/this/file/doesnt/exist", 777); |
| 148 | + fail("Chmod of non-existent file didn't fail"); |
| 149 | + } catch (NativeIOException nioe) { |
| 150 | + assertEquals(Errno.ENOENT, nioe.getErrno()); |
| 151 | + } |
| 152 | + |
| 153 | + File toChmod = new File(TEST_DIR, "testChmod"); |
| 154 | + assertTrue("Create test subject", |
| 155 | + toChmod.exists() || toChmod.mkdir()); |
| 156 | + NativeIO.chmod(toChmod.getAbsolutePath(), 0777); |
| 157 | + assertPermissions(toChmod, 0777); |
| 158 | + NativeIO.chmod(toChmod.getAbsolutePath(), 0000); |
| 159 | + assertPermissions(toChmod, 0000); |
| 160 | + NativeIO.chmod(toChmod.getAbsolutePath(), 0644); |
| 161 | + assertPermissions(toChmod, 0644); |
| 162 | + } |
| 163 | + |
| 164 | + private void assertPermissions(File f, int expected) throws IOException { |
| 165 | + FileSystem localfs = FileSystem.getLocal(new Configuration()); |
| 166 | + FsPermission perms = localfs.getFileStatus( |
| 167 | + new Path(f.getAbsolutePath())).getPermission(); |
| 168 | + assertEquals(expected, perms.toShort()); |
| 169 | + } |
| 170 | + |
137 | 171 | } |
0 commit comments