-
Notifications
You must be signed in to change notification settings - Fork 346
Understanding assembly type from source #1529
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
Changes from all 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(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)) | ||
|
Contributor
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 ReadWrite
Contributor
Author
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. Its share permission for other threads |
||
| 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) ? | ||
|
Contributor
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
Contributor
Author
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("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); | ||
|
Contributor
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. this should go in resources
Contributor
Author
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. these are logs only. |
||
| } | ||
|
|
||
| return assemblyType; | ||
| } | ||
| } | ||
| } | ||
| 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> | ||
|
Contributor
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.
Contributor
Author
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" /> | ||
|
Contributor
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?
Contributor
Author
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.