• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 typedef struct {
34     INT32 pthread_ID;
35     mqd_t mqueue_ID;
36 } mq_info;
37 
PthreadF01(VOID * info)38 static VOID *PthreadF01(VOID *info)
39 {
40     INT32 i, ret;
41     const CHAR *msgptr[] = { "msg test 1", "msg test 2", "msg test 3" };
42     mq_info sendInfo;
43 
44     sendInfo.pthread_ID = ((mq_info *)info)->pthread_ID;
45     sendInfo.mqueue_ID = ((mq_info *)info)->mqueue_ID;
46     for (i = 0; i < 3; i++) { // 3, the loop frequency.
47         ret = mq_send(sendInfo.mqueue_ID, msgptr[i], MQUEUE_STANDARD_NAME_LENGTH, 0);
48         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
49     }
50 
51     LOS_AtomicInc(&g_testCount);
52 
53     pthread_exit(nullptr);
54 
55     return NULL;
56 EXIT:
57     pthread_exit(nullptr);
58     g_testCount = 0;
59     return NULL;
60 }
61 
PthreadF02(VOID * info)62 static VOID *PthreadF02(VOID *info)
63 {
64     INT32 i, ret;
65     CHAR msgrcd[3][MQUEUE_STANDARD_NAME_LENGTH * 2];
66     mq_info recvInfo;
67 
68     recvInfo.pthread_ID = ((mq_info *)info)->pthread_ID;
69     recvInfo.mqueue_ID = ((mq_info *)info)->mqueue_ID;
70     for (i = 0; i < 3; i++) { // 3, the loop frequency.
71         ret = mq_receive(recvInfo.mqueue_ID, msgrcd[i], MQUEUE_STANDARD_NAME_LENGTH, NULL);
72         ICUNIT_GOTO_EQUAL(ret, MQUEUE_STANDARD_NAME_LENGTH, ret, EXIT);
73     }
74 
75     LOS_AtomicInc(&g_testCount);
76 
77     pthread_exit(nullptr);
78 
79     return NULL;
80 EXIT:
81     pthread_exit(nullptr);
82     g_testCount = 0;
83     return NULL;
84 }
85 
Testcase(VOID)86 static UINT32 Testcase(VOID)
87 {
88     INT32 i, ret, oflag;
89     UINT32 uret;
90     const CHAR *mqname[MQUEUE_PTHREAD_NUM_TEST] = {"/msg1", "/msg2", "/msg3", "/msg4", "/msg5"};
91     mqd_t mqueue[MQUEUE_PTHREAD_NUM_TEST];
92     struct mq_attr mqstat;
93     pthread_t pthreadSend[MQUEUE_PTHREAD_NUM_TEST];
94     pthread_t pthreadRecev[MQUEUE_PTHREAD_NUM_TEST];
95     mq_info info[MQUEUE_PTHREAD_NUM_TEST];
96 
97     g_testCount = 0;
98 
99     oflag = O_CREAT | O_NONBLOCK | O_RDWR;
100 
101     ret = memset_s(&mqstat, sizeof(mqstat), 0, sizeof(mqstat));
102     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
103     mqstat.mq_maxmsg = MQUEUE_SHORT_ARRAY_LENGTH;
104     mqstat.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
105     mqstat.mq_flags = 0;
106 
107     for (i = 0; i < MQUEUE_PTHREAD_NUM_TEST; i++) {
108         mqueue[i] = mq_open(mqname[i], oflag,  S_IRWXU | S_IRWXG | S_IRWXO, &mqstat);
109         ICUNIT_GOTO_NOT_EQUAL(mqueue[i], (mqd_t)-1, mqueue[i], EXIT);
110     }
111     for (i = 0; i < MQUEUE_PTHREAD_NUM_TEST; i++) {
112         info[i].pthread_ID = i;
113         info[i].mqueue_ID = mqueue[i];
114         ret = pthread_create(&pthreadSend[i], NULL, PthreadF01, (void *)&info[i]);
115         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
116 
117         ret = pthread_create(&pthreadRecev[i], NULL, PthreadF02, (void *)&info[i]);
118         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
119     }
120 
121     uret = sleep(5); // 5, seconds.
122     ICUNIT_GOTO_EQUAL(uret, 0, uret, EXIT1);
123 
124     ICUNIT_GOTO_EQUAL(g_testCount, MQUEUE_PTHREAD_NUM_TEST * 2, g_testCount, EXIT1); // 2, assert the g_testCount.
125 
126     for (i = 0; i < MQUEUE_PTHREAD_NUM_TEST; i++) {
127         ret = pthread_join(pthreadSend[i], NULL);
128         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
129 
130         ret = pthread_join(pthreadRecev[i], NULL);
131         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT1);
132     }
133 
134     for (i = 0; i < MQUEUE_PTHREAD_NUM_TEST; i++) {
135         ret = mq_close(mqueue[i]);
136         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
137 
138         ret = mq_unlink(mqname[i]);
139         ICUNIT_GOTO_EQUAL(ret, MQUEUE_NO_ERROR, ret, EXIT);
140     }
141 
142     return MQUEUE_NO_ERROR;
143 EXIT1:
144     for (i = 0; i < MQUEUE_PTHREAD_NUM_TEST; i++) {
145         pthread_join(pthreadSend[i], NULL);
146         pthread_join(pthreadRecev[i], NULL);
147     }
148 EXIT:
149     for (i = 0; i < MQUEUE_PTHREAD_NUM_TEST; i++) {
150         mq_close(mqueue[i]);
151         mq_unlink(mqname[i]);
152     }
153 
154     return MQUEUE_NO_ERROR;
155 }
156 
ItPosixQueue146(VOID)157 VOID ItPosixQueue146(VOID) // IT_Layer_ModuleORFeature_No
158 {
159     TEST_ADD_CASE("IT_POSIX_QUEUE_146", Testcase, TEST_POSIX, TEST_QUE, TEST_LEVEL2, TEST_FUNCTION);
160 }
161