[pypy-commit] pypy nedbat-sandbox: A new test: try opening too many files in the sandbox, see that you can't

ned noreply at buildbot.pypy.org
Sun Dec 4 22:06:00 CET 2011


Author: Ned Batchelder <ned at nedbatchelder.com>
Branch: nedbat-sandbox
Changeset: r50155:6f823f8ecd00
Date: 2011-12-03 22:23 -0500
http://bitbucket.org/pypy/pypy/changeset/6f823f8ecd00/

Log:	A new test: try opening too many files in the sandbox, see that you
	can't

diff --git a/pypy/translator/sandbox/test/test_sandlib.py b/pypy/translator/sandbox/test/test_sandlib.py
--- a/pypy/translator/sandbox/test/test_sandlib.py
+++ b/pypy/translator/sandbox/test/test_sandlib.py
@@ -1,14 +1,17 @@
 import py
-import os, StringIO
+import errno, os, StringIO
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rpython.lltypesystem import rffi
 from pypy.translator.sandbox.sandlib import SandboxedProc
 from pypy.translator.sandbox.sandlib import SimpleIOSandboxedProc
+from pypy.translator.sandbox.sandlib import VirtualizedSandboxedProc
 from pypy.translator.sandbox.sandlib import VirtualizedSocketProc
 from pypy.translator.sandbox.test.test_sandbox import compile
+from pypy.translator.sandbox.vfs import Dir, File
 
 
-class MySandboxedProc(SandboxedProc):
+class MockSandboxedProc(SandboxedProc):
+    """A sandbox process wrapper that replays expected syscalls."""
 
     def __init__(self, args, expected):
         SandboxedProc.__init__(self, args)
@@ -48,7 +51,7 @@
         return 0
     exe = compile(entry_point)
 
-    proc = MySandboxedProc([exe, 'x1', 'y2'], expected = [
+    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),
@@ -69,7 +72,7 @@
         return n
     exe = compile(entry_point)
 
-    proc = MySandboxedProc([exe, 'spam', 'egg'], expected = [
+    proc = MockSandboxedProc([exe, 'spam', 'egg'], expected = [
         ("foobar", ("spam",), 2),
         ("foobar", ("egg",), 0),
         ])
@@ -122,9 +125,45 @@
         return 0
     exe = compile(entry_point)
 
-    proc = MySandboxedProc([exe], expected = [
+    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.
+
+    """
+    def build_virtual_root(self):
+        return Dir({
+            'hi.txt': File("Hello, world!\n"),
+             })
+
+def test_too_many_opens():
+    def entry_point(argv):
+        try:
+            open_files = []
+            for i in range(500):
+                fd = os.open('/hi.txt', os.O_RDONLY, 0777)
+                open_files.append(fd)
+                txt = os.read(fd, 100)
+                if txt != "Hello, world!\n":
+                    print "Wrong content: %s" % txt
+        except OSError, e:
+            if e.errno != errno.EMFILE:
+                print "OSError: %s!" % (e.errno,)
+        else:
+            print "We opened 500 files! Shouldn't have been able to."
+        print "All ok!"
+        return 0
+    exe = compile(entry_point)
+
+    proc = SandboxedProcWithFiles([exe])
+    output, error = proc.communicate("")
+    assert output == "All ok!\n"
+    assert error == ""


More information about the pypy-commit mailing list