diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/DataFlowTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/DataFlowTests.scala index 16831a609aba..9c4042f46e64 100644 --- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/DataFlowTests.scala +++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/dataflow/DataFlowTests.scala @@ -1698,4 +1698,37 @@ class DataFlowTests extends DataFlowCodeToCpgSuite { } } + "Across the file data flow test" should { + val cpg = code( + """ + |def foo(arg) + | loop do + | arg += 1 + | if arg > 3 + | puts arg + | return + | end + | end + |end + |""".stripMargin, + "foo.rb" + ) + .moreCode( + """ + |x = 1 + |foo x + |""".stripMargin, + "bar.rb" + ) + + "be found in" in { + val source = cpg.literal.code("1").l + val sink = cpg.call.name("puts").argument(1).l + sink.reachableByFlows(source).size shouldBe 1 + + val src = cpg.identifier("x").l + sink.reachableByFlows(source).size shouldBe 1 + } + } + } diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/IdentifierLocalTests.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/IdentifierLocalTests.scala new file mode 100644 index 000000000000..269d3d2eef0a --- /dev/null +++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/IdentifierLocalTests.scala @@ -0,0 +1,87 @@ +package io.joern.rubysrc2cpg.passes.ast + +import io.joern.rubysrc2cpg.testfixtures.RubyCode2CpgFixture +import io.shiftleft.semanticcpg.language._ + +class IdentifierLocalTests extends RubyCode2CpgFixture { + + val cpg = code(""" + |def method1() + | x = 1 + | x = 2 + |end + | + |def method2(x) + | x = 2 + |end + | + |def method3(x) + | y = 0 + | + | if true + | innerx = 0 + | innery = 0 + | + | innerx = 1 + | innery = 1 + | end + | + | x = 1 + | y = 1 + |end + | + |""".stripMargin) + + // TODO: Need to be fixed. + "be correct for local x in method1" ignore { + val List(method) = cpg.method.nameExact("method1").l + method.block.ast.isIdentifier.l.size shouldBe 2 + val List(indentifierX, _) = method.block.ast.isIdentifier.l + indentifierX.name shouldBe "x" + + val localX = indentifierX._localViaRefOut.get + localX.name shouldBe "x" + } + + // TODO: Need to be fixed + "be correct for parameter x in method2" ignore { + val List(method) = cpg.method.nameExact("method2").l + val List(indentifierX) = method.block.ast.isIdentifier.l + indentifierX.name shouldBe "x" + + indentifierX.refsTo.l.size shouldBe 1 + val List(paramx) = indentifierX.refsTo.l + paramx.name shouldBe "x" + + val parameterX = indentifierX._methodParameterInViaRefOut.get + parameterX.name shouldBe "x" + } + + // TODO: Need to be fixed. + "Reach parameter from last identifer" ignore { + val List(method) = cpg.method.nameExact("method3").l + val List(outerIdentifierX) = method.ast.isIdentifier.lineNumber(22).l + val parameterX = outerIdentifierX._methodParameterInViaRefOut.get + parameterX.name shouldBe "x" + } + + // TODO: Need to be fixed. + "inner block test" ignore { + val List(method) = cpg.method.nameExact("method3").l + method.block.astChildren.isBlock.l.size shouldBe 1 + val List(nestedBlock) = method.block.astChildren.isBlock.l + nestedBlock.ast.isIdentifier.nameExact("innerx").l.size shouldBe 2 + } + + // TODO: Need to be fixed. + "nested block identifer to local taversal" ignore { + val List(method) = cpg.method.nameExact("method3").l + method.block.astChildren.isBlock.l.size shouldBe 1 + val List(nestedBlock) = method.block.astChildren.isBlock.l + nestedBlock.ast.isIdentifier.nameExact("innerx").l.size shouldBe 2 + val List(nestedIdentifierX, _) = nestedBlock.ast.isIdentifier.nameExact("innerx").l + + val nestedLocalX = nestedIdentifierX._localViaRefOut.get + nestedLocalX.name shouldBe "innerx" + } +} diff --git a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/TypeDeclAstCreationPassTest.scala b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/TypeDeclAstCreationPassTest.scala index a4f27e298085..a1d75641c574 100644 --- a/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/TypeDeclAstCreationPassTest.scala +++ b/joern-cli/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/passes/ast/TypeDeclAstCreationPassTest.scala @@ -18,6 +18,17 @@ class TypeDeclAstCreationPassTest extends RubyCode2CpgFixture { myClass.fullName shouldBe "Test0.rb::program:MyClass" } + // TODO: Need to be fixed. + "generate a basic type declaration node for an empty class with Class.new" ignore { + val cpg = code(""" + |MyClass = Class.new do + |end + |""".stripMargin) + val Some(myClass) = cpg.typeDecl.nameExact("MyClass").headOption: @unchecked + myClass.name shouldBe "MyClass" + myClass.fullName shouldBe "Test0.rb::program:MyClass" + } + "generate methods under type declarations" in { val cpg = code(""" |class Vehicle