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
|
#!/usr/bin/env python
import unittest
import pexpect
import os
import subprocess
from getpass import getuser
class TestFunctions(unittest.TestCase):
user = getuser()
child = pexpect.spawn('../lshellmodule/lshell.py --config ../etc/lshell.conf ')
def spawnlshell(self, oldchild=None):
""" spawn lshell with pexpext and return the child """
child = pexpect.spawn('../lshellmodule/lshell.py --config ../etc/lshell.conf ')
if oldchild:
oldchild.close()
child.expect('%s:~\$' % getuser())
return child
def test_01(self):
""" 01 - test lshell welcome message """
expected = "You are in a limited shell.\r\nType '?' or 'help' to get " \
"the list of allowed commands\r\n"
self.child.expect('%s:~\$' % self.user)
result = self.child.before
self.assertEqual(expected, result)
def test_02(self):
""" 02 - get the output of ls """
p = subprocess.Popen( "ls ~",
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE )
(cin, cout) = (p.stdin, p.stdout)
expected = map(lambda x: x.strip(), cout)
self.child.sendline('ls')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('ls\r',1)[1].split()
self.assertEqual(expected, result)
def test_03(self):
""" 03 - echo number """
expected = "32"
self.child.sendline('echo 32')
self.child.expect("%s:~\$" % self.user)
result = self.child.before.split()[2]
self.assertEqual(expected, result)
def test_04(self):
""" 04 - echo anything """
expected = "bla blabla 32 blibli! plop."
self.child.sendline('echo "%s"' % expected)
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1].strip()
self.assertEqual(expected, result)
def test_05(self):
""" 05 - echo $(uptime) """
expected = "*** forbidden syntax -> \"echo $(uptime)\"\r\n*** You have"\
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('echo $(uptime)')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_06_0(self):
""" 06.0 - change directory """
self.child = self.spawnlshell(self.child)
expected = ""
self.child.sendline('cd tmp')
self.child.expect('%s:~/tmp\$' % self.user)
self.child.sendline('cd ..')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_06_1(self):
""" 06.1 - tilda bug """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/etc/passwd\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('cd tmp')
self.child.expect('%s:~/tmp\$' % self.user)
self.child.sendline('ls ~/../../etc/passwd')
self.child.expect('%s:~/tmp\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_07(self):
""" 07 - quotes in cd "/" """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('ls -ld "/"')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_08(self):
""" 08 - ls ~root """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/root/\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('ls ~root')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_09(self):
""" 09 - cd ~root """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/root/\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('cd ~root')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_10(self):
""" 10 - empty variable 'ls "$a"/etc/passwd' """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/etc/passwd\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('ls "$a"/etc/passwd')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_11(self):
""" 11 - empty variable 'ls -l .*./.*./etc/passwd' """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/etc/passwd\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('ls -l .*./.*./etc/passwd')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_12(self):
""" 12 - empty variable 'ls -l .?/.?/etc/passwd' """
self.child = self.spawnlshell(self.child)
expected = "*** forbidden path -> \"/etc/passwd\"\r\n*** You have" \
+ " 1 warning(s) left, before getting kicked out.\r\nThis " \
+ "incident has been reported.\r\n"
self.child.sendline('ls -l .?/.?/etc/passwd')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1]
self.assertEqual(expected, result)
def test_13(self):
""" 13 - completion with ~/ """
self.child = self.spawnlshell(self.child)
p = subprocess.Popen( "ls -F ~/tmp",
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE )
(cin, cout) = (p.stdin, p.stdout)
expected = map(lambda x: x.strip(), cout)
self.child.sendline('cd ~/tmp/\t\t')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n',1)[1].split()
result.sort()
self.assertEqual(expected, result)
# def test_14(self):
# """ 14 - command over ssh """
def test_99(self):
""" 99 - tab to list commands """
self.child = self.spawnlshell(self.child)
expected = '\x07\r\ncd clear echo exit help ll lpath ls'
self.child.sendline('\t\t')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.strip()
self.assertEqual(expected, result)
def test_99(self):
""" 99 - completion test 1 """
self.child = self.spawnlshell(self.child)
p = subprocess.Popen( "\nls -F ~",
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE )
(cin, cout) = (p.stdin, p.stdout)
expected = map(lambda x: x.strip(), cout)
self.child.sendline('ls ~\t\t')
self.child.expect('%s:~\$' % self.user)
result = self.child.before.split('\n', 1)[1].split()
self.assertEqual(expected, result)
# def test_exit(self):
# expected = ''
# self.child.sendline('exit')
# self.child.expect('$')
# result = self.child.before
# self.assertEqual(expected, result)
if __name__ == '__main__':
unittest.main()
|