1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <setjmp.h>
21 #include <time.h>
22 #include <sys/time.h>
23 #include <sys/resource.h>
24 
25 #include "gtest/gtest.h"
26 #include "XtsActsUtil.h"
27 
28 using namespace testing::ext;
29 
30 class ActsProcessApiTest : public testing::Test {
31 protected:
32     // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)33     static void SetUpTestCase(void)
34     {
35     }
36     // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)37     static void TearDownTestCase(void)
38     {
39     }
40     // Testcase setup
SetUp()41     virtual void SetUp()
42     {
43         errno = 0;
44     }
45     // Testcase teardown
TearDown()46     virtual void TearDown()
47     {
48     }
49 };
50 
51 jmp_buf g_buf;
TestLongjmp01(void)52 void TestLongjmp01(void)
53 {
54     LogPrint("    TestLongjmp() Start\n");
55     _longjmp(g_buf, 1);
56     LogPrint("    TestLongjmp() End\n");
57 }
58 
TestLongjmp02(void)59 void TestLongjmp02(void)
60 {
61     LogPrint("    TestLongjmp() Start\n");
62     longjmp(g_buf, 1);
63     LogPrint("    TestLongjmp() End\n");
64 }
65 
ThreadFunc(void* arg)66 static void *ThreadFunc(void* arg)
67 {
68     LogPrint("    This is ThreadFunc()\n");
69     return nullptr;
70 }
71 
72 /**
73 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_LONGJMP_0100
74 * @tc.name       test _longjmp api
75 * @tc.desc       [C- SOFTWARE -0200]
76 */
HWTEST_F(ActsProcessApiTest, testLongjmp0100, Function | MediumTest | Level1)77 HWTEST_F(ActsProcessApiTest, testLongjmp0100, Function | MediumTest | Level1) {
78     int flagVal = 1;
79 
80     if (setjmp(g_buf)) {
81         LogPrint("    back TestLongjmp()\n");
82         flagVal = 0;
83     } else {
84         LogPrint("    first time to setjmp\n");
85         flagVal = 1;
86         TestLongjmp01();
87     }
88 
89     EXPECT_EQ(flagVal, 0) << "ErrInfo: _longjmp fail.";
90 }
91 
92 /**
93 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_LONGJMP_0200
94 * @tc.name       test longjmp api
95 * @tc.desc       [C- SOFTWARE -0200]
96 */
HWTEST_F(ActsProcessApiTest, testLongjmp0200, Function | MediumTest | Level1)97 HWTEST_F(ActsProcessApiTest, testLongjmp0200, Function | MediumTest | Level1) {
98     int flagVal = 1;
99 
100     if (setjmp(g_buf)) {
101         LogPrint("    back TestLongjmp()\n");
102         flagVal = 0;
103     } else {
104         LogPrint("    first time to setjmp\n");
105         flagVal = 1;
106         TestLongjmp02();
107     }
108 
109     EXPECT_EQ(flagVal, 0) << "ErrInfo: longjmp fail.";
110 }
111 
112 /**
113 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_SETJMP_0100
114 * @tc.name       test _setjmp api
115 * @tc.desc       [C- SOFTWARE -0200]
116 */
HWTEST_F(ActsProcessApiTest, testSetjmp0100, Function | MediumTest | Level1)117 HWTEST_F(ActsProcessApiTest, testSetjmp0100, Function | MediumTest | Level1) {
118     int returnVal;
119 
120     returnVal = _setjmp(g_buf);
121     LogPrint("    _setjmp env:='struct jmp_buf',   --> returnVal:='%d'\n", returnVal);
122 
123     if (returnVal != 0) {
124         LogPrint("    back testLongjmp()\n");
125         ASSERT_NE(returnVal, 0) << "ErrInfo: second time _setjmp EQ 0,   --> returnVal:='" << returnVal << "'";
126     } else {
127         LogPrint("    first time to setjmp\n");
128         ASSERT_EQ(returnVal, 0) << "ErrInfo: first time _setjmp not EQ 0,   --> returnVal:='" << returnVal << "'";
129         TestLongjmp01();
130     }
131 }
132 
133 /**
134 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_SETJMP_0200
135 * @tc.name       test setjmp api
136 * @tc.desc       [C- SOFTWARE -0200]
137 */
HWTEST_F(ActsProcessApiTest, testSetjmp0200, Function | MediumTest | Level1)138 HWTEST_F(ActsProcessApiTest, testSetjmp0200, Function | MediumTest | Level1) {
139     int returnVal;
140 
141     returnVal = setjmp(g_buf);
142     LogPrint("    setjmp env:='struct jmp_buf',   returnVal:='%d'\n", returnVal);
143 
144     if (returnVal != 0) {
145         LogPrint("    back testLongjmp()\n");
146         ASSERT_NE(returnVal, 0) << "ErrInfo: second time setjmp EQ 0,   --> returnVal:='" << returnVal << "'";
147     } else {
148         LogPrint("    first time to setjmp\n");
149         ASSERT_EQ(returnVal, 0) << "ErrInfo: first time setjmp not EQ 0,   --> returnVal:='" << returnVal << "'";
150         TestLongjmp02();
151     }
152 }
153 
154 /**
155 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_PTHREAD_SETNAME_NP_0100
156 * @tc.name       test pthread_setname_np api
157 * @tc.desc       [C- SOFTWARE -0200]
158 */
HWTEST_F(ActsProcessApiTest, testPthreadSetnameNp0100, Function | MediumTest | Level1)159 HWTEST_F(ActsProcessApiTest, testPthreadSetnameNp0100, Function | MediumTest | Level1) {
160     pthread_t thisThread;
161     int returnVal;
162 
163     thisThread = pthread_self();
164     LogPrint("    pthread_self(),   --> return pthread_t:='%u(0x%x)'\n",
165         (unsigned int)thisThread, (unsigned int)thisThread);
166 
167     returnVal = pthread_setname_np(thisThread, "funcThreadName");
168     LogPrint("    pthread_setname_np thread:='%u(0x%x)' *name:='funcThreadName',   "
169         "--> returnVal:='%d'\n", thisThread, thisThread, returnVal);
170 
171     EXPECT_EQ(returnVal, 0)
172         << "ErrInfo: pthread_setname_np thread:='" << thisThread
173         << "(0x" << thisThread << ")' *name:='funcThreadName',   "
174         << "--> returnVal:='" << returnVal << "'";
175 }
176 
177 /**
178 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_PTHREAD_SETNAME_NP_1000
179 * @tc.name       test pthread_setname_np api para stringlong
180 * @tc.desc       [C- SOFTWARE -0200]
181 */
HWTEST_F(ActsProcessApiTest, testPthreadSetnameNp1000, Function | MediumTest | Level1)182 HWTEST_F(ActsProcessApiTest, testPthreadSetnameNp1000, Function | MediumTest | Level1) {
183     pthread_t thisThread;
184     int returnVal;
185 
186     thisThread = pthread_self();
187     LogPrint("    pthread_self(),   --> return pthread_t:='%u(0x%x)'\n",
188         (unsigned int)thisThread, (unsigned int)thisThread);
189 
190     returnVal = pthread_setname_np(thisThread, "funcThreadNamelongName");
191     LogPrint("    pthread_setname_np thread:='%u(0x%x)' *name:='funcThreadNamelongName',   "
192         "--> returnVal:='%d'\n", thisThread, thisThread, returnVal);
193 
194     EXPECT_NE(returnVal, 0)
195         << "ErrInfo: pthread_setname_np thread:='" << thisThread
196         << "(0x" << thisThread << ")' *name:='funcThreadNamelongName',"
197         << "   --> returnVal:='" << returnVal << "'";
198     EXPECT_EQ(returnVal, ERANGE)
199         << "ErrInfo: pthread_setname_np thread:='" << thisThread
200         << "(0x" << thisThread << ")' *name:='funcThreadNamelongName',"
201         << "   --> returnVal:='" << returnVal << "'";
202 }
203 
204 /**
205 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_PTHREAD_SETNAME_NP_1100
206 * @tc.name       test pthread_setname_np api para notsupport
207 * @tc.desc       [C- SOFTWARE -0200]
208 */
HWTEST_F(ActsProcessApiTest, testPthreadSetnameNp1100, Function | MediumTest | Level1)209 HWTEST_F(ActsProcessApiTest, testPthreadSetnameNp1100, Function | MediumTest | Level1) {
210     pthread_t newThread;
211     int returnVal;
212 
213     returnVal = pthread_create(&newThread, NULL, ThreadFunc, NULL);
214     LogPrint("    pthread_create *thread:='&newThread' *attr:='NULL' *start_routine:='ThreadFunc' arg:='NULL', \n");
215     LogPrint("    --> returnVal:='%d', newThread:='%u(0x%x)', size:=%d\n",
216         returnVal, newThread, newThread, sizeof(newThread));
217 
218     returnVal = pthread_setname_np(newThread, "fThreadName");
219     LogPrint("    pthread_setname_np thread:='%u(0x%x)' *name:='fThreadName',   "
220         "--> returnVal:='%d'\n", newThread, newThread, returnVal);
221 
222     EXPECT_NE(returnVal, 0)
223         << "ErrInfo: pthread_setname_np thread:='" << newThread
224         << "(0x" << newThread << ")' *name:='fThreadName',"
225         << "   --> returnVal:='" << returnVal << "'";
226     EXPECT_EQ(pthread_join(newThread, nullptr), 0) << "pthread join errno = " << errno;
227 }
228 
229 /**
230 * @tc.number     SUB_KERNEL_NDKAPI_PROCESS_PTHREAD_ATTR_GETGUARDSIZE_0100
231 * @tc.name       test pthread_attr_getguardsize api
232 * @tc.desc       [C- SOFTWARE -0200]
233 */
HWTEST_F(ActsProcessApiTest, testPthreadAttrGetguardsize0100, Function | MediumTest | Level1)234 HWTEST_F(ActsProcessApiTest, testPthreadAttrGetguardsize0100, Function | MediumTest | Level1) {
235     pthread_t newThread;
236     pthread_attr_t threadAttr = {};
237     int returnVal;
238 
239     returnVal = pthread_create(&newThread, &threadAttr, ThreadFunc, NULL);
240     LogPrint("    pthread_create *thread:='&newThread' *attr:='&threadAttr' "
241         "*start_routine:='ThreadFunc' arg:='NULL', \n");
242     LogPrint("    --> returnVal:='%d',newThread:='%u(0x%x)'\n", returnVal, newThread, newThread);
243 
244     size_t guardsize;
245     returnVal = pthread_attr_getguardsize(&threadAttr, &guardsize);
246     LogPrint("    pthread_attr_getguardsize attr:='&threadAttr' guardsize:='&guardsize',"
247         "    --> returnVal:='%d', guardsize:='%d'\n", returnVal, guardsize);
248 
249     EXPECT_EQ(returnVal, 0)
250         << "ErrInfo: pthread_attr_getguardsize attr:='&threadAttr' guardsize:='&guardsize',"
251         << "    --> returnVal:='" << returnVal << "', guardsize:='" << guardsize << "'";
252 
253     returnVal = pthread_attr_setguardsize(&threadAttr, 4096);
254     LogPrint("    pthread_attr_setguardsize attr:='&threadAttr' guardsize:='4096',"
255         "    --> returnVal:='%d', guardsize:='%d'\n", returnVal, guardsize);
256 
257     returnVal = pthread_attr_getguardsize(&threadAttr, &guardsize);
258     LogPrint("    pthread_attr_getguardsize attr:='&threadAttr' guardsize:='&guardsize',"
259         "    --> returnVal:='%d', guardsize:='%d'\n", returnVal, guardsize);
260 
261     EXPECT_EQ(returnVal, 0)
262         << "ErrInfo: pthread_attr_getguardsize attr:='&threadAttr' guardsize:='&guardsize',"
263         << "    --> returnVal:='" << returnVal << "', guardsize:='" << guardsize << "'";
264     EXPECT_EQ(guardsize, 4096)
265         << "ErrInfo: pthread_attr_getguardsize attr:='&threadAttr' guardsize:='&guardsize',"
266         << "    --> returnVal:='" << returnVal << "', guardsize:='" << guardsize << "'";
267     EXPECT_EQ(pthread_join(newThread, nullptr), 0) << "pthread join errno = " << errno;
268 }
269