Skip to content

Commit

Permalink
[gosrc2cpg] AST Creation and Dataflow tests segregation (#3513)
Browse files Browse the repository at this point in the history
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
karan-batavia authored Aug 16, 2023
1 parent 4ba43ae commit d61e71c
Show file tree
Hide file tree
Showing 11 changed files with 918 additions and 752 deletions.
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
}
}
}

This file was deleted.

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

}
}
}
Loading

0 comments on commit d61e71c

Please sign in to comment.