1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/basictypes.h"
8 #include "chrome/test/logging/win/mof_data_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // A test fixture for Mof parser tests.
12 class MofDataParserTest
: public ::testing::Test
{
14 EVENT_TRACE
* MakeEventWithDataOfSize(size_t size
);
15 template<typename T
> EVENT_TRACE
* MakeEventWithBlittedValue(T value
) {
16 EVENT_TRACE
* event
= MakeEventWithDataOfSize(sizeof(value
));
17 *reinterpret_cast<T
*>(event
->MofData
) = value
;
20 EVENT_TRACE
* MakeEventWithDWORD(DWORD value
);
21 EVENT_TRACE
* MakeEventWithPointerArray(const void* const* pointers
,
23 EVENT_TRACE
* MakeEventWithString(const char* a_string
, size_t length
);
25 std::vector
<uint8
> buffer_
;
28 EVENT_TRACE
* MofDataParserTest::MakeEventWithDataOfSize(size_t size
) {
29 buffer_
.assign(sizeof(EVENT_TRACE
) + size
, 0);
30 EVENT_TRACE
* event
= reinterpret_cast<EVENT_TRACE
*>(&buffer_
[0]);
31 event
->MofLength
= size
;
32 event
->MofData
= &buffer_
[sizeof(EVENT_TRACE
)];
36 EVENT_TRACE
* MofDataParserTest::MakeEventWithDWORD(DWORD value
) {
37 return MakeEventWithBlittedValue(value
);
40 EVENT_TRACE
* MofDataParserTest::MakeEventWithPointerArray(
41 const void* const* pointers
,
44 MakeEventWithDataOfSize(sizeof(DWORD
) + sizeof(*pointers
) * size
);
45 *reinterpret_cast<DWORD
*>(event
->MofData
) = size
;
46 ::memcpy(reinterpret_cast<DWORD
*>(event
->MofData
) + 1, pointers
,
47 sizeof(*pointers
) * size
);
51 // |length| is the number of bytes to put in (i.e., include the terminator if
53 EVENT_TRACE
* MofDataParserTest::MakeEventWithString(const char* a_string
,
55 EVENT_TRACE
* event
= MakeEventWithDataOfSize(length
);
56 ::memcpy(event
->MofData
, a_string
, length
);
60 // Tests reading a primitive value. ReadDWORD, ReadInt, and ReadPointer share
61 // the same implementation, so this test covers all three.
62 TEST_F(MofDataParserTest
, ReadPrimitive
) {
64 // Read a valid DWORD.
65 EVENT_TRACE
* event
= MakeEventWithDWORD(5);
68 logging_win::MofDataParser
parser(event
);
69 EXPECT_FALSE(parser
.empty());
70 EXPECT_TRUE(parser
.ReadDWORD(&value
));
71 EXPECT_EQ(5UL, value
);
72 EXPECT_TRUE(parser
.empty());
75 // Try again if there's insufficient data.
79 logging_win::MofDataParser
parser(event
);
80 EXPECT_FALSE(parser
.empty());
81 EXPECT_FALSE(parser
.ReadDWORD(&value
));
82 EXPECT_EQ(0UL, value
);
86 // Tests reading an array of pointer-sized values. These arrays are encoded by
87 // writing a DWORD item count followed by the items.
88 TEST_F(MofDataParserTest
, ReadPointerArray
) {
89 const void* const pointers
[] = { this, &buffer_
};
90 const DWORD array_size
= arraysize(pointers
);
92 // Read a valid array of two pointers.
93 EVENT_TRACE
* event
= MakeEventWithPointerArray(&pointers
[0], array_size
);
96 const intptr_t* values
= NULL
;
98 logging_win::MofDataParser
parser(event
);
99 EXPECT_FALSE(parser
.empty());
100 EXPECT_TRUE(parser
.ReadDWORD(&size
));
101 EXPECT_EQ(array_size
, size
);
102 EXPECT_TRUE(parser
.ReadPointerArray(size
, &values
));
103 EXPECT_EQ(0, ::memcmp(&pointers
[0], values
, sizeof(*values
) * size
));
104 EXPECT_TRUE(parser
.empty());
107 // Try again if there's insufficient data.
108 --(event
->MofLength
);
111 const intptr_t* values
= NULL
;
113 logging_win::MofDataParser
parser(event
);
114 EXPECT_FALSE(parser
.empty());
115 EXPECT_TRUE(parser
.ReadDWORD(&size
));
116 EXPECT_EQ(array_size
, size
);
117 EXPECT_FALSE(parser
.ReadPointerArray(size
, &values
));
118 EXPECT_FALSE(parser
.empty());
122 // Tests reading a structure.
123 TEST_F(MofDataParserTest
, ReadStructure
) {
128 const Spam canned_meat
= { 47, 'Y' };
130 // Read a pointer to a structure.
131 EVENT_TRACE
* event
= MakeEventWithBlittedValue(canned_meat
);
133 const Spam
* value
= NULL
;
134 logging_win::MofDataParser
parser(event
);
135 EXPECT_FALSE(parser
.empty());
136 EXPECT_TRUE(parser
.ReadStructure(&value
));
137 EXPECT_EQ(canned_meat
.blorf
, value
->blorf
);
138 EXPECT_EQ(canned_meat
.spiffy
, value
->spiffy
);
139 EXPECT_TRUE(parser
.empty());
142 // Try again if there's insufficient data.
143 --(event
->MofLength
);
145 const Spam
* value
= NULL
;
146 logging_win::MofDataParser
parser(event
);
147 EXPECT_FALSE(parser
.empty());
148 EXPECT_FALSE(parser
.ReadStructure(&value
));
149 EXPECT_FALSE(parser
.empty());
153 // Tests reading null-terminated string.
154 TEST_F(MofDataParserTest
, ReadString
) {
155 const char a_string_nl
[] = "sometimes i get lost in my own thoughts.\n";
156 const char a_string
[] = "sometimes i get lost in my own thoughts.";
158 // Read a string with a trailing newline.
160 MakeEventWithString(a_string_nl
, arraysize(a_string_nl
));
162 base::StringPiece value
;
163 logging_win::MofDataParser
parser(event
);
164 EXPECT_FALSE(parser
.empty());
165 EXPECT_TRUE(parser
.ReadString(&value
));
166 EXPECT_EQ(base::StringPiece(&a_string_nl
[0], arraysize(a_string_nl
) - 2),
168 EXPECT_TRUE(parser
.empty());
171 // Read a string without a trailing newline.
172 event
= MakeEventWithString(a_string
, arraysize(a_string
));
174 base::StringPiece value
;
175 logging_win::MofDataParser
parser(event
);
176 EXPECT_FALSE(parser
.empty());
177 EXPECT_TRUE(parser
.ReadString(&value
));
178 EXPECT_EQ(base::StringPiece(&a_string
[0], arraysize(a_string
) - 1), value
);
179 EXPECT_TRUE(parser
.empty());
182 // Try a string that isn't terminated.
183 event
= MakeEventWithString(a_string
, arraysize(a_string
) - 1);
185 base::StringPiece value
;
186 logging_win::MofDataParser
parser(event
);
187 EXPECT_FALSE(parser
.empty());
188 EXPECT_FALSE(parser
.ReadString(&value
));
189 EXPECT_FALSE(parser
.empty());