1 : // Copyright 2013 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/block_graph/orderer.h"
16 :
17 : #include "gmock/gmock.h"
18 : #include "gtest/gtest.h"
19 :
20 : namespace block_graph {
21 :
22 : namespace {
23 :
24 : using testing::Return;
25 :
26 : class LenientMockBlockGraphOrderer : public BlockGraphOrdererInterface {
27 : public:
28 E : virtual ~LenientMockBlockGraphOrderer() { }
29 E : virtual const char* name() const { return "MockBlockGraphOrderer"; }
30 :
31 E : MOCK_METHOD2(OrderBlockGraph, bool(OrderedBlockGraph*, BlockGraph::Block*));
32 : };
33 : typedef testing::StrictMock<LenientMockBlockGraphOrderer>
34 : MockBlockGraphOrderer;
35 :
36 : class BlockGraphOrdererTest : public testing::Test {
37 : public:
38 E : virtual void SetUp() {
39 E : header_block_ = block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 1, "header");
40 :
41 E : BlockGraph::Section* text = block_graph_.AddSection(".text", 0);
42 E : BlockGraph::Section* rdata = block_graph_.AddSection(".rdata", 0);
43 :
44 : BlockGraph::Block* code = block_graph_.AddBlock(
45 E : BlockGraph::CODE_BLOCK, 3, "code");
46 : BlockGraph::Block* data = block_graph_.AddBlock(
47 E : BlockGraph::DATA_BLOCK, 2, "data");
48 :
49 E : code->set_section(text->id());
50 E : data->set_section(rdata->id());
51 :
52 : // Create the ordered block graph. This can only be created after the
53 : // block graph is set up.
54 E : ordered_block_graph_.reset(new OrderedBlockGraph(&block_graph_));
55 E : }
56 :
57 : BlockGraph block_graph_;
58 : BlockGraph::Block* header_block_;
59 :
60 : scoped_ptr<OrderedBlockGraph> ordered_block_graph_;
61 : };
62 :
63 : } // namespace
64 :
65 E : TEST_F(BlockGraphOrdererTest, ApplyOrderersSucceeds) {
66 E : MockBlockGraphOrderer o1, o2, o3;
67 E : std::vector<BlockGraphOrdererInterface*> orderers;
68 E : orderers.push_back(&o1);
69 E : orderers.push_back(&o2);
70 E : orderers.push_back(&o3);
71 :
72 : EXPECT_CALL(o1, OrderBlockGraph(ordered_block_graph_.get(), header_block_))
73 E : .WillOnce(Return(true));
74 : EXPECT_CALL(o2, OrderBlockGraph(ordered_block_graph_.get(), header_block_))
75 E : .WillOnce(Return(true));
76 : EXPECT_CALL(o3, OrderBlockGraph(ordered_block_graph_.get(), header_block_))
77 E : .WillOnce(Return(true));
78 :
79 : EXPECT_TRUE(ApplyBlockGraphOrderers(
80 E : orderers, ordered_block_graph_.get(), header_block_));
81 E : }
82 :
83 E : TEST_F(BlockGraphOrdererTest, ApplyOrderersFails) {
84 E : MockBlockGraphOrderer o1, o2, o3;
85 E : std::vector<BlockGraphOrdererInterface*> orderers;
86 E : orderers.push_back(&o1);
87 E : orderers.push_back(&o2);
88 E : orderers.push_back(&o3);
89 :
90 : EXPECT_CALL(o1, OrderBlockGraph(ordered_block_graph_.get(), header_block_))
91 E : .WillOnce(Return(true));
92 : EXPECT_CALL(o2, OrderBlockGraph(ordered_block_graph_.get(), header_block_))
93 E : .WillOnce(Return(false));
94 :
95 : EXPECT_FALSE(ApplyBlockGraphOrderers(
96 E : orderers, ordered_block_graph_.get(), header_block_));
97 E : }
98 :
99 : } // namespace block_graph
|