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/msg.h>
26 #include <sys/stat.h>
27 #include <sys/sem.h>
28 #include <sys/types.h>
29 #include "securec.h"
30 
31 using namespace testing::ext;
32 using namespace std;
33 
34 class HatsMsgctlTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 private:
41 };
SetUp()42 void HatsMsgctlTest::SetUp()
43 {
44 }
TearDown()45 void HatsMsgctlTest::TearDown()
46 {
47 }
SetUpTestCase()48 void HatsMsgctlTest::SetUpTestCase()
49 {
50 }
TearDownTestCase()51 void HatsMsgctlTest::TearDownTestCase()
52 {
53 }
54 
55 /*
56  * @tc.number : SUB_KERNEL_SYSCALL_MSGCTL_0100
57  * @tc.name   : MsgctlGetStatusSuccess_0001
58  * @tc.desc   : msgctl get message queue status success.
59  * @tc.size   : MediumTest
60  * @tc.type   : Function
61  * @tc.level  : Level 1
62  */
HWTEST_F(HatsMsgctlTest, MsgctlGetStatusSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(HatsMsgctlTest, MsgctlGetStatusSuccess_0001, Function | MediumTest | Level1)
64 {
65     struct msqid_ds msqBuf;
66 
67     int msqid = msgget(IPC_PRIVATE, O_CREAT | 0777);
68     EXPECT_TRUE(msqid >= 0);
69 
70     int ret = msgctl(msqid, IPC_STAT, &msqBuf);
71     EXPECT_EQ(ret, 0);
72 
73     ret = msgctl(msqid, IPC_SET, &msqBuf);
74     EXPECT_EQ(ret, 0);
75 
76     ret = msgctl(msqid, IPC_RMID, nullptr);
77     EXPECT_EQ(ret, 0);
78 }
79 
80 /*
81  * @tc.number : SUB_KERNEL_SYSCALL_MSGCTL_0200
82  * @tc.name   : MsgctlInvalidMsqidFail_0002
83  * @tc.desc   : msgctl perform control operation on an invalid message queue id fail.
84  * @tc.size   : MediumTest
85  * @tc.type   : Function
86  * @tc.level  : Level 2
87  */
HWTEST_F(HatsMsgctlTest, MsgctlInvalidMsqidFail_0002, Function | MediumTest | Level2)88 HWTEST_F(HatsMsgctlTest, MsgctlInvalidMsqidFail_0002, Function | MediumTest | Level2)
89 {
90     int ret;
91     int msqid = -1;
92     struct msqid_ds msqBuf;
93 
94     errno = 0;
95     ret = msgctl(msqid, IPC_STAT, &msqBuf);
96     EXPECT_TRUE(ret == -1);
97     EXPECT_EQ(errno, EINVAL);
98 
99     errno = 0;
100     ret = msgctl(msqid, IPC_RMID, nullptr);
101     EXPECT_TRUE(ret == -1);
102     EXPECT_EQ(errno, EINVAL);
103 }
104 
105 /*
106  * @tc.number : SUB_KERNEL_SYSCALL_MSGCTL_0300
107  * @tc.name   : MsgctlInvalidCmdFail_0003
108  * @tc.desc   : msgctl perform invalid command failed, errno EINVAL.
109  * @tc.size   : MediumTest
110  * @tc.type   : Function
111  * @tc.level  : Level 2
112  */
HWTEST_F(HatsMsgctlTest, MsgctlInvalidOperationFail_0003, Function | MediumTest | Level2)113 HWTEST_F(HatsMsgctlTest, MsgctlInvalidOperationFail_0003, Function | MediumTest | Level2)
114 {
115     int ret;
116     int msqid;
117     int cmd = -1;
118     struct msqid_ds msqBuf;
119 
120     msqid = msgget(IPC_PRIVATE, O_CREAT | 0777);
121     EXPECT_TRUE(msqid >= 0);
122 
123     errno = 0;
124     ret = msgctl(msqid, cmd, &msqBuf);
125     EXPECT_TRUE(ret == -1);
126     EXPECT_EQ(errno, EINVAL);
127 
128     ret = msgctl(msqid, IPC_RMID, nullptr);
129     EXPECT_EQ(ret, 0);
130 }