Expect-like tool for binary protocol testing
I'd like to write tests for a simple byte-oriented protocol using something like Expect. The test scripts would look like a plain stream o开发者_运维问答f:
send "data"
expect "data"
(Where send writes to stdout and expect reads just enough from stdin, exiting if it doesn't match.)
I can't use expect itself because it is designed for buffered, line-oriented interactions. Pattern-matching with grep or awk is out for the same reason.
Is there a UNIX tool or library I can use? Even better if it is standard and portable, as it needs to integrate with an existing program.
A somewhat ugly solution would be to write a small perl or python script which converts the binary stream to textual hexadecimal numbers and then use Expect to work with that.
pexpect should let you use binary (byte strings) without any problem, if you're OK with Python for the logic -- and it's pure-Python and portable to all sufficiently unix-y platforms (basically one with pty's;-). Similarly, Python's regular expressions and other byte string manipulations have no problems with any binary string whatsoever; and Python standard library modules like struct
and array
ease the manipulation for such binary byte strings and their conversion back and forth to other type of meaningful representations for the data.
精彩评论