Skip to content

Commit

Permalink
Started adding unit tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Nov 8, 2019
1 parent c6558a0 commit 5991516
Show file tree
Hide file tree
Showing 13 changed files with 482 additions and 19 deletions.
64 changes: 64 additions & 0 deletions tests/Workshell.PE.Tests/DOSHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Text;

using FluentAssertions;
using NUnit.Framework;

namespace Workshell.PE.Tests
{
[TestFixture]
public sealed class DOSHeaderTests
{
[TestCase("clrtest.any.dll", 0x0090)]
[TestCase("clrtest.x86.dll", 0x0090)]
[TestCase("clrtest.x64.dll", 0x0090)]
[TestCase("nativetest.x86.dll", 0x0050)]
[TestCase("nativetest.x64.dll", 0x0050)]
public void Bytes_On_Last_Page_Of_File_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.DOSHeader.BytesOnLastPage;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x0040)]
[TestCase("clrtest.x86.dll", 0x0040)]
[TestCase("clrtest.x64.dll", 0x0040)]
[TestCase("nativetest.x86.dll", 0x0040)]
[TestCase("nativetest.x64.dll", 0x0040)]
public void File_Address_Relocation_Table_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.DOSHeader.FileAddressRelocationTable;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x00000080)]
[TestCase("clrtest.x86.dll", 0x00000080)]
[TestCase("clrtest.x64.dll", 0x00000080)]
[TestCase("nativetest.x86.dll", 0x00000100)]
[TestCase("nativetest.x64.dll", 0x00000100)]
public void File_Address_New_Header_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.DOSHeader.FileAddressNewHeader;

value.Should().Be(expectedValue);
}
}
}
}
98 changes: 98 additions & 0 deletions tests/Workshell.PE.Tests/FileHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Text;

using FluentAssertions;
using NUnit.Framework;

namespace Workshell.PE.Tests
{
[TestFixture]
public sealed class FileHeaderTests
{
[TestCase("clrtest.any.dll", 0x014C)]
[TestCase("clrtest.x86.dll", 0x014C)]
[TestCase("clrtest.x64.dll", 0x8664)]
[TestCase("nativetest.x86.dll", 0x014C)]
[TestCase("nativetest.x64.dll", 0x8664)]
public void Machine_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.NTHeaders.FileHeader.Machine;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x0003)]
[TestCase("clrtest.x86.dll", 0x0003)]
[TestCase("clrtest.x64.dll", 0x0002)]
[TestCase("nativetest.x86.dll", 0x000B)]
[TestCase("nativetest.x64.dll", 0x000B)]
public void NumberOfSections_Is_Correct(string fileName, int expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.NTHeaders.FileHeader.NumberOfSections;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0xD8FCD8BF)]
[TestCase("clrtest.x86.dll", 0xC404A333)]
[TestCase("clrtest.x64.dll", 0xE88B9424)]
[TestCase("nativetest.x86.dll", 0x5DC4ACFF)]
[TestCase("nativetest.x64.dll", 0x5DC4AD3D)]
public void TimeDateStamp_Is_Correct(string fileName, long expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.NTHeaders.FileHeader.TimeDateStamp;

value.Should().Be(Convert.ToUInt32(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x00E0)]
[TestCase("clrtest.x86.dll", 0x00E0)]
[TestCase("clrtest.x64.dll", 0x00F0)]
[TestCase("nativetest.x86.dll", 0x00E0)]
[TestCase("nativetest.x64.dll", 0x00F0)]
public void SizeOfOptionalHeader_Is_Correct(string fileName, long expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.NTHeaders.FileHeader.SizeOfOptionalHeader;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x2022)]
[TestCase("clrtest.x86.dll", 0x2102)]
[TestCase("clrtest.x64.dll", 0x2022)]
[TestCase("nativetest.x86.dll", 0xA18E)]
[TestCase("nativetest.x64.dll", 0x2022)]
public void Characteristics_Is_Correct(string fileName, long expectedValue)
{
var file = Utils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.NTHeaders.FileHeader.Characteristics;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}
}
}
Binary file not shown.
Binary file added tests/Workshell.PE.Tests/Files/clrtest.x64.dll
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions tests/Workshell.PE.Tests/Files/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) Workshell Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file not shown.
Binary file not shown.
189 changes: 189 additions & 0 deletions tests/Workshell.PE.Tests/GeneralTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FluentAssertions;
using NUnit.Framework;

namespace Workshell.PE.Tests
{
[TestFixture]
public sealed class GeneralTests
{
[Test]
public void IsValid_With_Text_File_Returns_False()
{
var file = Utils.GetFileFromResources("license.txt");
var isValid = PortableExecutableImage.IsValid(file);

isValid.Should().BeFalse();
}

[Test]
public void IsValid_With_Executable_File_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");
var isValid = PortableExecutableImage.IsValid(file);

isValid.Should().BeTrue();
}

[Test]
public void GetStream_Returns_Successfully()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
var stream = image.GetStream();

stream.Should().NotBeNull();
}
}

[Test]
public void GetCalculator_Returns_Successfully()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
var calc = image.GetCalculator();

calc.Should().NotBeNull();
}
}

[Test]
public void Is32Bit_With_32Bit_Image_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.x86.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
image.Is32Bit.Should().BeTrue();
}
}

[Test]
public void Is32Bit_With_64Bit_Image_Returns_False()
{
var file = Utils.GetFileFromResources("clrtest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
image.Is32Bit.Should().BeFalse();
}
}

[Test]
public void Is64Bit_With_64Bit_Image_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.x64.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
image.Is64Bit.Should().BeTrue();
}
}

[Test]
public void Is64Bit_With_32Bit_Image_Returns_False()
{
var file = Utils.GetFileFromResources("clrtest.x86.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
image.Is64Bit.Should().BeFalse();
}
}

[Test]
public void IsCLR_With_CLR_Image_Returns_True()
{
var file = Utils.GetFileFromResources("clrtest.any.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
image.IsCLR.Should().BeTrue();
}
}

[Test]
public void IsCLR_With_Native_Image_Returns_False()
{
var file = Utils.GetFileFromResources("nativetest.x86.dll");

using (var image = PortableExecutableImage.FromStream(file))
{
image.IsCLR.Should().BeFalse();
}
}

[Test]
public void IsSigned_Returns_False()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.IsSigned.Should().BeFalse();
}
}

[Test]
public void DOSHeader_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.DOSHeader.Should().NotBeNull();
}
}

[Test]
public void DOSStub_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.DOSStub.Should().NotBeNull();
}
}

[Test]
public void NTHeaders_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.NTHeaders.Should().NotBeNull();
}
}

[Test]
public void FileHeader_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.NTHeaders.FileHeader.Should().NotBeNull();
}
}

[Test]
public void OptionalHeader_Is_Not_Null()
{
var file = Utils.GetFileFromResources(Utils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.NTHeaders.OptionalHeader.Should().NotBeNull();
}
}
}
}
Loading

0 comments on commit 5991516

Please sign in to comment.