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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#define TST_LOG_REDIRECT
#include <test.hpp>
#include <config.h>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestFailure.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/TextTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <Poco/RegularExpression.h>
#include <Poco/DirectoryIterator.h>
#include <Poco/FileStream.h>
#include <Poco/StreamCopier.h>
#include <Poco/StringTokenizer.h>
#include <Log.hpp>
class HTTPGetTest;
bool filterTests(CPPUNIT_NS::TestRunner& runner, CPPUNIT_NS::Test* testRegistry, const std::string& testName)
{
Poco::RegularExpression re(testName, Poco::RegularExpression::RE_CASELESS);
Poco::RegularExpression::Match reMatch;
bool haveTests = false;
for (int i = 0; i < testRegistry->getChildTestCount(); ++i)
{
CPPUNIT_NS::Test* testSuite = testRegistry->getChildTestAt(i);
for (int j = 0; j < testSuite->getChildTestCount(); ++j)
{
CPPUNIT_NS::Test* testCase = testSuite->getChildTestAt(j);
try
{
if (re.match(testCase->getName(), reMatch))
{
runner.addTest(testCase);
haveTests = true;
}
}
catch (const std::exception& exc)
{
// Nothing to do; skip.
}
}
}
return haveTests;
}
static bool IsDebugrun = false;
int main(int argc, char** argv)
{
bool verbose = false;
for (int i = 1; i < argc; ++i)
{
const std::string arg(argv[i]);
if (arg == "--verbose")
{
verbose = true;
}
else if (arg == "--debugrun")
{
IsDebugrun = true;
}
}
const char* loglevel = verbose ? "trace" : "warning";
Log::initialize("tst", loglevel, true, false, {});
return runClientTests(true, verbose)? 0: 1;
}
static bool IsStandalone = false;
bool isStandalone()
{
return IsStandalone;
}
static std::mutex errorMutex;
static bool IsVerbose = false;
static std::stringstream errors;
void tstLog(const std::ostringstream &stream)
{
if (IsVerbose)
std::cerr << stream.str() << std::endl;
else
{
std::lock_guard<std::mutex> lock(errorMutex);
errors << stream.str();
}
}
// returns true on success
bool runClientTests(bool standalone, bool verbose)
{
IsVerbose = verbose;
IsStandalone = standalone;
CPPUNIT_NS::TestResult controller;
CPPUNIT_NS::TestResultCollector result;
controller.addListener(&result);
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener(&progress);
CPPUNIT_NS::TextTestProgressListener listener;
controller.addListener(&listener);
CPPUNIT_NS::Test* testRegistry = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
CPPUNIT_NS::TestRunner runner;
const char* envar = std::getenv("CPPUNIT_TEST_NAME");
std::string testName;
if (envar)
{
testName = std::string(envar);
}
if (testName.empty())
{
// Add all tests.
runner.addTest(testRegistry);
}
else
{
const bool testsAdded = filterTests(runner, testRegistry, testName);
if (!testsAdded)
{
std::cerr << "Failed to match [" << testName << "] to any names in the external test-suite. "
<< "No external tests will be executed" << std::endl;
}
}
if (!verbose)
{
runner.run(controller);
// output the errors we got during the testing
if (!result.wasSuccessful())
std::cerr << errors.str() << std::endl;
}
else
{
runner.run(controller);
}
CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr);
outputter.setNoWrap();
outputter.write();
const std::deque<CPPUNIT_NS::TestFailure *> &failures = result.failures();
if (!envar && failures.size() > 0)
{
std::cerr << "\nTo reproduce the first test failure use:\n\n";
#ifndef UNIT_CLIENT_TESTS
const char *cmd = "./run_unit.sh --verbose";
if (getenv("UNITTEST"))
cmd = "./unittest";
std::cerr << "(cd test; CPPUNIT_TEST_NAME=\"" << (*failures.begin())->failedTestName() << "\" " << cmd << ")\n\n";
#else
std::cerr << "(cd test; CPPUNIT_TEST_NAME=\"" << (*failures.begin())->failedTestName() << "\" make check)\n\n";
#endif
}
return result.wasSuccessful();
}
// Versions assuming a single user, on a single machine
#ifndef UNIT_CLIENT_TESTS
std::vector<int> getProcPids(const char* exec_filename)
{
std::vector<int> pids;
// Ensure we're in the same group.
const int grp = getpgrp();
// Get all lokit processes.
for (auto it = Poco::DirectoryIterator(std::string("/proc")); it != Poco::DirectoryIterator(); ++it)
{
try
{
const Poco::Path& procEntry = it.path();
const std::string& fileName = procEntry.getFileName();
int pid;
std::size_t endPos = 0;
try
{
pid = std::stoi(fileName, &endPos);
}
catch (const std::invalid_argument&)
{
pid = 0;
}
if (pid > 1 && endPos == fileName.length())
{
Poco::FileInputStream stat(procEntry.toString() + "/stat");
std::string statString;
Poco::StreamCopier::copyToString(stat, statString);
Poco::StringTokenizer tokens(statString, " ");
if (tokens.count() > 6 && tokens[1].find(exec_filename) == 0)
{
// We could have several make checks running at once.
int kidGrp = std::atoi(tokens[4].c_str());
// Don't require matching grp for --debugrun invocations.
if (kidGrp != grp && !IsDebugrun)
continue;
switch (tokens[2].c_str()[0])
{
// Dead & zombie markers for old and new kernels.
case 'x':
case 'X':
case 'Z':
break;
default:
pids.push_back(pid);
break;
}
}
}
}
catch (const Poco::Exception&)
{
}
}
return pids;
}
std::vector<int> getSpareKitPids()
{
return getProcPids("(kit_spare_");
}
std::vector<int> getDocKitPids()
{
return getProcPids("(kitbroker_");
}
std::vector<int> getKitPids()
{
std::vector<int> pids = getSpareKitPids();
for (int pid : getDocKitPids())
pids.push_back(pid);
return pids;
}
int getLoolKitProcessCount()
{
return getKitPids().size();
}
std::vector<int> getForKitPids()
{
std::vector<int> pids, pids2;
pids = getProcPids("(loolforkit)");
pids2 = getProcPids("(forkit)");
pids.insert(pids.end(), pids2.begin(), pids2.end());
return pids;
}
#else // UNIT_CLIENT_TESTS
// Here we are compiled inside UnitClient.cpp and we have
// full access to the WSD process internals.
std::vector<int> getKitPids()
{
return LOOLWSD::getKitPids();
}
/// Get the PID of the forkit
std::vector<int> getForKitPids()
{
std::vector<int> pids;
if (LOOLWSD::ForKitProcId >= 0)
pids.push_back(LOOLWSD::ForKitProcId);
return pids;
}
/// How many live loolkit processes do we have ?
int getLoolKitProcessCount()
{
return getKitPids().size();
}
#endif // UNIT_CLIENT_TESTS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|