views:

183

answers:

2

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 of:

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.

+1  A: 

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.

shoosh
Looks like I'll dissect the protocol with perl's getc(). Not my favourite language to learn, but it *is* ubiquitous.
Tobu
pack / unpack would be the best to 'dissect' the protocol, and getc a spectacularly poor choice for a non-line oriented binary protocol. use read and print (or sysread and syswrite to avoid buffering) to speak to the other side.
MkV
A: 

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.

Alex Martelli