• 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 "statvfs_core.h"
17 
18 #include <fcntl.h>
19 #include <gtest/gtest.h>
20 #include <sys/statvfs.h>
21 
22 namespace OHOS::FileManagement::ModuleFileIO::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 
27 class StatvFsCoreTest : public testing::Test {
28 public:
29     static void SetUpTestCase(void);
30     static void TearDownTestCase(void);
31     void SetUp();
32     void TearDown();
33 };
34 
SetUpTestCase(void)35 void StatvFsCoreTest::SetUpTestCase(void)
36 {
37     GTEST_LOG_(INFO) << "SetUpTestCase";
38     int32_t fd = open("/data/test/statvfs_test.txt", O_CREAT | O_RDWR, 0644);
39     if (fd <= 0) {
40         close(fd);
41         ASSERT_TRUE(false);
42     }
43     close(fd);
44 }
45 
TearDownTestCase(void)46 void StatvFsCoreTest::TearDownTestCase(void)
47 {
48     GTEST_LOG_(INFO) << "TearDownTestCase";
49     rmdir("/data/test/statvfs_test.txt");
50 }
51 
SetUp(void)52 void StatvFsCoreTest::SetUp(void)
53 {
54     GTEST_LOG_(INFO) << "SetUp";
55 }
56 
TearDown(void)57 void StatvFsCoreTest::TearDown(void)
58 {
59     GTEST_LOG_(INFO) << "TearDown";
60 }
61 
62 /**
63  * @tc.name: StatvFsCoreTest_DoGetFreeSize_001
64  * @tc.desc: Test function of DoGetFreeSize interface for SUCCESS.
65  * @tc.size: MEDIUM
66  * @tc.type: FUNC
67  * @tc.level Level 1
68  */
69 HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetFreeSize_001, testing::ext::TestSize.Level1)
70 {
71     GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetFreeSize_001";
72 
73     struct statvfs diskInfo;
74     diskInfo.f_bsize = 2;
75     diskInfo.f_bfree = 1;
76 
77     auto result = ModuleStatvfs::StatvfsCore::DoGetFreeSize("/data/test/statvfs_test.txt");
78     EXPECT_EQ(result.IsSuccess(), true);
79 
80     GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetFreeSize_001";
81 }
82 
83 /**
84  * @tc.name: StatvFsCoreTest_DoGetFreeSize_002
85  * @tc.desc: Test function of DoGetFreeSize interface for FALSE.
86  * @tc.size: MEDIUM
87  * @tc.type: FUNC
88  * @tc.level Level 1
89  */
90 HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetFreeSize_002, testing::ext::TestSize.Level1)
91 {
92     GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetFreeSize_002";
93 
94     auto result = ModuleStatvfs::StatvfsCore::DoGetFreeSize("/test/path");
95     EXPECT_EQ(result.IsSuccess(), false);
96     auto err = result.GetError();
97     EXPECT_EQ(err.GetErrNo(), 13900002);
98 
99     GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetFreeSize_002";
100 }
101 
102 /**
103  * @tc.name: StatvFsCoreTest_DoGetTotalSize_003
104  * @tc.desc: Test function of DoGetTotalSize interface for SUCCESS.
105  * @tc.size: MEDIUM
106  * @tc.type: FUNC
107  * @tc.level Level 1
108  */
109 HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetTotalSize_003, testing::ext::TestSize.Level1)
110 {
111     GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetTotalSize_003";
112 
113     struct statvfs diskInfo;
114     diskInfo.f_bsize = 2;
115     diskInfo.f_blocks = 1;
116 
117     auto result = ModuleStatvfs::StatvfsCore::DoGetTotalSize("/data/test/statvfs_test.txt");
118     EXPECT_EQ(result.IsSuccess(), true);
119 
120     GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetTotalSize_003";
121 }
122 
123 /**
124  * @tc.name: StatvFsCoreTest_DoGetTotalSize_004
125  * @tc.desc: Test function of DoGetTotalSize interface for FALSE.
126  * @tc.size: MEDIUM
127  * @tc.type: FUNC
128  * @tc.level Level 1
129  */
130 HWTEST_F(StatvFsCoreTest, StatvFsCoreTest_DoGetTotalSize_004, testing::ext::TestSize.Level1)
131 {
132     GTEST_LOG_(INFO) << "StatvFsCoreTest-begin StatvFsCoreTest_DoGetTotalSize_004";
133 
134     auto result = ModuleStatvfs::StatvfsCore::DoGetTotalSize("/test/path");
135     EXPECT_EQ(result.IsSuccess(), false);
136     auto err = result.GetError();
137     EXPECT_EQ(err.GetErrNo(), 13900002);
138 
139     GTEST_LOG_(INFO) << "StatvFsCoreTest-end StatvFsCoreTest_DoGetTotalSize_004";
140 }
141 
142 }