Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 BogDan Vatra <bogdan@kde.org>
2// Copyright (C) 2023 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5#include <QtCore/QCoreApplication>
6#include <QtCore/QDeadlineTimer>
7#include <QtCore/qdebug.h>
8#include <QtCore/QDir>
9#include <QtCore/QHash>
10#include <QtCore/QProcess>
11#include <QtCore/QProcessEnvironment>
12#include <QtCore/QRegularExpression>
13#include <QtCore/QSystemSemaphore>
14#include <QtCore/QThread>
15#include <QtCore/QXmlStreamReader>
16#include <QtCore/QFileInfo>
17#include <QtCore/QSysInfo>
18#include <QtCore/QTemporaryFile>
19
20#include <atomic>
21#include <csignal>
22#include <functional>
23#include <optional>
24#if defined(Q_OS_WIN32)
25#include <process.h>
26#else
27#include <unistd.h>
28#endif
29
30using namespace Qt::StringLiterals;
31
32
33// QTest-based test processes may exit with up to 127 for normal test failures
34static constexpr int HIGHEST_QTEST_EXITCODE = 127;
35// Something went wrong in androidtestrunner, in general
36static constexpr int EXIT_ERROR = 254;
37// More specific exit codes for failures in androidtestrunner:
38static constexpr int EXIT_NOEXITCODE = 253; // Failed to transfer exit code from device
39static constexpr int EXIT_ANR = 252; // Android ANR error (Application Not Responding)
40static constexpr int EXIT_NORESULTS = 251; // Failed to transfer result files from device
41
42
43struct Options
44{
45 bool helpRequested = false;
46 bool verbose = false;
47 bool skipAddInstallRoot = false;
48 int timeoutSecs = 600; // 10 minutes
66 bool showLogcatOutput = false;
68};
69
71
73{
74 int sdkVersion = -1;
75 int pid = -1;
77
78 std::atomic<bool> isPackageInstalled { false };
79 std::atomic<bool> isTestRunnerInterrupted { false };
80};
81
83
84// QTest-based processes return 0 if all tests PASSed, or the number of FAILs up to 127.
85// Other exitcodes signify abnormal termination and are system-dependent.
86static bool isTestExitCodeNormal(const int ec)
87{
88 return (ec >= 0 && ec <= HIGHEST_QTEST_EXITCODE);
89}
90
91static bool execCommand(const QString &program, const QStringList &args,
92 QByteArray *output = nullptr, bool verbose = false)
93{
94 const auto command = program + " "_L1 + args.join(u' ');
95
96 if (verbose && g_options.verbose)
97 fprintf(stdout,"Execute %s.\n", command.toUtf8().constData());
98
99 QProcess process;
100 process.start(program, args);
101 if (!process.waitForStarted()) {
102 qCritical("Cannot execute command %s.", qPrintable(command));
103 return false;
104 }
105
106 // If the command is not adb, for example, make or ninja, it can take more that
107 // QProcess::waitForFinished() 30 secs, so for that use a higher timeout.
108 const int FinishTimeout = program.endsWith("adb"_L1) ? 30000 : g_options.timeoutSecs * 1000;
109 if (!process.waitForFinished(FinishTimeout)) {
110 qCritical("Execution of command %s timed out.", qPrintable(command));
111 return false;
112 }
113
114 const auto stdOut = process.readAllStandardOutput();
115 if (output)
116 output->append(stdOut);
117
118 if (verbose && g_options.verbose)
119 fprintf(stdout, "%s\n", stdOut.constData());
120
121 return process.exitCode() == 0;
122}
123
124static bool execAdbCommand(const QStringList &args, QByteArray *output = nullptr,
125 bool verbose = true)
126{
127 if (g_options.serial.isEmpty())
128 return execCommand(g_options.adbCommand, args, output, verbose);
129
130 QStringList argsWithSerial = {"-s"_L1, g_options.serial};
131 argsWithSerial.append(args);
132
133 return execCommand(g_options.adbCommand, argsWithSerial, output, verbose);
134}
135
136static bool execBundletoolCommand(const QStringList &args, QByteArray *output = nullptr,
137 bool verbose = true)
138{
139 QString java("java"_L1);
140 QStringList argsFull = QStringList() << "-jar"_L1 << g_options.bundletoolPath << args;
141 return execCommand(java, argsFull, output, verbose);
142}
143
144static void setPackagePath(const QString &path)
145{
146 if (!g_options.packagePath.isEmpty()) {
147 qCritical("Both --aab and --apk options provided. This is not supported.");
149 return;
150 }
151 g_options.packagePath = path;
152}
153
154static bool execCommand(const QString &command, QByteArray *output = nullptr, bool verbose = true)
155{
156 auto args = QProcess::splitCommand(command);
157 const auto program = args.first();
158 args.removeOne(program);
159 return execCommand(program, args, output, verbose);
160}
161
162static bool parseOptions()
163{
164 QStringList arguments = QCoreApplication::arguments();
165 int i = 1;
166 for (; i < arguments.size(); ++i) {
167 const QString &argument = arguments.at(i);
168 if (argument.compare("--adb"_L1, Qt::CaseInsensitive) == 0) {
169 if (i + 1 == arguments.size())
171 else
172 g_options.adbCommand = arguments.at(++i);
173 } else if (argument.compare("--bundletool"_L1, Qt::CaseInsensitive) == 0) {
174 if (i + 1 == arguments.size())
176 else
177 g_options.bundletoolPath = arguments.at(++i);
178 } else if (argument.compare("--path"_L1, Qt::CaseInsensitive) == 0) {
179 if (i + 1 == arguments.size())
181 else
182 g_options.buildPath = arguments.at(++i);
183 } else if (argument.compare("--manifest"_L1, Qt::CaseInsensitive) == 0) {
184 if (i + 1 == arguments.size())
186 else
187 g_options.manifestPath = arguments.at(++i);
188 } else if (argument.compare("--make"_L1, Qt::CaseInsensitive) == 0) {
189 if (i + 1 == arguments.size())
191 else
192 g_options.makeCommand = arguments.at(++i);
193 } else if (argument.compare("--apk"_L1, Qt::CaseInsensitive) == 0) {
194 if (i + 1 == arguments.size())
196 else
197 setPackagePath(arguments.at(++i));
198 } else if (argument.compare("--aab"_L1, Qt::CaseInsensitive) == 0) {
199 if (i + 1 == arguments.size())
201 else
202 setPackagePath(arguments.at(++i));
203 } else if (argument.compare("--activity"_L1, Qt::CaseInsensitive) == 0) {
204 if (i + 1 == arguments.size())
206 else
207 g_options.activity = arguments.at(++i);
208 } else if (argument.compare("--skip-install-root"_L1, Qt::CaseInsensitive) == 0) {
210 } else if (argument.compare("--show-logcat"_L1, Qt::CaseInsensitive) == 0) {
212 } else if (argument.compare("--ndk-stack"_L1, Qt::CaseInsensitive) == 0) {
213 if (i + 1 == arguments.size())
215 else
216 g_options.ndkStackPath = arguments.at(++i);
217 } else if (argument.compare("--timeout"_L1, Qt::CaseInsensitive) == 0) {
218 if (i + 1 == arguments.size())
220 else
221 g_options.timeoutSecs = arguments.at(++i).toInt();
222 } else if (argument.compare("--help"_L1, Qt::CaseInsensitive) == 0) {
224 } else if (argument.compare("--verbose"_L1, Qt::CaseInsensitive) == 0) {
225 g_options.verbose = true;
226 } else if (argument.compare("--pre-test-adb-command"_L1, Qt::CaseInsensitive) == 0) {
227 if (i + 1 == arguments.size())
229 else {
230 g_options.preTestRunAdbCommands += QProcess::splitCommand(arguments.at(++i));
231 }
232 } else if (argument.compare("--"_L1, Qt::CaseInsensitive) == 0) {
233 ++i;
234 break;
235 } else {
236 g_options.testArgsList << arguments.at(i);
237 }
238 }
239
241 // we need to run make INSTALL_ROOT=path install to install the application file(s) first
242 g_options.makeCommand = "%1 INSTALL_ROOT=%2 install"_L1
243 .arg(g_options.makeCommand)
244 .arg(QDir::toNativeSeparators(g_options.buildPath));
245 }
246
247 for (;i < arguments.size(); ++i)
248 g_options.testArgsList << arguments.at(i);
249
250 if (g_options.helpRequested || g_options.buildPath.isEmpty() || g_options.packagePath.isEmpty())
251 return false;
252
253 g_options.serial = qEnvironmentVariable("ANDROID_SERIAL");
254 if (g_options.serial.isEmpty())
255 g_options.serial = qEnvironmentVariable("ANDROID_DEVICE_SERIAL");
256
257 if (g_options.ndkStackPath.isEmpty()) {
258 const QString ndkPath = qEnvironmentVariable("ANDROID_NDK_ROOT");
259 const QString ndkStackPath = ndkPath + QDir::separator() + "ndk-stack"_L1;
260 if (QFile::exists(ndkStackPath))
261 g_options.ndkStackPath = ndkStackPath;
262 }
263
264 if (g_options.manifestPath.isEmpty()) {
265 const QStringList manifestCandidates = {
266 g_options.buildPath + "/AndroidManifest.xml"_L1,
267 g_options.buildPath + "/app/AndroidManifest.xml"_L1,
268 };
269 for (const QString &candidate : manifestCandidates) {
270 if (QFile::exists(candidate)) {
271 g_options.manifestPath = candidate;
272 break;
273 }
274 }
275 }
276
277 return true;
278}
279
280static void printHelp()
281{
282 qWarning("Syntax: %s <options> -- [TESTARGS] \n"
283 "\n"
284 " Runs a Qt for Android test on an emulator or a device. Specify a device\n"
285 " using the environment variables ANDROID_SERIAL or ANDROID_DEVICE_SERIAL.\n"
286 " Returns the number of failed tests, -1 on test runner deployment related\n"
287 " failures or zero on success."
288 "\n"
289 " Mandatory arguments:\n"
290 " --path <path>: The path where androiddeployqt builds the android package.\n"
291 "\n"
292 " --make <make cmd>: make command to create an APK, for example:\n"
293 " \"cmake --build <build-dir> --target <target>_make_apk\".\n"
294 "\n"
295 " --apk <apk path>: The test apk path. The apk has to exist already, if it\n"
296 " does not exist the make command must be provided for building the apk.\n"
297 "\n"
298 " --aab <aab path>: The test aab path. The aab has to exist already, if it\n"
299 " does not exist the make command must be provided for building the aab.\n"
300 "\n"
301 " Optional arguments:\n"
302 " --adb <adb cmd>: The Android ADB command. If missing the one from\n"
303 " $PATH will be used.\n"
304 "\n"
305 " --activity <acitvity>: The Activity to run. If missing the first\n"
306 " activity from AndroidManifest.qml file will be used.\n"
307 "\n"
308 " --timeout <seconds>: Timeout to run the test. Default is 10 minutes.\n"
309 "\n"
310 " --skip-install-root: Do not append INSTALL_ROOT=... to the make command.\n"
311 "\n"
312 " --show-logcat: Print Logcat output to stdout. If an ANR occurs during\n"
313 " the test run, logs from the system_server process are included.\n"
314 "\n"
315 " --ndk-stack: Path to ndk-stack tool that symbolizes crash stacktraces.\n"
316 " By default, ANDROID_NDK_ROOT env var is used to deduce the tool path.\n"
317 "\n"
318 " -- Arguments that will be passed to the test application.\n"
319 "\n"
320 " --verbose: Prints out information during processing.\n"
321 "\n"
322 " --pre-test-adb-command <command>: call the adb <command> after\n"
323 " installation and before the test run.\n"
324 "\n"
325 " --manifest <path>: Custom path to the AndroidManifest.xml.\n"
326 "\n"
327 " --bundletool <bundletool path>: The path to Android bundletool.\n"
328 " See https://developer.android.com/tools/bundletool for details.\n"
329 "\n"
330 " --help: Displays this information.\n",
331 qPrintable(QCoreApplication::arguments().at(0)));
332}
333
335{
336 static QString path;
337 if (!path.isEmpty())
338 return path;
339
340 QTemporaryFile initScript;
341 initScript.setAutoRemove(false);
342 if (!initScript.open())
343 return {};
344
345 initScript.write(
346 "gradle.projectsEvaluated {\n"
347 " def prop = gradle.rootProject.findProperty(\"property\")\n"
348 " def target = gradle.rootProject.findProject(':app') ?: gradle.rootProject\n"
349 " for (part in prop.tokenize('.')) {\n"
350 " target = target.\"${part}\"\n"
351 " }\n"
352 " println target\n"
353 "}\n"
354 "rootProject { tasks.register(\"printProjectProperty\") }\n");
355 initScript.close();
356
357 path = initScript.fileName();
358 qAddPostRoutine([] { QFile::remove(path); });
359 return path;
360}
361
362// Query a Gradle project's property using dot-separated path notation e.g. "android.namespace".
363static QString getGradleProjectProperty(const QString &androidBuildDir, const QString &property)
364{
365#ifdef Q_OS_WIN
366 QString gradlew = androidBuildDir + "/gradlew.bat"_L1;
367#else
368 QString gradlew = androidBuildDir + "/gradlew"_L1;
369#endif
370 if (!QFile::exists(gradlew))
371 return {};
372
373 const QString scriptPath = gradleInitScriptPath();
374 if (scriptPath.isEmpty())
375 return {};
376
377 QProcess process;
378 process.setWorkingDirectory(androidBuildDir);
379 process.start(gradlew, { "-q"_L1, "--init-script"_L1, scriptPath,
380 "-Pproperty="_L1 + property, "printProjectProperty"_L1 });
381
382 if (!process.waitForFinished(30000))
383 return {};
384
385 return QString::fromUtf8(process.readAllStandardOutput()).trimmed();
386}
387
389{
390 QFile androidManifestXml(g_options.manifestPath);
391 if (!androidManifestXml.open(QIODevice::ReadOnly)) {
392 qCritical("Unable to read android manifest '%s'", qPrintable(g_options.manifestPath));
393 return false;
394 }
395
396 QXmlStreamReader reader(&androidManifestXml);
397 while (!reader.atEnd()) {
398 reader.readNext();
399 if (!reader.isStartElement())
400 continue;
401
402 else if (reader.name() == "activity"_L1 && g_options.activity.isEmpty())
403 g_options.activity = reader.attributes().value("android:name"_L1).toString();
404 else if (reader.name() == "uses-permission"_L1)
405 g_options.permissions.append(reader.attributes().value("android:name"_L1).toString());
406 }
407 return true;
408}
409
411{
412 QByteArray output;
413 const QStringList args({ "shell"_L1, "dumpsys"_L1, "package"_L1, "permissions"_L1 });
414 if (!execAdbCommand(args, &output, false)) {
415 qWarning("Failed to query permissions via dumpsys");
416 return {};
417 }
418
419 /*
420 * Permissions section from this command look like:
421 *
422 * Permission [android.permission.INTERNET] (c8cafdc):
423 * sourcePackage=android
424 * uid=1000 gids=[3003] type=0 prot=normal|instant
425 * perm=PermissionInfo{5f5bfbb android.permission.INTERNET}
426 * flags=0x0
427 */
428 const static QRegularExpression regex("^\\s*Permission\\s+\\‍[([^\\‍]]+)\\‍]\\s+\\‍(([^)]+)\\‍):"_L1);
429 QStringList dangerousPermissions;
430 QString currentPerm;
431
432 const QStringList lines = QString::fromUtf8(output).split(u'\n');
433 for (const QString &line : lines) {
434 QRegularExpressionMatch match = regex.match(line);
435 if (match.hasMatch()) {
436 currentPerm = match.captured(1);
437 continue;
438 }
439
440 if (currentPerm.isEmpty())
441 continue;
442
443 int protIndex = line.indexOf("prot="_L1);
444 if (protIndex == -1)
445 continue;
446
447 QString protectionTypes = line.mid(protIndex + 5).trimmed();
448 if (protectionTypes.contains("dangerous"_L1, Qt::CaseInsensitive)) {
449 dangerousPermissions.append(currentPerm);
450 currentPerm.clear();
451 }
452 }
453
454 return dangerousPermissions;
455}
456
457static void setOutputFile(QString file, QString format)
458{
459 if (format.isEmpty())
460 format = "txt"_L1;
461
462 if ((file.isEmpty() || file == u'-')) {
463 if (g_options.outFiles.contains(format)) {
464 file = g_options.outFiles.value(format);
465 } else {
466 file = "stdout.%1"_L1.arg(format);
467 g_options.outFiles[format] = file;
468 }
469 g_options.stdoutFileName = QFileInfo(file).fileName();
470 } else {
471 g_options.outFiles[format] = file;
472 }
473}
474
475static bool parseTestArgs()
476{
477 QRegularExpression oldFormats{"^-(txt|csv|xunitxml|junitxml|xml|lightxml|teamcity|tap)$"_L1};
478 QRegularExpression newLoggingFormat{"^(.*),(txt|csv|xunitxml|junitxml|xml|lightxml|teamcity|tap)$"_L1};
479
480 QString file;
481 QString logType;
482 QStringList unhandledArgs;
483 for (int i = 0; i < g_options.testArgsList.size(); ++i) {
484 const QString &arg = g_options.testArgsList[i].trimmed();
485 if (arg == "--"_L1)
486 continue;
487 if (arg == "-o"_L1) {
488 if (i >= g_options.testArgsList.size() - 1)
489 return false; // missing file argument
490
491 const auto &filePath = g_options.testArgsList[++i];
492 const auto match = newLoggingFormat.match(filePath);
493 if (!match.hasMatch()) {
494 file = filePath;
495 } else {
496 const auto capturedTexts = match.capturedTexts();
497 setOutputFile(capturedTexts.at(1), capturedTexts.at(2));
498 }
499 } else {
500 auto match = oldFormats.match(arg);
501 if (match.hasMatch()) {
502 logType = match.capturedTexts().at(1);
503 } else {
504 // Use triple literal quotes so that QProcess::splitCommand() in androidjnimain.cpp
505 // keeps quotes characters inside the string.
506 QString quotedArg = QString(arg).replace("\""_L1, "\\\"\\\"\\\""_L1);
507 // Escape single quotes so they don't interfere with the shell command,
508 // and so they get passed to the app as single quote inside the string.
509 quotedArg.replace("'"_L1, "\'"_L1);
510 // Add escaped double quote character so that args with spaces are treated as one.
511 unhandledArgs << " \\\"%1\\\""_L1.arg(quotedArg);
512 }
513 }
514 }
515 if (g_options.outFiles.isEmpty() || !file.isEmpty() || !logType.isEmpty())
516 setOutputFile(file, logType);
517
518 QString testAppArgs;
519 for (auto it = g_options.outFiles.constBegin(); it != g_options.outFiles.constEnd(); ++it)
520 testAppArgs += "-o %1,%2 "_L1.arg(QFileInfo(it.value()).fileName(), it.key());
521
522 testAppArgs += unhandledArgs.join(u' ').trimmed();
523 testAppArgs = "\"%1\""_L1.arg(testAppArgs.trimmed());
524 const QString activityName = "%1/%2"_L1.arg(g_options.package).arg(g_options.activity);
525
526 // Pass over any qt or testlib env vars if set
527 QString testEnvVars;
528 const QStringList envVarsList = QProcessEnvironment::systemEnvironment().toStringList();
529 for (const QString &var : envVarsList) {
530 if (var.startsWith("QTEST_"_L1) || var.startsWith("QT_"_L1))
531 testEnvVars += "%1 "_L1.arg(var);
532 }
533
534 if (!testEnvVars.isEmpty()) {
535 testEnvVars = QString::fromUtf8(testEnvVars.trimmed().toUtf8().toBase64());
536 testEnvVars = "-e extraenvvars \"%4\""_L1.arg(testEnvVars);
537 }
538
539 g_options.amStarttestArgs = { "shell"_L1, "am"_L1, "start"_L1,
540 "-n"_L1, activityName,
541 "-e"_L1, "applicationArguments"_L1, testAppArgs,
542 testEnvVars
543 };
544
545 return true;
546}
547
548static int getPid(const QString &package)
549{
550 QByteArray output;
551 const QStringList psArgs = { "shell"_L1, "ps | grep ' %1'"_L1.arg(package) };
552 if (!execAdbCommand(psArgs, &output, false))
553 return false;
554
555 const QList<QByteArray> lines = output.split(u'\n');
556 if (lines.size() < 1)
557 return false;
558
559 QList<QByteArray> columns = lines.first().simplified().replace(u'\t', u' ').split(u' ');
560 if (columns.size() < 3)
561 return false;
562
563 bool ok = false;
564 int pid = columns.at(1).toInt(&ok);
565 if (ok)
566 return pid;
567
568 return -1;
569}
570
571static QString runCommandAsUserArgs(const QString &cmd)
572{
573 return "run-as %1 --user %2 %3"_L1.arg(g_options.package, g_testInfo.userId, cmd);
574}
575
576static bool isRunning() {
577 if (g_testInfo.pid < 1)
578 return false;
579
580 QByteArray output;
581 const QStringList psArgs = { "shell"_L1, "ps"_L1, "-p"_L1, QString::number(g_testInfo.pid),
582 "|"_L1, "grep"_L1, "-o"_L1, " %1$"_L1.arg(g_options.package) };
583 bool psSuccess = false;
584 for (int i = 1; i <= 3; ++i) {
585 psSuccess = execAdbCommand(psArgs, &output, false);
586 if (psSuccess)
587 break;
588 QThread::msleep(250);
589 }
590
591 return psSuccess && output.trimmed() == g_options.package.toUtf8();
592}
593
594static void waitForStarted()
595{
596 // wait to start and set PID
597 QDeadlineTimer startDeadline(10000);
598 do {
599 g_testInfo.pid = getPid(g_options.package);
600 if (g_testInfo.pid > 0)
601 break;
602 QThread::msleep(100);
603 } while (!startDeadline.hasExpired() && !g_testInfo.isTestRunnerInterrupted.load());
604}
605
607{
608 const QString lsCmd = "ls files/%1"_L1.arg(g_options.stdoutFileName);
609 const QStringList adbLsCmd = { "shell"_L1, runCommandAsUserArgs(lsCmd) };
610
611 QDeadlineTimer deadline(5000);
612 do {
613 if (execAdbCommand(adbLsCmd, nullptr, false))
614 break;
615 QThread::msleep(100);
616 } while (!deadline.hasExpired() && !g_testInfo.isTestRunnerInterrupted.load());
617}
618
619static bool setupStdoutLogger()
620{
621 // Start tail to get results to stdout as soon as they're available
622 const QString tailPipeCmd = "tail -n +1 -f files/%1"_L1.arg(g_options.stdoutFileName);
623 const QStringList adbTailCmd = { "shell"_L1, runCommandAsUserArgs(tailPipeCmd) };
624
625 g_options.stdoutLogger.emplace();
626 g_options.stdoutLogger->setProcessChannelMode(QProcess::ForwardedOutputChannel);
627 g_options.stdoutLogger->start(g_options.adbCommand, adbTailCmd);
628
629 if (!g_options.stdoutLogger->waitForStarted()) {
630 qCritical() << "Error: failed to run adb command to fetch stdout test results.";
631 g_options.stdoutLogger = std::nullopt;
632 return false;
633 }
634
635 return true;
636}
637
638static bool stopStdoutLogger()
639{
640 if (!g_options.stdoutLogger.has_value()) {
641 // In case this ever happens, it setupStdoutLogger() wasn't called, whether
642 // that's on purpose or not, return true since what it does is achieved.
643 qCritical() << "Trying to stop the stdout logger process while it's been uninitialised";
644 return true;
645 }
646
647 if (g_options.stdoutLogger->state() == QProcess::NotRunning) {
648 // We expect the tail command to be running until we stop it, so if it's
649 // not running it might have been terminated outside of the test runner.
650 qCritical() << "The stdout logger process was terminated unexpectedly, "
651 "It might have been terminated by an external process";
652 return false;
653 }
654
655 g_options.stdoutLogger->terminate();
656
657 if (!g_options.stdoutLogger->waitForFinished()) {
658 qCritical() << "Error: adb test results tail command timed out.";
659 return false;
660 }
661
662 return true;
663}
664
665static void waitForFinished()
666{
667 // Wait to finish
668 QDeadlineTimer finishedDeadline(g_options.timeoutSecs * 1000);
669 do {
670 if (!isRunning())
671 break;
672 QThread::msleep(250);
673 } while (!finishedDeadline.hasExpired() && !g_testInfo.isTestRunnerInterrupted.load());
674
675 if (finishedDeadline.hasExpired())
676 qWarning() << "Timed out while waiting for the test to finish";
677}
678
679static void obtainSdkVersion()
680{
681 // SDK version is necessary, as in SDK 23 pidof is broken, so we cannot obtain the pid.
682 // Also, Logcat cannot filter by pid in SDK 23, so we don't offer the --show-logcat option.
683 QByteArray output;
684 const QStringList versionArgs = { "shell"_L1, "getprop"_L1, "ro.build.version.sdk"_L1 };
685 execAdbCommand(versionArgs, &output, false);
686 bool ok = false;
687 int sdkVersion = output.toInt(&ok);
688 if (ok)
689 g_testInfo.sdkVersion = sdkVersion;
690 else
691 qCritical() << "Unable to obtain the SDK version of the target.";
692}
693
695{
696 // adb get-current-user command is available starting from API level 26.
697 QByteArray userId;
698 if (g_testInfo.sdkVersion >= 26) {
699 const QStringList userIdArgs = {"shell"_L1, "cmd"_L1, "activity"_L1, "get-current-user"_L1};
700 if (!execAdbCommand(userIdArgs, &userId, false)) {
701 qCritical() << "Error: failed to retrieve the user ID";
702 userId.clear();
703 }
704 }
705
706 if (userId.isEmpty())
707 userId = "0";
708
709 return QString::fromUtf8(userId.simplified());
710}
711
713{
714 QByteArray output;
715 execAdbCommand({ "devices"_L1 }, &output, false);
716
717 QStringList devices;
718 for (const QByteArray &line : output.split(u'\n')) {
719 if (line.contains("\tdevice"_L1))
720 devices.append(QString::fromUtf8(line.split(u'\t').first()));
721 }
722
723 return devices;
724}
725
726static bool pullResults()
727{
728 for (auto it = g_options.outFiles.constBegin(); it != g_options.outFiles.constEnd(); ++it) {
729 const QString filePath = it.value();
730 const QString fileName = QFileInfo(filePath).fileName();
731 // Get only stdout from cat and get rid of stderr and fail later if the output is empty
732 const QString catCmd = "cat files/%1 2> /dev/null"_L1.arg(fileName);
733 const QStringList fullCatArgs = { "shell"_L1, runCommandAsUserArgs(catCmd) };
734
735 bool catSuccess = false;
736 QByteArray output;
737
738 for (int i = 1; i <= g_options.resultsPullRetries; ++i) {
739 catSuccess = execAdbCommand(fullCatArgs, &output, false);
740 if (!catSuccess)
741 continue;
742 else if (!output.isEmpty())
743 break;
744 }
745
746 if (!catSuccess) {
747 qCritical() << "Error: failed to retrieve the test result file %1."_L1.arg(fileName);
748 return false;
749 }
750
751 if (output.isEmpty()) {
752 qCritical() << "Error: the test result file %1 is empty."_L1.arg(fileName);
753 return false;
754 }
755
756 QFile out{filePath};
757 if (!out.open(QIODevice::WriteOnly)) {
758 qCritical() << "Error: failed to open %1 to write results to host."_L1.arg(filePath);
759 return false;
760 }
761 out.write(output);
762 }
763
764 return true;
765}
766
768{
769 QString libsPath = "%1/libs/"_L1.arg(g_options.buildPath);
770 const QStringList abiArgs = { "shell"_L1, "getprop"_L1, "ro.product.cpu.abi"_L1 };
771 QByteArray abi;
772 if (!execAdbCommand(abiArgs, &abi, false)) {
773 QStringList subDirs = QDir(libsPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
774 if (!subDirs.isEmpty())
775 abi = subDirs.first().toUtf8();
776 }
777
778 abi = abi.trimmed();
779 if (abi.isEmpty())
780 qWarning() << "Failed to get the libs abi, falling to host architecture";
781
782 QString hostArch = QSysInfo::currentCpuArchitecture();
783 if (hostArch == "x86_64"_L1)
784 abi = "arm64-x86_64";
785 else if (hostArch == "arm64"_L1)
786 abi = "arm64-v8a";
787 else if (hostArch == "i386"_L1)
788 abi = "x86";
789 else
790 abi = "armeabi-v7a";
791
792 return libsPath + QString::fromUtf8(abi);
793}
794
795void printLogcatCrash(const QByteArray &logcat)
796{
797 // No crash report, do nothing
798 if (logcat.isEmpty())
799 return;
800
801 QByteArray crashLogcat(logcat);
802 if (!g_options.ndkStackPath.isEmpty()) {
803 QProcess ndkStackProc;
804 ndkStackProc.start(g_options.ndkStackPath, { "-sym"_L1, getAbiLibsPath() });
805
806 if (ndkStackProc.waitForStarted()) {
807 ndkStackProc.write(crashLogcat);
808 ndkStackProc.closeWriteChannel();
809
810 if (ndkStackProc.waitForReadyRead())
811 crashLogcat = ndkStackProc.readAllStandardOutput();
812
813 ndkStackProc.terminate();
814 if (!ndkStackProc.waitForFinished())
815 qCritical() << "Error: ndk-stack command timed out.";
816 } else {
817 qCritical() << "Error: failed to run ndk-stack command.";
818 return;
819 }
820 } else {
821 qWarning() << "Warning: ndk-stack path not provided and couldn't be deduced "
822 "using the ANDROID_NDK_ROOT environment variable.";
823 }
824
825 if (!crashLogcat.startsWith("********** Crash dump")) {
826 qDebug() << "[androidtestrunner] ********** BEGIN crash dump **********";
827 qDebug().noquote() << crashLogcat.trimmed();
828 qDebug() << "[androidtestrunner] ********** END crash dump **********";
829 }
830}
831
832void analyseLogcat(const QString &timeStamp, int *exitCode)
833{
834 QStringList logcatArgs = { "shell"_L1, "logcat"_L1, "-t"_L1, "'%1'"_L1.arg(timeStamp),
835 "-v"_L1, "brief"_L1 };
836
837 const bool useColor = qEnvironmentVariable("QTEST_ENVIRONMENT") != "ci"_L1;
838 if (useColor)
839 logcatArgs << "-v"_L1 << "color"_L1;
840
841 QByteArray logcat;
842 if (!execAdbCommand(logcatArgs, &logcat, false)) {
843 qCritical() << "Error: failed to fetch logcat of the test";
844 return;
845 }
846
847 if (logcat.isEmpty()) {
848 qWarning() << "The retrieved logcat is empty";
849 return;
850 }
851
852 const QByteArray crashMarker("*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***");
853 int crashMarkerIndex = logcat.indexOf(crashMarker);
854 QByteArray crashLogcat;
855
856 if (crashMarkerIndex != -1) {
857 crashLogcat = logcat.mid(crashMarkerIndex);
858 logcat = logcat.left(crashMarkerIndex);
859 }
860
861 // Check for ANRs
862 const bool anrOccurred = logcat.contains("ANR in %1"_L1.arg(g_options.package).toUtf8());
863 if (anrOccurred) {
864 // Rather improbable, but if the test managed to return a non-crash exitcode then overwrite
865 // it to signify that something blew up. Same if we didn't manage to collect an exit code.
866 // Preserve all other exitcodes, they might be useful crash information from the device.
867 if (isTestExitCodeNormal(*exitCode) || *exitCode == EXIT_NOEXITCODE)
868 *exitCode = EXIT_ANR;
869 qCritical("[androidtestrunner] An ANR has occurred while running the test '%s';"
870 " consult logcat for additional logs from the system_server process",
871 qPrintable(g_options.package));
872 }
873
874 int systemServerPid = getPid("system_server"_L1);
875
876 static const QRegularExpression logcatRegEx{
877 "(?:^\\x1B\\‍[[0-9]+m)?" // color
878 "(\\w)/" // message type 1. capture
879 ".*" // source
880 "(\\‍(\\s*\\d*\\‍)):" // pid 2. capture
881 "\\s*"
882 ".*" // message
883 "(?:\\x1B\\‍[[0-9]+m)?" // color
884 "[\\n\\r]*$"_L1
885 };
886
887 QByteArrayList testLogcat;
888 for (const QByteArray &line : logcat.split(u'\n')) {
889 QRegularExpressionMatch match = logcatRegEx.match(QString::fromUtf8(line));
890 if (match.hasMatch()) {
891 const QString msgType = match.captured(1);
892 const QString pidStr = match.captured(2);
893 const int capturedPid = pidStr.mid(1, pidStr.size() - 2).trimmed().toInt();
894 if (capturedPid == g_testInfo.pid || msgType == u'F')
895 testLogcat.append(line);
896 else if (anrOccurred && capturedPid == systemServerPid)
897 testLogcat.append(line);
898 } else {
899 // If we can't match then just print everything
900 testLogcat.append(line);
901 }
902 }
903
904 // If we have an unpredictable exitcode, possibly a crash, attempt to print both logcat and the
905 // crash buffer which includes the crash stacktrace that is not included in the default logcat.
906 const bool testCrashed = ( !isTestExitCodeNormal(*exitCode)
907 && !g_testInfo.isTestRunnerInterrupted.load());
908 if (testCrashed) {
909 qDebug() << "[androidtestrunner] ********** BEGIN logcat dump **********";
910 qDebug().noquote() << testLogcat.join(u'\n').trimmed();
911 qDebug() << "[androidtestrunner] ********** END logcat dump **********";
912
913 if (!crashLogcat.isEmpty())
914 printLogcatCrash(crashLogcat);
915 }
916}
917
919{
920 const QString timeFormat = (g_testInfo.sdkVersion <= 23) ?
921 "%m-%d %H:%M:%S.000"_L1 : "%Y-%m-%d %H:%M:%S.%3N"_L1;
922
923 QStringList dateArgs = { "shell"_L1, "date"_L1, "+'%1'"_L1.arg(timeFormat) };
924 QByteArray output;
925 if (!execAdbCommand(dateArgs, &output, false)) {
926 qWarning() << "[androidtestrunner] ERROR in command: adb shell date";
927 return {};
928 }
929
930 return QString::fromUtf8(output.simplified());
931}
932
933static int testExitCode()
934{
935 QByteArray exitCodeOutput;
936 const QString exitCodeCmd = "cat files/qtest_last_exit_code 2> /dev/null"_L1;
937 if (!execAdbCommand({ "shell"_L1, runCommandAsUserArgs(exitCodeCmd) }, &exitCodeOutput, false)) {
938 qCritical() << "[androidtestrunner] ERROR in command: adb shell cat files/qtest_last_exit_code";
939 return EXIT_NOEXITCODE;
940 }
941 qDebug() << "[androidtestrunner] Test exitcode: " << exitCodeOutput;
942
943 bool ok;
944 int exitCode = exitCodeOutput.toInt(&ok);
945
946 return ok ? exitCode : EXIT_NOEXITCODE;
947}
948
950{
951 return execAdbCommand({ "uninstall"_L1, g_options.package }, nullptr);
952}
953
955{
958
959 void acquire() { isAcquired.store(semaphore.acquire()); }
960
961 void release()
962 {
963#if !defined(Q_OS_WIN32)
964 // Block signals around CAS + release sequence so that no signal arrives
965 // after isAcquired is cleared but before the OS semaphore is actually
966 // released (which would leave it held after _exit() call).
967 sigset_t newMask, oldMask;
968 sigfillset(&newMask);
969 sigprocmask(SIG_BLOCK, &newMask, &oldMask);
970#endif
971 bool expected = true;
972 if (isAcquired.compare_exchange_strong(expected, false))
973 isAcquired.store(!semaphore.release());
974#if !defined(Q_OS_WIN32)
975 sigprocmask(SIG_SETMASK, &oldMask, nullptr);
976#endif
977 }
978
979 std::atomic<bool> isAcquired { false };
982};
983
985
986void sigHandler(int signal)
987{
988 std::signal(signal, SIG_DFL);
990 // Ideally we shouldn't be doing such calls from a signal handler,
991 // and we can't use QSocketNotifier because this tool doesn't spin
992 // a main event loop. Since, there's no other alternative to do this,
993 // let's do the cleanup anyway.
994 if (!g_testInfo.isPackageInstalled.load())
995 _exit(EXIT_ERROR);
996 g_testInfo.isTestRunnerInterrupted.store(true);
997}
998
999int main(int argc, char *argv[])
1000{
1001 std::signal(SIGINT, sigHandler);
1002 std::signal(SIGTERM, sigHandler);
1003
1004 QCoreApplication a(argc, argv);
1005 if (!parseOptions()) {
1007 return EXIT_ERROR;
1008 }
1009
1010 if (g_options.makeCommand.isEmpty()) {
1011 qCritical() << "It is required to provide a make command with the \"--make\" parameter "
1012 "to generate the apk.";
1013 return EXIT_ERROR;
1014 }
1015
1016 QByteArray buildOutput;
1017 if (!execCommand(g_options.makeCommand, &buildOutput, true)) {
1018 qCritical("The APK build command \"%s\" failed\n\n%s",
1019 qPrintable(g_options.makeCommand), buildOutput.constData());
1020 return EXIT_ERROR;
1021 }
1022
1023 if (!QFile::exists(g_options.packagePath)) {
1024 qCritical("No apk \"%s\" found after running the make command. "
1025 "Check the provided path and the make command.",
1026 qPrintable(g_options.packagePath));
1027 return EXIT_ERROR;
1028 }
1029
1030 const QStringList devices = runningDevices();
1031 if (devices.isEmpty()) {
1032 qCritical("No connected devices or running emulators can be found.");
1033 return EXIT_ERROR;
1034 } else if (!g_options.serial.isEmpty() && !devices.contains(g_options.serial)) {
1035 qCritical("No connected device or running emulator with serial '%s' can be found.",
1036 qPrintable(g_options.serial));
1037 return EXIT_ERROR;
1038 }
1039
1041
1042 g_testInfo.userId = userId();
1043
1044 if (!QFile::exists(g_options.manifestPath)) {
1045 qCritical("Unable to find '%s'.", qPrintable(g_options.manifestPath));
1046 return EXIT_ERROR;
1047 }
1048
1050 return EXIT_ERROR;
1051
1052 const QString ns = getGradleProjectProperty(g_options.buildPath, "android.namespace"_L1);
1053 if (!ns.isEmpty())
1054 g_options.package = ns;
1055
1056 if (g_options.package.isEmpty()) {
1057 qCritical("Unable to get package name for '%s'", qPrintable(g_options.packagePath));
1058 return EXIT_ERROR;
1059 }
1060
1061 // parseTestArgs depends on g_options.package
1062 if (!parseTestArgs())
1063 return EXIT_ERROR;
1064
1065 // do not install or run packages while another test is running
1067
1068 if (g_options.packagePath.endsWith(".apk"_L1)) {
1069 const QStringList installArgs = { "install"_L1, "-r"_L1, g_options.packagePath };
1070 g_testInfo.isPackageInstalled.store(execAdbCommand(installArgs, nullptr));
1071 if (!g_testInfo.isPackageInstalled)
1072 return EXIT_ERROR;
1073 } else if (g_options.packagePath.endsWith(".aab"_L1)) {
1074 QFileInfo aab(g_options.packagePath);
1075 const auto apksFilePath = aab.absoluteDir().absoluteFilePath(aab.baseName() + ".apks"_L1);
1076 if (!execBundletoolCommand({ "build-apks"_L1, "--bundle"_L1, g_options.packagePath,
1077 "--output"_L1, apksFilePath, "--local-testing"_L1,
1078 "--overwrite"_L1 }))
1079 return EXIT_ERROR;
1080
1081 if (!execBundletoolCommand({ "install-apks"_L1, "--apks"_L1, apksFilePath }))
1082 return EXIT_ERROR;
1083
1084 g_testInfo.isPackageInstalled.store(true);
1085 }
1086
1087 const QStringList dangerousPermisisons = queryDangerousPermissions();
1088 for (const auto &permission : g_options.permissions) {
1089 if (!dangerousPermisisons.contains(permission))
1090 continue;
1091
1092 if (!execAdbCommand({ "shell"_L1, "pm"_L1, "grant"_L1, g_options.package, permission },
1093 nullptr)) {
1094 qWarning("Unable to grant '%s' to '%s'. Probably the Android version mismatch.",
1095 qPrintable(permission), qPrintable(g_options.package));
1096 }
1097 }
1098
1099 // Call additional adb command if set after installation and before starting the test
1100 for (const auto &command : g_options.preTestRunAdbCommands) {
1101 QByteArray output;
1102 if (!execAdbCommand(command, &output)) {
1103 qCritical("The pre test ADB command \"%s\" failed with output:\n%s",
1104 qUtf8Printable(command.join(u' ')), output.constData());
1105 return EXIT_ERROR;
1106 }
1107 }
1108
1109 // Pre test start
1110 const QString formattedStartTime = getCurrentTimeString();
1111
1112 // Start the test
1113 if (!execAdbCommand(g_options.amStarttestArgs, nullptr))
1114 return EXIT_ERROR;
1115
1118
1120 return EXIT_ERROR;
1121
1123
1124 // Post test run
1125 if (!stopStdoutLogger())
1126 return EXIT_ERROR;
1127
1128 int exitCode = testExitCode();
1129
1131 analyseLogcat(formattedStartTime, &exitCode);
1132
1133 const bool pullRes = pullResults();
1134 if (!pullRes && isTestExitCodeNormal(exitCode))
1135 exitCode = EXIT_NORESULTS;
1136
1138 return EXIT_ERROR;
1139
1141
1142 if (g_testInfo.isTestRunnerInterrupted.load()) {
1143 qCritical() << "The androidtestrunner was interrupted and the was test cleaned up.";
1144 return EXIT_ERROR;
1145 }
1146
1147 return exitCode;
1148}
static QString userId()
Definition main.cpp:694
static QString runCommandAsUserArgs(const QString &cmd)
Definition main.cpp:571
static void waitForLoggingStarted()
Definition main.cpp:606
static bool setupStdoutLogger()
Definition main.cpp:619
static QString getCurrentTimeString()
Definition main.cpp:918
static int getPid(const QString &package)
Definition main.cpp:548
static QString getAbiLibsPath()
Definition main.cpp:767
static int testExitCode()
Definition main.cpp:933
static void setOutputFile(QString file, QString format)
Definition main.cpp:457
static bool parseTestArgs()
Definition main.cpp:475
static QString gradleInitScriptPath()
Definition main.cpp:334
static TestInfo g_testInfo
Definition main.cpp:82
static bool isRunning()
Definition main.cpp:576
static bool pullResults()
Definition main.cpp:726
static void obtainSdkVersion()
Definition main.cpp:679
static bool execAdbCommand(const QStringList &args, QByteArray *output=nullptr, bool verbose=true)
Definition main.cpp:124
void sigHandler(int signal)
Definition main.cpp:986
static void setPackagePath(const QString &path)
Definition main.cpp:144
static constexpr int EXIT_NOEXITCODE
Definition main.cpp:38
static bool execBundletoolCommand(const QStringList &args, QByteArray *output=nullptr, bool verbose=true)
Definition main.cpp:136
static constexpr int HIGHEST_QTEST_EXITCODE
Definition main.cpp:34
static void waitForFinished()
Definition main.cpp:665
static constexpr int EXIT_NORESULTS
Definition main.cpp:40
static bool parseOptions()
Definition main.cpp:162
void printLogcatCrash(const QByteArray &logcat)
Definition main.cpp:795
static bool isTestExitCodeNormal(const int ec)
Definition main.cpp:86
static QStringList queryDangerousPermissions()
Definition main.cpp:410
static constexpr int EXIT_ERROR
Definition main.cpp:36
TestRunnerSystemSemaphore testRunnerLock
Definition main.cpp:984
void analyseLogcat(const QString &timeStamp, int *exitCode)
Definition main.cpp:832
static QString getGradleProjectProperty(const QString &androidBuildDir, const QString &property)
Definition main.cpp:363
static void printHelp()
Definition main.cpp:280
static bool execCommand(const QString &command, QByteArray *output=nullptr, bool verbose=true)
Definition main.cpp:154
static void waitForStarted()
Definition main.cpp:594
static bool execCommand(const QString &program, const QStringList &args, QByteArray *output=nullptr, bool verbose=false)
Definition main.cpp:91
static bool stopStdoutLogger()
Definition main.cpp:638
static bool uninstallTestPackage()
Definition main.cpp:949
static bool processAndroidManifest()
Definition main.cpp:388
static constexpr int EXIT_ANR
Definition main.cpp:39
static Options g_options
Definition main.cpp:70
static QStringList runningDevices()
Definition main.cpp:712
int main(int argc, char *argv[])
[ctor_close]
QString makeCommand
Definition main.cpp:55
QStringList amStarttestArgs
Definition main.cpp:62
std::optional< QProcess > stdoutLogger
Definition main.cpp:67
QHash< QString, QString > outFiles
Definition main.cpp:61
QString adbCommand
Definition main.cpp:52
bool helpRequested
Definition main.cpp:129
int timeoutSecs
Definition main.cpp:48
QStringList permissions
Definition main.cpp:58
bool showLogcatOutput
Definition main.cpp:66
QString bundletoolPath
Definition main.cpp:53
int resultsPullRetries
Definition main.cpp:49
QString stdoutFileName
Definition main.cpp:60
bool skipAddInstallRoot
Definition main.cpp:47
QList< QStringList > preTestRunAdbCommands
Definition main.cpp:65
QString package
Definition main.cpp:56
QString serial
Definition main.cpp:54
QString packagePath
Definition main.cpp:63
bool verbose
Definition main.cpp:130
QString ndkStackPath
Definition main.cpp:64
QStringList testArgsList
Definition main.cpp:59
QString manifestPath
Definition main.cpp:51
QString activity
Definition main.cpp:57
QString buildPath
Definition main.cpp:50
QString userId
Definition main.cpp:76
std::atomic< bool > isPackageInstalled
Definition main.cpp:78
int sdkVersion
Definition main.cpp:74
int pid
Definition main.cpp:75
std::atomic< bool > isTestRunnerInterrupted
Definition main.cpp:79
QSystemSemaphore semaphore
Definition main.cpp:980
std::atomic< bool > isAcquired
Definition main.cpp:979