1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/pe/find.h"
16 :
17 : #include "base/win/scoped_handle.h"
18 : #include "gtest/gtest.h"
19 : #include "sawbuck/common/com_utils.h"
20 : #include "syzygy/core/file_util.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pe/unittest_util.h"
23 :
24 : namespace pe {
25 :
26 : namespace {
27 :
28 : class PeFindTest: public testing::PELibUnitTest {
29 : // Insert your customizations here.
30 : };
31 :
32 : } // namespace
33 :
34 E : TEST_F(PeFindTest, PeAndPdbAreMatchedMissingFiles) {
35 : EXPECT_FALSE(PeAndPdbAreMatched(
36 : base::FilePath(L"nonexistent_pe_file.dll"),
37 E : base::FilePath(L"nonexistent_pdb_file.pdb")));
38 E : }
39 :
40 E : TEST_F(PeFindTest, PeAndPdbAreMatchedMismatchedInputs) {
41 : EXPECT_FALSE(PeAndPdbAreMatched(
42 : testing::GetOutputRelativePath(testing::kTestDllName),
43 E : testing::GetOutputRelativePath(L"pe_unittests.exe.pdb")));
44 E : }
45 :
46 E : TEST_F(PeFindTest, PeAndPdbAreMatched) {
47 : EXPECT_TRUE(PeAndPdbAreMatched(
48 : testing::GetOutputRelativePath(testing::kTestDllName),
49 E : testing::GetOutputRelativePath(testing::kTestDllPdbName)));
50 E : }
51 :
52 E : TEST_F(PeFindTest, PeFindTestDllNoHint) {
53 : const base::FilePath module_path(testing::GetOutputRelativePath(
54 E : testing::kTestDllName));
55 :
56 E : PEFile pe_file;
57 E : ASSERT_TRUE(pe_file.Init(module_path));
58 :
59 E : PEFile::Signature module_signature;
60 E : pe_file.GetSignature(&module_signature);
61 :
62 E : base::FilePath found_path;
63 E : EXPECT_TRUE(FindModuleBySignature(module_signature, &found_path));
64 :
65 E : EXPECT_SAME_FILE(module_path, found_path);
66 E : }
67 :
68 E : TEST_F(PeFindTest, PeFindTestDllWithHint) {
69 : const base::FilePath orig_module_path(testing::GetOutputRelativePath(
70 E : testing::kTestDllName));
71 : const base::FilePath test_data_module_path(
72 E : testing::GetExeTestDataRelativePath(testing::kTestDllName));
73 :
74 E : PEFile pe_file;
75 E : ASSERT_TRUE(pe_file.Init(orig_module_path));
76 :
77 E : PEFile::Signature module_signature;
78 E : pe_file.GetSignature(&module_signature);
79 :
80 : // We expect the version of test_dll.dll in test_data to be found first
81 : // because we provide an explicit hint guiding the search in that direction.
82 E : base::FilePath found_path = test_data_module_path;
83 E : EXPECT_TRUE(FindModuleBySignature(module_signature, &found_path));
84 :
85 E : EXPECT_SAME_FILE(test_data_module_path, found_path);
86 E : }
87 :
88 E : TEST_F(PeFindTest, PeFindTestDllPdbNoHint) {
89 : // We have to be careful to use the output relative path, rather than simply
90 : // the executable relative path. This is because in the coverage unittests
91 : // pe_unittests.exe and test_dll.dll are copied to a new output directory
92 : // that contains the instrumented binaries. The copied test_dll.dll still
93 : // refers to the original test_dll.pdb in the Debug or Release output
94 : // directory, so that's the one that will be found first.
95 : const base::FilePath module_path(testing::GetOutputRelativePath(
96 E : testing::kTestDllName));
97 : const base::FilePath pdb_path(testing::GetOutputRelativePath(
98 E : testing::kTestDllPdbName));
99 :
100 E : base::FilePath found_path;
101 E : EXPECT_TRUE(FindPdbForModule(module_path, &found_path));
102 :
103 E : EXPECT_SAME_FILE(pdb_path, found_path);
104 E : }
105 :
106 E : TEST_F(PeFindTest, PeFindTestDllPdbWithHint) {
107 : const base::FilePath module_path(testing::GetOutputRelativePath(
108 E : testing::kTestDllName));
109 : const base::FilePath pdb_path(testing::GetExeTestDataRelativePath(
110 E : testing::kTestDllPdbName));
111 :
112 : // We provide an explicit hint to look in the test_data directory first. Even
113 : // though this is not the path that will be found in the debug data directory
114 : // it should be found first.
115 E : base::FilePath found_path = pdb_path;
116 E : EXPECT_TRUE(FindPdbForModule(module_path, &found_path));
117 :
118 E : EXPECT_SAME_FILE(pdb_path, found_path);
119 E : }
120 :
121 : } // namespace pe
|