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 "syzygy/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 : // Make sure we can do this on size and time stamp alone, as those
63 : // are the symbol server signature.
64 E : PEFile::Signature search_signature;
65 E : search_signature.path = module_signature.path;
66 E : search_signature.module_size = module_signature.module_size;
67 : search_signature.module_time_date_stamp =
68 E : module_signature.module_time_date_stamp;
69 :
70 E : base::FilePath found_path;
71 E : EXPECT_TRUE(FindModuleBySignature(search_signature, &found_path));
72 :
73 E : EXPECT_SAME_FILE(module_path, found_path);
74 E : }
75 :
76 E : TEST_F(PeFindTest, PeFindTestDllWithHint) {
77 : const base::FilePath orig_module_path(testing::GetOutputRelativePath(
78 E : testing::kTestDllName));
79 : const base::FilePath test_data_module_path(
80 E : testing::GetExeTestDataRelativePath(testing::kTestDllName));
81 :
82 E : PEFile pe_file;
83 E : ASSERT_TRUE(pe_file.Init(orig_module_path));
84 :
85 E : PEFile::Signature module_signature;
86 E : pe_file.GetSignature(&module_signature);
87 :
88 : // We expect the version of test_dll.dll in test_data to be found first
89 : // because we provide an explicit hint guiding the search in that direction.
90 E : base::FilePath found_path = test_data_module_path;
91 E : EXPECT_TRUE(FindModuleBySignature(module_signature, &found_path));
92 :
93 E : EXPECT_SAME_FILE(test_data_module_path, found_path);
94 E : }
95 :
96 E : TEST_F(PeFindTest, PeFindTestDllPdbNoHint) {
97 : // We have to be careful to use the output relative path, rather than simply
98 : // the executable relative path. This is because in the coverage unittests
99 : // pe_unittests.exe and test_dll.dll are copied to a new output directory
100 : // that contains the instrumented binaries. The copied test_dll.dll still
101 : // refers to the original test_dll.pdb in the Debug or Release output
102 : // directory, so that's the one that will be found first.
103 : const base::FilePath module_path(testing::GetOutputRelativePath(
104 E : testing::kTestDllName));
105 : const base::FilePath pdb_path(testing::GetOutputRelativePath(
106 E : testing::kTestDllPdbName));
107 :
108 E : base::FilePath found_path;
109 E : EXPECT_TRUE(FindPdbForModule(module_path, &found_path));
110 :
111 E : EXPECT_SAME_FILE(pdb_path, found_path);
112 E : }
113 :
114 E : TEST_F(PeFindTest, PeFindTestDllPdbWithHint) {
115 : const base::FilePath module_path(testing::GetOutputRelativePath(
116 E : testing::kTestDllName));
117 : const base::FilePath pdb_path(testing::GetExeTestDataRelativePath(
118 E : testing::kTestDllPdbName));
119 :
120 : // We provide an explicit hint to look in the test_data directory first. Even
121 : // though this is not the path that will be found in the debug data directory
122 : // it should be found first.
123 E : base::FilePath found_path = pdb_path;
124 E : EXPECT_TRUE(FindPdbForModule(module_path, &found_path));
125 :
126 E : EXPECT_SAME_FILE(pdb_path, found_path);
127 E : }
128 :
129 : } // namespace pe
|