Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 26a5702

Browse files
committed
Merge pull request #2631 from stephentoub/net_add_user_error
Fix TestUserCredentialsPropertiesOnWindows without admin privileges
2 parents 37b0abb + fff0947 commit 26a5702

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/Interop.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public struct SID_AND_ATTRIBUTES
9393
[DllImport("advapi32.dll")]
9494
internal static extern bool GetTokenInformation(SafeProcessHandle TokenHandle, uint TokenInformationClass, IntPtr TokenInformation, int TokenInformationLength, ref int ReturnLength);
9595

96-
internal static bool NetUserAdd(string username, string password)
96+
internal static void NetUserAdd(string username, string password)
9797
{
9898
USER_INFO_1 userInfo = new USER_INFO_1();
9999
userInfo.usri1_name = username;
@@ -103,12 +103,12 @@ internal static bool NetUserAdd(string username, string password)
103103
uint parm_err;
104104
uint result = NetUserAdd(null, 1, ref userInfo, out parm_err);
105105

106-
if (result != 0)
106+
if (result != 0) // NERR_Success
107107
{
108-
throw new Win32Exception();
108+
// most likely result == ERROR_ACCESS_DENIED
109+
// due to running without elevated privileges
110+
throw new Win32Exception((int)result);
109111
}
110-
111-
return true;
112112
}
113113

114114
internal static bool ProcessTokenToSid(SafeProcessHandle token, out SecurityIdentifier sid)

src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/ProcessStartInfoTests.cs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -230,47 +230,53 @@ public void TestCreateNoWindowProperty(bool value)
230230
public void TestUserCredentialsPropertiesOnWindows()
231231
{
232232
string username = "test", password = "PassWord123!!";
233-
234-
if (Interop.NetUserAdd(username, password))
233+
try
234+
{
235+
Interop.NetUserAdd(username, password);
236+
}
237+
catch (Exception exc)
235238
{
236-
Process p = CreateProcessInfinite();
239+
Console.Error.WriteLine("TestUserCredentialsPropertiesOnWindows: NetUserAdd failed: {0}", exc.Message);
240+
return; // test is irrelevant if we can't add a user
241+
}
242+
243+
Process p = CreateProcessInfinite();
237244

238-
p.StartInfo.LoadUserProfile = true;
239-
p.StartInfo.UserName = username;
240-
p.StartInfo.Password = GetSecureString(password);
245+
p.StartInfo.LoadUserProfile = true;
246+
p.StartInfo.UserName = username;
247+
p.StartInfo.Password = GetSecureString(password);
241248

242-
SafeProcessHandle handle = null;
243-
try
249+
SafeProcessHandle handle = null;
250+
try
251+
{
252+
p.Start();
253+
if (Interop.OpenProcessToken(p.SafeHandle, 0x8u, out handle))
244254
{
245-
p.Start();
246-
if (Interop.OpenProcessToken(p.SafeHandle, 0x8u, out handle))
255+
SecurityIdentifier sid;
256+
if (Interop.ProcessTokenToSid(handle, out sid))
247257
{
248-
SecurityIdentifier sid;
249-
if (Interop.ProcessTokenToSid(handle, out sid))
250-
{
251-
string actualUserName = sid.Translate(typeof(NTAccount)).ToString();
252-
int indexOfDomain = actualUserName.IndexOf('\\');
253-
if (indexOfDomain != -1)
254-
actualUserName = actualUserName.Substring(indexOfDomain + 1);
255-
256-
bool isProfileLoaded = GetNamesOfUserProfiles().Any(profile => profile.Equals(username));
257-
258-
Assert.Equal(username, actualUserName);
259-
Assert.True(isProfileLoaded);
260-
}
258+
string actualUserName = sid.Translate(typeof(NTAccount)).ToString();
259+
int indexOfDomain = actualUserName.IndexOf('\\');
260+
if (indexOfDomain != -1)
261+
actualUserName = actualUserName.Substring(indexOfDomain + 1);
262+
263+
bool isProfileLoaded = GetNamesOfUserProfiles().Any(profile => profile.Equals(username));
264+
265+
Assert.Equal(username, actualUserName);
266+
Assert.True(isProfileLoaded);
261267
}
262268
}
263-
finally
264-
{
265-
if (handle != null)
266-
handle.Dispose();
269+
}
270+
finally
271+
{
272+
if (handle != null)
273+
handle.Dispose();
267274

268-
if (!p.HasExited)
269-
p.Kill();
275+
if (!p.HasExited)
276+
p.Kill();
270277

271-
Interop.NetUserDel(null, username);
272-
Assert.True(p.WaitForExit(WaitInMS));
273-
}
278+
Interop.NetUserDel(null, username);
279+
Assert.True(p.WaitForExit(WaitInMS));
274280
}
275281
}
276282

0 commit comments

Comments
 (0)