testutility.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <QtTest>
  7. #include <QTemporaryDir>
  8. #include "common/utility.h"
  9. #include "config.h"
  10. using namespace OCC::Utility;
  11. namespace OCC {
  12. OCSYNC_EXPORT extern bool fsCasePreserving_override;
  13. }
  14. class TestUtility : public QObject
  15. {
  16. Q_OBJECT
  17. private slots:
  18. void initTestCase()
  19. {
  20. QStandardPaths::setTestModeEnabled(true);
  21. }
  22. void testFormatFingerprint()
  23. {
  24. QVERIFY2(formatFingerprint("68ac906495480a3404beee4874ed853a037a7a8f")
  25. == "68:ac:90:64:95:48:0a:34:04:be:ee:48:74:ed:85:3a:03:7a:7a:8f",
  26. "Utility::formatFingerprint() is broken");
  27. }
  28. void testOctetsToString()
  29. {
  30. QLocale::setDefault(QLocale("en"));
  31. QCOMPARE(octetsToString(999) , QString("999 B"));
  32. QCOMPARE(octetsToString(1024) , QString("1 KB"));
  33. QCOMPARE(octetsToString(1364) , QString("1 KB"));
  34. QCOMPARE(octetsToString(9110) , QString("9 KB"));
  35. QCOMPARE(octetsToString(9910) , QString("10 KB"));
  36. QCOMPARE(octetsToString(10240) , QString("10 KB"));
  37. QCOMPARE(octetsToString(123456) , QString("121 KB"));
  38. QCOMPARE(octetsToString(1234567) , QString("1.2 MB"));
  39. QCOMPARE(octetsToString(12345678) , QString("12 MB"));
  40. QCOMPARE(octetsToString(123456789) , QString("118 MB"));
  41. QCOMPARE(octetsToString(1000LL*1000*1000 * 5) , QString("4.7 GB"));
  42. QCOMPARE(octetsToString(1), QString("1 B"));
  43. QCOMPARE(octetsToString(2), QString("2 B"));
  44. QCOMPARE(octetsToString(1024), QString("1 KB"));
  45. QCOMPARE(octetsToString(1024*1024), QString("1 MB"));
  46. QCOMPARE(octetsToString(1024LL*1024*1024), QString("1 GB"));
  47. }
  48. void testLaunchOnStartup()
  49. {
  50. qsrand(QDateTime::currentDateTime().toTime_t());
  51. QString postfix = QString::number(qrand());
  52. const QString appName = QString::fromLatin1("testLaunchOnStartup.%1").arg(postfix);
  53. const QString guiName = "LaunchOnStartup GUI Name";
  54. QVERIFY(hasLaunchOnStartup(appName) == false);
  55. setLaunchOnStartup(appName, guiName, true);
  56. QVERIFY(hasLaunchOnStartup(appName) == true);
  57. setLaunchOnStartup(appName, guiName, false);
  58. QVERIFY(hasLaunchOnStartup(appName) == false);
  59. }
  60. void testDurationToDescriptiveString()
  61. {
  62. QLocale::setDefault(QLocale("C"));
  63. //NOTE: in order for the plural to work we would need to load the english translation
  64. quint64 sec = 1000;
  65. quint64 hour = 3600 * sec;
  66. QDateTime current = QDateTime::currentDateTimeUtc();
  67. QCOMPARE(durationToDescriptiveString2(0), QString("0 second(s)") );
  68. QCOMPARE(durationToDescriptiveString2(5), QString("0 second(s)") );
  69. QCOMPARE(durationToDescriptiveString2(1000), QString("1 second(s)") );
  70. QCOMPARE(durationToDescriptiveString2(1005), QString("1 second(s)") );
  71. QCOMPARE(durationToDescriptiveString2(56123), QString("56 second(s)") );
  72. QCOMPARE(durationToDescriptiveString2(90*sec), QString("1 minute(s) 30 second(s)") );
  73. QCOMPARE(durationToDescriptiveString2(3*hour), QString("3 hour(s)") );
  74. QCOMPARE(durationToDescriptiveString2(3*hour + 20*sec), QString("3 hour(s)") );
  75. QCOMPARE(durationToDescriptiveString2(3*hour + 70*sec), QString("3 hour(s) 1 minute(s)") );
  76. QCOMPARE(durationToDescriptiveString2(3*hour + 100*sec), QString("3 hour(s) 2 minute(s)") );
  77. QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
  78. QString("4 year(s) 5 month(s)") );
  79. QCOMPARE(durationToDescriptiveString2(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
  80. QString("2 day(s) 23 hour(s)") );
  81. QCOMPARE(durationToDescriptiveString1(0), QString("0 second(s)") );
  82. QCOMPARE(durationToDescriptiveString1(5), QString("0 second(s)") );
  83. QCOMPARE(durationToDescriptiveString1(1000), QString("1 second(s)") );
  84. QCOMPARE(durationToDescriptiveString1(1005), QString("1 second(s)") );
  85. QCOMPARE(durationToDescriptiveString1(56123), QString("56 second(s)") );
  86. QCOMPARE(durationToDescriptiveString1(90*sec), QString("2 minute(s)") );
  87. QCOMPARE(durationToDescriptiveString1(3*hour), QString("3 hour(s)") );
  88. QCOMPARE(durationToDescriptiveString1(3*hour + 20*sec), QString("3 hour(s)") );
  89. QCOMPARE(durationToDescriptiveString1(3*hour + 70*sec), QString("3 hour(s)") );
  90. QCOMPARE(durationToDescriptiveString1(3*hour + 100*sec), QString("3 hour(s)") );
  91. QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addYears(4).addMonths(5).addDays(2).addSecs(23*60*60))),
  92. QString("4 year(s)") );
  93. QCOMPARE(durationToDescriptiveString1(current.msecsTo(current.addDays(2).addSecs(23*60*60))),
  94. QString("3 day(s)") );
  95. }
  96. void testVersionOfInstalledBinary()
  97. {
  98. if (isLinux()) {
  99. // pass the cmd client from our build dir
  100. // this is a bit inaccurate as it does not test the "real thing"
  101. // but cmd and gui have the same --version handler by now
  102. // and cmd works without X in CI
  103. QString ver = versionOfInstalledBinary(QStringLiteral(OWNCLOUD_BIN_PATH "/" APPLICATION_EXECUTABLE "cmd"));
  104. qDebug() << "Version of installed Nextcloud: " << ver;
  105. QVERIFY(!ver.isEmpty());
  106. QRegExp rx(APPLICATION_SHORTNAME R"( version \d+\.\d+\.\d+.*)");
  107. QVERIFY(rx.exactMatch(ver));
  108. } else {
  109. QVERIFY(versionOfInstalledBinary().isEmpty());
  110. }
  111. }
  112. void testTimeAgo()
  113. {
  114. // Both times in same timezone
  115. QDateTime d1 = QDateTime::fromString("2015-01-24T09:20:30+01:00", Qt::ISODate);
  116. QDateTime d2 = QDateTime::fromString("2015-01-23T09:20:30+01:00", Qt::ISODate);
  117. QString s = timeAgoInWords(d2, d1);
  118. QCOMPARE(s, QLatin1String("1 day ago"));
  119. // Different timezones
  120. QDateTime earlyTS = QDateTime::fromString("2015-01-24T09:20:30+01:00", Qt::ISODate);
  121. QDateTime laterTS = QDateTime::fromString("2015-01-24T09:20:30-01:00", Qt::ISODate);
  122. s = timeAgoInWords(earlyTS, laterTS);
  123. QCOMPARE(s, QLatin1String("2 hours ago"));
  124. // 'Now' in whatever timezone
  125. earlyTS = QDateTime::currentDateTime();
  126. laterTS = earlyTS;
  127. s = timeAgoInWords(earlyTS, laterTS );
  128. QCOMPARE(s, QLatin1String("now"));
  129. earlyTS = earlyTS.addSecs(-6);
  130. s = timeAgoInWords(earlyTS, laterTS );
  131. QCOMPARE(s, QLatin1String("Less than a minute ago"));
  132. }
  133. void testFsCasePreserving()
  134. {
  135. QVERIFY(isMac() || isWindows() ? fsCasePreserving() : ! fsCasePreserving());
  136. QScopedValueRollback<bool> scope(OCC::fsCasePreserving_override);
  137. OCC::fsCasePreserving_override = true;
  138. QVERIFY(fsCasePreserving());
  139. OCC::fsCasePreserving_override = false;
  140. QVERIFY(! fsCasePreserving());
  141. }
  142. void testFileNamesEqual()
  143. {
  144. QTemporaryDir dir;
  145. QVERIFY(dir.isValid());
  146. QDir dir2(dir.path());
  147. QVERIFY(dir2.mkpath("test"));
  148. if( !fsCasePreserving() ) {
  149. QVERIFY(dir2.mkpath("TEST"));
  150. }
  151. QVERIFY(dir2.mkpath("test/TESTI"));
  152. QVERIFY(dir2.mkpath("TESTI"));
  153. QString a = dir.path();
  154. QString b = dir.path();
  155. QVERIFY(fileNamesEqual(a, b));
  156. QVERIFY(fileNamesEqual(a+"/test", b+"/test")); // both exist
  157. QVERIFY(fileNamesEqual(a+"/test/TESTI", b+"/test/../test/TESTI")); // both exist
  158. QScopedValueRollback<bool> scope(OCC::fsCasePreserving_override, true);
  159. QVERIFY(fileNamesEqual(a+"/test", b+"/TEST")); // both exist
  160. QVERIFY(!fileNamesEqual(a+"/test", b+"/test/TESTI")); // both are different
  161. dir.remove();
  162. }
  163. void testSanitizeForFileName_data()
  164. {
  165. QTest::addColumn<QString>("input");
  166. QTest::addColumn<QString>("output");
  167. QTest::newRow("")
  168. << "foobar"
  169. << "foobar";
  170. QTest::newRow("")
  171. << "a/b?c<d>e\\f:g*h|i\"j"
  172. << "abcdefghij";
  173. QTest::newRow("")
  174. << QString::fromLatin1("a\x01 b\x1f c\x80 d\x9f")
  175. << "a b c d";
  176. }
  177. void testSanitizeForFileName()
  178. {
  179. QFETCH(QString, input);
  180. QFETCH(QString, output);
  181. QCOMPARE(sanitizeForFileName(input), output);
  182. }
  183. void testNormalizeEtag()
  184. {
  185. QByteArray str;
  186. #define CHECK_NORMALIZE_ETAG(TEST, EXPECT) \
  187. str = OCC::Utility::normalizeEtag(TEST); \
  188. QCOMPARE(str.constData(), EXPECT); \
  189. CHECK_NORMALIZE_ETAG("foo", "foo");
  190. CHECK_NORMALIZE_ETAG("\"foo\"", "foo");
  191. CHECK_NORMALIZE_ETAG("\"nar123\"", "nar123");
  192. CHECK_NORMALIZE_ETAG("", "");
  193. CHECK_NORMALIZE_ETAG("\"\"", "");
  194. /* Test with -gzip (all combinaison) */
  195. CHECK_NORMALIZE_ETAG("foo-gzip", "foo");
  196. CHECK_NORMALIZE_ETAG("\"foo\"-gzip", "foo");
  197. CHECK_NORMALIZE_ETAG("\"foo-gzip\"", "foo");
  198. }
  199. };
  200. QTEST_GUILESS_MAIN(TestUtility)
  201. #include "testutility.moc"