Ticket #815: process_815_2.py

File process_815_2.py, 2.8 KB (added by therve, 15 years ago)
Line 
1Index: twisted/internet/process.py
2===================================================================
3--- twisted/internet/process.py (revision 18372)
4+++ twisted/internet/process.py (working copy)
5@@ -266,6 +266,23 @@
6         nuances of setXXuid on UNIX: it will assume that either your effective
7         or real UID is 0.)
8         """
9+        # Common check function
10+        def argChecker(arg):
11+            return isinstance(arg, (str, unicode)) and '\0' not in arg
12+
13+        # Make a few tests to check input validity
14+        if not isinstance(args, (tuple, list)):
15+            raise TypeError("Arguments must be a tuple or list")
16+        for arg in args:
17+            if not argChecker(arg):
18+               raise TypeError("Arguments contain a non-string value")
19+        if environment is not None:
20+            for key, val in environment.items():
21+                if not argChecker(key):
22+                    raise TypeError("Environment contains a non-string key")
23+                if not argChecker(val):
24+                    raise TypeError("Environment contains a non-string value")
25+
26         if not proto:
27             assert 'r' not in childFDs.values()
28             assert 'w' not in childFDs.values()
29Index: twisted/test/test_process.py
30===================================================================
31--- twisted/test/test_process.py        (revision 18372)
32+++ twisted/test/test_process.py        (working copy)
33@@ -288,6 +288,30 @@
34             self.assertEquals(recvdArgs, args)
35         return d.addCallback(processEnded)
36 
37+    def testWrongArguments(self):
38+        """
39+        Test invalid arguments to spawnProcess: arguments and environment must
40+        only contains string or unicode, and not null bytes.
41+        """
42+        exe = sys.executable
43+        scriptPath = util.sibpath(__file__, "process_tester.py")
44+        d = defer.Deferred()
45+        p = TestProcessProtocol()
46+        p.deferred = d
47+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
48+                          [exe, "-u", scriptPath], env={"foo": 2})
49+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
50+                          [exe, "-u", scriptPath], env={"foo": "egg\0a"})
51+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
52+                          [exe, "-u", scriptPath], env={3: "bar"})
53+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
54+                          [exe, "-u", scriptPath], env={"bar\0foo": "bar"})
55+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
56+                          [exe, 2], env=None)
57+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
58+                          "spam", env=None)
59+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
60+                          [exe, "\0"], env=None)
61 
62 class TwoProcessProtocol(protocol.ProcessProtocol):
63     num = -1