Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test] For named mutexes, check if Windows Nano Server uses different namespaces when using Local\ and Global\ prefixes #112266

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/libraries/System.Threading/tests/MutexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Win32.SafeHandles;
using Xunit;

namespace System.Threading.Tests
Expand Down Expand Up @@ -758,6 +759,30 @@ public void NamedMutex_DisposeWhenLockedRaceTest()
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void CheckWhichWindowsVersionsDistinguishLocalAndGlobalNamespaces()
{
const int MAXIMUM_ALLOWED = 0x02000000;
const int SYNCHRONIZE = 0x00100000;
const int MUTEX_MODIFY_STATE = 0x00000001;
const int MutexAccessRights = MAXIMUM_ALLOWED | SYNCHRONIZE | MUTEX_MODIFY_STATE;

const int ERROR_ALREADY_EXISTS = 0xB7;

string name = Guid.NewGuid().ToString("N");

using SafeWaitHandle m = CreateMutexEx(lpMutexAttributes: 0, @"Local\" + name, flags: 0, MutexAccessRights);
int errorCode = Marshal.GetLastPInvokeError();
Assert.False(m.IsInvalid);
Assert.NotEqual(ERROR_ALREADY_EXISTS, errorCode);

using SafeWaitHandle m2 = CreateMutexEx(lpMutexAttributes: 0, @"Global\" + name, flags: 0, MutexAccessRights);
errorCode = Marshal.GetLastPInvokeError();
Assert.False(m2.IsInvalid);
Assert.NotEqual(ERROR_ALREADY_EXISTS, errorCode);
}

public static TheoryData<string> GetValidNames()
{
var names = new TheoryData<string>() { Guid.NewGuid().ToString("N") };
Expand All @@ -768,6 +793,9 @@ public static TheoryData<string> GetValidNames()
return names;
}

[DllImport("kernel32.dll", EntryPoint = "CreateMutexExW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern SafeWaitHandle CreateMutexEx(nint lpMutexAttributes, string? name, uint flags, uint desiredAccess);

[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentThread();

Expand Down
Loading