-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
31 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
bridge/src/test/scala/protocbridge/frontend/OsSpecificFrontendSpec.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,56 @@ | ||
package protocbridge.frontend | ||
|
||
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 | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
bridge/src/test/scala/protocbridge/frontend/PosixPluginFrontendSpec.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,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) | ||
} | ||
} | ||
} |
33 changes: 2 additions & 31 deletions
33
bridge/src/test/scala/protocbridge/frontend/WindowsPluginFrontendSpec.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 |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |