-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplesh.py
More file actions
316 lines (240 loc) · 9.53 KB
/
simplesh.py
File metadata and controls
316 lines (240 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#! /usr/bin/env python3
# -*- coding: utf-8; -*-
from enum import Enum
import argparse
import json
import os
import pexpect
import re
import subprocess
import sys
import tempfile
version = "v.0.19.1"
"""
Testing `simplesh` v0.19.1 built on pexpect library:
https://pexpect.readthedocs.io/en/stable/index.html
Ampliación de Sistemas Operativos (Curso 2019/2020)
Departamento de Ingeniería y Tecnología de Computadores
Facultad de Informática de la Universidad de Murcia
"""
################################################################################
def info(*args):
print("{}:".format(os.path.basename(sys.argv[0])), *args)
def panic(*args):
info(*args)
sys.exit(1)
################################################################################
class Range2List(argparse.Action):
""" Helper class to parse lists of ranges. """
def __call__(self, parser, namespace, test_ranges, option_strings=None):
test_ranges = test_ranges.split(',')
tests = []
regex = re.compile('^(\d+)(?:-(\d+))?$')
for test_range in test_ranges:
m = regex.match(test_range)
if not m:
raise argparse.ArgumentTypeError('Invalid test range ' + test_range)
start = int(m.group(1))
end = int(m.group(2) or start)
tests.extend(range(start, end + 1))
setattr(namespace, self.dest, tests)
def parse_arguments():
""" Parse command-line arguments. """
parser = argparse.ArgumentParser(
usage='%(prog)s [-h] [options]',
description=f'simplesh testing module {version}',
epilog='Example: %(prog)s -i boletin1.json -l 1,3-5,7'
)
parser.add_argument(
'-i', '--in-test-file',
type=argparse.FileType('r'),
dest='test_file',
required=True,
help='JSON file containing list of tests.')
parser.add_argument(
'-t', '--testids',
type=str,
dest='testids',
required=False,
default=None,
action=Range2List,
help='List of ranges of test IDs.')
parser.add_argument(
'-d', '--debug',
dest='debug',
required=False,
default=False,
action='store_true',
help='Enable debug mode.')
return parser.parse_args()
################################################################################
class ShStatus(Enum):
SUCCESS = 0
FAILURE = 1
TIMEOUT = 2
EOFCORE = 3
NOPRINT = 4
UNKNOWN = 5
################################################################################
class ShTest:
""" Shell tests. """
id = 0
echo = None
desc = None
shell = None
prompt = None
timeout = None
@staticmethod
def setup(config_d):
# Initialize class variables
ShTest.echo = False
ShTest.desc = config_d.get('desc', 'B0')
ShTest.shell = config_d.get('shell', 'simplesh')
ShTest.prompt = config_d.get('prompt', 'simplesh> ')
ShTest.timeout = config_d.get('timeout', 3)
# Make sure pexpect can find the shell if it is in the current directory
os.environ['PATH'] = os.environ.get('PATH', '') + ':' + os.getcwd()
# TODO: Primitive filesystem sandboxing as chroot requires root privileges
# Create temporary directory
try:
ShTest.cwd = os.getcwd()
ShTest.tmp_dir = tempfile.TemporaryDirectory()
os.chdir(ShTest.tmp_dir.name)
except OSError:
panic("Error: Unable to create temporary directory: '{}'.".format(ShTest.tmp_dir.name))
else:
info("Created temporary directory: '{}'.".format(ShTest.tmp_dir.name))
# Execute commands
cmd = None
try:
cmds = config_d.get('cmds', '')
for cmd in cmds:
subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True, shell=True)
except OSError:
panic("Error: Setup command not found: '{}'.".format(cmd))
except subprocess.CalledProcessError:
panic("Error: Setup command failed: '{}'.".format(cmd))
else:
info("Successfully executed setup commands '{}'.".format(cmds))
def __init__(self, test_d, config_d):
if not hasattr(ShTest, 'id'):
panic("Error: Call ShTest.setup!")
if not ShTest.id:
ShTest.setup(config_d)
ShTest.id += 1
# Initialize instance variables
self.id = ShTest.id
self.cmd = test_d.get('cmd', '')
self.out = test_d.get('out', '')
self.shproc = None
self.status = ShStatus.UNKNOWN
self.result = ''
def run(self):
# Execute shell
try:
self.shproc = pexpect.spawn(ShTest.shell,
echo=ShTest.echo,
timeout=ShTest.timeout)
except pexpect.exceptions.ExceptionPexpect as e:
panic("Test {:2}: Error executing shell: {}".format(self.id, e))
# Wait for prompt, execute command and wait for prompt again
try:
idx = self.shproc.expect([ShTest.prompt])
assert(idx == 0)
self.shproc.sendline(self.cmd)
idx = self.shproc.expect([ShTest.prompt])
assert(idx == 0)
# Prompt not found
except pexpect.exceptions.TIMEOUT:
assert(self.shproc.isalive())
self.status = ShStatus.TIMEOUT
# Shell process finished or died
except pexpect.exceptions.EOF:
assert(not self.shproc.isalive())
if not self.shproc.status: # simplesh called exit(0)
try:
self.result = (self.shproc.before.decode('utf-8'))
except UnicodeDecodeError:
self.status = ShStatus.NOPRINT
else:
self.status = ShStatus.SUCCESS if re.match(self.out, self.result) else ShStatus.FAILURE
else: # simplesh crashed
self.status = ShStatus.EOFCORE
# Prompt found: retrieve command output
else:
assert(self.shproc.isalive())
try:
self.result = self.shproc.before.decode('utf-8')
except UnicodeDecodeError:
self.status = ShStatus.NOPRINT
else:
self.status = ShStatus.SUCCESS if re.match(self.out, self.result) else ShStatus.FAILURE
finally:
# Terminate process
if self.shproc.isalive():
self.shproc.close(force=True) # Try to terminate process with SIGHUP, SIGINT or SIGKILL
# Notes:
# - https://docs.python.org/3.6.5/library/re.html
# - https://pexpect.readthedocs.io/en/stable/overview.html#find-the-end-of-line-cr-lf-conventions
def print(self, debug=False):
if self.status == ShStatus.UNKNOWN:
panic("Test {:2}: Call self.run!".format(self.id))
if debug:
print()
header = "{}: {}.T{:02}: ".format(os.path.basename(sys.argv[0]), ShTest.desc, self.id)
print(header, end='')
if self.status == ShStatus.SUCCESS:
print("Result : OK!")
else:
print("Result : KO!")
if debug:
print(header, end='')
print("Command : '{:60}'".format(self.cmd[:60]))
if self.status == ShStatus.SUCCESS or self.status == ShStatus.FAILURE:
print(header, end='')
print("Expected : '{:60}'".format(self.out[:60]))
print(header, end='')
print("Produced : '{:60}'".format(self.result[:60]))
print("{}: {}.T{:02}: Produced: ".format(os.path.basename(sys.argv[0]), ShTest.desc, self.id), end='')
print('{}'.format(list(self.result)))
elif self.status == ShStatus.TIMEOUT:
print(header, end='')
print("Produced : '{:^60}'".format('TIMEOUT! Prompt not found!'))
elif self.status == ShStatus.EOFCORE: # Test with 'raise(SIGSEGV);'
print(header, end='')
print("Produced : '{:^60}'".format('CORE! (ulimit -c unlimited)'))
elif self.status == ShStatus.NOPRINT: # Test with 'printf("\U3451F50E");'
print(header, end='')
print("Produced : '{:^60}'".format('Non-printable charactes in output (possibly a memory leak)'))
################################################################################
def main():
""" Main driver. """
info("Version: {}".format(version))
# Parse command-line arguments
args = parse_arguments()
# Parse JSON file
tests_json = None
try:
tests_json = json.load(args.test_file)
except ValueError:
panic("Error: Invalid JSON format.".format(args.test_file.name))
# Instantiate test objects
tests = [ShTest(t, tests_json['setup']) for t in tests_json['tests']]
# Run tests
if args.testids is None:
for test in tests:
test.run()
test.print(debug=args.debug)
else:
if not set(args.testids) < set(range(1, len(tests)+1, 1)):
panic("Error: Invalid list or ranges of test IDs ({}).".format(args.testids))
for testid in args.testids:
tests[testid-1].run()
tests[testid-1].print(debug=args.debug)
return 0
################################################################################
if __name__ == "__main__":
sys.exit(main())