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

Commit bca11a0

Browse files
committed
Fix Process Start tests in outerloop on Unix.
Get the real user name in the test process, not the child process. The outerloop tests are run with 'sudo'. Which means by the time we are in the child process SUDO_USER will be set to 'root', since the parent test process was run with 'sudo'. Fixes #26675
1 parent ffa5db3 commit bca11a0

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public void TestStartWithNonExistingUserThrows()
319319
[Fact]
320320
public void TestStartWithNormalUser()
321321
{
322-
TestStartWithUserName();
322+
TestStartWithUserName(GetCurrentRealUserName());
323323
}
324324

325325
/// <summary>
@@ -331,26 +331,16 @@ public void TestStartWithNormalUser()
331331
[Trait(XunitConstants.Category, XunitConstants.RequiresElevation)]
332332
public void TestStartWithRootUser()
333333
{
334-
RunTestAsSudo((Func<int>)TestStartWithUserName);
334+
RunTestAsSudo(TestStartWithUserName, GetCurrentRealUserName());
335335
}
336336

337-
public static int TestStartWithUserName()
337+
public static int TestStartWithUserName(string realUserName)
338338
{
339+
Assert.NotNull(realUserName);
340+
Assert.NotEqual("root", realUserName);
341+
339342
using (ProcessTests testObject = new ProcessTests())
340343
{
341-
string realUserName;
342-
if (geteuid() == 0)
343-
{
344-
realUserName = Environment.GetEnvironmentVariable("SUDO_USER");
345-
}
346-
else
347-
{
348-
realUserName = Environment.UserName;
349-
}
350-
351-
Assert.NotNull(realUserName);
352-
Assert.NotEqual("root", realUserName);
353-
354344
using (Process p = testObject.CreateProcessPortable(GetCurrentEffectiveUserId))
355345
{
356346
p.StartInfo.UserName = realUserName;
@@ -374,6 +364,18 @@ public static int GetCurrentEffectiveUserId()
374364
return (int)geteuid();
375365
}
376366

367+
private static string GetCurrentRealUserName()
368+
{
369+
string realUserName = geteuid() == 0 ?
370+
Environment.GetEnvironmentVariable("SUDO_USER") :
371+
Environment.UserName;
372+
373+
Assert.NotNull(realUserName);
374+
Assert.NotEqual("root", realUserName);
375+
376+
return realUserName;
377+
}
378+
377379
/// <summary>
378380
/// Tests when running as root and starting a new process as a normal user,
379381
/// the new process can't elevate back to root.
@@ -383,18 +385,16 @@ public static int GetCurrentEffectiveUserId()
383385
[Trait(XunitConstants.Category, XunitConstants.RequiresElevation)]
384386
public void TestStartWithRootUserCannotElevate()
385387
{
386-
RunTestAsSudo((Func<int>)TestStartWithUserNameCannotElevate);
388+
RunTestAsSudo(TestStartWithUserNameCannotElevate, GetCurrentRealUserName());
387389
}
388390

389-
public static int TestStartWithUserNameCannotElevate()
391+
public static int TestStartWithUserNameCannotElevate(string realUserName)
390392
{
393+
Assert.NotNull(realUserName);
394+
Assert.NotEqual("root", realUserName);
395+
391396
using (ProcessTests testObject = new ProcessTests())
392397
{
393-
// This test should only be called with sudo
394-
string realUserName = Environment.GetEnvironmentVariable("SUDO_USER");
395-
Assert.NotNull(realUserName);
396-
Assert.NotEqual("root", realUserName);
397-
398398
using (Process p = testObject.CreateProcessPortable(SetEffectiveUserIdToRoot))
399399
{
400400
p.StartInfo.UserName = realUserName;
@@ -415,14 +415,14 @@ public static int SetEffectiveUserIdToRoot()
415415
return seteuid(0);
416416
}
417417

418-
private void RunTestAsSudo(Func<int> testMethod)
418+
private void RunTestAsSudo(Func<string, int> testMethod, string arg)
419419
{
420420
RemoteInvokeOptions options = new RemoteInvokeOptions()
421421
{
422422
Start = false,
423423
RunAsSudo = true
424424
};
425-
Process p = RemoteInvoke(testMethod, options).Process;
425+
Process p = RemoteInvoke(testMethod, arg, options).Process;
426426
AddProcessForDispose(p);
427427

428428
p.Start();

0 commit comments

Comments
 (0)