Skip to content

Commit

Permalink
[javasrc2cpg] add basic static field test (#5075)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho authored Nov 7, 2024
1 parent 7f0140c commit 6470dd7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
package io.joern.javasrc2cpg.querying.dataflow

import io.joern.javasrc2cpg.testfixtures.JavaDataflowFixture
import io.joern.javasrc2cpg.testfixtures.{JavaDataflowFixture, JavaSrcCode2CpgFixture}
import io.joern.dataflowengineoss.language.*
import io.shiftleft.semanticcpg.language.*

class NewObjectTests extends JavaSrcCode2CpgFixture(withOssDataflow = true) {

// FIXME: stopped working after https://github.com/joernio/joern/pull/5036
"static field passed as an argument inside a same-class static method whilst being referenced by its simple name" ignore {
val cpg = code("""
|class Bar {
| static String CONST = "<const>";
| static void run() {
| System.out.println(CONST);
| }
|}""".stripMargin)
val sink = cpg.call("println").argument(1)
val source = cpg.literal
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(
List(("String Bar.CONST = \"<const>\"", Some(3)), ("System.out.println(Bar.CONST)", Some(5)))
)
}

"static field passed as an argument inside a same-class static method whilst being referenced by its qualified name" in {
val cpg = code("""
|class Bar {
| static String CONST = "<const>";
| static void run() {
| System.out.println(Bar.CONST);
| }
|}""".stripMargin)
val sink = cpg.call("println").argument(1)
val source = cpg.literal
sink.reachableByFlows(source).map(flowToResultPairs).l shouldBe List(
List(("String Bar.CONST = \"<const>\"", Some(3)), ("System.out.println(Bar.CONST)", Some(5)))
)
}
}

class ObjectTests extends JavaDataflowFixture {

behavior of "Dataflow through objects"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.joern.javasrc2cpg.testfixtures

import io.joern.dataflowengineoss.DefaultSemantics
import io.joern.dataflowengineoss.language.Path
import io.joern.dataflowengineoss.semanticsloader.{FlowSemantic, Semantics}
import io.joern.dataflowengineoss.testfixtures.{SemanticCpgTestFixture, SemanticTestCpg}
import io.joern.javasrc2cpg.{Config, JavaSrc2Cpg}
Expand Down Expand Up @@ -81,4 +82,6 @@ class JavaSrcCode2CpgFixture(

(source, sink)
}

protected def flowToResultPairs(path: Path): List[(String, Option[Int])] = path.resultPairs()
}

0 comments on commit 6470dd7

Please sign in to comment.