-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gosrc2cpg] AST Creation and Dataflow tests segregation (#3513)
The `ASTCreationsPassTests.scala` file contained all kinds of tests, that were difficult to track. This PR segregates contents of `ASTCreationsPassTests.scala` into concise test-case files.
- Loading branch information
1 parent
4ba43ae
commit d61e71c
Showing
11 changed files
with
918 additions
and
752 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...ontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/dataflow/ConditionalsDataflowTests.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,94 @@ | ||
package io.joern.go2cpg.dataflow | ||
|
||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
import io.shiftleft.semanticcpg.language._ | ||
import io.joern.dataflowengineoss.language._ | ||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
|
||
class ConditionalsDataflowTests extends GoCodeToCpgSuite(withOssDataflow = true) { | ||
"Source to sink dataflow through if blocks" should { | ||
|
||
"be reachable (case 1)" in { | ||
val cpg = code(""" | ||
|package main | ||
|func main(){ | ||
| var a int = 4 | ||
| var b int | ||
| if (a > 6) { | ||
| c := a | ||
| b = c | ||
| } | ||
|} | ||
| | ||
|""".stripMargin) | ||
val source = cpg.identifier("a").lineNumber(4) | ||
val sink = cpg.identifier("b").lineNumber(8) | ||
sink.reachableByFlows(source).size shouldBe 1 | ||
|
||
} | ||
|
||
"be reachable (case 2)" in { | ||
val cpg = code(""" | ||
|package main | ||
|func main(){ | ||
| var a int = 4 | ||
| var b int | ||
| if (a > 6) { | ||
| b = a | ||
| }else { | ||
| c := a | ||
| b = c | ||
| } | ||
|} | ||
| | ||
|""".stripMargin) | ||
val source = cpg.identifier("a").lineNumber(4) | ||
val sink = cpg.identifier("b").lineNumber(10) | ||
sink.reachableByFlows(source).size shouldBe 1 | ||
|
||
} | ||
|
||
"be reachable (case 3)" in { | ||
val cpg = code(""" | ||
|package main | ||
|func main(){ | ||
| var a int = 4 | ||
| var b int | ||
| if (a > 6) { | ||
| b = a | ||
| }else if (a < 4) { | ||
| c := a | ||
| b = c | ||
| }else { | ||
| b = a | ||
| } | ||
|} | ||
| | ||
|""".stripMargin) | ||
val source = cpg.identifier("a").lineNumber(4) | ||
val sink = cpg.identifier("b").lineNumber(10) | ||
sink.reachableByFlows(source).size shouldBe 1 | ||
|
||
} | ||
|
||
"be reachable (case 4)" in { | ||
val cpg = code(""" | ||
|package main | ||
|func main(){ | ||
| var a int = 4 | ||
| var b int | ||
| if (a > 6) { | ||
| if (a < 10) { | ||
| c := a | ||
| b = c | ||
| } | ||
| } | ||
|} | ||
| | ||
|""".stripMargin) | ||
val source = cpg.identifier("a").lineNumber(4) | ||
val sink = cpg.identifier("b").lineNumber(9) | ||
sink.reachableByFlows(source).size shouldBe 1 | ||
} | ||
} | ||
} |
221 changes: 0 additions & 221 deletions
221
joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/dataflow/DataflowTests.scala
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
...-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/dataflow/LoopsDataflowTests.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,56 @@ | ||
package io.joern.go2cpg.dataflow | ||
|
||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
import io.shiftleft.semanticcpg.language._ | ||
import io.joern.dataflowengineoss.language._ | ||
import io.joern.go2cpg.testfixtures.GoCodeToCpgSuite | ||
|
||
class LoopsDataflowTests extends GoCodeToCpgSuite(withOssDataflow = true) { | ||
"Source and sink dataflow for for loop" should { | ||
"be reachable when initialization, condition and post step are present" in { | ||
val cpg = code(""" | ||
|package main | ||
|func main() { | ||
| var b int = 10 | ||
| | ||
| /* for loop execution */ | ||
| for a := 0; a < 10; a++ { | ||
| b += a | ||
| } | ||
| | ||
| z := b | ||
|} | ||
""".stripMargin) | ||
val source1 = cpg.identifier("b").lineNumber(4).l | ||
val sink1 = cpg.identifier("z").l | ||
sink1.reachableByFlows(source1).size shouldBe 1 | ||
|
||
val source2 = cpg.identifier("b").lineNumber(8).l | ||
val sink2 = cpg.identifier("z").l | ||
sink2.reachableByFlows(source2).size shouldBe 1 | ||
} | ||
|
||
// TODO Looks like some issue due to `range` being a unaryExpr here | ||
"be reachable for range statement" ignore { | ||
val cpg = code("""package main | ||
|func main() { | ||
| var message string = "Hello, Gophers!" | ||
| | ||
| counter := 0 | ||
| for index, char := range message { | ||
| counter += index | ||
| } | ||
| indexSum := counter | ||
|} | ||
|""".stripMargin) | ||
val source1 = cpg.identifier("counter").lineNumber(5).l | ||
val sink1 = cpg.identifier("z").l | ||
sink1.reachableByFlows(source1).size shouldBe 1 | ||
|
||
val source2 = cpg.identifier("counter").lineNumber(7).l | ||
val sink2 = cpg.identifier("indexSum").l | ||
sink2.reachableByFlows(source2).size shouldBe 1 | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.