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/random_order_generator.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/reorder/order_generator_test.h"
19 : #include "syzygy/pe/pe_utils.h"
20 :
21 : namespace reorder {
22 :
23 : class RandomOrderGeneratorTest : public testing::OrderGeneratorTest {
24 : protected:
25 E : RandomOrderGeneratorTest() : order_generator_(1234) {}
26 :
27 : void ExpectRandomOrder(
28 : const IMAGE_SECTION_HEADER* section,
29 E : const Reorderer::Order::BlockList& block_list) {
30 : // Verifies that the blocks in block_list match in count but not in order
31 : // to the blocks in the specified section.
32 E : Reorderer::Order::BlockList original_block_list;
33 E : GetBlockListForSection(section, &original_block_list);
34 E : EXPECT_EQ(original_block_list.size(), block_list.size());
35 E : EXPECT_FALSE(std::equal(original_block_list.begin(),
36 : original_block_list.end(),
37 : block_list.begin()));
38 E : }
39 :
40 : RandomOrderGenerator order_generator_;
41 : };
42 :
43 E : TEST_F(RandomOrderGeneratorTest, DoNotReorder) {
44 : EXPECT_TRUE(order_generator_.CalculateReordering(input_dll_,
45 : image_layout_,
46 : false,
47 : false,
48 E : &order_));
49 :
50 E : ExpectNoDuplicateBlocks();
51 :
52 : // Verify that the order found in order_ matches the original decomposed
53 : // image.
54 : Reorderer::Order::BlockListMap::const_iterator it =
55 E : order_.section_block_lists.begin();
56 E : for (; it != order_.section_block_lists.end(); ++it) {
57 i : const IMAGE_SECTION_HEADER* section = input_dll_.section_header(it->first);
58 i : ExpectNoReorder(section, it->second);
59 i : }
60 E : }
61 :
62 E : TEST_F(RandomOrderGeneratorTest, ReorderCode) {
63 : EXPECT_TRUE(order_generator_.CalculateReordering(input_dll_,
64 : image_layout_,
65 : true,
66 : false,
67 E : &order_));
68 :
69 E : ExpectNoDuplicateBlocks();
70 :
71 : // Verify that code blocks have been reordered and that data blocks have not.
72 : Reorderer::Order::BlockListMap::const_iterator it =
73 E : order_.section_block_lists.begin();
74 E : for (; it != order_.section_block_lists.end(); ++it) {
75 i : const IMAGE_SECTION_HEADER* section = input_dll_.section_header(it->first);
76 i : if (section->Characteristics & IMAGE_SCN_CNT_CODE) {
77 i : ExpectRandomOrder(section, it->second);
78 i : } else {
79 i : ExpectNoReorder(section, it->second);
80 : }
81 i : }
82 E : }
83 :
84 E : TEST_F(RandomOrderGeneratorTest, ReorderData) {
85 : EXPECT_TRUE(order_generator_.CalculateReordering(input_dll_,
86 : image_layout_,
87 : false,
88 : true,
89 E : &order_));
90 :
91 E : ExpectNoDuplicateBlocks();
92 :
93 : // Verify that data blocks have been reordered and that code blocks have not.
94 : Reorderer::Order::BlockListMap::const_iterator it =
95 E : order_.section_block_lists.begin();
96 E : for (; it != order_.section_block_lists.end(); ++it) {
97 E : const IMAGE_SECTION_HEADER* section = input_dll_.section_header(it->first);
98 E : if (section->Characteristics & pe::kReadOnlyDataCharacteristics) {
99 E : std::string name = input_dll_.GetSectionName(*section);
100 : // .tls and .rsrc only have one block.
101 E : if (name != ".tls" && name != ".rsrc")
102 E : ExpectRandomOrder(section, it->second);
103 E : } else {
104 i : ExpectNoReorder(section, it->second);
105 : }
106 E : }
107 E : }
108 :
109 : } // namespace reorder
|