Skip to content

Commit

Permalink
Add PosixPluginFrontendSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
bell-db committed Aug 16, 2024
1 parent a845105 commit 34606e3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package protocbridge.frontend

import org.apache.commons.io.IOUtils
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers
import protocbridge.{ExtraEnv, ProtocCodeGenerator}

import java.io.ByteArrayOutputStream
import scala.sys.process.ProcessIO
import scala.util.Random

class OsSpecificFrontendSpec extends AnyFlatSpec with Matchers {

protected def testPluginFrontend(frontend: PluginFrontend): Array[Byte] = {
val random = new Random()
val toSend = Array.fill(123)(random.nextInt(256).toByte)
val toReceive = Array.fill(456)(random.nextInt(256).toByte)
val env = new ExtraEnv(secondaryOutputDir = "tmp")

val fakeGenerator = new ProtocCodeGenerator {
override def run(request: Array[Byte]): Array[Byte] = {
request mustBe (toSend ++ env.toByteArrayAsField)
toReceive
}
}
val (path, state) = frontend.prepare(
fakeGenerator,
env
)
val actualOutput = new ByteArrayOutputStream()
val process = sys.process
.Process(path.toAbsolutePath.toString)
.run(new ProcessIO(writeInput => {
writeInput.write(toSend)
writeInput.close()
}, processOutput => {
val buffer = new Array[Byte](4096)
var bytesRead = 0
while (bytesRead != -1) {
bytesRead = processOutput.read(buffer)
if (bytesRead != -1) {
actualOutput.write(buffer, 0, bytesRead)
}
}
processOutput.close()
}, _.close()))
process.exitValue()
frontend.cleanup(state)
actualOutput.toByteArray
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package protocbridge.frontend

class PosixPluginFrontendSpec extends OsSpecificFrontendSpec {
if (!PluginFrontend.isWindows) {
it must "execute a program that forwards input and output to given stream" in {
testPluginFrontend(PosixPluginFrontend)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
package protocbridge.frontend

import java.io.ByteArrayInputStream

import protocbridge.{ProtocCodeGenerator, ExtraEnv}

import scala.sys.process.ProcessLogger
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

class WindowsPluginFrontendSpec extends AnyFlatSpec with Matchers {
class WindowsPluginFrontendSpec extends OsSpecificFrontendSpec {
if (PluginFrontend.isWindows) {
it must "execute a program that forwards input and output to given stream" in {
val toSend = "ping"
val toReceive = "pong"
val env = new ExtraEnv(secondaryOutputDir = "tmp")

val fakeGenerator = new ProtocCodeGenerator {
override def run(request: Array[Byte]): Array[Byte] = {
request mustBe (toSend.getBytes ++ env.toByteArrayAsField)
toReceive.getBytes
}
}
val (path, state) = WindowsPluginFrontend.prepare(
fakeGenerator,
env
)
val actualOutput = scala.collection.mutable.Buffer.empty[String]
val process = sys.process
.Process(path.toAbsolutePath.toString)
.#<(new ByteArrayInputStream(toSend.getBytes))
.run(ProcessLogger(o => actualOutput.append(o)))
process.exitValue()
actualOutput.mkString mustBe toReceive
WindowsPluginFrontend.cleanup(state)
testPluginFrontend(WindowsPluginFrontend)
}
}
}

0 comments on commit 34606e3

Please sign in to comment.