test.py 1.35 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/env python3

import os
import sys
import struct
from subprocess import Popen, PIPE




def main(argv):
Enrico Pozzobon committed
12
    p = Popen(argv[1:], bufsize=0, stdin=PIPE, stdout=PIPE)
Enrico Pozzobon committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    def write(data):
        l = p.stdin.write(data)
        if len(data) != l:
            raise Exception("could not write %d bytes of data (put %d)" % (len(data), l))

    def read(l):
        data = p.stdout.read(l)
        if len(data) != l:
            raise Exception("could not read %d bytes of data (got %d)" % (l, len(data)))
        return data

    def submit(action, data):
        h = struct.pack("<BI", ord(action), len(data))
        write(h)
        write(data)

    def obtain():
        l = read(4)
        (l, ) = struct.unpack("<I", l)
        return read(l)

    if read(14) != b"Hello, World!\n":
        raise Exception("Unexpected output")

    submit('c', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
    submit('m', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
    submit('a', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
    submit('k', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
    submit('p', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
    submit('s', bytes.fromhex("000102030405060708090A0B0C0D0E0F"))
    write(b'e')
    write(b'C')
    data = obtain()
    print(data.hex())
    

if __name__ == "__main__":
    sys.exit(main(sys.argv))