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