-
-
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
5 changed files
with
91 additions
and
75 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
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
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
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, | ||
generator: ProtocCodeGenerator, | ||
env: ExtraEnv, | ||
request: Array[Byte] | ||
): Array[Byte] = { | ||
val (path, state) = frontend.prepare( | ||
generator, | ||
env | ||
) | ||
val actualOutput = new ByteArrayOutputStream() | ||
val process = sys.process | ||
.Process(path.toAbsolutePath.toString) | ||
.run(new ProcessIO(writeInput => { | ||
writeInput.write(request) | ||
writeInput.close() | ||
}, processOutput => { | ||
IOUtils.copy(processOutput, actualOutput) | ||
processOutput.close() | ||
}, _.close())) | ||
process.exitValue() | ||
frontend.cleanup(state) | ||
actualOutput.toByteArray | ||
} | ||
|
||
protected def testSuccess(frontend: PluginFrontend): Unit = { | ||
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 response = testPluginFrontend(frontend, fakeGenerator, env, toSend) | ||
response mustBe toReceive | ||
} | ||
|
||
protected def testFailure(frontend: PluginFrontend): Unit = { | ||
val random = new Random() | ||
val toSend = Array.fill(123)(random.nextInt(256).toByte) | ||
val env = new ExtraEnv(secondaryOutputDir = "tmp") | ||
|
||
val fakeGenerator = new ProtocCodeGenerator { | ||
override def run(request: Array[Byte]): Array[Byte] = { | ||
throw new OutOfMemoryError("test error") | ||
} | ||
} | ||
val response = testPluginFrontend(frontend, fakeGenerator, env, toSend) | ||
response.length must be > 0 | ||
} | ||
} |
42 changes: 5 additions & 37 deletions
42
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 |
---|---|---|
@@ -1,45 +1,13 @@ | ||
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 PosixPluginFrontendSpec extends AnyFlatSpec with Matchers { | ||
class PosixPluginFrontendSpec extends OsSpecificFrontendSpec { | ||
if (!PluginFrontend.isWindows) { | ||
it must "execute a program that forwards input and output to given stream" in { | ||
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") | ||
testSuccess(PosixPluginFrontend) | ||
} | ||
|
||
val fakeGenerator = new ProtocCodeGenerator { | ||
override def run(request: Array[Byte]): Array[Byte] = { | ||
request mustBe (toSend ++ env.toByteArrayAsField) | ||
toReceive | ||
} | ||
} | ||
val (path, state) = PosixPluginFrontend.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 => { | ||
IOUtils.copy(processOutput, actualOutput) | ||
processOutput.close() | ||
}, _.close())) | ||
process.exitValue() | ||
actualOutput.toByteArray mustBe toReceive | ||
PosixPluginFrontend.cleanup(state) | ||
it must "not hang if there is an error in generator" in { | ||
testFailure(PosixPluginFrontend) | ||
} | ||
} | ||
} |
35 changes: 5 additions & 30 deletions
35
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,13 @@ | ||
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") | ||
testSuccess(WindowsPluginFrontend) | ||
} | ||
|
||
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) | ||
it must "not hang if there is an error in generator" in { | ||
testFailure(WindowsPluginFrontend) | ||
} | ||
} | ||
} |