1 /*
2 * Copyright (c) 2023-2023 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
31 #include "xts_ipc.h"
32
33 LITE_TEST_SUIT(IPC, IpcMqExceptionApi, IpcMqExceptionApiTestSuite);
34
IpcMqExceptionApiTestSuiteSetUp(void)35 static BOOL IpcMqExceptionApiTestSuiteSetUp(void)
36 {
37 return TRUE;
38 }
39
IpcMqExceptionApiTestSuiteTearDown(void)40 static BOOL IpcMqExceptionApiTestSuiteTearDown(void)
41 {
42 return TRUE;
43 }
44
45 /* *
46 * @tc.number SUB_KERNEL_IPC_MQ_RECEIVE_0200
47 * @tc.name mq_receive function errno for EBADF and EMSGSIZE test
48 * @tc.desc [C- SOFTWARE -0200]
49 */
50 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqReceiveEBADFEMSGSIZE001, Function | MediumTest | Level2)
51 {
52 int ret;
53 mqd_t queue;
54 unsigned int prio;
55 struct mq_attr attr = { 0 };
56 struct mq_attr getAttr = { 0 };
57 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN], rMsgErr[1];
58
59 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqReceiveEAGAIN_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
60 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
61 attr.mq_msgsize = MQ_MSG_SIZE;
62 attr.mq_maxmsg = MQ_MAX_MSG;
63 queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
64 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
65 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
66 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
67 ret = mq_getattr(queue, &getAttr);
68 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
69 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
70 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
71 ret = strncmp(MQ_MSG, rMsg, MQ_MSG_LEN);
72 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
73 ret = mq_receive(NULL, rMsg, getAttr.mq_msgsize, &prio);
74 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
75 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
76 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
77 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78 ret = mq_receive(queue, rMsgErr, sizeof(rMsgErr), &prio);
79 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
80 ICUNIT_ASSERT_EQUAL(errno, EMSGSIZE, errno);
81 ret = mq_close(queue);
82 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
83 ret = mq_unlink(qName);
84 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
85 return 0;
86 }
87
88 /* *
89 * @tc.number SUB_KERNEL_IPC_MQ_RECEIVE_0200
90 * @tc.name mq_receive function errno for EBADF and EMSGSIZE test
91 * @tc.desc [C- SOFTWARE -0200]
92 */
93 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqReceiveEBADFEMSGSIZE002, Function | MediumTest | Level2)
94 {
95 int ret;
96 mqd_t queue;
97 unsigned int prio;
98 struct mq_attr attr = { 0 };
99 struct mq_attr getAttr = { 0 };
100 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
101
102 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqReceiveEAGAIN_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
103 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
104 attr.mq_msgsize = MQ_MSG_SIZE;
105 attr.mq_maxmsg = MQ_MAX_MSG;
106 queue = mq_open(qName, O_CREAT | O_WRONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
107 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
108 ret = mq_getattr(queue, &getAttr);
109 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
110 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
111 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
112 attr.mq_flags |= O_NONBLOCK;
113 ret = mq_setattr(queue, &attr, NULL);
114 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
115 ret = mq_receive(queue, rMsg, getAttr.mq_msgsize, &prio);
116 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
117 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
118 ret = mq_close(queue);
119 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
120 ret = mq_unlink(qName);
121 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122 return 0;
123 }
124
125 /* *
126 * @tc.number SUB_KERNEL_IPC_MQ_RECEIVE_0300
127 * @tc.name mq_receive function errno for EINVAL test
128 * @tc.desc [C- SOFTWARE -0200]
129 */
130 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqReceiveEINVAL, Function | MediumTest | Level3)
131 {
132 int ret;
133 mqd_t queue;
134 unsigned int prio;
135 struct mq_attr attr = { 0 };
136 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
137
138 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqReceiveEINVAL_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
139 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
140 attr.mq_msgsize = MQ_MSG_SIZE;
141 attr.mq_maxmsg = MQ_MAX_MSG;
142 queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
143 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
144
145 ret = mq_receive(queue, rMsg, 0, &prio);
146 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
147 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
148
149 ret = mq_close(queue);
150 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
151 ret = mq_unlink(qName);
152 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
153 return 0;
154 }
155
156 /* *
157 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0100
158 * @tc.name mq_timedsend function errno for EAGAIN and EBADF test
159 * @tc.desc [C- SOFTWARE -0200]
160 */
161 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedSendEAGAINEBADF, Function | MediumTest | Level2)
162 {
163 int ret;
164 mqd_t queue;
165 struct timespec ts = { 0, 0 };
166 struct mq_attr attr = { 0 };
167 char qName[MQ_NAME_LEN];
168
169 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedSendEAGAIN_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
170 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
171 attr.mq_msgsize = MQ_MSG_SIZE;
172 attr.mq_maxmsg = 1; /* 1, common data for test, no special meaning */
173 queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
174 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
175
176 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
177 ts.tv_nsec = 0;
178 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
179 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
180
181 ts.tv_sec = 0;
182 ts.tv_nsec = 0;
183 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
184 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
185 ICUNIT_ASSERT_EQUAL(errno, EAGAIN, errno);
186 ret = mq_timedsend(NULL, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
187 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
188 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
189
190 ret = mq_close(queue);
191 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
192 ret = mq_unlink(qName);
193 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
194
195 attr.mq_msgsize = MQ_MSG_SIZE;
196 attr.mq_maxmsg = MQ_MAX_MSG;
197 queue = mq_open(qName, O_CREAT | O_RDONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
198 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
199
200 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
201 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
202 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
203
204 attr.mq_flags |= O_NONBLOCK;
205 ret = mq_setattr(queue, &attr, NULL);
206 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
207 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
208 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
209 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
210
211 ret = mq_close(queue);
212 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
213 ret = mq_unlink(qName);
214 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
215 return 0;
216 }
217
218 /* *
219 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0200
220 * @tc.name mq_timedsend function errno for EINVAL test
221 * @tc.desc [C- SOFTWARE -0200]
222 */
223 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedSendEINVAL, Function | MediumTest | Level2)
224 {
225 int ret;
226 mqd_t queue;
227 struct timespec ts = { 0, 0 };
228 struct mq_attr attr = { 0 };
229 char qName[MQ_NAME_LEN];
230
231 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedSendEINVAL_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
232 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
233 attr.mq_msgsize = MQ_MSG_SIZE;
234 attr.mq_maxmsg = MQ_MAX_MSG;
235 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
236 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
237
238 for (int i = 0; i < 3; i++) { /* 3, common data for test, no special meaning */
239 switch (i) {
240 case 0:
241 ts.tv_sec = -1; /* -1, common data for test, no special meaning */
242 ts.tv_nsec = 0;
243 break;
244 case 1:
245 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
246 ts.tv_nsec = -1; /* -1, common data for test, no special meaning */
247 break;
248 case 2:
249 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
250 ts.tv_nsec = 1000000000UL + 1UL;
251 break;
252 }
253
254 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
255 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
256 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
257 }
258 ret = mq_timedsend(queue, MQ_MSG, 0, MQ_MSG_PRIO, &ts);
259 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
260 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
261
262 ret = mq_close(queue);
263 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
264 ret = mq_unlink(qName);
265 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
266 return 0;
267 }
268
269 /* *
270 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0300
271 * @tc.name mq_timedsend function errno for EMSGSIZE test
272 * @tc.desc [C- SOFTWARE -0200]
273 */
274 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedSendEMSGSIZE, Function | MediumTest | Level3)
275 {
276 int ret;
277 mqd_t queue;
278 struct timespec ts = { 0, 0 };
279 struct mq_attr attr = { 0 };
280 char qName[MQ_NAME_LEN];
281
282 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedSendEMSGSIZE_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
283 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
284 attr.mq_msgsize = 1; /* 1, common data for test, no special meaning */
285 attr.mq_maxmsg = MQ_MAX_MSG;
286 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
287 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
288
289 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
290 ts.tv_nsec = 0;
291 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
292 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
293 ICUNIT_ASSERT_EQUAL(errno, EMSGSIZE, errno);
294
295 ret = mq_close(queue);
296 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
297 ret = mq_unlink(qName);
298 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
299 return 0;
300 }
301
302 /* *
303 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDSEND_0400
304 * @tc.name mq_timedsend function errno for ETIMEDOUT test
305 * @tc.desc [C- SOFTWARE -0200]
306 */
307 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedSendETIMEDOUT, Function | MediumTest | Level3)
308 {
309 int ret;
310 mqd_t queue;
311 struct timespec ts = { 0, 0 };
312 struct mq_attr attr = { 0 };
313 char qName[MQ_NAME_LEN];
314
315 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedSendETIMEDOUT_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
316 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
317 attr.mq_msgsize = MQ_MSG_SIZE;
318 attr.mq_maxmsg = 1; /* 1, common data for test, no special meaning */
319 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
320 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
321
322 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
323 ts.tv_nsec = 0;
324 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
325 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
326
327 ts.tv_sec = 0;
328 ts.tv_nsec = 100; /* 100, common data for test, no special meaning */
329 ret = mq_timedsend(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO, &ts);
330 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
331 ICUNIT_ASSERT_EQUAL(errno, ETIMEDOUT, errno);
332
333 ret = mq_close(queue);
334 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
335 ret = mq_unlink(qName);
336 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
337 return 0;
338 }
339
340 /* *
341 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0100
342 * @tc.name mq_timedreceive function errno for EAGAIN and EBADF test
343 * @tc.desc [C- SOFTWARE -0200]
344 */
345 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedReceiveEAGAINEBADF, Function | MediumTest | Level2)
346 {
347 int ret;
348 mqd_t queue;
349 unsigned int prio;
350 struct timespec ts = { 0, 0 };
351 struct mq_attr attr = { 0 };
352 struct mq_attr getAttr = { 0 };
353 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
354
355 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedReceiveEAGAIN_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
356 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
357 attr.mq_msgsize = MQ_MSG_SIZE;
358 attr.mq_maxmsg = MQ_MAX_MSG;
359 queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
360 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
361
362 ret = mq_getattr(queue, &getAttr);
363 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
364 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
365 ts.tv_nsec = 0;
366 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &ts);
367 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
368 ICUNIT_ASSERT_EQUAL(errno, EAGAIN, errno);
369 ret = mq_timedreceive(NULL, rMsg, getAttr.mq_msgsize, &prio, &ts);
370 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
371 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
372
373 ret = mq_close(queue);
374 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
375 ret = mq_unlink(qName);
376 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
377
378 attr.mq_msgsize = MQ_MSG_SIZE;
379 attr.mq_maxmsg = MQ_MAX_MSG;
380 queue = mq_open(qName, O_CREAT | O_WRONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr);
381 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
382
383 ret = mq_timedreceive(NULL, rMsg, getAttr.mq_msgsize, &prio, &ts);
384 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
385 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
386
387 attr.mq_flags |= O_NONBLOCK;
388 ret = mq_setattr(queue, &attr, NULL);
389 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
390 ret = mq_timedreceive(NULL, rMsg, getAttr.mq_msgsize, &prio, &ts);
391 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
392 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
393
394 ret = mq_close(queue);
395 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
396 ret = mq_unlink(qName);
397 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
398 return 0;
399 }
400
401 /* *
402 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0200
403 * @tc.name mq_timedreceive function errno for EINVAL test
404 * @tc.desc [C- SOFTWARE -0200]
405 */
406 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedReceiveEINVAL, Function | MediumTest | Level2)
407 {
408 int ret;
409 mqd_t queue;
410 unsigned int prio;
411 struct timespec ts = { 0, 0 };
412 struct mq_attr attr = { 0 };
413 struct mq_attr getAttr = { 0 };
414 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
415
416 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedReceiveEINVAL_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
417 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
418 attr.mq_msgsize = MQ_MSG_SIZE;
419 attr.mq_maxmsg = MQ_MAX_MSG;
420 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
421 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
422
423 for (int i = 0; i < 3; i++) { /* 3, common data for test, no special meaning */
424 switch (i) {
425 case 0:
426 ts.tv_sec = -1; /* -1, common data for test, no special meaning */
427 ts.tv_nsec = 0;
428 break;
429 case 1:
430 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
431 ts.tv_nsec = -1; /* -1, common data for test, no special meaning */
432 break;
433 case 2:
434 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
435 ts.tv_nsec = 1000000000UL + 1UL;
436 break;
437 }
438
439 ret = mq_getattr(queue, &getAttr);
440 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
441 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &ts);
442 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
443 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
444 }
445 ret = mq_timedreceive(queue, rMsg, 0, &prio, &ts);
446 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
447 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
448
449 ret = mq_close(queue);
450 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
451 ret = mq_unlink(qName);
452 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
453 return 0;
454 }
455
456 /* *
457 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0300
458 * @tc.name mq_timedreceive function errno for ETIMEDOUT test
459 * @tc.desc [C- SOFTWARE -0200]
460 */
461 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedReceiveETIMEDOUT, Function | MediumTest | Level3)
462 {
463 int ret;
464 mqd_t queue;
465 unsigned int prio;
466 struct timespec ts = { 0, 0 };
467 struct mq_attr attr = { 0 };
468 struct mq_attr getAttr = { 0 };
469 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
470
471 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedReceiveETIMEDOUT_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
472 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
473 attr.mq_msgsize = MQ_MSG_SIZE;
474 attr.mq_maxmsg = MQ_MAX_MSG;
475 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
476 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
477
478 ret = mq_getattr(queue, &getAttr);
479 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
480 ts.tv_sec = 0;
481 ts.tv_nsec = 100; /* 100, common data for test, no special meaning */
482 ret = mq_timedreceive(queue, rMsg, getAttr.mq_msgsize, &prio, &ts);
483 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
484 ICUNIT_ASSERT_EQUAL(errno, ETIMEDOUT, errno);
485
486 ret = mq_close(queue);
487 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
488 ret = mq_unlink(qName);
489 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
490 return 0;
491 }
492
493 /* *
494 * @tc.number SUB_KERNEL_IPC_MQ_TIMEDRECEIVE_0400
495 * @tc.name mq_timedreceive function errno for EMSGSIZE test
496 * @tc.desc [C- SOFTWARE -0200]
497 */
498 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqTimedReceiveEMSGSIZE, Function | MediumTest | Level3)
499 {
500 int ret;
501 mqd_t queue;
502 unsigned int prio;
503 struct timespec ts = { 0, 0 };
504 struct mq_attr attr = { 0 };
505 char qName[MQ_NAME_LEN], rMsg[MQ_RX_LEN];
506
507 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqTimedReceiveEMSGSIZE_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
508 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
509 attr.mq_msgsize = MQ_MSG_SIZE;
510 attr.mq_maxmsg = MQ_MAX_MSG;
511 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
512 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
513
514 ret = mq_send(queue, MQ_MSG, MQ_MSG_LEN, MQ_MSG_PRIO);
515 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
516 ts.tv_sec = time(NULL) + 1; /* 1, common data for test, no special meaning */
517 ts.tv_nsec = 0;
518 ret = mq_timedreceive(queue, rMsg, 1, &prio, &ts); /* 1, common data for test, no special meaning */
519 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
520 ICUNIT_ASSERT_EQUAL(errno, EMSGSIZE, errno);
521
522 ret = mq_close(queue);
523 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
524 ret = mq_unlink(qName);
525 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
526 return 0;
527 }
528
529 /* *
530 * @tc.number SUB_KERNEL_IPC_MQ_UNLINK_0100
531 * @tc.name mq_unlink function errno for ENAMETOOLONG test
532 * @tc.desc [C- SOFTWARE -0200]
533 */
534 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqUnlinkENAMETOOLONG, Function | MediumTest | Level2)
535 {
536 char qName[MAX_MQ_NAME_LEN + 10]; /* 10, common data for test, no special meaning */
537 int i;
538 for (i = 0; i < MAX_MQ_NAME_LEN + 5; i++) { /* 5, common data for test, no special meaning */
539 qName[i] = '8';
540 }
541 qName[i] = '\0';
542 int ret = mq_unlink(qName);
543 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
544 ICUNIT_ASSERT_EQUAL(errno, ENAMETOOLONG, errno);
545 return 0;
546 }
547
548 /* *
549 * @tc.number SUB_KERNEL_IPC_MQ_UNLINK_0200
550 * @tc.name mq_unlink function errno for ENOENT test
551 * @tc.desc [C- SOFTWARE -0200]
552 */
553 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqUnlinkENOENT, Function | MediumTest | Level2)
554 {
555 char qName[64] = "/mq_file-does-not-exit"; /* 64, common data for test, no special meaning */
556 int ret = mq_unlink(qName);
557 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
558 ICUNIT_ASSERT_EQUAL(errno, ENOENT, errno);
559 return 0;
560 }
561
562 /* *
563 * @tc.number SUB_KERNEL_IPC_MQ_UNLINK_0300
564 * @tc.name mq_unlink function errno for EINVAL test
565 * @tc.desc [C- SOFTWARE -0200]
566 */
567 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqUnlinkEINVAL, Function | MediumTest | Level3)
568 {
569 int ret = mq_unlink("");
570 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
571 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
572 return 0;
573 }
574
575 /* *
576 * @tc.number SUB_KERNEL_IPC_MQ_GETATTR_0100
577 * @tc.name mq_getattr function errno for EBAD and EINVALF test
578 * @tc.desc [C- SOFTWARE -0200]
579 */
580 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqGetAttrEBADFEINVAL, Function | MediumTest | Level2)
581 {
582 int ret;
583 mqd_t queue;
584 struct mq_attr mqstat = { 0 };
585 char qName[MQ_NAME_LEN];
586
587 memset_s(&mqstat, sizeof(mqstat), 0, sizeof(mqstat));
588 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqSendEINVAL_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
589 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
590 queue = mq_open(qName, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, NULL);
591
592 ret = mq_getattr(NULL, &mqstat);
593 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
594 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
595
596 ret = mq_getattr(queue, NULL);
597 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
598 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
599
600 ret = mq_close(queue);
601 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
602 ret = mq_unlink(qName);
603 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
604 return 0;
605 }
606
607 /* *
608 * @tc.number SUB_KERNEL_IPC_MQ_SETATTR_0100
609 * @tc.name mq_receive function errno for EBADF and EINVAL test
610 * @tc.desc [C- SOFTWARE -0200]
611 */
612 LITE_TEST_CASE(IpcMqExceptionApiTestSuite, testMqSetAttrEBADFEINVAL, Function | MediumTest | Level2)
613 {
614 int ret;
615 char qName[MQ_NAME_LEN];
616 mqd_t queue;
617 struct mq_attr gMqstat = { 0 }, sMqstat = { 0 };
618
619 ret = sprintf_s(qName, MQ_NAME_LEN, "testMqSetAttrEBADFEINVAL_%d", GetRandom(10000)); /* 10000, common data for test, no special meaning */
620 ICUNIT_ASSERT_EQUAL(ret, strlen(qName), ret);
621 queue = mq_open(qName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
622 ICUNIT_ASSERT_NOT_EQUAL(queue, (mqd_t)-1, queue); /* -1, common data for test, no special meaning */
623
624 memset_s(&gMqstat, sizeof(gMqstat), 0, sizeof(gMqstat));
625 memset_s(&sMqstat, sizeof(sMqstat), 0, sizeof(sMqstat));
626 ret = mq_getattr(queue, &gMqstat);
627 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
628
629 sMqstat.mq_flags |= O_NONBLOCK;
630 ret = mq_setattr(queue, &sMqstat, NULL);
631 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
632 ret = mq_getattr(queue, &gMqstat);
633 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
634 ICUNIT_ASSERT_NOT_EQUAL(gMqstat.mq_flags, sMqstat.mq_flags, gMqstat.mq_flags);
635
636 ret = mq_setattr(NULL, &sMqstat, NULL);
637 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
638 ICUNIT_ASSERT_EQUAL(errno, EBADF, errno);
639 ret = mq_setattr(queue, NULL, NULL);
640 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
641 ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
642
643 ret = mq_close(queue);
644 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
645 ret = mq_unlink(qName);
646 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
647 return 0;
648 }
649
650 RUN_TEST_SUITE(IpcMqExceptionApiTestSuite);
651
IpcMqExceptionFuncTest(void)652 void IpcMqExceptionFuncTest(void)
653 {
654 RUN_ONE_TESTCASE(testMqReceiveEBADFEMSGSIZE001);
655 RUN_ONE_TESTCASE(testMqReceiveEBADFEMSGSIZE002);
656 RUN_ONE_TESTCASE(testMqReceiveEINVAL);
657 RUN_ONE_TESTCASE(testMqTimedSendEAGAINEBADF);
658 RUN_ONE_TESTCASE(testMqTimedSendEINVAL);
659 RUN_ONE_TESTCASE(testMqTimedSendEMSGSIZE);
660 RUN_ONE_TESTCASE(testMqTimedSendETIMEDOUT);
661 RUN_ONE_TESTCASE(testMqTimedReceiveEAGAINEBADF);
662 RUN_ONE_TESTCASE(testMqTimedReceiveEINVAL);
663 RUN_ONE_TESTCASE(testMqTimedReceiveETIMEDOUT);
664 RUN_ONE_TESTCASE(testMqTimedReceiveEMSGSIZE);
665 RUN_ONE_TESTCASE(testMqUnlinkENAMETOOLONG);
666 RUN_ONE_TESTCASE(testMqUnlinkENOENT);
667 RUN_ONE_TESTCASE(testMqUnlinkEINVAL);
668 RUN_ONE_TESTCASE(testMqGetAttrEBADFEINVAL);
669 RUN_ONE_TESTCASE(testMqSetAttrEBADFEINVAL);
670 }
671