testfolderman.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // those should be allowed
  52. // QString FolderMan::checkPathValidityForNewFolder(const QString& path, const QUrl &serverUrl, bool forNewDirectory)
  53. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free"), QString());
  54. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/free2/"), QString());
  55. // Not an existing directory -> Ok
  56. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu"), QString());
  57. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/free/bliblablu"), QString());
  58. // QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/bliblablu/some/more"), QString());
  59. // A file -> Error
  60. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/file.txt").isNull());
  61. // There are folders configured in those folders, url needs to be taken into account: -> ERROR
  62. QUrl url2(url);
  63. const QString user = account->credentials()->user();
  64. url2.setUserName(user);
  65. // The following both fail because they refer to the same account (user and url)
  66. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url2).isNull());
  67. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url2).isNull());
  68. // Now it will work because the account is different
  69. QUrl url3("http://anotherexample.org");
  70. url3.setUserName("dummy");
  71. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1", url3), QString());
  72. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/", url3), QString());
  73. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath).isNull());
  74. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder").isNull());
  75. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/f").isNull());
  76. #ifndef Q_OS_WIN // no links on windows, no permissions
  77. // make a bunch of links
  78. QVERIFY(QFile::link(dirPath + "/sub/free", dirPath + "/link1"));
  79. QVERIFY(QFile::link(dirPath + "/sub", dirPath + "/link2"));
  80. QVERIFY(QFile::link(dirPath + "/sub/ownCloud1", dirPath + "/link3"));
  81. QVERIFY(QFile::link(dirPath + "/sub/ownCloud1/folder", dirPath + "/link4"));
  82. // Ok
  83. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1").isNull());
  84. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free").isNull());
  85. // Not Ok
  86. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link2").isNull());
  87. // link 3 points to an existing sync folder. To make it fail, the account must be the same
  88. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3", url2).isNull());
  89. // while with a different account, this is fine
  90. QCOMPARE(folderman->checkPathValidityForNewFolder(dirPath + "/link3", url3), QString());
  91. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link4").isNull());
  92. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder").isNull());
  93. // test some non existing sub path (error)
  94. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/some/sub/path").isNull());
  95. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
  96. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/sub/ownCloud1/folder/g/h").isNull());
  97. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/link3/folder/neu_folder").isNull());
  98. // Subfolder of links
  99. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link1/subfolder").isNull());
  100. QVERIFY(folderman->checkPathValidityForNewFolder(dirPath + "/link2/free/subfolder").isNull());
  101. // Should not have the rights
  102. QVERIFY(!folderman->checkPathValidityForNewFolder("/").isNull());
  103. QVERIFY(!folderman->checkPathValidityForNewFolder("/usr/bin/somefolder").isNull());
  104. #endif
  105. #ifdef Q_OS_WIN // drive-letter tests
  106. if (!QFileInfo("v:/").exists()) {
  107. QVERIFY(!folderman->checkPathValidityForNewFolder("v:").isNull());
  108. QVERIFY(!folderman->checkPathValidityForNewFolder("v:/").isNull());
  109. QVERIFY(!folderman->checkPathValidityForNewFolder("v:/foo").isNull());
  110. }
  111. if (QFileInfo("c:/").isWritable()) {
  112. QVERIFY(folderman->checkPathValidityForNewFolder("c:").isNull());
  113. QVERIFY(folderman->checkPathValidityForNewFolder("c:/").isNull());
  114. QVERIFY(folderman->checkPathValidityForNewFolder("c:/foo").isNull());
  115. }
  116. #endif
  117. // Invalid paths
  118. QVERIFY(!folderman->checkPathValidityForNewFolder("").isNull());
  119. // REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
  120. QDir(dirPath + "/ownCloud2/").removeRecursively();
  121. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/blublu").isNull());
  122. QVERIFY(!folderman->checkPathValidityForNewFolder(dirPath + "/ownCloud2/sub/subsub/sub").isNull());
  123. }
  124. void testFindGoodPathForNewSyncFolder()
  125. {
  126. // SETUP
  127. QTemporaryDir dir;
  128. ConfigFile::setConfDir(dir.path()); // we don't want to pollute the user's config file
  129. QVERIFY(dir.isValid());
  130. QDir dir2(dir.path());
  131. QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
  132. QVERIFY(dir2.mkpath("ownCloud"));
  133. QVERIFY(dir2.mkpath("ownCloud2"));
  134. QVERIFY(dir2.mkpath("ownCloud2/foo"));
  135. QVERIFY(dir2.mkpath("sub/free"));
  136. QVERIFY(dir2.mkpath("free2/sub"));
  137. QString dirPath = dir2.canonicalPath();
  138. AccountPtr account = Account::create();
  139. QUrl url("http://example.de");
  140. auto *cred = new HttpCredentialsTest("testuser", "secret");
  141. account->setCredentials(cred);
  142. account->setUrl( url );
  143. url.setUserName(cred->user());
  144. AccountStatePtr newAccountState(new AccountState(account));
  145. FolderMan *folderman = FolderMan::instance();
  146. QCOMPARE(folderman, &_fm);
  147. QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/sub/ownCloud/")));
  148. QVERIFY(folderman->addFolder(newAccountState.data(), folderDefinition(dirPath + "/ownCloud2/")));
  149. // TEST
  150. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/oc", url),
  151. QString(dirPath + "/oc"));
  152. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url),
  153. QString(dirPath + "/ownCloud3"));
  154. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url),
  155. QString(dirPath + "/ownCloud22"));
  156. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/foo", url),
  157. QString(dirPath + "/ownCloud2/foo"));
  158. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2/bar", url),
  159. QString(dirPath + "/ownCloud2/bar"));
  160. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/sub", url),
  161. QString(dirPath + "/sub2"));
  162. // REMOVE ownCloud2 from the filesystem, but keep a folder sync'ed to it.
  163. // We should still not suggest this folder as a new folder.
  164. QDir(dirPath + "/ownCloud2/").removeRecursively();
  165. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud", url),
  166. QString(dirPath + "/ownCloud3"));
  167. QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url),
  168. QString(dirPath + "/ownCloud22"));
  169. }
  170. };
  171. QTEST_APPLESS_MAIN(TestFolderMan)
  172. #include "testfolderman.moc"