| 1 |
#!/usr/bin/env python3 |
| 2 |
""" |
| 3 |
chamuyero is a tool to test and validate line-oriented commands and servers. |
| 4 |
|
| 5 |
It can launch and communicate with other processes, and follow a script of |
| 6 |
line-oriented request-response, validating the dialog as it goes along. |
| 7 |
|
| 8 |
This can be used to test line-oriented network protocols (such as SMTP) or |
| 9 |
interactive command-line tools. |
| 10 |
""" |
| 11 |
|
| 12 |
import argparse |
| 13 |
import os |
| 14 |
import re |
| 15 |
import ssl |
| 16 |
import socket |
| 17 |
import subprocess |
| 18 |
import sys |
| 19 |
import threading |
| 20 |
import time |
| 21 |
|
| 22 |
# Command-line flags. |
| 23 |
ap = argparse.ArgumentParser() |
| 24 |
ap.add_argument("script", type=argparse.FileType("r", encoding="utf8")) |
| 25 |
args = ap.parse_args() |
| 26 |
|
| 27 |
# Make sure stdout is open in utf8 mode, as we will print our input, which is |
| 28 |
# utf8, and want it to work regardless of the environment. |
| 29 |
sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf8", buffering=1) |
| 30 |
|
| 31 |
|
| 32 |
class Process(object): |
| 33 |
def __init__(self, cmd, **kwargs): |
| 34 |
self.cmd = subprocess.Popen(cmd, **kwargs) |
| 35 |
|
| 36 |
def write(self, s): |
| 37 |
self.cmd.stdin.write(s) |
| 38 |
|
| 39 |
def readline(self): |
| 40 |
return self.cmd.stdout.readline() |
| 41 |
|
| 42 |
def wait(self): |
| 43 |
return self.cmd.wait() |
| 44 |
|
| 45 |
def close(self): |
| 46 |
return self.cmd.stdin.close() |
| 47 |
|
| 48 |
|
| 49 |
class Sock(object): |
| 50 |
"""A (generic) socket. |
| 51 |
|
| 52 |
This class implements the common code for socket support. |
| 53 |
Subclasses will implement the behaviour specific to different socket |
| 54 |
types. |
| 55 |
""" |
| 56 |
|
| 57 |
def __init__(self, addr): |
| 58 |
self.addr = addr |
| 59 |
self.sock = NotImplemented |
| 60 |
self.connr = None |
| 61 |
self.connw = None |
| 62 |
self.has_conn = threading.Event() |
| 63 |
|
| 64 |
def listen(self): |
| 65 |
self.sock.bind(self.addr) |
| 66 |
self.sock.listen(1) |
| 67 |
threading.Thread(target=self._accept).start() |
| 68 |
|
| 69 |
def _accept(self): |
| 70 |
conn, _ = self.sock.accept() |
| 71 |
self.connr = conn.makefile(mode="r", encoding="utf8") |
| 72 |
self.connw = conn.makefile(mode="w", encoding="utf8") |
| 73 |
self.has_conn.set() |
| 74 |
|
| 75 |
def write(self, s): |
| 76 |
self.has_conn.wait() |
| 77 |
self.connw.write(s) |
| 78 |
self.connw.flush() |
| 79 |
|
| 80 |
def readline(self): |
| 81 |
self.has_conn.wait() |
| 82 |
return self.connr.readline() |
| 83 |
|
| 84 |
def close(self): |
| 85 |
self.connr.close() |
| 86 |
self.connw.close() |
| 87 |
self.sock.close() |
| 88 |
|
| 89 |
|
| 90 |
class UnixSock(Sock): |
| 91 |
def __init__(self, addr): |
| 92 |
Sock.__init__(self, addr) |
| 93 |
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 94 |
|
| 95 |
def listen(self): |
| 96 |
if os.path.exists(self.addr): |
| 97 |
os.remove(self.addr) |
| 98 |
Sock.listen(self) |
| 99 |
|
| 100 |
|
| 101 |
class TCPSock(Sock): |
| 102 |
def __init__(self, addr): |
| 103 |
host, port = addr.rsplit(":", 1) |
| 104 |
Sock.__init__(self, (host, int(port))) |
| 105 |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 106 |
|
| 107 |
def connect(self): |
| 108 |
self.sock = socket.create_connection(self.addr) |
| 109 |
self.connr = self.sock.makefile(mode="r", encoding="utf8") |
| 110 |
self.connw = self.sock.makefile(mode="w", encoding="utf8") |
| 111 |
self.has_conn.set() |
| 112 |
|
| 113 |
|
| 114 |
class TLSSock(Sock): |
| 115 |
def __init__(self, addr): |
| 116 |
host, port = addr.rsplit(":", 1) |
| 117 |
Sock.__init__(self, (host, int(port))) |
| 118 |
plain_sock = socket.create_connection(self.addr) |
| 119 |
|
| 120 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 121 |
context.check_hostname = False |
| 122 |
context.verify_mode = ssl.CERT_NONE |
| 123 |
self.sock = context.wrap_socket(plain_sock) |
| 124 |
|
| 125 |
def connect(self): |
| 126 |
self.connr = self.sock.makefile(mode="r", encoding="utf8") |
| 127 |
self.connw = self.sock.makefile(mode="w", encoding="utf8") |
| 128 |
self.has_conn.set() |
| 129 |
|
| 130 |
|
| 131 |
class Interpreter(object): |
| 132 |
"""Interpreter for chamuyero scripts.""" |
| 133 |
|
| 134 |
def __init__(self): |
| 135 |
# Processes and sockets we have spawn. Indexed by the id provided by |
| 136 |
# the user. |
| 137 |
self.procs = {} |
| 138 |
|
| 139 |
# Line number we are processing. |
| 140 |
self.nline = 0 |
| 141 |
|
| 142 |
def syntax_error(self, msg): |
| 143 |
raise SyntaxError("Error in line %d: %s" % (self.nline, msg)) |
| 144 |
|
| 145 |
def runtime_error(self, msg): |
| 146 |
raise RuntimeError("Error in line %d: %s" % (self.nline, msg)) |
| 147 |
|
| 148 |
def run(self, fd): |
| 149 |
"""Main processing loop.""" |
| 150 |
cont_l = "" |
| 151 |
for l in fd: |
| 152 |
self.nline += 1 |
| 153 |
|
| 154 |
# Remove rightmost \n. |
| 155 |
l = l[:-1] |
| 156 |
|
| 157 |
# Continuations with \. |
| 158 |
if cont_l: |
| 159 |
l = cont_l + " " + l.lstrip() |
| 160 |
if l.endswith("\\"): |
| 161 |
cont_l = l[:-1] |
| 162 |
continue |
| 163 |
else: |
| 164 |
cont_l = "" |
| 165 |
|
| 166 |
# Comments start with a "#". |
| 167 |
if l.strip().startswith("#") or l.strip() == "": |
| 168 |
continue |
| 169 |
|
| 170 |
print(l) |
| 171 |
|
| 172 |
# Everything else is of the form: |
| 173 |
# <proc> <op> [params] |
| 174 |
sp = l.split(None, 2) |
| 175 |
if len(sp) == 3: |
| 176 |
proc, op, params = sp |
| 177 |
else: |
| 178 |
proc, op = sp |
| 179 |
params = "" |
| 180 |
|
| 181 |
# = Launch a process. |
| 182 |
if op == "=": |
| 183 |
cmd = Process( |
| 184 |
params, |
| 185 |
shell=True, |
| 186 |
universal_newlines=True, |
| 187 |
stdin=subprocess.PIPE, |
| 188 |
stdout=subprocess.PIPE, |
| 189 |
stderr=subprocess.STDOUT, |
| 190 |
) |
| 191 |
self.procs[proc] = cmd |
| 192 |
|
| 193 |
# |= Launch a process, do not capture stdout. |
| 194 |
elif op == "|=": |
| 195 |
cmd = Process(params, shell=True, stdin=subprocess.PIPE) |
| 196 |
self.procs[proc] = cmd |
| 197 |
|
| 198 |
# unix_listen Listen on an UNIX socket. |
| 199 |
elif op == "unix_listen": |
| 200 |
sock = UnixSock(params) |
| 201 |
sock.listen() |
| 202 |
self.procs[proc] = sock |
| 203 |
|
| 204 |
# tcp_listen Listen on a TCP socket. |
| 205 |
elif op == "tcp_listen": |
| 206 |
sock = TCPSock(params) |
| 207 |
sock.listen() |
| 208 |
self.procs[proc] = sock |
| 209 |
|
| 210 |
elif op == "tcp_connect": |
| 211 |
sock = TCPSock(params) |
| 212 |
sock.connect() |
| 213 |
self.procs[proc] = sock |
| 214 |
|
| 215 |
elif op == "tls_connect": |
| 216 |
sock = TLSSock(params) |
| 217 |
sock.connect() |
| 218 |
self.procs[proc] = sock |
| 219 |
|
| 220 |
# -> Send to a process stdin, with a \r\n at the end. |
| 221 |
# .> Send to a process stdin, no \r\n at the end. |
| 222 |
# ~> Send to a process stdin, string is python-evaluated. |
| 223 |
elif op == "->": |
| 224 |
self.procs[proc].write(params + "\r\n") |
| 225 |
elif op == ".>": |
| 226 |
self.procs[proc].write(params) |
| 227 |
elif op == "~>": |
| 228 |
self.procs[proc].write(eval(params)) |
| 229 |
|
| 230 |
# <- Read from the process, expect matching input. |
| 231 |
# <~ Read from the process, match input using regexp. |
| 232 |
# <... Read many lines until one matches. |
| 233 |
elif op == "<-": |
| 234 |
read = self.procs[proc].readline() |
| 235 |
if read != params + "\n": |
| 236 |
self.runtime_error( |
| 237 |
"data different that expected:\n" |
| 238 |
+ " expected: %s\n" % repr(params) |
| 239 |
+ " got: %s" % repr(read) |
| 240 |
) |
| 241 |
elif op == "<~": |
| 242 |
read = self.procs[proc].readline() |
| 243 |
m = re.match(params, read) |
| 244 |
if m is None: |
| 245 |
self.runtime_error( |
| 246 |
"data did not match regexp:\n" |
| 247 |
+ " regexp: %s\n" % repr(params) |
| 248 |
+ " got: %s" % repr(read) |
| 249 |
) |
| 250 |
elif op == "<...": |
| 251 |
while True: |
| 252 |
read = self.procs[proc].readline() |
| 253 |
m = re.match(params, read) |
| 254 |
if m: |
| 255 |
break |
| 256 |
|
| 257 |
# sleep Sleep this number of seconds (process-independent). |
| 258 |
elif op == "sleep": |
| 259 |
time.sleep(float(params)) |
| 260 |
|
| 261 |
# wait Wait for the process to exit (with the given code). |
| 262 |
elif op == "wait": |
| 263 |
retcode = self.procs[proc].wait() |
| 264 |
if params and retcode != int(params): |
| 265 |
self.runtime_error( |
| 266 |
"return code did not match:\n" |
| 267 |
+ " expected %s, got %d" % (params, retcode) |
| 268 |
) |
| 269 |
|
| 270 |
# close Close the process. |
| 271 |
elif op == "close": |
| 272 |
self.procs[proc].close() |
| 273 |
|
| 274 |
else: |
| 275 |
self.syntax_error("unknown syntax") |
| 276 |
|
| 277 |
|
| 278 |
if __name__ == "__main__": |
| 279 |
i = Interpreter() |
| 280 |
i.run(args.script) |