• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "truncate_core.h"
17 #include "uv_fs_mock.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace OHOS::FileManagement::ModuleFileIO::Test {
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace std;
25 
26 class TruncateCoreMockTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32     static inline shared_ptr<UvfsMock> uvMock = nullptr;
33 };
34 
SetUpTestCase(void)35 void TruncateCoreMockTest::SetUpTestCase(void)
36 {
37     GTEST_LOG_(INFO) << "SetUpTestCase";
38     uvMock = std::make_shared<UvfsMock>();
39     Uvfs::ins = uvMock;
40 }
41 
TearDownTestCase(void)42 void TruncateCoreMockTest::TearDownTestCase(void)
43 {
44     GTEST_LOG_(INFO) << "TearDownTestCase";
45     Uvfs::ins = nullptr;
46     uvMock = nullptr;
47 }
48 
SetUp(void)49 void TruncateCoreMockTest::SetUp(void)
50 {
51     GTEST_LOG_(INFO) << "SetUp";
52 }
53 
TearDown(void)54 void TruncateCoreMockTest::TearDown(void)
55 {
56     GTEST_LOG_(INFO) << "TearDown";
57 }
58 
59 /**
60  * @tc.name: TruncateCoreMockTest_DoTruncate_001
61  * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed.
62  * @tc.size: MEDIUM
63  * @tc.type: FUNC
64  * @tc.level Level 1
65  */
66 HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_001, testing::ext::TestSize.Level1)
67 {
68     GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_001";
69 
70     FileInfo fileInfo;
71     fileInfo.isPath = true;
72     fileInfo.fdg = std::make_unique<DistributedFS::FDGuard>(1);
73 
74     EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(-1));
75     auto res = TruncateCore::DoTruncate(fileInfo);
76     EXPECT_EQ(res.IsSuccess(), false);
77 
78     GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_001";
79 }
80 
81 /**
82  * @tc.name: TruncateCoreMockTest_DoTruncate_002
83  * @tc.desc: Test function of RmdirCore::DoTruncate interface for Failed.
84  * @tc.size: MEDIUM
85  * @tc.type: FUNC
86  * @tc.level Level 1
87  */
88 HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_002, testing::ext::TestSize.Level1)
89 {
90     GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_002";
91 
92     FileInfo fileInfo;
93     fileInfo.isPath = true;
94     fileInfo.fdg = std::make_unique<DistributedFS::FDGuard>(1);
95 
96     EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1));
97     EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1));
98     auto res = TruncateCore::DoTruncate(fileInfo);
99     EXPECT_EQ(res.IsSuccess(), false);
100 
101     GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_002";
102 }
103 
104 /**
105  * @tc.name: TruncateCoreMockTest_DoTruncate_003
106  * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS.
107  * @tc.size: MEDIUM
108  * @tc.type: FUNC
109  * @tc.level Level 1
110  */
111 HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_003, testing::ext::TestSize.Level1)
112 {
113     GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_003";
114 
115     FileInfo fileInfo;
116     fileInfo.isPath = true;
117     fileInfo.fdg = std::make_unique<DistributedFS::FDGuard>(1);
118 
119     EXPECT_CALL(*uvMock, uv_fs_open(_, _, _, _, _, _)).WillOnce(Return(1));
120     EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1));
121     auto res = TruncateCore::DoTruncate(fileInfo);
122     EXPECT_EQ(res.IsSuccess(), true);
123 
124     GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_003";
125 }
126 
127 /**
128  * @tc.name: TruncateCoreMockTest_DoTruncate_004
129  * @tc.desc: Test function of RmdirCore::DoTruncate interface for FALSE.
130  * @tc.size: MEDIUM
131  * @tc.type: FUNC
132  * @tc.level Level 1
133  */
134 HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_004, testing::ext::TestSize.Level1)
135 {
136     GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_004";
137 
138     FileInfo fileInfo;
139     fileInfo.fdg = std::make_unique<DistributedFS::FDGuard>(1);
140 
141     EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(-1));
142     auto res = TruncateCore::DoTruncate(fileInfo);
143     EXPECT_EQ(res.IsSuccess(), false);
144 
145     GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_004";
146 }
147 
148 /**
149  * @tc.name: TruncateCoreMockTest_DoTruncate_005
150  * @tc.desc: Test function of RmdirCore::DoTruncate interface for SUCCESS.
151  * @tc.size: MEDIUM
152  * @tc.type: FUNC
153  * @tc.level Level 1
154  */
155 HWTEST_F(TruncateCoreMockTest, TruncateCoreMockTest_DoTruncate_005, testing::ext::TestSize.Level1)
156 {
157     GTEST_LOG_(INFO) << "TruncateCoreMockTest-begin TruncateCoreMockTest_DoTruncate_005";
158 
159     FileInfo fileInfo;
160     fileInfo.fdg = std::make_unique<DistributedFS::FDGuard>(1);
161 
162     EXPECT_CALL(*uvMock, uv_fs_ftruncate(_, _, _, _, _)).WillOnce(Return(1));
163     auto res = TruncateCore::DoTruncate(fileInfo);
164     EXPECT_EQ(res.IsSuccess(), true);
165 
166     GTEST_LOG_(INFO) << "TruncateCoreMockTest-end TruncateCoreMockTest_DoTruncate_005";
167 }
168 
169 } // namespace OHOS::FileManagement::ModuleFileIO::Test