1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
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 <cstdio>
17 #include <cstdlib>
18 #include <fcntl.h>
19 #include <string>
20 #include <unistd.h>
21 #include <vector>
22 #include <gtest/gtest.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/statfs.h>
26 #include "securec.h"
27
28 using namespace testing::ext;
29 using namespace std;
30
31 class FstatfsApiTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 private:
38 };
SetUp()39 void FstatfsApiTest::SetUp()
40 {
41 }
TearDown()42 void FstatfsApiTest::TearDown()
43 {
44 }
SetUpTestCase()45 void FstatfsApiTest::SetUpTestCase()
46 {
47 }
TearDownTestCase()48 void FstatfsApiTest::TearDownTestCase()
49 {
50 }
51
52 static const char* OPEN_API_TEST_FILE = "/data/local/tmp";
53
54 /*
55 * @tc.number : SUB_KERNEL_SYSCALL_FSTATFS_0100
56 * @tc.name : FstatfsValidFdSuccess_0001
57 * @tc.desc : fstatfs valid fd success.
58 * @tc.size : MediumTest
59 * @tc.type : Function
60 * @tc.level : Level 1
61 */
HWTEST_F(FstatfsApiTest, OpenAndGetStateFileSuccess_0001, Function | MediumTest | Level1)62 HWTEST_F(FstatfsApiTest, OpenAndGetStateFileSuccess_0001, Function | MediumTest | Level1)
63 {
64 int ret;
65 int fd = -1;
66 struct statfs fs = { 0 };
67
68 fd = open(OPEN_API_TEST_FILE, O_RDONLY);
69 EXPECT_TRUE(fd > 0);
70
71 ret = fstatfs(fd, &fs);
72 EXPECT_TRUE(ret == 0);
73 EXPECT_TRUE(fs.f_type > 0);
74
75 close(fd);
76 }
77
78 /*
79 * @tc.number : SUB_KERNEL_SYSCALL_FSTATFS_0200
80 * @tc.name : FstatfsUseInvalidFdFailed_0002
81 * @tc.desc : fstatfs use invalid fd failed, errno EBADF.
82 * @tc.size : MediumTest
83 * @tc.type : Function
84 * @tc.level : Level 2
85 */
HWTEST_F(FstatfsApiTest, FstatfsUseInvalidFdFailed_0002, Function | MediumTest | Level2)86 HWTEST_F(FstatfsApiTest, FstatfsUseInvalidFdFailed_0002, Function | MediumTest | Level2)
87 {
88 int ret;
89 int fd = -1;
90 struct statfs fs = { 0 };
91
92 errno = 0;
93 ret = fstatfs(fd, &fs);
94 EXPECT_EQ(ret, -1);
95 EXPECT_EQ(errno, EBADF);
96
97 close(fd);
98 }
99