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 : #include "syzygy/pe/pdb_info.h"
15 :
16 : #include "gtest/gtest.h"
17 : #include "syzygy/core/unittest_util.h"
18 : #include "syzygy/pdb/pdb_util.h"
19 : #include "syzygy/pe/unittest_util.h"
20 :
21 : namespace pe {
22 :
23 : namespace {
24 :
25 : class PdbInfoTest: public testing::PELibUnitTest {
26 : // Insert your customizations here.
27 : };
28 :
29 : // Creates a buffer that can be interpreted as a CvInfoPdb70 struct.
30 : CvInfoPdb70* CreateCvInfoPdb70(const char* path,
31 E : std::vector<unsigned char>* data) {
32 E : size_t cv_len = sizeof(CvInfoPdb70);
33 E : size_t path_len = ::strlen(path);
34 E : size_t data_len = cv_len + path_len;
35 :
36 E : data->clear();
37 E : data->resize(data_len, 0);
38 :
39 E : CvInfoPdb70* cv_info_pdb = reinterpret_cast<CvInfoPdb70*>(&data->at(0));
40 E : ::strcpy(cv_info_pdb->pdb_file_name, path);
41 :
42 E : return cv_info_pdb;
43 E : }
44 :
45 : } // namespace
46 :
47 E : TEST_F(PdbInfoTest, TestDllAndPdbAreConsistent) {
48 E : const FilePath test_dll(testing::GetExeRelativePath(testing::kTestDllName));
49 : const FilePath test_dll_pdb(testing::GetExeRelativePath(
50 E : testing::kTestDllPdbName));
51 :
52 E : PdbInfo pdb_info;
53 E : EXPECT_TRUE(pdb_info.Init(test_dll));
54 :
55 : pdb::PdbInfoHeader70 pdb_header;
56 E : ASSERT_TRUE(pdb::ReadPdbHeader(test_dll_pdb, &pdb_header));
57 :
58 E : EXPECT_TRUE(pdb_info.IsConsistent(pdb_header));
59 E : }
60 :
61 E : TEST_F(PdbInfoTest, BuildFromCvInfoPdb70) {
62 E : const char kPath[] = "C:\\foo\\foo.pdb";
63 E : const GUID kSignature = { 0xdeadbeef, 0xf00d, 0xcafe,
64 E : { 'c', 'u', 't', 'e', 'c', 'a', 't', 's' } };
65 :
66 E : std::vector<unsigned char> data;
67 E : CvInfoPdb70* cv_info_pdb = CreateCvInfoPdb70(kPath, &data);
68 E : cv_info_pdb->cv_signature = 0xdeadbeef;
69 E : cv_info_pdb->pdb_age = 2;
70 E : cv_info_pdb->signature = kSignature;
71 :
72 E : PdbInfo pdb_info;
73 E : EXPECT_TRUE(pdb_info.Init(*cv_info_pdb));
74 :
75 E : pdb::PdbInfoHeader70 pdb_header = {};
76 E : pdb_header.pdb_age = cv_info_pdb->pdb_age;
77 E : pdb_header.signature = cv_info_pdb->signature;
78 :
79 E : EXPECT_TRUE(pdb_info.IsConsistent(pdb_header));
80 :
81 : // An older PBD is not consistent with a newer image.
82 E : pdb_header.pdb_age = cv_info_pdb->pdb_age - 1;
83 E : EXPECT_FALSE(pdb_info.IsConsistent(pdb_header));
84 :
85 : // If the PDB age is newer than the image, we are consistent.
86 E : pdb_header.pdb_age = cv_info_pdb->pdb_age + 1;
87 E : EXPECT_TRUE(pdb_info.IsConsistent(pdb_header));
88 E : }
89 :
90 : } // namespace pe
|