-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontends] Added server mode to more frontends (#4993)
- Loading branch information
1 parent
488fd7b
commit 8dbccfb
Showing
63 changed files
with
812 additions
and
105 deletions.
There are no files selected for viewing
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
78 changes: 78 additions & 0 deletions
78
...ds/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/io/CSharp2CpgHTTPServerTests.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,78 @@ | ||
package io.joern.csharpsrc2cpg.io | ||
|
||
import better.files.File | ||
import io.joern.csharpsrc2cpg.testfixtures.CSharpCode2CpgFixture | ||
import io.joern.x2cpg.utils.server.FrontendHTTPClient | ||
import io.shiftleft.codepropertygraph.cpgloading.CpgLoader | ||
import io.shiftleft.semanticcpg.language.* | ||
import org.scalatest.BeforeAndAfterAll | ||
|
||
import scala.collection.parallel.CollectionConverters.RangeIsParallelizable | ||
import scala.util.Failure | ||
import scala.util.Success | ||
|
||
class CSharp2CpgHTTPServerTests extends CSharpCode2CpgFixture with BeforeAndAfterAll { | ||
|
||
private var port: Int = -1 | ||
|
||
private def newProjectUnderTest(index: Option[Int] = None): File = { | ||
val dir = File.newTemporaryDirectory("csharp2cpgTestsHttpTest") | ||
val file = dir / "main.cs" | ||
file.createIfNotExists(createParents = true) | ||
val indexStr = index.map(_.toString).getOrElse("") | ||
file.writeText(basicBoilerplate(s"Console.WriteLine($indexStr);")) | ||
file.deleteOnExit() | ||
dir.deleteOnExit() | ||
} | ||
|
||
override def beforeAll(): Unit = { | ||
// Start server | ||
port = io.joern.csharpsrc2cpg.Main.startup() | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
// Stop server | ||
io.joern.csharpsrc2cpg.Main.stop() | ||
} | ||
|
||
"Using csharp2cpg in server mode" should { | ||
"build CPGs correctly (single test)" in { | ||
val cpgOutFile = File.newTemporaryFile("csharp2cpg.bin") | ||
cpgOutFile.deleteOnExit() | ||
val projectUnderTest = newProjectUnderTest() | ||
val input = projectUnderTest.path.toAbsolutePath.toString | ||
val output = cpgOutFile.toString | ||
val client = FrontendHTTPClient(port) | ||
val req = client.buildRequest(Array(s"input=$input", s"output=$output")) | ||
client.sendRequest(req) match { | ||
case Failure(exception) => fail(exception.getMessage) | ||
case Success(out) => | ||
out shouldBe output | ||
val cpg = CpgLoader.load(output) | ||
cpg.method.name.l should contain("Main") | ||
cpg.call.code.l shouldBe List("Console.WriteLine()") | ||
} | ||
} | ||
|
||
"build CPGs correctly (multi-threaded test)" in { | ||
(0 until 10).par.foreach { index => | ||
val cpgOutFile = File.newTemporaryFile("csharp2cpg.bin") | ||
cpgOutFile.deleteOnExit() | ||
val projectUnderTest = newProjectUnderTest(Some(index)) | ||
val input = projectUnderTest.path.toAbsolutePath.toString | ||
val output = cpgOutFile.toString | ||
val client = FrontendHTTPClient(port) | ||
val req = client.buildRequest(Array(s"input=$input", s"output=$output")) | ||
client.sendRequest(req) match { | ||
case Failure(exception) => fail(exception.getMessage) | ||
case Success(out) => | ||
out shouldBe output | ||
val cpg = CpgLoader.load(output) | ||
cpg.method.name.l should contain("Main") | ||
cpg.call.code.l shouldBe List(s"Console.WriteLine($index)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
.../frontends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/io/ProjectParseTests.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
84 changes: 84 additions & 0 deletions
84
...-cli/frontends/gosrc2cpg/src/test/scala/io/joern/go2cpg/io/GoSrc2CpgHTTPServerTests.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,84 @@ | ||
package io.joern.go2cpg.io | ||
|
||
import better.files.File | ||
import io.joern.x2cpg.utils.server.FrontendHTTPClient | ||
import io.shiftleft.codepropertygraph.cpgloading.CpgLoader | ||
import io.shiftleft.semanticcpg.language.* | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.collection.parallel.CollectionConverters.RangeIsParallelizable | ||
import scala.util.Failure | ||
import scala.util.Success | ||
|
||
class GoSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndAfterAll { | ||
|
||
private var port: Int = -1 | ||
|
||
private def newProjectUnderTest(index: Option[Int] = None): File = { | ||
val dir = File.newTemporaryDirectory("gosrc2cpgTestsHttpTest") | ||
val file = dir / "main.go" | ||
file.createIfNotExists(createParents = true) | ||
val indexStr = index.map(_.toString).getOrElse("") | ||
file.writeText(s""" | ||
|package main | ||
|func main$indexStr() { | ||
| print("Hello World!") | ||
|} | ||
|""".stripMargin) | ||
file.deleteOnExit() | ||
dir.deleteOnExit() | ||
} | ||
|
||
override def beforeAll(): Unit = { | ||
// Start server | ||
port = io.joern.gosrc2cpg.Main.startup() | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
// Stop server | ||
io.joern.gosrc2cpg.Main.stop() | ||
} | ||
|
||
"Using gosrc2cpg in server mode" should { | ||
"build CPGs correctly (single test)" in { | ||
val cpgOutFile = File.newTemporaryFile("gosrc2cpg.bin") | ||
cpgOutFile.deleteOnExit() | ||
val projectUnderTest = newProjectUnderTest() | ||
val input = projectUnderTest.path.toAbsolutePath.toString | ||
val output = cpgOutFile.toString | ||
val client = FrontendHTTPClient(port) | ||
val req = client.buildRequest(Array(s"input=$input", s"output=$output")) | ||
client.sendRequest(req) match { | ||
case Failure(exception) => fail(exception.getMessage) | ||
case Success(out) => | ||
out shouldBe output | ||
val cpg = CpgLoader.load(output) | ||
cpg.method.name.l should contain("main") | ||
cpg.call.code.l shouldBe List("""print("Hello World!")""") | ||
} | ||
} | ||
|
||
"build CPGs correctly (multi-threaded test)" in { | ||
(0 until 10).par.foreach { index => | ||
val cpgOutFile = File.newTemporaryFile("gosrc2cpg.bin") | ||
cpgOutFile.deleteOnExit() | ||
val projectUnderTest = newProjectUnderTest(Some(index)) | ||
val input = projectUnderTest.path.toAbsolutePath.toString | ||
val output = cpgOutFile.toString | ||
val client = FrontendHTTPClient(port) | ||
val req = client.buildRequest(Array(s"input=$input", s"output=$output")) | ||
client.sendRequest(req) match { | ||
case Failure(exception) => fail(exception.getMessage) | ||
case Success(out) => | ||
out shouldBe output | ||
val cpg = CpgLoader.load(output) | ||
cpg.method.name.l should contain(s"main$index") | ||
cpg.call.code.l shouldBe List("""print("Hello World!")""") | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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
85 changes: 85 additions & 0 deletions
85
...tends/javasrc2cpg/src/test/scala/io/joern/javasrc2cpg/io/JavaSrc2CpgHTTPServerTests.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,85 @@ | ||
package io.joern.javasrc2cpg.io | ||
|
||
import better.files.File | ||
import io.joern.x2cpg.utils.server.FrontendHTTPClient | ||
import io.shiftleft.codepropertygraph.cpgloading.CpgLoader | ||
import io.shiftleft.semanticcpg.language.* | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.collection.parallel.CollectionConverters.RangeIsParallelizable | ||
import scala.util.Failure | ||
import scala.util.Success | ||
|
||
class JavaSrc2CpgHTTPServerTests extends AnyWordSpec with Matchers with BeforeAndAfterAll { | ||
|
||
private var port: Int = -1 | ||
|
||
private def newProjectUnderTest(index: Option[Int] = None): File = { | ||
val dir = File.newTemporaryDirectory("javasrc2cpgTestsHttpTest") | ||
val file = dir / "Main.java" | ||
file.createIfNotExists(createParents = true) | ||
val indexStr = index.map(_.toString).getOrElse("") | ||
file.writeText(s""" | ||
|class HelloWorld { | ||
| public static void main(String[] args) { | ||
| System.out.println($indexStr); | ||
| } | ||
|} | ||
|""".stripMargin) | ||
file.deleteOnExit() | ||
dir.deleteOnExit() | ||
} | ||
|
||
override def beforeAll(): Unit = { | ||
// Start server | ||
port = io.joern.javasrc2cpg.Main.startup() | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
// Stop server | ||
io.joern.javasrc2cpg.Main.stop() | ||
} | ||
|
||
"Using javasrc2cpg in server mode" should { | ||
"build CPGs correctly (single test)" in { | ||
val cpgOutFile = File.newTemporaryFile("javasrc2cpg.bin") | ||
cpgOutFile.deleteOnExit() | ||
val projectUnderTest = newProjectUnderTest() | ||
val input = projectUnderTest.path.toAbsolutePath.toString | ||
val output = cpgOutFile.toString | ||
val client = FrontendHTTPClient(port) | ||
val req = client.buildRequest(Array(s"input=$input", s"output=$output")) | ||
client.sendRequest(req) match { | ||
case Failure(exception) => fail(exception.getMessage) | ||
case Success(out) => | ||
out shouldBe output | ||
val cpg = CpgLoader.load(output) | ||
cpg.method.name.l should contain("main") | ||
cpg.call.code.l should contain("System.out.println()") | ||
} | ||
} | ||
|
||
"build CPGs correctly (multi-threaded test)" in { | ||
(0 until 10).par.foreach { index => | ||
val cpgOutFile = File.newTemporaryFile("javasrc2cpg.bin") | ||
cpgOutFile.deleteOnExit() | ||
val projectUnderTest = newProjectUnderTest(Some(index)) | ||
val input = projectUnderTest.path.toAbsolutePath.toString | ||
val output = cpgOutFile.toString | ||
val client = FrontendHTTPClient(port) | ||
val req = client.buildRequest(Array(s"input=$input", s"output=$output")) | ||
client.sendRequest(req) match { | ||
case Failure(exception) => fail(exception.getMessage) | ||
case Success(out) => | ||
out shouldBe output | ||
val cpg = CpgLoader.load(output) | ||
cpg.method.name.l should contain("main") | ||
cpg.call.code.l should contain(s"System.out.println($index)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.