-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Understanding assembly type from source (#1529)
* Understanding assembly type from source * review comments * review comments
- Loading branch information
Showing
18 changed files
with
482 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Reflection.PortableExecutable; | ||
|
||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers; | ||
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; | ||
|
||
public class PEReaderHelper | ||
{ | ||
private readonly IFileHelper fileHelper; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PEReaderHelper"/> class. | ||
/// </summary> | ||
public PEReaderHelper() : this(new FileHelper()) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PEReaderHelper"/> class. | ||
/// </summary> | ||
/// <param name="fileHelper">File helper.</param> | ||
public PEReaderHelper(IFileHelper fileHelper) | ||
{ | ||
this.fileHelper = fileHelper; | ||
} | ||
|
||
/// <summary> | ||
/// Determines assembly type from file. | ||
/// </summary> | ||
public AssemblyType GetAssemblyType(string filePath) | ||
{ | ||
var assemblyType = AssemblyType.None; | ||
|
||
try | ||
{ | ||
using (var fileStream = this.fileHelper.GetStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
using (var peReader = new PEReader(fileStream)) | ||
{ | ||
// Resources for PEReader: | ||
// 1. https://msdn.microsoft.com/library/windows/desktop/ms680547(v=vs.85).aspx?id=19509 | ||
// 2. https://github.com/dotnet/corefx/tree/master/src/System.Reflection.Metadata | ||
|
||
var peHeaders = peReader.PEHeaders; | ||
var corHeader = peHeaders.CorHeader; | ||
var corHeaderStartOffset = peHeaders.CorHeaderStartOffset; | ||
|
||
assemblyType = (corHeader != null && corHeaderStartOffset >= 0) ? | ||
AssemblyType.Managed : | ||
AssemblyType.Native; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
EqtTrace.Warning("PEReaderHelper.GetAssemblyType: failed to determine assembly type: {0} for assembly: {1}", ex, filePath); | ||
} | ||
|
||
if (EqtTrace.IsInfoEnabled) | ||
{ | ||
EqtTrace.Info("PEReaderHelper.GetAssemblyType: Determined assemblyType:'{0}' for source: '{1}'", assemblyType, filePath); | ||
} | ||
|
||
return assemblyType; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...soft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TestPlatformRoot Condition="$(TestPlatformRoot) == ''">..\..\</TestPlatformRoot> | ||
<TestProject>true</TestProject> | ||
</PropertyGroup> | ||
<Import Project="$(TestPlatformRoot)scripts/build/TestPlatform.Settings.targets" /> | ||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp1.0;net451</TargetFrameworks> | ||
<AssemblyName>Microsoft.TestPlatform.Common.PlatformTests</AssemblyName> | ||
<EnableCodeAnalysis>true</EnableCodeAnalysis> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.TestPlatform.TestUtilities\Microsoft.TestPlatform.TestUtilities.csproj" /> | ||
<PackageReference Include="Microsoft.TestPlatform.TestAsset.NativeCPP"> | ||
<Version>2.0.0</Version> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' "> | ||
<Reference Include="System.Runtime" /> | ||
<Reference Include="System" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Runtime.Serialization" /> | ||
</ItemGroup> | ||
<Import Project="$(TestPlatformRoot)scripts\build\TestPlatform.targets" /> | ||
</Project> |
78 changes: 78 additions & 0 deletions
78
test/Microsoft.TestPlatform.Common.PlatformTests/PEReaderHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace TestPlatform.Common.UnitTests.Utilities | ||
{ | ||
using Microsoft.TestPlatform.TestUtilities; | ||
using Microsoft.VisualStudio.TestPlatform.Common.Utilities; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class PEReaderHelperTests : IntegrationTestBase | ||
{ | ||
private PEReaderHelper peReaderHelper; | ||
|
||
public PEReaderHelperTests() | ||
{ | ||
this.peReaderHelper = new PEReaderHelper(); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("net451")] | ||
[DataRow("netcoreapp1.0")] | ||
[DataRow("netcoreapp2.0")] | ||
public void GetAssemblyTypeForManagedDll(string framework) | ||
{ | ||
var assemblyPath = this.testEnvironment.GetTestAsset("SimpleTestProject3.dll", framework); | ||
var assemblyType = this.peReaderHelper.GetAssemblyType(assemblyPath); | ||
|
||
Assert.AreEqual(AssemblyType.Managed, assemblyType); | ||
} | ||
|
||
[TestMethod] | ||
public void GetAssemblyTypeForNativeDll() | ||
{ | ||
var assemblyPath = $@"{this.testEnvironment.PackageDirectory}\microsoft.testplatform.testasset.nativecpp\2.0.0\contentFiles\any\any\Microsoft.TestPlatform.TestAsset.NativeCPP.dll"; | ||
var assemblyType = this.peReaderHelper.GetAssemblyType(assemblyPath); | ||
|
||
Assert.AreEqual(AssemblyType.Native, assemblyType); | ||
} | ||
|
||
[TestMethod] | ||
public void GetAssemblyTypeForManagedExe() | ||
{ | ||
var assemblyPath = this.testEnvironment.GetTestAsset("ConsoleManagedApp.exe", "net451"); | ||
var assemblyType = this.peReaderHelper.GetAssemblyType(assemblyPath); | ||
|
||
Assert.AreEqual(AssemblyType.Managed, assemblyType); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("netcoreapp1.0")] | ||
[DataRow("netcoreapp2.0")] | ||
public void GetAssemblyTypeForNetCoreManagedExe(string framework) | ||
{ | ||
var assemblyPath = this.testEnvironment.GetTestAsset("ConsoleManagedApp.dll", framework); | ||
var assemblyType = this.peReaderHelper.GetAssemblyType(assemblyPath); | ||
|
||
Assert.AreEqual(AssemblyType.Managed, assemblyType); | ||
} | ||
|
||
[TestMethod] | ||
public void GetAssemblyTypeForNativeExe() | ||
{ | ||
var assemblyPath = $@"{this.testEnvironment.PackageDirectory}\microsoft.testplatform.testasset.nativecpp\2.0.0\contentFiles\any\any\Microsoft.TestPlatform.TestAsset.ConsoleNativeApp.exe"; | ||
var assemblyType = this.peReaderHelper.GetAssemblyType(assemblyPath); | ||
|
||
Assert.AreEqual(AssemblyType.Native, assemblyType); | ||
} | ||
|
||
[TestMethod] | ||
public void GetAssemblyTypeShouldReturnNoneInCaseOfError() | ||
{ | ||
var assemblyType = this.peReaderHelper.GetAssemblyType("invalidFile.dll"); | ||
|
||
Assert.AreEqual(AssemblyType.None, assemblyType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// ConsoleNativeApp.cpp : Defines the entry point for the console application. | ||
// | ||
|
||
#include "stdafx.h" | ||
|
||
|
||
int main() | ||
{ | ||
return 0; | ||
} | ||
|
Oops, something went wrong.