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 "securec.h"
26
27using namespace testing::ext;
28using namespace std;
29
30class FstatApiTest : public testing::Test {
31public:
32    static void SetUpTestCase();
33    static void TearDownTestCase();
34    void SetUp();
35    void TearDown();
36private:
37};
38void FstatApiTest::SetUp()
39{
40}
41void FstatApiTest::TearDown()
42{
43}
44void FstatApiTest::SetUpTestCase()
45{
46}
47void FstatApiTest::TearDownTestCase()
48{
49}
50
51static const char *TEST_FILE = "/data/local/tmp/fstat.txt";
52mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
53
54/*
55 * @tc.number : SUB_KERNEL_SYSCALL_FSTAT_0100
56 * @tc.name   : FstatValidFdSuccess_0001
57 * @tc.desc   : fstat a valid fd success.
58 * @tc.size   : MediumTest
59 * @tc.type   : Function
60 * @tc.level  : Level 1
61 */
62HWTEST_F(FstatApiTest, FstatValidFdSuccess_0001, Function | MediumTest | Level1)
63{
64    struct stat stat = { 0 };
65    int fd = open(TEST_FILE, O_RDWR | O_CREAT, MODE_0644);
66    EXPECT_TRUE(fd > 0);
67
68    int ret = fstat(fd, &stat);
69    EXPECT_EQ(ret, 0);
70
71    close(fd);
72    remove(TEST_FILE);
73}
74
75/*
76 * @tc.number : SUB_KERNEL_SYSCALL_FSTAT_0200
77 * @tc.name   : FstatInvalidFdFailed_0002
78 * @tc.desc   : fstat invalid fd fail, errno EBADF.
79 * @tc.size   : MediumTest
80 * @tc.type   : Function
81 * @tc.level  : Level 2
82 */
83HWTEST_F(FstatApiTest, FstatInvalidFdFailed_0002, Function | MediumTest | Level2)
84{
85    struct stat stat = { 0 };
86    errno = 0;
87    int ret = fstat(-1, &stat);
88    EXPECT_EQ(ret, -1);
89    EXPECT_EQ(errno, EBADF);
90}
91