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 <cerrno> 17#include <cstdio> 18#include <cstdlib> 19#include <string> 20#include <vector> 21#include <fcntl.h> 22#include <unistd.h> 23#include <gtest/gtest.h> 24#include <sys/ipc.h> 25#include <sys/stat.h> 26#include <sys/sem.h> 27#include <sys/types.h> 28#include "securec.h" 29 30using namespace testing::ext; 31using namespace std; 32 33class HatsSemgetTest : public testing::Test { 34public: 35 static void SetUpTestCase(); 36 static void TearDownTestCase(); 37 void SetUp(); 38 void TearDown(); 39private: 40}; 41void HatsSemgetTest::SetUp() 42{ 43} 44void HatsSemgetTest::TearDown() 45{ 46} 47void HatsSemgetTest::SetUpTestCase() 48{ 49} 50void HatsSemgetTest::TearDownTestCase() 51{ 52} 53 54static const int SEMPAHORE_NUM = 1; 55 56/* 57 * @tc.number : SUB_KERNEL_SYSCALL_SEMGET_0100 58 * @tc.name : SemgetCreateSemaphoreSuccess_0001 59 * @tc.desc : semget create a semaphore set success. 60 * @tc.size : MediumTest 61 * @tc.type : Function 62 * @tc.level : Level 1 63 */ 64HWTEST_F(HatsSemgetTest, SemgetCreateSemaphoreSuccess_0001, Function | MediumTest | Level1) 65{ 66 int semid = semget(IPC_PRIVATE, SEMPAHORE_NUM, IPC_CREAT | 0777); 67 EXPECT_TRUE(semid >= 0); 68 69 int ret = semctl(semid, SEMPAHORE_NUM, IPC_RMID); 70 EXPECT_TRUE(ret == 0); 71} 72 73/* 74 * @tc.number : SUB_KERNEL_SYSCALL_SEMGET_0200 75 * @tc.name : SemgetCreateSempahoreSetUsingSameKeyFail_0002 76 * @tc.desc : semget create a sempahore set using a valid but used key fail. 77 * @tc.size : MediumTest 78 * @tc.type : Function 79 * @tc.level : Level 2 80 */ 81HWTEST_F(HatsSemgetTest, SemgetCreateSempahoreSetUsingSameKeyFail_0002, Function | MediumTest | Level2) 82{ 83 int semid1; 84 int semid2; 85 int key = 1234; 86 87 semid1 = semget(key, SEMPAHORE_NUM, IPC_CREAT | 0777); 88 EXPECT_TRUE(semid1 >= 0); 89 errno = 0; 90 semid2 = semget(key, SEMPAHORE_NUM, IPC_CREAT | IPC_EXCL | 0777); 91 EXPECT_TRUE(semid2 == -1); 92 EXPECT_EQ(errno, EEXIST); 93 94 int ret = semctl(semid1, SEMPAHORE_NUM, IPC_RMID); 95 EXPECT_TRUE(ret == 0); 96} 97 98/* 99 * @tc.number : SUB_KERNEL_SYSCALL_SEMGET_0300 100 * @tc.name : SemgetInvalidSemnumFail_0003 101 * @tc.desc : semget create a sempahore set with invalid semaphore number fail. 102 * @tc.size : MediumTest 103 * @tc.type : Function 104 * @tc.level : Level 2 105 */ 106HWTEST_F(HatsSemgetTest, SemgetInvalidSemnumFail_0003, Function | MediumTest | Level2) 107{ 108 errno = 0; 109 int semid = semget(IPC_PRIVATE, -1, IPC_CREAT | 0777); 110 EXPECT_TRUE(semid == -1); 111 EXPECT_EQ(errno, EINVAL); 112} 113