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 : typedef common::IndexedFrequencyData::DataType DataType;
35 : using block_graph::BlockGraph;
36 : using common::IndexedFrequencyData;
37 :
38 : const uint32 kAgentId = 0xDEADBEEF;
39 : const uint32 kAgentVersion = 5;
40 : const uint32 kNumEntries = 7;
41 :
42 : class AddFrequencyDataTransformTest
43 : : public testing::TestDllTransformTest {
44 : public:
45 E : virtual void SetUp() OVERRIDE {
46 E : ASSERT_NO_FATAL_FAILURE(DecomposeTestDll());
47 E : }
48 :
49 : void Apply(size_t num_entries,
50 : size_t num_columns,
51 : size_t frequency_size,
52 : DataType data_type);
53 : };
54 :
55 : void AddFrequencyDataTransformTest::Apply(size_t num_entries,
56 E : size_t num_columns, size_t frequency_size, DataType data_type) {
57 : AddIndexedFrequencyDataTransform tx(kAgentId, "Test", kAgentVersion,
58 E : data_type, sizeof(common::IndexedFrequencyData));
59 : ASSERT_TRUE(block_graph::ApplyBlockGraphTransform(
60 E : &tx, policy_, &block_graph_, header_block_));
61 :
62 E : BlockGraph::Block* frequency_data_block = tx.frequency_data_block();
63 :
64 : // The frequency data block should have the appropriate size.
65 E : ASSERT_EQ(sizeof(IndexedFrequencyData), frequency_data_block->data_size());
66 E : ASSERT_EQ(sizeof(IndexedFrequencyData), frequency_data_block->size());
67 :
68 : // The frequency data block should be appropriately initialized.
69 E : block_graph::ConstTypedBlock<IndexedFrequencyData> frequency_data;
70 E : ASSERT_TRUE(frequency_data.Init(0, frequency_data_block));
71 E : EXPECT_EQ(kAgentId, frequency_data->agent_id);
72 E : EXPECT_EQ(kAgentVersion, frequency_data->version);
73 E : EXPECT_EQ(data_type, frequency_data->data_type);
74 E : EXPECT_EQ(0U, frequency_data->num_entries);
75 E : EXPECT_EQ(0U, frequency_data->num_columns);
76 E : EXPECT_EQ(0U, frequency_data->frequency_size);
77 E : EXPECT_EQ(0U, frequency_data->initialization_attempted);
78 :
79 : // Configure the frequency data buffer.
80 : ASSERT_TRUE(tx.ConfigureFrequencyDataBuffer(num_entries,
81 : num_columns,
82 E : frequency_size));
83 E : BlockGraph::Block* buffer_block = tx.frequency_data_buffer_block();
84 E : EXPECT_TRUE(buffer_block != NULL);
85 :
86 : EXPECT_TRUE(frequency_data.HasReferenceAt(
87 E : frequency_data.OffsetOf(frequency_data->frequency_data)));
88 :
89 E : EXPECT_EQ(num_entries, frequency_data->num_entries);
90 E : EXPECT_EQ(num_columns, frequency_data->num_columns);
91 E : EXPECT_EQ(frequency_size, frequency_data->frequency_size);
92 E : EXPECT_EQ(0, buffer_block->data_size());
93 E : EXPECT_EQ((num_entries * num_columns * frequency_size), buffer_block->size());
94 E : }
95 :
96 : } // namespace
97 :
98 E : TEST_F(AddFrequencyDataTransformTest, ApplySingleByteColumn) {
99 E : const uint32 kNumColumns = 1;
100 E : const uint8 kFrequencySize = 1;
101 : ASSERT_NO_FATAL_FAILURE(
102 : Apply(kNumEntries, kNumColumns, kFrequencySize,
103 E : common::IndexedFrequencyData::BASIC_BLOCK_ENTRY));
104 E : }
105 :
106 E : TEST_F(AddFrequencyDataTransformTest, ApplyMultipleByteColumn) {
107 E : const uint32 kNumColumns = 4;
108 E : const uint8 kFrequencySize = 1;
109 : ASSERT_NO_FATAL_FAILURE(
110 : Apply(kNumEntries, kNumColumns, kFrequencySize,
111 E : common::IndexedFrequencyData::BASIC_BLOCK_ENTRY));
112 E : }
113 :
114 E : TEST_F(AddFrequencyDataTransformTest, ApplySingleWordColumn) {
115 E : const uint32 kNumColumns = 1;
116 E : const uint8 kFrequencySize = 4;
117 : ASSERT_NO_FATAL_FAILURE(
118 : Apply(kNumEntries, kNumColumns, kFrequencySize,
119 E : common::IndexedFrequencyData::BASIC_BLOCK_ENTRY));
120 E : }
121 :
122 E : TEST_F(AddFrequencyDataTransformTest, ApplyMultipleWordColumn) {
123 E : const uint32 kNumColumns = 4;
124 E : const uint8 kFrequencySize = 4;
125 : ASSERT_NO_FATAL_FAILURE(
126 : Apply(kNumEntries, kNumColumns, kFrequencySize,
127 E : common::IndexedFrequencyData::BASIC_BLOCK_ENTRY));
128 E : }
129 :
130 : } // namespace transforms
131 : } // namespace instrument
|