Test that Path.copyTo() copies the source file access permissions
This commit is contained in:
committed by
Space
parent
f0da420b1f
commit
6ddb0326bb
@@ -61,7 +61,7 @@ abstract class AbstractPathTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun withRestrictedRead(vararg paths: Path, block: () -> Unit) {
|
fun withRestrictedRead(vararg paths: Path, alsoReset: List<Path> = emptyList(), block: () -> Unit) {
|
||||||
try {
|
try {
|
||||||
if (paths.all { it.toFile().setReadable(false) }) {
|
if (paths.all { it.toFile().setReadable(false) }) {
|
||||||
block()
|
block()
|
||||||
@@ -70,6 +70,20 @@ abstract class AbstractPathTest {
|
|||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
paths.forEach { it.toFile().setReadable(true) }
|
paths.forEach { it.toFile().setReadable(true) }
|
||||||
|
alsoReset.forEach { it.toFile().setReadable(true) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun withRestrictedWrite(vararg paths: Path, alsoReset: List<Path> = emptyList(), block: () -> Unit) {
|
||||||
|
try {
|
||||||
|
if (paths.all { it.toFile().setWritable(false) }) {
|
||||||
|
block()
|
||||||
|
} else {
|
||||||
|
System.err.println("Couldn't restrict write access")
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
paths.forEach { it.toFile().setWritable(true) }
|
||||||
|
alsoReset.forEach { it.toFile().setWritable(true) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,6 +147,57 @@ class PathExtensionsTest : AbstractPathTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun copyToRestrictedReadSource() {
|
||||||
|
val root = createTempDirectory("copyTo-root").cleanupRecursively()
|
||||||
|
|
||||||
|
// copy file
|
||||||
|
val srcFile = createTempFile(root, "srcFile")
|
||||||
|
val dstFile = root.resolve("dstFile")
|
||||||
|
|
||||||
|
withRestrictedRead(srcFile, alsoReset = listOf(dstFile)) {
|
||||||
|
assertFailsWith<AccessDeniedException> { srcFile.copyTo(dstFile) } // fails to copy restricted file
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy directory
|
||||||
|
val srcDirectory = createTempDirectory(root, "srcDirectory")
|
||||||
|
val dstDirectory = root.resolve("dstDirectory")
|
||||||
|
|
||||||
|
withRestrictedRead(srcDirectory, alsoReset = listOf(dstDirectory)) {
|
||||||
|
srcDirectory.copyTo(dstDirectory) // successfully copies restricted directory
|
||||||
|
assertFalse(dstDirectory.isReadable()) // copies access permissions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun copyToRestrictedWriteDestination() {
|
||||||
|
val root = createTempDirectory("copyTo-root").cleanupRecursively()
|
||||||
|
|
||||||
|
// copy file
|
||||||
|
val srcFile = createTempFile(root, "srcFile")
|
||||||
|
val dstFile = createTempFile(root, "dstFile")
|
||||||
|
|
||||||
|
withRestrictedWrite(dstFile) {
|
||||||
|
assertFailsWith<FileAlreadyExistsException> { srcFile.copyTo(dstFile) }
|
||||||
|
try {
|
||||||
|
srcFile.copyTo(dstFile, overwrite = true) // successfully overwrites restricted file in Unix
|
||||||
|
assertTrue(dstFile.isWritable()) // copies access permissions
|
||||||
|
} catch (_: AccessDeniedException) {
|
||||||
|
// Windows does not allow to overwrite readonly file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy directory
|
||||||
|
val srcDirectory = createTempDirectory(root, "srcDirectory")
|
||||||
|
val dstDirectory = createTempDirectory(root, "dstDirectory")
|
||||||
|
|
||||||
|
withRestrictedWrite(dstDirectory) {
|
||||||
|
assertFailsWith<FileAlreadyExistsException> { srcDirectory.copyTo(dstDirectory) }
|
||||||
|
srcDirectory.copyTo(dstDirectory, overwrite = true) // successfully overwrites restricted directory
|
||||||
|
assertTrue(dstDirectory.isWritable()) // copies access permissions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun copyToNameWithoutParent() {
|
fun copyToNameWithoutParent() {
|
||||||
val currentDir = Path("").absolute()
|
val currentDir = Path("").absolute()
|
||||||
|
|||||||
Reference in New Issue
Block a user