1 : // Copyright 2011 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 :
15 : #include "syzygy/reorder/order_generator_test.h"
16 :
17 : #include "syzygy/block_graph/block_graph.h"
18 : #include "syzygy/core/address.h"
19 : #include "syzygy/core/unittest_util.h"
20 :
21 : using block_graph::BlockGraph;
22 : using core::RelativeAddress;
23 :
24 : namespace testing {
25 :
26 E : OrderGeneratorTest::OrderGeneratorTest() : image_layout_(&block_graph_) {
27 E : }
28 :
29 E : void OrderGeneratorTest::SetUp() {
30 E : FilePath test_data_dir = testing::GetExeRelativePath(L"test_data");
31 E : FilePath input_dll_path = test_data_dir.Append(kDllName);
32 :
33 E : ASSERT_TRUE(input_dll_.Init(input_dll_path));
34 E : pe::Decomposer decomposer(input_dll_);
35 E : ASSERT_TRUE(decomposer.Decompose(&image_layout_));
36 E : }
37 :
38 E : reorder::Reorderer::UniqueTime OrderGeneratorTest::GetSystemTime() {
39 E : return reorder::Reorderer::UniqueTime(base::Time::NowFromSystemTime());
40 E : }
41 :
42 E : void OrderGeneratorTest::ExpectNoDuplicateBlocks() {
43 : // Verifies that there are no duplicate blocks for each section.
44 : reorder::Reorderer::Order::BlockListMap::const_iterator it =
45 E : order_.section_block_lists.begin();
46 E : for (; it != order_.section_block_lists.end(); ++it) {
47 E : std::set<const BlockGraph::Block*> block_set;
48 E : for (size_t i = 0; i < it->second.size(); ++i) {
49 E : EXPECT_TRUE(block_set.insert(it->second[i]).second);
50 E : }
51 E : }
52 E : }
53 :
54 : void OrderGeneratorTest::ExpectNoReorder(
55 : const IMAGE_SECTION_HEADER* section,
56 E : const reorder::Reorderer::Order::BlockList& block_list) {
57 : // Verifies that the blocks in block_list match the order of the blocks
58 : // in the specified section.
59 E : reorder::Reorderer::Order::BlockList original_block_list;
60 E : GetBlockListForSection(section, &original_block_list);
61 E : EXPECT_EQ(original_block_list.size(), block_list.size());
62 : EXPECT_TRUE(std::equal(original_block_list.begin(),
63 : original_block_list.end(),
64 E : block_list.begin()));
65 E : }
66 :
67 : void OrderGeneratorTest::GetBlockListForSection(
68 : const IMAGE_SECTION_HEADER* section,
69 E : reorder::Reorderer::Order::BlockList* block_list) {
70 E : DCHECK(section != NULL);
71 E : DCHECK(block_list != NULL);
72 :
73 : RelativeAddress section_start =
74 E : RelativeAddress(section->VirtualAddress);
75 : BlockGraph::AddressSpace::RangeMapConstIterPair section_blocks =
76 : image_layout_.blocks.GetIntersectingBlocks(
77 E : section_start, section->Misc.VirtualSize);
78 : BlockGraph::AddressSpace::RangeMapConstIter& section_it =
79 E : section_blocks.first;
80 : const BlockGraph::AddressSpace::RangeMapConstIter& section_end =
81 E : section_blocks.second;
82 :
83 E : for (; section_it != section_end; ++section_it) {
84 E : block_list->push_back(section_it->second);
85 E : }
86 E : }
87 :
88 : } // namespace testing
|