[pypy-svn] r46417 - in pypy/dist/pypy/translator/sandbox: . test

arigo at codespeak.net arigo at codespeak.net
Sat Sep 8 11:30:32 CEST 2007


Author: arigo
Date: Sat Sep  8 11:30:32 2007
New Revision: 46417

Added:
   pypy/dist/pypy/translator/sandbox/test/test_pypy_interact.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/sandbox/pypy_interact.py
   pypy/dist/pypy/translator/sandbox/sandlib.py
Log:
A test for pypy_interact.py, emulating the I/O calls pypy does at start-up.


Modified: pypy/dist/pypy/translator/sandbox/pypy_interact.py
==============================================================================
--- pypy/dist/pypy/translator/sandbox/pypy_interact.py	(original)
+++ pypy/dist/pypy/translator/sandbox/pypy_interact.py	Sat Sep  8 11:30:32 2007
@@ -32,7 +32,7 @@
     virtual_env = {}
     virtual_console_isatty = True
 
-    def __init__(self, executable, arguments, tmpdir):
+    def __init__(self, executable, arguments, tmpdir=None):
         self.executable = executable = os.path.abspath(executable)
         self.tmpdir = tmpdir
         super(PyPySandboxedProc, self).__init__([self.argv0] + arguments,

Modified: pypy/dist/pypy/translator/sandbox/sandlib.py
==============================================================================
--- pypy/dist/pypy/translator/sandbox/sandlib.py	(original)
+++ pypy/dist/pypy/translator/sandbox/sandlib.py	Sat Sep  8 11:30:32 2007
@@ -314,6 +314,7 @@
         self._input = None
         self._output = None
         self._error = None
+        return returncode
 
     def do_ll_os__ll_os_read(self, fd, size):
         if fd == 0:

Added: pypy/dist/pypy/translator/sandbox/test/test_pypy_interact.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/sandbox/test/test_pypy_interact.py	Sat Sep  8 11:30:32 2007
@@ -0,0 +1,75 @@
+import autopath
+import os, sys, stat, errno
+from pypy.translator.sandbox.pypy_interact import PyPySandboxedProc
+from pypy.translator.interactive import Translation
+
+SITE_PY_CONTENT = open(os.path.join(autopath.pypydir, os.pardir,
+                                    'lib-python', 'modified-2.4.1', 'site.py'),
+                       'rb').read()
+ERROR_TEXT = os.strerror(errno.ENOENT)
+
+def assert_(cond, text):
+    if not cond:
+        print "assert failed:", text
+        raise AssertionError
+
+def mini_pypy_like_entry_point(argv):
+    """An RPython standalone executable that does the same kind of I/O as
+    PyPy when it starts up.
+    """
+    assert_(len(argv) == 3, "expected len(argv) == 3")
+    assert_(argv[1] == 'foo', "bad argv[1]")
+    assert_(argv[2] == 'bar', "bad argv[2]")
+    env = os.environ.items()
+    assert_(len(env) == 0, "empty environment expected")
+    assert_(argv[0] == '/bin/pypy-c', "bad argv[0]")
+    st = os.lstat('/bin/pypy-c')
+    assert_(stat.S_ISREG(st.st_mode), "bad st_mode for /bin/pypy-c")
+    for dirname in ['/bin/lib-python/2.4.1', '/bin/pypy/lib']:
+        st = os.stat(dirname)
+        assert_(stat.S_ISDIR(st.st_mode), "bad st_mode for " + dirname)
+    assert_(os.environ.get('PYTHONPATH') is None, "unexpected $PYTHONPATH")
+    try:
+        os.stat('site')
+    except OSError:
+        pass
+    else:
+        assert_(False, "os.stat('site') should have failed")
+    st = os.stat('/bin/lib-python/modified-2.4.1/site.py')
+    assert_(stat.S_ISREG(st.st_mode), "bad st_mode for .../site.py")
+    fd = os.open('/bin/lib-python/modified-2.4.1/site.py', os.O_RDONLY, 0666)
+    length = 8192
+    ofs = 0
+    while True:
+        data = os.read(fd, length)
+        if not data: break
+        end = ofs+length
+        if end > len(SITE_PY_CONTENT):
+            end = len(SITE_PY_CONTENT)
+        assert_(data == SITE_PY_CONTENT[ofs:end], "bad data from site.py")
+        ofs = end
+    os.close(fd)
+    assert_(ofs == len(SITE_PY_CONTENT), "not enough data from site.py")
+    assert_(os.getcwd() == '/tmp', "bad cwd")
+    assert_(os.strerror(errno.ENOENT) == ERROR_TEXT, "bad strerror(ENOENT)")
+    assert_(os.isatty(0), "isatty(0) returned False")
+    # an obvious 'attack'
+    try:
+        os.open('/spam', os.O_RDWR | os.O_CREAT, 0666)
+    except OSError:
+        pass
+    else:
+        assert_(False, "os.open('/spam') should have failed")
+    return 0
+
+
+def setup_module(mod):
+    t = Translation(mini_pypy_like_entry_point, backend='c',
+                    standalone=True, sandbox=True)
+    mod.executable = t.compile()
+
+
+def test_run():
+    sandproc = PyPySandboxedProc(executable, ['foo', 'bar'])
+    returncode = sandproc.interact()
+    assert returncode == 0



More information about the Pypy-commit mailing list