1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include "It_posix_queue.h"
32
PthreadF01(VOID * mq)33 static VOID *PthreadF01(VOID *mq)
34 {
35 INT32 i, ret;
36 mqd_t mqueue1 = *(mqd_t *)mq;
37 const CHAR *msgptr[] = { "msg test 1", "msg test 2", "msg test 3" };
38
39 for (i = 0; i < 3; i++) { // 3, the loop frequency.
40 ret = mq_send(mqueue1, msgptr[i], MQUEUE_STANDARD_NAME_LENGTH, 0);
41 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
42 }
43
44 LOS_AtomicInc(&g_testCount);
45
46 pthread_exit(nullptr);
47
48 return NULL;
49 EXIT:
50 pthread_exit(nullptr);
51 g_testCount = 0;
52 return NULL;
53 }
54
PthreadF02(VOID * mq)55 static VOID *PthreadF02(VOID *mq)
56 {
57 INT32 i, ret;
58 mqd_t mqueue2 = *(mqd_t *)mq;
59 const CHAR *msgptr[] = { "msg test 1", "msg test 2", "msg test 3" };
60
61 for (i = 0; i < 3; i++) { // 3, the loop frequency.
62 ret = mq_send(mqueue2, msgptr[i], MQUEUE_STANDARD_NAME_LENGTH, 0);
63 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
64 }
65 LOS_AtomicInc(&g_testCount);
66
67 pthread_exit(nullptr);
68
69 return NULL;
70 EXIT:
71 pthread_exit(nullptr);
72 g_testCount = 0;
73 return NULL;
74 }
75
PthreadF03(VOID * mq)76 static VOID *PthreadF03(VOID *mq)
77 {
78 INT32 i, ret;
79 mqd_t mqueue1 = *(mqd_t *)mq;
80 CHAR msgrcd[3][MQUEUE_STANDARD_NAME_LENGTH] = {{0, 0}};
81
82 for (i = 0; i < 3; i++) { // 3, the loop frequency.
83 ret = mq_receive(mqueue1, msgrcd[i], MQUEUE_STANDARD_NAME_LENGTH, NULL);
84 ICUNIT_GOTO_EQUAL(ret, MQUEUE_STANDARD_NAME_LENGTH, ret, EXIT);
85 }
86 LOS_AtomicInc(&g_testCount);
87
88 pthread_exit(nullptr);
89
90 return NULL;
91 EXIT:
92 pthread_exit(nullptr);
93 g_testCount = 0;
94 return NULL;
95 }
96
PthreadF04(VOID * mq)97 static VOID *PthreadF04(VOID *mq)
98 {
99 INT32 i, ret;
100 mqd_t mqueue2 = *(mqd_t *)mq;
101 CHAR msgrcd[3][MQUEUE_STANDARD_NAME_LENGTH];
102
103 for (i = 0; i < 3; i++) { // 3, the loop frequency.
104 ret = mq_receive(mqueue2, msgrcd[i], MQUEUE_STANDARD_NAME_LENGTH, NULL);
105 ICUNIT_GOTO_EQUAL(ret, MQUEUE_STANDARD_NAME_LENGTH, ret, EXIT);
106 }
107
108 LOS_AtomicInc(&g_testCount);
109
110 pthread_exit(nullptr);
111
112 return NULL;
113 EXIT:
114 pthread_exit(nullptr);
115 g_testCount = 0;
116 return NULL;
117 }
118
Testcase(VOID)119 static UINT32 Testcase(VOID)
120 {
121 INT32 ret, oflag;
122 CHAR mqname1[MQUEUE_STANDARD_NAME_LENGTH] = "";
123 CHAR mqname2[MQUEUE_STANDARD_NAME_LENGTH] = "";
124 mqd_t mqueue1 = 0, mqueue2 = 0;
125 struct mq_attr mqstat;
126 pthread_t pthreadSend1, pthreadSend2, pthreadRecev1, pthreadRecev2;
127
128 (void)snprintf_s(mqname1, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
129 "/mqueue145_1_%d", LosCurTaskIDGet());
130 (void)snprintf_s(mqname2, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
131 "/mqueue145_2_%d", LosCurTaskIDGet());
132
133 g_testCount = 0;
134
135 (void)memset_s(&mqstat, sizeof(mqstat), 0, sizeof(mqstat));
136 mqstat.mq_maxmsg = MQUEUE_SHORT_ARRAY_LENGTH;
137 mqstat.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
138 mqstat.mq_flags = 0;
139
140 oflag = O_CREAT | O_NONBLOCK | O_RDWR;
141
142 mqueue1 = mq_open(mqname1, oflag, S_IRWXU | S_IRWXG | S_IRWXO, &mqstat);
143 ICUNIT_GOTO_NOT_EQUAL(mqueue1, (mqd_t)-1, mqueue1, EXIT);
144
145 mqueue2 = mq_open(mqname2, oflag, S_IRWXU | S_IRWXG | S_IRWXO, &mqstat);
146 ICUNIT_GOTO_NOT_EQUAL(mqueue2, (mqd_t)-1, mqueue2, EXIT1);
147
148 ret = pthread_create(&pthreadSend1, NULL, PthreadF01, (void *)&mqueue1);
149 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
150
151 ret = pthread_create(&pthreadSend2, NULL, PthreadF02, (void *)&mqueue2);
152 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT3);
153
154 ret = pthread_create(&pthreadRecev1, NULL, PthreadF03, (void *)&mqueue1);
155 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT4);
156
157 ret = pthread_create(&pthreadRecev2, NULL, PthreadF04, (void *)&mqueue2);
158 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT5);
159
160 ret = pthread_join(pthreadSend1, NULL);
161 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT5);
162
163 ret = pthread_join(pthreadSend2, NULL);
164 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT5);
165
166 ret = pthread_join(pthreadRecev1, NULL);
167 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT5);
168
169 ret = pthread_join(pthreadRecev2, NULL);
170 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT5);
171
172 ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT1); // 4, Here, assert the g_testCount.
173
174 ret = mq_close(mqueue2);
175 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
176
177 ret = mq_unlink(mqname2);
178 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
179
180 ret = mq_close(mqueue1);
181 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
182
183 ret = mq_unlink(mqname1);
184 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
185
186 return MQUEUE_NO_ERROR;
187 EXIT5:
188 pthread_join(pthreadRecev2, NULL);
189 EXIT4:
190 pthread_join(pthreadRecev1, NULL);
191 EXIT3:
192 pthread_join(pthreadSend2, NULL);
193 EXIT2:
194 pthread_join(pthreadSend1, NULL);
195 EXIT1:
196 mq_close(mqueue2);
197 mq_unlink(mqname2);
198 EXIT:
199 mq_close(mqueue1);
200 mq_unlink(mqname1);
201 return MQUEUE_NO_ERROR;
202 }
203
ItPosixQueue145(VOID)204 VOID ItPosixQueue145(VOID) // IT_Layer_ModuleORFeature_No
205 {
206 TEST_ADD_CASE("IT_POSIX_QUEUE_145", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
207 }
208