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 ret;
35 const CHAR *msgptr = MQUEUE_SEND_STRING_TEST;
36
37 g_testCount = 1;
38
39 TestAssertWaitDelay((UINT32 *)&g_testCount, 3); // 3, Here, assert the g_testCount.
40
41 ret = mq_send(g_gqueue, msgptr, strlen(msgptr), 0);
42 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
43
44 g_testCount = 2; // 2, Init test count value.
45
46 return NULL;
47 EXIT:
48 mq_close(g_gqueue);
49 mq_unlink(g_gqname);
50 g_testCount = 0;
51 return NULL;
52 }
53
PthreadF02(VOID * argument)54 static VOID *PthreadF02(VOID *argument)
55 {
56 INT32 ret;
57 CHAR msgrv[MQUEUE_STANDARD_NAME_LENGTH] = {0};
58
59 g_testCount = 3; // 3, Init test count value.
60
61 ret = mq_receive(g_gqueue, msgrv, MQUEUE_STANDARD_NAME_LENGTH, NULL);
62 ICUNIT_GOTO_EQUAL(ret, MQUEUE_SHORT_ARRAY_LENGTH, ret, EXIT);
63
64 ret = MqueueTaskDelay(1);
65 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
66
67 ret = mq_close(g_gqueue);
68 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
69
70 ret = mq_unlink(g_gqname);
71 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
72
73 g_testCount = 4; // 4, Init test count value.
74 return NULL;
75 EXIT:
76 mq_close(g_gqueue);
77 EXIT1:
78 mq_unlink(g_gqname);
79 EXIT2:
80 g_testCount = 0;
81 return NULL;
82 }
83
Testcase(VOID)84 static UINT32 Testcase(VOID)
85 {
86 INT32 ret;
87 struct mq_attr attr = {0};
88 pthread_attr_t attr1;
89 pthread_t newTh1, newTh2;
90
91 (void)snprintf_s(g_gqname, MQUEUE_STANDARD_NAME_LENGTH, MQUEUE_STANDARD_NAME_LENGTH - 1,
92 "/mq036_%d", LOS_CurTaskIDGet());
93
94 attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
95 attr.mq_maxmsg = MQUEUE_STANDARD_NAME_LENGTH;
96 g_gqueue = mq_open(g_gqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
97 ICUNIT_GOTO_NOT_EQUAL(g_gqueue, (mqd_t)-1, g_gqueue, EXIT3);
98
99 g_testCount = 0;
100
101 ret = PosixPthreadInit(&attr1, MQUEUE_PTHREAD_PRIORITY_TEST1);
102 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
103
104 ret = pthread_create(&newTh1, &attr1, PthreadF01, NULL);
105 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
106
107 TestAssertWaitDelay((UINT32 *)&g_testCount, 1);
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(10); // 10, Set delay time.
116 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT2);
117
118 TestAssertWaitDelay((UINT32 *)&g_testCount, 4); // 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 EXIT2:
128 PosixPthreadDestroy(&attr1, newTh2);
129 EXIT1:
130 PosixPthreadDestroy(&attr1, newTh1);
131 EXIT:
132 mq_close(g_gqueue);
133 mq_unlink(g_gqname);
134 EXIT3:
135 return LOS_NOK;
136 }
137
138 /**
139 * @tc.name: ItPosixQueue036
140 * @tc.desc: Test interface mq_receive
141 * @tc.type: FUNC
142 * @tc.require: issueI61YO0
143 */
144
ItPosixQueue036(VOID)145 VOID ItPosixQueue036(VOID)
146 {
147 TEST_ADD_CASE("ItPosixQueue036", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
148 }
149