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

Commit

Permalink
Sudo fix (#20080) (#20119)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley committed May 23, 2017
1 parent 6406a1d commit a4429f5
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 109 deletions.
7 changes: 6 additions & 1 deletion src/Common/tests/System/PlatformDetection.Unix.cs
Expand Up @@ -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;
}
}
}
107 changes: 105 additions & 2 deletions src/System.Diagnostics.Process/tests/ProcessTests.Unix.cs
Expand Up @@ -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<InvalidOperationException>(()=>p.WaitForInputIdle());
}
}

[Fact]
public void MainWindowHandle_GetUnix_ThrowsPlatformNotSupportedException()
{
CreateDefaultProcess();

Assert.Throws<PlatformNotSupportedException>(() => _process.MainWindowHandle);
}

[Fact]
public void TestProcessOnRemoteMachineUnix()
{
Process currentProcess = Process.GetCurrentProcess();

Assert.Throws<PlatformNotSupportedException>(() => Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1"));
Assert.Throws<PlatformNotSupportedException>(() => 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<PlatformNotSupportedException>(() => 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();
Expand All @@ -29,7 +133,6 @@ public void TestStartOnUnixWithBadPermissions()
}

[Fact]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes to set permissions
public void TestStartOnUnixWithBadFormat()
{
string path = GetTestFilePath();
Expand Down
106 changes: 0 additions & 106 deletions src/System.Diagnostics.Process/tests/ProcessTests.cs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -659,29 +625,6 @@ public void PriorityBoostEnabled_GetNotStarted_ThrowsInvalidOperationException()
Assert.Throws<InvalidOperationException>(() => 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()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<PlatformNotSupportedException>(() => Process.GetProcessesByName(currentProcess.ProcessName, machineName));
}

[Fact]
public void GetProcessesByName_NoSuchProcess_ReturnsEmpty()
{
Expand Down Expand Up @@ -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<PlatformNotSupportedException>(() => Process.GetProcessesByName(currentProcess.ProcessName, "127.0.0.1"));
Assert.Throws<PlatformNotSupportedException>(() => Process.GetProcessById(currentProcess.Id, "127.0.0.1"));
}

[Fact]
public void StartInfo_GetFileName_ReturnsExpected()
{
Expand Down Expand Up @@ -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<PlatformNotSupportedException>(() => _process.MainWindowHandle);
}

[Fact]
public void MainWindowHandle_GetNotStarted_ThrowsInvalidOperationException()
{
Expand Down Expand Up @@ -1296,20 +1204,6 @@ public void Responding_GetNotStarted_ThrowsInvalidOperationException()
Assert.Throws<InvalidOperationException>(() => 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<InvalidOperationException>(()=>p.WaitForInputIdle());
}
}

[Fact]
public void TestNonpagedSystemMemorySize()
{
Expand Down
Expand Up @@ -18,6 +18,9 @@
<Compile Include="$(CommonTestPath)\System\PlatformDetection.cs">
<Link>Common\tests\System\PlatformDetection.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\PlatformDetection.Unix.cs" Condition="'$(TargetsWindows)' != 'true'">
<Link>Common\System\PlatformDetection.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorTestBase.cs">
<Link>Common\System\Diagnostics\RemoteExecutorTestBase.cs</Link>
</Compile>
Expand Down
Expand Up @@ -2,6 +2,7 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ProjectGuid>{484C92C6-6D2C-45BC-A5E2-4A12BA228E1E}</ProjectGuid>
<DefineConstants Condition="'$(TargetGroup)' == 'uap'">$(DefineConstants);uap</DefineConstants>
</PropertyGroup>
Expand Down

0 comments on commit a4429f5

Please sign in to comment.