bpo-30746: Port more tests for os.spawnvpe() and os.execve() from 2.7. (#2394)

This commit is contained in:
Serhiy Storchaka 2017-06-25 09:48:54 +03:00 committed by GitHub
parent 77703942c5
commit af5392f5c6
1 changed files with 36 additions and 9 deletions

View File

@ -1571,6 +1571,27 @@ class ExecTests(unittest.TestCase):
if os.name != "nt": if os.name != "nt":
self._test_internal_execvpe(bytes) self._test_internal_execvpe(bytes)
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(ValueError):
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(ValueError):
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") @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
class Win32ErrorTests(unittest.TestCase): class Win32ErrorTests(unittest.TestCase):
@ -2382,36 +2403,34 @@ class SpawnTests(unittest.TestCase):
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], ('',), {}) self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], ('',), {})
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [''], {}) self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [''], {})
@requires_os_func('spawnve') def _test_invalid_env(self, spawn):
def test_spawnve_invalid_env(self):
# null character in the enviroment variable name
args = [sys.executable, '-c', 'pass'] args = [sys.executable, '-c', 'pass']
# null character in the enviroment variable name
newenv = os.environ.copy() newenv = os.environ.copy()
newenv["FRUIT\0VEGETABLE"] = "cabbage" newenv["FRUIT\0VEGETABLE"] = "cabbage"
try: try:
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv) exitcode = spawn(os.P_WAIT, args[0], args, newenv)
except ValueError: except ValueError:
pass pass
else: else:
self.assertEqual(exitcode, 127) self.assertEqual(exitcode, 127)
# null character in the enviroment variable value # null character in the enviroment variable value
args = [sys.executable, '-c', 'pass']
newenv = os.environ.copy() newenv = os.environ.copy()
newenv["FRUIT"] = "orange\0VEGETABLE=cabbage" newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
try: try:
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv) exitcode = spawn(os.P_WAIT, args[0], args, newenv)
except ValueError: except ValueError:
pass pass
else: else:
self.assertEqual(exitcode, 127) self.assertEqual(exitcode, 127)
# equal character in the enviroment variable name # equal character in the enviroment variable name
args = [sys.executable, '-c', 'pass']
newenv = os.environ.copy() newenv = os.environ.copy()
newenv["FRUIT=ORANGE"] = "lemon" newenv["FRUIT=ORANGE"] = "lemon"
try: try:
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv) exitcode = spawn(os.P_WAIT, args[0], args, newenv)
except ValueError: except ValueError:
pass pass
else: else:
@ -2427,9 +2446,17 @@ class SpawnTests(unittest.TestCase):
args = [sys.executable, filename] args = [sys.executable, filename]
newenv = os.environ.copy() newenv = os.environ.copy()
newenv["FRUIT"] = "orange=lemon" newenv["FRUIT"] = "orange=lemon"
exitcode = os.spawnve(os.P_WAIT, args[0], args, newenv) exitcode = spawn(os.P_WAIT, args[0], args, newenv)
self.assertEqual(exitcode, 0) self.assertEqual(exitcode, 0)
@requires_os_func('spawnve')
def test_spawnve_invalid_env(self):
self._test_invalid_env(os.spawnve)
@requires_os_func('spawnvpe')
def test_spawnvpe_invalid_env(self):
self._test_invalid_env(os.spawnvpe)
# The introduction of this TestCase caused at least two different errors on # The introduction of this TestCase caused at least two different errors on
# *nix buildbots. Temporarily skip this to let the buildbots move along. # *nix buildbots. Temporarily skip this to let the buildbots move along.