Skip to content
Merged
Prev Previous commit
Next Next commit
check whether shm_open accepts a name that contains '+' and more than…
… one '/' (used by Base64 encoding)
  • Loading branch information
adamsitnik committed Jan 24, 2022
commit 64bb94b4ce1f37490940f245dfcda86b2c7f2683
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Win32.SafeHandles;
using Xunit;

namespace System.IO.MemoryMappedFiles.Tests
{
public partial class MemoryMappedFileTests_CreateNew : MemoryMappedFilesTestBase
{
[Theory]
[InlineData("/1dotnet/test1")] // contains '/'
[InlineData("/dotnet/test/")] // ends with '/' (in theory could suggest a directory)
[InlineData("/dotnet+test")] // contains '+'
[InlineData("/dotnet1+test+")] // ends with '+'
[InlineData("/dotnet1+/+test+/")] // combination of
public void ShmOpenAcceptsAllCharactersUsedByBase64Encoding(string mapName)
{
// The POSIX shared memory object name must begin with '/' and beside it's max length, that is all we know.
// The name is generated by MemoryMappedFile implementation and the user must pass null as map name on Unix.
// So we can't use the public API surface to test the behaviour for specific map names.
// The name is random, so there is no guarantee whether it's going to contain given characters or not.
// Base64 encoding uses a-Z, 0-9, '/' and '+' characters.
// The purpose of this test it to check whether the generated map name can contain '+' and more than one '/'.

Interop.Sys.OpenFlags flags = Interop.Sys.OpenFlags.O_RDWR | Interop.Sys.OpenFlags.O_CREAT | Interop.Sys.OpenFlags.O_EXCL;
Interop.Sys.Permissions perms = Interop.Sys.Permissions.S_IWUSR;

using SafeFileHandle fd = Interop.Sys.ShmOpen(mapName, flags, (int)perms);
if (fd.IsInvalid)
{
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
Assert.Equal(Interop.Error.ENOTSUP, errorInfo.Error); // it's not supported everywhere
}

Interop.Sys.ShmUnlink(mapName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.IO.MemoryMappedFiles.Tests
/// <summary>
/// Tests for MemoryMappedFile.CreateNew.
/// </summary>
public class MemoryMappedFileTests_CreateNew : MemoryMappedFilesTestBase
public partial class MemoryMappedFileTests_CreateNew : MemoryMappedFilesTestBase
{
/// <summary>
/// Tests invalid arguments to the CreateNew mapName parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,6 @@ namespace System.IO.MemoryMappedFiles.Tests
/// </summary>
public class SafeMemoryMappedViewHandleTests : MemoryMappedFilesTestBase
{
/// <summary>
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Windows.
/// </summary>
[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Windows()
{
const int BUF_SIZE = 256;

Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = default;
using SafeMemoryMappedFileHandle fileHandle = Interop.Kernel32.CreateFileMapping(
new IntPtr(-1),
ref secAttrs,
Interop.Kernel32.PageOptions.PAGE_EXECUTE_READWRITE,
0,
BUF_SIZE,
CreateUniqueMapName());

using SafeMemoryMappedViewHandle handle = Interop.Kernel32.MapViewOfFile(
fileHandle,
Interop.Kernel32.FileMapOptions.FILE_MAP_READ,
0,
0,
(UIntPtr)BUF_SIZE);

Assert.NotNull(handle);
}

/// <summary>
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Unix.
/// </summary>
Expand Down Expand Up @@ -67,6 +39,6 @@ public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Unix()
}

[DllImport("libc")]
private static unsafe extern SafeMemoryMappedViewHandle mmap(IntPtr addr, nint lengthint, int prot, int flags, int fd, nuint offset);
private static extern SafeMemoryMappedViewHandle mmap(IntPtr addr, nint lengthint, int prot, int flags, int fd, nuint offset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Win32.SafeHandles;
using Xunit;

namespace System.IO.MemoryMappedFiles.Tests
{
/// <summary>
/// Tests for SafeMemoryMappedViewHandle
/// </summary>
public class SafeMemoryMappedViewHandleTests : MemoryMappedFilesTestBase
{
/// <summary>
/// Tests that external code can use SafeMemoryMappedViewHandle as the result of a P/Invoke on Windows.
/// </summary>
[Fact]
public void SafeMemoryMappedViewHandle_CanUseInPInvoke_Windows()
{
const int BUF_SIZE = 256;

Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = default;
using SafeMemoryMappedFileHandle fileHandle = Interop.Kernel32.CreateFileMapping(
new IntPtr(-1),
ref secAttrs,
Interop.Kernel32.PageOptions.PAGE_EXECUTE_READWRITE,
0,
BUF_SIZE,
CreateUniqueMapName());

using SafeMemoryMappedViewHandle handle = Interop.Kernel32.MapViewOfFile(
fileHandle,
Interop.Kernel32.FileMapOptions.FILE_MAP_READ,
0,
0,
(UIntPtr)BUF_SIZE);

Assert.NotNull(handle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,37 @@
<Compile Include="MemoryMappedViewStream.Tests.cs" />
<Compile Include="MemoryMappedViewAccessor.Tests.cs" />
<Compile Include="MemoryMappedFilesTestsBase.cs" />
<Compile Include="MemoryMappedFilesTestsBase.Unix.cs" Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'" />
<Compile Include="MemoryMappedFilesTestsBase.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs" Link="Common\System\IO\TempFile.cs" />
<Compile Include="SafeMemoryMappedViewHandleTests.cs" />
<Compile Include="XunitAssemblyAttributes.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs" Link="ProductionCode\Common\Interop\Windows\Interop.BOOL.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs" Link="ProductionCode\Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MemOptions.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MemOptions.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="MemoryMappedFilesTestsBase.Windows.cs" />
<Compile Include="SafeMemoryMappedViewHandleTests.Windows.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs" Link="ProductionCode\Common\Interop\Windows\Interop.BOOL.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs" Link="ProductionCode\Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.CreateFileMapping.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MapViewOfFile.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MemOptions.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.MemOptions.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" Link="ProductionCode\Common\Interop\Windows\Kernel32\Interop.SECURITY_ATTRIBUTES.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'">
<Compile Include="MemoryMappedFilesTestsBase.Unix.cs" />
<Compile Include="SafeMemoryMappedViewHandleTests.Unix.cs" />
<Compile Include="MemoryMappedFile.CreateNew.Tests.Unix.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
Link="Common\Interop\Unix\Interop.Errors.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.OpenFlags.cs"
Link="Common\Interop\Unix\Interop.OpenFlags.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Permissions.cs"
Link="Common\Interop\Unix\Interop.Permissions.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ShmOpen.cs"
Link="Common\Interop\Unix\Interop.ShmOpen.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Unlink.cs"
Link="Common\Interop\Unix\Interop.Unlink.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
</ItemGroup>
</Project>
</Project>