diff --git a/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs b/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs
index 8a146c9b0dc19..205e045617d91 100644
--- a/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs
+++ b/src/libraries/Common/tests/System/IO/ReparsePointUtilities.cs
@@ -60,7 +60,7 @@ public static partial class MountHelper
// Reduce the risk we accidentally stop running these altogether
// on Windows, due to a bug in CreateSymbolicLink
if (!success && PlatformDetection.IsWindows)
- Assert.True(!PlatformDetection.IsWindowsAndElevated);
+ Assert.False(PlatformDetection.IsPrivilegedProcess);
return success;
});
diff --git a/src/libraries/Common/tests/TestUtilities/System/AdminHelpers.cs b/src/libraries/Common/tests/TestUtilities/System/AdminHelpers.cs
index bf64c9ffea37a..6c523ce780312 100644
--- a/src/libraries/Common/tests/TestUtilities/System/AdminHelpers.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/AdminHelpers.cs
@@ -34,6 +34,12 @@ public static int RunAsSudo(string commandLine)
public static unsafe bool IsProcessElevated()
{
+ // Browser does not have the concept of an elevated process
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
+ {
+ return false;
+ }
+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
uint userId = Interop.Sys.GetEUid();
diff --git a/src/libraries/Common/tests/TestUtilities/System/IO/FileCleanupTestBase.cs b/src/libraries/Common/tests/TestUtilities/System/IO/FileCleanupTestBase.cs
index 326786de7e9c4..0629483be0730 100644
--- a/src/libraries/Common/tests/TestUtilities/System/IO/FileCleanupTestBase.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/IO/FileCleanupTestBase.cs
@@ -14,12 +14,8 @@ namespace System.IO
/// Base class for test classes the use temporary files that need to be cleaned up.
public abstract partial class FileCleanupTestBase : IDisposable
{
- private static readonly Lazy s_isElevated = new Lazy(() => AdminHelpers.IsProcessElevated());
-
private string fallbackGuid = Guid.NewGuid().ToString("N").Substring(0, 10);
- protected static bool IsProcessElevated => s_isElevated.Value;
-
/// Initialize the test class base. This creates the associated test directory.
protected FileCleanupTestBase(string tempDirectory = null)
{
diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs
index 33d0d80c97ce2..258572fced24c 100644
--- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs
@@ -55,10 +55,6 @@ public static partial class PlatformDetection
public static bool IsNotFedoraOrRedHatFamily => !IsFedora && !IsRedHatFamily;
public static bool IsNotDebian10 => !IsDebian10;
- public static bool IsSuperUser => IsBrowser || IsWindows ? false : libc.geteuid() == 0;
-
- public static bool IsUnixAndSuperUser => !IsWindows && IsSuperUser;
-
public static Version OpenSslVersion => !IsOSXLike && !IsWindows && !IsAndroid ?
GetOpenSslVersion() :
throw new PlatformNotSupportedException();
diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Windows.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Windows.cs
index 7d2c35de4ff11..1b570872d06d9 100644
--- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Windows.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Windows.cs
@@ -248,31 +248,8 @@ public static bool IsInAppContainer
}
}
- public static bool CanRunImpersonatedTests => PlatformDetection.IsNotWindowsNanoServer && PlatformDetection.IsWindowsAndElevated;
+ public static bool CanRunImpersonatedTests => PlatformDetection.IsNotWindowsNanoServer && PlatformDetection.IsWindows && PlatformDetection.IsPrivilegedProcess;
public static bool IsWindowsX86OrX64 => PlatformDetection.IsWindows && (PlatformDetection.IsX86Process || PlatformDetection.IsX64Process);
-
- private static int s_isWindowsElevated = -1;
- public static bool IsWindowsAndElevated
- {
- get
- {
- if (s_isWindowsElevated != -1)
- return s_isWindowsElevated == 1;
-
- if (!IsWindows || IsInAppContainer)
- {
- s_isWindowsElevated = 0;
- return false;
- }
-
- s_isWindowsElevated = AdminHelpers.IsProcessElevated() ? 1 : 0;
-
- return s_isWindowsElevated == 1;
- }
- }
-
- public static bool IsWindowsAndNotElevated
- => IsWindows && !IsWindowsAndElevated;
}
}
diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
index f4e4ec8a168bf..245ff9ccaee00 100644
--- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs
@@ -73,6 +73,23 @@ public static partial class PlatformDetection
public static bool Is64BitProcess => IntPtr.Size == 8;
public static bool IsNotWindows => !IsWindows;
+ private static volatile int s_isPrivilegedProcess = -1;
+ public static bool IsPrivilegedProcess
+ {
+ get
+ {
+ int p = s_isPrivilegedProcess;
+ if (p == -1)
+ {
+ s_isPrivilegedProcess = p = AdminHelpers.IsProcessElevated() ? 1 : 0;
+ }
+
+ return p == 1;
+ }
+ }
+
+ public static bool IsNotPrivilegedProcess => !IsPrivilegedProcess;
+
public static bool IsMarshalGetExceptionPointersSupported => !IsMonoRuntime && !IsNativeAot;
private static readonly Lazy s_isCheckedRuntime = new Lazy(() => AssemblyConfigurationEquals("Checked"));
diff --git a/src/libraries/Common/tests/TestUtilities/System/WindowsIdentityFixture.cs b/src/libraries/Common/tests/TestUtilities/System/WindowsIdentityFixture.cs
index 794f09beeba73..708c22c078173 100644
--- a/src/libraries/Common/tests/TestUtilities/System/WindowsIdentityFixture.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/WindowsIdentityFixture.cs
@@ -39,7 +39,8 @@ public sealed partial class WindowsTestAccount : IDisposable
public WindowsTestAccount(string userName)
{
- Assert.True(PlatformDetection.IsWindowsAndElevated);
+ Assert.True(PlatformDetection.IsWindows);
+ Assert.True(PlatformDetection.IsPrivilegedProcess);
_userName = userName;
Password = GeneratePassword();
diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs
index 1fbe26a087691..66fe0a2dabf4a 100644
--- a/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs
+++ b/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs
@@ -27,7 +27,8 @@ public class ProcessStartInfoTests : ProcessTestBase
private const string ItemSeparator = "CAFF9451396B4EEF8A5155A15BDC2080"; // random string that shouldn't be in any env vars; used instead of newline to separate env var strings
private static bool IsAdmin_IsNotNano_RemoteExecutorIsSupported
- => PlatformDetection.IsWindowsAndElevated && PlatformDetection.IsNotWindowsNanoServer && RemoteExecutor.IsSupported;
+ => PlatformDetection.IsWindows && PlatformDetection.IsNotWindowsNanoServer
+ && PlatformDetection.IsPrivilegedProcess && RemoteExecutor.IsSupported;
[Fact]
public void TestEnvironmentProperty()
diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs
index 3a99e7d1c68c5..b29de573446e7 100644
--- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs
+++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs
@@ -20,7 +20,7 @@ namespace System.Diagnostics.Tests
{
public partial class ProcessTests : ProcessTestBase
{
- private static bool IsRemoteExecutorSupportedAndOnUnixAndSuperUser => RemoteExecutor.IsSupported && PlatformDetection.IsUnixAndSuperUser;
+ private static bool IsRemoteExecutorSupportedAndPrivilegedProcess => RemoteExecutor.IsSupported && PlatformDetection.IsPrivilegedProcess;
[Fact]
private void TestWindowApisUnix()
@@ -456,7 +456,7 @@ public void ProcessStart_UseShellExecuteTrue_OpenUrl_SuccessfullyReadsArgumentAr
}
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsUnixAndSuperUser))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPrivilegedProcess))]
public void TestPriorityClassUnix()
{
CreateDefaultProcess();
@@ -478,11 +478,11 @@ public void TestPriorityClassUnix()
}
catch (Win32Exception ex)
{
- Assert.True(!PlatformDetection.IsSuperUser, $"Failed even though superuser {ex.ToString()}");
+ Assert.False(PlatformDetection.IsPrivilegedProcess, $"Failed even though superuser {ex.ToString()}");
}
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsUnixAndSuperUser))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPrivilegedProcess))]
public void TestBasePriorityOnUnix()
{
CreateDefaultProcess();
@@ -499,7 +499,7 @@ public void TestBasePriorityOnUnix()
}
catch (Win32Exception ex)
{
- Assert.True(!PlatformDetection.IsSuperUser, $"Failed even though superuser {ex.ToString()}");
+ Assert.False(PlatformDetection.IsPrivilegedProcess, $"Failed even though superuser {ex.ToString()}");
}
}
@@ -596,8 +596,7 @@ public void TestCheckChildProcessUserAndGroupIds()
/// Tests when running as root and starting a new process as a normal user,
/// the new process doesn't have elevated privileges.
///
- [ConditionalTheory(nameof(IsRemoteExecutorSupportedAndOnUnixAndSuperUser))]
- [OuterLoop("Needs sudo access")]
+ [ConditionalTheory(nameof(IsRemoteExecutorSupportedAndPrivilegedProcess))]
[InlineData(true)]
[InlineData(false)]
public unsafe void TestCheckChildProcessUserAndGroupIdsElevated(bool useRootGroups)
@@ -743,7 +742,6 @@ private static async Task TryWaitProcessReapedAsync(int pid, int timeoutMs
/// Tests the ProcessWaitState reference count drops to zero.
///
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
- [PlatformSpecific(TestPlatforms.AnyUnix)] // Test validates Unix implementation
public async Task TestProcessWaitStateReferenceCount()
{
using (var exitedEventSemaphore = new SemaphoreSlim(0, 1))
@@ -833,7 +831,6 @@ public void TestProcessRecycledPid()
Assert.True(foundRecycled);
}
- [PlatformSpecific(TestPlatforms.AnyUnix)]
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData("/dev/stdin", O_RDONLY)]
[InlineData("/dev/stdout", O_WRONLY)]
diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
index b1a0e42577f5c..542869f962a98 100644
--- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
+++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
@@ -2546,7 +2546,8 @@ public void Start_PassesArgumentsList_WhichGetsEscaped()
}
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWindowsAndNotElevated))]
+ [PlatformSpecific(TestPlatforms.Windows)]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotPrivilegedProcess))]
public void NonElevatedUser_QueryProcessNameOfSystemProcess()
{
const string Services = "services";
diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/FuzzyTests.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/FuzzyTests.Etw.cs
index dcf3a4673a7f1..d15c1d4a09552 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/FuzzyTests.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/FuzzyTests.Etw.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System;
using System.Collections.Generic;
#if USE_MDT_EVENTSOURCE
using Microsoft.Diagnostics.Tracing;
@@ -14,7 +15,7 @@ public partial class FuzzyTests
{
static partial void Test_Write_Fuzzy_TestEtw(List tests, EventSource logger)
{
- if (TestUtilities.IsProcessElevated)
+ if (PlatformDetection.IsPrivilegedProcess)
{
using (var listener = new EtwListener())
{
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 94d6532f4eea9..a1c04cc13ba6f 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestEventCounter.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestEventCounter.Etw.cs
@@ -8,10 +8,7 @@ namespace BasicEventSourceTests
{
public partial class TestEventCounter
{
- // Specifies whether the process is elevated or not.
- private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
-
- [ConditionalFact(nameof(IsProcessElevated))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPrivilegedProcess))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/25035")]
public void Test_Write_Metric_ETW()
{
diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs
index 995c9f7512da5..62382c1d82dab 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestUtilities.cs
@@ -15,9 +15,6 @@ namespace BasicEventSourceTests
{
internal class TestUtilities
{
- // Specifies whether the process is elevated or not.
- 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 a07e673a62274..7c2f5d3eb1592 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsManifestGeneration.Etw.cs
@@ -25,9 +25,8 @@ namespace BasicEventSourceTests
public partial class TestsManifestGeneration
{
// Specifies whether the process is elevated or not.
- private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
private static bool IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported =>
- IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer && RemoteExecutor.IsSupported;
+ PlatformDetection.IsPrivilegedProcess && PlatformDetection.IsNotWindowsNanoServer && RemoteExecutor.IsSupported;
/// ETW only works with elevated process
[ConditionalFact(nameof(IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported))]
diff --git a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsUserErrors.Etw.cs b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsUserErrors.Etw.cs
index fdfb9d33ed000..c5743cfa7c4cf 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsUserErrors.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsUserErrors.Etw.cs
@@ -21,7 +21,7 @@ public void Test_BadEventSource_MismatchedIds_WithEtwListener()
{
// We expect only one session to be on when running the test but if a ETW session was left
// hanging, it will confuse the EventListener tests.
- if (TestUtilities.IsProcessElevated)
+ if (PlatformDetection.IsPrivilegedProcess)
{
EtwListener.EnsureStopped();
}
@@ -31,7 +31,7 @@ public void Test_BadEventSource_MismatchedIds_WithEtwListener()
var listenerGenerators = new List> { () => new EventListenerListener() };
- if (TestUtilities.IsProcessElevated)
+ if (PlatformDetection.IsPrivilegedProcess)
{
listenerGenerators.Add(() => new EtwListener());
}
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 e2b604ee75c34..35788ac712dea 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWrite.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWrite.Etw.cs
@@ -11,9 +11,8 @@ namespace BasicEventSourceTests
public partial class TestsWrite
{
// Specifies whether the process is elevated or not.
- private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
private static bool IsProcessElevatedAndNotWindowsNanoServer =>
- IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197
+ PlatformDetection.IsPrivilegedProcess && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197
///
/// Tests the EventSource.Write[T] method (can only use the self-describing mechanism).
@@ -30,7 +29,7 @@ public void Test_Write_T_ETW()
[ActiveIssue("https://github.com/dotnet/runtime/issues/21295", TargetFrameworkMonikers.NetFramework)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/25035")]
- [ConditionalFact(nameof(IsProcessElevated))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPrivilegedProcess))]
public void Test_Write_T_In_Manifest_Serialization_WithEtwListener()
{
using (var eventListener = new EventListenerListener())
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 7c1975836107b..088eafad41c63 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEvent.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEvent.Etw.cs
@@ -11,9 +11,8 @@ namespace BasicEventSourceTests
partial class TestsWriteEvent
{
// Specifies whether the process is elevated or not.
- private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
private static bool IsProcessElevatedAndNotWindowsNanoServer =>
- IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197
+ PlatformDetection.IsPrivilegedProcess && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197
///
/// Tests WriteEvent using the manifest based mechanism.
@@ -84,7 +83,7 @@ public void Test_WriteEvent_ByteArray_SelfDescribing_ETW()
static partial void Test_WriteEvent_AddEtwTests(List tests, EventSourceTest logger)
{
- if (!IsProcessElevated)
+ if (!PlatformDetection.IsPrivilegedProcess)
{
return;
}
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 fb38678b1e303..6d3eb85fc0e24 100644
--- a/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.Etw.cs
+++ b/src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestsWriteEventToListener.Etw.cs
@@ -14,10 +14,7 @@ namespace BasicEventSourceTests
{
public partial class TestsWriteEventToListener
{
- // Specifies whether the process is elevated or not.
- private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
-
- [ConditionalFact(nameof(IsProcessElevated))]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPrivilegedProcess))]
public void Test_WriteEvent_TransferEvents()
{
TestUtilities.CheckNoEventSourcesRunning("Start");
diff --git a/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.ExtractToFile.Tests.Unix.cs b/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.ExtractToFile.Tests.Unix.cs
index cee5f6bc7dfd8..d5438928a59dd 100644
--- a/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.ExtractToFile.Tests.Unix.cs
+++ b/src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.ExtractToFile.Tests.Unix.cs
@@ -23,7 +23,7 @@ public static IEnumerable