-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·59 lines (51 loc) · 1.98 KB
/
test.py
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
#! /usr/bin/env python3
import re, subprocess, sys, tempfile
# These regular expressions need to be as specific as possible while still
# catering for the different possible outputs that occur on real platforms.
RE_BIN_SH = re.compile("/bin/sh")
# On Debian:
# $ ls /doesntexist
# ls: cannot access '/doesntexist': No such file or directory
# On OpenBSD:
# $ ls /doesntexist
# ls: /doesntexist: No such file or directory
RE_DOESNT_EXIST = re.compile("ls: (cannot access ')?/doesntexist'?: No such file or directory")
def assert_in(l, res):
for r in res:
if r.match(l) is not None:
return
print("No match for '%s'" % l)
sys.exit(1)
s = subprocess.run(["./supuner", "ls", "/bin/sh"], \
capture_output=True, encoding="utf-8")
assert s.returncode == 0
assert s.stderr.strip() == ""
assert s.stdout.strip() == ""
s = subprocess.run(["./supuner", "ls", "/bin/sh", "/doesntexist"],
capture_output=True, encoding="utf-8")
assert s.returncode != 0
assert len(s.stderr.strip().split("\n")) == 2
for l in s.stderr.strip().split("\n"):
l = l.strip()
assert_in(l, [RE_BIN_SH, RE_DOESNT_EXIST])
assert s.stdout.strip() == ""
s = subprocess.run(["./supuner", "-e", "ls", "/bin/sh"],
capture_output=True, encoding="utf-8")
assert s.returncode == 1
assert s.stderr.strip() == "-e only allowed if -o is specified"
assert s.stdout.strip() == ""
with tempfile.NamedTemporaryFile() as tf:
s = subprocess.run(["./supuner", "-e", "-o", tf.name, "ls", "/bin/sh", "/doesntexist"],
capture_output=True, encoding="utf-8")
assert s.returncode != 0
assert s.stderr.strip() == ""
assert len(s.stdout.strip().split("\n")) == 2
for l in s.stdout.strip().split("\n"):
l = l.strip()
assert_in(l, [RE_BIN_SH, RE_DOESNT_EXIST])
with open(tf.name) as f:
d = f.read()
assert len(d.strip().split("\n")) == 2
for l in d.strip().split("\n"):
l = l.strip()
assert_in(l, [RE_BIN_SH, RE_DOESNT_EXIST])