1 : // Copyright 2012 Google Inc.
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 : #include "syzygy/pe/find.h"
15 :
16 : #include "base/win/scoped_handle.h"
17 : #include "gtest/gtest.h"
18 : #include "sawbuck/common/com_utils.h"
19 : #include "syzygy/core/file_util.h"
20 : #include "syzygy/core/unittest_util.h"
21 : #include "syzygy/pe/unittest_util.h"
22 :
23 : namespace pe {
24 :
25 : namespace {
26 :
27 : // A utility for ensuring that two file paths point to the same file. Upon
28 : // failure, outputs the actual paths as well.
29 : ::testing::AssertionResult AssertAreSameFile(const char* path1_expr,
30 : const char* path2_expr,
31 : const FilePath& path1,
32 E : const FilePath& path2) {
33 E : core::FilePathCompareResult result = core::CompareFilePaths(path1, path2);
34 E : if (result == core::kEquivalentFilePaths)
35 E : return ::testing::AssertionSuccess();
36 :
37 : return ::testing::AssertionFailure() << "FilePathsReferToSameFile("
38 : << path1_expr << ", " << path2_expr << ") returned " << result
39 : << ", expected " << core::kEquivalentFilePaths << " (" << path1_expr
40 : << " = \"" << path1.value() << "\", " << path2_expr << " = \""
41 i : << path2.value() << "\").";
42 E : }
43 :
44 : // A gtest-like macro for ensuring two paths refer to the same file.
45 : #define EXPECT_SAME_FILE(path1, path2) \
46 : EXPECT_PRED_FORMAT2(AssertAreSameFile, path1, path2)
47 :
48 : class FindTest: public testing::PELibUnitTest {
49 : // Insert your customizations here.
50 : };
51 :
52 : } // namespace
53 :
54 E : TEST_F(FindTest, PeAndPdbAreMatchedMissingFiles) {
55 : EXPECT_FALSE(PeAndPdbAreMatched(
56 : FilePath(L"nonexistent_pe_file.dll"),
57 E : FilePath(L"nonexistent_pdb_file.pdb")));
58 E : }
59 :
60 E : TEST_F(FindTest, PeAndPdbAreMatchedMismatchedInputs) {
61 : EXPECT_FALSE(PeAndPdbAreMatched(
62 : testing::GetOutputRelativePath(kDllName),
63 E : testing::GetOutputRelativePath(L"pe_unittests.pdb")));
64 E : }
65 :
66 E : TEST_F(FindTest, PeAndPdbAreMatched) {
67 : EXPECT_TRUE(PeAndPdbAreMatched(
68 : testing::GetOutputRelativePath(kDllName),
69 E : testing::GetOutputRelativePath(kDllPdbName)));
70 E : }
71 :
72 E : TEST_F(FindTest, FindTestDll) {
73 E : const FilePath module_path(testing::GetOutputRelativePath(kDllName));
74 :
75 E : PEFile pe_file;
76 E : ASSERT_TRUE(pe_file.Init(module_path));
77 :
78 E : PEFile::Signature module_signature;
79 E : pe_file.GetSignature(&module_signature);
80 :
81 E : FilePath found_path;
82 E : EXPECT_TRUE(FindModuleBySignature(module_signature, &found_path));
83 :
84 E : EXPECT_SAME_FILE(module_path, found_path);
85 E : }
86 :
87 E : TEST_F(FindTest, FindTestDllPdb) {
88 : // We have to be careful to use the output relative path, rather than simply
89 : // the executable relative path. This is because in the coverage unittests
90 : // pe_unittests.exe and test_dll.dll are copied to a new output directory
91 : // that contains the instrumented binaries. The copied test_dll.dll still
92 : // refers to the original test_dll.pdb in the Debug or Release output
93 : // directory, so that's the one that will be found first.
94 E : const FilePath module_path(testing::GetOutputRelativePath(kDllName));
95 E : const FilePath pdb_path(testing::GetOutputRelativePath(kDllPdbName));
96 :
97 E : FilePath found_path;
98 E : EXPECT_TRUE(FindPdbForModule(module_path, &found_path));
99 :
100 E : EXPECT_SAME_FILE(pdb_path, found_path);
101 E : }
102 :
103 : } // namespace pe
|