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 union Semun {
34 int val;
35 struct semid_ds *buf;
36 unsigned short *arry;
37 };
38
39 static const int MAX_LEN = 1024;
40
41 class HatsSemctlTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 private:
48 };
SetUp()49 void HatsSemctlTest::SetUp()
50 {
51 }
TearDown()52 void HatsSemctlTest::TearDown()
53 {
54 }
SetUpTestCase()55 void HatsSemctlTest::SetUpTestCase()
56 {
57 }
TearDownTestCase()58 void HatsSemctlTest::TearDownTestCase()
59 {
60 }
61
62 static const int SEMPAHORE_NUM = 1;
63
64 /*
65 * @tc.number : SUB_KERNEL_SYSCALL_SEMCTL_0100
66 * @tc.name : SemctlCmdTestSuccess_0001
67 * @tc.desc : semctl command test success.
68 * @tc.size : MediumTest
69 * @tc.type : Function
70 * @tc.level : Level 1
71 */
HWTEST_F(HatsSemctlTest, SemctlCmdTestSuccess_0001, Function | MediumTest | Level1)72 HWTEST_F(HatsSemctlTest, SemctlCmdTestSuccess_0001, Function | MediumTest | Level1)
73 {
74 union Semun sem = {
75 .val = 0,
76 };
77 char buffer[MAX_LEN] = { 0 };
78
79 int semid = semget(IPC_PRIVATE, SEMPAHORE_NUM, IPC_CREAT | 0777);
80 EXPECT_TRUE(semid >= 0);
81
82 int ret = semctl(semid, SEMPAHORE_NUM, IPC_SET, &sem);
83 EXPECT_TRUE(ret == 0);
84
85 ret = semctl(semid, SEMPAHORE_NUM, IPC_STAT, &buffer);
86 EXPECT_TRUE(ret == 0);
87
88 ret = semctl(semid, SEMPAHORE_NUM, IPC_RMID);
89 EXPECT_TRUE(ret == 0);
90 }
91
92 /*
93 * @tc.number : SUB_KERNEL_SYSCALL_SEMCTL_0200
94 * @tc.name : SemctlUseInvalidIdFail_0002
95 * @tc.desc : semctl use valid id fail.
96 * @tc.size : MediumTest
97 * @tc.type : Function
98 * @tc.level : Level 2
99 */
HWTEST_F(HatsSemctlTest, SemctlUseInvalidIdFail_0002, Function | MediumTest | Level2)100 HWTEST_F(HatsSemctlTest, SemctlUseInvalidIdFail_0002, Function | MediumTest | Level2)
101 {
102 union Semun sem = {
103 .val = 0,
104 };
105 errno = 0;
106 int ret = semctl(-1, SEMPAHORE_NUM, IPC_STAT, &sem);
107 EXPECT_EQ(ret, -1);
108 EXPECT_EQ(errno, EINVAL);
109 }
110
111 /*
112 * @tc.number : SUB_KERNEL_SYSCALL_SEMCTL_0300
113 * @tc.name : SemctlUsdInvalidCmdFail_0003
114 * @tc.desc : semctl use valid cmd fail.
115 * @tc.size : MediumTest
116 * @tc.type : Function
117 * @tc.level : Level 2
118 */
HWTEST_F(HatsSemctlTest, SemctlUsdInvalidCmdFail_0003, Function | MediumTest | Level2)119 HWTEST_F(HatsSemctlTest, SemctlUsdInvalidCmdFail_0003, Function | MediumTest | Level2)
120 {
121 int semid = semget(IPC_PRIVATE, SEMPAHORE_NUM, IPC_CREAT | 0777);
122 EXPECT_TRUE(semid >= 0);
123
124 errno = 0;
125 int ret = semctl(semid, SEMPAHORE_NUM, 999);
126 EXPECT_EQ(ret, -1);
127 EXPECT_EQ(errno, EINVAL);
128
129 ret = semctl(semid, SEMPAHORE_NUM, IPC_RMID);
130 EXPECT_TRUE(ret == 0);
131 }
132