From a4429f5525f338ba4cf3cb33cf1b1707f3e80078 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 23 May 2017 08:51:48 -0700 Subject: [PATCH] Sudo fix (#20080) (#20119) --- .../tests/System/PlatformDetection.Unix.cs | 7 +- .../tests/ProcessTests.Unix.cs | 107 +++++++++++++++++- .../tests/ProcessTests.cs | 106 ----------------- .../System.Diagnostics.Process.Tests.csproj | 3 + .../tests/System.Globalization.Tests.csproj | 1 + 5 files changed, 115 insertions(+), 109 deletions(-) diff --git a/src/Common/tests/System/PlatformDetection.Unix.cs b/src/Common/tests/System/PlatformDetection.Unix.cs index 52a03cc43905..d55f8fb12ab3 100644 --- a/src/Common/tests/System/PlatformDetection.Unix.cs +++ b/src/Common/tests/System/PlatformDetection.Unix.cs @@ -30,5 +30,10 @@ private static Version GetOSXKernelVersion() [DllImport("libc", SetLastError = true)] private static extern int sysctlbyname(string ctlName, byte[] oldp, ref IntPtr oldpLen, byte[] newp, IntPtr newpLen); + + [DllImport("libc", SetLastError = true)] + internal static extern unsafe uint geteuid(); + + public static bool IsSuperUser => geteuid() == 0; } -} +} \ No newline at end of file diff --git a/src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index 323cba3f4d6f..4099e474d5d2 100644 --- a/src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -17,7 +17,111 @@ namespace System.Diagnostics.Tests public partial class ProcessTests : ProcessTestBase { [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes to set permissions + private void TestWindowApisUnix() + { + // This tests the hardcoded implementations of these APIs on Unix. + using (Process p = Process.GetCurrentProcess()) + { + Assert.True(p.Responding); + Assert.Equal(string.Empty, p.MainWindowTitle); + Assert.False(p.CloseMainWindow()); + Assert.Throws(()=>p.WaitForInputIdle()); + } + } + + [Fact] + public void MainWindowHandle_GetUnix_ThrowsPlatformNotSupportedException() + { + CreateDefaultProcess(); + + Assert.Throws(() => _process.MainWindowHandle); + } + + [Fact] + public void TestProcessOnRemoteMachineUnix() + { + Process currentProcess = Process.GetCurrentProcess(); + + Assert.Throws(() => Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1")); + Assert.Throws(() => Process.GetProcessById(currentProcess.Id, "127.0.0.1")); + } + + [Theory] + [MemberData(nameof(MachineName_Remote_TestData))] + public void GetProcessesByName_RemoteMachineNameUnix_ThrowsPlatformNotSupportedException(string machineName) + { + Process currentProcess = Process.GetCurrentProcess(); + Assert.Throws(() => Process.GetProcessesByName(currentProcess.ProcessName, machineName)); + } + + [Fact] + public void TestRootGetProcessById() + { + Process p = Process.GetProcessById(1); + Assert.Equal(1, p.Id); + } + + [Fact] + public void TestUseShellExecute_Unix_Succeeds() + { + using (var p = Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = "exit", Arguments = "42" })) + { + Assert.True(p.WaitForExit(WaitInMS)); + Assert.Equal(42, p.ExitCode); + } + } + + [Fact] + [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] + public void TestPriorityClassUnix() + { + CreateDefaultProcess(); + + ProcessPriorityClass priorityClass = _process.PriorityClass; + + _process.PriorityClass = ProcessPriorityClass.Idle; + Assert.Equal(_process.PriorityClass, ProcessPriorityClass.Idle); + + try + { + _process.PriorityClass = ProcessPriorityClass.High; + Assert.Equal(_process.PriorityClass, ProcessPriorityClass.High); + + _process.PriorityClass = ProcessPriorityClass.Normal; + Assert.Equal(_process.PriorityClass, ProcessPriorityClass.Normal); + + _process.PriorityClass = priorityClass; + } + catch (Win32Exception ex) + { + Assert.True(!PlatformDetection.IsSuperUser, $"Failed even though superuser {ex.ToString()}"); + } + } + + [Fact] + [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] + public void TestBasePriorityOnUnix() + { + CreateDefaultProcess(); + + ProcessPriorityClass originalPriority = _process.PriorityClass; + Assert.Equal(ProcessPriorityClass.Normal, originalPriority); + + SetAndCheckBasePriority(ProcessPriorityClass.Idle, 19); + + try + { + SetAndCheckBasePriority(ProcessPriorityClass.Normal, 0); + SetAndCheckBasePriority(ProcessPriorityClass.High, -11); + _process.PriorityClass = originalPriority; + } + catch (Win32Exception ex) + { + Assert.True(!PlatformDetection.IsSuperUser, $"Failed even though superuser {ex.ToString()}"); + } + } + + [Fact] public void TestStartOnUnixWithBadPermissions() { string path = GetTestFilePath(); @@ -29,7 +133,6 @@ public void TestStartOnUnixWithBadPermissions() } [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes to set permissions public void TestStartOnUnixWithBadFormat() { string path = GetTestFilePath(); diff --git a/src/System.Diagnostics.Process/tests/ProcessTests.cs b/src/System.Diagnostics.Process/tests/ProcessTests.cs index 1add1a5410d1..c8835c5bb071 100644 --- a/src/System.Diagnostics.Process/tests/ProcessTests.cs +++ b/src/System.Diagnostics.Process/tests/ProcessTests.cs @@ -85,29 +85,6 @@ public void TestBasePriorityOnWindows() } } - [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior varies on Windows and Unix - [OuterLoop] - [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] - public void TestBasePriorityOnUnix() - { - CreateDefaultProcess(); - - ProcessPriorityClass originalPriority = _process.PriorityClass; - Assert.Equal(ProcessPriorityClass.Normal, originalPriority); - - try - { - SetAndCheckBasePriority(ProcessPriorityClass.High, -11); - SetAndCheckBasePriority(ProcessPriorityClass.Idle, 19); - SetAndCheckBasePriority(ProcessPriorityClass.Normal, 0); - } - finally - { - _process.PriorityClass = originalPriority; - } - } - [Theory] [InlineData(true)] [InlineData(false)] @@ -157,17 +134,6 @@ public void TestExitCode() } } - [PlatformSpecific(TestPlatforms.AnyUnix)] // Tests UseShellExecute with ProcessStartInfo - [Fact] - public void TestUseShellExecute_Unix_Succeeds() - { - using (var p = Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = "exit", Arguments = "42" })) - { - Assert.True(p.WaitForExit(WaitInMS)); - Assert.Equal(42, p.ExitCode); - } - } - [Fact] public void TestExitTime() { @@ -659,29 +625,6 @@ public void PriorityBoostEnabled_GetNotStarted_ThrowsInvalidOperationException() Assert.Throws(() => process.PriorityBoostEnabled = true); } - [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] // Expected behavior varies on Windows and Unix - [OuterLoop] - [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] - public void TestPriorityClassUnix() - { - CreateDefaultProcess(); - - ProcessPriorityClass priorityClass = _process.PriorityClass; - try - { - _process.PriorityClass = ProcessPriorityClass.High; - Assert.Equal(_process.PriorityClass, ProcessPriorityClass.High); - - _process.PriorityClass = ProcessPriorityClass.Normal; - Assert.Equal(_process.PriorityClass, ProcessPriorityClass.Normal); - } - finally - { - _process.PriorityClass = priorityClass; - } - } - [Fact, PlatformSpecific(TestPlatforms.Windows)] // Expected behavior varies on Windows and Unix public void TestPriorityClassWindows() { @@ -798,14 +741,6 @@ public void TestGetProcessById() Assert.Equal(_process.ProcessName, p.ProcessName); } - [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes to get process Id - public void TestRootGetProcessById() - { - Process p = Process.GetProcessById(1); - Assert.Equal(1, p.Id); - } - [Fact] public void TestGetProcesses() { @@ -888,15 +823,6 @@ public void GetProcessesByName_RemoteMachineNameWindows_ReturnsExpected(string m } } - [Theory] - [MemberData(nameof(MachineName_Remote_TestData))] - [PlatformSpecific(TestPlatforms.AnyUnix)] // Accessing processes on remote machines is not supported on Unix. - public void GetProcessesByName_RemoteMachineNameUnix_ThrowsPlatformNotSupportedException(string machineName) - { - Process currentProcess = Process.GetCurrentProcess(); - Assert.Throws(() => Process.GetProcessesByName(currentProcess.ProcessName, machineName)); - } - [Fact] public void GetProcessesByName_NoSuchProcess_ReturnsEmpty() { @@ -946,15 +872,6 @@ void TestRemoteProccess(Process remoteProcess) } } - [Fact, PlatformSpecific(TestPlatforms.AnyUnix)] // Behavior differs on Windows and Unix - public void TestProcessOnRemoteMachineUnix() - { - Process currentProcess = Process.GetCurrentProcess(); - - Assert.Throws(() => Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1")); - Assert.Throws(() => Process.GetProcessById(currentProcess.Id, "127.0.0.1")); - } - [Fact] public void StartInfo_GetFileName_ReturnsExpected() { @@ -1229,15 +1146,6 @@ public void MainWindowHandle_NoWindow_ReturnsEmptyHandle() Assert.Equal(_process.MainWindowHandle, _process.MainWindowHandle); } - [Fact] - [PlatformSpecific(TestPlatforms.AnyUnix)] // MainWindowHandle is not supported on Unix. - public void MainWindowHandle_GetUnix_ThrowsPlatformNotSupportedException() - { - CreateDefaultProcess(); - - Assert.Throws(() => _process.MainWindowHandle); - } - [Fact] public void MainWindowHandle_GetNotStarted_ThrowsInvalidOperationException() { @@ -1296,20 +1204,6 @@ public void Responding_GetNotStarted_ThrowsInvalidOperationException() Assert.Throws(() => process.Responding); } - [PlatformSpecific(TestPlatforms.AnyUnix)] // Needs to get the process Id from OS - [Fact] - private void TestWindowApisUnix() - { - // This tests the hardcoded implementations of these APIs on Unix. - using (Process p = Process.GetCurrentProcess()) - { - Assert.True(p.Responding); - Assert.Equal(string.Empty, p.MainWindowTitle); - Assert.False(p.CloseMainWindow()); - Assert.Throws(()=>p.WaitForInputIdle()); - } - } - [Fact] public void TestNonpagedSystemMemorySize() { diff --git a/src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj b/src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj index ee3e539e3fb6..fd1e907a95cd 100644 --- a/src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj +++ b/src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj @@ -18,6 +18,9 @@ Common\tests\System\PlatformDetection.cs + + Common\System\PlatformDetection.Unix.cs + Common\System\Diagnostics\RemoteExecutorTestBase.cs diff --git a/src/System.Globalization/tests/System.Globalization.Tests.csproj b/src/System.Globalization/tests/System.Globalization.Tests.csproj index 4da2ba6e2fca..9dcadd7507ad 100644 --- a/src/System.Globalization/tests/System.Globalization.Tests.csproj +++ b/src/System.Globalization/tests/System.Globalization.Tests.csproj @@ -2,6 +2,7 @@ + true {484C92C6-6D2C-45BC-A5E2-4A12BA228E1E} $(DefineConstants);uap