Update FileAccess tests

This commit is contained in:
JeremyStar™ 2024-12-16 02:37:16 +01:00
parent ce7bd233b6
commit 560c01100f
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -20,13 +20,17 @@
package de.staropensource.engine.base.utility package de.staropensource.engine.base.utility
import de.staropensource.engine.base.utility.FileAccess.Type
import de.staropensource.engine.testing.TestBase import de.staropensource.engine.testing.TestBase
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.ValueSource
import java.io.File import java.io.File
import kotlin.test.assertFalse
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@Suppress("EmptyFunctionBlock") @Suppress("EmptyFunctionBlock")
class FileAccessTest : TestBase() { class FileAccessTest : TestBase() {
@ -40,14 +44,16 @@ class FileAccessTest : TestBase() {
"/some/test/file", "/some/test/file" "/some/test/file", "/some/test/file"
"\very\nice\test\file.txt", "/very/nice/test/file.txt" "\very\nice\test\file.txt", "/very/nice/test/file.txt"
"/./did/somebody\\/say\\yoga?", "/did/somebody/say/yoga?" "/./did/somebody\\/say\\yoga?", "/did/somebody/say/yoga?"
"test.txt", "+%test.txt"""" "test.txt", "+/test.txt""""
) )
fun toStringTest(supplyValue: String, compareValue: String) { fun toStringTest(supplyValue: String, compareValue: String) {
assertEquals( assertEquals(
compareValue compareValue
.replace("+", "${System.getProperty("user.dir")}") .replace("+", FileAccess.temporaryCacheDirectory!!.toString()),
.replace("%", File.separator), FileAccess
FileAccess(supplyValue).toString() .temporaryCacheDirectory!!
.traverse(supplyValue)
.toString()
) )
} }
@ -64,9 +70,12 @@ class FileAccessTest : TestBase() {
fun toStringRaw(supplyValue: String, compareValue: String) { fun toStringRaw(supplyValue: String, compareValue: String) {
assertEquals( assertEquals(
compareValue compareValue
.replace("+", "${System.getProperty("user.dir")}") .replace("+", FileAccess.temporaryCacheDirectory!!.toString())
.replace("%", File.separator), .replace("%", File.separator),
FileAccess(supplyValue).toStringRaw() FileAccess
.temporaryCacheDirectory!!
.traverse(supplyValue)
.toStringRaw()
) )
} }
@ -83,7 +92,10 @@ class FileAccessTest : TestBase() {
fun getBaseName(supplyValue: String, compareValue: String) { fun getBaseName(supplyValue: String, compareValue: String) {
assertEquals( assertEquals(
compareValue, compareValue,
FileAccess(supplyValue).getBaseName() FileAccess
.temporaryCacheDirectory!!
.traverse(supplyValue)
.getBaseName()
) )
} }
@ParameterizedTest @ParameterizedTest
@ -95,14 +107,17 @@ class FileAccessTest : TestBase() {
"\very\nice\test\file.txt", "/very/nice/test" "\very\nice\test\file.txt", "/very/nice/test"
"/./did/somebody\\/say\\yoga?", "/did/somebody/say" "/./did/somebody\\/say\\yoga?", "/did/somebody/say"
"test.txt", "+" "test.txt", "+"
"configs/default.conf", "+%configs"""" "configs/default.conf", "+/configs""""
) )
fun parent(supplyValue: String, compareValue: String) { fun parent(supplyValue: String, compareValue: String) {
assertEquals( assertEquals(
compareValue compareValue
.replace("+", "${System.getProperty("user.dir")}") .replace("+", FileAccess.temporaryCacheDirectory!!.toString()),
.replace("%", File.separator), FileAccess
FileAccess(supplyValue).parent().toString() .temporaryCacheDirectory!!
.traverse(supplyValue)
.parent()
.toString()
) )
} }
@ -111,96 +126,401 @@ class FileAccessTest : TestBase() {
// These tests are executed on FileAccess' default paths // These tests are executed on FileAccess' default paths
@Test @Test
fun getTemporaryCacheDirectory() { fun getTemporaryCacheDirectory() {
assertNotNull(FileAccess.temporaryCacheDirectory) assertEquals(
Type.DIRECTORY,
FileAccess
.temporaryCacheDirectory
?.getType()
)
} }
@Test @Test
fun getPersistentCacheDirectory() { fun getPersistentCacheDirectory() {
assertNotNull(FileAccess.persistentCacheDirectory) assertEquals(
Type.DIRECTORY,
FileAccess
.persistentCacheDirectory
?.getType()
)
} }
@Test @Test
fun getHomeDirectory() { fun getHomeDirectory() {
assertNotNull(FileAccess.homeDirectory) assertEquals(
Type.DIRECTORY,
FileAccess
.homeDirectory
?.getType()
)
} }
@Test @Test
fun getConfigDirectory() { fun getConfigDirectory() {
assertNotNull(FileAccess.configDirectory) assertEquals(
Type.DIRECTORY,
FileAccess
.configDirectory
?.getType()
)
} }
@Test @Test
fun getDataDirectory() { fun getDataDirectory() {
assertNotNull(FileAccess.dataDirectory) assertEquals(
Type.DIRECTORY,
FileAccess
.dataDirectory
?.getType()
)
} }
@Test @Test
fun exists() { fun existsVoid() {
assertFalse(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsVoid.exist")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.exists()
)
} }
@Test @Test
fun getType() { fun existsFile() {
assertTrue(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsFile.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createFile()
.exists()
)
} }
@Test @Test
fun isSymbolicLink() { fun existsDirectory() {
assertTrue(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsDirectory.d")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createDirectory()
.exists()
)
}
@ParameterizedTest
@ValueSource(booleans = [
true,
false
])
fun existsSymlinkReal(noFollowSymbolicLink: Boolean) {
assertTrue(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsSymlinkReal.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess.temporaryCacheDirectory!!
.traverse("existsSymlinkReal.target.txt")
.createFile()
.verifyIsFile(),
false
)
.exists(noFollowSymbolicLink = noFollowSymbolicLink)
)
}
@ParameterizedTest
@ValueSource(booleans = [
true,
false
])
fun existsSymlinkFake(noFollowSymbolicLink: Boolean) {
assertEquals(
noFollowSymbolicLink,
FileAccess
.temporaryCacheDirectory!!
.traverse("existsSymlinkFake.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsSymlinkFake.target.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true),
false
)
.exists(noFollowSymbolicLink = noFollowSymbolicLink)
)
}
@ParameterizedTest
@ValueSource(booleans = [
true,
false
])
fun existsHardlinkReal(noFollowSymbolicLink: Boolean) {
assertTrue(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsHardlinkReal.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess.temporaryCacheDirectory!!
.traverse("existsHardlinkReal.target.txt")
.createFile()
.verifyIsFile(),
true
)
.exists(noFollowSymbolicLink = noFollowSymbolicLink)
)
}
@ParameterizedTest
@ValueSource(booleans = [
true,
false
])
fun existsHardlinkFake(noFollowSymbolicLink: Boolean) {
val target: FileAccess = FileAccess
.temporaryCacheDirectory!!
.traverse("existsHardlinkFake.target.txt")
.createFile()
.verifyIsFile()
val link: FileAccess = FileAccess
.temporaryCacheDirectory!!
.traverse("existsHardlinkFake.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
target,
true
)
// Delete target
target.delete()
assertTrue(link.exists(noFollowSymbolicLink = noFollowSymbolicLink))
} }
@Test @Test
fun isHidden() { fun getTypeVoid() {
assertEquals(
Type.VOID,
FileAccess
.temporaryCacheDirectory!!
.traverse("getTypeVoid.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.getType()
)
} }
@Test @Test
fun isReadable() { fun getTypeFile() {
assertEquals(
Type.FILE,
FileAccess
.temporaryCacheDirectory!!
.traverse("getTypeFile.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createFile()
.verifyIsFile()
.getType()
)
} }
@Test @Test
fun isWritable() { fun getTypeDirectory() {
assertEquals(
Type.DIRECTORY,
FileAccess
.temporaryCacheDirectory!!
.traverse("getTypeDirectory.d")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createDirectory()
.verifyIsDirectory()
.getType()
)
} }
@Test @Test
fun isExecutable() { fun isSymbolicLinkReal() {
assertTrue(
FileAccess
.temporaryCacheDirectory!!
.traverse("isSymbolicLinkReal.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess.temporaryCacheDirectory!!
.traverse("existsSymlinkReal.target.txt")
.createFile()
.verifyIsFile(),
false
)
.isSymbolicLink()
)
} }
@Test @Test
fun getPosixPermissions() { fun isSymbolicLinkFake() {
assertTrue(
FileAccess
.temporaryCacheDirectory!!
.traverse("isSymbolicLinkFake.test.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess
.temporaryCacheDirectory!!
.traverse("existsSymlinkFake.target.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true),
false
)
.isSymbolicLink()
)
} }
@Test @ParameterizedTest
fun getLinkDestination() { @CsvSource(
delimiter = ',',
quoteCharacter = '"',
textBlock = """
"very\\\cool\\/directory/structure", "+%very/cool/directory/structure"
"some.spot", "+%some.spot""""
)
fun getLinkDestinationReal(supplyValue: String, compareValue: String) {
assertEquals(
compareValue
.replace("+", FileAccess.temporaryCacheDirectory!!.toString())
.replace("%", File.separator),
FileAccess
.temporaryCacheDirectory!!
.traverse("getLinkDestinationSymlinkReal.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess
.temporaryCacheDirectory!!
.traverse(supplyValue)
.createFile()
.verifyIsFile(),
false
)
.getLinkDestination()
.toString()
)
}
@ParameterizedTest
@CsvSource(
delimiter = ',',
quoteCharacter = '"',
textBlock = """
"getLinkDestinationSymlinkFake.d/very\\\cool\\/directory/structure", "+%getLinkDestinationSymlinkFake.d/very/cool/directory/structure"
"getLinkDestinationSymlinkFake.d/some.spot", "+%getLinkDestinationSymlinkFake.d/some.spot""""
)
fun getLinkDestinationFake(supplyValue: String, compareValue: String) {
assertEquals(
compareValue
.replace("+", FileAccess.temporaryCacheDirectory!!.toString())
.replace("%", File.separator),
FileAccess
.temporaryCacheDirectory!!
.traverse("getLinkDestinationSymlinkFake.link.txt")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createLink(
FileAccess
.temporaryCacheDirectory!!
.traverse(supplyValue)
.delete()
.verifyNotExists(noFollowSymbolicLink = true),
false
)
.getLinkDestination()
.toString()
)
} }
@Test @Test
fun getFileSystem() { fun getFileSystem() {
} assertNotNull(
FileAccess
@Test .temporaryCacheDirectory!!
fun isFilesystemPosixCompliant() { .traverse("justSomeTestFile")
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.getFileSystem()
)
} }
@Test @Test
fun getFilesystemRestrictedNames() { fun getFilesystemRestrictedNames() {
val file: FileAccess =
FileAccess
.temporaryCacheDirectory!!
.traverse("test/file")
assertTrue(
file
.getFilesystemRestrictedNames()
.toRegex()
.containsMatchIn(file.toString())
)
} }
@Test @ParameterizedTest
fun createFile() { @ValueSource(strings = [
"createFile.d/cool.test.file",
"createFile.d/something/in/directories.txt",
"createFile.d/hello.world"
])
fun createFile(path: String) {
assertEquals(
Type.FILE,
FileAccess
.temporaryCacheDirectory!!
.traverse(path)
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createFile()
.getType()
)
} }
@Test @ParameterizedTest
fun createDirectory() { @ValueSource(strings = [
"createDirectory.d/some/directory/stuff",
"createDirectory.d/directories/are/very/cool",
"createDirectory.d/very\\old/operating/systems\\only\\had/the/top/level\\and/at/most\\user/folders/without\\any\\/security/measures"
])
fun createDirectory(path: String) {
assertEquals(
Type.DIRECTORY,
FileAccess
.temporaryCacheDirectory!!
.traverse(path)
.delete()
.verifyNotExists(noFollowSymbolicLink = true)
.createDirectory()
.getType()
)
} }
@Test // The createLink method is pretty much
fun createLink() { // already covered by the exists* link tests
}
@Test @Test
fun move() { fun move() {