| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "filesystem/dcapmanager.h" |
| 6 |
#include "filesystem/dcapfile.h" |
| 7 |
|
| 8 |
#include <gtest/gtest.h> |
| 9 |
#include <QDir> |
| 10 |
#include <QDir> |
| 11 |
#include <QTemporaryDir> |
| 12 |
|
| 13 |
DCORE_USE_NAMESPACE |
| 14 |
|
| 15 |
#ifndef GTEST_SKIP |
| 16 |
#define SKIP return GTEST_SUCCEED() << "Skip all tests" |
| 17 |
#else |
| 18 |
#define SKIP GTEST_SKIP() << "Skip all tests" |
| 19 |
#endif |
| 20 |
|
| 21 |
#define TMPCAP_PATH "/tmp/cap" |
| 22 |
|
| 23 |
class ut_DCapFile : public testing::Test |
| 24 |
{ |
| 25 |
protected: |
| 26 |
void SetUp() override; |
| 27 |
void TearDown() override; |
| 28 |
|
| 29 |
DCapManager *manager; |
| 30 |
QFile *file; |
| 31 |
}; |
| 32 |
|
| 33 |
void ut_DCapFile::SetUp() |
| 34 |
{ |
| 35 |
manager = DCapManager::instance(); |
| 36 |
manager->removePath("/tmp"); |
| 37 |
manager->appendPath(TMPCAP_PATH); |
| 38 |
|
| 39 |
file = new QFile(nullptr); |
| 40 |
QDir dir(TMPCAP_PATH); |
| 41 |
if (!dir.exists()) |
| 42 |
ASSERT_TRUE(dir.mkdir(TMPCAP_PATH)); |
| 43 |
} |
| 44 |
|
| 45 |
void ut_DCapFile::TearDown() |
| 46 |
{ |
| 47 |
manager->appendPath("/tmp"); |
| 48 |
delete file; |
| 49 |
QDir dir(TMPCAP_PATH); |
| 50 |
if (dir.exists()) |
| 51 |
ASSERT_TRUE(dir.removeRecursively()); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(ut_DCapManager, paths) |
| 55 |
{ |
| 56 |
auto size = DCapManager::instance()->paths().size(); |
| 57 |
EXPECT_TRUE(size > 0); |
| 58 |
|
| 59 |
DCapManager::instance()->appendPaths({"/path/to/myCap"}); |
| 60 |
EXPECT_TRUE(DCapManager::instance()->paths().contains("/path/to/myCap")); |
| 61 |
|
| 62 |
DCapManager::instance()->removePaths({"/path/to/myCap"}); |
| 63 |
EXPECT_FALSE(DCapManager::instance()->paths().contains("/path/to/myCap")); |
| 64 |
} |
| 65 |
|
| 66 |
TEST(ut_DCapFileAndDir, testDCapFileOpen) |
| 67 |
{ |
| 68 |
DCapFile file("/tmp/test0"); |
| 69 |
ASSERT_TRUE(file.open(DCapFile::WriteOnly)); // Default, '/tmp' is allowable for writing. |
| 70 |
file.close(); |
| 71 |
|
| 72 |
DCapManager::instance()->removePath("/tmp"); |
| 73 |
ASSERT_FALSE(file.open(DCapFile::WriteOnly)); |
| 74 |
DCapManager::instance()->appendPath("/tmp"); |
| 75 |
} |
| 76 |
|
| 77 |
TEST(ut_DCapFileAndDir, testDCapFileOperation) |
| 78 |
{ |
| 79 |
auto CheckResult = [](bool result) { |
| 80 |
DCapFile file("/tmp/test0"); |
| 81 |
ASSERT_EQ(file.open(DCapFile::WriteOnly), result); |
| 82 |
file.close(); |
| 83 |
|
| 84 |
ASSERT_EQ(file.exists(), result); |
| 85 |
ASSERT_EQ(DCapFile::exists(file.fileName()), result); |
| 86 |
ASSERT_EQ(file.remove(), result); |
| 87 |
|
| 88 |
ASSERT_EQ(file.open(DCapFile::WriteOnly), result); |
| 89 |
file.close(); |
| 90 |
ASSERT_EQ(DCapFile::remove(file.fileName()), result); |
| 91 |
|
| 92 |
ASSERT_EQ(file.open(DCapFile::WriteOnly), result); |
| 93 |
file.close(); |
| 94 |
ASSERT_EQ(file.rename("/tmp/test1"), result); |
| 95 |
ASSERT_EQ(DCapFile::rename(file.fileName(), "/tmp/test0"), result); |
| 96 |
|
| 97 |
file.setFileName("/tmp/test0"); |
| 98 |
ASSERT_EQ(file.link("/tmp/test_link0"), result); |
| 99 |
ASSERT_EQ(DCapFile::link(file.fileName(), "/tmp/test_link1"), result); |
| 100 |
ASSERT_EQ(DCapFile::remove("/tmp/test_link0"), result); |
| 101 |
ASSERT_EQ(DCapFile::remove("/tmp/test_link1"), result); |
| 102 |
|
| 103 |
file.setFileName("/tmp/test0"); |
| 104 |
ASSERT_EQ(file.copy("/tmp/test_copy0"), result); |
| 105 |
ASSERT_EQ(DCapFile::copy(file.fileName(), "/tmp/test_copy1"), result); |
| 106 |
ASSERT_EQ(DCapFile::remove("/tmp/test_copy0"), result); |
| 107 |
ASSERT_EQ(DCapFile::remove("/tmp/test_copy1"), result); |
| 108 |
|
| 109 |
ASSERT_EQ(file.resize(10), result); |
| 110 |
ASSERT_EQ(file.size() == 10, result); |
| 111 |
ASSERT_EQ(DCapFile::resize(file.fileName(), 5), result); |
| 112 |
ASSERT_EQ(file.size() == 5, result); |
| 113 |
ASSERT_EQ(file.remove(), result); |
| 114 |
}; |
| 115 |
|
| 116 |
CheckResult(true); |
| 117 |
DCapManager::instance()->removePath("/tmp"); |
| 118 |
CheckResult(false); |
| 119 |
DCapManager::instance()->appendPath("/tmp"); |
| 120 |
} |
| 121 |
|
| 122 |
TEST(ut_DCapFileAndDir, testDCapDirOperation) |
| 123 |
{ |
| 124 |
DCapDir dir("/tmp"); |
| 125 |
ASSERT_TRUE(dir.exists()); |
| 126 |
ASSERT_FALSE(dir.entryList().isEmpty()); |
| 127 |
ASSERT_FALSE(dir.entryInfoList().isEmpty()); |
| 128 |
|
| 129 |
DCapFile file(dir.path() + "/test0"); |
| 130 |
ASSERT_TRUE(file.open(DCapFile::WriteOnly)); |
| 131 |
file.close(); |
| 132 |
ASSERT_TRUE(dir.exists("test0")); |
| 133 |
|
| 134 |
ASSERT_TRUE(dir.mkdir("cap")); |
| 135 |
ASSERT_TRUE(dir.exists("cap")); |
| 136 |
ASSERT_TRUE(dir.rmdir("cap")); |
| 137 |
ASSERT_FALSE(dir.exists("cap")); |
| 138 |
|
| 139 |
dir.mkdir("cap"); |
| 140 |
ASSERT_TRUE(dir.cd("cap")); |
| 141 |
ASSERT_TRUE(dir.exists()); |
| 142 |
DCapManager::instance()->removePath("/tmp"); |
| 143 |
DCapManager::instance()->appendPath(dir.path()); |
| 144 |
ASSERT_FALSE(dir.cd("..")); |
| 145 |
dir.setPath("/tmp"); |
| 146 |
|
| 147 |
ASSERT_TRUE(dir.entryList().isEmpty()); |
| 148 |
ASSERT_TRUE(dir.entryInfoList().isEmpty()); |
| 149 |
ASSERT_TRUE(dir.exists("cap")); |
| 150 |
ASSERT_FALSE(dir.remove("test0")); |
| 151 |
ASSERT_FALSE(dir.rename("test0", "test1")); |
| 152 |
|
| 153 |
ASSERT_TRUE(dir.mkpath(TMPCAP_PATH"/subdir")); |
| 154 |
ASSERT_TRUE(dir.rmpath(TMPCAP_PATH"/subdir")); |
| 155 |
|
| 156 |
DCapManager::instance()->appendPath("/tmp"); |
| 157 |
ASSERT_TRUE(dir.rename("test0", "test1")); |
| 158 |
ASSERT_TRUE(dir.remove("test1")); |
| 159 |
} |