From a0418aeeb767d3bb64407c5f40ea5030aa2158e8 Mon Sep 17 00:00:00 2001 From: Robert Haimerl Date: Wed, 3 Jul 2024 10:02:52 +0200 Subject: [PATCH 1/6] handle windows path with spaces for VersionProviderTest --- .../de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt index 5af5d0bf0..8cb9738e8 100644 --- a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt +++ b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt @@ -16,6 +16,7 @@ package de.fraunhofer.aisec.codyze.core import org.junit.jupiter.api.Test +import java.io.File import java.io.FileOutputStream import java.util.Properties import kotlin.test.assertEquals @@ -34,7 +35,7 @@ class VersionProviderTest { // change property s.t. internal check fails val oldValue = properties.setProperty("project.name", "test") as String - FileOutputStream(propFile.file).use { + FileOutputStream(File(propFile.toURI())).use { properties.store(it, null) } @@ -49,7 +50,7 @@ class VersionProviderTest { // restore original properties file properties.setProperty("project.name", oldValue) - FileOutputStream(propFile.file).use { + FileOutputStream(File(propFile.toURI())).use { properties.store(it, null) } } From 06490067b68280d106389d7d1a3e7b86dbc8473d Mon Sep 17 00:00:00 2001 From: Robert Haimerl Date: Wed, 3 Jul 2024 10:03:21 +0200 Subject: [PATCH 2/6] correctly handle absolute Windows paths in PluginTests --- .../plugins/compiled/CompiledPluginTest.kt | 17 +++++++++-------- .../codyze/plugins/source/SourcePluginTest.kt | 7 ++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt index 80c92c34e..e3e9b021f 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt @@ -17,23 +17,24 @@ package de.fraunhofer.aisec.codyze.plugins.compiled import de.fraunhofer.aisec.codyze.plugins.PluginTest import java.nio.file.Path +import kotlin.io.path.toPath import kotlin.test.assertNotNull abstract class CompiledPluginTest : PluginTest() { override fun scanFiles() { - val libPath = PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")?.path + val libPath = PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")!!.toURI().toPath() val contextPaths = listOf( - PluginTest::class.java.classLoader.getResource("targets/libs/bcpkix-jdk18on-1.75.jar")?.path, - PluginTest::class.java.classLoader.getResource("targets/libs/bcprov-jdk18on-1.75.jar")?.path, - PluginTest::class.java.classLoader.getResource("targets/libs/bctls-jdk18on-1.75.jar")?.path, - PluginTest::class.java.classLoader.getResource("targets/libs/bcutil-jdk18on-1.75.jar")?.path + PluginTest::class.java.classLoader.getResource("targets/libs/bcpkix-jdk18on-1.75.jar")!!.toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bcprov-jdk18on-1.75.jar")!!.toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bctls-jdk18on-1.75.jar")!!.toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bcutil-jdk18on-1.75.jar")!!.toURI().toPath() ) assertNotNull(libPath) plugin.execute( - listOf(Path.of(libPath)), - contextPaths.map { Path.of(it!!) }, - Path.of(libPath).parent.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() + listOf(libPath), + contextPaths, + libPath.parent.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() ) } } diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt index 1e0d7a479..3858b28f7 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt @@ -17,17 +17,18 @@ package de.fraunhofer.aisec.codyze.plugins.source import de.fraunhofer.aisec.codyze.plugins.PluginTest import java.nio.file.Path +import kotlin.io.path.toPath import kotlin.test.assertNotNull abstract class SourcePluginTest : PluginTest() { override fun scanFiles() { - val sourcePath = PluginTest::class.java.classLoader.getResource("targets/TlsServer.java")?.path + val sourcePath = PluginTest::class.java.classLoader.getResource("targets/TlsServer.java")!!.toURI().toPath() assertNotNull(sourcePath) plugin.execute( - listOf(Path.of(sourcePath)), + listOf(sourcePath), listOf(), - Path.of(sourcePath).parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() + sourcePath.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() ) } } From b4efb6b583b921b80e8473fda96c122ce55311ae Mon Sep 17 00:00:00 2001 From: Robert Haimerl Date: Wed, 3 Jul 2024 10:05:05 +0200 Subject: [PATCH 3/6] fix various style issues --- .../plugins/compiled/CompiledPluginTest.kt | 17 +++++++++++------ .../codyze/plugins/source/SourcePluginTest.kt | 1 - 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt index e3e9b021f..b6fbee20a 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt @@ -16,18 +16,23 @@ package de.fraunhofer.aisec.codyze.plugins.compiled import de.fraunhofer.aisec.codyze.plugins.PluginTest -import java.nio.file.Path import kotlin.io.path.toPath import kotlin.test.assertNotNull abstract class CompiledPluginTest : PluginTest() { override fun scanFiles() { - val libPath = PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")!!.toURI().toPath() + val libPath = + PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")!! + .toURI().toPath() val contextPaths = listOf( - PluginTest::class.java.classLoader.getResource("targets/libs/bcpkix-jdk18on-1.75.jar")!!.toURI().toPath(), - PluginTest::class.java.classLoader.getResource("targets/libs/bcprov-jdk18on-1.75.jar")!!.toURI().toPath(), - PluginTest::class.java.classLoader.getResource("targets/libs/bctls-jdk18on-1.75.jar")!!.toURI().toPath(), - PluginTest::class.java.classLoader.getResource("targets/libs/bcutil-jdk18on-1.75.jar")!!.toURI().toPath() + PluginTest::class.java.classLoader.getResource("targets/libs/bcpkix-jdk18on-1.75.jar")!! + .toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bcprov-jdk18on-1.75.jar")!! + .toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bctls-jdk18on-1.75.jar")!! + .toURI().toPath(), + PluginTest::class.java.classLoader.getResource("targets/libs/bcutil-jdk18on-1.75.jar")!! + .toURI().toPath() ) assertNotNull(libPath) diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt index 3858b28f7..9314dbe66 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt @@ -16,7 +16,6 @@ package de.fraunhofer.aisec.codyze.plugins.source import de.fraunhofer.aisec.codyze.plugins.PluginTest -import java.nio.file.Path import kotlin.io.path.toPath import kotlin.test.assertNotNull From 75d4849f569a6fa13a396635f196965f19aa9277 Mon Sep 17 00:00:00 2001 From: "Wendland, Florian" Date: Thu, 18 Jul 2024 10:55:56 +0200 Subject: [PATCH 4/6] Use Kotlin fluid File extensions Highlights the conversion of resources to URIs and then to files before getting an output stream. --- .../de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt index 8cb9738e8..af0d0cdfc 100644 --- a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt +++ b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt @@ -35,7 +35,7 @@ class VersionProviderTest { // change property s.t. internal check fails val oldValue = properties.setProperty("project.name", "test") as String - FileOutputStream(File(propFile.toURI())).use { + File(propFile.toURI()).outputStream().use { properties.store(it, null) } @@ -50,7 +50,7 @@ class VersionProviderTest { // restore original properties file properties.setProperty("project.name", oldValue) - FileOutputStream(File(propFile.toURI())).use { + File(propFile.toURI()).outputStream().use { properties.store(it, null) } } From 60d8e74e4933cbd85ba5542e3de7dc0b4d1288de Mon Sep 17 00:00:00 2001 From: Robert Haimerl Date: Mon, 22 Jul 2024 09:13:55 +0200 Subject: [PATCH 5/6] remove now unused import --- .../de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt index af0d0cdfc..87e423576 100644 --- a/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt +++ b/codyze-core/src/test/kotlin/de/fraunhofer/aisec/codyze/core/VersionProviderTest.kt @@ -17,7 +17,6 @@ package de.fraunhofer.aisec.codyze.core import org.junit.jupiter.api.Test import java.io.File -import java.io.FileOutputStream import java.util.Properties import kotlin.test.assertEquals From 06366b174738b84a58ac218d7f98289ec763f7dc Mon Sep 17 00:00:00 2001 From: "Wendland, Florian" Date: Tue, 17 Sep 2024 10:24:08 +0200 Subject: [PATCH 6/6] Simplify resource path determination --- .../aisec/codyze/plugins/PluginTest.kt | 25 ++++++++++++------- .../plugins/compiled/CompiledPluginTest.kt | 4 ++- .../codyze/plugins/source/SourcePluginTest.kt | 3 ++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt index 3270bc2eb..4813b33a8 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/PluginTest.kt @@ -21,6 +21,8 @@ import io.github.detekt.sarif4k.Result import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import java.io.File +import kotlin.io.path.deleteExisting +import kotlin.io.path.toPath import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -31,12 +33,19 @@ abstract class PluginTest { open val expectedSuccess: Boolean = true abstract val expectedResults: List + val reportUri by lazy { + // use an existing anchor in the resources directory + PluginTest::class.java.classLoader.getResource("targets")!!.toURI() + .resolve("..") + .resolve("generatedReports") + .resolve(resultFileName) + } + @Test fun testResults() { scanFiles() - val resultURI = PluginTest::class.java.classLoader.getResource("generatedReports/$resultFileName")?.toURI() - assertNotNull(resultURI) - val run = extractLastRun(File(resultURI)) + + val run = extractLastRun(File(reportUri)) assertNotNull(run) var results = run.results @@ -58,9 +67,8 @@ abstract class PluginTest { @Test fun testInvocation() { scanFiles() - val resultURI = PluginTest::class.java.classLoader.getResource("generatedReports/$resultFileName")?.toURI() - assertNotNull(resultURI) - val run = extractLastRun(File(resultURI)) + + val run = extractLastRun(File(reportUri)) assertNotNull(run) if (!run.invocations.isNullOrEmpty()) { @@ -72,9 +80,8 @@ abstract class PluginTest { @AfterEach fun cleanup() { - val resultURI = PluginTest::class.java.classLoader.getResource("generatedReports/$resultFileName")?.toURI() - if (resultURI != null) { - File(resultURI).delete() + reportUri.runCatching { + this.toPath().deleteExisting() } } diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt index b6fbee20a..6b1979360 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/compiled/CompiledPluginTest.kt @@ -16,11 +16,13 @@ package de.fraunhofer.aisec.codyze.plugins.compiled import de.fraunhofer.aisec.codyze.plugins.PluginTest +import java.io.File import kotlin.io.path.toPath import kotlin.test.assertNotNull abstract class CompiledPluginTest : PluginTest() { override fun scanFiles() { + // certain that resources are available val libPath = PluginTest::class.java.classLoader.getResource("targets/libs/demo-cloud-service-1.0.0.jar")!! .toURI().toPath() @@ -39,7 +41,7 @@ abstract class CompiledPluginTest : PluginTest() { plugin.execute( listOf(libPath), contextPaths, - libPath.parent.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() + File(reportUri) ) } } diff --git a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt index 9314dbe66..bcdbadf32 100644 --- a/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt +++ b/codyze-plugins/src/test/kotlin/de/fraunhofer/aisec/codyze/plugins/source/SourcePluginTest.kt @@ -16,6 +16,7 @@ package de.fraunhofer.aisec.codyze.plugins.source import de.fraunhofer.aisec.codyze.plugins.PluginTest +import java.io.File import kotlin.io.path.toPath import kotlin.test.assertNotNull @@ -27,7 +28,7 @@ abstract class SourcePluginTest : PluginTest() { plugin.execute( listOf(sourcePath), listOf(), - sourcePath.parent.parent.resolve("generatedReports").resolve(resultFileName).toFile() + File(reportUri) ) } }