From 28bc31e9779d3846e8f40dea93524c5af6b31e68 Mon Sep 17 00:00:00 2001 From: Alexander Kuechler Date: Thu, 14 Mar 2024 16:38:25 +0100 Subject: [PATCH] Try more tests, problems with ContextProvider --- .../enhancements/DFGFunctionSummariesTest.kt | 45 +++++++++++++++++++ cpg-core/src/test/resources/function-dfg2.yml | 36 +++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 cpg-core/src/test/resources/function-dfg2.yml diff --git a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/DFGFunctionSummariesTest.kt b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/DFGFunctionSummariesTest.kt index b8efe1f1b6..db16e05afc 100644 --- a/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/DFGFunctionSummariesTest.kt +++ b/cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/DFGFunctionSummariesTest.kt @@ -63,6 +63,51 @@ class DFGFunctionSummariesTest { assertEquals(jsonSummaries.functionToDFGEntryMap, yamlSummaries.functionToDFGEntryMap) } + @Test + fun testMatching() { + val code = + GraphExamples.testFrontend( + TranslationConfiguration.builder() + .defaultPasses() + .registerLanguage(TestLanguage(".")) + .registerFunctionSummary(File("src/test/resources/function-dfg2.yml")) + .inferenceConfiguration( + InferenceConfiguration.builder() + .inferDfgForUnresolvedCalls(true) + .inferFunctions(true) + .build() + ) + .build() + ) + .build { + translationResult { + translationUnit("DfgInferredCall.c") { + function("main", t("int")) { + body { + declare { + variable("a", t("test.List")) { construct("test.List") } + } + memberCall("addAll", ref("a", t("test.List"))) { + literal(1, t("int")) + construct("test.Object") + } + + returnStmt { ref("a") } + } + } + } + } + } + + val methodAddAllTwoArgs = code.methods["addAll"] + assertNotNull(methodAddAllTwoArgs) + assertEquals(2, methodAddAllTwoArgs.parameters.size) + assertEquals( + setOf(methodAddAllTwoArgs.receiver!!), + methodAddAllTwoArgs.parameters[1].nextDFG + ) + } + @Test fun testPropagateArguments() { val dfgTest = getDfgInferredCall() diff --git a/cpg-core/src/test/resources/function-dfg2.yml b/cpg-core/src/test/resources/function-dfg2.yml new file mode 100644 index 0000000000..1ba2d35878 --- /dev/null +++ b/cpg-core/src/test/resources/function-dfg2.yml @@ -0,0 +1,36 @@ +- functionDeclaration: + language: de.fraunhofer.aisec.cpg.frontends.TestLanguage + methodName: test.List.addAll + signature: + - int + - test.Object + dataFlows: + - from: param1 + to: base + dfgType: full + +- functionDeclaration: + language: de.fraunhofer.aisec.cpg.frontends.TestLanguage + methodName: test.List.addAll + signature: + - test.Object + dataFlows: + - from: param0 + to: base + dfgType: full + +- functionDeclaration: + language: de.fraunhofer.aisec.cpg.frontends.TestLanguage + methodName: test.List.add + dataFlows: + - from: param0 + to: base + dfgType: full + +- functionDeclaration: + language: de.fraunhofer.aisec.cpg.frontends.TestLanguage + methodName: memcpy + dataFlows: + - from: param1 + to: param0 + dfgType: full \ No newline at end of file