1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "It_posix_queue.h"
31
PthreadF01(VOID * argument)32 static VOID *PthreadF01(VOID *argument)
33 {
34 INT32 i;
35 INT32 ret;
36 const CHAR *msgptr = MQUEUE_SEND_STRING_TEST;
37
38 g_testCount = 1;
39
40 g_mqueueTaskPID = LOS_CurTaskIDGet();
41
42 for (i = 0; i < 5 + 1; i++) { // 5, The loop frequency.
43 ret = mq_send(g_gqueue, msgptr, strlen(msgptr), 0);
44 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
45 }
46 ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, Here, assert the g_testCount.
47
48 g_testCount = 2; // 2, Init test count value.
49
50 return NULL;
51 EXIT:
52 g_testCount = 0;
53 return NULL;
54 }
55
PthreadF02(VOID * argument)56 static VOID *PthreadF02(VOID *argument)
57 {
58 INT32 ret;
59
60 CHAR msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = "";
61
62 g_testCount = 3; // 3, Init test count value.
63
64 ret = mq_receive(g_gqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
65 ICUNIT_GOTO_EQUAL(ret, strlen(MQUEUE_SEND_STRING_TEST), ret, EXIT);
66
67 ret = mq_close(g_gqueue);
68 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
69
70 ret = mq_unlink(g_gqname);
71 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
72
73 g_testCount = 4; // 4, Init test count value.
74
75 return NULL;
76 EXIT:
77 g_testCount = 0;
78 return NULL;
79 }
80
Testcase(VOID)81 static UINT32 Testcase(VOID)
82 {
83 pthread_t newTh1, newTh2;
84 UINT32 ret;
85 struct mq_attr attr = {0};
86 pthread_attr_t attr1;
87
88 ret = snprintf_s(g_gqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
89 "/mq025_%d", LOS_CurTaskIDGet());
90 ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT3);
91
92 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
93 attr.mq_maxmsg = 5; // 5, queue max message size.
94
95 g_testCount = 0;
96
97 g_gqueue = mq_open(g_gqname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR, &attr);
98 ICUNIT_GOTO_NOT_EQUAL(g_gqueue, (mqd_t)-1, g_gqueue, EXIT);
99
100 ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST1);
101 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
102
103 ret = pthread_create(&newTh1, &attr1, PthreadF01, NULL);
104 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
105
106 MqueueTaskDelay(5); // 5, Set delay time.
107 ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT1);
108
109 ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST2);
110 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
111
112 ret = pthread_create(&newTh2, &attr1, PthreadF02, NULL);
113 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
114
115 ret = MqueueTaskDelay(5); // 5, Set delay time.
116 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
117
118 ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT2); // 4, Here, assert the g_testCount.
119
120 ret = PosixPthreadDestroy(&attr1, newTh2);
121 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
122
123 ret = PosixPthreadDestroy(&attr1, newTh1);
124 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
125
126 return LOS_OK;
127
128 EXIT2:
129 PosixPthreadDestroy(&attr1, newTh2);
130 EXIT1:
131 PosixPthreadDestroy(&attr1, newTh1);
132 EXIT:
133 mq_close(g_gqueue);
134 mq_unlink(g_gqname);
135 EXIT3:
136 return LOS_NOK;
137 }
138
139 /**
140 * @tc.name: ItPosixQueue025
141 * @tc.desc: Test interface mq_send
142 * @tc.type: FUNC
143 * @tc.require: issueI6148G
144 */
145
ItPosixQueue025(VOID)146 VOID ItPosixQueue025(VOID)
147 {
148 TEST_ADD_CASE("ItPosixQueue025", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
149 }
150