1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "IpcMqTest.h"
17f6603c60Sopenharmony_ci#include <mqueue.h>
18f6603c60Sopenharmony_ci#include <fcntl.h>
19f6603c60Sopenharmony_ci#include <sys/stat.h>
20f6603c60Sopenharmony_ci#include <sys/types.h>
21f6603c60Sopenharmony_ci#include <sys/wait.h>
22f6603c60Sopenharmony_ci#include "log.h"
23f6603c60Sopenharmony_ci#include "utils.h"
24f6603c60Sopenharmony_ci#include "KernelConstants.h"
25f6603c60Sopenharmony_ci#include <securec.h>
26f6603c60Sopenharmony_ci
27f6603c60Sopenharmony_ciusing namespace testing::ext;
28f6603c60Sopenharmony_ci
29f6603c60Sopenharmony_ci/* *
30f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0100
31f6603c60Sopenharmony_ci * @tc.name   mq_send and mq_receive function test
32f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
33f6603c60Sopenharmony_ci */
34f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqOneLevelCom, Function | MediumTest | Level0)
35f6603c60Sopenharmony_ci{
36f6603c60Sopenharmony_ci    mqd_t queue;
37f6603c60Sopenharmony_ci    unsigned int prio;
38f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
39f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
40f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
41f6603c60Sopenharmony_ci
42f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqOneLevelCom_%d", GetRandom(10000));
43f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
44f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
45f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
46f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
47f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
48f6603c60Sopenharmony_ci
49f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
50f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_msgsize == MQ_MSG_SIZE) << "getAttr.mq_msgsize != MQ_MSG_SIZE, "
51f6603c60Sopenharmony_ci                                                   << "getAttr.mq_msgsize = " << getAttr.mq_msgsize;
52f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_maxmsg == MQ_MAX_MSG) << "getAttr.mq_maxmsg != MQ_MAX_MSG, "
53f6603c60Sopenharmony_ci                                                 << "getAttr.mq_maxmsg = " << getAttr.mq_maxmsg;
54f6603c60Sopenharmony_ci
55f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
56f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == 0) << "ERROR: prio != 0, prio = " << prio;
57f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
58f6603c60Sopenharmony_ci        rMsg;
59f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
60f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
61f6603c60Sopenharmony_ci}
62f6603c60Sopenharmony_ci
63f6603c60Sopenharmony_ci/* *
64f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0200
65f6603c60Sopenharmony_ci * @tc.name   mq_timedsend and mq_timedreceive function test
66f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
67f6603c60Sopenharmony_ci */
68f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqTimedOneLevelCom, Function | MediumTest | Level1)
69f6603c60Sopenharmony_ci{
70f6603c60Sopenharmony_ci    mqd_t queue;
71f6603c60Sopenharmony_ci    unsigned int prio;
72f6603c60Sopenharmony_ci    struct timespec tts = { 0 }, rts = { 0 };
73f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
74f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
75f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
76f6603c60Sopenharmony_ci
77f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqTimedOneLevelCom_%d", GetRandom(10000));
78f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
79f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
80f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
81f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
82f6603c60Sopenharmony_ci
83f6603c60Sopenharmony_ci    tts.tv_sec = time(NULL) + 1;
84f6603c60Sopenharmony_ci    tts.tv_nsec = 0;
85f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
86f6603c60Sopenharmony_ci
87f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
88f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_msgsize == MQ_MSG_SIZE) << "getAttr.mq_msgsize != MQ_MSG_SIZE, "
89f6603c60Sopenharmony_ci                                                   << "getAttr.mq_msgsize = " << getAttr.mq_msgsize;
90f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_maxmsg == MQ_MAX_MSG) << "getAttr.mq_maxmsg != MQ_MAX_MSG, "
91f6603c60Sopenharmony_ci                                                 << "getAttr.mq_maxmsg = " << getAttr.mq_maxmsg;
92f6603c60Sopenharmony_ci    rts.tv_sec = time(NULL) + 1;
93f6603c60Sopenharmony_ci    rts.tv_nsec = 0;
94f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
95f6603c60Sopenharmony_ci        "ERROR: mq_timedreceive() == -1";
96f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == 0) << "ERROR: prio != 0, prio = " << prio;
97f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
98f6603c60Sopenharmony_ci        rMsg;
99f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
100f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
101f6603c60Sopenharmony_ci}
102f6603c60Sopenharmony_ci
103f6603c60Sopenharmony_ci/* *
104f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0300
105f6603c60Sopenharmony_ci * @tc.name   all send and all receive function test
106f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
107f6603c60Sopenharmony_ci */
108f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqAllOneLevelCom, Function | MediumTest | Level2)
109f6603c60Sopenharmony_ci{
110f6603c60Sopenharmony_ci    mqd_t queue;
111f6603c60Sopenharmony_ci    int memRet = -1;
112f6603c60Sopenharmony_ci    unsigned int prio;
113f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
114f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
115f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
116f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
117f6603c60Sopenharmony_ci
118f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqAllOneLevelCom_%d", GetRandom(10000));
119f6603c60Sopenharmony_ci
120f6603c60Sopenharmony_ci    memRet = memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
121f6603c60Sopenharmony_ci    EXPECT_EQ(0, memRet);
122f6603c60Sopenharmony_ci    memRet = memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
123f6603c60Sopenharmony_ci    EXPECT_EQ(0, memRet);
124f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
125f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
126f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
127f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
128f6603c60Sopenharmony_ci
129f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &setAttr) == 0) << "ERROR: mq_getattr() != 0";
130f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
131f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
132f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_setattr(queue, &setAttr, NULL) == 0) << "ERROR: mq_setattr() != 0";
133f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
134f6603c60Sopenharmony_ci
135f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
136f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_msgsize == setAttr.mq_msgsize) << "ERROR: getAttr.mq_msgsize != setAttr.mq_msgsize,"
137f6603c60Sopenharmony_ci        " getAttr.mq_msgsize = " <<
138f6603c60Sopenharmony_ci        getAttr.mq_msgsize << " setAttr.mq_msgsize = " << setAttr.mq_msgsize;
139f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_maxmsg == setAttr.mq_maxmsg) << "ERROR: getAttr.mq_maxmsg != setAttr.mq_maxmsg,"
140f6603c60Sopenharmony_ci        " getAttr.mq_maxmsg = " <<
141f6603c60Sopenharmony_ci        getAttr.mq_maxmsg << " setAttr.mq_maxmsg = " << setAttr.mq_maxmsg;
142f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1, getAttr.mq_curmsgs = " <<
143f6603c60Sopenharmony_ci        getAttr.mq_curmsgs;
144f6603c60Sopenharmony_ci
145f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
146f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
147f6603c60Sopenharmony_ci        rMsg;
148f6603c60Sopenharmony_ci
149f6603c60Sopenharmony_ci    tts.tv_sec = time(NULL) + 1;
150f6603c60Sopenharmony_ci    tts.tv_nsec = 0;
151f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
152f6603c60Sopenharmony_ci
153f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
154f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_msgsize == setAttr.mq_msgsize) << "ERROR: getAttr.mq_msgsize != setAttr.mq_msgsize";
155f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_maxmsg == setAttr.mq_maxmsg) << "ERROR: getAttr.mq_maxmsg != setAttr.mq_maxmsg";
156f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1";
157f6603c60Sopenharmony_ci
158f6603c60Sopenharmony_ci    rts.tv_sec = time(NULL) + 1;
159f6603c60Sopenharmony_ci    rts.tv_nsec = 0;
160f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
161f6603c60Sopenharmony_ci        "ERROR: mq_timedreceive() == -1";
162f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
163f6603c60Sopenharmony_ci        rMsg;
164f6603c60Sopenharmony_ci
165f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
166f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0"
167f6603c60Sopenharmony_ci                                       << " errno = " << errno << " strerror(errno) = " << strerror(errno);
168f6603c60Sopenharmony_ci}
169f6603c60Sopenharmony_ci
170f6603c60Sopenharmony_ci/* *
171f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0400
172f6603c60Sopenharmony_ci * @tc.name   mq_send and mq_receive function test in child and parent process
173f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
174f6603c60Sopenharmony_ci */
175f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqTwoLevelCom, Function | MediumTest | Level1)
176f6603c60Sopenharmony_ci{
177f6603c60Sopenharmony_ci    mqd_t queue;
178f6603c60Sopenharmony_ci    pid_t pid;
179f6603c60Sopenharmony_ci    unsigned int prio;
180f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
181f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
182f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MAX_MQ_MSG_SIZE], sMsg[MAX_MQ_MSG_SIZE];
183f6603c60Sopenharmony_ci
184f6603c60Sopenharmony_ci    for (int i = 0; i < MAX_MQ_MSG_SIZE; i++) {
185f6603c60Sopenharmony_ci        sMsg[i] = 0x36;
186f6603c60Sopenharmony_ci        rMsg[i] = 0x00;
187f6603c60Sopenharmony_ci    }
188f6603c60Sopenharmony_ci
189f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqMaxLenTwoLevelCom_%d", GetRandom(10000));
190f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MAX_MQ_MSG_SIZE;
191f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
192f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
193f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
194f6603c60Sopenharmony_ci
195f6603c60Sopenharmony_ci    pid = fork();
196f6603c60Sopenharmony_ci    if (pid < 0) {
197f6603c60Sopenharmony_ci        goto FINISH;
198f6603c60Sopenharmony_ci    } else if (pid == 0) {
199f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
200f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
201f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == 0) << "ERROR: prio != 0, prio = " << prio;
202f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(sMsg, rMsg, MAX_MQ_MSG_SIZE) == 0) << "sent != received: sent = " << sMsg <<
203f6603c60Sopenharmony_ci            ", received = " << rMsg;
204f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_send(queue, sMsg, MAX_MQ_MSG_SIZE, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
205f6603c60Sopenharmony_ci        exit(0);
206f6603c60Sopenharmony_ci    } else {
207f6603c60Sopenharmony_ci        int status;
208f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_send(queue, sMsg, MAX_MQ_MSG_SIZE, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
209f6603c60Sopenharmony_ci
210f6603c60Sopenharmony_ci        pid = waitpid(pid, &status, 0);
211f6603c60Sopenharmony_ci        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
212f6603c60Sopenharmony_ci        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
213f6603c60Sopenharmony_ci        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
214f6603c60Sopenharmony_ci                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
215f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
216f6603c60Sopenharmony_ci        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
217f6603c60Sopenharmony_ci                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
218f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
219f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
220f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(sMsg, rMsg, MAX_MQ_MSG_SIZE) == 0) << "sent != received: sent = " << sMsg <<
221f6603c60Sopenharmony_ci            ", received = " << rMsg;
222f6603c60Sopenharmony_ci    }
223f6603c60Sopenharmony_ci
224f6603c60Sopenharmony_ciFINISH:
225f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
226f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
227f6603c60Sopenharmony_ci}
228f6603c60Sopenharmony_ci
229f6603c60Sopenharmony_ci/* *
230f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0500
231f6603c60Sopenharmony_ci * @tc.name   mq_timedsend and mq_timedreceive function test in child and parent process
232f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
233f6603c60Sopenharmony_ci */
234f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqTimedTwoLevelCom, Function | MediumTest | Level1)
235f6603c60Sopenharmony_ci{
236f6603c60Sopenharmony_ci    mqd_t queue;
237f6603c60Sopenharmony_ci    pid_t pid;
238f6603c60Sopenharmony_ci    unsigned int prio;
239f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
240f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
241f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
242f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
243f6603c60Sopenharmony_ci
244f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqTimedTwoLevelCom_%d", GetRandom(10000));
245f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
246f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
247f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
248f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
249f6603c60Sopenharmony_ci
250f6603c60Sopenharmony_ci    pid = fork();
251f6603c60Sopenharmony_ci    if (pid < 0) {
252f6603c60Sopenharmony_ci        goto FINISH;
253f6603c60Sopenharmony_ci    } else if (pid == 0) {
254f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
255f6603c60Sopenharmony_ci        rts.tv_sec = time(NULL) + 3;
256f6603c60Sopenharmony_ci        rts.tv_nsec = 0;
257f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
258f6603c60Sopenharmony_ci            "ERROR: mq_timedreceive() == -1";
259f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
260f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
261f6603c60Sopenharmony_ci            ", received = " << rMsg;
262f6603c60Sopenharmony_ci
263f6603c60Sopenharmony_ci        tts.tv_sec = time(NULL) + 3;
264f6603c60Sopenharmony_ci        tts.tv_nsec = 0;
265f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
266f6603c60Sopenharmony_ci        exit(0);
267f6603c60Sopenharmony_ci    } else {
268f6603c60Sopenharmony_ci        int status;
269f6603c60Sopenharmony_ci        tts.tv_sec = time(NULL) + 3;
270f6603c60Sopenharmony_ci        tts.tv_nsec = 0;
271f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
272f6603c60Sopenharmony_ci
273f6603c60Sopenharmony_ci        pid = waitpid(pid, &status, 0);
274f6603c60Sopenharmony_ci        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
275f6603c60Sopenharmony_ci        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
276f6603c60Sopenharmony_ci        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
277f6603c60Sopenharmony_ci                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
278f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
279f6603c60Sopenharmony_ci        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
280f6603c60Sopenharmony_ci                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
281f6603c60Sopenharmony_ci        rts.tv_sec = time(NULL) + 3;
282f6603c60Sopenharmony_ci        rts.tv_nsec = 0;
283f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
284f6603c60Sopenharmony_ci            "ERROR: mq_timedreceive() == -1";
285f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
286f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
287f6603c60Sopenharmony_ci            ", received = " << rMsg;
288f6603c60Sopenharmony_ci    }
289f6603c60Sopenharmony_ci
290f6603c60Sopenharmony_ciFINISH:
291f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
292f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
293f6603c60Sopenharmony_ci}
294f6603c60Sopenharmony_ci
295f6603c60Sopenharmony_ci/* *
296f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0600
297f6603c60Sopenharmony_ci * @tc.name   all send and all receive function test in child and parent process
298f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
299f6603c60Sopenharmony_ci */
300f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqAllTwoLevelCom, Function | MediumTest | Level1)
301f6603c60Sopenharmony_ci{
302f6603c60Sopenharmony_ci    mqd_t queue;
303f6603c60Sopenharmony_ci    pid_t pid;
304f6603c60Sopenharmony_ci    unsigned int prio;
305f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
306f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
307f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
308f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
309f6603c60Sopenharmony_ci
310f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqAllTwoLevelCom_%d", GetRandom(10000));
311f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
312f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
313f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
314f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
315f6603c60Sopenharmony_ci
316f6603c60Sopenharmony_ci    pid = fork();
317f6603c60Sopenharmony_ci    if (pid < 0) {
318f6603c60Sopenharmony_ci        goto FINISH;
319f6603c60Sopenharmony_ci    } else if (pid == 0) {
320f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
321f6603c60Sopenharmony_ci        rts.tv_sec = time(NULL) + 1;
322f6603c60Sopenharmony_ci        rts.tv_nsec = 0;
323f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
324f6603c60Sopenharmony_ci            "ERROR: mq_timedreceive() == -1";
325f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
326f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
327f6603c60Sopenharmony_ci            ", received = " << rMsg;
328f6603c60Sopenharmony_ci
329f6603c60Sopenharmony_ci        tts.tv_sec = time(NULL) + 1;
330f6603c60Sopenharmony_ci        tts.tv_nsec = 0;
331f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
332f6603c60Sopenharmony_ci        exit(0);
333f6603c60Sopenharmony_ci    } else {
334f6603c60Sopenharmony_ci        int status;
335f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
336f6603c60Sopenharmony_ci
337f6603c60Sopenharmony_ci        pid = waitpid(pid, &status, 0);
338f6603c60Sopenharmony_ci        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
339f6603c60Sopenharmony_ci        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
340f6603c60Sopenharmony_ci        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
341f6603c60Sopenharmony_ci                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
342f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
343f6603c60Sopenharmony_ci        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
344f6603c60Sopenharmony_ci                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
345f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
346f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
347f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
348f6603c60Sopenharmony_ci            ", received = " << rMsg;
349f6603c60Sopenharmony_ci    }
350f6603c60Sopenharmony_ci
351f6603c60Sopenharmony_ciFINISH:
352f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
353f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
354f6603c60Sopenharmony_ci}
355f6603c60Sopenharmony_ci
356f6603c60Sopenharmony_cistatic void *PthreadCom(void *arg)
357f6603c60Sopenharmony_ci{
358f6603c60Sopenharmony_ci    mqd_t queue;
359f6603c60Sopenharmony_ci    unsigned int prio;
360f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
361f6603c60Sopenharmony_ci    char rMsg[MQ_RX_LEN];
362f6603c60Sopenharmony_ci
363f6603c60Sopenharmony_ci    queue = (mqd_t)arg;
364f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
365f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
366f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
367f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
368f6603c60Sopenharmony_ci        rMsg;
369f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
370f6603c60Sopenharmony_ci
371f6603c60Sopenharmony_ci    return nullptr;
372f6603c60Sopenharmony_ci}
373f6603c60Sopenharmony_ci
374f6603c60Sopenharmony_ci/* *
375f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0700
376f6603c60Sopenharmony_ci * @tc.name   mq_send and mq_receive function test in thread and process
377f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
378f6603c60Sopenharmony_ci */
379f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqTwoThreadCom, Function | MediumTest | Level1)
380f6603c60Sopenharmony_ci{
381f6603c60Sopenharmony_ci    mqd_t queue;
382f6603c60Sopenharmony_ci    pthread_t tid;
383f6603c60Sopenharmony_ci    unsigned int prio;
384f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
385f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
386f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
387f6603c60Sopenharmony_ci
388f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqTwoLevelCom_%d", GetRandom(10000));
389f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
390f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
391f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
392f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
393f6603c60Sopenharmony_ci
394f6603c60Sopenharmony_ci    EXPECT_TRUE(pthread_create(&tid, NULL, PthreadCom, (void *)queue) != -1) << "ERROR: pthread_create() == -1";
395f6603c60Sopenharmony_ci
396f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
397f6603c60Sopenharmony_ci    EXPECT_TRUE(pthread_join(tid, NULL) == 0) << "ERROR: pthread_join() != 0";
398f6603c60Sopenharmony_ci
399f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
400f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
401f6603c60Sopenharmony_ci                                         << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
402f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
403f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
404f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
405f6603c60Sopenharmony_ci        rMsg;
406f6603c60Sopenharmony_ci
407f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
408f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
409f6603c60Sopenharmony_ci}
410f6603c60Sopenharmony_ci
411f6603c60Sopenharmony_cistatic void *PthreadTimedCom(void *arg)
412f6603c60Sopenharmony_ci{
413f6603c60Sopenharmony_ci    mqd_t queue;
414f6603c60Sopenharmony_ci    unsigned int prio;
415f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
416f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
417f6603c60Sopenharmony_ci    char rMsg[MQ_RX_LEN];
418f6603c60Sopenharmony_ci
419f6603c60Sopenharmony_ci    queue = (mqd_t)arg;
420f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
421f6603c60Sopenharmony_ci    rts.tv_sec = time(NULL) + 1;
422f6603c60Sopenharmony_ci    rts.tv_nsec = 0;
423f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
424f6603c60Sopenharmony_ci        "ERROR: mq_timedreceive() == -1";
425f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
426f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
427f6603c60Sopenharmony_ci        rMsg;
428f6603c60Sopenharmony_ci
429f6603c60Sopenharmony_ci    tts.tv_sec = time(NULL) + 1;
430f6603c60Sopenharmony_ci    tts.tv_nsec = 0;
431f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
432f6603c60Sopenharmony_ci
433f6603c60Sopenharmony_ci    return nullptr;
434f6603c60Sopenharmony_ci}
435f6603c60Sopenharmony_ci
436f6603c60Sopenharmony_ci/* *
437f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0800
438f6603c60Sopenharmony_ci * @tc.name   mq_timedsend and mq_timedreceive function test in thread and process
439f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
440f6603c60Sopenharmony_ci */
441f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqTimedTwoThreadCom, Function | MediumTest | Level1)
442f6603c60Sopenharmony_ci{
443f6603c60Sopenharmony_ci    mqd_t queue;
444f6603c60Sopenharmony_ci    pthread_t tid;
445f6603c60Sopenharmony_ci    unsigned int prio;
446f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
447f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
448f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
449f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
450f6603c60Sopenharmony_ci
451f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqTimedTwoThreadCom_%d", GetRandom(10000));
452f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
453f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
454f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
455f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
456f6603c60Sopenharmony_ci
457f6603c60Sopenharmony_ci    EXPECT_TRUE(pthread_create(&tid, NULL, PthreadTimedCom, (void *)queue) != -1) << "ERROR: pthread_create() == -1";
458f6603c60Sopenharmony_ci
459f6603c60Sopenharmony_ci    tts.tv_sec = time(NULL) + 1;
460f6603c60Sopenharmony_ci    tts.tv_nsec = 0;
461f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
462f6603c60Sopenharmony_ci    EXPECT_TRUE(pthread_join(tid, NULL) == 0) << "ERROR: pthread_join() != 0";
463f6603c60Sopenharmony_ci
464f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
465f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
466f6603c60Sopenharmony_ci                                         << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
467f6603c60Sopenharmony_ci    rts.tv_sec = time(NULL) + 1;
468f6603c60Sopenharmony_ci    rts.tv_nsec = 0;
469f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
470f6603c60Sopenharmony_ci        "ERROR: mq_timedreceive() == -1";
471f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
472f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
473f6603c60Sopenharmony_ci        rMsg;
474f6603c60Sopenharmony_ci
475f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
476f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
477f6603c60Sopenharmony_ci}
478f6603c60Sopenharmony_ci
479f6603c60Sopenharmony_cistatic void *PthreadAllCom(void *arg)
480f6603c60Sopenharmony_ci{
481f6603c60Sopenharmony_ci    mqd_t queue;
482f6603c60Sopenharmony_ci    unsigned int prio;
483f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
484f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
485f6603c60Sopenharmony_ci    char rMsg[MQ_RX_LEN];
486f6603c60Sopenharmony_ci
487f6603c60Sopenharmony_ci    queue = (mqd_t)arg;
488f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
489f6603c60Sopenharmony_ci    rts.tv_sec = time(NULL) + 1;
490f6603c60Sopenharmony_ci    rts.tv_nsec = 0;
491f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
492f6603c60Sopenharmony_ci        "ERROR: mq_timedreceive() == -1";
493f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
494f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
495f6603c60Sopenharmony_ci        rMsg;
496f6603c60Sopenharmony_ci    tts.tv_sec = time(NULL) + 1;
497f6603c60Sopenharmony_ci    tts.tv_nsec = 0;
498f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
499f6603c60Sopenharmony_ci
500f6603c60Sopenharmony_ci    return nullptr;
501f6603c60Sopenharmony_ci}
502f6603c60Sopenharmony_ci
503f6603c60Sopenharmony_ci/* *
504f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_0900
505f6603c60Sopenharmony_ci * @tc.name   all send and all receive function test in thread and process
506f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
507f6603c60Sopenharmony_ci */
508f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqAllTwoThreadCom, Function | MediumTest | Level1)
509f6603c60Sopenharmony_ci{
510f6603c60Sopenharmony_ci    mqd_t queue;
511f6603c60Sopenharmony_ci    pthread_t tid;
512f6603c60Sopenharmony_ci    unsigned int prio;
513f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
514f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
515f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
516f6603c60Sopenharmony_ci
517f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqAllTwoThreadCom_%d", GetRandom(10000));
518f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
519f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
520f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
521f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
522f6603c60Sopenharmony_ci
523f6603c60Sopenharmony_ci    EXPECT_TRUE(pthread_create(&tid, NULL, PthreadAllCom, (void *)queue) != -1) << "ERROR: pthread_create() == -1";
524f6603c60Sopenharmony_ci
525f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
526f6603c60Sopenharmony_ci    EXPECT_TRUE(pthread_join(tid, NULL) == 0) << "ERROR: pthread_join() != 0";
527f6603c60Sopenharmony_ci
528f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
529f6603c60Sopenharmony_ci    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
530f6603c60Sopenharmony_ci                                         << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
531f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
532f6603c60Sopenharmony_ci    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
533f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
534f6603c60Sopenharmony_ci        rMsg;
535f6603c60Sopenharmony_ci
536f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
537f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
538f6603c60Sopenharmony_ci}
539f6603c60Sopenharmony_ci
540f6603c60Sopenharmony_ci/* *
541f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_1000
542f6603c60Sopenharmony_ci * @tc.name   all send and all receive function test in more process
543f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
544f6603c60Sopenharmony_ci */
545f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqThreeLevelCom, Function | MediumTest | Level2)
546f6603c60Sopenharmony_ci{
547f6603c60Sopenharmony_ci    mqd_t queue;
548f6603c60Sopenharmony_ci    pid_t pid;
549f6603c60Sopenharmony_ci    unsigned int prio;
550f6603c60Sopenharmony_ci    struct timespec tts { 0 }, rts = { 0 };
551f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
552f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
553f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
554f6603c60Sopenharmony_ci
555f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqThreeLevelCom_%d", GetRandom(10000));
556f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
557f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
558f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
559f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
560f6603c60Sopenharmony_ci
561f6603c60Sopenharmony_ci    pid = fork();
562f6603c60Sopenharmony_ci    if (pid < 0) {
563f6603c60Sopenharmony_ci        goto FINISH;
564f6603c60Sopenharmony_ci    } else if (pid == 0) {
565f6603c60Sopenharmony_ci        pid = fork();
566f6603c60Sopenharmony_ci        if (pid == 0) {
567f6603c60Sopenharmony_ci            EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
568f6603c60Sopenharmony_ci            EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1"
569f6603c60Sopenharmony_ci                                                                                  << " errno = " << errno <<
570f6603c60Sopenharmony_ci                " strerror(errno) = " << strerror(errno);
571f6603c60Sopenharmony_ci            EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
572f6603c60Sopenharmony_ci            EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
573f6603c60Sopenharmony_ci                ", received = " << rMsg;
574f6603c60Sopenharmony_ci            EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
575f6603c60Sopenharmony_ci        } else {
576f6603c60Sopenharmony_ci            int status;
577f6603c60Sopenharmony_ci
578f6603c60Sopenharmony_ci            pid = waitpid(pid, &status, 0);
579f6603c60Sopenharmony_ci            EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
580f6603c60Sopenharmony_ci            EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
581f6603c60Sopenharmony_ci            EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
582f6603c60Sopenharmony_ci                                                  << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
583f6603c60Sopenharmony_ci
584f6603c60Sopenharmony_ci            EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
585f6603c60Sopenharmony_ci            rts.tv_sec = time(NULL) + 1;
586f6603c60Sopenharmony_ci            rts.tv_nsec = 0;
587f6603c60Sopenharmony_ci            EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
588f6603c60Sopenharmony_ci                "ERROR: mq_timedreceive() == -1";
589f6603c60Sopenharmony_ci            EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
590f6603c60Sopenharmony_ci            EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
591f6603c60Sopenharmony_ci                ", received = " << rMsg;
592f6603c60Sopenharmony_ci
593f6603c60Sopenharmony_ci            tts.tv_sec = time(NULL) + 1;
594f6603c60Sopenharmony_ci            tts.tv_nsec = 0;
595f6603c60Sopenharmony_ci            EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) <<
596f6603c60Sopenharmony_ci                "ERROR: mq_timedsend() != 0";
597f6603c60Sopenharmony_ci        }
598f6603c60Sopenharmony_ci        exit(0);
599f6603c60Sopenharmony_ci    } else {
600f6603c60Sopenharmony_ci        int status;
601f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
602f6603c60Sopenharmony_ci
603f6603c60Sopenharmony_ci        pid = waitpid(pid, &status, 0);
604f6603c60Sopenharmony_ci        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
605f6603c60Sopenharmony_ci        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
606f6603c60Sopenharmony_ci        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
607f6603c60Sopenharmony_ci                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
608f6603c60Sopenharmony_ci
609f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
610f6603c60Sopenharmony_ci        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
611f6603c60Sopenharmony_ci                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
612f6603c60Sopenharmony_ci        rts.tv_sec = time(NULL) + 1;
613f6603c60Sopenharmony_ci        rts.tv_nsec = 0;
614f6603c60Sopenharmony_ci        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
615f6603c60Sopenharmony_ci            "ERROR: mq_timedreceive() == -1";
616f6603c60Sopenharmony_ci        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
617f6603c60Sopenharmony_ci        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
618f6603c60Sopenharmony_ci            ", received = " << rMsg;
619f6603c60Sopenharmony_ci    }
620f6603c60Sopenharmony_ci
621f6603c60Sopenharmony_ciFINISH:
622f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
623f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
624f6603c60Sopenharmony_ci}
625f6603c60Sopenharmony_ci
626f6603c60Sopenharmony_ci/* *
627f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_1100
628f6603c60Sopenharmony_ci * @tc.name   mq_send and mq_receive max packet function test
629f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
630f6603c60Sopenharmony_ci */
631f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqMinMaxLen, Function | MediumTest | Level2)
632f6603c60Sopenharmony_ci{
633f6603c60Sopenharmony_ci    mqd_t queue;
634f6603c60Sopenharmony_ci    unsigned int prio;
635f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
636f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
637f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN], rMsg[MAX_MQ_MSG_SIZE], sMsg[MAX_MQ_MSG_SIZE];
638f6603c60Sopenharmony_ci
639f6603c60Sopenharmony_ci    for (int i = 0; i < MAX_MQ_MSG_SIZE; i++) {
640f6603c60Sopenharmony_ci        sMsg[i] = 0x36;
641f6603c60Sopenharmony_ci        rMsg[i] = 0x00;
642f6603c60Sopenharmony_ci    }
643f6603c60Sopenharmony_ci
644f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqMaxLen_%d", GetRandom(10000));
645f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MAX_MQ_MSG_SIZE;
646f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
647f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
648f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1"
649f6603c60Sopenharmony_ci                                    << " errno = " << errno << " strerror(errno) = " << strerror(errno);
650f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_send(queue, sMsg, MAX_MQ_MSG_SIZE, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
651f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
652f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
653f6603c60Sopenharmony_ci    EXPECT_TRUE(strncmp(sMsg, rMsg, MAX_MQ_MSG_SIZE) == 0) << "sent != received: sent = " << sMsg << ", received = " <<
654f6603c60Sopenharmony_ci        rMsg;
655f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
656f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
657f6603c60Sopenharmony_ci}
658f6603c60Sopenharmony_ci
659f6603c60Sopenharmony_ci/* *
660f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_IPC_MQ_1200
661f6603c60Sopenharmony_ci * @tc.name   mq_setattr set and clean mq_flags for O_NONBLOCK function test
662f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
663f6603c60Sopenharmony_ci */
664f6603c60Sopenharmony_ciHWTEST_F(IpcMqTest, testMqSetGetAttr, Function | MediumTest | Level1)
665f6603c60Sopenharmony_ci{
666f6603c60Sopenharmony_ci    mqd_t queue;
667f6603c60Sopenharmony_ci    struct mq_attr getAttr = { 0 };
668f6603c60Sopenharmony_ci    struct mq_attr setAttr = { 0 };
669f6603c60Sopenharmony_ci    char qName[MQ_NAME_LEN];
670f6603c60Sopenharmony_ci    int memRet = -1;
671f6603c60Sopenharmony_ci
672f6603c60Sopenharmony_ci    sprintf_s(qName, sizeof(qName), "testMqFunction_%d", GetRandom(10000));
673f6603c60Sopenharmony_ci
674f6603c60Sopenharmony_ci    memRet = memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
675f6603c60Sopenharmony_ci    EXPECT_EQ(0, memRet);
676f6603c60Sopenharmony_ci    setAttr.mq_msgsize = MQ_MSG_SIZE;
677f6603c60Sopenharmony_ci    setAttr.mq_maxmsg = MQ_MAX_MSG;
678f6603c60Sopenharmony_ci    setAttr.mq_flags = O_NONBLOCK;
679f6603c60Sopenharmony_ci    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
680f6603c60Sopenharmony_ci    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
681f6603c60Sopenharmony_ci
682f6603c60Sopenharmony_ci    memRet = memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
683f6603c60Sopenharmony_ci    EXPECT_EQ(0, memRet);
684f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
685f6603c60Sopenharmony_ci    EXPECT_TRUE((getAttr.mq_flags & O_NONBLOCK) == O_NONBLOCK);
686f6603c60Sopenharmony_ci
687f6603c60Sopenharmony_ci    setAttr.mq_flags &= ~O_NONBLOCK;
688f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_setattr(queue, &setAttr, NULL) == 0) << "ERROR: mq_setattr() != 0";
689f6603c60Sopenharmony_ci
690f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
691f6603c60Sopenharmony_ci    LOG("setAttr.mq_flags = 0x%08x, getAttr.mq_flags = 0x%08x ", setAttr.mq_flags, getAttr.mq_flags);
692f6603c60Sopenharmony_ci    EXPECT_TRUE((getAttr.mq_flags & O_NONBLOCK) == 0) << "ERROR: getAttr.mq_flags & O_NONBLOCK != 0,"
693f6603c60Sopenharmony_ci        " getAttr.mq_flags = " <<
694f6603c60Sopenharmony_ci        getAttr.mq_flags << " setAttr.mq_flags = " << setAttr.mq_flags;
695f6603c60Sopenharmony_ci    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
696f6603c60Sopenharmony_ci    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0"
697f6603c60Sopenharmony_ci                                       << " errno = " << errno << " strerror(errno) = " << strerror(errno);
698f6603c60Sopenharmony_ci}
699