IpcMqTest.cpp 31.2 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
M
mamingshuai 已提交
7
 * http://www.apache.org/licenses/LICENSE-2.0
W
wenjun 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "IpcMqTest.h"
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "log.h"
#include "utils.h"
#include "KernelConstants.h"
M
mamingshuai 已提交
25
#include <securec.h>
W
wenjun 已提交
26 27 28

using namespace testing::ext;

M
mamingshuai 已提交
29
/* *
W
wenjun 已提交
30 31 32 33
 * @tc.number SUB_KERNEL_IPC_MQ_0100
 * @tc.name   mq_send and mq_receive function test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
34
HWTEST_F(IpcMqTest, testMqOneLevelCom, Function | MediumTest | Level0)
W
wenjun 已提交
35 36 37
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
38 39
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
40 41
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
42
    sprintf_s(qName, sizeof(qName), "testMqOneLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
43 44 45 46 47 48 49
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";
    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
50 51 52 53
    EXPECT_TRUE(getAttr.mq_msgsize == MQ_MSG_SIZE) << "getAttr.mq_msgsize != MQ_MSG_SIZE, "
                                                   << "getAttr.mq_msgsize = " << getAttr.mq_msgsize;
    EXPECT_TRUE(getAttr.mq_maxmsg == MQ_MAX_MSG) << "getAttr.mq_maxmsg != MQ_MAX_MSG, "
                                                 << "getAttr.mq_maxmsg = " << getAttr.mq_maxmsg;
W
wenjun 已提交
54 55 56

    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
    EXPECT_TRUE(prio == 0) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
57 58
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
59 60 61 62
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
63
/* *
W
wenjun 已提交
64 65 66 67
 * @tc.number SUB_KERNEL_IPC_MQ_0200
 * @tc.name   mq_timedsend and mq_timedreceive function test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
68
HWTEST_F(IpcMqTest, testMqTimedOneLevelCom, Function | MediumTest | Level1)
W
wenjun 已提交
69 70 71
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
72 73 74
    struct timespec tts = { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
75 76
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
77
    sprintf_s(qName, sizeof(qName), "testMqTimedOneLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
78 79 80 81 82 83 84 85 86 87
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    tts.tv_sec = time(NULL) + 1;
    tts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
88 89 90 91
    EXPECT_TRUE(getAttr.mq_msgsize == MQ_MSG_SIZE) << "getAttr.mq_msgsize != MQ_MSG_SIZE, "
                                                   << "getAttr.mq_msgsize = " << getAttr.mq_msgsize;
    EXPECT_TRUE(getAttr.mq_maxmsg == MQ_MAX_MSG) << "getAttr.mq_maxmsg != MQ_MAX_MSG, "
                                                 << "getAttr.mq_maxmsg = " << getAttr.mq_maxmsg;
W
wenjun 已提交
92 93
    rts.tv_sec = time(NULL) + 1;
    rts.tv_nsec = 0;
M
mamingshuai 已提交
94 95
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
        "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
96
    EXPECT_TRUE(prio == 0) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
97 98
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
99 100 101 102
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
103
/* *
W
wenjun 已提交
104 105 106 107
 * @tc.number SUB_KERNEL_IPC_MQ_0300
 * @tc.name   all send and all receive function test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
108
HWTEST_F(IpcMqTest, testMqAllOneLevelCom, Function | MediumTest | Level2)
W
wenjun 已提交
109 110 111
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
112 113 114
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
115 116
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
117
    sprintf_s(qName, sizeof(qName), "testMqAllOneLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
118

M
mamingshuai 已提交
119 120
    memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
    memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
W
wenjun 已提交
121 122 123 124 125 126 127 128 129 130 131 132
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_getattr(queue, &setAttr) == 0) << "ERROR: mq_getattr() != 0";
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    EXPECT_TRUE(mq_setattr(queue, &setAttr, NULL) == 0) << "ERROR: mq_setattr() != 0";
    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
133 134 135 136 137 138 139 140 141 142 143 144
    EXPECT_TRUE(getAttr.mq_msgsize == setAttr.mq_msgsize) << "ERROR: getAttr.mq_msgsize != setAttr.mq_msgsize,"
        " getAttr.mq_msgsize = " <<
        getAttr.mq_msgsize << " setAttr.mq_msgsize = " << setAttr.mq_msgsize;
    EXPECT_TRUE(getAttr.mq_maxmsg == setAttr.mq_maxmsg) << "ERROR: getAttr.mq_maxmsg != setAttr.mq_maxmsg,"
        " getAttr.mq_maxmsg = " <<
        getAttr.mq_maxmsg << " setAttr.mq_maxmsg = " << setAttr.mq_maxmsg;
    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1, getAttr.mq_curmsgs = " <<
        getAttr.mq_curmsgs;

    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
145 146 147 148 149 150 151 152 153 154 155 156

    tts.tv_sec = time(NULL) + 1;
    tts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    EXPECT_TRUE(getAttr.mq_msgsize == setAttr.mq_msgsize) << "ERROR: getAttr.mq_msgsize != setAttr.mq_msgsize";
    EXPECT_TRUE(getAttr.mq_maxmsg == setAttr.mq_maxmsg) << "ERROR: getAttr.mq_maxmsg != setAttr.mq_maxmsg";
    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1";

    rts.tv_sec = time(NULL) + 1;
    rts.tv_nsec = 0;
M
mamingshuai 已提交
157 158 159 160
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
        "ERROR: mq_timedreceive() == -1";
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
161 162

    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
M
mamingshuai 已提交
163 164
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0"
                                       << " errno = " << errno << " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
165 166
}

M
mamingshuai 已提交
167
/* *
W
wenjun 已提交
168 169 170 171
 * @tc.number SUB_KERNEL_IPC_MQ_0400
 * @tc.name   mq_send and mq_receive function test in child and parent process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
172
HWTEST_F(IpcMqTest, testMqTwoLevelCom, Function | MediumTest | Level1)
W
wenjun 已提交
173 174 175 176
{
    mqd_t queue;
    pid_t pid;
    unsigned int prio;
M
mamingshuai 已提交
177 178
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
179 180
    char qName[MQ_NAME_LEN], rMsg[MAX_MQ_MSG_SIZE], sMsg[MAX_MQ_MSG_SIZE];

M
mamingshuai 已提交
181
    for (int i = 0; i < MAX_MQ_MSG_SIZE; i++) {
W
wenjun 已提交
182 183 184 185
        sMsg[i] = 0x36;
        rMsg[i] = 0x00;
    }

M
mamingshuai 已提交
186
    sprintf_s(qName, sizeof(qName), "testMqMaxLenTwoLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
187 188 189 190 191 192 193 194 195 196 197 198
    setAttr.mq_msgsize = MAX_MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    pid = fork();
    if (pid < 0) {
        goto FINISH;
    } else if (pid == 0) {
        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
        EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
        EXPECT_TRUE(prio == 0) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
199 200
        EXPECT_TRUE(strncmp(sMsg, rMsg, MAX_MQ_MSG_SIZE) == 0) << "sent != received: sent = " << sMsg <<
            ", received = " << rMsg;
W
wenjun 已提交
201 202 203 204 205 206 207 208 209
        EXPECT_TRUE(mq_send(queue, sMsg, MAX_MQ_MSG_SIZE, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
        exit(0);
    } else {
        int status;
        EXPECT_TRUE(mq_send(queue, sMsg, MAX_MQ_MSG_SIZE, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";

        pid = waitpid(pid, &status, 0);
        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
M
mamingshuai 已提交
210
        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
W
wenjun 已提交
211 212
                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
213
        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
W
wenjun 已提交
214 215 216
                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
        EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
217 218
        EXPECT_TRUE(strncmp(sMsg, rMsg, MAX_MQ_MSG_SIZE) == 0) << "sent != received: sent = " << sMsg <<
            ", received = " << rMsg;
W
wenjun 已提交
219 220 221 222 223 224 225
    }

FINISH:
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
226
/* *
W
wenjun 已提交
227 228 229 230
 * @tc.number SUB_KERNEL_IPC_MQ_0500
 * @tc.name   mq_timedsend and mq_timedreceive function test in child and parent process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
231
HWTEST_F(IpcMqTest, testMqTimedTwoLevelCom, Function | MediumTest | Level1)
W
wenjun 已提交
232 233 234 235
{
    mqd_t queue;
    pid_t pid;
    unsigned int prio;
M
mamingshuai 已提交
236 237 238
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
239 240
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
241
    sprintf_s(qName, sizeof(qName), "testMqTimedTwoLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
242 243 244 245 246 247 248 249 250 251 252 253
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    pid = fork();
    if (pid < 0) {
        goto FINISH;
    } else if (pid == 0) {
        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
        rts.tv_sec = time(NULL) + 3;
        rts.tv_nsec = 0;
M
mamingshuai 已提交
254 255
        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
            "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
256
        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
257 258
        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
            ", received = " << rMsg;
W
wenjun 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272

        tts.tv_sec = time(NULL) + 3;
        tts.tv_nsec = 0;
        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
        exit(0);
    } else {
        int status;
        tts.tv_sec = time(NULL) + 3;
        tts.tv_nsec = 0;
        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";

        pid = waitpid(pid, &status, 0);
        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
M
mamingshuai 已提交
273
        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
W
wenjun 已提交
274 275
                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
276
        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
W
wenjun 已提交
277 278 279
                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
        rts.tv_sec = time(NULL) + 3;
        rts.tv_nsec = 0;
M
mamingshuai 已提交
280 281
        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
            "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
282
        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
283 284
        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
            ", received = " << rMsg;
W
wenjun 已提交
285 286 287 288 289 290 291
    }

FINISH:
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
292
/* *
W
wenjun 已提交
293 294 295 296
 * @tc.number SUB_KERNEL_IPC_MQ_0600
 * @tc.name   all send and all receive function test in child and parent process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
297
HWTEST_F(IpcMqTest, testMqAllTwoLevelCom, Function | MediumTest | Level1)
W
wenjun 已提交
298 299 300 301
{
    mqd_t queue;
    pid_t pid;
    unsigned int prio;
M
mamingshuai 已提交
302 303 304
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
305 306
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
307
    sprintf_s(qName, sizeof(qName), "testMqAllTwoLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
308 309 310 311 312 313 314 315 316 317 318 319
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    pid = fork();
    if (pid < 0) {
        goto FINISH;
    } else if (pid == 0) {
        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
        rts.tv_sec = time(NULL) + 1;
        rts.tv_nsec = 0;
M
mamingshuai 已提交
320 321
        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
            "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
322
        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
323 324
        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
            ", received = " << rMsg;
W
wenjun 已提交
325 326 327 328 329 330 331 332 333 334 335 336

        tts.tv_sec = time(NULL) + 1;
        tts.tv_nsec = 0;
        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
        exit(0);
    } else {
        int status;
        EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";

        pid = waitpid(pid, &status, 0);
        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
M
mamingshuai 已提交
337
        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
W
wenjun 已提交
338 339
                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
340
        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
W
wenjun 已提交
341 342 343
                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
        EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
344 345
        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
            ", received = " << rMsg;
W
wenjun 已提交
346 347 348 349 350 351 352 353 354 355 356
    }

FINISH:
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

static void *PthreadCom(void *arg)
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
357
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
358 359 360 361 362 363
    char rMsg[MQ_RX_LEN];

    queue = (mqd_t)arg;
    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
364 365
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
366 367
    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";

M
mamingshuai 已提交
368
    return nullptr;
W
wenjun 已提交
369 370
}

M
mamingshuai 已提交
371
/* *
W
wenjun 已提交
372 373 374 375
 * @tc.number SUB_KERNEL_IPC_MQ_0700
 * @tc.name   mq_send and mq_receive function test in thread and process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
376
HWTEST_F(IpcMqTest, testMqTwoThreadCom, Function | MediumTest | Level1)
W
wenjun 已提交
377 378 379 380
{
    mqd_t queue;
    pthread_t tid;
    unsigned int prio;
M
mamingshuai 已提交
381 382
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
383 384
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
385
    sprintf_s(qName, sizeof(qName), "testMqTwoLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
386 387 388 389 390 391 392 393 394 395 396
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(pthread_create(&tid, NULL, PthreadCom, (void *)queue) != -1) << "ERROR: pthread_create() == -1";

    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
    EXPECT_TRUE(pthread_join(tid, NULL) == 0) << "ERROR: pthread_join() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
397 398
    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
                                         << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
W
wenjun 已提交
399 400
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != 0, prio = " << prio;
M
mamingshuai 已提交
401 402
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
403 404 405 406 407 408 409 410 411

    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

static void *PthreadTimedCom(void *arg)
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
412 413
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
414 415 416 417 418 419
    char rMsg[MQ_RX_LEN];

    queue = (mqd_t)arg;
    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    rts.tv_sec = time(NULL) + 1;
    rts.tv_nsec = 0;
M
mamingshuai 已提交
420 421
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
        "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
422
    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
423 424
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
425 426 427 428 429

    tts.tv_sec = time(NULL) + 1;
    tts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";

M
mamingshuai 已提交
430
    return nullptr;
W
wenjun 已提交
431 432
}

M
mamingshuai 已提交
433
/* *
W
wenjun 已提交
434 435 436 437
 * @tc.number SUB_KERNEL_IPC_MQ_0800
 * @tc.name   mq_timedsend and mq_timedreceive function test in thread and process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
438
HWTEST_F(IpcMqTest, testMqTimedTwoThreadCom, Function | MediumTest | Level1)
W
wenjun 已提交
439 440 441 442
{
    mqd_t queue;
    pthread_t tid;
    unsigned int prio;
M
mamingshuai 已提交
443 444 445
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
446 447
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
448
    sprintf_s(qName, sizeof(qName), "testMqTimedTwoThreadCom_%d", GetRandom(10000));
W
wenjun 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(pthread_create(&tid, NULL, PthreadTimedCom, (void *)queue) != -1) << "ERROR: pthread_create() == -1";

    tts.tv_sec = time(NULL) + 1;
    tts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";
    EXPECT_TRUE(pthread_join(tid, NULL) == 0) << "ERROR: pthread_join() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
462 463
    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
                                         << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
W
wenjun 已提交
464 465
    rts.tv_sec = time(NULL) + 1;
    rts.tv_nsec = 0;
M
mamingshuai 已提交
466 467
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
        "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
468
    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
469 470
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
471 472 473 474 475 476 477 478 479

    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

static void *PthreadAllCom(void *arg)
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
480 481
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
482 483 484 485 486 487
    char rMsg[MQ_RX_LEN];

    queue = (mqd_t)arg;
    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    rts.tv_sec = time(NULL) + 1;
    rts.tv_nsec = 0;
M
mamingshuai 已提交
488 489
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
        "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
490
    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
491 492
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
493 494 495 496
    tts.tv_sec = time(NULL) + 1;
    tts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) << "ERROR: mq_timedsend() != 0";

M
mamingshuai 已提交
497
    return nullptr;
W
wenjun 已提交
498 499
}

M
mamingshuai 已提交
500
/* *
W
wenjun 已提交
501 502 503 504
 * @tc.number SUB_KERNEL_IPC_MQ_0900
 * @tc.name   all send and all receive function test in thread and process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
505
HWTEST_F(IpcMqTest, testMqAllTwoThreadCom, Function | MediumTest | Level1)
W
wenjun 已提交
506 507 508 509
{
    mqd_t queue;
    pthread_t tid;
    unsigned int prio;
M
mamingshuai 已提交
510 511
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
512 513
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
514
    sprintf_s(qName, sizeof(qName), "testMqAllTwoThreadCom_%d", GetRandom(10000));
W
wenjun 已提交
515 516 517 518 519 520 521 522 523 524 525
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(pthread_create(&tid, NULL, PthreadAllCom, (void *)queue) != -1) << "ERROR: pthread_create() == -1";

    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
    EXPECT_TRUE(pthread_join(tid, NULL) == 0) << "ERROR: pthread_join() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
526 527
    EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
                                         << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
W
wenjun 已提交
528 529
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
    EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
530 531
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG << ", received = " <<
        rMsg;
W
wenjun 已提交
532 533 534 535 536

    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
537
/* *
W
wenjun 已提交
538 539 540 541
 * @tc.number SUB_KERNEL_IPC_MQ_1000
 * @tc.name   all send and all receive function test in more process
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
542
HWTEST_F(IpcMqTest, testMqThreeLevelCom, Function | MediumTest | Level2)
W
wenjun 已提交
543 544 545 546
{
    mqd_t queue;
    pid_t pid;
    unsigned int prio;
M
mamingshuai 已提交
547 548 549
    struct timespec tts { 0 }, rts = { 0 };
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
550 551
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

M
mamingshuai 已提交
552
    sprintf_s(qName, sizeof(qName), "testMqThreeLevelCom_%d", GetRandom(10000));
W
wenjun 已提交
553 554 555 556 557 558 559 560 561 562 563 564
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    pid = fork();
    if (pid < 0) {
        goto FINISH;
    } else if (pid == 0) {
        pid = fork();
        if (pid == 0) {
            EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
565 566 567
            EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1"
                                                                                  << " errno = " << errno <<
                " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
568
            EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
569 570
            EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
                ", received = " << rMsg;
W
wenjun 已提交
571 572 573 574 575 576 577
            EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
        } else {
            int status;

            pid = waitpid(pid, &status, 0);
            EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
            EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
M
mamingshuai 已提交
578 579
            EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
                                                  << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);
W
wenjun 已提交
580 581 582 583

            EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
            rts.tv_sec = time(NULL) + 1;
            rts.tv_nsec = 0;
M
mamingshuai 已提交
584 585
            EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
                "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
586
            EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
587 588
            EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
                ", received = " << rMsg;
W
wenjun 已提交
589 590 591

            tts.tv_sec = time(NULL) + 1;
            tts.tv_nsec = 0;
M
mamingshuai 已提交
592 593
            EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &tts) == 0) <<
                "ERROR: mq_timedsend() != 0";
W
wenjun 已提交
594 595 596 597 598 599 600 601 602
        }
        exit(0);
    } else {
        int status;
        EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";

        pid = waitpid(pid, &status, 0);
        EXPECT_TRUE(pid != 0) << "ERROR: pid == 0";
        EXPECT_TRUE(WIFEXITED(status) != 0) << "ERROR: WIFEXITED(status) == 0";
M
mamingshuai 已提交
603
        EXPECT_TRUE(WEXITSTATUS(status) == 0) << "ERROR: WEXITSTATUS(status) != 0"
W
wenjun 已提交
604 605 606
                                              << ", WEXITSTATUS(status) = " << WEXITSTATUS(status);

        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
607
        EXPECT_TRUE(getAttr.mq_curmsgs == 1) << "ERROR: getAttr.mq_curmsgs != 1"
W
wenjun 已提交
608 609 610
                                             << ", getAttr.mq_curmsgs = " << getAttr.mq_curmsgs;
        rts.tv_sec = time(NULL) + 1;
        rts.tv_nsec = 0;
M
mamingshuai 已提交
611 612
        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &rts) != -1) <<
            "ERROR: mq_timedreceive() == -1";
W
wenjun 已提交
613
        EXPECT_TRUE(prio == MQ_MSG_PRIO) << "ERROR: prio != MQ_MSG_PRIO, prio = " << prio;
M
mamingshuai 已提交
614 615
        EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "sent != received: sent = " << MQ_MSG <<
            ", received = " << rMsg;
W
wenjun 已提交
616 617 618 619 620 621 622
    }

FINISH:
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
623
/* *
W
wenjun 已提交
624 625 626 627
 * @tc.number SUB_KERNEL_IPC_MQ_1100
 * @tc.name   mq_send and mq_receive max packet function test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
628
HWTEST_F(IpcMqTest, testMqMinMaxLen, Function | MediumTest | Level2)
W
wenjun 已提交
629 630 631
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
632 633
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
634 635
    char qName[MQ_NAME_LEN], rMsg[MAX_MQ_MSG_SIZE], sMsg[MAX_MQ_MSG_SIZE];

M
mamingshuai 已提交
636
    for (int i = 0; i < MAX_MQ_MSG_SIZE; i++) {
W
wenjun 已提交
637 638 639 640
        sMsg[i] = 0x36;
        rMsg[i] = 0x00;
    }

M
mamingshuai 已提交
641
    sprintf_s(qName, sizeof(qName), "testMqMaxLen_%d", GetRandom(10000));
W
wenjun 已提交
642 643 644
    setAttr.mq_msgsize = MAX_MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
M
mamingshuai 已提交
645 646
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1"
                                    << " errno = " << errno << " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
647 648 649
    EXPECT_TRUE(mq_send(queue, sMsg, MAX_MQ_MSG_SIZE, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
M
mamingshuai 已提交
650 651
    EXPECT_TRUE(strncmp(sMsg, rMsg, MAX_MQ_MSG_SIZE) == 0) << "sent != received: sent = " << sMsg << ", received = " <<
        rMsg;
W
wenjun 已提交
652 653 654 655
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
656
/* *
W
wenjun 已提交
657 658 659 660
 * @tc.number SUB_KERNEL_IPC_MQ_1200
 * @tc.name   mq_setattr set and clean mq_flags for O_NONBLOCK function test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
661
HWTEST_F(IpcMqTest, testMqSetGetAttr, Function | MediumTest | Level1)
W
wenjun 已提交
662 663
{
    mqd_t queue;
M
mamingshuai 已提交
664 665 666
    struct mq_attr getAttr = { 0 };
    struct mq_attr setAttr = { 0 };
    char qName[MQ_NAME_LEN];
W
wenjun 已提交
667

M
mamingshuai 已提交
668
    sprintf_s(qName, sizeof(qName), "testMqFunction_%d", GetRandom(10000));
W
wenjun 已提交
669

M
mamingshuai 已提交
670
    memset_s(&setAttr, sizeof(setAttr), 0, sizeof(setAttr));
W
wenjun 已提交
671 672 673 674 675 676
    setAttr.mq_msgsize = MQ_MSG_SIZE;
    setAttr.mq_maxmsg = MQ_MAX_MSG;
    setAttr.mq_flags = O_NONBLOCK;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

M
mamingshuai 已提交
677
    memset_s(&getAttr, sizeof(getAttr), 0, sizeof(getAttr));
W
wenjun 已提交
678 679 680 681 682 683 684 685
    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    EXPECT_TRUE((getAttr.mq_flags & O_NONBLOCK) == O_NONBLOCK);

    setAttr.mq_flags &= ~O_NONBLOCK;
    EXPECT_TRUE(mq_setattr(queue, &setAttr, NULL) == 0) << "ERROR: mq_setattr() != 0";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    LOG("setAttr.mq_flags = 0x%08x, getAttr.mq_flags = 0x%08x ", setAttr.mq_flags, getAttr.mq_flags);
M
mamingshuai 已提交
686 687 688
    EXPECT_TRUE((getAttr.mq_flags & O_NONBLOCK) == 0) << "ERROR: getAttr.mq_flags & O_NONBLOCK != 0,"
        " getAttr.mq_flags = " <<
        getAttr.mq_flags << " setAttr.mq_flags = " << setAttr.mq_flags;
W
wenjun 已提交
689
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
M
mamingshuai 已提交
690 691
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0"
                                       << " errno = " << errno << " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
692
}