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 : // Add indexed block frequency transform unittests.
16 :
17 : #include "syzygy/instrument/transforms/add_indexed_frequency_data_transform.h"
18 :
19 : #include "gtest/gtest.h"
20 : #include "syzygy/block_graph/transform.h"
21 : #include "syzygy/block_graph/typed_block.h"
22 : #include "syzygy/common/indexed_frequency_data.h"
23 : #include "syzygy/core/unittest_util.h"
24 : #include "syzygy/instrument/transforms/unittest_util.h"
25 : #include "syzygy/pe/decomposer.h"
26 : #include "syzygy/pe/pe_utils.h"
27 : #include "syzygy/pe/unittest_util.h"
28 :
29 : namespace instrument {
30 : namespace transforms {
31 :
32 : namespace {
33 :
34 : using block_graph::BlockGraph;
35 : using common::IndexedFrequencyData;
36 :
37 : const uint32 kAgentId = 0xDEADBEEF;
38 : const uint32 kAgentVersion = 5;
39 : const uint32 kNumEntries = 7;
40 : const uint8 kFrequencySize = 4;
41 : const uint8 kDataType = 9;
42 :
43 : class AddFrequencyDataTransformTest
44 : : public testing::TestDllTransformTest {
45 : public:
46 E : virtual void SetUp() OVERRIDE {
47 E : ASSERT_NO_FATAL_FAILURE(DecomposeTestDll());
48 E : }
49 : };
50 :
51 : } // namespace
52 :
53 E : TEST_F(AddFrequencyDataTransformTest, Apply) {
54 : AddIndexedFrequencyDataTransform tx(kAgentId, "Test", kAgentVersion,
55 E : static_cast<IndexedFrequencyData::DataType>(kDataType));
56 : ASSERT_TRUE(block_graph::ApplyBlockGraphTransform(
57 E : &tx, &block_graph_, dos_header_block_));
58 :
59 E : BlockGraph::Block* frequency_data_block = tx.frequency_data_block();
60 :
61 : // The frequency data block should have the appropriate size.
62 E : ASSERT_EQ(sizeof(IndexedFrequencyData), frequency_data_block->data_size());
63 E : ASSERT_EQ(sizeof(IndexedFrequencyData), frequency_data_block->size());
64 :
65 : // The frequency data block should be appropriately initialized.
66 E : block_graph::ConstTypedBlock<IndexedFrequencyData> frequency_data;
67 E : ASSERT_TRUE(frequency_data.Init(0, frequency_data_block));
68 E : EXPECT_EQ(kAgentId, frequency_data->agent_id);
69 E : EXPECT_EQ(kAgentVersion, frequency_data->version);
70 E : EXPECT_EQ(kDataType, frequency_data->data_type);
71 E : EXPECT_EQ(TLS_OUT_OF_INDEXES, frequency_data->tls_index);
72 E : EXPECT_EQ(0U, frequency_data->num_entries);
73 E : EXPECT_EQ(0U, frequency_data->frequency_size);
74 E : EXPECT_EQ(0U, frequency_data->initialization_attempted);
75 :
76 : // Configure the frequency data buffer.
77 : ASSERT_TRUE(tx.ConfigureFrequencyDataBuffer(kNumEntries,
78 E : kFrequencySize));
79 E : BlockGraph::Block* buffer_block = tx.frequency_data_buffer_block();
80 E : EXPECT_TRUE(buffer_block != NULL);
81 :
82 : EXPECT_TRUE(frequency_data.HasReferenceAt(
83 E : frequency_data.OffsetOf(frequency_data->frequency_data)));
84 :
85 E : EXPECT_EQ(kNumEntries, frequency_data->num_entries);
86 E : EXPECT_EQ(kFrequencySize, frequency_data->frequency_size);
87 E : EXPECT_EQ(0, buffer_block->data_size());
88 E : EXPECT_EQ((kNumEntries * kFrequencySize), buffer_block->size());
89 E : }
90 :
91 : } // namespace transforms
92 : } // namespace instrument
|