Cleanup pathtoUNC and its test
authorHannah von Reth <hannah.vonreth@owncloud.com>
Mon, 14 Dec 2020 12:32:28 +0000 (13:32 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Wed, 30 Dec 2020 15:17:46 +0000 (16:17 +0100)
src/common/filesystembase.cpp
src/common/filesystembase.h
test/CMakeLists.txt
test/csync/CMakeLists.txt
test/csync/encoding_tests/check_encoding.cpp [deleted file]
test/testlongwinpath.cpp [new file with mode: 0644]

index 6b22717771795f1c77de1a6aa5f339b7c2825e25..9914c1a7e62842e09e794c8e795a0baf65389d9f 100644 (file)
@@ -497,33 +497,26 @@ bool FileSystem::isJunction(const QString &filename)
 #endif
 }
 
-QString FileSystem::pathtoUNC(const QString &str)
+#ifdef Q_OS_WIN
+QString FileSystem::pathtoUNC(const QString &_str)
 {
-    int len = 0;
-    QString longStr;
-
-    len = str.length();
-    longStr.reserve(len + 4);
-
-    // prepend \\?\ and convert '/' => '\' to support long names
-    if (str[0] == QLatin1Char('/') || str[0] == QLatin1Char('\\')) {
-        // Don't prepend if already UNC
-        if (!(len > 1 && (str[1] == QLatin1Char('/') || str[1] == QLatin1Char('\\')))) {
-            longStr.append(QStringLiteral("\\\\?"));
-        }
-    } else {
-        longStr.append(QStringLiteral("\\\\?\\")); // prepend string by this four magic chars.
+    if (_str.isEmpty()) {
+        return _str;
     }
-    longStr += str;
+    const QString str = QDir::toNativeSeparators(_str);
+    const QLatin1Char sep('\\');
 
-    /* replace all occurences of / with the windows native \ */
+    // we already have a unc path
+    if (str.startsWith(sep + sep)) {
+        return str;
+    }
+    // prepend \\?\ and to support long names
 
-    for (auto &c : longStr) {
-        if (c == QLatin1Char('/')) {
-            c = QLatin1Char('\\');
-        }
+    if (str.at(0) == sep) {
+        return QStringLiteral(R"(\\?)") + str;
     }
-    return longStr;
+    return QStringLiteral(R"(\\?\)") + str;
 }
+#endif
 
 } // namespace OCC
index 4e35730588a67d6c4290540896c33fa72dbf2771..0259fb6c7f0a3a702e326ee051be5d287f99e804 100644 (file)
@@ -128,6 +128,19 @@ namespace FileSystem {
      * Returns the file system used at the given path.
      */
     QString fileSystemForPath(const QString &path);
+
+    /*
+     * This function takes a path and converts it to a UNC representation of the
+     * string. That means that it prepends a \\?\ (unless already UNC) and converts
+     * all slashes to backslashes.
+     *
+     * Note the following:
+     *  - The string must be absolute.
+     *  - it needs to contain a drive character to be a valid UNC
+     *  - A conversion is only done if the path len is larger than 245. Otherwise
+     *    the windows API functions work with the normal "unixoid" representation too.
+     */
+    QString OCSYNC_EXPORT pathtoUNC(const QString &str);
 #endif
 
     /**
@@ -144,19 +157,6 @@ namespace FileSystem {
      * Returns whether the file is a junction (windows only)
      */
     bool OCSYNC_EXPORT isJunction(const QString &filename);
-
-    /*
-     * This function takes a path and converts it to a UNC representation of the
-     * string. That means that it prepends a \\?\ (unless already UNC) and converts
-     * all slashes to backslashes.
-     *
-     * Note the following:
-     *  - The string must be absolute.
-     *  - it needs to contain a drive character to be a valid UNC
-     *  - A conversion is only done if the path len is larger than 245. Otherwise
-     *    the windows API functions work with the normal "unixoid" representation too.
-     */
-    QString OCSYNC_EXPORT pathtoUNC(const QString &str);
 }
 
 /** @} */
index b13f6bff16fc146857733351ea7fb47b20e0941e..0d510c51a6896d2eeb395521f266429120324344 100644 (file)
@@ -74,6 +74,7 @@ if( UNIX AND NOT APPLE )
 endif(UNIX AND NOT APPLE)
 
 if (WIN32)
+    nextcloud_add_test(LongWinPath "")
     nextcloud_add_test(SyncCfApi "")
 endif()
 
index 224ecb6067db9e05349a7bb64b5b2c13e457cfab..27e6d385721bd94c32cd7639decd1d53f2451827 100644 (file)
@@ -24,6 +24,3 @@ add_cmocka_test(check_std_c_jhash std_tests/check_std_c_jhash.c ${TEST_TARGET_LI
 # vio
 add_cmocka_test(check_vio_ext vio_tests/check_vio_ext.cpp ${TEST_TARGET_LIBRARIES})
 
-# encoding
-add_cmocka_test(check_encoding_functions encoding_tests/check_encoding.cpp ${TEST_TARGET_LIBRARIES})
-
diff --git a/test/csync/encoding_tests/check_encoding.cpp b/test/csync/encoding_tests/check_encoding.cpp
deleted file mode 100644 (file)
index 98ac0fa..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2013 by Klaas Freitag <freitag@owncloud.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-#include <cstdio>
-#include "common/filesystembase.h"
-#include "torture.h"
-
-#ifdef _WIN32
-#include <string.h>
-#endif
-
-#include "torture.h"
-
-static void check_long_win_path(void **state)
-{
-    (void) state; /* unused */
-
-    {
-        const auto path = QStringLiteral("C://DATA/FILES/MUSIC/MY_MUSIC.mp3"); // check a short path
-        const auto exp_path = QStringLiteral(R"(\\?\C:\\DATA\FILES\MUSIC\MY_MUSIC.mp3)");
-        QString new_short = OCC::FileSystem::pathtoUNC(path);
-        assert_string_equal(new_short.constData(), exp_path.constData());
-    }
-
-    {
-        const auto path = QStringLiteral(R"(\\foo\bar/MY_MUSIC.mp3)");
-        const auto exp_path = QStringLiteral(R"(\\foo\bar\MY_MUSIC.mp3)");
-        QString new_short = OCC::FileSystem::pathtoUNC(path);
-        assert_string_equal(new_short.constData(), exp_path.constData());
-    }
-
-    {
-        const auto path = QStringLiteral(R"(//foo\bar/MY_MUSIC.mp3)");
-        const auto exp_path = QStringLiteral(R"(\\foo\bar\MY_MUSIC.mp3)");
-        QString new_short = OCC::FileSystem::pathtoUNC(path);
-        assert_string_equal(new_short.constData(), exp_path.constData());
-    }
-
-    {
-        const auto path = QStringLiteral(R"(\foo\bar)");
-        const auto exp_path = QStringLiteral(R"(\\?\foo\bar)");
-        QString new_short = OCC::FileSystem::pathtoUNC(path);
-        assert_string_equal(new_short.constData(), exp_path.constData());
-    }
-
-    {
-        const auto path = QStringLiteral("/foo/bar");
-        const auto exp_path = QStringLiteral(R"(\\?\foo\bar)");
-        QString new_short = OCC::FileSystem::pathtoUNC(path);
-        assert_string_equal(new_short.constData(), exp_path.constData());
-    }
-
-    const auto longPath = QStringLiteral("D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
-                                         "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
-                                         "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
-                                         "olonglonglonglong/file.txt");
-    const auto longPathConv = QStringLiteral(R"(\\?\D:\\alonglonglonglong\blonglonglonglong\clonglonglonglong\dlonglonglonglong\)"
-                                             R"(elonglonglonglong\flonglonglonglong\glonglonglonglong\hlonglonglonglong\ilonglonglonglong\)"
-                                             R"(jlonglonglonglong\klonglonglonglong\llonglonglonglong\mlonglonglonglong\nlonglonglonglong\)"
-                                             R"(olonglonglonglong\file.txt)");
-
-    QString new_long = OCC::FileSystem::pathtoUNC(longPath);
-    // printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved);
-
-    assert_string_equal(new_long.constData(), longPathConv.constData());
-
-    // printf( "YYYYYYYYYYYY %ld\n", strlen(new_long));
-    assert_int_equal(new_long.length(), 286);
-}
-
-int torture_run_tests(void)
-{
-    const struct CMUnitTest tests[] = {
-        cmocka_unit_test(check_long_win_path),
-
-    };
-
-    return cmocka_run_group_tests(tests, nullptr, nullptr);
-}
-
diff --git a/test/testlongwinpath.cpp b/test/testlongwinpath.cpp
new file mode 100644 (file)
index 0000000..1693381
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * libcsync -- a library to sync a directory with another
+ *
+ * Copyright (c) 2013 by Klaas Freitag <freitag@owncloud.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include "common/filesystembase.h"
+
+#include <QTest>
+
+
+class TestLongWindowsPath : public QObject
+{
+    Q_OBJECT
+
+private Q_SLOTS:
+    void check_long_win_path()
+    {
+        {
+            const auto path = QStringLiteral("C://DATA/FILES/MUSIC/MY_MUSIC.mp3"); // check a short path
+            const auto exp_path = QStringLiteral("\\\\?\\C:\\\\DATA\\FILES\\MUSIC\\MY_MUSIC.mp3");
+            QString new_short = OCC::FileSystem::pathtoUNC(path);
+            QCOMPARE(new_short, exp_path);
+        }
+
+        {
+            const auto path = QStringLiteral("\\\\foo\\bar/MY_MUSIC.mp3");
+            const auto exp_path = QStringLiteral("\\\\foo\\bar\\MY_MUSIC.mp3");
+            QString new_short = OCC::FileSystem::pathtoUNC(path);
+            QCOMPARE(new_short, exp_path);
+        }
+
+        {
+            const auto path = QStringLiteral("//foo\\bar/MY_MUSIC.mp3");
+            const auto exp_path = QStringLiteral("\\\\foo\\bar\\MY_MUSIC.mp3");
+            QString new_short = OCC::FileSystem::pathtoUNC(path);
+            QCOMPARE(new_short, exp_path);
+        }
+
+        {
+            const auto path = QStringLiteral("\\foo\\bar");
+            const auto exp_path = QStringLiteral("\\\\?\\foo\\bar");
+            QString new_short = OCC::FileSystem::pathtoUNC(path);
+            QCOMPARE(new_short, exp_path);
+        }
+
+        {
+            const auto path = QStringLiteral("/foo/bar");
+            const auto exp_path = QStringLiteral("\\\\?\\foo\\bar");
+            QString new_short = OCC::FileSystem::pathtoUNC(path);
+            QCOMPARE(new_short, exp_path);
+        }
+
+        const auto longPath = QStringLiteral("D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/"
+                                             "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/"
+                                             "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/"
+                                             "olonglonglonglong/file.txt");
+        const auto longPathConv = QStringLiteral("\\\\?\\D:\\\\alonglonglonglong\\blonglonglonglong\\clonglonglonglong\\dlonglonglonglong\\"
+                                                 "elonglonglonglong\\flonglonglonglong\\glonglonglonglong\\hlonglonglonglong\\ilonglonglonglong\\"
+                                                 "jlonglonglonglong\\klonglonglonglong\\llonglonglonglong\\mlonglonglonglong\\nlonglonglonglong\\"
+                                                 "olonglonglonglong\\file.txt");
+
+        QString new_long = OCC::FileSystem::pathtoUNC(longPath);
+        // printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved);
+
+        QCOMPARE(new_long, longPathConv);
+
+        // printf( "YYYYYYYYYYYY %ld\n", strlen(new_long));
+        QCOMPARE(new_long.length(), 286);
+    }
+};
+
+QTEST_GUILESS_MAIN(TestLongWindowsPath)
+#include "testlongwinpath.moc"