testfolderman.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. *
  6. */
  7. #include <qglobal.h>
  8. #include <QTemporaryDir>
  9. #include <QtTest>
  10. #include "common/utility.h"
  11. #include "folderman.h"
  12. #include "account.h"
  13. #include "accountstate.h"
  14. #include "configfile.h"
  15. #include "testhelper.h"
  16. using namespace OCC;
  17. class TestFolderMan: public QObject
  18. {
  19. Q_OBJECT
  20. FolderMan _fm;
  21. private slots:
  22. void testCheckPathValidityForNewFolder()
  23. {
  24. #ifdef Q_OS_WIN
  25. Utility::NtfsPermissionLookupRAII ntfs_perm;
  26. #endif
  27. QTemporaryDir dir;
  28. ConfigFile::setConfDir(dir.path()); // we don't want to pollute the user's config file
  29. QVERIFY(dir.isValid());
  30. QDir dir2(dir.path());
  31. QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
  32. QVERIFY(dir2.mkpath("ownCloud2"));
  33. QVERIFY(dir2.mkpath("sub/free"));
  34. QVERIFY(dir2.mkpath("free2/sub"));
  35. {
  36. QFile f(dir.path() + "/sub/file.txt");
  37. f.open(QFile::WriteOnly);
  38. f.write("hello");
  39. }
  40. QString dirPath = dir2.canonicalPath();
  41. AccountPtr account = Account::create();
  42. QUrl url("http://example.de");
  43. auto *cred = new HttpCredentialsTest("testuser", "secret");
  44. account->setCredentials(cred);
  45. account->setUrl( url );
  46. AccountStatePtr newAccountState(new AccountState(account));
  47. FolderMan *folderman = FolderMan::instance();
  48. QCOMPARE(folderman, &_fm);
  49. QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/sub/ownCloud1")));
  50. QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/ownCloud2")));
  51. const auto folderList = folderman->map();
  52. for (const auto &folder : folderList) {
  53. QVERIFY(!folder->isSyncRunning());
  54. }
  55. // those should be allowed
  56. // QString FolderMan::checkPathValidityForNewFolder(const QString& path, const QUrl &serverUrl, bool forNewDirectory)
  57. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free"), QString());
  58. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/free2/"), QString());
  59. // Not an existing directory -> Ok
  60. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu"), QString());
  61. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free/bliblablu"), QString());
  62. // QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu/some/more"), QString());
  63. // A file -> Error
  64. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/file.txt").isNull());
  65. // There are folders configured in those folders, url needs to be taken into account: -> ERROR
  66. QUrl url2(url);
  67. const QString user = account->credentials()->user();
  68. url2.setUserName(user);
  69. // The following both fail because they refer to the same account (user and url)
  70. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url2).isNull());
  71. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url2).isNull());
  72. // Now it will work because the account is different
  73. QUrl url3("http://anotherexample.org");
  74. url3.setUserName("dummy");
  75. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url3), QString());
  76. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url3), QString());
  77. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath).isNull());
  78. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder").isNull());
  79. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/f").isNull());
  80. #ifndef Q_OS_WIN // no links on windows, no permissions
  81. // make a bunch of links
  82. QVERIFY(QFile::link(dirPath + "/sub/free", dirPath + "/link1"));
  83. QVERIFY(QFile::link(dirPath + "/sub", dirPath + "/link2"));
  84. QVERIFY(QFile::link(dirPath + "/sub/ownCloud1", dirPath + "/link3"));
  85. QVERIFY(QFile::link(dirPath + "/sub/ownCloud1/folder", dirPath + "/link4"));
  86. // Ok
  87. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1").isNull());
  88. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free").isNull());
  89. // Not Ok
  90. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link2").isNull());
  91. // link 3 points to an existing sync folder. To make it fail, the account must be the same
  92. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3", url2).isNull());
  93. // while with a different account, this is fine
  94. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/link3", url3), QString());
  95. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link4").isNull());
  96. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder").isNull());
  97. // test some non existing sub path (error)
  98. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/some/sub/path").isNull());
  99. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
  100. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/g/h").isNull());
  101. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder/neu_folder").isNull());
  102. // Subfolder of links
  103. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1/subfolder").isNull());
  104. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free/subfolder").isNull());
  105. // Should not have the rights
  106. QVERIFY(!folderman->checkPathValidityForNewFolder("/").isNull());
  107. QVERIFY(!folderman->checkPathValidityForNewFolder("/usr/bin/somefolder").isNull());
  108. #endif
  109. #ifdef Q_OS_WIN // drive-letter tests
  110. if (!QFileInfo("v:/").exists()) {
  111. QVERIFY(!folderman->checkPathValidityForNewFolder("v:").isNull());
  112. QVERIFY(!folderman->checkPathValidityForNewFolder("v:/").isNull());
  113. QVERIFY(!folderman->checkPathValidityForNewFolder("v:/foo").isNull());
  114. }
  115. if (QFileInfo("c:/").isWritable()) {
  116. QVERIFY(folderman->checkPathValidityForNewFolder("c:").isNull());
  117. QVERIFY(folderman->checkPathValidityForNewFolder("c:/").isNull());
  118. QVERIFY(folderman->checkPathValidityForNewFolder("c:/foo").isNull());
  119. }
  120. #endif
  121. // Invalid paths
  122. QVERIFY(!folderman->checkPathValidityForNewFolder("").isNull());
  123. // REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
  124. QDir(dirPath + "/ownCloud2/").removeRecursively();
  125. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
  126. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/sub/subsub/sub").isNull());
  127. }
  128. void testFindGoodPathForNewSyncFolder()
  129. {
  130. // SETUP
  131. QTemporaryDir dir;
  132. ConfigFile::setConfDir(dir.path()); // we don't want to pollute the user's config file
  133. QVERIFY(dir.isValid());
  134. QDir dir2(dir.path());
  135. QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
  136. QVERIFY(dir2.mkpath("ownCloud"));
  137. QVERIFY(dir2.mkpath("ownCloud2"));
  138. QVERIFY(dir2.mkpath("ownCloud2/foo"));
  139. QVERIFY(dir2.mkpath("sub/free"));
  140. QVERIFY(dir2.mkpath("free2/sub"));
  141. QString dirPath = dir2.canonicalPath();
  142. AccountPtr account = Account::create();
  143. QUrl url("http://example.de");
  144. auto *cred = new HttpCredentialsTest("testuser", "secret");
  145. account->setCredentials(cred);
  146. account->setUrl( url );
  147. url.setUserName(cred->user());
  148. AccountStatePtr newAccountState(new AccountState(account));
  149. FolderMan *folderman = FolderMan::instance();
  150. QCOMPARE(folderman, &_fm);
  151. QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/sub/ownCloud/")));
  152. QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/ownCloud2/")));
  153. // TEST
  154. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/oc", url),
  155. QString(dirPath + "/oc"));
  156. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url),
  157. QString(dirPath + "/ownCloud3"));
  158. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url),
  159. QString(dirPath + "/ownCloud22"));
  160. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/foo", url),
  161. QString(dirPath + "/ownCloud2/foo"));
  162. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/bar", url),
  163. QString(dirPath + "/ownCloud2/bar"));
  164. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/sub", url),
  165. QString(dirPath + "/sub2"));
  166. // REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
  167. // We should still not suggest this folder as a new folder.
  168. QDir(dirPath + "/ownCloud2/").removeRecursively();
  169. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url),
  170. QString(dirPath + "/ownCloud3"));
  171. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url),
  172. QString(dirPath + "/ownCloud22"));
  173. }
  174. };
  175. QTEST_APPLESS_MAIN(TestFolderMan)
  176. #include "testfolderman.moc"