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
|
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "filesystem/dcapmanager.h"
#include "filesystem/dcapfile.h"
#include <gtest/gtest.h>
#include <QDir>
#include <QDir>
#include <QTemporaryDir>
DCORE_USE_NAMESPACE
#ifndef GTEST_SKIP
#define SKIP return GTEST_SUCCEED() << "Skip all tests"
#else
#define SKIP GTEST_SKIP() << "Skip all tests"
#endif
#define TMPCAP_PATH "/tmp/cap"
class ut_DCapFile : public testing::Test
{
protected:
void SetUp() override;
void TearDown() override;
DCapManager *manager;
QFile *file;
};
void ut_DCapFile::SetUp()
{
manager = DCapManager::instance();
manager->removePath("/tmp");
manager->appendPath(TMPCAP_PATH);
file = new QFile(nullptr);
QDir dir(TMPCAP_PATH);
if (!dir.exists())
ASSERT_TRUE(dir.mkdir(TMPCAP_PATH));
}
void ut_DCapFile::TearDown()
{
manager->appendPath("/tmp");
delete file;
QDir dir(TMPCAP_PATH);
if (dir.exists())
ASSERT_TRUE(dir.removeRecursively());
}
TEST(ut_DCapManager, paths)
{
auto size = DCapManager::instance()->paths().size();
EXPECT_TRUE(size > 0);
DCapManager::instance()->appendPaths({"/path/to/myCap"});
EXPECT_TRUE(DCapManager::instance()->paths().contains("/path/to/myCap"));
DCapManager::instance()->removePaths({"/path/to/myCap"});
EXPECT_FALSE(DCapManager::instance()->paths().contains("/path/to/myCap"));
}
TEST(ut_DCapFileAndDir, testDCapFileOpen)
{
DCapFile file("/tmp/test0");
ASSERT_TRUE(file.open(DCapFile::WriteOnly)); // Default, '/tmp' is allowable for writing.
file.close();
DCapManager::instance()->removePath("/tmp");
ASSERT_FALSE(file.open(DCapFile::WriteOnly));
DCapManager::instance()->appendPath("/tmp");
}
TEST(ut_DCapFileAndDir, testDCapFileOperation)
{
auto CheckResult = [](bool result) {
DCapFile file("/tmp/test0");
ASSERT_EQ(file.open(DCapFile::WriteOnly), result);
file.close();
ASSERT_EQ(file.exists(), result);
ASSERT_EQ(DCapFile::exists(file.fileName()), result);
ASSERT_EQ(file.remove(), result);
ASSERT_EQ(file.open(DCapFile::WriteOnly), result);
file.close();
ASSERT_EQ(DCapFile::remove(file.fileName()), result);
ASSERT_EQ(file.open(DCapFile::WriteOnly), result);
file.close();
ASSERT_EQ(file.rename("/tmp/test1"), result);
ASSERT_EQ(DCapFile::rename(file.fileName(), "/tmp/test0"), result);
file.setFileName("/tmp/test0");
ASSERT_EQ(file.link("/tmp/test_link0"), result);
ASSERT_EQ(DCapFile::link(file.fileName(), "/tmp/test_link1"), result);
ASSERT_EQ(DCapFile::remove("/tmp/test_link0"), result);
ASSERT_EQ(DCapFile::remove("/tmp/test_link1"), result);
file.setFileName("/tmp/test0");
ASSERT_EQ(file.copy("/tmp/test_copy0"), result);
ASSERT_EQ(DCapFile::copy(file.fileName(), "/tmp/test_copy1"), result);
ASSERT_EQ(DCapFile::remove("/tmp/test_copy0"), result);
ASSERT_EQ(DCapFile::remove("/tmp/test_copy1"), result);
ASSERT_EQ(file.resize(10), result);
ASSERT_EQ(file.size() == 10, result);
ASSERT_EQ(DCapFile::resize(file.fileName(), 5), result);
ASSERT_EQ(file.size() == 5, result);
ASSERT_EQ(file.remove(), result);
};
CheckResult(true);
DCapManager::instance()->removePath("/tmp");
CheckResult(false);
DCapManager::instance()->appendPath("/tmp");
}
TEST(ut_DCapFileAndDir, testDCapDirOperation)
{
DCapDir dir("/tmp");
ASSERT_TRUE(dir.exists());
ASSERT_FALSE(dir.entryList().isEmpty());
ASSERT_FALSE(dir.entryInfoList().isEmpty());
DCapFile file(dir.path() + "/test0");
ASSERT_TRUE(file.open(DCapFile::WriteOnly));
file.close();
ASSERT_TRUE(dir.exists("test0"));
ASSERT_TRUE(dir.mkdir("cap"));
ASSERT_TRUE(dir.exists("cap"));
ASSERT_TRUE(dir.rmdir("cap"));
ASSERT_FALSE(dir.exists("cap"));
dir.mkdir("cap");
ASSERT_TRUE(dir.cd("cap"));
ASSERT_TRUE(dir.exists());
DCapManager::instance()->removePath("/tmp");
DCapManager::instance()->appendPath(dir.path());
ASSERT_FALSE(dir.cd(".."));
dir.setPath("/tmp");
ASSERT_TRUE(dir.entryList().isEmpty());
ASSERT_TRUE(dir.entryInfoList().isEmpty());
ASSERT_TRUE(dir.exists("cap"));
ASSERT_FALSE(dir.remove("test0"));
ASSERT_FALSE(dir.rename("test0", "test1"));
ASSERT_TRUE(dir.mkpath(TMPCAP_PATH"/subdir"));
ASSERT_TRUE(dir.rmpath(TMPCAP_PATH"/subdir"));
DCapManager::instance()->appendPath("/tmp");
ASSERT_TRUE(dir.rename("test0", "test1"));
ASSERT_TRUE(dir.remove("test1"));
}
|