2015-07-13 15:35:19 +03:00
|
|
|
/*
|
|
|
|
* This software is in the public domain, furnished "as is", without technical
|
|
|
|
* support, and with no warranty, express or implied, as to its usefulness for
|
|
|
|
* any purpose.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-07-15 10:47:42 +03:00
|
|
|
#include <qglobal.h>
|
2015-07-13 15:35:19 +03:00
|
|
|
#include <QTemporaryDir>
|
|
|
|
#include <QtTest>
|
|
|
|
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2015-07-13 15:35:19 +03:00
|
|
|
#include "folderman.h"
|
2016-03-16 21:07:40 +03:00
|
|
|
#include "account.h"
|
2015-07-13 15:35:19 +03:00
|
|
|
#include "accountstate.h"
|
2016-10-11 12:55:20 +03:00
|
|
|
#include "configfile.h"
|
2016-10-07 17:24:09 +03:00
|
|
|
#include "creds/httpcredentials.h"
|
|
|
|
|
2015-07-13 15:35:19 +03:00
|
|
|
using namespace OCC;
|
|
|
|
|
2016-10-07 17:24:09 +03:00
|
|
|
class HttpCredentialsTest : public HttpCredentials {
|
|
|
|
public:
|
|
|
|
HttpCredentialsTest(const QString& user, const QString& password)
|
2017-01-03 12:52:39 +03:00
|
|
|
: HttpCredentials(user, password)
|
2016-10-07 17:24:09 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
void askFromUser() Q_DECL_OVERRIDE {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2015-07-13 15:35:19 +03:00
|
|
|
|
|
|
|
static FolderDefinition folderDefinition(const QString &path) {
|
|
|
|
FolderDefinition d;
|
|
|
|
d.localPath = path;
|
|
|
|
d.targetPath = path;
|
|
|
|
d.alias = path;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class TestFolderMan: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
FolderMan _fm;
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void testCheckPathValidityForNewFolder()
|
|
|
|
{
|
|
|
|
QTemporaryDir dir;
|
2016-10-11 12:55:20 +03:00
|
|
|
ConfigFile::setConfDir(dir.path()); // we don't want to pollute the user's config file
|
2015-07-13 15:35:19 +03:00
|
|
|
QVERIFY(dir.isValid());
|
|
|
|
QDir dir2(dir.path());
|
|
|
|
QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
|
|
|
|
QVERIFY(dir2.mkpath("ownCloud2"));
|
|
|
|
QVERIFY(dir2.mkpath("sub/free"));
|
|
|
|
QVERIFY(dir2.mkpath("free2/sub"));
|
2015-07-28 13:14:52 +03:00
|
|
|
{
|
|
|
|
QFile f(dir.path() + "/sub/file.txt");
|
|
|
|
f.open(QFile::WriteOnly);
|
|
|
|
f.write("hello");
|
|
|
|
}
|
2017-02-09 20:17:01 +03:00
|
|
|
QString dirPath = dir2.canonicalPath();
|
2015-07-13 15:35:19 +03:00
|
|
|
|
2016-10-07 17:24:09 +03:00
|
|
|
AccountPtr account = Account::create();
|
|
|
|
QUrl url("http://example.de");
|
|
|
|
HttpCredentialsTest *cred = new HttpCredentialsTest("testuser", "secret");
|
|
|
|
account->setCredentials(cred);
|
|
|
|
account->setUrl( url );
|
|
|
|
|
|
|
|
AccountStatePtr newAccountState(new AccountState(account));
|
2015-07-13 15:35:19 +03:00
|
|
|
FolderMan *folderman = FolderMan::instance();
|
|
|
|
QCOMPARE(folderman, &_fm);
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/sub/ownCloud1")));
|
|
|
|
QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/ownCloud2")));
|
2015-07-13 15:35:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
// those should be allowed
|
2016-10-07 17:24:09 +03:00
|
|
|
// QString FolderMan::checkPathValidityForNewFolder(const QString& path, const QUrl &serverUrl, bool forNewDirectory)
|
|
|
|
|
2017-02-09 20:17:01 +03:00
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free"), QString());
|
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/free2/"), QString());
|
2015-07-28 13:14:52 +03:00
|
|
|
// Not an existing directory -> Ok
|
2017-02-09 20:17:01 +03:00
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu"), QString());
|
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free/bliblablu"), QString());
|
|
|
|
// QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu/some/more"), QString());
|
2015-07-13 15:35:19 +03:00
|
|
|
|
2015-07-28 13:14:52 +03:00
|
|
|
// A file -> Error
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/file.txt").isNull());
|
2015-07-13 15:35:19 +03:00
|
|
|
|
2016-10-07 17:24:09 +03:00
|
|
|
// There are folders configured in those folders, url needs to be taken into account: -> ERROR
|
|
|
|
QUrl url2(url);
|
|
|
|
const QString user = account->credentials()->user();
|
|
|
|
url2.setUserName(user);
|
|
|
|
|
|
|
|
// The following both fail because they refer to the same account (user and url)
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url2).isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url2).isNull());
|
2016-10-07 17:24:09 +03:00
|
|
|
|
|
|
|
// Now it will work because the account is different
|
|
|
|
QUrl url3("http://anotherexample.org");
|
|
|
|
url3.setUserName("dummy");
|
2017-02-09 20:17:01 +03:00
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url3), QString());
|
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url3), QString());
|
2016-10-07 17:24:09 +03:00
|
|
|
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath).isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder").isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/f").isNull());
|
2015-07-13 15:35:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
// make a bunch of links
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(QFile::link(dirPath + "/sub/free", dirPath + "/link1"));
|
|
|
|
QVERIFY(QFile::link(dirPath + "/sub", dirPath + "/link2"));
|
|
|
|
QVERIFY(QFile::link(dirPath + "/sub/ownCloud1", dirPath + "/link3"));
|
|
|
|
QVERIFY(QFile::link(dirPath + "/sub/ownCloud1/folder", dirPath + "/link4"));
|
2015-07-13 15:35:19 +03:00
|
|
|
|
|
|
|
// Ok
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1").isNull());
|
|
|
|
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free").isNull());
|
2015-07-13 15:35:19 +03:00
|
|
|
|
|
|
|
// Not Ok
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link2").isNull());
|
2016-10-07 17:24:09 +03:00
|
|
|
|
|
|
|
// link 3 points to an existing sync folder. To make it fail, the account must be the same
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3", url2).isNull());
|
2016-10-07 17:24:09 +03:00
|
|
|
// while with a different account, this is fine
|
2017-02-09 20:17:01 +03:00
|
|
|
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/link3", url3), QString());
|
2016-10-07 17:24:09 +03:00
|
|
|
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link4").isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder").isNull());
|
2015-07-28 13:14:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
// test some non existing sub path (error)
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/some/sub/path").isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/g/h").isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder/neu_folder").isNull());
|
2015-07-28 13:14:52 +03:00
|
|
|
|
|
|
|
// Subfolder of links
|
2017-02-09 20:17:01 +03:00
|
|
|
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1/subfolder").isNull());
|
|
|
|
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free/subfolder").isNull());
|
2015-07-28 13:14:52 +03:00
|
|
|
|
|
|
|
// Invalid paths
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder("").isNull());
|
|
|
|
|
|
|
|
// Should not have the rights
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder("/").isNull());
|
|
|
|
QVERIFY(!folderman->checkPathValidityForNewFolder("/usr/bin/somefolder").isNull());
|
2017-03-20 16:28:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testFindGoodPathForNewSyncFolder()
|
|
|
|
{
|
|
|
|
// SETUP
|
|
|
|
|
|
|
|
QTemporaryDir dir;
|
|
|
|
ConfigFile::setConfDir(dir.path()); // we don't want to pollute the user's config file
|
|
|
|
QVERIFY(dir.isValid());
|
|
|
|
QDir dir2(dir.path());
|
|
|
|
QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
|
|
|
|
QVERIFY(dir2.mkpath("ownCloud"));
|
|
|
|
QVERIFY(dir2.mkpath("ownCloud2"));
|
|
|
|
QVERIFY(dir2.mkpath("ownCloud2/foo"));
|
|
|
|
QVERIFY(dir2.mkpath("sub/free"));
|
|
|
|
QVERIFY(dir2.mkpath("free2/sub"));
|
|
|
|
QString dirPath = dir2.canonicalPath();
|
|
|
|
|
|
|
|
AccountPtr account = Account::create();
|
|
|
|
QUrl url("http://example.de");
|
|
|
|
HttpCredentialsTest *cred = new HttpCredentialsTest("testuser", "secret");
|
|
|
|
account->setCredentials(cred);
|
|
|
|
account->setUrl( url );
|
|
|
|
|
|
|
|
AccountStatePtr newAccountState(new AccountState(account));
|
|
|
|
FolderMan *folderman = FolderMan::instance();
|
|
|
|
QCOMPARE(folderman, &_fm);
|
|
|
|
QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/sub/ownCloud/")));
|
|
|
|
QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/ownCloud2/")));
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
|
|
|
|
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/oc", url),
|
|
|
|
QString(dirPath + "/oc"));
|
|
|
|
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url),
|
|
|
|
QString(dirPath + "/ownCloud3"));
|
|
|
|
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url),
|
|
|
|
QString(dirPath + "/ownCloud22"));
|
|
|
|
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/foo", url),
|
|
|
|
QString(dirPath + "/ownCloud2/foo"));
|
|
|
|
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/bar", url),
|
|
|
|
QString(dirPath + "/ownCloud2/bar"));
|
|
|
|
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/sub", url),
|
|
|
|
QString(dirPath + "/sub2"));
|
2015-07-13 15:35:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-11 02:17:16 +03:00
|
|
|
QTEST_APPLESS_MAIN(TestFolderMan)
|
2016-03-30 18:58:15 +03:00
|
|
|
#include "testfolderman.moc"
|