| 1 |
import py |
| 2 |
import errno, os, StringIO |
| 3 |
from rpython.tool.sourcetools import func_with_new_name |
| 4 |
from rpython.rtyper.lltypesystem import rffi |
| 5 |
from rpython.translator.sandbox.sandlib import SandboxedProc |
| 6 |
from rpython.translator.sandbox.sandlib import SimpleIOSandboxedProc |
| 7 |
from rpython.translator.sandbox.sandlib import VirtualizedSandboxedProc |
| 8 |
from rpython.translator.sandbox.sandlib import VirtualizedSocketProc |
| 9 |
from rpython.translator.sandbox.test.test_sandbox import compile |
| 10 |
from rpython.translator.sandbox.vfs import Dir, File, RealDir, RealFile |
| 11 |
|
| 12 |
|
| 13 |
class MockSandboxedProc(SandboxedProc): |
| 14 |
"""A sandbox process wrapper that replays expected syscalls.""" |
| 15 |
|
| 16 |
def __init__(self, args, expected): |
| 17 |
SandboxedProc.__init__(self, args) |
| 18 |
self.expected = expected |
| 19 |
self.seen = 0 |
| 20 |
|
| 21 |
def _make_method(name): |
| 22 |
def do_xxx(self, *input): |
| 23 |
print "decoded from subprocess: %s%r" % (name, input) |
| 24 |
expectedmsg, expectedinput, output = self.expected[self.seen] |
| 25 |
assert name == expectedmsg |
| 26 |
assert input == expectedinput |
| 27 |
self.seen += 1 |
| 28 |
if isinstance(output, Exception): |
| 29 |
raise output |
| 30 |
return output |
| 31 |
return func_with_new_name(do_xxx, 'do_%s' % name) |
| 32 |
|
| 33 |
do_ll_os__ll_os_open = _make_method("open") |
| 34 |
do_ll_os__ll_os_read = _make_method("read") |
| 35 |
do_ll_os__ll_os_write = _make_method("write") |
| 36 |
do_ll_os__ll_os_close = _make_method("close") |
| 37 |
|
| 38 |
|
| 39 |
def test_lib(): |
| 40 |
def entry_point(argv): |
| 41 |
fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) |
| 42 |
assert fd == 77 |
| 43 |
res = os.read(fd, 123) |
| 44 |
assert res == "he\x00llo" |
| 45 |
count = os.write(fd, "world\x00!\x00") |
| 46 |
assert count == 42 |
| 47 |
for arg in argv: |
| 48 |
count = os.write(fd, arg) |
| 49 |
assert count == 61 |
| 50 |
os.close(fd) |
| 51 |
return 0 |
| 52 |
exe = compile(entry_point) |
| 53 |
|
| 54 |
proc = MockSandboxedProc([exe, 'x1', 'y2'], expected = [ |
| 55 |
("open", ("/tmp/foobar", os.O_RDONLY, 0777), 77), |
| 56 |
("read", (77, 123), "he\x00llo"), |
| 57 |
("write", (77, "world\x00!\x00"), 42), |
| 58 |
("write", (77, exe), 61), |
| 59 |
("write", (77, "x1"), 61), |
| 60 |
("write", (77, "y2"), 61), |
| 61 |
("close", (77,), None), |
| 62 |
]) |
| 63 |
proc.handle_forever() |
| 64 |
assert proc.seen == len(proc.expected) |
| 65 |
|
| 66 |
def test_foobar(): |
| 67 |
py.test.skip("to be updated") |
| 68 |
foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG) |
| 69 |
def entry_point(argv): |
| 70 |
s = rffi.str2charp(argv[1]); n = foobar(s); rffi.free_charp(s) |
| 71 |
s = rffi.str2charp(argv[n]); n = foobar(s); rffi.free_charp(s) |
| 72 |
return n |
| 73 |
exe = compile(entry_point) |
| 74 |
|
| 75 |
proc = MockSandboxedProc([exe, 'spam', 'egg'], expected = [ |
| 76 |
("foobar", ("spam",), 2), |
| 77 |
("foobar", ("egg",), 0), |
| 78 |
]) |
| 79 |
proc.handle_forever() |
| 80 |
assert proc.seen == len(proc.expected) |
| 81 |
|
| 82 |
def test_simpleio(): |
| 83 |
def entry_point(argv): |
| 84 |
print "Please enter a number:" |
| 85 |
buf = "" |
| 86 |
while True: |
| 87 |
t = os.read(0, 1) |
| 88 |
if not t: |
| 89 |
raise EOFError |
| 90 |
if t == '\n': |
| 91 |
break |
| 92 |
buf += t |
| 93 |
num = int(buf) |
| 94 |
print "The double is:", num * 2 |
| 95 |
return 0 |
| 96 |
exe = compile(entry_point) |
| 97 |
|
| 98 |
proc = SimpleIOSandboxedProc([exe, 'x1', 'y2']) |
| 99 |
output, error = proc.communicate("21\n") |
| 100 |
assert output == "Please enter a number:\nThe double is: 42\n" |
| 101 |
assert error == "" |
| 102 |
|
| 103 |
def test_socketio(): |
| 104 |
class SocketProc(VirtualizedSocketProc, SimpleIOSandboxedProc): |
| 105 |
def build_virtual_root(self): |
| 106 |
pass |
| 107 |
|
| 108 |
def entry_point(argv): |
| 109 |
fd = os.open("tcp://python.org:80", os.O_RDONLY, 0777) |
| 110 |
os.write(fd, 'GET /\n') |
| 111 |
print os.read(fd, 50) |
| 112 |
return 0 |
| 113 |
exe = compile(entry_point) |
| 114 |
|
| 115 |
proc = SocketProc([exe]) |
| 116 |
output, error = proc.communicate("") |
| 117 |
assert output.startswith('HTTP/1.1 400 Bad Request') |
| 118 |
|
| 119 |
def test_oserror(): |
| 120 |
def entry_point(argv): |
| 121 |
try: |
| 122 |
os.open("/tmp/foobar", os.O_RDONLY, 0777) |
| 123 |
except OSError as e: |
| 124 |
os.close(e.errno) |
| 125 |
return 0 |
| 126 |
exe = compile(entry_point) |
| 127 |
|
| 128 |
proc = MockSandboxedProc([exe], expected = [ |
| 129 |
("open", ("/tmp/foobar", os.O_RDONLY, 0777), OSError(-42, "baz")), |
| 130 |
("close", (-42,), None), |
| 131 |
]) |
| 132 |
proc.handle_forever() |
| 133 |
assert proc.seen == len(proc.expected) |
| 134 |
|
| 135 |
|
| 136 |
class SandboxedProcWithFiles(VirtualizedSandboxedProc, SimpleIOSandboxedProc): |
| 137 |
"""A sandboxed process with a simple virtualized filesystem. |
| 138 |
|
| 139 |
For testing file operations. |
| 140 |
|
| 141 |
""" |
| 142 |
def build_virtual_root(self): |
| 143 |
return Dir({ |
| 144 |
'hi.txt': File("Hello, world!\n"), |
| 145 |
'this.pyc': RealFile(__file__), |
| 146 |
}) |
| 147 |
|
| 148 |
def test_too_many_opens(): |
| 149 |
def entry_point(argv): |
| 150 |
try: |
| 151 |
open_files = [] |
| 152 |
for i in range(500): |
| 153 |
fd = os.open('/hi.txt', os.O_RDONLY, 0777) |
| 154 |
open_files.append(fd) |
| 155 |
txt = os.read(fd, 100) |
| 156 |
if txt != "Hello, world!\n": |
| 157 |
print "Wrong content: %s" % txt |
| 158 |
except OSError as e: |
| 159 |
|
| 160 |
if e.errno != errno.EMFILE: |
| 161 |
print "OSError: %s!" % (e.errno,) |
| 162 |
else: |
| 163 |
print "We opened 500 fake files! Shouldn't have been able to." |
| 164 |
|
| 165 |
for fd in open_files: |
| 166 |
os.close(fd) |
| 167 |
|
| 168 |
try: |
| 169 |
open_files = [] |
| 170 |
for i in range(500): |
| 171 |
fd = os.open('/this.pyc', os.O_RDONLY, 0777) |
| 172 |
open_files.append(fd) |
| 173 |
except OSError as e: |
| 174 |
|
| 175 |
if e.errno != errno.EMFILE: |
| 176 |
print "OSError: %s!" % (e.errno,) |
| 177 |
else: |
| 178 |
print "We opened 500 real files! Shouldn't have been able to." |
| 179 |
|
| 180 |
print "All ok!" |
| 181 |
return 0 |
| 182 |
exe = compile(entry_point) |
| 183 |
|
| 184 |
proc = SandboxedProcWithFiles([exe]) |
| 185 |
output, error = proc.communicate("") |
| 186 |
assert output == "All ok!\n" |
| 187 |
assert error == "" |
| 188 |
|
| 189 |
def test_fstat(): |
| 190 |
def compare(a, b, i): |
| 191 |
if a != b: |
| 192 |
print "stat and fstat differ @%d: %s != %s" % (i, a, b) |
| 193 |
|
| 194 |
def entry_point(argv): |
| 195 |
try: |
| 196 |
|
| 197 |
fd = os.open('/hi.txt', os.O_RDONLY, 0777) |
| 198 |
st = os.stat('/hi.txt') |
| 199 |
fs = os.fstat(fd) |
| 200 |
|
| 201 |
compare(st[0], fs[0], 0) |
| 202 |
compare(st[1], fs[1], 1) |
| 203 |
compare(st[2], fs[2], 2) |
| 204 |
compare(st[3], fs[3], 3) |
| 205 |
compare(st[4], fs[4], 4) |
| 206 |
compare(st[5], fs[5], 5) |
| 207 |
compare(st[6], fs[6], 6) |
| 208 |
compare(st[7], fs[7], 7) |
| 209 |
compare(st[8], fs[8], 8) |
| 210 |
compare(st[9], fs[9], 9) |
| 211 |
except OSError as e: |
| 212 |
print "OSError: %s" % (e.errno,) |
| 213 |
print "All ok!" |
| 214 |
return 0 |
| 215 |
exe = compile(entry_point) |
| 216 |
|
| 217 |
proc = SandboxedProcWithFiles([exe]) |
| 218 |
output, error = proc.communicate("") |
| 219 |
assert output == "All ok!\n" |
| 220 |
assert error == "" |
| 221 |
|
| 222 |
def test_lseek(): |
| 223 |
def char_should_be(c, should): |
| 224 |
if c != should: |
| 225 |
print "Wrong char: '%s' should be '%s'" % (c, should) |
| 226 |
|
| 227 |
def entry_point(argv): |
| 228 |
fd = os.open('/hi.txt', os.O_RDONLY, 0777) |
| 229 |
char_should_be(os.read(fd, 1), "H") |
| 230 |
new = os.lseek(fd, 3, os.SEEK_CUR) |
| 231 |
if new != 4: |
| 232 |
print "Wrong offset, %d should be 4" % new |
| 233 |
char_should_be(os.read(fd, 1), "o") |
| 234 |
new = os.lseek(fd, -3, os.SEEK_END) |
| 235 |
if new != 11: |
| 236 |
print "Wrong offset, %d should be 11" % new |
| 237 |
char_should_be(os.read(fd, 1), "d") |
| 238 |
new = os.lseek(fd, 7, os.SEEK_SET) |
| 239 |
if new != 7: |
| 240 |
print "Wrong offset, %d should be 7" % new |
| 241 |
char_should_be(os.read(fd, 1), "w") |
| 242 |
print "All ok!" |
| 243 |
return 0 |
| 244 |
exe = compile(entry_point) |
| 245 |
|
| 246 |
proc = SandboxedProcWithFiles([exe]) |
| 247 |
output, error = proc.communicate("") |
| 248 |
assert output == "All ok!\n" |
| 249 |
assert error == "" |
| 250 |
|
| 251 |
def test_getuid(): |
| 252 |
if not hasattr(os, 'getuid'): |
| 253 |
py.test.skip("posix only") |
| 254 |
|
| 255 |
def entry_point(argv): |
| 256 |
import os |
| 257 |
print "uid is %s" % os.getuid() |
| 258 |
print "euid is %s" % os.geteuid() |
| 259 |
print "gid is %s" % os.getgid() |
| 260 |
print "egid is %s" % os.getegid() |
| 261 |
return 0 |
| 262 |
exe = compile(entry_point) |
| 263 |
|
| 264 |
proc = SandboxedProcWithFiles([exe]) |
| 265 |
output, error = proc.communicate("") |
| 266 |
assert output == "uid is 1000\neuid is 1000\ngid is 1000\negid is 1000\n" |
| 267 |
assert error == "" |