testlongpath.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * libcsync -- a library to sync a directory with another
  3. *
  4. * Copyright (c) 2013 by Klaas Freitag <freitag@owncloud.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "common/filesystembase.h"
  21. #include "csync/csync.h"
  22. #include "csync/vio/csync_vio_local.h"
  23. #include <QTemporaryFile>
  24. #include <QTest>
  25. class TestLongWindowsPath : public QObject
  26. {
  27. Q_OBJECT
  28. private Q_SLOTS:
  29. #ifdef Q_OS_WIN
  30. void check_long_win_path()
  31. {
  32. {
  33. const auto path = QStringLiteral("C://DATA/FILES/MUSIC/MY_MUSIC.mp3"); // check a short path
  34. const auto exp_path = QStringLiteral("\\\\?\\C:\\\\DATA\\FILES\\MUSIC\\MY_MUSIC.mp3");
  35. QString new_short = OCC::FileSystem::pathtoUNC(path);
  36. QCOMPARE(new_short, exp_path);
  37. }
  38. {
  39. const auto path = QStringLiteral("\\\\foo\\bar/MY_MUSIC.mp3");
  40. const auto exp_path = QStringLiteral("\\\\foo\\bar\\MY_MUSIC.mp3");
  41. QString new_short = OCC::FileSystem::pathtoUNC(path);
  42. QCOMPARE(new_short, exp_path);
  43. }
  44. {
  45. const auto path = QStringLiteral("//foo\\bar/MY_MUSIC.mp3");
  46. const auto exp_path = QStringLiteral("\\\\foo\\bar\\MY_MUSIC.mp3");
  47. QString new_short = OCC::FileSystem::pathtoUNC(path);
  48. QCOMPARE(new_short, exp_path);
  49. }
  50. {
  51. const auto path = QStringLiteral("\\foo\\bar");
  52. const auto exp_path = QStringLiteral("\\\\?\\foo\\bar");
  53. QString new_short = OCC::FileSystem::pathtoUNC(path);
  54. QCOMPARE(new_short, exp_path);
  55. }
  56. {
  57. const auto path = QStringLiteral("/foo/bar");
  58. const auto exp_path = QStringLiteral("\\\\?\\foo\\bar");
  59. QString new_short = OCC::FileSystem::pathtoUNC(path);
  60. QCOMPARE(new_short, exp_path);
  61. }
  62. const auto longPath = QStringLiteral("D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
  63. "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
  64. "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
  65. "olonglonglonglong/file.txt");
  66. const auto longPathConv = QStringLiteral("\\\\?\\D:\\\\alonglonglonglong\\blonglonglonglong\\clonglonglonglong\\dlonglonglonglong\\"
  67. "elonglonglonglong\\flonglonglonglong\\glonglonglonglong\\hlonglonglonglong\\ilonglonglonglong\\"
  68. "jlonglonglonglong\\klonglonglonglong\\llonglonglonglong\\mlonglonglonglong\\nlonglonglonglong\\"
  69. "olonglonglonglong\\file.txt");
  70. QString new_long = OCC::FileSystem::pathtoUNC(longPath);
  71. // printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved);
  72. QCOMPARE(new_long, longPathConv);
  73. // printf( "YYYYYYYYYYYY %ld\n", strlen(new_long));
  74. QCOMPARE(new_long.length(), 286);
  75. }
  76. #endif
  77. void testLongPathStat_data()
  78. {
  79. QTest::addColumn<QString>("name");
  80. QTest::newRow("long") << QStringLiteral("/alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
  81. "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
  82. "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
  83. "olonglonglonglong/file.txt");
  84. QTest::newRow("long emoji") << QString::fromUtf8("/alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
  85. "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
  86. "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
  87. "olonglonglonglong/file🐷.txt");
  88. QTest::newRow("long russian") << QString::fromUtf8("/alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
  89. "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
  90. "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
  91. "olonglonglonglong/собственное.txt");
  92. QTest::newRow("long arabic") << QString::fromUtf8("/alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
  93. "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
  94. "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
  95. "olonglonglonglong/السحاب.txt");
  96. QTest::newRow("long chinese") << QString::fromUtf8("/alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
  97. "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
  98. "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
  99. "olonglonglonglong/自己的云.txt");
  100. }
  101. void testLongPathStat()
  102. {
  103. QTemporaryDir tmp;
  104. QFETCH(QString, name);
  105. const QFileInfo longPath(tmp.path() + name);
  106. const auto data = QByteArrayLiteral("hello");
  107. qDebug() << longPath;
  108. QVERIFY(longPath.dir().mkpath("."));
  109. QFile file(longPath.filePath());
  110. QVERIFY(file.open(QFile::WriteOnly));
  111. QVERIFY(file.write(data.constData()) == data.size());
  112. file.close();
  113. csync_file_stat_t buf;
  114. QVERIFY(csync_vio_local_stat(longPath.filePath(), &buf) != -1);
  115. QVERIFY(buf.size == data.size());
  116. QVERIFY(buf.size == longPath.size());
  117. QVERIFY(tmp.remove());
  118. }
  119. };
  120. QTEST_GUILESS_MAIN(TestLongWindowsPath)
  121. #include "testlongpath.moc"