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/ipc.h>
26 #include <sys/shm.h>
27 #include "securec.h"
28 
29 using namespace testing::ext;
30 using namespace std;
31 
32 class ShmgetApiTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 private:
39 };
SetUp()40 void ShmgetApiTest::SetUp()
41 {
42 }
TearDown()43 void ShmgetApiTest::TearDown()
44 {
45 }
SetUpTestCase()46 void ShmgetApiTest::SetUpTestCase()
47 {
48 }
TearDownTestCase()49 void ShmgetApiTest::TearDownTestCase()
50 {
51 }
52 
53 const int MEM_SIZE = 1024;
54 
55 /*
56  * @tc.number : SUB_KERNEL_SYSCALL_SHMGET_0100
57  * @tc.name   : ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0001
58  * @tc.desc   : Create a shared memory object and return the shared memory identifier success.
59  * @tc.size   : MediumTest
60  * @tc.type   : Function
61  * @tc.level  : Level 1
62  */
HWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0001, Function | MediumTest | Level1)
64 {
65     key_t key = ftok("shmfile", 65);
66     int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT);
67     EXPECT_TRUE(shmid != 0);
68 
69     int ret = shmctl(shmid, IPC_RMID, nullptr);
70     EXPECT_TRUE(ret == 0);
71 }
72 
73 /*
74  * @tc.number : SUB_KERNEL_SYSCALL_SHMGET_0200
75  * @tc.name   : ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0002
76  * @tc.desc   : Create a shared memory object and return the shared memory identifier success.
77  * @tc.size   : MediumTest
78  * @tc.type   : Function
79  * @tc.level  : Level 1
80  */
HWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0002, Function | MediumTest | Level1)81 HWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0002, Function | MediumTest | Level1)
82 {
83     char* ptrSharedData = nullptr;
84     const char* data = "Hello, shared memory!";
85     key_t key = ftok("shmfile", 66);
86 
87     int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT);
88     EXPECT_TRUE(shmid != 0);
89 
90     void* ptrData = shmat(shmid, nullptr, 0);
91     EXPECT_TRUE(ptrData != reinterpret_cast<void*>(-1));
92 
93     ptrSharedData = (char*)ptrData;
94     strncpy_s(ptrSharedData, MEM_SIZE, data, strlen(data));
95     ptrSharedData[strlen(data)] = '\0';
96 
97     int ret = shmdt(ptrSharedData);
98     EXPECT_TRUE(ret == 0);
99 
100     ret = shmctl(shmid, IPC_RMID, nullptr);
101     EXPECT_TRUE(ret == 0);
102 }
103 
104 /*
105  * @tc.number : SUB_KERNEL_SYSCALL_SHMGET_0300
106  * @tc.name   : ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0003
107  * @tc.desc   : Create a shared memory object and return the shared memory identifier success.
108  * @tc.size   : MediumTest
109  * @tc.type   : Function
110  * @tc.level  : Level 1
111  */
HWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0003, Function | MediumTest | Level1)112 HWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0003, Function | MediumTest | Level1)
113 {
114     key_t key1 = ftok("shmfile1", 65);
115     key_t key2 = ftok("shmfile2", 65);
116 
117     int shmid1 = shmget(key1, MEM_SIZE, IPC_CREAT | 0666);
118     EXPECT_TRUE(shmid1 != -1);
119 
120     int shmid2 = shmget(key2, MEM_SIZE, IPC_CREAT | 0666);
121     EXPECT_TRUE(shmid2 != -1);
122 
123     EXPECT_TRUE(shmid1 == shmid2);
124 
125     if (shmid1 != -1) {
126         shmctl(shmid1, IPC_RMID, nullptr);
127     }
128 
129     if (shmid2 != -1) {
130         shmctl(shmid2, IPC_RMID, nullptr);
131     }
132 }