From d3c3315c05005f927e81747221187adf94512e22 Mon Sep 17 00:00:00 2001 From: abhishkk Date: Thu, 5 Apr 2018 20:25:03 +0530 Subject: [PATCH 1/3] Understanding assembly type from source --- TestPlatform.sln | 15 ++ .../Utilities/PEReaderHelper.cs | 55 ++++++ ...osoft.TestPlatform.Common.UnitTests.csproj | 6 + .../Utilities/PEReaderHelperTests.cs | 78 +++++++++ .../CPPSimpleProj/CPPSimpleProj.vcxproj | 2 +- .../ConsoleNativeApp/ConsoleNativeApp.cpp | Bin 0 -> 294 bytes .../ConsoleNativeApp/ConsoleNativeApp.vcxproj | 165 ++++++++++++++++++ .../ConsoleNativeApp.vcxproj.filters | 33 ++++ .../ConsoleNativeApp/stdafx.cpp | Bin 0 -> 608 bytes .../ConsoleNativeApp/stdafx.h | Bin 0 -> 642 bytes .../ConsoleNativeApp/targetver.h | Bin 0 -> 630 bytes ...rosoft.TestPlatform.Asset.NativeCPP.nuspec | 2 + .../ConsoleManagedApp.csproj | 12 ++ test/TestAssets/ConsoleManagedApp/Program.cs | 12 ++ 14 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs create mode 100644 test/Microsoft.TestPlatform.Common.UnitTests/Utilities/PEReaderHelperTests.cs create mode 100644 test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.cpp create mode 100644 test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj create mode 100644 test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj.filters create mode 100644 test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.cpp create mode 100644 test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.h create mode 100644 test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/targetver.h create mode 100644 test/TestAssets/ConsoleManagedApp/ConsoleManagedApp.csproj create mode 100644 test/TestAssets/ConsoleManagedApp/Program.cs diff --git a/TestPlatform.sln b/TestPlatform.sln index ab2597f00a..e72001f4a5 100644 --- a/TestPlatform.sln +++ b/TestPlatform.sln @@ -159,6 +159,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "perf", "perf", "{0D4DF78D-7 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "vstest.console.PlatformTests", "test\vstest.console.PlatformTests\vstest.console.PlatformTests.csproj", "{8C068694-23A2-47A2-A0DD-DB82D0AF0142}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleManagedApp", "test\TestAssets\ConsoleManagedApp\ConsoleManagedApp.csproj", "{A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -781,6 +783,18 @@ Global {8C068694-23A2-47A2-A0DD-DB82D0AF0142}.Release|x64.Build.0 = Release|Any CPU {8C068694-23A2-47A2-A0DD-DB82D0AF0142}.Release|x86.ActiveCfg = Release|Any CPU {8C068694-23A2-47A2-A0DD-DB82D0AF0142}.Release|x86.Build.0 = Release|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x64.ActiveCfg = Debug|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x64.Build.0 = Debug|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x86.ActiveCfg = Debug|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x86.Build.0 = Debug|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|Any CPU.Build.0 = Release|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x64.ActiveCfg = Release|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x64.Build.0 = Release|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x86.ActiveCfg = Release|Any CPU + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -849,6 +863,7 @@ Global {826CD5AF-44FA-40F6-B731-3980CADED8C0} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A} {0D4DF78D-7E5F-4516-B19F-E6AA71A1DBF4} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330} {8C068694-23A2-47A2-A0DD-DB82D0AF0142} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999} + {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0541B30C-FF51-4E28-B172-83F5F3934BCD} diff --git a/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs b/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs new file mode 100644 index 0000000000..2a9d13cf88 --- /dev/null +++ b/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs @@ -0,0 +1,55 @@ +// 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; + + public class PEReaderHelper + { + private static PEReaderHelper instance; + + /// + /// Gets the PEReaderHelper instance. + /// + internal static PEReaderHelper Instance => instance ?? (instance = new PEReaderHelper()); + + /// + /// Determines assembly type from file. + /// + public AssemblyType GetAssemblyType(string filePath) + { + var assemblyType = AssemblyType.None; + + try + { + using (var assemblyStream = File.Open(filePath, FileMode.Open, FileAccess.Read)) + using (var peReader = new PEReader(assemblyStream)) + { + 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("GetAssemblyTypeFromAssemblyMetadata: failed to determine assembly type: {0} for assembly: {1}", ex, filePath); + } + + if (EqtTrace.IsInfoEnabled) + { + EqtTrace.Info("AssemblyMetadataProvider.GetAssemblyType: Determined assemblyType:'{0}' for source: '{1}'", assemblyType, filePath); + } + + return assemblyType; + } + } +} diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj b/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj index 0c24139b03..7b090ae301 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj +++ b/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj @@ -10,6 +10,12 @@ net451;netcoreapp1.0 Microsoft.TestPlatform.Common.UnitTests + + + + 2.0.0 + + diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/Utilities/PEReaderHelperTests.cs b/test/Microsoft.TestPlatform.Common.UnitTests/Utilities/PEReaderHelperTests.cs new file mode 100644 index 0000000000..c3ce517c48 --- /dev/null +++ b/test/Microsoft.TestPlatform.Common.UnitTests/Utilities/PEReaderHelperTests.cs @@ -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); + } + } +} diff --git a/test/TestAssets/CPPSimpleTestProject/CPPSimpleProj/CPPSimpleProj.vcxproj b/test/TestAssets/CPPSimpleTestProject/CPPSimpleProj/CPPSimpleProj.vcxproj index 8d03529bc8..64881bccfa 100644 --- a/test/TestAssets/CPPSimpleTestProject/CPPSimpleProj/CPPSimpleProj.vcxproj +++ b/test/TestAssets/CPPSimpleTestProject/CPPSimpleProj/CPPSimpleProj.vcxproj @@ -22,7 +22,7 @@ {076CE7E6-92E8-49FD-9D98-57D377FAA46E} Win32Proj CPPSimpleProj - 10.0.15063.0 + 10.0.16299.0 Microsoft.TestPlatform.TestAsset.NativeCPP diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.cpp b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e041dfc7f1a1aee0446d9d6a4bd1a3f7eb09d3b3 GIT binary patch literal 294 zcmY+8!3x4K5JcxJ_z$68^wN6PljzxhDA7`(){;h1#9vq61}#cR_9Z)++1ImBA*Z!Y zYSnW#+H0#)Q3qY%l{!`~pQ_UW^@jI~eoO7SN7ayHw{Xx6?(i~2gey4w==@)rBh4}z z^!}4<)8ra!#B@cf#*^geQJ%F+i5dA3F3Gs^qgFPk?Z NK;bmkGIbw=(;FukE+hZ| literal 0 HcmV?d00001 diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj new file mode 100644 index 0000000000..e52de0bb3d --- /dev/null +++ b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj @@ -0,0 +1,165 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {84935EDB-ADBF-4438-815B-1E75EF608816} + Win32Proj + ConsoleNativeApp + 10.0.16299.0 + Microsoft.TestPlatform.TestAsset.ConsoleNativeApp + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + Create + Create + Create + Create + + + + + + \ No newline at end of file diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj.filters b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj.filters new file mode 100644 index 0000000000..5e29ba0d45 --- /dev/null +++ b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.cpp b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5001625e6ca43e02b589657d7416866e59d719f9 GIT binary patch literal 608 zcmZ`%yKcfj5S$t*|8OF8pr=DYC{_LY4fP)Eg6;4)6jC@T@@WyVbS_a z7$=OH`~R#bOvprBUJ)4Ca2L|Mp?G}rzBFDl?t9jC_%SvmZ#zTj(T>E(@WQBoFGfes zLKU0Dc-9yY$K3~3JB+HUPGuda*P*M&I4wHjN-<^R%&*BrdU>lQY^`VW59&Eg?`BcZ z(nm*qHu%7bOVd%GIhRgNL5R}1vomF<{)U-Q&#?lURYyK2o)vSm+hiO*u_4M5Yi#)< X-u|c+=ctUp+Ofl^iMQ$ZZ$zxG%cE$b literal 0 HcmV?d00001 diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.h b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.h new file mode 100644 index 0000000000000000000000000000000000000000..94d4ed877dbfd8cd2c783931c444cad664143b10 GIT binary patch literal 642 zcmaJ<%Z`FT5UjJw{sYnLM(@V#$-9^R0UQRwu#ECB^7Cp{4I>V}Q z8vEtLe;CsQ`f9DHxFP?Ev!}!l=AXA(tbaPXB)?e2)ZxZ=WBxB^dUob-#=8i1_Gje& z#NH7xP7*QWxk;u;fEfw0WxN;+co0fK2Vs4B9U*sm0{`t1wOo&Foy0~*EXI^K{ z&F{}p2USW{Xp2p>*G`#oJ!s%(q!H-M(Ty4fmG}kNtEQTB%)V1m=}BwwfWPvrT#@e@ zH0NG}74Ao{glS)#QXA|NYdIfY7hrMp+Ji@H`t9kzWx_SD6;~Ri;cvXH)6CO{-I1p_IJPQ#X=r zigZeSqQguJz35q;zt9^Q_C^^>*mmuXUCp&pw^fPoGX+dho4RCrySs7j@9_UipWkA5 OQDt4mH~x-^Z~X_?9czaG literal 0 HcmV?d00001 diff --git a/test/TestAssets/CPPSimpleTestProject/Microsoft.TestPlatform.Asset.NativeCPP.nuspec b/test/TestAssets/CPPSimpleTestProject/Microsoft.TestPlatform.Asset.NativeCPP.nuspec index aff044584e..1aa97e694e 100644 --- a/test/TestAssets/CPPSimpleTestProject/Microsoft.TestPlatform.Asset.NativeCPP.nuspec +++ b/test/TestAssets/CPPSimpleTestProject/Microsoft.TestPlatform.Asset.NativeCPP.nuspec @@ -19,5 +19,7 @@ + + diff --git a/test/TestAssets/ConsoleManagedApp/ConsoleManagedApp.csproj b/test/TestAssets/ConsoleManagedApp/ConsoleManagedApp.csproj new file mode 100644 index 0000000000..39d9d2d398 --- /dev/null +++ b/test/TestAssets/ConsoleManagedApp/ConsoleManagedApp.csproj @@ -0,0 +1,12 @@ + + + + + + + ConsoleManagedApp + Exe + netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;net451 + + + diff --git a/test/TestAssets/ConsoleManagedApp/Program.cs b/test/TestAssets/ConsoleManagedApp/Program.cs new file mode 100644 index 0000000000..247024f0a0 --- /dev/null +++ b/test/TestAssets/ConsoleManagedApp/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ConsoleManagedApp +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} From 7111cc365511abe7175c0de43100b08196b90b92 Mon Sep 17 00:00:00 2001 From: abhishkk Date: Thu, 5 Apr 2018 22:25:21 +0530 Subject: [PATCH 2/3] review comments --- TestPlatform.sln | 28 +++++++++--------- .../Microsoft.TestPlatform.Common.csproj | 1 + .../Utilities/PEReaderHelper.cs | 27 +++++++++++++---- .../Helpers/FileHelper.cs | 7 +++++ .../Helpers/Interfaces/IFileHelper.cs | 10 +++++++ ...t.TestPlatform.Common.PlatformTests.csproj | 27 +++++++++++++++++ .../PEReaderHelperTests.cs | 0 ...osoft.TestPlatform.Common.UnitTests.csproj | 6 ---- .../ConsoleNativeApp/ConsoleNativeApp.cpp | Bin 294 -> 135 bytes .../ConsoleNativeApp/stdafx.cpp | Bin 608 -> 295 bytes .../ConsoleNativeApp/stdafx.h | Bin 642 -> 305 bytes .../ConsoleNativeApp/targetver.h | Bin 630 -> 306 bytes test/TestAssets/TestAssets.sln/TestAssets.sln | 6 ++++ 13 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj rename test/{Microsoft.TestPlatform.Common.UnitTests/Utilities => Microsoft.TestPlatform.Common.PlatformTests}/PEReaderHelperTests.cs (100%) diff --git a/TestPlatform.sln b/TestPlatform.sln index e72001f4a5..8f2bb70cd0 100644 --- a/TestPlatform.sln +++ b/TestPlatform.sln @@ -159,7 +159,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "perf", "perf", "{0D4DF78D-7 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "vstest.console.PlatformTests", "test\vstest.console.PlatformTests\vstest.console.PlatformTests.csproj", "{8C068694-23A2-47A2-A0DD-DB82D0AF0142}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleManagedApp", "test\TestAssets\ConsoleManagedApp\ConsoleManagedApp.csproj", "{A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.TestPlatform.Common.PlatformTests", "test\Microsoft.TestPlatform.Common.PlatformTests\Microsoft.TestPlatform.Common.PlatformTests.csproj", "{24C7683D-2607-4901-B8EB-83A57E49E93D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -783,18 +783,18 @@ Global {8C068694-23A2-47A2-A0DD-DB82D0AF0142}.Release|x64.Build.0 = Release|Any CPU {8C068694-23A2-47A2-A0DD-DB82D0AF0142}.Release|x86.ActiveCfg = Release|Any CPU {8C068694-23A2-47A2-A0DD-DB82D0AF0142}.Release|x86.Build.0 = Release|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x64.ActiveCfg = Debug|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x64.Build.0 = Debug|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x86.ActiveCfg = Debug|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Debug|x86.Build.0 = Debug|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|Any CPU.Build.0 = Release|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x64.ActiveCfg = Release|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x64.Build.0 = Release|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x86.ActiveCfg = Release|Any CPU - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106}.Release|x86.Build.0 = Release|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Debug|x64.ActiveCfg = Debug|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Debug|x64.Build.0 = Debug|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Debug|x86.ActiveCfg = Debug|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Debug|x86.Build.0 = Debug|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Release|Any CPU.Build.0 = Release|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Release|x64.ActiveCfg = Release|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Release|x64.Build.0 = Release|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Release|x86.ActiveCfg = Release|Any CPU + {24C7683D-2607-4901-B8EB-83A57E49E93D}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -863,7 +863,7 @@ Global {826CD5AF-44FA-40F6-B731-3980CADED8C0} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A} {0D4DF78D-7E5F-4516-B19F-E6AA71A1DBF4} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330} {8C068694-23A2-47A2-A0DD-DB82D0AF0142} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999} - {A4AAB1DF-BAD4-4C75-A0FD-10EDC2F38106} = {8DA7CBD9-F17E-41B6-90C4-CFF55848A25A} + {24C7683D-2607-4901-B8EB-83A57E49E93D} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0541B30C-FF51-4E28-B172-83F5F3934BCD} diff --git a/src/Microsoft.TestPlatform.Common/Microsoft.TestPlatform.Common.csproj b/src/Microsoft.TestPlatform.Common/Microsoft.TestPlatform.Common.csproj index 1729412020..982e0e7d36 100644 --- a/src/Microsoft.TestPlatform.Common/Microsoft.TestPlatform.Common.csproj +++ b/src/Microsoft.TestPlatform.Common/Microsoft.TestPlatform.Common.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs b/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs index 2a9d13cf88..47b881a990 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs @@ -8,15 +8,28 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities 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 static PEReaderHelper instance; + private readonly IFileHelper fileHelper; /// - /// Gets the PEReaderHelper instance. + /// Initializes a new instance of the class. /// - internal static PEReaderHelper Instance => instance ?? (instance = new PEReaderHelper()); + public PEReaderHelper() : this(new FileHelper()) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// File helper. + public PEReaderHelper(FileHelper fileHelper) + { + this.fileHelper = fileHelper; + } /// /// Determines assembly type from file. @@ -27,9 +40,13 @@ public AssemblyType GetAssemblyType(string filePath) try { - using (var assemblyStream = File.Open(filePath, FileMode.Open, FileAccess.Read)) - using (var peReader = new PEReader(assemblyStream)) + using (var fileStream = 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; diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/FileHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/FileHelper.cs index c324607f50..de8b1bac5b 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/FileHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/FileHelper.cs @@ -48,6 +48,13 @@ public Stream GetStream(string filePath, FileMode mode, FileAccess access = File return new FileStream(filePath, mode, access); } + /// + public Stream GetStream(string filePath, FileMode mode, FileAccess access, FileShare share) + { + return new FileStream(filePath, mode, access, share); + } + + /// public IEnumerable EnumerateFiles( string directory, diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/Interfaces/IFileHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/Interfaces/IFileHelper.cs index c130248fc2..fa7393242e 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/Interfaces/IFileHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/Interfaces/IFileHelper.cs @@ -49,6 +49,16 @@ public interface IFileHelper /// A that supports read/write on the file. Stream GetStream(string filePath, FileMode mode, FileAccess access = FileAccess.ReadWrite); + /// + /// Gets a stream for the file. + /// + /// Path to the file. + /// for file operations. + /// for file operations. + /// for file operations. + /// A that supports read/write on the file. + Stream GetStream(string filePath, FileMode mode, FileAccess access, FileShare share); + /// /// Enumerates files which match a pattern (case insensitive) in a directory. /// diff --git a/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj b/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj new file mode 100644 index 0000000000..29bc190f1d --- /dev/null +++ b/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj @@ -0,0 +1,27 @@ + + + + ..\..\ + true + + + + netcoreapp1.0;net451 + Exe + Microsoft.TestPlatform.Common.PlatformTests + true + + + + + 2.0.0 + + + + + + + + + + diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/Utilities/PEReaderHelperTests.cs b/test/Microsoft.TestPlatform.Common.PlatformTests/PEReaderHelperTests.cs similarity index 100% rename from test/Microsoft.TestPlatform.Common.UnitTests/Utilities/PEReaderHelperTests.cs rename to test/Microsoft.TestPlatform.Common.PlatformTests/PEReaderHelperTests.cs diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj b/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj index 7b090ae301..0c24139b03 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj +++ b/test/Microsoft.TestPlatform.Common.UnitTests/Microsoft.TestPlatform.Common.UnitTests.csproj @@ -10,12 +10,6 @@ net451;netcoreapp1.0 Microsoft.TestPlatform.Common.UnitTests - - - - 2.0.0 - - diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.cpp b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/ConsoleNativeApp.cpp index e041dfc7f1a1aee0446d9d6a4bd1a3f7eb09d3b3..bad0955883a88dcd779054942eedcfc9f85c3128 100644 GIT binary patch literal 135 zcmXBLF%H5o3`XJoKZR8mSQ=)S5X{^mN*&cwaIC~dAjI7%@Wv;7A<(&MuZ6GC<<2Lw zF`3bUE}TWVlg~`7-kxMt)G62Yd+E<8niWYat~w$FU?ZB!GBFL^r NK;bmkGIbw=(;FukE+hZ| diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.cpp b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.cpp index 5001625e6ca43e02b589657d7416866e59d719f9..8d211a49695439ad1520c13e3aa5f777355af254 100644 GIT binary patch literal 295 zcmZXQv2MdK3`BQ*#UY(*@7aO`MW<$HhOUf6!wM8qkmR=h`%taXr7Ow1cgJx=ZGoom z!RL%UsxHMdrkEIQriLg!ErBa;OKmn{yC(@$!r#h`BaSLn6TeZ5pM1!9$bQCGObOR+ z#K?u8z237K2h2=?OV@31z21JWy-K4f|0QkBY{W^+-54s)J#Epj+f3%2>z0@E>AXkb si3^K$Az48o#IB;Wu`5_LY4fP)Eg6;4)6jC@T@@WyVbS_a z7$=OH`~R#bOvprBUJ)4Ca2L|Mp?G}rzBFDl?t9jC_%SvmZ#zTj(T>E(@WQBoFGfes zLKU0Dc-9yY$K3~3JB+HUPGuda*P*M&I4wHjN-<^R%&*BrdU>lQY^`VW59&Eg?`BcZ z(nm*qHu%7bOVd%GIhRgNL5R}1vomF<{)U-Q&#?lURYyK2o)vSm+hiO*u_4M5Yi#)< X-u|c+=ctUp+Ofl^iMQ$ZZ$zxG%cE$b diff --git a/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.h b/test/TestAssets/CPPSimpleTestProject/ConsoleNativeApp/stdafx.h index 94d4ed877dbfd8cd2c783931c444cad664143b10..b005a839def24829d348825ce6303a5538707527 100644 GIT binary patch literal 305 zcmY+9F^&Q;3`P5#!eU!A(<4MhPs0UF@-qpVNe0J3JGWo6(uyTTq~tw)`;Q|v4IICR zLn&pm)*p6#k)j7wk%)V8KrK7_V+<47j0Qt$9eZH3P<`%jBBu@VcYHw21;M@P|l z+taPI>V}Q z8vEtLe;CsQ`f9DHxFP?Ev!}!l=AXA(tbaPXB)?e2)ZxZ=WBxB^dUob-#=8i1_Gje& z#NH7xP7*QWxk;u;fEfw0WxN;+co0fK2Vs4B9U*sm0{`t1wOo&Foy0~*EXI^K{ z&F{}p2USW{Xp2p>*G`#oJ!s%(q!H-M(Ty4fmG}kNtEQTB%)V1m=}BwwfWPvrT#@e@ zH0NG}74Ao{glS)#QXA|NYdIfY7hrMp+Ji@H`t9kzWx_SD6;~Ri;cvXH)6CO{-I1p_IJPQ#X=r zigZeSqQguJz35q;zt9^Q_C^^>*mmuXUCp&pw^fPoGX+dho4RCrySs7j@9_UipWkA5 OQDt4mH~x-^Z~X_?9czaG diff --git a/test/TestAssets/TestAssets.sln/TestAssets.sln b/test/TestAssets/TestAssets.sln/TestAssets.sln index 35396765d0..c969b472f1 100644 --- a/test/TestAssets/TestAssets.sln/TestAssets.sln +++ b/test/TestAssets/TestAssets.sln/TestAssets.sln @@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DisableAppdomainTest1", ".. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DisableAppdomainTest2", "..\DisableAppdomainTest2\DisableAppdomainTest2.csproj", "{A09B21CC-F726-413A-B185-3AE1172BAED0}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleManagedApp", "..\ConsoleManagedApp\ConsoleManagedApp.csproj", "{132E4690-DE43-4684-BA05-6942155EEAB5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -117,6 +119,10 @@ Global {A09B21CC-F726-413A-B185-3AE1172BAED0}.Debug|Any CPU.Build.0 = Debug|Any CPU {A09B21CC-F726-413A-B185-3AE1172BAED0}.Release|Any CPU.ActiveCfg = Release|Any CPU {A09B21CC-F726-413A-B185-3AE1172BAED0}.Release|Any CPU.Build.0 = Release|Any CPU + {132E4690-DE43-4684-BA05-6942155EEAB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {132E4690-DE43-4684-BA05-6942155EEAB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {132E4690-DE43-4684-BA05-6942155EEAB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {132E4690-DE43-4684-BA05-6942155EEAB5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From ce10941767259c6aa919c7eeda1c5ca0602ee939 Mon Sep 17 00:00:00 2001 From: abhishkk Date: Mon, 9 Apr 2018 15:37:10 +0530 Subject: [PATCH 3/3] review comments --- .../Utilities/PEReaderHelper.cs | 8 ++++---- .../Microsoft.TestPlatform.Common.PlatformTests.csproj | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs b/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs index 47b881a990..7498d526db 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/PEReaderHelper.cs @@ -26,7 +26,7 @@ public PEReaderHelper() : this(new FileHelper()) /// Initializes a new instance of the class. /// /// File helper. - public PEReaderHelper(FileHelper fileHelper) + public PEReaderHelper(IFileHelper fileHelper) { this.fileHelper = fileHelper; } @@ -40,7 +40,7 @@ public AssemblyType GetAssemblyType(string filePath) try { - using (var fileStream = fileHelper.GetStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (var fileStream = this.fileHelper.GetStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var peReader = new PEReader(fileStream)) { // Resources for PEReader: @@ -58,12 +58,12 @@ public AssemblyType GetAssemblyType(string filePath) } catch (Exception ex) { - EqtTrace.Warning("GetAssemblyTypeFromAssemblyMetadata: failed to determine assembly type: {0} for assembly: {1}", ex, filePath); + EqtTrace.Warning("PEReaderHelper.GetAssemblyType: failed to determine assembly type: {0} for assembly: {1}", ex, filePath); } if (EqtTrace.IsInfoEnabled) { - EqtTrace.Info("AssemblyMetadataProvider.GetAssemblyType: Determined assemblyType:'{0}' for source: '{1}'", assemblyType, filePath); + EqtTrace.Info("PEReaderHelper.GetAssemblyType: Determined assemblyType:'{0}' for source: '{1}'", assemblyType, filePath); } return assemblyType; diff --git a/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj b/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj index 29bc190f1d..a60bcbe10d 100644 --- a/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj +++ b/test/Microsoft.TestPlatform.Common.PlatformTests/Microsoft.TestPlatform.Common.PlatformTests.csproj @@ -7,7 +7,6 @@ netcoreapp1.0;net451 - Exe Microsoft.TestPlatform.Common.PlatformTests true