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 HatsMsggetTest : 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 HatsMsggetTest::SetUp()
43 {
44 }
TearDown()45 void HatsMsggetTest::TearDown()
46 {
47 }
SetUpTestCase()48 void HatsMsggetTest::SetUpTestCase()
49 {
50 }
TearDownTestCase()51 void HatsMsggetTest::TearDownTestCase()
52 {
53 }
54 
55 /*
56  * @tc.number : SUB_KERNEL_SYSCALL_MSGGET_0100
57  * @tc.name   : MsggetCreateMessageQueueSuccess_0001
58  * @tc.desc   : msgget create a message queue success.
59  * @tc.size   : MediumTest
60  * @tc.type   : Function
61  * @tc.level  : Level 1
62  */
HWTEST_F(HatsMsggetTest, MsggetCreateMessageQueueSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(HatsMsggetTest, MsggetCreateMessageQueueSuccess_0001, Function | MediumTest | Level1)
64 {
65     // create IPC_PRIVATE ipc
66     int msgId = msgget(IPC_PRIVATE, IPC_CREAT | 0777);
67     EXPECT_TRUE(msgId >= 0);
68     int ret = msgctl(msgId, IPC_RMID, nullptr);
69     EXPECT_EQ(ret, 0);
70 
71     // create common ipc
72     key_t key = 1234;
73     msgId = msgget(key, IPC_CREAT | 0777);
74     EXPECT_TRUE(msgId >= 0);
75     ret = msgctl(msgId, IPC_RMID, nullptr);
76     EXPECT_EQ(ret, 0);
77 }
78 
79 /*
80  * @tc.number : SUB_KERNEL_SYSCALL_MSGGET_0200
81  * @tc.name   : MsggetTwiceFailed_0002
82  * @tc.desc   : msgget create a message queue twice failed, errno EEXIST.
83  * @tc.size   : MediumTest
84  * @tc.type   : Function
85  * @tc.level  : Level 2
86  */
HWTEST_F(HatsMsggetTest, MsggetTwiceFailed_0002, Function | MediumTest | Level2)87 HWTEST_F(HatsMsggetTest, MsggetTwiceFailed_0002, Function | MediumTest | Level2)
88 {
89     key_t key = 1234;
90     int msgId = msgget(key, IPC_CREAT | 0777);
91     EXPECT_TRUE(msgId >= 0);
92 
93     errno = 0;
94     int newMsgId = msgget(key, IPC_CREAT | IPC_EXCL | 0777);
95     EXPECT_TRUE(newMsgId == -1);
96     EXPECT_EQ(errno, EEXIST);
97 
98     int ret = msgctl(msgId, IPC_RMID, nullptr);
99     EXPECT_EQ(ret, 0);
100 }
101