IpcMqExceptionTest.cpp 33.9 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 <stdio.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.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_OPEN_0100
 * @tc.name   mq_open function errno for EEXIST test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
34
HWTEST_F(IpcMqTest, testMqOpenEEXIST, Function | MediumTest | Level2)
W
wenjun 已提交
35 36 37 38
{
    char qName[MQ_NAME_LEN];
    mqd_t queue, queueOther;

M
mamingshuai 已提交
39
    sprintf_s(qName, sizeof(qName), "testMqOpenEEXIST_%d", GetRandom(10000));
W
wenjun 已提交
40 41 42 43 44
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    queueOther = mq_open(qName, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, NULL);
    EXPECT_TRUE(queueOther == (mqd_t)-1) << "ERROR: mq_open() != (mqd_t)-1, queueOther = " << queueOther;
M
mamingshuai 已提交
45 46
    EXPECT_TRUE(errno == EEXIST) << "ERROR: errno != EEXIST"
                                 << " errno = " << errno << " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
47 48 49 50
    EXPECT_TRUE(mq_close(queue) == 0) << "ERROR: mq_close() != 0";
    ASSERT_TRUE(mq_unlink(qName) == 0) << "ERROR: mq_unlink() != 0";
}

M
mamingshuai 已提交
51
/* *
W
wenjun 已提交
52 53 54 55
 * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0200
 * @tc.name   mq_open function errno for EINVAL test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
56
HWTEST_F(IpcMqTest, testMqOpenEINVAL, Function | MediumTest | Level2)
W
wenjun 已提交
57 58 59
{
    int i;
    mqd_t queue;
M
mamingshuai 已提交
60
    struct mq_attr attr = { 0 };
W
wenjun 已提交
61 62 63
    char qName[MQ_NAME_LEN];
    const int max = 65535;

M
mamingshuai 已提交
64
    sprintf_s(qName, sizeof(qName), "testMqOpenEINVAL_%d", GetRandom(10000));
W
wenjun 已提交
65

M
mamingshuai 已提交
66
    for (i = 0; i < 6; i++) {
W
wenjun 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        switch (i) {
            case 0:
                attr.mq_msgsize = -1;
                attr.mq_maxmsg = max;
                break;
            case 1:
                /* attr.mq_msgsize > USHRT_MAX - 4 */
                attr.mq_msgsize = max;
                attr.mq_maxmsg = max;
                break;
            case 2:
                attr.mq_msgsize = 10;
                attr.mq_maxmsg = -1;
                break;
            case 3:
                attr.mq_msgsize = 10;
                attr.mq_maxmsg = max + 1;
                break;

            case 4:
                attr.mq_msgsize = 0;
                attr.mq_maxmsg = 16;
                break;

            case 5:
                attr.mq_msgsize = 64;
                attr.mq_maxmsg = 0;
                break;
        }

        queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
        EXPECT_TRUE(queue == (mqd_t)-1) << "ERROR: mq_open() != (mqd_t)-1, i = " << i;

        if (queue != (mqd_t)-1) {
            mq_close(queue);
            mq_unlink(qName);
        }

        /* if NOT call mq_close & mq_unlink then errno == ENOENT */
        EXPECT_TRUE(errno == EINVAL) << "errno != EINVAL, errno = " << errno << " i = " << i << " qName = " << qName;
    }

M
mamingshuai 已提交
109
    for (i = 0; i < MQ_NAME_LEN; i++) {
W
wenjun 已提交
110 111 112 113 114 115 116 117
        qName[i] = 0;
    }
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    EXPECT_TRUE(errno == EINVAL) << "errno != EINVAL, errno = " << errno << " i = " << i << " qName = " << qName;
}

M
mamingshuai 已提交
118
/* *
W
wenjun 已提交
119 120 121 122
 * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0300
 * @tc.name   mq_open function errno for ENAMETOOLONG test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
123
HWTEST_F(IpcMqTest, testMqOpenENAMETOOLONG, Function | MediumTest | Level2)
W
wenjun 已提交
124 125 126 127 128
{
    char qName[MAX_MQ_NAME_LEN + 10];
    mqd_t queue;
    int i;

M
mamingshuai 已提交
129
    for (i = 0; i < MAX_MQ_NAME_LEN + 5; i++) {
W
wenjun 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        qName[i] = '8';
    }
    qName[i] = '\0';

    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
    EXPECT_TRUE(queue == (mqd_t)-1) << "ERROR: mq_open() != (mqd_t)-1 ";

    if (queue != (mqd_t)-1) {
        mq_close(queue);
        mq_unlink(qName);
    }

    ASSERT_TRUE(errno == ENAMETOOLONG) << "errno != ENAMETOOLONG, errno = " << errno;
}

M
mamingshuai 已提交
145
/* *
W
wenjun 已提交
146 147 148 149
 * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0400
 * @tc.name   mq_open function errno for ENOENT test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
150
HWTEST_F(IpcMqTest, testMqOpenENOENT, Function | MediumTest | Level3)
W
wenjun 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{
    mqd_t queue;
    char qName[MQ_NAME_LEN];

    sprintf(qName, "testMqOpenENOENT_%d", GetRandom(10000));
    queue = mq_open(qName, O_RDWR, S_IRUSR | S_IWUSR, NULL);
    EXPECT_TRUE(queue == (mqd_t)-1) << "ERROR: mq_open() != (mqd_t)-1 ";

    if (queue != (mqd_t)-1) {
        mq_close(queue);
        mq_unlink(qName);
    }
    ASSERT_TRUE(errno == ENOENT) << "errno != ENOENT, errno = " << errno;
}

M
mamingshuai 已提交
166
/* *
W
wenjun 已提交
167 168 169 170
 * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0500
 * @tc.name   mq_open function errno for ENFILE test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
171
HWTEST_F(IpcMqTest, testMqOpenENFILE, Function | MediumTest | Level3)
W
wenjun 已提交
172 173 174
{
    char qName[MAX_MQ_NUMBER + 1][30];
    mqd_t queue[MAX_MQ_NUMBER + 1];
M
mamingshuai 已提交
175 176 177
    int flag = 0;
    int i;
    for (i = 0; i < MAX_MQ_NUMBER + 1; i++) {
W
wenjun 已提交
178 179 180
        sprintf(qName[i], "testMqOpenENFILE_%d", i);
    }

M
mamingshuai 已提交
181
    for (i = 0; i < MAX_MQ_NUMBER; i++) {
W
wenjun 已提交
182 183 184 185 186 187 188 189
        queue[i] = mq_open(qName[i], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);

        if (queue[i] == (mqd_t)-1) {
            flag = 1;
            LOG("break: i = %d", i);
            break;
        }

M
mamingshuai 已提交
190 191
        EXPECT_TRUE(queue[i] != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1, i = " << i << " errno = " << errno <<
            " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
192 193 194 195 196 197
    }

    LOG("func: i = %d", i);
    if (flag == 0) {
        queue[i] = mq_open(qName[i], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
    }
M
mamingshuai 已提交
198 199 200 201
    EXPECT_TRUE(queue[i] == (mqd_t)-1) << "ERROR: mq_open() != (mqd_t)-1, i = " << i << " queue = " << queue[i] <<
        " errno = " << errno << " strerror(errno) = " << strerror(errno);
    EXPECT_TRUE(errno == ENFILE) << "ERROR: errno != ENFILE, i = " << i << " errno = " << errno <<
        " strerror(errno) = " << strerror(errno);
W
wenjun 已提交
202

M
mamingshuai 已提交
203
    for (i = 0; i < MAX_MQ_NUMBER + 1; i++) {
W
wenjun 已提交
204 205 206 207 208
        mq_close(queue[i]);
        mq_unlink(qName[i]);
    }
}

M
mamingshuai 已提交
209
/* *
W
wenjun 已提交
210 211 212 213
 * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0600
 * @tc.name   mq_open function errno for ENOSPC test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
214
HWTEST_F(IpcMqTest, testMqOpenENOSPC, Function | MediumTest | Level3)
W
wenjun 已提交
215 216
{
    mqd_t queue;
M
mamingshuai 已提交
217
    struct mq_attr setAttr = { 0 };
W
wenjun 已提交
218 219 220 221
    char qName[MQ_NAME_LEN];

    sprintf(qName, "testMqOpenENOSPC_%d", GetRandom(10000));
    setAttr.mq_msgsize = MAX_MQ_MSG_SIZE + 1;
M
mamingshuai 已提交
222
    setAttr.mq_maxmsg = MAX_MQ_NAME_LEN;
W
wenjun 已提交
223 224 225 226 227 228 229 230 231 232
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &setAttr);
    EXPECT_TRUE(queue == (mqd_t)-1) << "ERROR: mq_open() != (mqd_t)-1 ";

    if (queue != (mqd_t)-1) {
        mq_close(queue);
        mq_unlink(qName);
    }
    ASSERT_TRUE(errno == ENOSPC) << "errno != ENOSPC, errno = " << errno;
}

M
mamingshuai 已提交
233
/* *
W
wenjun 已提交
234 235 236 237
 * @tc.number SUB_KERNEL_IPC_MQ_CLOSE_0100
 * @tc.name   mq_close function errno for EBADF test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
238
HWTEST_F(IpcMqTest, testMqCloseEBADF, Function | MediumTest | Level2)
W
wenjun 已提交
239 240 241 242 243
{
    ASSERT_TRUE(mq_close(NULL) == -1) << "ERROR: mq_close() != -1";
    ASSERT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;
}

M
mamingshuai 已提交
244
/* *
W
wenjun 已提交
245 246 247 248
 * @tc.number SUB_KERNEL_IPC_MQ_SEND_0100
 * @tc.name   mq_send function errno for EAGAIN test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
249
HWTEST_F(IpcMqTest, testMqSendEAGAIN, Function | MediumTest | Level2)
W
wenjun 已提交
250 251
{
    mqd_t queue;
M
mamingshuai 已提交
252
    struct mq_attr attr = { 0 };
W
wenjun 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    char qName[MQ_NAME_LEN];

    sprintf(qName, "testMqSendEAGAIN_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = 1;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, 0) == 0) << "ERROR: mq_send() != 0";
    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, 0) == -1) << "ERROR: mq_send() != -1";
    EXPECT_TRUE(errno == EAGAIN) << "ERROR: errno != EAGAIN, errno = " << errno << " EAGAIN = " << EAGAIN;

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

M
mamingshuai 已提交
269
/* *
W
wenjun 已提交
270 271 272 273
 * @tc.number SUB_KERNEL_IPC_MQ_SEND_0200
 * @tc.name   mq_send function errno for EBADF and EMSGSIZE test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
274
HWTEST_F(IpcMqTest, testMqSendEBADFEMSGSIZE, Function | MediumTest | Level2)
W
wenjun 已提交
275 276
{
    mqd_t queue;
M
mamingshuai 已提交
277
    struct mq_attr attr = { 0 };
W
wenjun 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    char qName[MQ_NAME_LEN];

    sprintf(qName, "testMqSendEAGAIN_%d", GetRandom(10000));
    attr.mq_msgsize = 1;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_send(NULL, MQ_MSG, 1, MQ_MSG_PRIO) == -1) << "ERROR: mq_send() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == -1) << "ERROR: mq_send() != -1";
    EXPECT_TRUE(errno == EMSGSIZE) << "ERROR: errno != EMSGSIZE, errno = " << errno << " EMSGSIZE = " << EMSGSIZE;

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

    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    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) == -1) << "ERROR: mq_send() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    attr.mq_flags |= O_NONBLOCK;
    EXPECT_TRUE(mq_setattr(queue, &attr, NULL) == 0) << "ERROR: mq_setattr() != 0";
    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == -1) << "ERROR: mq_send() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

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

M
mamingshuai 已提交
312
/* *
W
wenjun 已提交
313 314 315 316
 * @tc.number SUB_KERNEL_IPC_MQ_SEND_0300
 * @tc.name   mq_send function errno for EINVAL  test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
317
HWTEST_F(IpcMqTest, testMqSendEINVAL, Function | MediumTest | Level3)
W
wenjun 已提交
318 319
{
    mqd_t queue;
M
mamingshuai 已提交
320
    struct mq_attr attr = { 0 };
W
wenjun 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    char qName[MQ_NAME_LEN];

    sprintf(qName, "testMqSendEINVAL_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_send(queue, MQ_MSG, 0, MQ_MSG_PRIO) == -1) << "ERROR: mq_send() != -1";
    EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL;

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

M
mamingshuai 已提交
336
/* *
W
wenjun 已提交
337 338 339 340
 * @tc.number SUB_KERNEL_IPC_MQ_RECEIVE_0100
 * @tc.name   mq_receive function errno for EAGAIN test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
341
HWTEST_F(IpcMqTest, testMqReceiveEAGAIN, Function | MediumTest | Level2)
W
wenjun 已提交
342 343 344
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
345 346
    struct mq_attr attr = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
347 348 349 350 351 352 353 354 355 356 357 358
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

    sprintf(qName, "testMqReceiveEAGAIN_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    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";
    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 已提交
359 360
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "ERROR: strncmp() != 0, sent != received: sent = " <<
        MQ_MSG << ", received = " << rMsg;
W
wenjun 已提交
361 362 363 364 365 366 367
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) == -1) << "ERROR: mq_receive() != -1";
    EXPECT_TRUE(errno == EAGAIN) << "ERROR: errno != EAGAIN, errno = " << errno << " EAGAIN = " << EAGAIN;

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

M
mamingshuai 已提交
368
/* *
W
wenjun 已提交
369 370 371 372
 * @tc.number SUB_KERNEL_IPC_MQ_RECEIVE_0200
 * @tc.name   mq_receive function errno for EBADF and EMSGSIZE test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
373
HWTEST_F(IpcMqTest, testMqReceiveEBADFEMSGSIZE, Function | MediumTest | Level2)
W
wenjun 已提交
374 375 376
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
377 378
    struct mq_attr attr = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
379 380 381 382 383 384 385 386 387 388 389
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN], rMsgErr[1];

    sprintf(qName, "testMqReceiveEAGAIN_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    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";
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) != -1) << "ERROR: mq_receive() == -1";
M
mamingshuai 已提交
390 391
    EXPECT_TRUE(strncmp(MQ_MSG, rMsg, MQ_MSG_LEN) == 0) << "ERROR: strncmp() != 0, sent != received: sent = " <<
        MQ_MSG << ", received = " << rMsg;
W
wenjun 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

    EXPECT_TRUE(mq_receive(NULL, rMsg, getAttr.mq_msgsize, &prio) == -1) << "ERROR: mq_receive() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    EXPECT_TRUE(mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO) == 0) << "ERROR: mq_send() != 0";
    EXPECT_TRUE(mq_receive(queue, rMsgErr, sizeof(rMsgErr), &prio) == -1) << "ERROR: mq_receive() != -1";
    EXPECT_TRUE(errno == EMSGSIZE) << "ERROR: errno != EMSGSIZE, errno = " << errno << " EMSGSIZE = " << EMSGSIZE;

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

    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_WRONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) == -1) << "ERROR: mq_receive() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    attr.mq_flags |= O_NONBLOCK;
    EXPECT_TRUE(mq_setattr(queue, &attr, NULL) == 0) << "ERROR: mq_setattr() != 0";
    EXPECT_TRUE(mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio) == -1) << "ERROR: mq_receive() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

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

M
mamingshuai 已提交
420
/* *
W
wenjun 已提交
421 422 423 424
 * @tc.number SUB_KERNEL_IPC_MQ_RECEIVE_0300
 * @tc.name   mq_receive function errno for EINVAL test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
425
HWTEST_F(IpcMqTest, testMqReceiveEINVAL, Function | MediumTest | Level3)
W
wenjun 已提交
426 427 428
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
429
    struct mq_attr attr = { 0 };
W
wenjun 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

    sprintf(qName, "testMqReceiveEINVAL_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_receive(queue, rMsg, 0, &prio) == -1) << "ERROR: mq_receive() != -1";
    EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL;

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

M
mamingshuai 已提交
445 446
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0100
W
wenjun 已提交
447 448 449
 * @tc.name   mq_timedsend function errno for EAGAIN and EBADF test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
450
HWTEST_F(IpcMqTest, testMqTimedSendEAGAINEBADF, Function | MediumTest | Level2)
W
wenjun 已提交
451 452
{
    mqd_t queue;
M
mamingshuai 已提交
453 454 455
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    char qName[MQ_NAME_LEN];
W
wenjun 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494

    sprintf(qName, "testMqTimedSendEAGAIN_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = 1;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

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

    ts.tv_sec = 0;
    ts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts) == -1) << "ERROR: mq_timedsend() != 0";
    EXPECT_TRUE(errno == EAGAIN) << "ERROR: errno != EAGAIN, errno = " << errno << " EAGAIN = " << EAGAIN;

    EXPECT_TRUE(mq_timedsend(NULL, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts) == -1) << "ERROR: mq_timedsend() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

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

    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts) == -1) << "ERROR: mq_timedsend() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    attr.mq_flags |= O_NONBLOCK;
    EXPECT_TRUE(mq_setattr(queue, &attr, NULL) == 0) << "ERROR: mq_setattr() != 0";
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts) == -1) << "ERROR: mq_timedsend() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

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

M
mamingshuai 已提交
495 496
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0200
W
wenjun 已提交
497 498 499
 * @tc.name   mq_timedsend function errno for EINVAL test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
500
HWTEST_F(IpcMqTest, testMqTimedSendEINVAL, Function | MediumTest | Level2)
W
wenjun 已提交
501 502 503
{
    mqd_t queue;
    int i;
M
mamingshuai 已提交
504 505 506
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    char qName[MQ_NAME_LEN];
W
wenjun 已提交
507 508 509 510 511 512 513

    sprintf(qName, "testMqTimedSendEINVAL_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

M
mamingshuai 已提交
514
    for (i = 0; i < 3; i++) {
W
wenjun 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
        switch (i) {
            case 0:
                ts.tv_sec = -1;
                ts.tv_nsec = 0;
                break;
            case 1:
                ts.tv_sec = time(NULL) + 1;
                ts.tv_nsec = -1;
                break;
            case 2:
                ts.tv_sec = time(NULL) + 1;
                ts.tv_nsec = 1000000000UL + 1UL;
                break;
        }

M
mamingshuai 已提交
530 531 532 533
        EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts) == -1) <<
            "ERROR: mq_timedsend() != -1, i = " << i;
        EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL <<
            " i = " << i;
W
wenjun 已提交
534 535
    }

M
mamingshuai 已提交
536 537
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, 0, MQ_MSG_PRIO, &ts) == -1) << "ERROR: mq_timedsend() != -1";
    EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL;
W
wenjun 已提交
538 539 540 541 542

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

M
mamingshuai 已提交
543 544
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0300
W
wenjun 已提交
545 546 547
 * @tc.name   mq_timedsend function errno for EMSGSIZE test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
548
HWTEST_F(IpcMqTest, testMqTimedSendEMSGSIZE, Function | MediumTest | Level3)
W
wenjun 已提交
549 550
{
    mqd_t queue;
M
mamingshuai 已提交
551 552 553
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    char qName[MQ_NAME_LEN];
W
wenjun 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

    sprintf(qName, "testMqTimedSendEMSGSIZE_%d", GetRandom(10000));
    attr.mq_msgsize = 1;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

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

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

M
mamingshuai 已提交
570 571
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0400
W
wenjun 已提交
572 573 574
 * @tc.name   mq_timedsend function errno for ETIMEDOUT test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
575
HWTEST_F(IpcMqTest, testMqTimedSendETIMEDOUT, Function | MediumTest | Level3)
W
wenjun 已提交
576 577
{
    mqd_t queue;
M
mamingshuai 已提交
578 579 580
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    char qName[MQ_NAME_LEN];
W
wenjun 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

    sprintf(qName, "testMqTimedSendETIMEDOUT_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = 1;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

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

    ts.tv_sec = 0;
    ts.tv_nsec = 100;
    EXPECT_TRUE(mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts) == -1) << "ERROR: mq_timedsend() != -1";
    EXPECT_TRUE(errno == ETIMEDOUT) << "ERROR: errno != ETIMEDOUT, errno = " << errno << " ETIMEDOUT = " << ETIMEDOUT;

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

M
mamingshuai 已提交
601 602
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0100
W
wenjun 已提交
603 604 605
 * @tc.name   mq_timedreceive function errno for EAGAIN and EBADF test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
606
HWTEST_F(IpcMqTest, testMqTimedReceiveEAGAINEBADF, Function | MediumTest | Level2)
W
wenjun 已提交
607 608 609
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
610 611 612
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

    sprintf(qName, "testMqTimedReceiveEAGAIN_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    ts.tv_sec = time(NULL) + 1;
    ts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == EAGAIN) << "ERROR: errno != EAGAIN, errno = " << errno << " EAGAIN = " << EAGAIN;

    EXPECT_TRUE(mq_timedreceive(NULL, rMsg, getAttr.mq_msgsize, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

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

    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_WRONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_timedreceive(NULL, rMsg, getAttr.mq_msgsize, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    attr.mq_flags |= O_NONBLOCK;
    EXPECT_TRUE(mq_setattr(queue, &attr, NULL) == 0) << "ERROR: mq_setattr() != 0";
    EXPECT_TRUE(mq_timedreceive(NULL, rMsg, getAttr.mq_msgsize, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

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

M
mamingshuai 已提交
650 651
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0200
W
wenjun 已提交
652 653 654
 * @tc.name   mq_timedreceive function errno for EINVAL test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
655
HWTEST_F(IpcMqTest, testMqTimedReceiveEINVAL, Function | MediumTest | Level2)
W
wenjun 已提交
656 657 658 659
{
    mqd_t queue;
    int i;
    unsigned int prio;
M
mamingshuai 已提交
660 661 662
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
663 664 665 666 667 668 669 670
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

    sprintf(qName, "testMqTimedReceiveEINVAL_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

M
mamingshuai 已提交
671
    for (i = 0; i < 3; i++) {
W
wenjun 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
        switch (i) {
            case 0:
                ts.tv_sec = -1;
                ts.tv_nsec = 0;
                break;
            case 1:
                ts.tv_sec = time(NULL) + 1;
                ts.tv_nsec = -1;
                break;
            case 2:
                ts.tv_sec = time(NULL) + 1;
                ts.tv_nsec = 1000000000UL + 1UL;
                break;
        }

        EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
688 689 690 691
        EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &ts) == -1) <<
            "ERROR: mq_timedreceive() != -1, i = " << i;
        EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL <<
            " i = " << i;
W
wenjun 已提交
692 693
    }

M
mamingshuai 已提交
694 695
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, 0, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL;
W
wenjun 已提交
696 697 698 699 700

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

M
mamingshuai 已提交
701 702
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0300
W
wenjun 已提交
703 704 705
 * @tc.name   mq_timedreceive function errno for ETIMEDOUT test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
706
HWTEST_F(IpcMqTest, testMqTimedReceiveETIMEDOUT, Function | MediumTest | Level3)
W
wenjun 已提交
707 708 709
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
710 711 712
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
    struct mq_attr getAttr = { 0 };
W
wenjun 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

    sprintf(qName, "testMqTimedReceiveETIMEDOUT_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

    EXPECT_TRUE(mq_getattr(queue, &getAttr) == 0) << "ERROR: mq_getattr() != 0";
    ts.tv_sec = 0;
    ts.tv_nsec = 100;
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == ETIMEDOUT) << "ERROR: errno != ETIMEDOUT, errno = " << errno << " ETIMEDOUT = " << ETIMEDOUT;

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

M
mamingshuai 已提交
731 732
/* *
 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0400
W
wenjun 已提交
733 734 735
 * @tc.name   mq_timedreceive function errno for EMSGSIZE test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
736
HWTEST_F(IpcMqTest, testMqTimedReceiveEMSGSIZE, Function | MediumTest | Level3)
W
wenjun 已提交
737 738 739
{
    mqd_t queue;
    unsigned int prio;
M
mamingshuai 已提交
740 741
    struct timespec ts = { 0, 0 };
    struct mq_attr attr = { 0 };
W
wenjun 已提交
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
    char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];

    sprintf(qName, "testMqTimedReceiveEMSGSIZE_%d", GetRandom(10000));
    attr.mq_msgsize = MQ_MSG_SIZE;
    attr.mq_maxmsg = MQ_MAX_MSG;
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
    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";
    ts.tv_sec = time(NULL) + 1;
    ts.tv_nsec = 0;
    EXPECT_TRUE(mq_timedreceive(queue, rMsg, 1, &prio, &ts) == -1) << "ERROR: mq_timedreceive() != -1";
    EXPECT_TRUE(errno == EMSGSIZE) << "ERROR: errno != EMSGSIZE, errno = " << errno << " EMSGSIZE = " << EMSGSIZE;

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

M
mamingshuai 已提交
760
/* *
W
wenjun 已提交
761 762 763 764
 * @tc.number SUB_KERNEL_IPC_MQ_UNLINK_0100
 * @tc.name   mq_unlink function errno for ENAMETOOLONG test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
765
HWTEST_F(IpcMqTest, testMqUnlinkENAMETOOLONG, Function | MediumTest | Level2)
W
wenjun 已提交
766 767 768 769
{
    char qName[MAX_MQ_NAME_LEN + 10];
    int i;

M
mamingshuai 已提交
770
    for (i = 0; i < MAX_MQ_NAME_LEN + 5; i++) {
W
wenjun 已提交
771 772 773 774 775 776 777 778
        qName[i] = '8';
    }
    qName[i] = '\0';

    ASSERT_TRUE(mq_unlink(qName) == -1) << "ERROR: mq_unlink() != -1";
    ASSERT_TRUE(errno == ENAMETOOLONG) << "ERROR: errno != ENAMETOOLONG";
}

M
mamingshuai 已提交
779
/* *
W
wenjun 已提交
780 781 782 783
 * @tc.number SUB_KERNEL_IPC_MQ_UNLINK_0200
 * @tc.name   mq_unlink function errno for ENOENT test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
784
HWTEST_F(IpcMqTest, testMqUnlinkENOENT, Function | MediumTest | Level2)
W
wenjun 已提交
785 786 787 788 789 790 791
{
    char qName[MQ_NAME_LEN] = "/mq_file-does-not-exit";

    ASSERT_TRUE(mq_unlink(qName) == -1) << "ERROR: mq_unlink() != -1";
    ASSERT_TRUE(errno == ENOENT) << "ERROR: errno != ENOENT";
}

M
mamingshuai 已提交
792
/* *
W
wenjun 已提交
793 794 795 796
 * @tc.number SUB_KERNEL_IPC_MQ_UNLINK_0300
 * @tc.name   mq_unlink function errno for EINVAL test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
797
HWTEST_F(IpcMqTest, testMqUnlinkEINVAL, Function | MediumTest | Level3)
W
wenjun 已提交
798 799 800 801 802
{
    ASSERT_TRUE(mq_unlink("") == -1) << "ERROR: mq_unlink() != -1";
    ASSERT_TRUE(errno == EINVAL) << "ERROR: errno != ENOENT";
}

M
mamingshuai 已提交
803
/* *
W
wenjun 已提交
804 805 806 807
 * @tc.number SUB_KERNEL_IPC_MQ_GETATTR_0100
 * @tc.name   mq_getattr function errno for EBAD and EINVALF test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
808
HWTEST_F(IpcMqTest, testMqGetAttrEBADFEINVAL, Function | MediumTest | Level2)
W
wenjun 已提交
809 810 811
{
    int ret;
    mqd_t queue;
M
mamingshuai 已提交
812
    struct mq_attr mqstat = { 0 };
W
wenjun 已提交
813 814
    char qName[MQ_NAME_LEN];

M
mamingshuai 已提交
815
    memset_s(&mqstat, sizeof(mqstat), 0, sizeof(mqstat));
W
wenjun 已提交
816 817 818 819 820 821 822 823

    sprintf(qName, "testMqSendEINVAL_%d", GetRandom(10000));
    queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, NULL);

    ret = mq_getattr(NULL, &mqstat);
    EXPECT_TRUE(ret == -1) << "ERROR: mq_getattr() != -1, ret = " << ret;
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

M
mamingshuai 已提交
824
    ret = mq_getattr(queue, nullptr);
W
wenjun 已提交
825 826 827 828 829 830 831
    EXPECT_TRUE(ret == -1) << "ERROR: mq_getattr() != -1, ret = " << ret;
    EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL , errno = " << errno << " EINVAL  = " << EINVAL;

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

M
mamingshuai 已提交
832
/* *
W
wenjun 已提交
833 834 835 836
 * @tc.number SUB_KERNEL_IPC_MQ_SETATTR_0100
 * @tc.name   mq_receive function errno for EBADF and EINVAL test
 * @tc.desc   [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
837
HWTEST_F(IpcMqTest, testMqSetAttrEBADFEINVAL, Function | MediumTest | Level2)
W
wenjun 已提交
838 839 840
{
    char qName[MQ_NAME_LEN];
    mqd_t queue;
M
mamingshuai 已提交
841
    struct mq_attr gMqstat = { 0 }, sMqstat = { 0 };
W
wenjun 已提交
842 843

    sprintf(qName, "testMqSetAttrEBADFEINVAL_%d", GetRandom(10000));
M
mamingshuai 已提交
844
    queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
W
wenjun 已提交
845 846
    ASSERT_TRUE(queue != (mqd_t)-1) << "ERROR: mq_open() == (mqd_t)-1";

M
mamingshuai 已提交
847 848
    memset_s(&gMqstat, sizeof(gMqstat), 0, sizeof(gMqstat));
    memset_s(&sMqstat, sizeof(sMqstat), 0, sizeof(sMqstat));
W
wenjun 已提交
849 850 851 852 853
    EXPECT_TRUE(mq_getattr(queue, &gMqstat) == 0) << "ERROR: mq_getattr() != 0";

    sMqstat.mq_flags |= O_NONBLOCK;
    EXPECT_TRUE(mq_setattr(queue, &sMqstat, NULL) == 0) << "ERROR: mq_setattr() != 0";
    EXPECT_TRUE(mq_getattr(queue, &gMqstat) == 0) << "ERROR: mq_getattr() != 0";
M
mamingshuai 已提交
854 855
    EXPECT_TRUE(gMqstat.mq_flags != sMqstat.mq_flags) << "ERROR: gMqstat != sMqstat, gMqstat.mq_flags = " <<
        gMqstat.mq_flags << "sMqstat.mq_flags = " << sMqstat.mq_flags;
W
wenjun 已提交
856 857 858 859 860 861 862 863 864 865

    EXPECT_TRUE(mq_setattr(NULL, &sMqstat, NULL) == -1) << "ERROR: mq_setattr() != -1";
    EXPECT_TRUE(errno == EBADF) << "ERROR: errno != EBADF, errno = " << errno << " EBADF = " << EBADF;

    EXPECT_TRUE(mq_setattr(queue, NULL, NULL) == -1) << "ERROR: mq_setattr() != -1";
    EXPECT_TRUE(errno == EINVAL) << "ERROR: errno != EINVAL, errno = " << errno << " EINVAL = " << EINVAL;

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