From 1b9d7382761f3dbea3c931a9ad21ca8b55331b48 Mon Sep 17 00:00:00 2001 From: Ankit Kumar <118803988+ankit-privado@users.noreply.github.com> Date: Wed, 20 Sep 2023 18:57:12 +0530 Subject: [PATCH] [GoSrc2cpg] - ValueSpec and FuncDecl reference-id fix (#3679) Handled the ValueSpec and FuncDecl reference-id issue fixed Ticket: #3652 --- .../gosrc2cpg/astcreation/CacheBuilder.scala | 8 ++ .../joern/go2cpg/passes/ast/MethodTests.scala | 81 +++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/CacheBuilder.scala b/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/CacheBuilder.scala index e2866dd9554c..48dc5bee393e 100644 --- a/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/CacheBuilder.scala +++ b/joern-cli/frontends/gosrc2cpg/src/main/scala/io/joern/gosrc2cpg/astcreation/CacheBuilder.scala @@ -40,6 +40,14 @@ trait CacheBuilder { this: AstCreator => ) ) { processFuncDecl(obj) + createParserNodeInfo(obj) + } else if ( + json.obj + .contains(ParserKeys.NodeType) && obj(ParserKeys.NodeType).str == "ast.ValueSpec" && !json.obj.contains( + ParserKeys.NodeReferenceId + ) + ) { + createParserNodeInfo(obj) } obj.value.values.foreach(subJson => findAndProcess(subJson)) case arr: Arr => diff --git a/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/MethodTests.scala b/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/MethodTests.scala index 4a548ac76bba..e6294f9b7f85 100644 --- a/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/MethodTests.scala +++ b/joern-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/passes/ast/MethodTests.scala @@ -1563,6 +1563,87 @@ class MethodTests extends GoCodeToCpgSuite { } } + + "when constant is used in initializing struct" should { + val cpg = code(""" + |package main + | + |var person = Person() + | + |type Name struct { + | name string + |} + | + |const ( + | personName string = "peter" + |) + | + |func Person() Name { + | return Name{ + | name: personName, + | } + |} + |""".stripMargin) + + "test basic ast structure for Person" in { + val List(method) = cpg.method.name("Person").l + method.signature shouldBe "main.Person()main.Name" + + val List(typeDeclNode) = cpg.typeDecl.name("Name").l + typeDeclNode.fullName shouldBe "main.Name" + typeDeclNode.member.size shouldBe 1 + val List(name) = typeDeclNode.member.l + name.code shouldBe "name" + name.typeFullName shouldBe "string" + } + + "test basic ast structure for identifiers" in { + + val List(person, personName, _) = cpg.identifier.l + person.name shouldBe "person" + personName.name shouldBe "personName" + personName.typeFullName shouldBe "string" + } + } + + "when function is passed as argument" should { + val cpg = code(""" + |package main + | + |func help() { + | Person(Hello, "hi") + |} + | + |func Hello(value string) {} + | + |func Person(hello func(string), value string) { + | hello(value) + |} + |""".stripMargin) + + // TODO: Handle function as argument + + "test basic ast structure for help" in { + val List(methodNode) = cpg.method.name("help").l + methodNode.signature shouldBe "main.help()" + + val List(personCall) = cpg.call.name("Person").l + personCall.signature shouldBe "main.Person(ANY, string)" + + val List(arg1, arg2) = personCall.argument.l + arg1.code shouldBe "Hello" + arg2.code shouldBe "\"hi\"" + } + + "test basic ast structure for Person" in { + val List(methodNode) = cpg.method.name("Person").l + methodNode.signature shouldBe "main.Person(ANY, string)" + + val List(callNode) = methodNode.call.l + callNode.code shouldBe "hello(value)" + callNode.signature shouldBe "main.hello()" + } + } // TODO: add unit test for "sem chan int" // resultErrChan := make(chan error) // sem := make(chan int, concurrency)