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
{
using System.Reflection.Tests;
using System.Runtime.CompilerServices;
using System.Security;
+using System.Text;
using Xunit;
[assembly:
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()
{