diff --git a/src/util.cpp b/src/util.cpp index 6a12026..969e6df 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,78 +1,78 @@ /* SPDX-FileCopyrightText: 2014-2023 Anne Jan Brouwer SPDX-FileCopyrightText: 2023 g10 Code GmbH SPDX-FileContributor: Sune Stolborg Vuorela SPDX-License-Identifier: GPL-3.0-or-later */ #include "util.h" #include #include /** * @brief Util::findPasswordStore look for common .password-store folder * location. * @return */ QString Util::findPasswordStore() { QString path; if (qEnvironmentVariableIsSet("PASSWORD_STORE_DIR")) { path = qEnvironmentVariable("PASSWORD_STORE_DIR"); } else { #ifdef Q_OS_WIN - path = QDir::homePath() + QDir::separator() + QStringLiteral("password-store") + QDir::separator(); + path = QDir::homePath() + QLatin1Char('/') + QStringLiteral("password-store") + QLatin1Char('/'); #else - path = QDir::homePath() + QDir::separator() + QStringLiteral(".password-store") + QDir::separator(); + path = QDir::homePath() + QLatin1Char('/') + QStringLiteral(".password-store") + QLatin1Char('/'); #endif } return Util::normalizeFolderPath(path); } /** * @brief Util::normalizeFolderPath let's always end folders with a * QDir::separator() * @param path * @return */ QString Util::normalizeFolderPath(QString path) { - if (!path.endsWith(QLatin1Char('/')) && !path.endsWith(QDir::separator())) - path += QDir::separator(); + if (!path.endsWith(QLatin1Char('/'))) + path += QLatin1Char('/'); return path; } /** * @brief Util::copyDir * @param src * @param dest */ void Util::copyDir(const QString &src, const QString &dest) { QDir srcDir(src); if (!srcDir.exists()) { return; } srcDir.mkpath(dest); const auto srcDirs = srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); for (const QString &dir : srcDirs) { copyDir(src + QDir::separator() + dir, dest + QDir::separator() + dir); } const auto srcFiles = srcDir.entryList(QDir::Files); for (const QString &file : srcFiles) { QFile::copy(src + QDir::separator() + file, dest + QDir::separator() + file); } } const QRegularExpression &Util::endsWithGpg() { static const QRegularExpression expr{QStringLiteral("\\.gpg$")}; return expr; } const QRegularExpression &Util::protocolRegex() { static const QRegularExpression regex{QStringLiteral("((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://\\S+)")}; return regex; } diff --git a/tests/auto/util/tst_util.cpp b/tests/auto/util/tst_util.cpp index 69a2ec0..b5e0db1 100644 --- a/tests/auto/util/tst_util.cpp +++ b/tests/auto/util/tst_util.cpp @@ -1,121 +1,121 @@ /* SPDX-FileCopyrightText: 2014-2023 Anne Jan Brouwer SPDX-FileCopyrightText: 2018 Lukas Vogel SPDX-License-Identifier: GPL-3.0-or-later */ #include "filecontent.h" #include "util.h" #include #include #include /** * @brief The tst_util class is our first unit test */ class tst_util : public QObject { Q_OBJECT public: tst_util(); ~tst_util() override; public Q_SLOTS: void init(); void cleanup(); private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void normalizeFolderPath(); void fileContent(); }; bool operator==(const NamedValue &a, const NamedValue &b) { return a.name == b.name && a.value == b.value; } /** * @brief tst_util::tst_util basic constructor */ tst_util::tst_util() = default; /** * @brief tst_util::~tst_util basic destructor */ tst_util::~tst_util() = default; /** * @brief tst_util::init unit test init method */ void tst_util::init() { } /** * @brief tst_util::cleanup unit test cleanup method */ void tst_util::cleanup() { } /** * @brief tst_util::initTestCase test case init method */ void tst_util::initTestCase() { } /** * @brief tst_util::cleanupTestCase test case cleanup method */ void tst_util::cleanupTestCase() { } /** * @brief tst_util::normalizeFolderPath test to check correct working * of Util::normalizeFolderPath the paths should always end with a slash */ void tst_util::normalizeFolderPath() { - QCOMPARE(Util::normalizeFolderPath(QStringLiteral("test")), QDir::toNativeSeparators(QStringLiteral("test/"))); - QCOMPARE(Util::normalizeFolderPath(QStringLiteral("test/")), QDir::toNativeSeparators(QStringLiteral("test/"))); + QCOMPARE(Util::normalizeFolderPath(QStringLiteral("test")), QStringLiteral("test/")); + QCOMPARE(Util::normalizeFolderPath(QStringLiteral("test/")), QStringLiteral("test/")); } void tst_util::fileContent() { NamedValue key = {QStringLiteral("key"), QStringLiteral("val")}; NamedValue key2 = {QStringLiteral("key2"), QStringLiteral("val2")}; QString password = QStringLiteral("password"); FileContent fc = FileContent::parse(QStringLiteral("password\n"), {}, false); QCOMPARE(fc.getPassword(), password); QCOMPARE(fc.getNamedValues(), {}); QCOMPARE(fc.getRemainingData(), QString()); fc = FileContent::parse(QStringLiteral("password"), {}, false); QCOMPARE(fc.getPassword(), password); QCOMPARE(fc.getNamedValues(), {}); QCOMPARE(fc.getRemainingData(), QString()); fc = FileContent::parse(QStringLiteral("password\nfoobar\n"), {}, false); QCOMPARE(fc.getPassword(), password); QCOMPARE(fc.getNamedValues(), {}); QCOMPARE(fc.getRemainingData(), QStringLiteral("foobar\n")); fc = FileContent::parse(QStringLiteral("password\nkey: val\nkey2: val2"), {QStringLiteral("key2")}, false); QCOMPARE(fc.getPassword(), password); QCOMPARE(fc.getNamedValues(), {key2}); QCOMPARE(fc.getRemainingData(), QStringLiteral("key: val")); fc = FileContent::parse(QStringLiteral("password\nkey: val\nkey2: val2"), {QStringLiteral("key2")}, true); QCOMPARE(fc.getPassword(), password); QCOMPARE(fc.getNamedValues(), NamedValues({key, key2})); QCOMPARE(fc.getRemainingData(), QString()); } QTEST_MAIN(tst_util) #include "tst_util.moc"