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 <fcntl.h>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <sys/mman.h>
21 #include "securec.h"
22 
23 using namespace testing::ext;
24 using namespace std;
25 
26 static const char *SHMEM_NAME = "shmem_test";
27 
28 class MenFdCreateApiTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34 private:
35 };
SetUp()36 void MenFdCreateApiTest::SetUp()
37 {
38 }
TearDown()39 void MenFdCreateApiTest::TearDown()
40 {
41 }
SetUpTestCase()42 void MenFdCreateApiTest::SetUpTestCase()
43 {
44 }
TearDownTestCase()45 void MenFdCreateApiTest::TearDownTestCase()
46 {
47 }
48 
49 /*
50  * @tc.number : SUB_KERNEL_SYSCALL_MEMFDCREATE_0100
51  * @tc.name   : MemFdCreateSuccess_0001
52  * @tc.desc   : memfd_create create fd success.
53  * @tc.size   : MediumTest
54  * @tc.type   : Function
55  * @tc.level  : Level 1
56  */
HWTEST_F(MenFdCreateApiTest, MenFdCreateSuccess_0001, Function | MediumTest | Level1)57 HWTEST_F(MenFdCreateApiTest, MenFdCreateSuccess_0001, Function | MediumTest | Level1)
58 {
59     int fd = memfd_create(SHMEM_NAME, 0);
60     EXPECT_TRUE(fd > 0);
61     close(fd);
62 
63     fd = memfd_create(SHMEM_NAME, MFD_ALLOW_SEALING);
64     EXPECT_TRUE(fd > 0);
65     close(fd);
66 
67     fd = memfd_create(SHMEM_NAME, MFD_CLOEXEC);
68     EXPECT_TRUE(fd > 0);
69     close(fd);
70 }
71 
72 /*
73  * @tc.number : SUB_KERNEL_SYSCALL_MEMFDCREATE_0200
74  * @tc.name   : MemFdCreateNullptrPathFailed_0002
75  * @tc.desc   : memfd_create with nullptr Failed return EFAULT.
76  * @tc.size   : MediumTest
77  * @tc.type   : Function
78  * @tc.level  : Level 2
79  */
HWTEST_F(MenFdCreateApiTest, MemFdCreateNullptrPathFailed_0002, Function | MediumTest | Level2)80 HWTEST_F(MenFdCreateApiTest, MemFdCreateNullptrPathFailed_0002, Function | MediumTest | Level2)
81 {
82     errno = 0;
83     int fd = memfd_create(nullptr, 0);
84     EXPECT_EQ(fd, -1);
85     EXPECT_EQ(errno, EFAULT);
86     close(fd);
87 }
88 
89 /*
90  * @tc.number : SUB_KERNEL_SYSCALL_MEMFDCREATE_0300
91  * @tc.name   : MemFdCreateInvalidFlagsFailed_0003
92  * @tc.desc   : memfd_create with invalid flags Failed return EFAULT.
93  * @tc.size   : MediumTest
94  * @tc.type   : Function
95  * @tc.level  : Level 2
96  */
HWTEST_F(MenFdCreateApiTest, MemFdCreateInvalidFlagsFailed_0003, Function | MediumTest | Level2)97 HWTEST_F(MenFdCreateApiTest, MemFdCreateInvalidFlagsFailed_0003, Function | MediumTest | Level2)
98 {
99     errno = 0;
100     int fd = memfd_create(SHMEM_NAME, -1);
101     EXPECT_EQ(fd, -1);
102     EXPECT_EQ(errno, EINVAL);
103     close(fd);
104 }