make mmap tests friendlier to containers (dotnet/corefx#36342)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Mon, 25 Mar 2019 21:43:28 +0000 (14:43 -0700)
committerGitHub <noreply@github.com>
Mon, 25 Mar 2019 21:43:28 +0000 (14:43 -0700)
* make mmap tests friendlier to containers

* move { to right place

Commit migrated from https://github.com/dotnet/corefx/commit/7cdbadc0399009202d3821024f106efcf12b23c4

src/libraries/System.IO.MemoryMappedFiles/tests/MemoryMappedViewAccessor.Tests.cs
src/libraries/System.IO.MemoryMappedFiles/tests/MemoryMappedViewStream.Tests.cs

index 71e736d..eeb93ec 100644 (file)
@@ -5,6 +5,7 @@
 using Microsoft.Win32.SafeHandles;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using Microsoft.DotNet.XUnitExtensions;
 using Xunit;
 
 namespace System.IO.MemoryMappedFiles.Tests
@@ -56,7 +57,7 @@ namespace System.IO.MemoryMappedFiles.Tests
             }
         }
 
-        [Theory]
+        [ConditionalTheory]
         [InlineData(MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileAccess.Read)]
         [InlineData(MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileAccess.Write)]
         [InlineData(MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileAccess.ReadWrite)]
@@ -80,10 +81,22 @@ namespace System.IO.MemoryMappedFiles.Tests
             AssertExtensions.ThrowsIf<IOException>(PlatformDetection.IsUap && mapAccess == MemoryMappedFileAccess.ReadWriteExecute && viewAccess == MemoryMappedFileAccess.ReadWriteExecute,
             () =>
             {
-                using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Capacity, mapAccess))
-                using (MemoryMappedViewAccessor acc = mmf.CreateViewAccessor(0, Capacity, viewAccess))
+                try
+                {
+                    using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Capacity, mapAccess))
+                    using (MemoryMappedViewAccessor acc = mmf.CreateViewAccessor(0, Capacity, viewAccess))
+                    {
+                        ValidateMemoryMappedViewAccessor(acc, Capacity, viewAccess);
+                    }
+                }
+                catch (UnauthorizedAccessException)
                 {
-                    ValidateMemoryMappedViewAccessor(acc, Capacity, viewAccess);
+                    if (PlatformDetection.IsInContainer && (viewAccess == MemoryMappedFileAccess.ReadExecute || viewAccess == MemoryMappedFileAccess.ReadWriteExecute))
+                    {
+                        throw new SkipTestException("Execute permission failing in container.");
+                    }
+
+                    throw;
                 }
             });
         }
index 87bce4c..b4d03c1 100644 (file)
@@ -5,6 +5,7 @@
 using Microsoft.Win32.SafeHandles;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using Microsoft.DotNet.XUnitExtensions;
 using Xunit;
 
 namespace System.IO.MemoryMappedFiles.Tests
@@ -56,7 +57,7 @@ namespace System.IO.MemoryMappedFiles.Tests
             }
         }
 
-        [Theory]
+        [ConditionalTheory]
         [InlineData(MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileAccess.Read)]
         [InlineData(MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileAccess.Write)]
         [InlineData(MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileAccess.ReadWrite)]
@@ -80,10 +81,22 @@ namespace System.IO.MemoryMappedFiles.Tests
             AssertExtensions.ThrowsIf<IOException>(PlatformDetection.IsUap && mapAccess == MemoryMappedFileAccess.ReadWriteExecute && viewAccess == MemoryMappedFileAccess.ReadWriteExecute,
             () =>
             {
-                using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Capacity, mapAccess))
-                using (MemoryMappedViewStream s = mmf.CreateViewStream(0, Capacity, viewAccess))
+                try
+                {
+                    using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Capacity, mapAccess))
+                    using (MemoryMappedViewStream s = mmf.CreateViewStream(0, Capacity, viewAccess))
+                    {
+                        ValidateMemoryMappedViewStream(s, Capacity, viewAccess);
+                    }
+                }
+                catch (UnauthorizedAccessException)
                 {
-                    ValidateMemoryMappedViewStream(s, Capacity, viewAccess);
+                    if (PlatformDetection.IsInContainer && (viewAccess == MemoryMappedFileAccess.ReadExecute || viewAccess == MemoryMappedFileAccess.ReadWriteExecute))
+                    {
+                        throw new SkipTestException("Execute permission failing in container.");
+                    }
+
+                    throw;
                 }
             });
         }