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 struct timespec ts;
36 const CHAR *msgptr = MQUEUE_SEND_STRING_TEST;
37 UINT32 ret;
38
39 ts.tv_sec = 0xffff;
40 ts.tv_nsec = 0;
41
42 g_testCount = 1;
43
44 for (i = 0; i < 5 + 1; i++) { // 5, The loop frequency.
45 ret = mq_timedsend(g_gqueue, msgptr, strlen(msgptr), 0, &ts);
46 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
47 }
48 ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT1); // 3, Here, assert the g_testCount.
49
50 g_testCount = 2; // 2, Init test count value.
51
52 EXIT1:
53 return NULL;
54 EXIT2:
55 g_testCount = 0;
56 return NULL;
57 }
58
PthreadF02(VOID * argument)59 static VOID *PthreadF02(VOID *argument)
60 {
61 INT32 ret;
62 CHAR msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = "";
63
64 g_testCount = 3; // 3, Init test count value.
65
66 ret = mq_receive(g_gqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
67 ICUNIT_GOTO_EQUAL(ret, strlen(MQUEUE_SEND_STRING_TEST), ret, EXIT1);
68
69 TestExtraTaskDelay(2); // 2, Set delay time.
70 ret = mq_close(g_gqueue);
71 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
72
73 ret = mq_unlink(g_gqname);
74 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
75
76 g_testCount = 4; // 4, Init test count value.
77
78 return NULL;
79 EXIT1:
80 mq_close(g_gqueue);
81 EXIT:
82 mq_unlink(g_gqname);
83 EXIT2:
84 g_testCount = 0;
85 return NULL;
86 }
87
Testcase(VOID)88 static UINT32 Testcase(VOID)
89 {
90 UINT32 ret;
91 struct mq_attr attr = {0};
92 pthread_attr_t attr1;
93 pthread_t newTh1, newTh2;
94
95 ret = snprintf_s(g_gqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1, \
96 "/mq026_%d", LOS_CurTaskIDGet());
97 ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT3);
98
99 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
100 attr.mq_maxmsg = 5; // 5, queue max message size.
101 g_gqueue = mq_open(g_gqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
102 ICUNIT_GOTO_NOT_EQUAL(g_gqueue, (mqd_t)-1, g_gqueue, EXIT);
103
104 g_testCount = 0;
105
106 ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST1);
107 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
108
109 ret = pthread_create(&newTh1, &attr1, PthreadF01, NULL);
110 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
111
112 MqueueTaskDelay(5); // 5, Set delay time.
113 ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT1);
114
115 ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST2);
116 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
117
118 ret = pthread_create(&newTh2, &attr1, PthreadF02, NULL);
119 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
120
121 ret = MqueueTaskDelay(5); // 5, Set delay time.
122 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
123
124 ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT2); // 4, Here, assert the g_testCount.
125
126 ret = PosixPthreadDestroy(&attr1, newTh2);
127 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
128
129 ret = PosixPthreadDestroy(&attr1, newTh1);
130 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
131
132 return LOS_OK;
133
134 EXIT2:
135 PosixPthreadDestroy(&attr1, newTh2);
136 EXIT1:
137 PosixPthreadDestroy(&attr1, newTh1);
138 EXIT:
139 mq_close(g_gqueue);
140 mq_unlink(g_gqname);
141 EXIT3:
142 return LOS_NOK;
143 }
144
145 /**
146 * @tc.name: ItPosixQueue026
147 * @tc.desc: Test interface mq_timedsend
148 * @tc.type: FUNC
149 * @tc.require: issueI6148G
150 */
151
ItPosixQueue026(VOID)152 VOID ItPosixQueue026(VOID)
153 {
154 TEST_ADD_CASE("ItPosixQueue026", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
155 }
156