Skip to content

Commit

Permalink
More tests for sections, also the Utils class.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Nov 8, 2019
1 parent c4c0eb8 commit 81699fb
Show file tree
Hide file tree
Showing 14 changed files with 513 additions and 58 deletions.
8 changes: 5 additions & 3 deletions Src/Workshell.PE/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static ulong ReadUInt64(byte[] bytes)
return BitConverter.ToUInt64(bytes,0);
}

/*
public static void Write(sbyte value, Stream stream)
{
var buffer = BitConverter.GetBytes(value);
Expand Down Expand Up @@ -153,6 +154,7 @@ public static void Write(ulong value, Stream stream)
Write(buffer, stream);
}
*/

public static void Write(byte[] bytes, Stream stream)
{
Expand All @@ -165,8 +167,8 @@ public static void Write<T>(T structure, byte[] buffer, int startIndex, int coun

try
{
Marshal.StructureToPtr(structure,ptr,false);
Marshal.Copy(ptr,buffer,startIndex,count);
Marshal.StructureToPtr(structure, ptr, false);
Marshal.Copy(ptr, buffer, startIndex, count);
}
finally
{
Expand All @@ -178,7 +180,7 @@ public static void Write<T>(T structure, Stream stream) where T : struct
{
var size = Utils.SizeOf<T>();

Write<T>(structure,stream,size);
Write<T>(structure, stream, size);
}

public static void Write<T>(T structure, Stream stream, int size) where T : struct
Expand Down
2 changes: 2 additions & 0 deletions src/Workshell.PE/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

#if !SIGNED
[assembly: InternalsVisibleTo("Workshell.PE.Resources")]
[assembly: InternalsVisibleTo("Workshell.PE.Tests")]
#else
[assembly: InternalsVisibleTo("Workshell.PE.Resources, PublicKey=0024000004800000940000000602000000240000525341310004000001000100259ed23116da6a496f873182c31284a428d040b37885524e9b53049cd99d5cc84feb00dbe77278afda8ebc9def14111b20b561f8d958e3f4aea2d492fed946245c528b16cad6ee785995ccfd7e6b7b34fe4be452a651069b2c0bbcf668bfb1dd9b99a7f30ab10d289525d61e82fd45e1ebcc11fc3d286e6096a1ee7edeee6091")]
[assembly: InternalsVisibleTo("Workshell.PE.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100259ed23116da6a496f873182c31284a428d040b37885524e9b53049cd99d5cc84feb00dbe77278afda8ebc9def14111b20b561f8d958e3f4aea2d492fed946245c528b16cad6ee785995ccfd7e6b7b34fe4be452a651069b2c0bbcf668bfb1dd9b99a7f30ab10d289525d61e82fd45e1ebcc11fc3d286e6096a1ee7edeee6091")]
#endif

[assembly: AssemblyTitle("Workshell.PE")]
Expand Down
8 changes: 4 additions & 4 deletions tests/Workshell.PE.Tests/DOSHeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class DOSHeaderTests
[Test]
public void DOSHeader_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -28,7 +28,7 @@ public void DOSHeader_Is_Not_Null()
[TestCase("nativetest.x64.dll", 0x0050)]
public void Bytes_On_Last_Page_Of_File_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -45,7 +45,7 @@ public void Bytes_On_Last_Page_Of_File_Is_Correct(string fileName, int expectedV
[TestCase("nativetest.x64.dll", 0x0040)]
public void File_Address_Relocation_Table_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -62,7 +62,7 @@ public void File_Address_Relocation_Table_Is_Correct(string fileName, int expect
[TestCase("nativetest.x64.dll", 0x00000100)]
public void File_Address_New_Header_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand Down
22 changes: 11 additions & 11 deletions tests/Workshell.PE.Tests/DataDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class DataDirectoryTests
[Test]
public void DataDirectory_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -25,7 +25,7 @@ public void DataDirectory_Is_Not_Null()
[Test]
public void DataDirectory_Is_Not_Empty()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public void DataDirectory_CLR_Exists_Or_Not(string fileName, bool expectedValue)
[Test]
public void DataDirectory_ExportTable_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -201,7 +201,7 @@ public void DataDirectory_ExportTable_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_ImportTable_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -216,7 +216,7 @@ public void DataDirectory_ImportTable_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_ResourceTable_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -231,7 +231,7 @@ public void DataDirectory_ResourceTable_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_ExceptionTable_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -246,7 +246,7 @@ public void DataDirectory_ExceptionTable_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_RelocationTable_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -261,7 +261,7 @@ public void DataDirectory_RelocationTable_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_DebugDirectory_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -276,7 +276,7 @@ public void DataDirectory_DebugDirectory_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_ImportAddressTable_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -291,7 +291,7 @@ public void DataDirectory_ImportAddressTable_RVA_And_Size_Is_Correct()
[Test]
public void DataDirectory_DelayImportDescriptors_RVA_And_Size_Is_Correct()
{
var file = Utils.GetFileFromResources("nativetest.x64.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -305,7 +305,7 @@ public void DataDirectory_DelayImportDescriptors_RVA_And_Size_Is_Correct()

private static void DataDirectory_Exists(string fileName, DataDirectoryType type, bool expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand Down
12 changes: 6 additions & 6 deletions tests/Workshell.PE.Tests/FileHeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class FileHeaderTests
[Test]
public void FileHeader_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -28,7 +28,7 @@ public void FileHeader_Is_Not_Null()
[TestCase("nativetest.x64.dll", 0x8664)]
public void Machine_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -45,7 +45,7 @@ public void Machine_Is_Correct(string fileName, int expectedValue)
[TestCase("nativetest.x64.dll", 0x000B)]
public void NumberOfSections_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -62,7 +62,7 @@ public void NumberOfSections_Is_Correct(string fileName, int expectedValue)
[TestCase("nativetest.x64.dll", 0x5DC4AD3D)]
public void TimeDateStamp_Is_Correct(string fileName, long expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -79,7 +79,7 @@ public void TimeDateStamp_Is_Correct(string fileName, long expectedValue)
[TestCase("nativetest.x64.dll", 0x00F0)]
public void SizeOfOptionalHeader_Is_Correct(string fileName, long expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -96,7 +96,7 @@ public void SizeOfOptionalHeader_Is_Correct(string fileName, long expectedValue)
[TestCase("nativetest.x64.dll", 0x2022)]
public void Characteristics_Is_Correct(string fileName, long expectedValue)
{
var file = Utils.GetFileFromResources(fileName);
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
Expand Down
24 changes: 12 additions & 12 deletions tests/Workshell.PE.Tests/GeneralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class GeneralTests
[Test]
public void IsValid_With_Text_File_Returns_False()
{
var file = Utils.GetFileFromResources("license.txt");
var file = TestingUtils.GetFileFromResources("license.txt");
var isValid = PortableExecutableImage.IsValid(file);

isValid.Should().BeFalse();
Expand All @@ -22,7 +22,7 @@ public void IsValid_With_Text_File_Returns_False()
[Test]
public void IsValid_With_Executable_File_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");
var file = TestingUtils.GetFileFromResources("clrtest.any.dll");
var isValid = PortableExecutableImage.IsValid(file);

isValid.Should().BeTrue();
Expand All @@ -31,7 +31,7 @@ public void IsValid_With_Executable_File_Returns_True()
[Test]
public void GetStream_Returns_Successfully()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");
var file = TestingUtils.GetFileFromResources("clrtest.any.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -44,7 +44,7 @@ public void GetStream_Returns_Successfully()
[Test]
public void GetCalculator_Returns_Successfully()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");
var file = TestingUtils.GetFileFromResources("clrtest.any.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -57,7 +57,7 @@ public void GetCalculator_Returns_Successfully()
[Test]
public void Is32Bit_With_32Bit_Image_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.x86.dll");
var file = TestingUtils.GetFileFromResources("clrtest.x86.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -68,7 +68,7 @@ public void Is32Bit_With_32Bit_Image_Returns_True()
[Test]
public void Is32Bit_With_64Bit_Image_Returns_False()
{
var file = Utils.GetFileFromResources("clrtest.x64.dll");
var file = TestingUtils.GetFileFromResources("clrtest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -79,7 +79,7 @@ public void Is32Bit_With_64Bit_Image_Returns_False()
[Test]
public void Is64Bit_With_64Bit_Image_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.x64.dll");
var file = TestingUtils.GetFileFromResources("clrtest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -90,7 +90,7 @@ public void Is64Bit_With_64Bit_Image_Returns_True()
[Test]
public void Is64Bit_With_32Bit_Image_Returns_False()
{
var file = Utils.GetFileFromResources("clrtest.x86.dll");
var file = TestingUtils.GetFileFromResources("clrtest.x86.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -101,7 +101,7 @@ public void Is64Bit_With_32Bit_Image_Returns_False()
[Test]
public void IsCLR_With_CLR_Image_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");
var file = TestingUtils.GetFileFromResources("clrtest.any.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -112,7 +112,7 @@ public void IsCLR_With_CLR_Image_Returns_True()
[Test]
public void IsCLR_With_Native_Image_Returns_False()
{
var file = Utils.GetFileFromResources("nativetest.x86.dll");
var file = TestingUtils.GetFileFromResources("nativetest.x86.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -123,7 +123,7 @@ public void IsCLR_With_Native_Image_Returns_False()
[Test]
public void IsSigned_Returns_False()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand All @@ -134,7 +134,7 @@ public void IsSigned_Returns_False()
[Test]
public void DOSStub_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand Down
10 changes: 10 additions & 0 deletions tests/Workshell.PE.Tests/LocationCalculatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Workshell.PE.Tests
{
public sealed class LocationCalculatorTests
{
}
}
2 changes: 1 addition & 1 deletion tests/Workshell.PE.Tests/NTHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class NTHeadersTests
[Test]
public void NTHeaders_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
Expand Down
Loading

0 comments on commit 81699fb

Please sign in to comment.