From cfca42906779f4dbde1d42687068d3daddc2e55a Mon Sep 17 00:00:00 2001 From: Xavier Michelon Date: Mon, 17 Jul 2023 12:45:06 +0200 Subject: [PATCH] fix(GODT-2807): fix issue where sessionID would not be removed from command-line on restart by bridge-gui. --- .../bridge-gui/bridge-gui/AppController.cpp | 27 ++++++---- .../bridge-gui/bridgepp/CMakeLists.txt | 2 + .../bridge-gui/bridgepp/Test/TestCLI.cpp | 50 +++++++++++++++++++ .../bridgepp/bridgepp/CLI/CLIUtils.cpp | 45 +++++++++++++++++ .../bridgepp/bridgepp/CLI/CLIUtils.h | 32 ++++++++++++ 5 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp create mode 100644 internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp create mode 100644 internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h diff --git a/internal/frontend/bridge-gui/bridge-gui/AppController.cpp b/internal/frontend/bridge-gui/bridge-gui/AppController.cpp index e7ce8189..0f1a639b 100644 --- a/internal/frontend/bridge-gui/bridge-gui/AppController.cpp +++ b/internal/frontend/bridge-gui/bridge-gui/AppController.cpp @@ -20,6 +20,7 @@ #include "QMLBackend.h" #include "SentryUtils.h" #include "Settings.h" +#include #include #include #include @@ -101,19 +102,23 @@ void AppController::onFatalError(Exception const &exception) { qApp->exit(EXIT_FAILURE); } - +//**************************************************************************************************************************************************** +/// \param[in] isCrashing Is the restart triggered by a crash. +//**************************************************************************************************************************************************** void AppController::restart(bool isCrashing) { - if (!launcher_.isEmpty()) { - QProcess p; - log_->info(QString("Restarting - App : %1 - Args : %2").arg(launcher_, launcherArgs_.join(" "))); - QStringList args = launcherArgs_; - if (isCrashing) { - args.append(noWindowFlag); - } - - p.startDetached(launcher_, args); - p.waitForStarted(); + if (launcher_.isEmpty()) { + return; } + + QProcess p; + QStringList args = stripStringParameterFromCommandLine("--session-id", launcherArgs_); + if (isCrashing) { + args.append(noWindowFlag); + } + + log_->info(QString("Restarting - App : %1 - Args : %2").arg(launcher_, args.join(" "))); + p.startDetached(launcher_, args); + p.waitForStarted(); } diff --git a/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt b/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt index 1d26b26d..87530c4c 100644 --- a/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt +++ b/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt @@ -136,6 +136,7 @@ add_custom_command( add_library(bridgepp bridgepp/BridgeUtils.cpp bridgepp/BridgeUtils.h + bridgepp/CLI/CLIUtils.cpp bridgepp/CLI/CLIUtils.h bridgepp/Exception/Exception.h bridgepp/Exception/Exception.cpp bridgepp/GRPC/GRPCClient.cpp bridgepp/GRPC/GRPCClient.h bridgepp/GRPC/GRPCErrors.h bridgepp/GRPC/GRPCErrors.cpp @@ -188,6 +189,7 @@ enable_testing() #***************************************************************************************************************************************************** add_executable(bridgepp-test EXCLUDE_FROM_ALL Test/TestBridgeUtils.cpp + Test/TestCLI.cpp Test/TestException.cpp Test/TestSessionID.cpp Test/TestWorker.cpp Test/TestWorker.h diff --git a/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp b/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp new file mode 100644 index 00000000..5bd24c54 --- /dev/null +++ b/internal/frontend/bridge-gui/bridgepp/Test/TestCLI.cpp @@ -0,0 +1,50 @@ +// Copyright (c) 2023 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + + +#include +#include + + +using namespace bridgepp; + + + +//**************************************************************************************************************************************************** +// +//**************************************************************************************************************************************************** +TEST(CLI, stripStringParameterFromCommandLine) { + struct Test { + QStringList input; + QStringList expectedOutput; + }; + QList const tests = { + {{}, {}}, + {{ "--a", "-b", "--C" }, { "--a", "-b", "--C" } }, + {{ "--string", "value" }, {} }, + {{ "--string" }, {} }, + {{ "--string", "value", "-b", "--C" }, { "-b", "--C" } }, + {{ "--string", "value", "-b", "--string", "value", "--C" }, { "-b", "--C" } }, + {{ "--string", "value", "-b", "--string", "value", "--C" }, { "-b", "--C" } }, + {{ "--string", "value", "-b", "--string"}, { "-b" } }, + {{ "--string", "--string", "value", "-b", "--string"}, { "value", "-b" } }, + }; + + for (Test const& test: tests) { + EXPECT_EQ(stripStringParameterFromCommandLine("--string", test.input), test.expectedOutput); + } +} diff --git a/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp new file mode 100644 index 00000000..c118c660 --- /dev/null +++ b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2023 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + +#include "CLIUtils.h" + + +namespace bridgepp { + + +//**************************************************************************************************************************************************** +/// \param[in] paramName The parameter name, including prefix dashes (e.g. '--string'). +/// \param[in] commandLineParams The command-line parameters. +/// \return The command-line parameters where all occurrences of paramName and it associated value have been removed. Comparison is case-sensitive. +//**************************************************************************************************************************************************** +QStringList stripStringParameterFromCommandLine(QString const ¶mName, QStringList const &commandLineParams) { + qint32 i = 0; + QStringList result; + while (i < commandLineParams.count()) { + if (paramName == commandLineParams[i]) { + i += 2; + continue; + } + result.append(commandLineParams[i]); + i++; + } + + return result; +} + + +} // namespace bridgepp diff --git a/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h new file mode 100644 index 00000000..c05ccd8d --- /dev/null +++ b/internal/frontend/bridge-gui/bridgepp/bridgepp/CLI/CLIUtils.h @@ -0,0 +1,32 @@ +// Copyright (c) 2023 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + + +#ifndef BRIDGEPP_CLI_UTILS_H +#define BRIDGEPP_CLI_UTILS_H + + +namespace bridgepp { + + +QStringList stripStringParameterFromCommandLine(QString const ¶mName, QStringList const &commandLineParams); ///< Remove a string parameter from a list of command-line parameters. + + +} + + +#endif // BRIDGEPP_CLI_UTILS_H