Tests for BIFE containing path (dotnet/corefx#42150)
authorDan Moseley <danmose@microsoft.com>
Mon, 4 Nov 2019 04:09:24 +0000 (20:09 -0800)
committerGitHub <noreply@github.com>
Mon, 4 Nov 2019 04:09:24 +0000 (20:09 -0800)
* Tests for BIFE containing path

* Update src/System.Reflection/tests/AssemblyTests.cs

Co-Authored-By: Stephen Toub <stoub@microsoft.com>
* Update src/System.Reflection/tests/AssemblyTests.cs

Co-Authored-By: Stephen Toub <stoub@microsoft.com>
Commit migrated from https://github.com/dotnet/corefx/commit/d718eb86f958bdd18ec1236387d2b72f18b0a86c

src/libraries/Common/tests/CoreFx.Private.TestUtilities/System/AssertExtensions.cs
src/libraries/System.Reflection/tests/AssemblyTests.cs

index b952314..6559850 100644 (file)
@@ -21,6 +21,12 @@ namespace System
             Assert.Equal(expectedMessage, Assert.Throws<T>(action).Message);
         }
 
+        public static void ThrowsContains<T>(Action action, string expectedMessageContent)
+            where T : Exception
+        {
+            Assert.Contains(expectedMessageContent, Assert.Throws<T>(action).Message);
+        }
+
         public static void Throws<T>(string netCoreParamName, string netFxParamName, Action action)
             where T : ArgumentException
         {
index b9f1a56..a3e3ac9 100644 (file)
@@ -11,6 +11,7 @@ using System.Linq;
 using System.Reflection.Tests;
 using System.Runtime.CompilerServices;
 using System.Security;
+using System.Text;
 using Xunit;
 
 [assembly:
@@ -323,6 +324,39 @@ namespace System.Reflection.Tests
             AssertExtensions.Throws<ArgumentException>("path", null, () => Assembly.LoadFile("System.Runtime.Tests.dll"));
         }
 
+        // This test should apply equally to Unix, but this reliably hits a particular one of the
+        // myriad ways that assembly load can fail
+        [Fact]
+        [PlatformSpecific(TestPlatforms.Windows)]
+        public void LoadFile_ValidPEBadIL_ThrowsBadImageFormatExceptionWithPath()
+        {
+            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "kernelbase.dll");
+            if (!File.Exists(path))
+                return;
+
+            AssertExtensions.ThrowsContains<BadImageFormatException>(() => Assembly.LoadFile(path), path);
+        }
+
+        [Theory]
+        [InlineData(0)]
+        [InlineData(5)]
+        [InlineData(50)]
+        [InlineData(100)]
+        // Higher numbers hit some codepaths that currently don't include the path in the exception message
+        public void LoadFile_ValidPEBadIL_ThrowsBadImageFormatExceptionWithPath(int seek)
+        {
+            ReadOnlySpan<byte> garbage = Encoding.UTF8.GetBytes(new string('X', 500));
+            string path = GetTestFilePath();
+            File.Copy(SourceTestAssemblyPath, path);
+            using (var fs = new FileStream(path, FileMode.Open))
+            {
+                fs.Position = seek;
+                fs.Write(garbage);
+            }
+
+            AssertExtensions.ThrowsContains<BadImageFormatException>(() => Assembly.LoadFile(path), path);
+        }
+
         [Fact]
         public void LoadFromUsingHashValue_Netcore()
         {