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/transforms/add_pdb_info_transform.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/block_graph/typed_block.h"
19 : #include "syzygy/core/unittest_util.h"
20 : #include "syzygy/pe/decomposer.h"
21 : #include "syzygy/pe/unittest_util.h"
22 :
23 : namespace pe {
24 : namespace transforms {
25 :
26 : using block_graph::TypedBlock;
27 :
28 : namespace {
29 :
30 E : const base::FilePath kPdbPath(L"dummy.pdb");
31 : const uint32 kPdbAge = 0;
32 : const GUID kPdbGuid = { 0x11111111, 0x2222, 0x3333,
33 : { 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB } };
34 :
35 : class AddPdbInfoTransformTest : public testing::PELibUnitTest {
36 : public:
37 : AddPdbInfoTransformTest()
38 : : image_layout_(&block_graph_),
39 E : dos_header_block_(NULL) {
40 E : }
41 :
42 : ImageLayout image_layout_;
43 : BlockGraph block_graph_;
44 : BlockGraph::Block* dos_header_block_;
45 : };
46 :
47 : } // namespace
48 :
49 E : TEST_F(AddPdbInfoTransformTest, UpdateExisting) {
50 E : PEFile pe_file;
51 E : ASSERT_TRUE(pe_file.Init(testing::GetExeRelativePath(testing::kTestDllName)));
52 :
53 E : Decomposer decomposer(pe_file);
54 E : ASSERT_TRUE(decomposer.Decompose(&image_layout_));
55 :
56 : dos_header_block_ = image_layout_.blocks.GetBlockByAddress(
57 E : core::RelativeAddress(0));
58 E : ASSERT_TRUE(dos_header_block_ != NULL);
59 :
60 E : AddPdbInfoTransform transform(kPdbPath, kPdbAge, kPdbGuid);
61 : EXPECT_TRUE(block_graph::ApplyBlockGraphTransform(
62 E : &transform, &block_graph_, dos_header_block_));
63 :
64 : // TODO(chrisha): Create the image and the PDB in a temp directory and
65 : // see if pe::FindPdbForModule can find it. If so, then so will the
66 : // debug machinery.
67 E : }
68 :
69 E : TEST_F(AddPdbInfoTransformTest, CreateNew) {
70 : // Create some empty dummy headers and hook them up.
71 : dos_header_block_ = block_graph_.AddBlock(
72 E : BlockGraph::DATA_BLOCK, sizeof(IMAGE_DOS_HEADER), "Dos Header");
73 E : ASSERT_TRUE(dos_header_block_ != NULL);
74 E : dos_header_block_->AllocateData(sizeof(IMAGE_DOS_HEADER));
75 :
76 : BlockGraph::Block* nt_headers_block = block_graph_.AddBlock(
77 E : BlockGraph::DATA_BLOCK, sizeof(IMAGE_NT_HEADERS), "Nt Headers");
78 E : ASSERT_TRUE(nt_headers_block != NULL);
79 E : nt_headers_block->AllocateData(sizeof(IMAGE_NT_HEADERS));
80 :
81 : dos_header_block_->SetReference(
82 : offsetof(IMAGE_DOS_HEADER, e_lfanew),
83 : BlockGraph::Reference(BlockGraph::RELATIVE_REF,
84 : sizeof(core::RelativeAddress),
85 : nt_headers_block,
86 E : 0, 0));
87 :
88 :
89 E : AddPdbInfoTransform transform(kPdbPath, kPdbAge, kPdbGuid);
90 : EXPECT_TRUE(block_graph::ApplyBlockGraphTransform(
91 E : &transform, &block_graph_, dos_header_block_));
92 E : }
93 :
94 : } // namespace transforms
95 : } // namespace pe
|