From e8d26b388796ab26ee7a5716fa25126953666aeb Mon Sep 17 00:00:00 2001 From: Robert Haimerl Date: Fri, 3 May 2024 16:09:26 +0200 Subject: [PATCH] Fix Windows Paths in Coko Tests (#853) Co-authored-by: Florian Wendland --- .../coko/dsl/CokoScriptHostTest.kt | 3 +- .../coko/dsl/ConceptTranslationTest.kt | 26 ++++------------- .../coko/dsl/ScriptAnalysisTest.kt | 9 ++++-- .../coko/dsl/TestUtils.kt | 29 +++++++++++++++++++ .../coko/dsl/WheneverEvaluatorTest.kt | 6 ++-- 5 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/TestUtils.kt diff --git a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoScriptHostTest.kt b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoScriptHostTest.kt index 72882a789..206ed9cbf 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoScriptHostTest.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoScriptHostTest.kt @@ -88,10 +88,11 @@ class CokoScriptHostTest { """.trimIndent() ) + val modelImport = modelDefinitionFile.toAbsoluteInvariant() assertDoesNotThrow { CokoExecutor.eval( """ - @file:Import("${modelDefinitionFile.toAbsolutePath()}") + @file:Import("$modelImport") class TestImpl: TestConcept { override fun log(message: String) { } diff --git a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ConceptTranslationTest.kt b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ConceptTranslationTest.kt index 13cecd6cd..8ea1d84e7 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ConceptTranslationTest.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ConceptTranslationTest.kt @@ -22,7 +22,7 @@ import de.fraunhofer.aisec.cpg.passes.EdgeCachePass import de.fraunhofer.aisec.cpg.passes.UnreachableEOGPass import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Test -import kotlin.io.path.Path +import kotlin.io.path.toPath import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -34,7 +34,7 @@ class ConceptTranslationTest { .getResource("IntegrationTests/CokoCpg/Main.java"), CokoCpgIntegrationTest::class.java.classLoader .getResource("IntegrationTests/CokoCpg/SimpleOrder.java") - ).map { Path(it.path) }.also { assertEquals(2, it.size) } + ).map { it.toURI().toPath() }.also { assertEquals(2, it.size) } val cpgConfiguration = CPGConfiguration( @@ -62,15 +62,7 @@ class ConceptTranslationTest { val specFiles = listOfNotNull( CokoCpgIntegrationTest::class.java.classLoader .getResource("concept/bsi-tr.concepts"), - ).map { Path(it.path) } - - val cokoConfiguration = - CokoConfiguration( - goodFindings = true, - pedantic = false, - spec = specFiles, - disabledSpecRules = emptyList(), - ) + ).map { it.toURI().toPath() } val backend = CokoCpgBackend(cpgConfiguration) val specEvaluator = CokoExecutor.compileScriptsIntoSpecEvaluator(backend, specFiles) @@ -101,15 +93,7 @@ class ConceptTranslationTest { val specFiles = listOfNotNull( CokoCpgIntegrationTest::class.java.classLoader .getResource("concept/some.concepts"), - ).map { Path(it.path) } - - val cokoConfiguration = - CokoConfiguration( - goodFindings = true, - pedantic = false, - spec = specFiles, - disabledSpecRules = emptyList(), - ) + ).map { it.toURI().toPath() } val backend = CokoCpgBackend(cpgConfiguration) val specEvaluator = CokoExecutor.compileScriptsIntoSpecEvaluator(backend, specFiles) @@ -148,7 +132,7 @@ class ConceptTranslationTest { .getResource("concept/followedByImplementations.codyze.kts"), CokoCpgIntegrationTest::class.java.classLoader .getResource("concept/followedByRule.codyze.kts"), - ).map { Path(it.path) } + ).map { it.toURI().toPath() } val cokoConfiguration = CokoConfiguration( diff --git a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ScriptAnalysisTest.kt b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ScriptAnalysisTest.kt index 06bcad0a5..73d872fe9 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ScriptAnalysisTest.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/ScriptAnalysisTest.kt @@ -96,10 +96,11 @@ class ScriptAnalysisTest { """.trimIndent() ) + val modelImport = modelDefinitionFile.toAbsoluteInvariant() val result = CokoExecutor.eval( """ - @file:Import("${modelDefinitionFile.toAbsolutePath()}") + @file:Import("$modelImport") class TestImpl: TestConcept { override fun log(message: String) { } @@ -123,9 +124,10 @@ class ScriptAnalysisTest { ) val implementationFile = tempDir.resolve("implementation.codyze.kts") + val modelImport = modelDefinitionFile.toAbsoluteInvariant() implementationFile.writeText( """ - @file:Import("${modelDefinitionFile.toAbsolutePath()}") + @file:Import("$modelImport") class TestImpl: TestConcept { override fun log(message: String) { } @@ -177,10 +179,11 @@ class ScriptAnalysisTest { """.trimIndent() ) + val modelImport = modelDefinitionFile.toAbsoluteInvariant() val implementationFile = tempDir.resolve("implementation.codyze.kts") implementationFile.writeText( """ - @file:Import("${modelDefinitionFile.toAbsolutePath()}") + @file:Import("$modelImport") class TestImpl: TestConcept { override fun log(message: String) = op { diff --git a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/TestUtils.kt b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/TestUtils.kt new file mode 100644 index 000000000..925d9ca32 --- /dev/null +++ b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/TestUtils.kt @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024, Fraunhofer AISEC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.fraunhofer.aisec.codyze.specificationLanguages.coko.dsl + +import java.nio.file.Path +import kotlin.io.path.invariantSeparatorsPathString + +/** + * Forces any path into an absolute path String that is system invariant. + * + * This is especially important when testing with Windows, as paths with backward slashes are not allowed + * as Coko imports. + */ +fun Path.toAbsoluteInvariant(): String { + return this.toAbsolutePath().invariantSeparatorsPathString +} diff --git a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/WheneverEvaluatorTest.kt b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/WheneverEvaluatorTest.kt index 5b5255a56..2fee635a4 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/WheneverEvaluatorTest.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/test/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/WheneverEvaluatorTest.kt @@ -22,7 +22,7 @@ import de.fraunhofer.aisec.cpg.passes.EdgeCachePass import de.fraunhofer.aisec.cpg.passes.UnreachableEOGPass import io.github.detekt.sarif4k.ResultKind import org.junit.jupiter.api.Test -import kotlin.io.path.Path +import kotlin.io.path.toPath import kotlin.test.assertEquals // TODO: should probably in codyze-backends or coko-core @@ -31,7 +31,7 @@ class WheneverEvaluatorTest { private val sourceFiles = listOfNotNull( CokoCpgIntegrationTest::class.java.classLoader .getResource("concept/CipherTestFile.java"), - ).map { Path(it.path) }.also { assertEquals(1, it.size) } + ).map { it.toURI().toPath() }.also { assertEquals(1, it.size) } val cpgConfiguration = CPGConfiguration( @@ -64,7 +64,7 @@ class WheneverEvaluatorTest { .getResource("concept/bsi-tr-rules.codyze.kts"), CokoCpgIntegrationTest::class.java.classLoader .getResource("concept/jca-cipher.codyze.kts") - ).map { Path(it.path) }.also { assertEquals(2, it.size) } + ).map { it.toURI().toPath() }.also { assertEquals(2, it.size) } val cokoConfiguration = CokoConfiguration(