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 * arg)33 static VOID *PthreadF01(VOID *arg)
34 {
35 INT32 i, ret;
36 INT32 count = MQUEUE_MAX_NUM_TEST;
37 struct mq_attr attr = { 0 };
38
39 g_testCount = 0;
40
41 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
42 attr.mq_maxmsg = 5; // 5, queue max message size.
43
44 for (i = 0; i < count; i++, g_testCount++) {
45 (void)snprintf_s(g_mqueueName[i], MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
46 "/mq160_%d_%d", LosCurTaskIDGet(), i);
47 g_mqueueId[i] = mq_open(g_mqueueName[i], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
48 ICUNIT_GOTO_NOT_EQUAL(g_mqueueId[i], (mqd_t)-1, g_mqueueId[i], EXIT);
49 }
50
51 #ifndef LOSCFG_DEBUG_QUEUE
52 (void)snprintf_s(g_mqueueName[i], MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
53 "/mq160_%d_%d", LosCurTaskIDGet(), i);
54 g_mqueueId[i] = mq_open(g_mqueueName[i], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
55 ICUNIT_GOTO_EQUAL(g_mqueueId[i], (mqd_t)-1, g_mqueueId[i], EXIT);
56 #endif
57
58 return NULL;
59 EXIT:
60 for (i = 0; i < g_testCount; i++) {
61 mq_close(g_mqueueId[i]);
62 mq_unlink(g_mqueueName[i]);
63 }
64 return NULL;
65 }
66
PthreadF02(VOID * arg)67 static VOID *PthreadF02(VOID *arg)
68 {
69 INT32 ret;
70 UINT32 i;
71 const CHAR *msgptr = MQUEUE_SEND_STRING_TEST;
72 CHAR msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
73 struct timespec ts = { 0 };
74
75 ts.tv_sec = 0xffff;
76
77 for (i = 0; i < g_testCount; i++) {
78 ret = mq_timedsend(g_mqueueId[i % g_testCount], msgptr, strlen(msgptr), 0, &ts);
79 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
80
81 ret = mq_timedreceive(g_mqueueId[i % g_testCount], msgrcd, MQUEUE_STANDARD_NAME_LENGTH, 0, &ts);
82 ICUNIT_GOTO_EQUAL(ret, MQUEUE_SHORT_ARRAY_LENGTH, ret, EXIT);
83 ICUNIT_GOTO_STRING_EQUAL(msgrcd, MQUEUE_SEND_STRING_TEST, msgrcd, EXIT);
84 }
85 return NULL;
86 EXIT:
87 for (i = 0; i < g_testCount; i++) {
88 mq_close(g_mqueueId[i]);
89 mq_unlink(g_mqueueName[i]);
90 }
91 return NULL;
92 }
93
PthreadF03(VOID * arg)94 static VOID *PthreadF03(VOID *arg)
95 {
96 INT32 ret, i;
97
98 for (i = 0; i < g_testCount; i++) {
99 ret = mq_close(g_mqueueId[i]);
100 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
101
102 ret = mq_unlink(g_mqueueName[i]);
103 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
104 }
105
106 return NULL;
107 EXIT:
108 for (i = 0; i < g_testCount; i++) {
109 mq_close(g_mqueueId[i]);
110 mq_unlink(g_mqueueName[i]);
111 }
112 return NULL;
113 }
114
115
Testcase(VOID)116 static UINT32 Testcase(VOID)
117 {
118 INT32 ret, i;
119 pthread_t pthread1, pthread2, pthread3;
120 pthread_attr_t attr1;
121 struct sched_param schedparam;
122
123 g_testCount = 0;
124
125 ret = pthread_attr_init(&attr1);
126 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
127
128 schedparam.sched_priority = MQUEUE_PTHREAD_PRIORITY_TEST2;
129 ret = pthread_attr_setschedparam(&attr1, &schedparam);
130 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
131
132 ret = pthread_attr_init(&attr1);
133 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
134
135 for (i = 0; i < MQUEUE_STANDARD_NAME_LENGTH; i++) {
136 ret = pthread_create(&pthread1, &attr1, PthreadF01, NULL);
137 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
138
139 ret = pthread_join(pthread1, NULL);
140 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
141
142 ret = pthread_create(&pthread2, &attr1, PthreadF02, NULL);
143 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
144
145 ret = pthread_join(pthread2, NULL);
146 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT2);
147
148 ret = pthread_create(&pthread3, &attr1, PthreadF03, NULL);
149 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT3);
150
151 ret = pthread_join(pthread3, NULL);
152 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT3);
153 }
154
155 ret = pthread_attr_destroy(&attr1);
156 ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
157
158 return MQUEUE_NO_ERROR;
159 EXIT3:
160 pthread_join(pthread3, NULL);
161 EXIT2:
162 pthread_join(pthread2, NULL);
163 EXIT1:
164 pthread_join(pthread1, NULL);
165 EXIT:
166 pthread_attr_destroy(&attr1);
167 return MQUEUE_NO_ERROR;
168 }
169
ItPosixQueue160(VOID)170 VOID ItPosixQueue160(VOID) // IT_Layer_ModuleORFeature_No
171 {
172 TEST_ADD_CASE("IT_POSIX_QUEUE_160", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
173 }
174