-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·142 lines (91 loc) · 3.97 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
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
#!/usr/bin/env python3
import os
import unittest
import sys
import subprocess
import subb
class TestSubb(unittest.TestCase):
def setUp(self):
sys.stdout.flush()
print("\n*** testing: ", self._testMethodName, "***\n")
sys.stdout.flush()
def tearDown(self):
sys.stdout.flush()
def test_trace_on(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_WITH_TIMESTAMP)
cmd.run("ls -al")
cmd.run("openssl rand -hex 9")
cmd.run("git ls-files")
def test_trace_on_logger(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_WITH_TIMESTAMP|subb.RunCommand.TRACE_LOG_INFO)
cmd.run("ls -al")
print("Command standard output: ", cmd.output)
cmd.run("openssl rand -hex 9")
print("Command standard output: ", cmd.output)
cmd.run("git ls-files")
def test_exit_on_error(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_ON, exit_on_error = True)
cmd.run("git branch -vv")
cmd.run("true")
got_exit = False
try:
cmd.run("false")
except SystemExit as ex:
print("caught SystemExit from run('false')", str(ex))
got_exit = True
self.assertTrue(got_exit)
def test_binary_in_out(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_ON, exit_on_error = True, convert_to_text = None)
in_plain = b'123123'
cmd.run("openssl enc -e -aes-256-ecb -pass pass:blabla", in_plain)
self.assertTrue( isinstance(cmd.output, bytes) )
enc = cmd.output
cmd.run("openssl enc -d -aes-256-ecb -pass pass:blabla", enc)
self.assertTrue( isinstance( cmd.output, bytes) )
print("in_plain", bytes.hex(in_plain), "result: ", bytes.hex(cmd.output))
self.assertTrue( bytes.hex(in_plain) == bytes.hex(cmd.output), "match encryption results")
def test_binary_out(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_ON, exit_on_error = True, convert_to_text = None)
cmd.run("openssl rand 16")
self.assertTrue( isinstance(cmd.output, bytes), "hex output expected")
self.assertTrue( len(cmd.output) == 16 )
def test_use_shell(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_ON, use_shell = True, exit_on_error = True)
cmd.run("""ls *.py | grep -c test.py""")
print("shell output: ", cmd.output)
self.assertTrue( cmd.output.rstrip() == "1" )
def test_stderr_stdout(self):
# is bash available on windows? not sure.
if sys.platform not in ("linux", "darwin"):
return
cmd = subb.RunCommand(stderr_as_stdout=True)
cmd.run("bash -x fac.sh")
print("stderr and stdout:", cmd.output)
def test_posix(self):
if sys.platform not in ("linux", "darwin"):
return
key = "secret secret"
read_end, write_end = os.pipe()
os.write(write_end, bytes(key, encoding='utf-8'))
os.close(write_end)
os.set_inheritable(read_end, True)
print("parent read_fd: ", read_end)
env = {**os.environ, "read_fd": str(read_end)}
posix_opts = subb.PlatformOptionsPosix( pass_fds=(read_end,) )
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_ON, platform_option=posix_opts, env=env)
cmd.run("python3 read.py")
print("posix test output: ", cmd.output)
self.assertTrue(cmd.output == "message from parent: " + key + "\n")
def test_timeout(self):
cmd = subb.RunCommand(trace_on=subb.RunCommand.TRACE_WITH_TIMESTAMP, timeout_sec=7)
got_timeout = False
try:
cmd.run("python3 stuck.py")
except subprocess.TimeoutExpired as exc:
print("got timeout exception: ", exc)
got_timeout = True
self.assertTrue(got_timeout)
if __name__ == '__main__':
#unittest.TestLoader.sortTestMethodsUsing = None
unittest.main()
print("*** eof test ***")