• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 using namespace testing::ext;
31 using namespace std;
32 
33 class HatsSemopTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 private:
40 };
SetUp()41 void HatsSemopTest::SetUp()
42 {
43 }
TearDown()44 void HatsSemopTest::TearDown()
45 {
46 }
SetUpTestCase()47 void HatsSemopTest::SetUpTestCase()
48 {
49 }
TearDownTestCase()50 void HatsSemopTest::TearDownTestCase()
51 {
52 }
53 
54 static const int SEMPAHORE_NUM = 1;
55 
56 /*
57  * @tc.number : SUB_KERNEL_SYSCALL_SEMOP_0100
58  * @tc.name   : SemopAddSemvalSuccess_0001
59  * @tc.desc   : semop add semaphore value success.
60  * @tc.size   : MediumTest
61  * @tc.type   : Function
62  * @tc.level  : Level 1
63  */
64 HWTEST_F(HatsSemopTest, SemopAddSemvalSuccess_0001, Function | MediumTest | Level1)
65 {
66     int ret;
67     int semid;
68     struct sembuf sop;
69     sop.sem_op = 1;
70 
71     semid = semget(IPC_PRIVATE, SEMPAHORE_NUM, IPC_CREAT | 0666);
72     EXPECT_TRUE(semid >= 0);
73 
74     ret = semop(semid, &sop, 1);
75     EXPECT_TRUE(ret == 0);
76 }
77 
78 /*
79  * @tc.number : SUB_KERNEL_SYSCALL_SEMOP_0200
80  * @tc.name   : SemopInvalidSemidFail_0002
81  * @tc.desc   : semop add semaphore value of a invalid sephamore id fail.
82  * @tc.size   : MediumTest
83  * @tc.type   : Function
84  * @tc.level  : Level 2
85  */
86 HWTEST_F(HatsSemopTest, SemopInvalidSemidFail_0002, Function | MediumTest | Level2)
87 {
88     int ret;
89     int semid;
90     struct sembuf sop;
91     sop.sem_op = 1;
92     semid = -1;
93 
94     errno = 0;
95     ret = semop(semid, &sop, 1);
96     EXPECT_TRUE(ret == -1);
97     EXPECT_EQ(errno, EINVAL);
98 }
99