From fda6505eb02ddcbfd16880858901611d0ea7a68b Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 8 Feb 2018 21:02:42 -0800 Subject: [PATCH] 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 dotnet/corefx#26675 Commit migrated from https://github.com/dotnet/corefx/commit/bca11a079c914ed971b2f300b876f89a920a0398 --- .../tests/ProcessTests.Unix.cs | 50 +++++++++++----------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 5784943..849d94a 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -319,7 +319,7 @@ namespace System.Diagnostics.Tests [Fact] public void TestStartWithNormalUser() { - TestStartWithUserName(); + TestStartWithUserName(GetCurrentRealUserName()); } /// @@ -331,26 +331,16 @@ namespace System.Diagnostics.Tests [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] public void TestStartWithRootUser() { - RunTestAsSudo((Func)TestStartWithUserName); + RunTestAsSudo(TestStartWithUserName, GetCurrentRealUserName()); } - public static int TestStartWithUserName() + public static int TestStartWithUserName(string realUserName) { + Assert.NotNull(realUserName); + Assert.NotEqual("root", realUserName); + using (ProcessTests testObject = new ProcessTests()) { - string realUserName; - if (geteuid() == 0) - { - realUserName = Environment.GetEnvironmentVariable("SUDO_USER"); - } - else - { - realUserName = Environment.UserName; - } - - Assert.NotNull(realUserName); - Assert.NotEqual("root", realUserName); - using (Process p = testObject.CreateProcessPortable(GetCurrentEffectiveUserId)) { p.StartInfo.UserName = realUserName; @@ -374,6 +364,18 @@ namespace System.Diagnostics.Tests return (int)geteuid(); } + private static string GetCurrentRealUserName() + { + string realUserName = geteuid() == 0 ? + Environment.GetEnvironmentVariable("SUDO_USER") : + Environment.UserName; + + Assert.NotNull(realUserName); + Assert.NotEqual("root", realUserName); + + return realUserName; + } + /// /// Tests when running as root and starting a new process as a normal user, /// the new process can't elevate back to root. @@ -383,18 +385,16 @@ namespace System.Diagnostics.Tests [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] public void TestStartWithRootUserCannotElevate() { - RunTestAsSudo((Func)TestStartWithUserNameCannotElevate); + RunTestAsSudo(TestStartWithUserNameCannotElevate, GetCurrentRealUserName()); } - public static int TestStartWithUserNameCannotElevate() + public static int TestStartWithUserNameCannotElevate(string realUserName) { + Assert.NotNull(realUserName); + Assert.NotEqual("root", realUserName); + using (ProcessTests testObject = new ProcessTests()) { - // This test should only be called with sudo - string realUserName = Environment.GetEnvironmentVariable("SUDO_USER"); - Assert.NotNull(realUserName); - Assert.NotEqual("root", realUserName); - using (Process p = testObject.CreateProcessPortable(SetEffectiveUserIdToRoot)) { p.StartInfo.UserName = realUserName; @@ -415,14 +415,14 @@ namespace System.Diagnostics.Tests return seteuid(0); } - private void RunTestAsSudo(Func testMethod) + private void RunTestAsSudo(Func testMethod, string arg) { RemoteInvokeOptions options = new RemoteInvokeOptions() { Start = false, RunAsSudo = true }; - Process p = RemoteInvoke(testMethod, options).Process; + Process p = RemoteInvoke(testMethod, arg, options).Process; AddProcessForDispose(p); p.Start(); -- 2.7.4