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 "pthread_test.h"
32
33 LITE_TEST_SUIT(PROCESS, PthreadBasicApi, PthreadBasicApiTestSuite);
34
PthreadBasicApiTestSuiteSetUp(void)35 static BOOL PthreadBasicApiTestSuiteSetUp(void)
36 {
37 return TRUE;
38 }
39
PthreadBasicApiTestSuiteTearDown(void)40 static BOOL PthreadBasicApiTestSuiteTearDown(void)
41 {
42 return TRUE;
43 }
44
ThreadPthreadCreateBasic(void * arg)45 void *ThreadPthreadCreateBasic(void *arg)
46 {
47 char *s = (char*)arg;
48 ICUNIT_ASSERT_STRING_EQUAL(s, "1234567890 !@#$%^&*()_= ZXCVBNM [];'./>?:\" +-*/qwertyuiopasdfghjklzxcvbnm", s);
49 return arg;
50 }
51
52 /**
53 * @tc.number SUB_KERNEL_PTHREAD_CREATE_0100
54 * @tc.name pthread_create create a thread
55 * @tc.desc [C- SOFTWARE -0200]
56 */
57 LITE_TEST_CASE(PthreadBasicApiTestSuite, testPthreadCreateBasic, Function | MediumTest | Level2)
58 {
59 int ret;
60 pthread_t tid;
61 char str[] = "1234567890 !@#$%^&*()_= ZXCVBNM [];'./>?:\" +-*/qwertyuiopasdfghjklzxcvbnm";
62 ret = pthread_create(&tid, NULL, ThreadPthreadCreateBasic, (void*)str);
63 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
64 ret = pthread_join(tid, NULL);
65 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
66 return 0;
67 }
68
69 /**
70 * @tc.number SUB_KERNEL_PTHREAD_JOIN_0200
71 * @tc.name Test the function of pthread_join to get the return value
72 * @tc.desc [C- SOFTWARE -0200]
73 */
74 LITE_TEST_CASE(PthreadBasicApiTestSuite, testJoinReturn, Function | MediumTest | Level3)
75 {
76 int ret;
77 pthread_t tid;
78 int num = 4; /* 4, common data for test, no special meaning */
79 void *joinRe = NULL;
80 ret = pthread_create(&tid, NULL, ThreadPublic, (void*)&num);
81 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
82 ret = pthread_join(tid, &joinRe);
83 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
84
85 int *p = (int*)joinRe;
86 ICUNIT_ASSERT_EQUAL(&num, p, &num);
87 return 0;
88 }
89
90 /**
91 * @tc.number SUB_KERNEL_PTHREAD_JOIN_0300
92 * @tc.name Test the function about pthread_join, but child thread Exited
93 * @tc.desc [C- SOFTWARE -0200]
94 */
95 LITE_TEST_CASE(PthreadBasicApiTestSuite, testJoinExited, Function | MediumTest | Level3)
96 {
97 int ret;
98 pthread_t tid;
99 ret = pthread_create(&tid, NULL, ThreadPublic, NULL);
100 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
101 usleep(50); /* 50, common data for test, no special meaning */
102 ret = pthread_join(tid, NULL);
103 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
104 return 0;
105 }
106
ThreadPthreadExitThread(void * arg)107 void *ThreadPthreadExitThread(void *arg)
108 {
109 pthread_exit(arg);
110 return NULL;
111 }
112
113 /**
114 * @tc.number SUB_KERNEL_PTHREAD_EXIT_0100
115 * @tc.name Test the return function of pthread_exit in the child thread
116 * @tc.desc [C- SOFTWARE -0200]
117 */
118 LITE_TEST_CASE(PthreadBasicApiTestSuite, testPthreadExitThread, Function | MediumTest | Level3)
119 {
120 int ret;
121 pthread_t tid;
122 int num = 4; /* 4, common data for test, no special meaning */
123 void *joinRe = NULL;
124 ret = pthread_create(&tid, NULL, ThreadPthreadExitThread, (void*)&num);
125 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
126 ret = pthread_join(tid, &joinRe);
127 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
128 int *p = (int*)joinRe;
129 ICUNIT_ASSERT_EQUAL(&num, p, &num);
130 return 0;
131 }
132
FunPthreadExit(void * arg)133 void FunPthreadExit(void *arg)
134 {
135 pthread_exit(arg);
136 }
137
ThreadPthreadExitFunction(void * arg)138 void *ThreadPthreadExitFunction(void *arg)
139 {
140 FunPthreadExit(arg);
141 return NULL;
142 }
143
144 /**
145 * @tc.number SUB_KERNEL_PTHREAD_EXIT_0200
146 * @tc.name Test the return function of pthread_exit in the child thread function
147 * @tc.desc [C- SOFTWARE -0200]
148 */
149 LITE_TEST_CASE(PthreadBasicApiTestSuite, testPthreadExitFunction, Function | MediumTest | Level3)
150 {
151 int ret;
152 pthread_t tid;
153 int num = 4; /* 4, common data for test, no special meaning */
154 void *joinRe = NULL;
155 ret = pthread_create(&tid, NULL, ThreadPthreadExitFunction, (void*)&num);
156 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
157 ret = pthread_join(tid, &joinRe);
158 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
159 int *p = (int*)joinRe;
160 ICUNIT_ASSERT_EQUAL(&num, p, &num);
161 return 0;
162 }
163
164 /**
165 * @tc.number SUB_KERNEL_PTHREAD_DETACH_0100
166 * @tc.name Use pthread_detach to detach child threads
167 * @tc.desc [C- SOFTWARE -0200]
168 */
169 LITE_TEST_CASE(PthreadBasicApiTestSuite, testPthreadDetach, Function | MediumTest | Level3)
170 {
171 int ret;
172 pthread_t tid;
173 ret = pthread_create(&tid, NULL, ThreadPublic, NULL);
174 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
175 ret = pthread_detach(tid);
176 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
177 ret = pthread_join(tid, NULL);
178 ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
179 return 0;
180 }
181
ThreadPthreadEqual(void * arg)182 void *ThreadPthreadEqual(void *arg)
183 {
184 int ret;
185 pthread_t *tid = (pthread_t*)arg;
186 ret = pthread_equal(*tid, pthread_self());
187 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
188 usleep(10); /* 10, common data for test, no special meaning */
189 return arg;
190 }
191
192 /**
193 * @tc.number SUB_KERNEL_PTHREAD_EQUAL_0100
194 * @tc.name Use pthread_equal checks process equality
195 * @tc.desc [C- SOFTWARE -0200]
196 */
197 LITE_TEST_CASE(PthreadBasicApiTestSuite, testPthreadEqual, Function | MediumTest | Level3)
198 {
199 int ret;
200 pthread_t tid;
201 ret = pthread_create(&tid, NULL, ThreadPthreadEqual, (void*)&tid);
202 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
203 ret = pthread_equal(tid, pthread_self());
204 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
205 ret = pthread_join(tid, NULL);
206 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
207 return 0;
208 }
209
210 RUN_TEST_SUITE(PthreadBasicApiTestSuite);
211
PthreadBasicApiTest(void)212 void PthreadBasicApiTest(void)
213 {
214 RUN_ONE_TESTCASE(testPthreadCreateBasic);
215 RUN_ONE_TESTCASE(testJoinReturn);
216 RUN_ONE_TESTCASE(testJoinExited);
217 RUN_ONE_TESTCASE(testPthreadExitThread);
218 RUN_ONE_TESTCASE(testPthreadExitFunction);
219 RUN_ONE_TESTCASE(testPthreadDetach);
220 RUN_ONE_TESTCASE(testPthreadEqual);
221 }
222
223