Skip to content

Commit

Permalink
[pysrc2cpg] add dataflow test (#5147)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierpinho authored Nov 30, 2024
1 parent a6ce083 commit 9605b7e
Showing 1 changed file with 36 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,63 +170,6 @@ class DataFlowTests extends PySrc2CpgFixture(withOssDataflow = true) {
flows shouldBe empty
}

"no flow from aliased literal to method call return value given argument1-only semantics" ignore {
val cpg = code("""
|def foo(x):
| return x
|
|a = 20
|print(foo(a))
|""".stripMargin)
.withSemantics(DefaultSemantics().plus(List(FlowSemantic("Test0.py:<module>.foo", List(FlowMapping(1, 1))))))
val source = cpg.literal("20").l
val sink = cpg.call("print").argument(1).l
val flows = sink.reachableByFlows(source).l
flows shouldBe empty
}

"no flow from literal to method call return value given empty semantics" ignore {
val cpg = code("""
|def foo(x):
| return x
|
|print(foo(20))
|""".stripMargin)
.withSemantics(DefaultSemantics().plus(List(FlowSemantic("Test0.py:<module>.foo", List()))))
val source = cpg.literal("20").l
val sink = cpg.call("print").argument(1).l
val flows = sink.reachableByFlows(source).l
flows shouldBe empty
}

"no flow from literal to method call return value given receiver-only semantics" ignore {
val cpg = code("""
|def foo(x):
| return x
|
|print(foo(20))
|""".stripMargin)
.withSemantics(DefaultSemantics().plus(List(FlowSemantic("Test0.py:<module>.foo", List(FlowMapping(0, 0))))))
val source = cpg.literal("20").l
val sink = cpg.call("print").argument(1).l
val flows = sink.reachableByFlows(source).l
flows shouldBe empty
}

"no flow from literal to method call return value given argument1-only semantics" ignore {
val cpg = code("""
|def foo(x):
| return x
|
|print(foo(20))
|""".stripMargin)
.withSemantics(DefaultSemantics().plus(List(FlowSemantic("Test0.py:<module>.foo", List(FlowMapping(1, 1))))))
val source = cpg.literal("20").l
val sink = cpg.call("print").argument(1).l
val flows = sink.reachableByFlows(source).l
flows shouldBe empty
}

"don't taint the return value when specifying a named argument" in {
val cpg = code("""
|import foo
Expand Down Expand Up @@ -885,6 +828,42 @@ class DataFlowTests extends PySrc2CpgFixture(withOssDataflow = true) {

}

// Showcases that, even though `foo` is defined in the source-code, we are still able to override its semantics.
// Note that using `withSemantics` only updates the query-time semantics.
class InternalMethodCustomSemanticsDataFlowTest
extends PySrc2CpgFixture(
withOssDataflow = true,
semantics = DefaultSemantics().plus(List(FlowSemantic("Test0.py:<module>.foo", List(FlowMapping(1, 1)))))
) {

"no flow from literal to method call return value" in {
val cpg = code("""
|def foo(x):
| return x
|
|print(foo(20))
|""".stripMargin)
val source = cpg.literal("20")
val sink = cpg.call("print").argument(1)
val flows = sink.reachableByFlows(source)
flows shouldBe empty
}

"no flow from literal (in an assignment) to method call return value" in {
val cpg = code("""
|def foo(x):
| return x
|
|a = 20
|print(foo(a))
|""".stripMargin)
val source = cpg.literal("20")
val sink = cpg.call("print").argument(1)
val flows = sink.reachableByFlows(source)
flows shouldBe empty
}
}

class DefaultSemanticsDataFlowTest1 extends PySrc2CpgFixture(withOssDataflow = true, semantics = DefaultSemantics()) {

"DefaultSemantics cross-taints arguments to external method calls" in {
Expand Down

0 comments on commit 9605b7e

Please sign in to comment.