19762338dSopenharmony_ci/* 29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization. 39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49762338dSopenharmony_ci * you may not use this file except in compliance with the License. 59762338dSopenharmony_ci * You may obtain a copy of the License at 69762338dSopenharmony_ci * 79762338dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89762338dSopenharmony_ci * 99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129762338dSopenharmony_ci * See the License for the specific language governing permissions and 139762338dSopenharmony_ci * limitations under the License. 149762338dSopenharmony_ci */ 159762338dSopenharmony_ci 169762338dSopenharmony_ci#include <cstdio> 179762338dSopenharmony_ci#include <cstdlib> 189762338dSopenharmony_ci#include <fcntl.h> 199762338dSopenharmony_ci#include <string> 209762338dSopenharmony_ci#include <unistd.h> 219762338dSopenharmony_ci#include <vector> 229762338dSopenharmony_ci#include <gtest/gtest.h> 239762338dSopenharmony_ci#include <sys/stat.h> 249762338dSopenharmony_ci#include <sys/types.h> 259762338dSopenharmony_ci#include <sys/ipc.h> 269762338dSopenharmony_ci#include <sys/shm.h> 279762338dSopenharmony_ci#include "securec.h" 289762338dSopenharmony_ci 299762338dSopenharmony_ciusing namespace testing::ext; 309762338dSopenharmony_ciusing namespace std; 319762338dSopenharmony_ci 329762338dSopenharmony_ciclass ShmgetApiTest : public testing::Test { 339762338dSopenharmony_cipublic: 349762338dSopenharmony_ci static void SetUpTestCase(); 359762338dSopenharmony_ci static void TearDownTestCase(); 369762338dSopenharmony_ci void SetUp(); 379762338dSopenharmony_ci void TearDown(); 389762338dSopenharmony_ciprivate: 399762338dSopenharmony_ci}; 409762338dSopenharmony_civoid ShmgetApiTest::SetUp() 419762338dSopenharmony_ci{ 429762338dSopenharmony_ci} 439762338dSopenharmony_civoid ShmgetApiTest::TearDown() 449762338dSopenharmony_ci{ 459762338dSopenharmony_ci} 469762338dSopenharmony_civoid ShmgetApiTest::SetUpTestCase() 479762338dSopenharmony_ci{ 489762338dSopenharmony_ci} 499762338dSopenharmony_civoid ShmgetApiTest::TearDownTestCase() 509762338dSopenharmony_ci{ 519762338dSopenharmony_ci} 529762338dSopenharmony_ci 539762338dSopenharmony_ciconst int MEM_SIZE = 1024; 549762338dSopenharmony_ci 559762338dSopenharmony_ci/* 569762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SHMGET_0100 579762338dSopenharmony_ci * @tc.name : ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0001 589762338dSopenharmony_ci * @tc.desc : Create a shared memory object and return the shared memory identifier success. 599762338dSopenharmony_ci * @tc.size : MediumTest 609762338dSopenharmony_ci * @tc.type : Function 619762338dSopenharmony_ci * @tc.level : Level 1 629762338dSopenharmony_ci */ 639762338dSopenharmony_ciHWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0001, Function | MediumTest | Level1) 649762338dSopenharmony_ci{ 659762338dSopenharmony_ci key_t key = ftok("shmfile", 65); 669762338dSopenharmony_ci int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT); 679762338dSopenharmony_ci EXPECT_TRUE(shmid != 0); 689762338dSopenharmony_ci 699762338dSopenharmony_ci int ret = shmctl(shmid, IPC_RMID, nullptr); 709762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 719762338dSopenharmony_ci} 729762338dSopenharmony_ci 739762338dSopenharmony_ci/* 749762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SHMGET_0200 759762338dSopenharmony_ci * @tc.name : ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0002 769762338dSopenharmony_ci * @tc.desc : Create a shared memory object and return the shared memory identifier success. 779762338dSopenharmony_ci * @tc.size : MediumTest 789762338dSopenharmony_ci * @tc.type : Function 799762338dSopenharmony_ci * @tc.level : Level 1 809762338dSopenharmony_ci */ 819762338dSopenharmony_ciHWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0002, Function | MediumTest | Level1) 829762338dSopenharmony_ci{ 839762338dSopenharmony_ci char* ptrSharedData = nullptr; 849762338dSopenharmony_ci const char* data = "Hello, shared memory!"; 859762338dSopenharmony_ci key_t key = ftok("shmfile", 66); 869762338dSopenharmony_ci 879762338dSopenharmony_ci int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT); 889762338dSopenharmony_ci EXPECT_TRUE(shmid != 0); 899762338dSopenharmony_ci 909762338dSopenharmony_ci void* ptrData = shmat(shmid, nullptr, 0); 919762338dSopenharmony_ci EXPECT_TRUE(ptrData != reinterpret_cast<void*>(-1)); 929762338dSopenharmony_ci 939762338dSopenharmony_ci ptrSharedData = (char*)ptrData; 949762338dSopenharmony_ci strncpy_s(ptrSharedData, MEM_SIZE, data, strlen(data)); 959762338dSopenharmony_ci ptrSharedData[strlen(data)] = '\0'; 969762338dSopenharmony_ci 979762338dSopenharmony_ci int ret = shmdt(ptrSharedData); 989762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 999762338dSopenharmony_ci 1009762338dSopenharmony_ci ret = shmctl(shmid, IPC_RMID, nullptr); 1019762338dSopenharmony_ci EXPECT_TRUE(ret == 0); 1029762338dSopenharmony_ci} 1039762338dSopenharmony_ci 1049762338dSopenharmony_ci/* 1059762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SHMGET_0300 1069762338dSopenharmony_ci * @tc.name : ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0003 1079762338dSopenharmony_ci * @tc.desc : Create a shared memory object and return the shared memory identifier success. 1089762338dSopenharmony_ci * @tc.size : MediumTest 1099762338dSopenharmony_ci * @tc.type : Function 1109762338dSopenharmony_ci * @tc.level : Level 1 1119762338dSopenharmony_ci */ 1129762338dSopenharmony_ciHWTEST_F(ShmgetApiTest, ShmgetCreateSharedMemoryObjectAndReturnIdentifierSuccess_0003, Function | MediumTest | Level1) 1139762338dSopenharmony_ci{ 1149762338dSopenharmony_ci key_t key1 = ftok("shmfile1", 65); 1159762338dSopenharmony_ci key_t key2 = ftok("shmfile2", 65); 1169762338dSopenharmony_ci 1179762338dSopenharmony_ci int shmid1 = shmget(key1, MEM_SIZE, IPC_CREAT | 0666); 1189762338dSopenharmony_ci EXPECT_TRUE(shmid1 != -1); 1199762338dSopenharmony_ci 1209762338dSopenharmony_ci int shmid2 = shmget(key2, MEM_SIZE, IPC_CREAT | 0666); 1219762338dSopenharmony_ci EXPECT_TRUE(shmid2 != -1); 1229762338dSopenharmony_ci 1239762338dSopenharmony_ci EXPECT_TRUE(shmid1 == shmid2); 1249762338dSopenharmony_ci 1259762338dSopenharmony_ci if (shmid1 != -1) { 1269762338dSopenharmony_ci shmctl(shmid1, IPC_RMID, nullptr); 1279762338dSopenharmony_ci } 1289762338dSopenharmony_ci 1299762338dSopenharmony_ci if (shmid2 != -1) { 1309762338dSopenharmony_ci shmctl(shmid2, IPC_RMID, nullptr); 1319762338dSopenharmony_ci } 1329762338dSopenharmony_ci}