From 82a40f084b8ddb5f7f11d49f3296df9dba359301 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Mon, 4 Dec 2023 16:51:29 +0200 Subject: [PATCH] AndroidTestRunner: fix args with quotes and spaces First, replace double quote characters with 3 escaped double quotes to allow QProcess::splitCommand() to treat it as an actual quote character that's part of the tag. Then, escape single quote characters so they don't interfere with the shell command and also to be treated as part of the argument. Lastly, surround the args with escaped double quote so that args with spaces are also treated as one. Amends b044323c1656aeeec508afab8457755cc1e8c587. Example of this: tst_qkeyevent::modifiers("M","e","t","a") for double quotes tst_qunicodetools::wordBreakClass(two words) for spaces tst_QSpinBox::stepSelectAll("don't select all") for single quotes Task-number: QTQAINFRA-5703 Change-Id: Ie4317e4350bbac619bac41e41f42613f50cf1ad4 Reviewed-by: Axel Spoerl --- src/plugins/platforms/android/androidjnimain.cpp | 4 +--- src/tools/androidtestrunner/main.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/plugins/platforms/android/androidjnimain.cpp b/src/plugins/platforms/android/androidjnimain.cpp index fcb2cae8c9a..1a451b61e70 100644 --- a/src/plugins/platforms/android/androidjnimain.cpp +++ b/src/plugins/platforms/android/androidjnimain.cpp @@ -449,9 +449,7 @@ static jboolean startQtAndroidPlugin(JNIEnv *env, jobject /*object*/, jstring pa m_androidContentFileEngineHandler = new AndroidContentFileEngineHandler(); m_mainLibraryHnd = nullptr; - // QProcess::splitCommand() treats triple quotes as the quote character itself. - QString params = QJniObject(paramsString).toString().replace("\""_L1, "\"\"\""_L1); - const QStringList argsList = QProcess::splitCommand(params); + const QStringList argsList = QProcess::splitCommand(QJniObject(paramsString).toString()); for (const QString &arg : argsList) m_applicationParams.append(arg.toUtf8()); diff --git a/src/tools/androidtestrunner/main.cpp b/src/tools/androidtestrunner/main.cpp index 365ddab79ea..03a38d0ec7a 100644 --- a/src/tools/androidtestrunner/main.cpp +++ b/src/tools/androidtestrunner/main.cpp @@ -368,7 +368,14 @@ static bool parseTestArgs() if (match.hasMatch()) { logType = match.capturedTexts().at(1); } else { - unhandledArgs << " %1"_L1.arg(arg); + // Use triple literal quotes so that QProcess::splitCommand() in androidjnimain.cpp + // keeps quotes characters inside the string. + QString quotedArg = QString(arg).replace("\""_L1, "\\\"\\\"\\\""_L1); + // Escape single quotes so they don't interfere with the shell command, + // and so they get passed to the app as single quote inside the string. + quotedArg.replace("'"_L1, "\'"_L1); + // Add escaped double quote character so that args with spaces are treated as one. + unhandledArgs << " \\\"%1\\\""_L1.arg(quotedArg); } } } @@ -380,7 +387,7 @@ static bool parseTestArgs() testAppArgs += "-o output.%1,%1 "_L1.arg(format); testAppArgs += unhandledArgs.join(u' ').trimmed(); - testAppArgs = "'%1'"_L1.arg(testAppArgs); + testAppArgs = "\"%1\""_L1.arg(testAppArgs); const QString activityName = "%1/%2"_L1.arg(g_options.package).arg(g_options.activity); // Pass over any testlib env vars if set