1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <filesystem>
17 #include <fstream>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "mkdir_core.h"
22 #include "uv_fs_mock.h"
23
24 namespace OHOS::FileManagement::ModuleFileIO::Test {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28
29 class MkdirCoreMockTest : public testing::Test {
30 public:
31 static filesystem::path tempFilePath;
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 static inline shared_ptr<UvfsMock> uvMock = nullptr;
37 };
38
39 filesystem::path MkdirCoreMockTest::tempFilePath;
40
SetUpTestCase(void)41 void MkdirCoreMockTest::SetUpTestCase(void)
42 {
43 GTEST_LOG_(INFO) << "SetUpTestCase";
44 tempFilePath = filesystem::temp_directory_path() / "mkdir_core_mock_test";
45 std::filesystem::create_directory(tempFilePath);
46 uvMock = std::make_shared<UvfsMock>();
47 Uvfs::ins = uvMock;
48 }
49
TearDownTestCase(void)50 void MkdirCoreMockTest::TearDownTestCase(void)
51 {
52 GTEST_LOG_(INFO) << "TearDownTestCase";
53 filesystem::remove_all(tempFilePath);
54 Uvfs::ins = nullptr;
55 uvMock = nullptr;
56 }
57
SetUp(void)58 void MkdirCoreMockTest::SetUp(void)
59 {
60 GTEST_LOG_(INFO) << "SetUp";
61 }
62
TearDown(void)63 void MkdirCoreMockTest::TearDown(void)
64 {
65 GTEST_LOG_(INFO) << "TearDown";
66 }
67
68 /**
69 * @tc.name: MkdirCoreMockTest_DoMkdir_0001
70 * @tc.desc: Test function of DoMkdir() interface for SUCCESS.
71 * @tc.size: MEDIUM
72 * @tc.type: FUNC
73 * @tc.level Level 1
74 */
75 HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0001, testing::ext::TestSize.Level1)
76 {
77 GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0001";
78
79 EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(0));
80
81 string path = tempFilePath.string() + "/test01";
82 auto ret = MkdirCore::DoMkdir(path);
83 EXPECT_EQ(ret.IsSuccess(), true);
84
85 GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0001";
86 }
87
88 /**
89 * @tc.name: MkdirCoreMockTest_DoMkdir_0002
90 * @tc.desc: Test function of DoMkdir() interface for SUCCESS.
91 * @tc.size: MEDIUM
92 * @tc.type: FUNC
93 * @tc.level Level 1
94 */
95 HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0002, testing::ext::TestSize.Level1)
96 {
97 GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0002";
98
99 EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(-2)).WillOnce(Return(0));
100
101 string path = tempFilePath.string() + "/test02/testDir";
102 auto ret = MkdirCore::DoMkdir(path, true);
103 EXPECT_EQ(ret.IsSuccess(), true);
104
105 GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0002";
106 }
107
108 /**
109 * @tc.name: MkdirCoreMockTest_DoMkdir_0003
110 * @tc.desc: Test function of DoMkdir() interface is FAILED for uv_fs_mkdir return 1.
111 * @tc.size: MEDIUM
112 * @tc.type: FUNC
113 * @tc.level Level 1
114 */
115 HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0003, testing::ext::TestSize.Level1)
116 {
117 GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0003";
118
119 EXPECT_CALL(*uvMock, uv_fs_mkdir(_, _, _, _, _)).WillOnce(Return(1));
120
121 string path = tempFilePath.string() + "/test03";
122 auto ret = MkdirCore::DoMkdir(path);
123 EXPECT_EQ(ret.IsSuccess(), false);
124
125 GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0003";
126 }
127
128 /**
129 * @tc.name: MkdirCoreMockTest_DoMkdir_0004
130 * @tc.desc: Test function of DoMkdir() interface is FAILED for file exists.
131 * @tc.size: MEDIUM
132 * @tc.type: FUNC
133 * @tc.level Level 1
134 */
135 HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0004, testing::ext::TestSize.Level1)
136 {
137 GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0004";
138
139 EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(0));
140
141 string path = "/";
142 auto ret = MkdirCore::DoMkdir(path, true);
143 EXPECT_EQ(ret.IsSuccess(), false);
144 auto err = ret.GetError();
145 int errCode = err.GetErrNo();
146 EXPECT_EQ(errCode, 13900015);
147 auto msg = err.GetErrMsg();
148 EXPECT_EQ(msg, "File exists");
149
150 GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0004";
151 }
152
153 /**
154 * @tc.name: MkdirCoreMockTest_DoMkdir_0005
155 * @tc.desc: Test function of DoMkdir() interface is FAILED for no such file or directory.
156 * @tc.size: MEDIUM
157 * @tc.type: FUNC
158 * @tc.level Level 1
159 */
160 HWTEST_F(MkdirCoreMockTest, MkdirCoreMockTest_DoMkdir_0005, testing::ext::TestSize.Level1)
161 {
162 GTEST_LOG_(INFO) << "MkdirCoreMockTest-begin MkdirCoreMockTest_DoMkdir_0005";
163
164 EXPECT_CALL(*uvMock, uv_fs_access(_, _, _, _, _)).WillOnce(Return(2));
165
166 string path = "";
167 auto ret = MkdirCore::DoMkdir(path, true);
168 EXPECT_EQ(ret.IsSuccess(), false);
169 auto err = ret.GetError();
170 int errCode = err.GetErrNo();
171 EXPECT_EQ(errCode, 13900002);
172 auto msg = err.GetErrMsg();
173 EXPECT_EQ(msg, "No such file or directory");
174
175 GTEST_LOG_(INFO) << "MkdirCoreMockTest-end MkdirCoreMockTest_DoMkdir_0005";
176 }
177
178 } // namespace OHOS::FileManagement::ModuleFileIO::Test