testfolderman.cpp 6.5 KB

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