Skip to content

Commit 40206b8

Browse files
committed
Add PosixPluginFrontendSpec
1 parent a845105 commit 40206b8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package protocbridge.frontend
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.must.Matchers
5+
import protocbridge.{ExtraEnv, ProtocCodeGenerator}
6+
7+
import java.io.ByteArrayOutputStream
8+
import scala.sys.process.ProcessIO
9+
import scala.util.Random
10+
11+
class PosixPluginFrontendSpec extends AnyFlatSpec with Matchers {
12+
if (!PluginFrontend.isWindows) {
13+
it must "execute a program that forwards input and output to given stream" in {
14+
val random = new Random()
15+
val toSend = Array.fill(123)(random.nextInt(256).toByte)
16+
val toReceive = Array.fill(456)(random.nextInt(256).toByte)
17+
val env = new ExtraEnv(secondaryOutputDir = "tmp")
18+
19+
val fakeGenerator = new ProtocCodeGenerator {
20+
override def run(request: Array[Byte]): Array[Byte] = {
21+
request mustBe (toSend ++ env.toByteArrayAsField)
22+
toReceive
23+
}
24+
}
25+
val (path, state) = PosixPluginFrontend.prepare(
26+
fakeGenerator,
27+
env
28+
)
29+
val actualOutput = new ByteArrayOutputStream()
30+
val process = sys.process
31+
.Process(path.toAbsolutePath.toString)
32+
.run(new ProcessIO(writeInput => {
33+
writeInput.write(toSend)
34+
writeInput.close()
35+
}, processOutput => {
36+
val buffer = new Array[Byte](4096)
37+
var bytesRead = 0
38+
while (bytesRead != -1) {
39+
bytesRead = processOutput.read(buffer)
40+
if (bytesRead != -1) {
41+
actualOutput.write(buffer, 0, bytesRead)
42+
}
43+
}
44+
processOutput.close()
45+
}, _.close()))
46+
process.exitValue()
47+
actualOutput.toByteArray mustBe toReceive
48+
PosixPluginFrontend.cleanup(state)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)