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 <unistd.h>
20 #include <gtest/gtest.h>
21 #include <sys/mman.h>
22 #include "securec.h"
23
24 using namespace testing::ext;
25 using namespace std;
26
27 static const char *FILE_NAME = "shmem_file";
28
29 class DupApiTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 private:
36 };
SetUp()37 void DupApiTest::SetUp()
38 {
39 }
TearDown()40 void DupApiTest::TearDown()
41 {
42 }
SetUpTestCase()43 void DupApiTest::SetUpTestCase()
44 {
45 }
TearDownTestCase()46 void DupApiTest::TearDownTestCase()
47 {
48 }
49
50 /*
51 * @tc.number : SUB_KERNEL_SYSCALL_DUP_0100
52 * @tc.name : DupFileFdSuccess_0001
53 * @tc.desc : dup file fd success.
54 * @tc.size : MediumTest
55 * @tc.type : Function
56 * @tc.level : Level 1
57 */
HWTEST_F(DupApiTest, DupFileFdSuccess_0001, Function | MediumTest | Level1)58 HWTEST_F(DupApiTest, DupFileFdSuccess_0001, Function | MediumTest | Level1)
59 {
60 int fd = -1;
61 int newFd = -1;
62
63 fd = memfd_create(FILE_NAME, 0);
64 newFd = dup(fd);
65 EXPECT_TRUE(newFd >= 0);
66
67 close(newFd);
68 close(fd);
69 }
70
71 /*
72 * @tc.number : SUB_KERNEL_SYSCALL_DUP_0200
73 * @tc.name : DupInvalidFdFailed_0002
74 * @tc.desc : dup fd -1 failed, errno EBADF.
75 * @tc.size : MediumTest
76 * @tc.type : Function
77 * @tc.level : Level 2
78 */
HWTEST_F(DupApiTest, DupInvalidFdFailed_0002, Function | MediumTest | Level2)79 HWTEST_F(DupApiTest, DupInvalidFdFailed_0002, Function | MediumTest | Level2)
80 {
81 int fd = -1;
82 int newFd = -1;
83 errno = 0;
84 newFd = dup(fd);
85 EXPECT_EQ(newFd, -1);
86 EXPECT_EQ(errno, EBADF);
87 }