summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_os.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-06-25 06:50:00 (GMT)
committerGitHub <noreply@github.com>2017-06-25 06:50:00 (GMT)
commit787826c9316b03ac8a197078ec1cdf98fa840c5c (patch)
tree7f56970b47897fd01034ebef783b11d13db39b84 /Lib/test/test_os.py
parent9dda2caca8edc7ff1285f6b0d1c5279b51854b7d (diff)
downloadcpython-787826c9316b03ac8a197078ec1cdf98fa840c5c.zip
cpython-787826c9316b03ac8a197078ec1cdf98fa840c5c.tar.gz
cpython-787826c9316b03ac8a197078ec1cdf98fa840c5c.tar.bz2
[2.7] bpo-30746: Prohibited the '=' character in environment variable names (GH-2382) (#2393)
in `os.putenv()` and `os.spawn*()`.. (cherry picked from commit 77703942c5997dff00c48f10df1b29b11645624c)
Diffstat (limited to 'Lib/test/test_os.py')
-rw-r--r--Lib/test/test_os.py84
1 files changed, 81 insertions, 3 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index b70e285..aca03fd 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -599,11 +599,32 @@ class URandomFDTests(unittest.TestCase):
assert_python_ok('-c', code)
-class ExecvpeTests(unittest.TestCase):
+class ExecTests(unittest.TestCase):
def test_execvpe_with_bad_arglist(self):
self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
+ def test_execve_invalid_env(self):
+ args = [sys.executable, '-c', 'pass']
+
+ # null character in the enviroment variable name
+ newenv = os.environ.copy()
+ newenv["FRUIT\0VEGETABLE"] = "cabbage"
+ with self.assertRaises(TypeError):
+ os.execve(args[0], args, newenv)
+
+ # null character in the enviroment variable value
+ newenv = os.environ.copy()
+ newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
+ with self.assertRaises(TypeError):
+ os.execve(args[0], args, newenv)
+
+ # equal character in the enviroment variable name
+ newenv = os.environ.copy()
+ newenv["FRUIT=ORANGE"] = "lemon"
+ with self.assertRaises(ValueError):
+ os.execve(args[0], args, newenv)
+
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
class Win32ErrorTests(unittest.TestCase):
@@ -879,6 +900,62 @@ class Win32KillTests(unittest.TestCase):
self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
+class SpawnTests(unittest.TestCase):
+ def _test_invalid_env(self, spawn):
+ args = [sys.executable, '-c', 'pass']
+
+ # null character in the enviroment variable name
+ newenv = os.environ.copy()
+ newenv["FRUIT\0VEGETABLE"] = "cabbage"
+ try:
+ exitcode = spawn(os.P_WAIT, args[0], args, newenv)
+ except TypeError:
+ pass
+ else:
+ self.assertEqual(exitcode, 127)
+
+ # null character in the enviroment variable value
+ newenv = os.environ.copy()
+ newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
+ try:
+ exitcode = spawn(os.P_WAIT, args[0], args, newenv)
+ except TypeError:
+ pass
+ else:
+ self.assertEqual(exitcode, 127)
+
+ # equal character in the enviroment variable name
+ newenv = os.environ.copy()
+ newenv["FRUIT=ORANGE"] = "lemon"
+ try:
+ exitcode = spawn(os.P_WAIT, args[0], args, newenv)
+ except ValueError:
+ pass
+ else:
+ self.assertEqual(exitcode, 127)
+
+ # equal character in the enviroment variable value
+ filename = test_support.TESTFN
+ self.addCleanup(test_support.unlink, filename)
+ with open(filename, "w") as fp:
+ fp.write('import sys, os\n'
+ 'if os.getenv("FRUIT") != "orange=lemon":\n'
+ ' raise AssertionError')
+ args = [sys.executable, filename]
+ newenv = os.environ.copy()
+ newenv["FRUIT"] = "orange=lemon"
+ exitcode = spawn(os.P_WAIT, args[0], args, newenv)
+ self.assertEqual(exitcode, 0)
+
+ @unittest.skipUnless(hasattr(os, 'spawnve'), 'test needs os.spawnve()')
+ def test_spawnve_invalid_env(self):
+ self._test_invalid_env(os.spawnve)
+
+ @unittest.skipUnless(hasattr(os, 'spawnvpe'), 'test needs os.spawnvpe()')
+ def test_spawnvpe_invalid_env(self):
+ self._test_invalid_env(os.spawnvpe)
+
+
def test_main():
test_support.run_unittest(
FileTests,
@@ -890,11 +967,12 @@ def test_main():
DevNullTests,
URandomTests,
URandomFDTests,
- ExecvpeTests,
+ ExecTests,
Win32ErrorTests,
TestInvalidFD,
PosixUidGidTests,
- Win32KillTests
+ Win32KillTests,
+ SpawnTests,
)
if __name__ == "__main__":