1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
* Xournal++
*
* This file is part of the Xournal UnitTests
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/
#include <filesystem>
#include <gtest/gtest.h>
#include "util/PathUtil.h"
#include "util/raii/CStringWrapper.h"
#include "filesystem.h"
TEST(UtilPath, testUnsupportedUri) {
auto a = Util::fromUri("http://localhost/test.txt");
EXPECT_EQ(true, !a);
auto b = Util::fromUri("file://invalid");
EXPECT_EQ(true, !b);
}
TEST(UtilPath, testPathFromUri) {
auto b = Util::fromUri("file:///tmp/test.txt");
EXPECT_EQ(false, !b);
EXPECT_EQ(G_DIR_SEPARATOR_S + std::string("tmp") + G_DIR_SEPARATOR_S + std::string("test.txt"), b->string());
}
TEST(UtilPath, testPathIsChildOf) {
EXPECT_TRUE(Util::isChildOrEquivalent("C:/Users/Subdir", "C:/Users"));
EXPECT_TRUE(Util::isChildOrEquivalent("C:/Users/Subdir", "C:/Users/"));
EXPECT_TRUE(Util::isChildOrEquivalent("C:/Users/Subdir/", "C:/Users/"));
EXPECT_TRUE(Util::isChildOrEquivalent("C:/Users/Subdir/", "C:/Users"));
EXPECT_TRUE(Util::isChildOrEquivalent("C:/Users/Subdir/", "C:/Users/Subdir/"));
EXPECT_TRUE(Util::isChildOrEquivalent("D:/Users/Subdir", "D:/Users"));
EXPECT_TRUE(!Util::isChildOrEquivalent("D:/Users/Subdir", "D:/users"));
EXPECT_TRUE(!Util::isChildOrEquivalent("C:/A/B", "C:/B/A"));
EXPECT_TRUE(!Util::isChildOrEquivalent("C:/B/A", "C:/A/B"));
EXPECT_TRUE(!Util::isChildOrEquivalent("D:/Users/Subdir", "C:/Users"));
EXPECT_TRUE(!Util::isChildOrEquivalent("D:/Users/Subdir", "C:/Users"));
// Todo add a symlink test
}
TEST(UtilPath, testClearExtensions) {
// These tests use the preferred separator (i.e. "\\" on Windows and "/" on POSIX)
auto a = fs::path("C:") / "test" / "abc" / "xyz.txt";
fs::path old_path(a);
Util::clearExtensions(a);
EXPECT_EQ(old_path.string(), a.string());
a = fs::path("C:") / "test" / "abc" / "xyz";
old_path = a;
a += ".xopp";
Util::clearExtensions(a);
EXPECT_EQ(old_path.string(), a.string());
// The following tests use the generic separator which works on all systems
auto b = fs::path("/test/asdf.TXT");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.TXT"), b.string());
Util::clearExtensions(b, ".txt");
EXPECT_EQ(std::string("/test/asdf"), b.string());
b = fs::path("/test/asdf.asdf/asdf");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.asdf/asdf"), b.string());
b = fs::path("/test/asdf.PDF");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.PDF"), b.string());
Util::clearExtensions(b, ".pdf");
EXPECT_EQ(std::string("/test/asdf"), b.string());
b = fs::path("/test/asdf.PDF.xoj");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.PDF"), b.string());
b = fs::path("/test/asdf.PDF.xoj");
Util::clearExtensions(b, ".Pdf");
EXPECT_EQ(std::string("/test/asdf"), b.string());
b = fs::path("/test/asdf.pdf.pdf");
Util::clearExtensions(b, ".pdf");
EXPECT_EQ(std::string("/test/asdf.pdf"), b.string());
b = fs::path("/test/asdf.xopp.xopp");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.xopp"), b.string());
b = fs::path("/test/asdf.PDF.xopp");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.PDF"), b.string());
b = fs::path("/test/asdf.SVG.xopp");
Util::clearExtensions(b, ".svg");
EXPECT_EQ(std::string("/test/asdf"), b.string());
b = fs::path("/test/asdf.xoj");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf"), b.string());
b = fs::path("/test/asdf.xopp");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf"), b.string());
b = fs::path("/test/asdf.pdf");
Util::clearExtensions(b);
EXPECT_EQ(std::string("/test/asdf.pdf"), b.string());
}
TEST(UtilPath, normalizeAssetPath) {
auto p = fs::path();
#ifdef _WIN32
// Test \\ to / conversion + not relative
// Test, if the implementation of std::filesystem::proximate and std::filesystem::relative resolves "/" and "\\" to
// the same path
EXPECT_EQ(std::filesystem::proximate("D:/home/freddy", "C:/home"),
std::filesystem::proximate("D:\\home\\freddy", "C:\\home"));
EXPECT_EQ(std::filesystem::relative("D:/home/freddy", "C:/home"),
std::filesystem::relative("D:\\home\\freddy", "C:\\home"));
p = Util::normalizeAssetPath("C:\\dir\\file.txt", "D:", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"C:/dir/file.txt"), p.u8string());
p = Util::normalizeAssetPath("C:\\dir\\file.txt", "D:\\base", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"C:/dir/file.txt"), p.u8string());
p = Util::normalizeAssetPath("C:/dir/file.txt", "D:", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"C:/dir/file.txt"), p.u8string());
p = Util::normalizeAssetPath("C:/dir/file.txt", "D:/base", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"C:/dir/file.txt"), p.u8string());
// Test \\ to / conversion + relative
p = Util::normalizeAssetPath("C:\\dir\\file.txt", "C:\\dir", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"file.txt"), p.u8string());
p = Util::normalizeAssetPath("C:\\dir\\file.txt", "C:\\base", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"../dir/file.txt"), p.u8string());
p = Util::normalizeAssetPath("C:\\dir\\..\\dir2\\.\\dir3\\..\\file.txt", "C:\\base",
Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"../dir2/file.txt"), p.u8string());
p = Util::normalizeAssetPath("C:\\dir\\..\\dir2\\.\\dir3\\..\\file.txt", "C:\\base",
Util::PathStorageMode::AS_ABSOLUTE_PATH);
EXPECT_EQ(std::u8string_view(u8"C:/dir2/file.txt"), p.u8string());
auto current = fs::current_path();
auto gen = fs::weakly_canonical((current / "..\\dir\\file.txt")).generic_u8string();
// relative asset path
p = Util::normalizeAssetPath("..\\dir\\file.txt", "H:", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(gen, p.u8string());
p = Util::normalizeAssetPath(".\\file.txt", fs::current_path(), Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"file.txt"), p.u8string());
#else
p = Util::normalizeAssetPath("/dir/file.txt", "/", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"dir/file.txt"), p.u8string());
p = Util::normalizeAssetPath("/dir/file.txt", "/base", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"../dir/file.txt"), p.u8string());
p = Util::normalizeAssetPath("/dir/../dir2/./dir3/../file.txt", "/base", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"../dir2/file.txt"), p.u8string());
p = Util::normalizeAssetPath("../../dir/../dir2/./dir3/../file.txt", {}, Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"../../dir2/file.txt"), p.u8string());
p = Util::normalizeAssetPath("/dir/../dir2/./dir3/../file.txt", "/base", Util::PathStorageMode::AS_ABSOLUTE_PATH);
EXPECT_EQ(std::u8string_view(u8"/dir2/file.txt"), p.u8string());
p = Util::normalizeAssetPath("dir/file.txt", "dir/..", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"dir/file.txt"), p.u8string());
// do not return empty if asset_path is relative
p = Util::normalizeAssetPath("../dir/file.txt", "/", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_TRUE(!p.empty());
p = Util::normalizeAssetPath("../dir/file.txt", "/base", Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_TRUE(!p.empty());
// symlinks are not resolved
if (!fs::exists("xournalpp-test-symlink")) {
fs::create_directory_symlink("../dir", "xournalpp-test-symlink");
p = Util::normalizeAssetPath("xournalpp-test-symlink/file.txt", "base",
Util::PathStorageMode::AS_RELATIVE_PATH);
EXPECT_EQ(std::u8string_view(u8"../xournalpp-test-symlink/file.txt"), p.u8string());
fs::remove("xournalpp-test-symlink");
} else {
FAIL() << "File named \"xournalpp-test-symlink\" already exists";
}
#endif
}
TEST(UtilPath, pathGFilenameConvertion) {
auto test = [](const char8_t* str) {
fs::path p = fs::path(str);
auto gf = xoj::util::OwnedCString::assumeOwnership(
g_filename_from_utf8(char_cast(str), -1, nullptr, nullptr, nullptr));
EXPECT_EQ(p.u8string(), str);
EXPECT_STREQ(Util::toGFilename(p).c_str(), gf.get());
EXPECT_EQ(p, Util::fromGFilename(gf.get()));
xoj::util::GObjectSPtr<GFile> gfile(g_file_new_for_path(gf.get()), xoj::util::adopt);
EXPECT_TRUE(g_file_equal(Util::toGFile(p).get(), gfile.get()));
EXPECT_EQ(fs::absolute(p), fs::absolute(Util::fromGFile(gfile.get())));
EXPECT_EQ(fs::absolute(p), fs::absolute(Util::fromUri(Util::toUri(p).value()).value()));
};
test(u8"foo/µbar/Ñ/ë€ds测试q.pdf");
test(u8"/foo/µbar/Ñ/ë€ds测试q.pdf");
}
|