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