forked from etas/boost-external-test-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBoostTestTreeDebugLister.cpp
325 lines (264 loc) · 8.1 KB
/
CBoostTestTreeDebugLister.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "CBoostTestTreeDebugLister.h"
#include <stdexcept>
#define _NO_CVCONST_H
#include <DbgHelp.h>
//suppression of warnings related to 3rd party files
#pragma warning ( disable: 6001 )
#pragma warning ( disable: 6031 )
#include <boost/test/utils/xml_printer.hpp>
#pragma warning ( default: 6001 )
#pragma warning ( default: 6031 )
//end suppression of warnings related to 3rd party files
namespace etas
{
namespace boost
{
namespace unit_test
{
namespace
{
::boost::unit_test::attr_value attr_value()
{
return ::boost::unit_test::attr_value();
};
struct SSourceInfo
{
SSourceInfo() :
m_file(c_unkownLocation),
m_lineNumber(0)
{
};
std::string m_file;
unsigned long m_lineNumber;
static const std::string c_unkownLocation;
};
const std::string SSourceInfo::c_unkownLocation("unknown location");
struct SSearchRequest
{
SSearchRequest(
HANDLE handle,
DWORD64 dllBase,
const std::vector<const ::boost::unit_test::test_suite*>& suites,
const ::boost::unit_test::test_case& test
) :
m_handle(handle),
m_dllBase(dllBase),
m_suites(&suites),
m_test(&test)
{
};
HANDLE m_handle;
DWORD64 m_dllBase;
const std::vector<const ::boost::unit_test::test_suite*>* m_suites;
const ::boost::unit_test::test_case* m_test;
};
struct SSearchContext
{
SSearchContext(const SSearchRequest& request) :
m_request(&request)
{
};
const SSearchRequest* m_request;
SSourceInfo m_testSuiteMatch;
SSourceInfo m_freeMatch;
};
template <typename Iterator>
bool SubstringEqual(Iterator lhsBegin, Iterator lhsEnd, Iterator rhsBegin, Iterator rhsEnd)
{
for (; (lhsBegin != lhsEnd) && (rhsBegin != rhsEnd); ++lhsBegin, ++rhsBegin)
{
if (*lhsBegin != *rhsBegin)
{
return false;
}
}
return (rhsBegin == rhsEnd);
}
bool EndsWith(const std::string& base, const std::string& match)
{
return SubstringEqual(base.rbegin(), base.rend(), match.rbegin(), match.rend());
}
std::string::const_iterator SubstringMatches(const std::string& base, const std::string::const_iterator begin, const std::string& match)
{
if (SubstringEqual(begin, base.end(), match.begin(), match.end()))
{
return begin + match.length();
}
return base.end();
}
bool MatchesSearchRequest(const SSearchRequest& request, const std::string& name)
{
// This is an auto registered test case, match the test suite as well.
if (EndsWith(name, "::test_method"))
{
std::string::const_iterator index = name.begin();
for (auto i = request.m_suites->begin(), end = request.m_suites->end(); i != end; ++i)
{
index = SubstringMatches(name, index, (*i)->p_name.value);
if (index == name.end())
{
break;
}
index = SubstringMatches(name, index, "::");
if (index == name.end())
{
break;
}
}
if ((index != name.end()) && (SubstringMatches(name, index, request.m_test->p_name.value) != name.end()))
{
return true;
}
}
return false;
}
const char* const GetName(SYMBOL_INFO& symInfo)
{
return (symInfo.MaxNameLen > 0) ? symInfo.Name : nullptr;
};
BOOL CALLBACK EnumSymbolsCallback(PSYMBOL_INFO symInfo, ULONG symbolSize, PVOID userContext)
{
SSearchContext* context = static_cast<SSearchContext*>(userContext);
if (symInfo != NULL)
{
// Unfortunately, we do not make use the symInfo->Flags because it always returns 0 for some odd reason...
const char* const name = GetName(*symInfo);
if (name != nullptr)
{
SSourceInfo* info = nullptr;
// Test for strong match, i.e. <test suite>::<test name>::test_method
if (MatchesSearchRequest(*(context->m_request), name))
{
// This request satisfies the strong guarantee
info = &context->m_testSuiteMatch;
}
// Test for lesser match, i.e. match name only.
else if (context->m_request->m_test->p_name.value == name)
{
// This is a test case which was (most probably) programmatically registered
info = &context->m_freeMatch;
}
// If either one of the guarantees is met, record line information
if (info != nullptr)
{
IMAGEHLP_LINE64 line;
DWORD displacement;
if (SymGetLineFromAddr64(context->m_request->m_handle, symInfo->Address, &displacement, &line))
{
info->m_file = line.FileName;
info->m_lineNumber = line.LineNumber;
}
}
}
}
// Terminate enumeration if the strong guarantee is met.
if (context->m_testSuiteMatch.m_file == SSourceInfo::c_unkownLocation)
{
// Source information has still not been found. Continue enumeration.
return TRUE;
}
// Source information has been located. Terminate enumeration.
return FALSE;
};
SSourceInfo GetSourceInfo(const SSearchRequest& request)
{
if ((request.m_handle == NULL) || (request.m_dllBase == 0))
{
// No debug information can be retrieved since module has not been loaded correctly
return SSourceInfo();
}
SSearchContext context(request);
SymEnumSymbols(request.m_handle, request.m_dllBase, NULL, EnumSymbolsCallback, &context);
// Prefer the auto registered case over the free registered case
if (context.m_testSuiteMatch.m_file != SSourceInfo::c_unkownLocation)
{
return context.m_testSuiteMatch;
}
else if (context.m_freeMatch.m_file != SSourceInfo::c_unkownLocation)
{
return context.m_freeMatch;
}
// No source information is available
return SSourceInfo();
};
} // namespace (anonymous)
CBoostTestTreeDebugLister::CBoostTestTreeDebugLister(const std::string& source) :
TBase(source),
m_handle(NULL),
m_dllBase(-1)
{
Init();
}
CBoostTestTreeDebugLister::CBoostTestTreeDebugLister(const std::string& source, std::ostream* out) :
TBase(source, out),
m_handle(NULL),
m_dllBase(0)
{
Init();
}
CBoostTestTreeDebugLister::~CBoostTestTreeDebugLister()
{
if (m_handle != NULL)
{
if (m_dllBase > 0)
{
SymUnloadModule64(m_handle, m_dllBase);
}
SymCleanup(m_handle);
}
}
void CBoostTestTreeDebugLister::Init()
{
m_handle = GetCurrentProcess();
if (SymInitialize(m_handle, NULL, FALSE))
{
SymSetOptions(SYMOPT_LOAD_LINES);
m_dllBase = SymLoadModuleEx(m_handle, NULL, GetSource().c_str(), NULL, 0, 0, NULL, 0);
}
else
{
m_handle = NULL;
}
}
void CBoostTestTreeDebugLister::visit(const ::boost::unit_test::test_case& testCase)
{
SSourceInfo info = GetSourceInfo(SSearchRequest(m_handle, m_dllBase, m_suites, testCase));
Tab() << "<TestCase"
" id" << attr_value() << testCase.p_id <<
" name" << attr_value() << testCase.p_name.value;
// Avoid listing source information for unkown sources
if (info.m_file != SSourceInfo::c_unkownLocation)
{
Out() << " file" << attr_value() << info.m_file <<
" line" << attr_value() << info.m_lineNumber;
}
Out() << " />";
if (GetPrettyPrint())
{
Out() << std::endl;
}
}
bool CBoostTestTreeDebugLister::IsMasterTestSuite(const ::boost::unit_test::test_suite& testSuite) const
{
return testSuite.p_id == 1;
}
bool CBoostTestTreeDebugLister::test_suite_start(const ::boost::unit_test::test_suite& testSuite)
{
// Skip the master test suite
if (!IsMasterTestSuite(testSuite))
{
m_suites.push_back(&testSuite);
}
return TBase::test_suite_start(testSuite);
}
void CBoostTestTreeDebugLister::test_suite_finish(const ::boost::unit_test::test_suite& testSuite)
{
if (!IsMasterTestSuite(testSuite))
{
m_suites.pop_back();
}
return TBase::test_suite_finish(testSuite);
}
} // namespace unit_test
} // namespace boost
} // namespace etas