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