2024-11-28 23:22:53 -05:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2026-03-18 16:37:59 -04:00
|
|
|
namespace SabreTools.Wrappers.Test
|
2024-11-28 23:22:53 -05:00
|
|
|
{
|
|
|
|
|
public class PortableExecutableTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NullArray_Null()
|
|
|
|
|
{
|
|
|
|
|
byte[]? data = null;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
var actual = PortableExecutable.Create(data, offset);
|
|
|
|
|
Assert.Null(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void EmptyArray_Null()
|
|
|
|
|
{
|
|
|
|
|
byte[]? data = [];
|
|
|
|
|
int offset = 0;
|
|
|
|
|
var actual = PortableExecutable.Create(data, offset);
|
|
|
|
|
Assert.Null(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void InvalidArray_Null()
|
|
|
|
|
{
|
|
|
|
|
byte[]? data = [.. Enumerable.Repeat<byte>(0xFF, 1024)];
|
|
|
|
|
int offset = 0;
|
|
|
|
|
var actual = PortableExecutable.Create(data, offset);
|
|
|
|
|
Assert.Null(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void NullStream_Null()
|
|
|
|
|
{
|
|
|
|
|
Stream? data = null;
|
|
|
|
|
var actual = PortableExecutable.Create(data);
|
|
|
|
|
Assert.Null(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void EmptyStream_Null()
|
|
|
|
|
{
|
|
|
|
|
Stream? data = new MemoryStream([]);
|
|
|
|
|
var actual = PortableExecutable.Create(data);
|
|
|
|
|
Assert.Null(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void InvalidStream_Null()
|
|
|
|
|
{
|
|
|
|
|
Stream? data = new MemoryStream([.. Enumerable.Repeat<byte>(0xFF, 1024)]);
|
|
|
|
|
var actual = PortableExecutable.Create(data);
|
|
|
|
|
Assert.Null(actual);
|
|
|
|
|
}
|
2026-10-19 15:50:21 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ExtractWiseOverlay_NotWise_FileNotKeptOpen()
|
|
|
|
|
{
|
|
|
|
|
// Any executable that is not a Wise installer, here a copy of this test assembly
|
|
|
|
|
string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
|
|
|
|
string outputDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
|
|
|
|
File.Copy(typeof(PortableExecutableTests).Assembly.Location, path);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
|
|
|
|
{
|
|
|
|
|
var pe = PortableExecutable.Create(stream);
|
|
|
|
|
Assert.NotNull(pe);
|
|
|
|
|
Assert.False(pe.ExtractWiseOverlay(outputDirectory, includeDebug: false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Opening the file exclusively fails while another handle to it is still open
|
|
|
|
|
using var exclusive = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None);
|
|
|
|
|
Assert.True(exclusive.CanRead);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-28 23:22:53 -05:00
|
|
|
}
|
2025-11-14 09:06:59 -05:00
|
|
|
}
|