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 <string>
19#include <vector>
20#include <dirent.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <gtest/gtest.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include "securec.h"
27
28using namespace testing::ext;
29using namespace std;
30
31class Getdents64ApiTest : public testing::Test {
32public:
33    static void SetUpTestCase();
34    static void TearDownTestCase();
35    void SetUp();
36    void TearDown();
37private:
38};
39void Getdents64ApiTest::SetUp()
40{
41}
42void Getdents64ApiTest::TearDown()
43{
44}
45void Getdents64ApiTest::SetUpTestCase()
46{
47}
48void Getdents64ApiTest::TearDownTestCase()
49{
50}
51
52static const char* TEST_FILE = "/data/local/tmp";
53
54/*
55 * @tc.number : SUB_KERNEL_SYSCALL_GETDENTS64_0100
56 * @tc.name   : Getdents64GetDirInfoSuccess_0001
57 * @tc.desc   : open and get directory entry information success.
58 * @tc.size   : MediumTest
59 * @tc.type   : Function
60 * @tc.level  : Level 1
61 */
62HWTEST_F(Getdents64ApiTest, Getdents64GetDirInfoSuccess_0001, Function | MediumTest | Level1)
63{
64    struct dirent buf = { 0 };
65    int fd = open(TEST_FILE, O_RDONLY);
66    EXPECT_TRUE(fd > 0);
67
68    ssize_t size = getdents64(fd, &buf, sizeof(buf));
69    EXPECT_TRUE(size > 0);
70}
71
72/*
73 * @tc.number : SUB_KERNEL_SYSCALL_GETDENTS64_0200
74 * @tc.name   : Getdents64GetDirectoryEntryInfoFailed_0002
75 * @tc.desc   : get illegal file handle directory entry information failed, errno EBADF.
76 * @tc.size   : MediumTest
77 * @tc.type   : Function
78 * @tc.level  : Level 2
79 */
80HWTEST_F(Getdents64ApiTest, Getdents64GetDirectoryEntryInfoFailed_0002, Function | MediumTest | Level2)
81{
82    struct dirent buf = { 0 };
83    errno = 0;
84    ssize_t size = getdents64(-1, &buf, sizeof(buf));
85    EXPECT_TRUE(size <= 0);
86    EXPECT_EQ(errno, EBADF);
87}