diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index a1aca15d4dd73b..3a99e7d1c68c56 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -656,7 +656,7 @@ private static string GetUserGroupIds(string username) private static string GetCurrentRealUserName() { - string realUserName = geteuid() == 0 ? + string realUserName = Environment.IsPrivilegedProcess ? Environment.GetEnvironmentVariable("SUDO_USER") : Environment.UserName; diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestEventCounter.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestEventCounter.Etw.cs index 5cae348f1e8e16..94d6532f4eea91 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestEventCounter.Etw.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestEventCounter.Etw.cs @@ -9,8 +9,7 @@ namespace BasicEventSourceTests public partial class TestEventCounter { // Specifies whether the process is elevated or not. - private static readonly Lazy s_isElevated = new Lazy(AdminHelpers.IsProcessElevated); - private static bool IsProcessElevated => s_isElevated.Value; + private static bool IsProcessElevated => Environment.IsPrivilegedProcess; [ConditionalFact(nameof(IsProcessElevated))] [ActiveIssue("https://github.com/dotnet/runtime/issues/25035")] diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs index 2990b25b20390c..995c9f7512da5d 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs @@ -16,8 +16,7 @@ namespace BasicEventSourceTests internal class TestUtilities { // Specifies whether the process is elevated or not. - private static readonly Lazy s_isElevated = new Lazy(() => AdminHelpers.IsProcessElevated()); - internal static bool IsProcessElevated => s_isElevated.Value; + internal static bool IsProcessElevated => Environment.IsPrivilegedProcess; /// /// Confirms that there are no EventSources running. diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs index 873def84cadd73..a07e673a62274f 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs @@ -25,8 +25,7 @@ namespace BasicEventSourceTests public partial class TestsManifestGeneration { // Specifies whether the process is elevated or not. - private static readonly Lazy s_isElevated = new Lazy(AdminHelpers.IsProcessElevated); - private static bool IsProcessElevated => s_isElevated.Value; + private static bool IsProcessElevated => Environment.IsPrivilegedProcess; private static bool IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported => IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer && RemoteExecutor.IsSupported; diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWrite.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWrite.Etw.cs index 87cd921f8b5d66..e2b604ee75c344 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWrite.Etw.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWrite.Etw.cs @@ -11,8 +11,7 @@ namespace BasicEventSourceTests public partial class TestsWrite { // Specifies whether the process is elevated or not. - private static readonly Lazy s_isElevated = new Lazy(AdminHelpers.IsProcessElevated); - private static bool IsProcessElevated => s_isElevated.Value; + private static bool IsProcessElevated => Environment.IsPrivilegedProcess; private static bool IsProcessElevatedAndNotWindowsNanoServer => IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197 diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEvent.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEvent.Etw.cs index 652cb99e684f5f..7c1975836107b9 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEvent.Etw.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEvent.Etw.cs @@ -11,8 +11,7 @@ namespace BasicEventSourceTests partial class TestsWriteEvent { // Specifies whether the process is elevated or not. - private static readonly Lazy s_isElevated = new Lazy(AdminHelpers.IsProcessElevated); - private static bool IsProcessElevated => s_isElevated.Value; + private static bool IsProcessElevated => Environment.IsPrivilegedProcess; private static bool IsProcessElevatedAndNotWindowsNanoServer => IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197 diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.Etw.cs index 8b24a7c6a3e7a4..fb38678b1e303c 100644 --- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.Etw.cs +++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.Etw.cs @@ -15,8 +15,7 @@ namespace BasicEventSourceTests public partial class TestsWriteEventToListener { // Specifies whether the process is elevated or not. - private static readonly Lazy s_isElevated = new Lazy(AdminHelpers.IsProcessElevated); - private static bool IsProcessElevated => s_isElevated.Value; + private static bool IsProcessElevated => Environment.IsPrivilegedProcess; [ConditionalFact(nameof(IsProcessElevated))] public void Test_WriteEvent_TransferEvents() diff --git a/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Unix.cs b/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Unix.cs index c01ce6e1a5291c..2ff8d6436e9601 100644 --- a/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Unix.cs +++ b/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Unix.cs @@ -36,7 +36,7 @@ public NamedPipeTest_CurrentUserOnly_Unix(ITestOutputHelper output) public async Task Connection_UnderDifferentUsers_BehavesAsExpected( PipeOptions serverPipeOptions, PipeOptions clientPipeOptions, PipeDirection clientPipeDirection) { - bool isRoot = AdminHelpers.IsProcessElevated(); + bool isRoot = Environment.IsPrivilegedProcess; if (clientPipeOptions == PipeOptions.CurrentUserOnly && isRoot) { throw new SkipTestException("Current user is root, RemoteExecutor is unable to use a different user for CurrentUserOnly."); diff --git a/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.RunAsClient.Unix.cs b/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.RunAsClient.Unix.cs index d522fbe79f4936..9c93d00b833a6c 100644 --- a/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.RunAsClient.Unix.cs +++ b/src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.RunAsClient.Unix.cs @@ -19,7 +19,7 @@ public class NamedPipeTest_RunAsClient [DllImport("libc", SetLastError = true)] internal static extern unsafe uint geteuid(); - public static bool IsSuperUserAndRemoteExecutorSupported => geteuid() == 0 && RemoteExecutor.IsSupported; + public static bool IsSuperUserAndRemoteExecutorSupported => Environment.IsPrivilegedProcess && RemoteExecutor.IsSupported; [ConditionalFact(nameof(IsSuperUserAndRemoteExecutorSupported))] [PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs index b983b5fd95daac..4cc267e0113eb8 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs @@ -40,7 +40,7 @@ public CreateSocket(ITestOutputHelper output) new object[] { SocketType.Unknown, ProtocolType.Udp }, }; - private static bool SupportsRawSockets => AdminHelpers.IsProcessElevated(); + private static bool SupportsRawSockets => Environment.IsPrivilegedProcess; private static bool NotSupportsRawSockets => !SupportsRawSockets; [OuterLoop] @@ -480,7 +480,7 @@ await Task.Run(async () => } } } - }).WaitAsync(TestSettings.PassingTestTimeout); + }).WaitAsync(TestSettings.PassingTestTimeout); } [DllImport("libc")] diff --git a/src/libraries/System.Security.Cryptography/tests/X509Certificates/X509StoreTests.Unix.cs b/src/libraries/System.Security.Cryptography/tests/X509Certificates/X509StoreTests.Unix.cs index cde7778882e647..0efb6c12028fb9 100644 --- a/src/libraries/System.Security.Cryptography/tests/X509Certificates/X509StoreTests.Unix.cs +++ b/src/libraries/System.Security.Cryptography/tests/X509Certificates/X509StoreTests.Unix.cs @@ -50,6 +50,6 @@ private void X509Store_MachineStoreLoadSkipsInvalidFiles() }, new RemoteInvokeOptions { StartInfo = psi }).Dispose(); } - public static bool NotRunningAsRootAndRemoteExecutorSupported => Interop.Sys.GetEUid() != 0 && RemoteExecutor.IsSupported; + public static bool NotRunningAsRootAndRemoteExecutorSupported => !Environment.IsPrivilegedProcess && RemoteExecutor.IsSupported; } } diff --git a/src/libraries/System.ServiceProcess.ServiceController/tests/TestServiceProvider.cs b/src/libraries/System.ServiceProcess.ServiceController/tests/TestServiceProvider.cs index 88d3735179ce7e..3a12773dca5360 100644 --- a/src/libraries/System.ServiceProcess.ServiceController/tests/TestServiceProvider.cs +++ b/src/libraries/System.ServiceProcess.ServiceController/tests/TestServiceProvider.cs @@ -17,19 +17,8 @@ internal sealed class TestServiceProvider private const int readTimeout = 60000; - private static readonly Lazy s_runningWithElevatedPrivileges = new Lazy(() => - { - using WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); - return new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator); - }); - private NamedPipeClientStream _client; - public static bool RunningWithElevatedPrivileges - { - get { return s_runningWithElevatedPrivileges.Value; } - } - public NamedPipeClientStream Client { get