This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_shellescape.py
51 lines (44 loc) · 1.93 KB
/
test_shellescape.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
from itertools import combinations_with_replacement
from shellescape import shell_escape
import subprocess
import unittest
class ShellEscapeTest(unittest.TestCase):
def test_tricky_strings(self):
cases = [
("", "''"),
("Hello world", "'Hello world'"),
("this_is_a_test", "this_is_a_test"),
("Hi!", "'Hi!'"),
("bash -c 'rm -rf /'", '\'bash -c \'"\'"\'rm -rf /\'"\'"\'\''),
("~", "'~'"),
("$HOME", "'$HOME'"),
("xeyes &", "'xeyes &'"),
("\\", "'\\'"),
("\\\\", "'\\\\'"),
]
for case, expected in cases:
argv = ["bash", "-c", 'echo -n %s' % (shell_escape(case),)]
child = subprocess.Popen(argv, stdout=subprocess.PIPE)
stdout, stderr = child.communicate()
assert child.returncode == 0, repr(argv)
self.assertEqual(stdout, case)
self.assertEqual(shell_escape(case), expected)
def test_all_3_char_strings(self):
# To reduce the search space we assume that all bytes > 128 are
# treated the same way as byte 128. We also assume that all letters
# and digits are treated the same way.
bytes = range(1, 66) + range(90, 98) + range(122, 129)
chars = "".join(chr(b) for b in bytes)
assert "a" in chars
assert "A" in chars
assert chr(128) in chars
for length in range(3):
for case in combinations_with_replacement(chars, length):
case = "".join(case)
argv = ["bash", "-c", 'echo -n %s' % (shell_escape(case),)]
child = subprocess.Popen(argv, stdout=subprocess.PIPE)
stdout, stderr = child.communicate()
assert child.returncode == 0, repr(argv)
self.assertEqual(stdout, case)
if __name__ == "__main__":
unittest.main()