diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -34,6 +34,7 @@ deletejobtest.cpp urlutiltest.cpp batchrenamejobtest.cpp + ksambasharetest.cpp NAME_PREFIX "kiocore-" LINK_LIBRARIES KF5::KIOCore KF5::I18n Qt5::Test Qt5::Network ) diff --git a/autotests/ksambasharetest.h b/autotests/ksambasharetest.h new file mode 100644 --- /dev/null +++ b/autotests/ksambasharetest.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018 Kai Uwe Broulik + * + * This program 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program 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 this program. If not, see . + */ + +#ifndef KSAMBASHARETEST_H +#define KSAMBASHARETEST_H + +#include + +class KSambaShareTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void testAcl(); + void testAcl_data(); + void testOwnAcl(); +}; + +#endif // KSAMBASHARETEST_H + diff --git a/autotests/ksambasharetest.cpp b/autotests/ksambasharetest.cpp new file mode 100644 --- /dev/null +++ b/autotests/ksambasharetest.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018 Kai Uwe Broulik + * + * This program 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program 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 this program. If not, see . + */ + +#include "ksambasharetest.h" + +#include +#include + +#include + +QTEST_MAIN(KSambaShareTest) + +Q_DECLARE_METATYPE(KSambaShareData::UserShareError) + +void KSambaShareTest::initTestCase() +{ + qRegisterMetaType(); +} + +void KSambaShareTest::testAcl() +{ + QFETCH(QString, acl); + QFETCH(KSambaShareData::UserShareError, result); + + KSambaShareData data; + + QCOMPARE(data.setAcl(acl), result); +} + +void KSambaShareTest::testAcl_data() +{ + QTest::addColumn("acl"); + QTest::addColumn("result"); + + QTest::newRow("one entry") << QStringLiteral("Everyone:r") << KSambaShareData::UserShareAclOk; + QTest::newRow("one entry, trailing comma") << QStringLiteral("Everyone:r,") << KSambaShareData::UserShareAclOk; + + QTest::newRow("one entry with hostname") << QStringLiteral("Host\\Someone:r") << KSambaShareData::UserShareAclOk; + + QTest::newRow("space in hostname") << QStringLiteral("Everyone:r,Unix User\\Someone:f,") << KSambaShareData::UserShareAclOk; + + QTest::newRow("garbage") << QStringLiteral("Garbage") << KSambaShareData::UserShareAclInvalid; +} + +void KSambaShareTest::testOwnAcl() +{ + const auto shares = KSambaShare::instance()->shareNames(); + + for (const QString &share : shares) { + KSambaShareData shareData = KSambaShare::instance()->getShareByName(share); + + // KSambaShare reads acl from net usershare info's "usershare_acl" field with no validation + QCOMPARE(shareData.setAcl(shareData.acl()), KSambaShareData::UserShareAclOk); + } +} diff --git a/src/core/ksambashare.cpp b/src/core/ksambashare.cpp --- a/src/core/ksambashare.cpp +++ b/src/core/ksambashare.cpp @@ -285,7 +285,7 @@ KSambaShareData::UserShareError KSambaSharePrivate::isAclValid(const QString &acl) const { - const QRegExp aclRx(QStringLiteral("(?:(?:(\\w+\\s*)\\\\|)(\\w+\\s*):([fFrRd]{1})(?:,|))*")); + const QRegExp aclRx(QStringLiteral("(?:(?:(\\w(\\w|\\s)*)\\\\|)(\\w+\\s*):([fFrRd]{1})(?:,|))*")); // TODO: check if user is a valid smb user return aclRx.exactMatch(acl) ? KSambaShareData::UserShareAclOk : KSambaShareData::UserShareAclInvalid;