Skip to content

Commit 0318762

Browse files
author
Lukasz Mendakiewicz
committed
Discover dynamically linked Google Test executables
1 parent 5d0897e commit 0318762

16 files changed

Lines changed: 494 additions & 7 deletions

GoogleTestAdapter/Core.Tests/GoogleTestDiscovererTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file has been modified by Microsoft on 8/2017.
1+
// This file has been modified by Microsoft on 9/2017.
22

33
using System;
44
using System.Collections.Generic;
@@ -82,6 +82,26 @@ public void IsGoogleTestExecutable_WithoutIndicatorFile_IsRecognizedAsTestExecut
8282
}
8383
}
8484

85+
[TestMethod]
86+
[TestCategory(Unit)]
87+
public void IsGoogleTestExecutable_DependingOnGtestDll_IsRecognizedAsTestExecutable()
88+
{
89+
bool result = GoogleTestDiscoverer
90+
.IsGoogleTestExecutable(TestResources.FakeGtestDllExe, "", TestEnvironment.Logger);
91+
92+
result.Should().BeTrue();
93+
}
94+
95+
[TestMethod]
96+
[TestCategory(Unit)]
97+
public void IsGoogleTestExecutable_DependingOnGtestDllX64_IsRecognizedAsTestExecutable()
98+
{
99+
bool result = GoogleTestDiscoverer
100+
.IsGoogleTestExecutable(TestResources.FakeGtestDllExeX64, "", TestEnvironment.Logger);
101+
102+
result.Should().BeTrue();
103+
}
104+
85105
[TestMethod]
86106
[TestCategory(Integration)]
87107
public void GetTestsFromExecutable_SampleTestsDebug_FindsTestsWithLocation()

GoogleTestAdapter/Core/GoogleTestConstants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file has been modified by Microsoft on 6/2017.
1+
// This file has been modified by Microsoft on 9/2017.
22

33
using System;
44

@@ -30,6 +30,8 @@ public static class GoogleTestConstants
3030
public const string ParameterizedTestMarker = " # GetParam() = ";
3131
public const string TypedTestMarker = ". # TypeParam = ";
3232

33+
public const string GoogleTestDllMarker = "gtest.dll";
34+
3335
public static readonly string[] GoogleTestExecutableMarkers =
3436
{
3537
"This program contains tests written using Google Test. You can use the",

GoogleTestAdapter/Core/GoogleTestDiscoverer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public static bool IsGoogleTestExecutable(string executable, string customRegex,
104104

105105
if (string.IsNullOrWhiteSpace(customRegex))
106106
{
107-
if (Utils.BinaryFileContainsStrings(executable, Encoding.ASCII, GoogleTestConstants.GoogleTestExecutableMarkers))
107+
if (PeParser.FindImport(executable, GoogleTestConstants.GoogleTestDllMarker, StringComparison.OrdinalIgnoreCase, logger)
108+
|| Utils.BinaryFileContainsStrings(executable, Encoding.ASCII, GoogleTestConstants.GoogleTestExecutableMarkers))
108109
{
109110
return true;
110111
}

GoogleTestAdapter/DiaResolver/PeParser.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,43 @@ private static void ParsePeFile(string executable, ILogger logger, Action<Loaded
246246
}
247247
}
248248

249-
public static List<string> ParseImports(string executable, ILogger logger)
249+
private static void ProcessImports(string executable, ILogger logger, Func<string, bool> predicate)
250250
{
251-
var imports = new List<string>();
252251
ParsePeFile(executable, logger, (image) =>
253252
{
253+
bool shouldContinue = true;
254254
uint size = 0u;
255255
var directoryEntry = (IMAGE_IMPORT_DESCRIPTOR*)NativeMethods.ImageDirectoryEntryToData(image.MappedAddress, 0, 1, &size);
256-
while (directoryEntry->OriginalFirstThunk != 0u)
256+
while (shouldContinue && directoryEntry->OriginalFirstThunk != 0u)
257257
{
258-
imports.Add(GetString(image, directoryEntry->Name));
258+
shouldContinue = predicate(GetString(image, directoryEntry->Name));
259259
directoryEntry++;
260260
}
261261
});
262+
}
263+
264+
public static List<string> ParseImports(string executable, ILogger logger)
265+
{
266+
var imports = new List<string>();
267+
ProcessImports(executable, logger, (import) =>
268+
{
269+
imports.Add(import);
270+
return true; // Always continue.
271+
});
262272
return imports;
263273
}
264274

275+
public static bool FindImport(string executable, string import, StringComparison comparisonType, ILogger logger)
276+
{
277+
var found = false;
278+
ProcessImports(executable, logger, (currentImport) =>
279+
{
280+
found = String.Compare(import, currentImport, comparisonType) == 0;
281+
return !found; // Continue only if not found yet.
282+
});
283+
return found;
284+
}
285+
265286
private static string PtrToStringUtf8(IntPtr ptr)
266287
{
267288
if (ptr == IntPtr.Zero)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__declspec(dllimport) void FakeGtestDllLibrary_ExportedFunction();
2+
3+
int main()
4+
{
5+
FakeGtestDllLibrary_ExportedFunction();
6+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|x64">
5+
<Configuration>Debug</Configuration>
6+
<Platform>x64</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|x64">
9+
<Configuration>Release</Configuration>
10+
<Platform>x64</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{05C435E1-603C-4402-B28C-E54932F3131C}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>FakeGtestDllApplication</RootNamespace>
17+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
18+
</PropertyGroup>
19+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
20+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Common.props))\Common.props" />
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
22+
<ConfigurationType>Application</ConfigurationType>
23+
<UseDebugLibraries>true</UseDebugLibraries>
24+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '10.0'">v100</PlatformToolset>
25+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '11.0'">v110</PlatformToolset>
26+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '12.0'">v120</PlatformToolset>
27+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '14.0'">v140</PlatformToolset>
28+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
29+
<CharacterSet>Unicode</CharacterSet>
30+
</PropertyGroup>
31+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
32+
<ConfigurationType>Application</ConfigurationType>
33+
<UseDebugLibraries>false</UseDebugLibraries>
34+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '10.0'">v100</PlatformToolset>
35+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '11.0'">v110</PlatformToolset>
36+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '12.0'">v120</PlatformToolset>
37+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '14.0'">v140</PlatformToolset>
38+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
43+
<ImportGroup Label="ExtensionSettings">
44+
</ImportGroup>
45+
<ImportGroup Label="Shared">
46+
</ImportGroup>
47+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
48+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
49+
</ImportGroup>
50+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
51+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
52+
</ImportGroup>
53+
<PropertyGroup Label="UserMacros" />
54+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
55+
<LinkIncremental>true</LinkIncremental>
56+
</PropertyGroup>
57+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
58+
<LinkIncremental>false</LinkIncremental>
59+
</PropertyGroup>
60+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
61+
<ClCompile>
62+
<WarningLevel>Level3</WarningLevel>
63+
<Optimization>Disabled</Optimization>
64+
<SDLCheck>true</SDLCheck>
65+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
66+
<PrecompiledHeader />
67+
</ClCompile>
68+
<Link>
69+
<SubSystem>Console</SubSystem>
70+
<GenerateDebugInformation>true</GenerateDebugInformation>
71+
</Link>
72+
</ItemDefinitionGroup>
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<Optimization>Disabled</Optimization>
77+
<SDLCheck>true</SDLCheck>
78+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
79+
<PrecompiledHeader />
80+
</ClCompile>
81+
<Link>
82+
<SubSystem>Console</SubSystem>
83+
<GenerateDebugInformation>true</GenerateDebugInformation>
84+
</Link>
85+
</ItemDefinitionGroup>
86+
<ItemGroup>
87+
<ClCompile Include="Application.cpp" />
88+
</ItemGroup>
89+
<ItemGroup>
90+
<ProjectReference Include="FakeGtestDllLibrary-x64.vcxproj">
91+
<Project>{8c66b902-b3b3-40c1-b178-7f5405f654b0}</Project>
92+
</ProjectReference>
93+
</ItemGroup>
94+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
95+
<ImportGroup Label="ExtensionTargets">
96+
</ImportGroup>
97+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="Application.cpp" />
5+
</ItemGroup>
6+
</Project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{C2601F9E-8B97-4901-85F4-2531102543C8}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>FakeGtestDllApplication</RootNamespace>
17+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
18+
</PropertyGroup>
19+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
20+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Common.props))\Common.props" />
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
22+
<ConfigurationType>Application</ConfigurationType>
23+
<UseDebugLibraries>true</UseDebugLibraries>
24+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '10.0'">v100</PlatformToolset>
25+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '11.0'">v110</PlatformToolset>
26+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '12.0'">v120</PlatformToolset>
27+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '14.0'">v140</PlatformToolset>
28+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
29+
<CharacterSet>Unicode</CharacterSet>
30+
</PropertyGroup>
31+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
32+
<ConfigurationType>Application</ConfigurationType>
33+
<UseDebugLibraries>false</UseDebugLibraries>
34+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '10.0'">v100</PlatformToolset>
35+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '11.0'">v110</PlatformToolset>
36+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '12.0'">v120</PlatformToolset>
37+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '14.0'">v140</PlatformToolset>
38+
<PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
43+
<ImportGroup Label="ExtensionSettings">
44+
</ImportGroup>
45+
<ImportGroup Label="Shared">
46+
</ImportGroup>
47+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
48+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
49+
</ImportGroup>
50+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
51+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
52+
</ImportGroup>
53+
<PropertyGroup Label="UserMacros" />
54+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
55+
<LinkIncremental>true</LinkIncremental>
56+
</PropertyGroup>
57+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
58+
<LinkIncremental>false</LinkIncremental>
59+
</PropertyGroup>
60+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<ClCompile>
62+
<WarningLevel>Level3</WarningLevel>
63+
<Optimization>Disabled</Optimization>
64+
<SDLCheck>true</SDLCheck>
65+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
66+
<PrecompiledHeader />
67+
</ClCompile>
68+
<Link>
69+
<SubSystem>Console</SubSystem>
70+
<GenerateDebugInformation>true</GenerateDebugInformation>
71+
</Link>
72+
</ItemDefinitionGroup>
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<Optimization>Disabled</Optimization>
77+
<SDLCheck>true</SDLCheck>
78+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
79+
<PrecompiledHeader />
80+
</ClCompile>
81+
<Link>
82+
<SubSystem>Console</SubSystem>
83+
<GenerateDebugInformation>true</GenerateDebugInformation>
84+
</Link>
85+
</ItemDefinitionGroup>
86+
<ItemGroup>
87+
<ClCompile Include="Application.cpp" />
88+
</ItemGroup>
89+
<ItemGroup>
90+
<ProjectReference Include="FakeGtestDllLibrary.vcxproj">
91+
<Project>{db701549-3f79-4d1d-9db9-ca55683d679e}</Project>
92+
</ProjectReference>
93+
</ItemGroup>
94+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
95+
<ImportGroup Label="ExtensionTargets">
96+
</ImportGroup>
97+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="Application.cpp" />
5+
</ItemGroup>
6+
</Project>

0 commit comments

Comments
 (0)