22Test the win_runas util
33"""
44
5+ import os
6+ import tempfile
57from random import randint
68
79import pytest
@@ -42,8 +44,8 @@ def int_user():
4244 "cmd, expected" ,
4345 [
4446 ("hostname && whoami" , "username" ),
45- ("hostname && echo foo" , "foo" ),
46- ("hostname && python --version" , "Python" ),
47+ ("hostname & echo foo" , "foo" ),
48+ ("hostname & python --version" , "Python" ),
4749 ],
4850)
4951def test_compound_runas (user , cmd , expected ):
@@ -61,8 +63,8 @@ def test_compound_runas(user, cmd, expected):
6163 "cmd, expected" ,
6264 [
6365 ("hostname && whoami" , "username" ),
64- ("hostname && echo foo" , "foo" ),
65- ("hostname && python --version" , "Python" ),
66+ ("hostname & echo foo" , "foo" ),
67+ ("hostname & python --version" , "Python" ),
6668 ],
6769)
6870def test_compound_runas_unpriv (user , cmd , expected ):
@@ -76,6 +78,44 @@ def test_compound_runas_unpriv(user, cmd, expected):
7678 assert expected in result ["stdout" ]
7779
7880
81+ def test_runas_cd_ampersand_dir (user ):
82+ """
83+ ``cd /d ... & dir`` must run both parts in the same cmd /c line under runas
84+ (regression for CreateProcessWithTokenW command-line parsing).
85+ """
86+ with tempfile .TemporaryDirectory () as tmpdir :
87+ marker = "salt_runas_cd_dir_marker.txt"
88+ marker_path = os .path .join (tmpdir , marker )
89+ with open (marker_path , "w" , encoding = "utf-8" ) as f :
90+ f .write ("x" )
91+ inner = f'cd /d "{ tmpdir } " & dir /b'
92+ result = win_runas .runas (
93+ cmd = salt .platform .win .prepend_cmd ("cmd" , inner ),
94+ username = user .username ,
95+ password = user .password ,
96+ )
97+ assert result ["retcode" ] == 0 , result
98+ lines = [line .strip () for line in result ["stdout" ].splitlines () if line .strip ()]
99+ assert marker in lines , (result ["stdout" ], lines )
100+
101+
102+ def test_runas_unpriv_cd_ampersand_dir (user ):
103+ with tempfile .TemporaryDirectory () as tmpdir :
104+ marker = "salt_runas_cd_dir_marker_unpriv.txt"
105+ marker_path = os .path .join (tmpdir , marker )
106+ with open (marker_path , "w" , encoding = "utf-8" ) as f :
107+ f .write ("x" )
108+ inner = f'cd /d "{ tmpdir } " & dir /b'
109+ result = win_runas .runas_unpriv (
110+ cmd = salt .platform .win .prepend_cmd ("cmd" , inner ),
111+ username = user .username ,
112+ password = user .password ,
113+ )
114+ assert result ["retcode" ] == 0 , result
115+ lines = [line .strip () for line in result ["stdout" ].splitlines () if line .strip ()]
116+ assert marker in lines , (result ["stdout" ], lines )
117+
118+
79119def test_runas_str_user (user ):
80120 result = win_runas .runas (
81121 cmd = "whoami" , username = user .username , password = user .password
0 commit comments