/* * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "posix_test.h" #include #include #include "kernel_test.h" #include "log.h" #define MQUEUE_STANDARD_NAME_LENGTH 52 #define MQUEUE_NO_ERROR 0 #define MQUEUE_SEND_STRING_TEST "mq_test" #define MQUEUE_SHORT_ARRAY_LENGTH strlen(MQUEUE_SEND_STRING_TEST) const int MQ_NAME_LEN = 64; // mqueue name len const int MQ_TX_LEN = 64; // mqueue send buffer len const int MQ_RX_LEN = 64; // mqueue receive buffer len const int MQ_MSG_SIZE = 64; // mqueue message size const int MQ_MSG_PRIO = 0; // mqueue message priority const int MQ_MAX_MSG = 16; // mqueue message number const char MQ_MSG[] = "MessageToSend"; // mqueue message to send const int MQ_MSG_LEN = sizeof(MQ_MSG); // mqueue message len to send const int MAX_MQ_NUMBER = LOSCFG_BASE_IPC_QUEUE_LIMIT - 1; // max mqueue number const int MAX_MQ_NAME_LEN = 256; // max mqueue name length const int MAX_MQ_MSG_SIZE = 65530; // max mqueue message size // return n: 0 < n <= max static unsigned int GetRandom(unsigned int max) { if (max == 0 || max == 1) { return 1; } return (rand() % max) + 1; } /** * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency * @param : subsystem name is mqueue * @param : module name is mqueue * @param : test suit name is MqueueFuncTestSuite */ LITE_TEST_SUIT(Posix, Mqueue, MqueueFuncTestSuite); /** * @tc.setup : setup for all testcases * @return : setup result, TRUE is success, FALSE is fail */ static BOOL MqueueFuncTestSuiteSetUp(void) { return TRUE; } /** * @tc.teardown : teardown for all testcases * @return : teardown result, TRUE is success, FALSE is fail */ static BOOL MqueueFuncTestSuiteTearDown(void) { printf("+-------------------------------------------+\n"); return TRUE; } /** * @tc.number : SUB_KERNEL_PTHREAD_OPERATION_001 * @tc.name : event operation for create * @tc.desc : [C- SOFTWARE -0200] */ LITE_TEST_CASE(MqueueFuncTestSuite, TestMqueue001, Function | MediumTest | Level1) { unsigned int ret; char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0}; char mqname[MQUEUE_STANDARD_NAME_LENGTH] = ""; const char *msgptr = MQUEUE_SEND_STRING_TEST; struct mq_attr attr = { 0 }; mqd_t mqueue; attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH; attr.mq_maxmsg = 1; snprintf_s(mqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, "/mq002_%d", 1); mqueue = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); ICUNIT_GOTO_NOT_EQUAL(mqueue, (mqd_t)-1, mqueue, EXIT1); ret = mq_send(mqueue, msgptr, strlen(msgptr), 0); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1); ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL); ICUNIT_GOTO_EQUAL(ret, MQUEUE_SHORT_ARRAY_LENGTH, ret, EXIT1); ICUNIT_GOTO_STRING_EQUAL(msgrcd, MQUEUE_SEND_STRING_TEST, msgrcd, EXIT1); ret = mq_close(mqueue); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1); ret = mq_unlink(mqname); ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT); return 0; EXIT1: mq_close(mqueue); EXIT: mq_unlink(mqname); return 0; }; /** * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0100 * @tc.name mq_open function errno for EEXIST test * @tc.desc [C- SOFTWARE -0200] */ LITE_TEST_CASE(MqueueFuncTestSuite, TestMqOpenEEXIST, Function | MediumTest | Level2) { char qName[MQ_NAME_LEN]; mqd_t queue, queueOther; int ret; sprintf_s(qName, MQ_NAME_LEN, "testMqOpenEEXIST_%d", GetRandom(10000)); queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL); ICUNIT_GOTO_NOT_EQUAL(queue, (mqd_t)-1, queue, EXIT1); queueOther = mq_open(qName, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, NULL); ICUNIT_GOTO_EQUAL(queueOther, (mqd_t)-1, queueOther, EXIT1); ICUNIT_GOTO_EQUAL(errno, EEXIST, errno, EXIT1); EXIT1: ret = mq_close(queue); ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); EXIT: ret = mq_unlink(qName); ICUNIT_ASSERT_EQUAL(ret, 0, ret); return 0; } /** * @tc.number SUB_KERNEL_IPC_MQ_OPEN_0200 * @tc.name mq_open function errno for EINVAL test * @tc.desc [C- SOFTWARE -0200] */ LITE_TEST_CASE(MqueueFuncTestSuite, TestMqOpenEINVAL, Function | MediumTest | Level2) { int i; mqd_t queue; struct mq_attr attr = {0}; char qName[MQ_NAME_LEN]; const int max = 65535; sprintf_s(qName, MQ_NAME_LEN, "testMqOpenEINVAL_%d", GetRandom(10000)); for (i = 0; i<6; i++) { 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); ICUNIT_TRACK_EQUAL(queue, (mqd_t)-1, queue); if (queue != (mqd_t)-1) { mq_close(queue); mq_unlink(qName); } /* if NOT call mq_close & mq_unlink then errno == ENOENT */ ICUNIT_TRACK_EQUAL(errno, EINVAL, errno); } for (i=0; i