@@ -588,6 +588,46 @@ def test_empty_env(self):
588588 # environment
589589 b"['__CF_USER_TEXT_ENCODING']" ))
590590
591+ def test_invalid_cmd (self ):
592+ # null character in the command name
593+ cmd = sys .executable + '\0 '
594+ with self .assertRaises ((ValueError , TypeError )):
595+ subprocess .Popen ([cmd , "-c" , "pass" ])
596+
597+ # null character in the command argument
598+ with self .assertRaises ((ValueError , TypeError )):
599+ subprocess .Popen ([sys .executable , "-c" , "pass#\0 " ])
600+
601+ def test_invalid_env (self ):
602+ # null character in the enviroment variable name
603+ newenv = os .environ .copy ()
604+ newenv ["FRUIT\0 VEGETABLE" ] = "cabbage"
605+ with self .assertRaises ((ValueError , TypeError )):
606+ subprocess .Popen ([sys .executable , "-c" , "pass" ], env = newenv )
607+
608+ # null character in the enviroment variable value
609+ newenv = os .environ .copy ()
610+ newenv ["FRUIT" ] = "orange\0 VEGETABLE=cabbage"
611+ with self .assertRaises ((ValueError , TypeError )):
612+ subprocess .Popen ([sys .executable , "-c" , "pass" ], env = newenv )
613+
614+ # equal character in the enviroment variable name
615+ newenv = os .environ .copy ()
616+ newenv ["FRUIT=ORANGE" ] = "lemon"
617+ with self .assertRaises (ValueError ):
618+ subprocess .Popen ([sys .executable , "-c" , "pass" ], env = newenv )
619+
620+ # equal character in the enviroment variable value
621+ newenv = os .environ .copy ()
622+ newenv ["FRUIT" ] = "orange=lemon"
623+ with subprocess .Popen ([sys .executable , "-c" ,
624+ 'import sys, os;'
625+ 'sys.stdout.write(os.getenv("FRUIT"))' ],
626+ stdout = subprocess .PIPE ,
627+ env = newenv ) as p :
628+ stdout , stderr = p .communicate ()
629+ self .assertEqual (stdout , b"orange=lemon" )
630+
591631 def test_communicate_stdin (self ):
592632 p = subprocess .Popen ([sys .executable , "-c" ,
593633 'import sys;'
0 commit comments