-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Understanding assembly type from source #1529
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(FileHelper fileHelper) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{ | ||
this.fileHelper = fileHelper; | ||
} | ||
|
||
/// <summary> | ||
/// Determines assembly type from file. | ||
/// </summary> | ||
public AssemblyType GetAssemblyType(string filePath) | ||
{ | ||
var assemblyType = AssemblyType.None; | ||
|
||
try | ||
{ | ||
using (var fileStream = fileHelper.GetStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add link to documentation in comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
assemblyType = (corHeader != null && corHeaderStartOffset >= 0) ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you point the documentation for how to determine if the assembly is managed or native There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation links added in above lines |
||
AssemblyType.Managed : | ||
AssemblyType.Native; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
EqtTrace.Warning("GetAssemblyTypeFromAssemblyMetadata: failed to determine assembly type: {0} for assembly: {1}", ex, filePath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Keep message prefix to Classname.Method name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
if (EqtTrace.IsInfoEnabled) | ||
{ | ||
EqtTrace.Info("AssemblyMetadataProvider.GetAssemblyType: Determined assemblyType:'{0}' for source: '{1}'", assemblyType, filePath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: change the class name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
return assemblyType; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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> | ||
<OutputType Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">Exe</OutputType> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this line is not required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
<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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please never upload nupkg with same version after changing the content. It will leads to different behavior based on user nuget cache. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not doing in this as we are only using it this nuget. Will update if happens in future changes |
||
</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> |
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
<ProjectGuid>{076CE7E6-92E8-49FD-9D98-57D377FAA46E}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>CPPSimpleProj</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0.15063.0</WindowsTargetPlatformVersion> | ||
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion> | ||
<ProjectName>Microsoft.TestPlatform.TestAsset.NativeCPP</ProjectName> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why ConsoleNativeApp.cpp is binary file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
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; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this class Singleton?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not required. Removed.