-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rubysrc2cpg] dataflow tests & start measuring the gap against the pr…
…evious frontend (#3798)
- Loading branch information
1 parent
3d67ca3
commit 4d90eef
Showing
15 changed files
with
288 additions
and
38 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/ConditionalTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.joern.rubysrc2cpg.dataflow | ||
|
||
import io.joern.dataflowengineoss.language.* | ||
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class ConditionalTests extends RubyCode2CpgFixture(withPostProcessing = true, withDataFlow = true) { | ||
|
||
"flow through both branches of a ternary `.. ? .. : ..` operator" in { | ||
val cpg = code(""" | ||
|x = 1 | ||
|y = 2 | ||
|z = foo ? x : y | ||
|puts z | ||
|""".stripMargin) | ||
val source = cpg.literal | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set( | ||
List(("x = 1", 2), ("x", 2), ("foo ? x : y", 4), ("z", 4), ("puts z", 5)), | ||
List(("y = 2", 3), ("y", 3), ("foo ? x : y", 4), ("z", 4), ("puts z", 5)) | ||
) | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...ends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/ControlStructureTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.joern.rubysrc2cpg.dataflow | ||
|
||
import io.joern.dataflowengineoss.language.* | ||
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class ControlStructureTests extends RubyCode2CpgFixture(withPostProcessing = true, withDataFlow = true) { | ||
|
||
"flow through body of a `while-end` statement" in { | ||
val cpg = code(""" | ||
|x = 10 | ||
|while x > 0 do | ||
| x = x - 1 | ||
|end | ||
|puts x | ||
|""".stripMargin) | ||
val source = cpg.literal("10") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("x = 10", 2), ("x", 2), ("x - 1", 4), ("x = x - 1", 4), ("x > 0", 3), ("puts x", 6))) | ||
} | ||
|
||
"flow through body of an `until-end` statement" in { | ||
val cpg = code(""" | ||
|x = 10 | ||
|until x <= 0 do | ||
| x = x - 1 | ||
|end | ||
|puts x | ||
|""".stripMargin) | ||
val source = cpg.literal("10") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("x = 10", 2), ("x", 2), ("x - 1", 4), ("x = x - 1", 4), ("x <= 0", 3), ("puts x", 6))) | ||
} | ||
|
||
"flow through the 1st branch of an `if-end` statement" in { | ||
val cpg = code(""" | ||
|t = 100 | ||
|if true | ||
| t = t + 1 | ||
|end | ||
|puts t | ||
|""".stripMargin) | ||
val source = cpg.literal("100") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("t = 100", 2), ("t", 2), ("t + 1", 4), ("t = t + 1", 4), ("puts t", 6))) | ||
} | ||
|
||
"flow through the 2nd branch of an `if-else-end` statement" in { | ||
val cpg = code(""" | ||
|t = 100 | ||
|if false | ||
| foo | ||
|else | ||
| t = t - 1 | ||
|end | ||
|puts t | ||
|""".stripMargin) | ||
val source = cpg.literal("100") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("t = 100", 2), ("t", 2), ("t - 1", 6), ("t = t - 1", 6), ("puts t", 8))) | ||
} | ||
|
||
"flow through the 2nd branch of an `if-elsif-end` statement" in { | ||
val cpg = code(""" | ||
|t = 100 | ||
|if false | ||
| foo | ||
|elsif true | ||
| t = t * 2 | ||
|end | ||
|puts t | ||
|""".stripMargin) | ||
val source = cpg.literal("100") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("t = 100", 2), ("t", 2), ("t * 2", 6), ("t = t * 2", 6), ("puts t", 8))) | ||
} | ||
|
||
"flow through both branches of an `if-else-end` statement" in { | ||
val cpg = code(""" | ||
|t = 100 | ||
|if false | ||
| puts t + 1 | ||
|else | ||
| puts t + 2 | ||
|end | ||
|""".stripMargin) | ||
val source = cpg.literal("100") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("t = 100", 2), ("t", 2), ("t + 2", 6)), List(("t = 100", 2), ("t", 2), ("t + 1", 4))) | ||
} | ||
|
||
"flow through an `unless-end` statement" in { | ||
val cpg = code(""" | ||
|x = 1 | ||
|unless __LINE__ == 0 then | ||
| x = x * 2 | ||
|end | ||
|puts x | ||
|""".stripMargin) | ||
val source = cpg.literal("1") | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set(List(("x = 1", 2), ("x", 2), ("x * 2", 4), ("x = x * 2", 4), ("puts x", 6))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/SingleAssignmentTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.joern.rubysrc2cpg.dataflow | ||
|
||
import io.joern.dataflowengineoss.language.* | ||
import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class SingleAssignmentTests extends RubyCode2CpgFixture(withPostProcessing = true, withDataFlow = true) { | ||
|
||
"flow through two inline assignments `z = x = y = 1`" in { | ||
val cpg = code(""" | ||
|z = x = y = 1 | ||
|puts y | ||
|puts x | ||
|puts z | ||
|""".stripMargin) | ||
val source = cpg.literal | ||
val sink = cpg.method.name("puts").callIn.argument | ||
val flows = sink.reachableByFlows(source) | ||
flows.map(flowToResultPairs).toSet shouldBe | ||
Set( | ||
List(("y = 1", 2), ("y", 2), ("y = 1", 2), ("x", 2), ("x = y = 1", 2), ("z", 2), ("puts z", 5)), | ||
List(("y = 1", 2), ("y", 2), ("y = 1", 2), ("x", 2), ("puts x", 4)), | ||
List(("y = 1", 2), ("y", 2), ("puts y", 3)) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
...src2cpg/src/test/scala/io/joern/rubysrc2cpg/deprecated/passes/ast/AttributeCpgTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.