testfolderman.cpp 8.2 KB

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