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
31using namespace testing::ext;
32using namespace std;
33
34static const int MAX_LEN = 1024;
35static const int MSG_TYPE = 1;
36
37struct MsgBuf {
38    long type;
39    char text[MAX_LEN];
40};
41
42class HatsMsgHandleTest : public testing::Test {
43public:
44    static void SetUpTestCase();
45    static void TearDownTestCase();
46    void SetUp();
47    void TearDown();
48private:
49};
50void HatsMsgHandleTest::SetUp()
51{
52}
53void HatsMsgHandleTest::TearDown()
54{
55}
56void HatsMsgHandleTest::SetUpTestCase()
57{
58}
59void HatsMsgHandleTest::TearDownTestCase()
60{
61}
62
63/*
64 * @tc.number : SUB_KERNEL_SYSCALL_MSG_HANDLE_0100
65 * @tc.name   : MsgsndSendValueAndMsgrcvSuccess_0001
66 * @tc.desc   : msgsnd send message queue and msgrcv message success.
67 * @tc.size   : MediumTest
68 * @tc.type   : Function
69 * @tc.level  : Level 1
70 */
71HWTEST_F(HatsMsgHandleTest, MsgsndSendValueAndMsgrcvSuccess_0001, Function | MediumTest | Level1)
72{
73    int ret;
74    int msqid;
75
76    struct MsgBuf buffer = {
77        .type = MSG_TYPE,
78        .text = "Hello, world!",
79    };
80
81    msqid = msgget(IPC_PRIVATE, IPC_CREAT | 0777);
82    EXPECT_TRUE(msqid >= 0);
83
84    ret = msgsnd(msqid, &buffer, MAX_LEN, IPC_NOWAIT);
85    EXPECT_EQ(ret, 0);
86
87    memset_s(&buffer, sizeof(buffer), 0, sizeof(buffer));
88
89    ret = msgrcv(msqid, &buffer, MAX_LEN, MSG_TYPE, IPC_NOWAIT);
90    EXPECT_TRUE(ret >= 0);
91    EXPECT_STREQ(buffer.text, "Hello, world!");
92
93    ret = msgctl(msqid, IPC_RMID, nullptr);
94    EXPECT_EQ(ret, 0);
95}
96
97/*
98 * @tc.number : SUB_KERNEL_SYSCALL_MSG_HANDLE_0200
99 * @tc.name   : MsgsndAndMsgrcvUseInvalidMsqidFail_0002
100 * @tc.desc   : msgsnd and msgrcv use a invalid msqid fail.
101 * @tc.size   : MediumTest
102 * @tc.type   : Function
103 * @tc.level  : Level 2
104 */
105HWTEST_F(HatsMsgHandleTest, MsgsndAndMsgrcvUseInvalidMsqidFail_0002, Function | MediumTest | Level2)
106{
107    int ret;
108
109    struct MsgBuf buffer = {
110        .type = MSG_TYPE,
111        .text = "Hello, world!",
112    };
113
114    errno = 0;
115    ret = msgsnd(-1, &buffer, MAX_LEN, IPC_NOWAIT);
116    EXPECT_TRUE(ret == -1);
117    EXPECT_EQ(errno, EINVAL);
118
119    errno = 0;
120    ret = msgrcv(-1, &buffer, MAX_LEN, MSG_TYPE, IPC_NOWAIT);
121    EXPECT_TRUE(ret == -1);
122    EXPECT_EQ(errno, EINVAL);
123}
124
125/*
126 * @tc.number : SUB_KERNEL_SYSCALL_MSG_HANDLE_0300
127 * @tc.name   : MsgsndAndMsgrcvUseInvalidLenFail_0003
128 * @tc.desc   : msgsnd and msgrcv use invalid message size given fail.
129 * @tc.size   : MediumTest
130 * @tc.type   : Function
131 * @tc.level  : Level 2
132 */
133HWTEST_F(HatsMsgHandleTest, MsgsndAndMsgrcvUseInvalidLenFail_0003, Function | MediumTest | Level2)
134{
135    int ret;
136    int msqid;
137
138    struct MsgBuf buffer = {
139        .type = MSG_TYPE,
140        .text = "Hello, world!",
141    };
142
143    msqid = msgget(IPC_PRIVATE, IPC_CREAT | 0777);
144    EXPECT_TRUE(msqid >= 0);
145
146    errno = 0;
147    ret = msgsnd(msqid, &buffer, -1, IPC_NOWAIT);
148    EXPECT_EQ(ret, -1);
149    EXPECT_EQ(errno, EINVAL);
150
151    errno = 0;
152    ret = msgrcv(msqid, &buffer, -1, MSG_TYPE, IPC_NOWAIT);
153    EXPECT_EQ(ret, -1);
154    EXPECT_EQ(errno, EINVAL);
155
156    ret = msgctl(msqid, IPC_RMID, nullptr);
157    EXPECT_EQ(ret, 0);
158}
159