def test_lib(): def entry_point(argv): fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) assert fd == 77 res = os.read(fd, 123) assert res == "he\x00llo" count = os.write(fd, "world\x00!\x00") assert count == 42 for arg in argv: count = os.write(fd, arg) assert count == 61 os.close(fd) return 0 exe = compile(entry_point) proc = MockSandboxedProc([exe, 'x1', 'y2'], expected = [ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), 77), ("read", (77, 123), "he\x00llo"), ("write", (77, "world\x00!\x00"), 42), ("write", (77, exe), 61), ("write", (77, "x1"), 61), ("write", (77, "y2"), 61), ("close", (77,), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def test_lseek(): def char_should_be(c, should): if c != should: print "Wrong char: '%s' should be '%s'" % (c, should) def entry_point(argv): fd = os.open('/hi.txt', os.O_RDONLY, 0777) char_should_be(os.read(fd, 1), "H") new = os.lseek(fd, 3, os.SEEK_CUR) if new != 4: print "Wrong offset, %d should be 4" % new char_should_be(os.read(fd, 1), "o") new = os.lseek(fd, -3, os.SEEK_END) if new != 11: print "Wrong offset, %d should be 11" % new char_should_be(os.read(fd, 1), "d") new = os.lseek(fd, 7, os.SEEK_SET) if new != 7: print "Wrong offset, %d should be 7" % new char_should_be(os.read(fd, 1), "w") print "All ok!" return 0 exe = compile(entry_point) proc = SandboxedProcWithFiles([exe]) output, error = proc.communicate("") assert output == "All ok!\n" assert error == ""
def test_lib(): def entry_point(argv): fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) assert fd == 77 res = os.read(fd, 123) assert res == "he\x00llo" count = os.write(fd, "world\x00!\x00") assert count == 42 for arg in argv: count = os.write(fd, arg) assert count == 61 os.close(fd) return 0 exe = compile(entry_point) proc = MySandboxedProc([exe, 'x1', 'y2'], expected=[ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), 77), ("read", (77, 123), "he\x00llo"), ("write", (77, "world\x00!\x00"), 42), ("write", (77, exe), 61), ("write", (77, "x1"), 61), ("write", (77, "y2"), 61), ("close", (77, ), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def test_getuid(): def entry_point(argv): import os print "uid is %s" % os.getuid() print "euid is %s" % os.geteuid() print "gid is %s" % os.getgid() print "egid is %s" % os.getegid() return 0 exe = compile(entry_point) proc = SandboxedProcWithFiles([exe]) output, error = proc.communicate("") assert output == "uid is 1000\neuid is 1000\ngid is 1000\negid is 1000\n" assert error == ""
def test_foobar(): py.test.skip("to be updated") foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG) def entry_point(argv): s = rffi.str2charp(argv[1]); n = foobar(s); rffi.free_charp(s) s = rffi.str2charp(argv[n]); n = foobar(s); rffi.free_charp(s) return n exe = compile(entry_point) proc = MockSandboxedProc([exe, 'spam', 'egg'], expected = [ ("foobar", ("spam",), 2), ("foobar", ("egg",), 0), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def test_socketio(): class SocketProc(VirtualizedSocketProc, SimpleIOSandboxedProc): def build_virtual_root(self): pass def entry_point(argv): fd = os.open("tcp://codespeak.net:80", os.O_RDONLY, 0777) os.write(fd, 'GET /\n') print os.read(fd, 30) return 0 exe = compile(entry_point) proc = SocketProc([exe]) output, error = proc.communicate("") assert output.startswith('<!DOCTYPE')
def test_simpleio(): def entry_point(argv): print "Please enter a number:" buf = "" while True: t = os.read(0, 1) # 1 character from stdin if not t: raise EOFError if t == '\n': break buf += t num = int(buf) print "The double is:", num * 2 return 0 exe = compile(entry_point) proc = SimpleIOSandboxedProc([exe, 'x1', 'y2']) output, error = proc.communicate("21\n") assert output == "Please enter a number:\nThe double is: 42\n" assert error == ""
def test_foobar(): py.test.skip("to be updated") foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG) def entry_point(argv): s = rffi.str2charp(argv[1]) n = foobar(s) rffi.free_charp(s) s = rffi.str2charp(argv[n]) n = foobar(s) rffi.free_charp(s) return n exe = compile(entry_point) proc = MySandboxedProc([exe, 'spam', 'egg'], expected=[ ("foobar", ("spam", ), 2), ("foobar", ("egg", ), 0), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
print os.read(fd, 30) return 0 exe = compile(entry_point) proc = SocketProc([exe]) output, error = proc.communicate("") assert output.startswith('<!DOCTYPE') def test_oserror(): def entry_point(argv): try: os.open("/tmp/foobar", os.O_RDONLY, 0777) except OSError, e: os.close(e.errno) # nonsense, just to see outside return 0 exe = compile(entry_point) proc = MockSandboxedProc([exe], expected = [ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), OSError(-42, "baz")), ("close", (-42,), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected) class SandboxedProcWithFiles(VirtualizedSandboxedProc, SimpleIOSandboxedProc): """A sandboxed process with a simple virtualized filesystem. For testing file operations. """
fd = os.open("tcp://codespeak.net:80", os.O_RDONLY, 0777) os.write(fd, 'GET /\n') print os.read(fd, 30) return 0 exe = compile(entry_point) proc = SocketProc([exe]) output, error = proc.communicate("") assert output.startswith('<!DOCTYPE') def test_oserror(): def entry_point(argv): try: os.open("/tmp/foobar", os.O_RDONLY, 0777) except OSError, e: os.close(e.errno) # nonsense, just to see outside return 0 exe = compile(entry_point) proc = MySandboxedProc([exe], expected=[ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), OSError(-42, "baz")), ("close", (-42, ), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected)