testfolderman.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #pragma once
  8. #include <qglobal.h>
  9. #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
  10. #include <QTemporaryDir>
  11. #endif
  12. #include <QtTest>
  13. #include "utility.h"
  14. #include "folderman.h"
  15. #include "accountstate.h"
  16. using namespace OCC;
  17. static FolderDefinition folderDefinition(const QString &path) {
  18. FolderDefinition d;
  19. d.localPath = path;
  20. d.targetPath = path;
  21. d.alias = path;
  22. return d;
  23. }
  24. class TestFolderMan: public QObject
  25. {
  26. Q_OBJECT
  27. FolderMan _fm;
  28. private slots:
  29. void testCheckPathValidityForNewFolder()
  30. {
  31. #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
  32. QTemporaryDir dir;
  33. QVERIFY(dir.isValid());
  34. QDir dir2(dir.path());
  35. QVERIFY(dir2.mkpath("sub/ownCloud1/folder/f"));
  36. QVERIFY(dir2.mkpath("ownCloud2"));
  37. QVERIFY(dir2.mkpath("sub/free"));
  38. QVERIFY(dir2.mkpath("free2/sub"));
  39. {
  40. QFile f(dir.path() + "/sub/file.txt");
  41. f.open(QFile::WriteOnly);
  42. f.write("hello");
  43. }
  44. FolderMan *folderman = FolderMan::instance();
  45. QCOMPARE(folderman, &_fm);
  46. QVERIFY(folderman->addFolder(0, folderDefinition(dir.path() + "/sub/ownCloud1")));
  47. QVERIFY(folderman->addFolder(0, folderDefinition(dir.path() + "/ownCloud2")));
  48. // those should be allowed
  49. QCOMPARE(folderman->checkPathValidityForNewFolder(dir.path() + "/sub/free"), QString());
  50. QCOMPARE(folderman->checkPathValidityForNewFolder(dir.path() + "/free2/"), QString());
  51. // Not an existing directory -> Ok
  52. QCOMPARE(folderman->checkPathValidityForNewFolder(dir.path() + "/sub/bliblablu"), QString());
  53. QCOMPARE(folderman->checkPathValidityForNewFolder(dir.path() + "/sub/free/bliblablu"), QString());
  54. QCOMPARE(folderman->checkPathValidityForNewFolder(dir.path() + "/sub/bliblablu/some/more"), QString());
  55. // A file -> Error
  56. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/file.txt").isNull());
  57. // There are folders configured in those folders: -> ERROR
  58. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1").isNull());
  59. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/ownCloud2/").isNull());
  60. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub").isNull());
  61. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/").isNull());
  62. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path()).isNull());
  63. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1/folder").isNull());
  64. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1/folder/f").isNull());
  65. // make a bunch of links
  66. QVERIFY(QFile::link(dir.path() + "/sub/free", dir.path() + "/link1"));
  67. QVERIFY(QFile::link(dir.path() + "/sub", dir.path() + "/link2"));
  68. QVERIFY(QFile::link(dir.path() + "/sub/ownCloud1", dir.path() + "/link3"));
  69. QVERIFY(QFile::link(dir.path() + "/sub/ownCloud1/folder", dir.path() + "/link4"));
  70. // Ok
  71. QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/link1").isNull());
  72. QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/link2/free").isNull());
  73. // Not Ok
  74. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link2").isNull());
  75. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link3").isNull());
  76. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link4").isNull());
  77. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link3/folder").isNull());
  78. // test some non existing sub path (error)
  79. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1/some/sub/path").isNull());
  80. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/ownCloud2/blublu").isNull());
  81. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/sub/ownCloud1/folder/g/h").isNull());
  82. QVERIFY(!folderman->checkPathValidityForNewFolder(dir.path() + "/link3/folder/neu_folder").isNull());
  83. // Subfolder of links
  84. QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/link1/subfolder").isNull());
  85. QVERIFY(folderman->checkPathValidityForNewFolder(dir.path() + "/link2/free/subfolder").isNull());
  86. // Invalid paths
  87. QVERIFY(!folderman->checkPathValidityForNewFolder("").isNull());
  88. // Should not have the rights
  89. QVERIFY(!folderman->checkPathValidityForNewFolder("/").isNull());
  90. QVERIFY(!folderman->checkPathValidityForNewFolder("/usr/bin/somefolder").isNull());
  91. #else
  92. QSKIP("Test not supported with Qt4", SkipSingle);
  93. #endif
  94. }
  95. };