From 3a36e86e1953a994b2b433f82e9326c099d79daa Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Mon, 18 Dec 2023 14:05:48 +0100 Subject: [PATCH] fix(entry): root path should only have a single slash (+tests) --- CMakeLists.txt | 1 + src/dwarfs/entry.cpp | 4 +- test/entry.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 test/entry.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cfbaabbc..6cc63cd8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -595,6 +595,7 @@ if(WITH_TESTS) test/dwarfs.cpp test/dwarfs_compat.cpp test/dwarfs_badfs.cpp + test/entry.cpp test/utils_test.cpp test/block_merger_test.cpp test/metadata_requirements_test.cpp diff --git a/src/dwarfs/entry.cpp b/src/dwarfs/entry.cpp index 4ee66582b..266842d82 100644 --- a/src/dwarfs/entry.cpp +++ b/src/dwarfs/entry.cpp @@ -84,7 +84,7 @@ std::string entry::path_as_string() const { std::string entry::dpath() const { auto p = path_as_string(); - if (type() == E_DIR) { + if (type() == E_DIR && !p.empty() && p.back() != '/') { p += '/'; } return p; @@ -93,7 +93,7 @@ std::string entry::dpath() const { std::string entry::unix_dpath() const { auto p = name_; - if (type() == E_DIR) { + if (type() == E_DIR && !p.empty() && p.back() != '/') { p += '/'; } diff --git a/test/entry.cpp b/test/entry.cpp new file mode 100644 index 000000000..75fd3113d --- /dev/null +++ b/test/entry.cpp @@ -0,0 +1,90 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#include + +#include "dwarfs/entry.h" + +#include "test_helpers.h" + +using namespace dwarfs; + +struct entry_test : public ::testing::Test { + std::shared_ptr os; + std::unique_ptr ef; + + void SetUp() override { + os = test::os_access_mock::create_test_instance(); + ef = entry_factory::create(); + } + + void TearDown() override { + ef.reset(); + os.reset(); + } +}; + +TEST_F(entry_test, path) { + auto e1 = ef->create(*os, "/"); + auto e2 = ef->create(*os, "somelink", e1); + auto e3 = ef->create(*os, "somedir", e1); + auto e4 = ef->create(*os, "somedir/ipsum.py", e3); + + EXPECT_FALSE(e1->has_parent()); + EXPECT_TRUE(e1->is_directory()); + EXPECT_EQ(e1->type(), entry::E_DIR); + + EXPECT_EQ("/", e1->name()); + EXPECT_EQ("/", e1->fs_path()); + EXPECT_EQ("/", e1->path_as_string()); + EXPECT_EQ("/", e1->dpath()); + EXPECT_EQ("/", e1->unix_dpath()); + + EXPECT_TRUE(e2->has_parent()); + EXPECT_FALSE(e2->is_directory()); + EXPECT_EQ(e2->type(), entry::E_LINK); + + EXPECT_EQ("somelink", e2->name()); + EXPECT_EQ("/somelink", e2->fs_path()); + EXPECT_EQ("/somelink", e2->path_as_string()); + EXPECT_EQ("/somelink", e2->dpath()); + EXPECT_EQ("/somelink", e2->unix_dpath()); + + EXPECT_TRUE(e3->has_parent()); + EXPECT_TRUE(e3->is_directory()); + EXPECT_EQ(e3->type(), entry::E_DIR); + + EXPECT_EQ("somedir", e3->name()); + EXPECT_EQ("/somedir", e3->fs_path()); + EXPECT_EQ("/somedir", e3->path_as_string()); + EXPECT_EQ("/somedir/", e3->dpath()); + EXPECT_EQ("/somedir/", e3->unix_dpath()); + + EXPECT_TRUE(e4->has_parent()); + EXPECT_FALSE(e4->is_directory()); + EXPECT_EQ(e4->type(), entry::E_FILE); + + EXPECT_EQ("ipsum.py", e4->name()); + EXPECT_EQ("/somedir/ipsum.py", e4->fs_path()); + EXPECT_EQ("/somedir/ipsum.py", e4->path_as_string()); + EXPECT_EQ("/somedir/ipsum.py", e4->dpath()); + EXPECT_EQ("/somedir/ipsum.py", e4->unix_dpath()); +}