@@ -385,6 +385,46 @@ def test_env(self):
385385 self .addCleanup (p .stdout .close )
386386 self .assertEqual (p .stdout .read (), "orange" )
387387
388+ def test_invalid_cmd (self ):
389+ # null character in the command name
390+ cmd = sys .executable + '\0 '
391+ with self .assertRaises (TypeError ):
392+ subprocess .Popen ([cmd , "-c" , "pass" ])
393+
394+ # null character in the command argument
395+ with self .assertRaises (TypeError ):
396+ subprocess .Popen ([sys .executable , "-c" , "pass#\0 " ])
397+
398+ def test_invalid_env (self ):
399+ # null character in the enviroment variable name
400+ newenv = os .environ .copy ()
401+ newenv ["FRUIT\0 VEGETABLE" ] = "cabbage"
402+ with self .assertRaises (TypeError ):
403+ subprocess .Popen ([sys .executable , "-c" , "pass" ], env = newenv )
404+
405+ # null character in the enviroment variable value
406+ newenv = os .environ .copy ()
407+ newenv ["FRUIT" ] = "orange\0 VEGETABLE=cabbage"
408+ with self .assertRaises (TypeError ):
409+ subprocess .Popen ([sys .executable , "-c" , "pass" ], env = newenv )
410+
411+ # equal character in the enviroment variable name
412+ newenv = os .environ .copy ()
413+ newenv ["FRUIT=ORANGE" ] = "lemon"
414+ with self .assertRaises (ValueError ):
415+ subprocess .Popen ([sys .executable , "-c" , "pass" ], env = newenv )
416+
417+ # equal character in the enviroment variable value
418+ newenv = os .environ .copy ()
419+ newenv ["FRUIT" ] = "orange=lemon"
420+ p = subprocess .Popen ([sys .executable , "-c" ,
421+ 'import sys, os;'
422+ 'sys.stdout.write(os.getenv("FRUIT"))' ],
423+ stdout = subprocess .PIPE ,
424+ env = newenv )
425+ stdout , stderr = p .communicate ()
426+ self .assertEqual (stdout , "orange=lemon" )
427+
388428 def test_communicate_stdin (self ):
389429 p = subprocess .Popen ([sys .executable , "-c" ,
390430 'import sys;'
0 commit comments