testfolderman.cpp 9.3 KB

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