1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
/*
* 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.
*
*/
#include <qglobal.h>
#include <QTemporaryDir>
#include <QtTest>
#include "common/utility.h"
#include "folderman.h"
#include "account.h"
#include "accountstate.h"
#include "configfile.h"
#include "testutils/testutils.h"
using namespace OCC;
class TestFolderMan: public QObject
{
Q_OBJECT
private slots:
void testCheckPathValidityForNewFolder()
{
#ifdef Q_OS_WIN
Utility::NtfsPermissionLookupRAII ntfs_perm;
#endif
QTemporaryDir dir;
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"));
{
QFile f(dir.path() + "/sub/file.txt");
f.open(QFile::WriteOnly);
f.write("hello");
}
QString dirPath = dir2.canonicalPath();
AccountPtr account = TestUtils::createDummyAccount();
AccountStatePtr newAccountState(new AccountState(account));
FolderMan *folderman = TestUtils::folderMan();
QCOMPARE(folderman, FolderMan::instance());
QVERIFY(folderman->addFolder(newAccountState.data(), TestUtils::createDummyFolderDefinition(dirPath + "/sub/ownCloud1")));
QVERIFY(folderman->addFolder(newAccountState.data(), TestUtils::createDummyFolderDefinition(dirPath + "/ownCloud2")));
// those should be allowed
// QString FolderMan::checkPathValidityForNewFolder(const QString& path, const QUrl &serverUrl, bool forNewDirectory)
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free"), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/free2/"), QString());
// Not an existing directory -> Ok
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu"), QString());
QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free/bliblablu"), QString());
// QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu/some/more"), QString());
// A file -> Error
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/file.txt").isNull());
// The following both fail because they refer to the same account
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath).isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/f").isNull());
#ifndef Q_OS_WIN // no links on windows, no permissions
// make a bunch of links
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"));
// Ok
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free").isNull());
// Not Ok
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link2").isNull());
// link 3 points to an existing sync folder. To make it fail, the account must be the same
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link4").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder").isNull());
// test some non existing sub path (error)
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());
// Subfolder of links
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1/subfolder").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free/subfolder").isNull());
// Should not have the rights
QVERIFY(!folderman->checkPathValidityForNewFolder("/").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("/usr/bin/somefolder").isNull());
#endif
#ifdef Q_OS_WIN // drive-letter tests
if (!QFileInfo("v:/").exists()) {
QVERIFY(!folderman->checkPathValidityForNewFolder("v:").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:/").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder("v:/foo").isNull());
}
if (QFileInfo("c:/").isWritable()) {
QVERIFY(folderman->checkPathValidityForNewFolder("c:").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:/").isNull());
QVERIFY(folderman->checkPathValidityForNewFolder("c:/foo").isNull());
}
#endif
// Invalid paths
QVERIFY(!folderman->checkPathValidityForNewFolder("").isNull());
// REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
QDir(dirPath + "/ownCloud2/").removeRecursively();
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/sub/subsub/sub").isNull());
{ // check for rejection of a directory with `.sync_*.db`
QVERIFY(dir2.mkpath("db-check1"));
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/db-check1").isNull());
QFile f(dirPath + "/db-check1/.sync_something.db");
QVERIFY(f.open(QFile::Truncate | QFile::WriteOnly));
f.close();
QVERIFY(QFileInfo::exists(dirPath + "/db-check1/.sync_something.db"));
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/db-check1").isNull());
}
{ // check for rejection of a directory with `._sync_*.db`
QVERIFY(dir2.mkpath("db-check2"));
QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/db-check2").isNull());
QFile f(dirPath + "/db-check2/._sync_something.db");
QVERIFY(f.open(QFile::Truncate | QFile::WriteOnly));
f.close();
QVERIFY(QFileInfo::exists(dirPath + "/db-check2/._sync_something.db"));
QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/db-check2").isNull());
}
}
void testFindGoodPathForNewSyncFolder()
{
// SETUP
QTemporaryDir dir;
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 = TestUtils::createDummyAccount();
AccountStatePtr newAccountState(new AccountState(account));
FolderMan *folderman = TestUtils::folderMan();
QVERIFY(folderman->addFolder(newAccountState.data(), TestUtils::createDummyFolderDefinition(dirPath + "/sub/ownCloud/")));
QVERIFY(folderman->addFolder(newAccountState.data(), TestUtils::createDummyFolderDefinition(dirPath + "/ownCloud2/")));
// TEST
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/oc"),
QString(dirPath + "/oc"));
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud"),
QString(dirPath + "/ownCloud3"));
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2"),
QString(dirPath + "/ownCloud22"));
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/foo"),
QString(dirPath + "/ownCloud2/foo"));
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/bar"),
QString(dirPath + "/ownCloud2/bar"));
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/sub"),
QString(dirPath + "/sub2"));
// REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
// We should still not suggest this folder as a new folder.
QDir(dirPath + "/ownCloud2/").removeRecursively();
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud"),
QString(dirPath + "/ownCloud3"));
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2"),
QString(dirPath + "/ownCloud22"));
}
};
QTEST_GUILESS_MAIN(TestFolderMan)
#include "testfolderman.moc"
|