Android: handle quoted args passed to an app

Currently, arguments passed to the app through applicationArguments
extra bundle treat every space as an argument separator. This then
doesn't handle the case where an argument is a space separated quoted
multi-word. This is more apparent when androidtestrunner is passing
test arguments to the app where an argument can be a test case with
a data tag that contains a space, which then is treated as two separate
tag names.

This change makes sure that androidtestrunner quotes each argument,
and the app doesn't split the arguments list by spaces, but rather
passed the argument string directly to c++ where
QProcess::splitCommand() is used to get the correct set of arguments
that will be passed to main().

Pick-to: 6.4 6.3 6.2
Task-number: QTBUG-104730
Change-Id: I45d8ca979d90f2a383c84623f0eb2eec29bba727
Reviewed-by: Dimitrios Apostolou <jimis@qt.io>
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
This commit is contained in:
Assam Boudjelthia 2022-08-10 00:02:59 +03:00
parent 55af91f822
commit edc024e826
3 changed files with 14 additions and 10 deletions

View File

@ -399,7 +399,7 @@ public abstract class QtLoader {
}
if (appParams != null)
loaderParams.putString(APPLICATION_PARAMETERS_KEY, appParams.replace(' ', '\t').trim());
loaderParams.putString(APPLICATION_PARAMETERS_KEY, appParams);
loadApplication(loaderParams);
return;

View File

@ -27,6 +27,7 @@
#include <QtCore/qbasicatomic.h>
#include <QtCore/qjnienvironment.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qprocess.h>
#include <QtCore/qresource.h>
#include <QtCore/qthread.h>
#include <QtGui/private/qguiapplication_p.h>
@ -445,11 +446,11 @@ static jboolean startQtAndroidPlugin(JNIEnv *env, jobject /*object*/, jstring pa
m_mainLibraryHnd = nullptr;
const char *nativeString = env->GetStringUTFChars(paramsString, 0);
QByteArray string = nativeString;
const QStringList argsList = QProcess::splitCommand(QString::fromUtf8(nativeString));
env->ReleaseStringUTFChars(paramsString, nativeString);
for (auto str : string.split('\t'))
m_applicationParams.append(str.split(' '));
for (const QString &arg : argsList)
m_applicationParams.append(arg.toUtf8());
// Go home
QDir::setCurrent(QDir::homePath());

View File

@ -313,7 +313,7 @@ static bool parseTestArgs()
QString file;
QString logType;
QString unhandledArgs;
QStringList unhandledArgs;
for (int i = 0; i < g_options.testArgsList.size(); ++i) {
const QString &arg = g_options.testArgsList[i].trimmed();
if (arg == QStringLiteral("--"))
@ -335,7 +335,7 @@ static bool parseTestArgs()
if (match.hasMatch()) {
logType = match.capturedTexts().at(1);
} else {
unhandledArgs += QStringLiteral(" %1").arg(arg);
unhandledArgs << QStringLiteral(" \\\"%1\\\"").arg(arg);
}
}
}
@ -345,10 +345,13 @@ static bool parseTestArgs()
for (const auto &format : g_options.outFiles.keys())
g_options.testArgs += QStringLiteral(" -o output.%1,%1").arg(format);
g_options.testArgs += unhandledArgs;
g_options.testArgs = QStringLiteral("shell am start -e applicationArguments \\\"%1\\\" -n %2/%3").arg(shellQuote(g_options.testArgs.trimmed()),
g_options.package,
g_options.activity);
g_options.testArgs += unhandledArgs.join(u' ');
g_options.testArgs = QStringLiteral("shell am start -e applicationArguments \"%1\" -n %2/%3")
.arg(shellQuote(g_options.testArgs.trimmed()))
.arg(g_options.package)
.arg(g_options.activity);
return true;
}