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/zap_timestamp/zap_timestamp.h"
16 :
17 : #include "base/file_util.h"
18 : #include "base/files/scoped_temp_dir.h"
19 : #include "gtest/gtest.h"
20 : #include "syzygy/core/unittest_util.h"
21 :
22 : namespace zap_timestamp {
23 :
24 : namespace {
25 :
26 : // We don't bother with having specific data for the 'Coverage' case.
27 : #define TEST_DATA_PREFIX_0 L"syzygy\\zap_timestamp\\test_data\\"
28 : #ifdef NDEBUG
29 : #define TEST_DATA_PREFIX_1 L"Release\\"
30 : #else
31 : #define TEST_DATA_PREFIX_1 L"Debug\\"
32 : #endif
33 : #define TEST_DATA_PREFIX TEST_DATA_PREFIX_0 TEST_DATA_PREFIX_1
34 :
35 : struct RawPePdbPathPair {
36 : const wchar_t* pe_path;
37 : const wchar_t* pdb_path;
38 : };
39 : RawPePdbPathPair kRawTestPaths[] = {
40 : { TEST_DATA_PREFIX L"copy0\\test_dll.dll",
41 : TEST_DATA_PREFIX L"copy0\\test_dll.pdb" },
42 : { TEST_DATA_PREFIX L"copy1\\test_dll.dll",
43 : TEST_DATA_PREFIX L"copy1\\test_dll.pdb" },
44 : { TEST_DATA_PREFIX L"copy2\\test_dll.dll",
45 : TEST_DATA_PREFIX L"copy2\\test_dll.pdb" } };
46 :
47 : struct PePdbPathPair {
48 : base::FilePath pe_path;
49 : base::FilePath pdb_path;
50 : };
51 :
52 : class ZapTimestampTest : public testing::Test {
53 : public:
54 E : virtual void SetUp() OVERRIDE {
55 E : testing::Test::SetUp();
56 :
57 E : temp_dir_.CreateUniqueTempDir();
58 :
59 : // Get the full test data paths.
60 E : for (size_t i = 0; i < arraysize(kRawTestPaths); ++i) {
61 E : PePdbPathPair pair;
62 E : pair.pe_path = testing::GetSrcRelativePath(kRawTestPaths[i].pe_path);
63 E : pair.pdb_path = testing::GetSrcRelativePath(kRawTestPaths[i].pdb_path);
64 E : test_paths_.push_back(pair);
65 E : }
66 :
67 E : temp_pe_path_ = temp_dir_.path().Append(L"test_dll.dll");
68 E : temp_pdb_path_ = temp_dir_.path().Append(L"test_dll.pdb");
69 E : }
70 :
71 : void CopyTestData(const base::FilePath& pe_path,
72 E : const base::FilePath& pdb_path) {
73 E : ASSERT_TRUE(file_util::CopyFile(pe_path, temp_pe_path_));
74 E : ASSERT_TRUE(file_util::CopyFile(pdb_path, temp_pdb_path_));
75 E : }
76 :
77 E : void CopyTestData(size_t index) {
78 E : ASSERT_GT(test_paths_.size(), index);
79 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(
80 : test_paths_[index].pe_path, test_paths_[index].pdb_path));
81 E : }
82 :
83 : base::ScopedTempDir temp_dir_;
84 : std::vector<PePdbPathPair> test_paths_;
85 :
86 : base::FilePath temp_pe_path_;
87 : base::FilePath temp_pdb_path_;
88 : };
89 :
90 : } // namespace
91 :
92 E : TEST_F(ZapTimestampTest, InitFailsForNonExistentPath) {
93 E : ZapTimestamp zap;
94 E : EXPECT_FALSE(zap.Init(base::FilePath(L"nonexistent_pe_file.dll")));
95 E : }
96 :
97 E : TEST_F(ZapTimestampTest, InitFailsForMismatchedPeAndPdb) {
98 : ASSERT_NO_FATAL_FAILURE(CopyTestData(
99 E : test_paths_[0].pe_path, test_paths_[1].pdb_path));
100 E : ZapTimestamp zap;
101 E : EXPECT_FALSE(zap.Init(temp_pe_path_));
102 E : }
103 :
104 E : TEST_F(ZapTimestampTest, InitFailsWithMissingPdb) {
105 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(0));
106 E : ASSERT_TRUE(file_util::Delete(temp_pdb_path_, false));
107 E : ZapTimestamp zap;
108 E : EXPECT_FALSE(zap.Init(temp_pe_path_));
109 E : }
110 :
111 E : TEST_F(ZapTimestampTest, InitAutoFindPdb) {
112 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(0));
113 E : ZapTimestamp zap;
114 E : EXPECT_TRUE(zap.Init(temp_pe_path_));
115 E : EXPECT_EQ(temp_pdb_path_, zap.pdb_path());
116 E : }
117 :
118 E : TEST_F(ZapTimestampTest, InitExplicitPdb) {
119 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(0));
120 E : ZapTimestamp zap;
121 E : EXPECT_TRUE(zap.Init(temp_pe_path_, temp_pdb_path_));
122 E : }
123 :
124 E : TEST_F(ZapTimestampTest, IsIdempotent) {
125 : // Zap the first set of the PE and PDB files.
126 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(0));
127 E : ZapTimestamp zap0;
128 E : EXPECT_TRUE(zap0.Init(temp_pe_path_));
129 E : EXPECT_EQ(temp_pdb_path_, zap0.pdb_path());
130 E : EXPECT_TRUE(zap0.Zap(true, true));
131 :
132 : // Make a copy of the singly zapped files.
133 E : base::FilePath pe_path_0 = temp_dir_.path().Append(L"test_dll_0.dll");
134 E : base::FilePath pdb_path_0 = temp_dir_.path().Append(L"test_dll_0.pdb");
135 E : ASSERT_TRUE(file_util::CopyFile(temp_pe_path_, pe_path_0));
136 E : ASSERT_TRUE(file_util::CopyFile(temp_pdb_path_, pdb_path_0));
137 :
138 : // Zap them again.
139 E : ZapTimestamp zap1;
140 E : EXPECT_TRUE(zap1.Init(temp_pe_path_));
141 E : EXPECT_EQ(temp_pdb_path_, zap1.pdb_path());
142 E : EXPECT_TRUE(zap1.Zap(true, true));
143 :
144 : // The singly and doubly zapped files should be the same.
145 E : EXPECT_TRUE(file_util::ContentsEqual(temp_pe_path_, pe_path_0));
146 E : EXPECT_TRUE(file_util::ContentsEqual(temp_pdb_path_, pdb_path_0));
147 E : }
148 :
149 E : TEST_F(ZapTimestampTest, Succeeds) {
150 : // Zap the first set of the PE and PDB files.
151 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(0));
152 E : ZapTimestamp zap0;
153 E : EXPECT_TRUE(zap0.Init(temp_pe_path_));
154 E : EXPECT_EQ(temp_pdb_path_, zap0.pdb_path());
155 E : EXPECT_TRUE(zap0.Zap(true, true));
156 :
157 : // Rename and move the PE and PDB file.
158 E : base::FilePath pe_path_0 = temp_dir_.path().Append(L"test_dll_0.dll");
159 E : base::FilePath pdb_path_0 = temp_dir_.path().Append(L"test_dll_0.pdb");
160 E : ASSERT_TRUE(file_util::Move(temp_pe_path_, pe_path_0));
161 E : ASSERT_TRUE(file_util::Move(temp_pdb_path_, pdb_path_0));
162 :
163 : // Zap the second set of the PE and PDB files.
164 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(1));
165 E : ZapTimestamp zap1;
166 E : EXPECT_TRUE(zap1.Init(temp_pe_path_, temp_pdb_path_));
167 E : EXPECT_TRUE(zap1.Zap(true, true));
168 :
169 : // Rename and move the PE and PDB file.
170 E : base::FilePath pe_path_1 = temp_dir_.path().Append(L"test_dll_1.dll");
171 E : base::FilePath pdb_path_1 = temp_dir_.path().Append(L"test_dll_1.pdb");
172 E : ASSERT_TRUE(file_util::Move(temp_pe_path_, pe_path_1));
173 E : ASSERT_TRUE(file_util::Move(temp_pdb_path_, pdb_path_1));
174 :
175 : // Zap the third set of the PE and PDB files.
176 E : ASSERT_NO_FATAL_FAILURE(CopyTestData(2));
177 E : ZapTimestamp zap2;
178 E : EXPECT_TRUE(zap2.Init(temp_pe_path_, temp_pdb_path_));
179 E : EXPECT_TRUE(zap2.Zap(true, true));
180 :
181 : // The sets of zapped files should match.
182 E : EXPECT_TRUE(file_util::ContentsEqual(temp_pe_path_, pe_path_0));
183 E : EXPECT_TRUE(file_util::ContentsEqual(temp_pe_path_, pe_path_1));
184 E : EXPECT_TRUE(file_util::ContentsEqual(temp_pdb_path_, pdb_path_0));
185 E : EXPECT_TRUE(file_util::ContentsEqual(temp_pdb_path_, pdb_path_1));
186 E : }
187 :
188 : } // namespace zap_timestamp
|