• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include <pthread.h>
3 #include <syscall.h>
4 #include <thread>
5 #include <threads.h>
6 
7 using namespace testing::ext;
8 
9 #define PTHREAD_CREATE_EINVAL (-1)
10 #define REAL_SIZE 4096
11 
12 constexpr size_t TEST_MULTIPLE_ONE = 4;
13 constexpr size_t TEST_MULTIPLE_TWO = 64;
14 constexpr size_t TEST_MULTIPLE_THREE = 68;
15 constexpr size_t TEST_BASE = 1024;
16 
17 class ThreadAttrTest : public testing::Test {
18 protected:
19     pthread_attr_t pthreadAttrMusl;
20     int attrStateMusl;
21 
SetUp()22     void SetUp() override
23     {
24         EXPECT_EQ(0, pthread_attr_init(&pthreadAttrMusl));
25     }
TearDown()26     void TearDown() override {}
27 };
28 
29 /**
30  * @tc.name: pthread_attr_setdetachstate_001
31  * @tc.desc: Setting the PTHREAD_CREATE_DETACHED property for the thread returns success.
32  * @tc.type: FUNC
33  * */
34 HWTEST_F(ThreadAttrTest, pthread_attr_setdetachstate_001, TestSize.Level1)
35 {
36     EXPECT_EQ(0, pthread_attr_setdetachstate(&pthreadAttrMusl, PTHREAD_CREATE_DETACHED));
37     EXPECT_EQ(0, pthread_attr_getdetachstate(&pthreadAttrMusl, &attrStateMusl));
38     EXPECT_EQ(PTHREAD_CREATE_DETACHED, attrStateMusl);
39 }
40 
41 /**
42  * @tc.name: pthread_attr_setdetachstate_002
43  * @tc.desc: Setting the PTHREAD_CREATE_JOINABLE property for the thread returns success.
44  * @tc.type: FUNC
45  * */
46 HWTEST_F(ThreadAttrTest, pthread_attr_setdetachstate_002, TestSize.Level1)
47 {
48     EXPECT_EQ(0, pthread_attr_setdetachstate(&pthreadAttrMusl, PTHREAD_CREATE_JOINABLE));
49     EXPECT_EQ(0, pthread_attr_getdetachstate(&pthreadAttrMusl, &attrStateMusl));
50     EXPECT_EQ(PTHREAD_CREATE_JOINABLE, attrStateMusl);
51 }
52 
53 /**
54  * @tc.name: pthread_attr_getdetachstate_001
55  * @tc.desc: Setting the invalid property for the thread returns invalidity and obtains its status verification
56  * @tc.type: FUNC
57  * */
58 HWTEST_F(ThreadAttrTest, pthread_attr_getdetachstate_001, TestSize.Level1)
59 {
60     EXPECT_EQ(EINVAL, pthread_attr_setdetachstate(&pthreadAttrMusl, PTHREAD_CREATE_EINVAL));
61     EXPECT_EQ(0, pthread_attr_getdetachstate(&pthreadAttrMusl, &attrStateMusl));
62     EXPECT_EQ(PTHREAD_CREATE_JOINABLE, attrStateMusl);
63 }
64 
65 /**
66  * @tc.name: pthread_attr_setinheritsched_001
67  * @tc.desc: Set an invalid scheduling policy and inheritance mode to ensure that the thread can run normally
68  * @tc.type: FUNC
69  * */
70 HWTEST_F(ThreadAttrTest, pthread_attr_setinheritsched_001, TestSize.Level1)
71 {
72     sched_param schedParam = { .sched_priority = sched_get_priority_max(SCHED_FIFO) + 1 };
73     int inHeritSched;
74     pthread_attr_setschedparam(&pthreadAttrMusl, &schedParam);
75     pthread_attr_setschedpolicy(&pthreadAttrMusl, SCHED_FIFO);
76     EXPECT_EQ(0, pthread_attr_setinheritsched(&pthreadAttrMusl, PTHREAD_INHERIT_SCHED));
77     pthread_attr_getinheritsched(&pthreadAttrMusl, &inHeritSched);
78     EXPECT_EQ(PTHREAD_INHERIT_SCHED, inHeritSched);
__anon5dd623f50102(void* threadArgMusl) 79     auto returnFunc = [](void* threadArgMusl) -> void* { return threadArgMusl; };
80     pthread_t threadMusl;
81     EXPECT_EQ(0, pthread_create(&threadMusl, &pthreadAttrMusl, returnFunc, nullptr));
82     EXPECT_EQ(0, pthread_join(threadMusl, nullptr));
83 }
84 
GetAttrRealGuardSizeThread(void * threadArgMusl)85 static void* GetAttrRealGuardSizeThread(void* threadArgMusl)
86 {
87     pthread_attr_t guardAttributes;
88     pthread_getattr_np(pthread_self(), &guardAttributes);
89     pthread_attr_getguardsize(&guardAttributes, reinterpret_cast<size_t*>(threadArgMusl));
90     return nullptr;
91 }
92 
GetAttrRealGuardSize(const pthread_attr_t & pthreadAttrMusl)93 static size_t GetAttrRealGuardSize(const pthread_attr_t& pthreadAttrMusl)
94 {
95     size_t attrGuardSizeResult;
96     pthread_t threadMusl;
97     pthread_create(&threadMusl, &pthreadAttrMusl, GetAttrRealGuardSizeThread, &attrGuardSizeResult);
98     pthread_join(threadMusl, nullptr);
99     return attrGuardSizeResult;
100 }
101 
GetAttrRealStackSizeThread(void * threadArgMusl)102 static void* GetAttrRealStackSizeThread(void* threadArgMusl)
103 {
104     pthread_attr_t stackAttributes;
105     pthread_getattr_np(pthread_self(), &stackAttributes);
106     pthread_attr_getstacksize(&stackAttributes, reinterpret_cast<size_t*>(threadArgMusl));
107     return nullptr;
108 }
109 
GetAttrRealStackSize(const pthread_attr_t & pthreadAttrMusl)110 static size_t GetAttrRealStackSize(const pthread_attr_t& pthreadAttrMusl)
111 {
112     size_t attrStackSizeResult;
113     pthread_t threadMusl;
114     pthread_create(&threadMusl, &pthreadAttrMusl, GetAttrRealStackSizeThread, &attrStackSizeResult);
115     pthread_join(threadMusl, nullptr);
116     return attrStackSizeResult;
117 }
118 
119 /**
120  * @tc.name: pthread_attr_setguardsize_001
121  * @tc.desc: Set/obtain smaller protection pthreadAttrMusl and verify equality, obtain unset true protection
122  *           pthreadAttrMusl and verify correctness
123  * @tc.type: FUNC
124  * */
125 HWTEST_F(ThreadAttrTest, pthread_attr_setguardsize_001, TestSize.Level1)
126 {
127     size_t attrGuardSizeMusl;
128     EXPECT_EQ(0, pthread_attr_init(&pthreadAttrMusl));
129     EXPECT_EQ(0, pthread_attr_setguardsize(&pthreadAttrMusl, TEST_MULTIPLE_ONE * TEST_MULTIPLE_TWO));
130     EXPECT_EQ(0, pthread_attr_getguardsize(&pthreadAttrMusl, &attrGuardSizeMusl));
131     EXPECT_EQ(TEST_MULTIPLE_ONE * TEST_MULTIPLE_TWO, attrGuardSizeMusl);
132     EXPECT_EQ(REAL_SIZE, GetAttrRealGuardSize(pthreadAttrMusl));
133 }
134 
135 /**
136  * @tc.name: pthread_attr_setguardsize_002
137  * @tc.desc: Set/obtain the true protection attribute size and verify equality, observe if the true size changes after
138  *           setting
139  * @tc.type: FUNC
140  * */
141 HWTEST_F(ThreadAttrTest, pthread_attr_setguardsize_002, TestSize.Level1)
142 {
143     size_t attrGuardSizeMusl;
144     EXPECT_EQ(0, pthread_attr_setguardsize(&pthreadAttrMusl, TEST_MULTIPLE_TWO * TEST_BASE));
145     EXPECT_EQ(0, pthread_attr_getguardsize(&pthreadAttrMusl, &attrGuardSizeMusl));
146     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE, attrGuardSizeMusl);
147     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE, GetAttrRealGuardSize(pthreadAttrMusl));
148 }
149 
150 /**
151  * @tc.name: pthread_attr_setguardsize_003
152  * @tc.desc: Set/obtain a protection attribute that is slightly larger than the actual one
153  *           and verify its equality. Observe whether the actual size changes after setting
154  * @tc.type: FUNC
155  * */
156 HWTEST_F(ThreadAttrTest, pthread_attr_setguardsize_003, TestSize.Level1)
157 {
158     size_t attrGuardSizeMusl;
159     EXPECT_EQ(0, pthread_attr_setguardsize(&pthreadAttrMusl, TEST_MULTIPLE_TWO * TEST_BASE + 1));
160     EXPECT_EQ(0, pthread_attr_getguardsize(&pthreadAttrMusl, &attrGuardSizeMusl));
161     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE + 1, attrGuardSizeMusl);
162     EXPECT_EQ(TEST_MULTIPLE_THREE * TEST_BASE, GetAttrRealGuardSize(pthreadAttrMusl));
163 }
164 
165 /**
166  * @tc.name: pthread_attr_setguardsize_004
167  * @tc.desc: Set/obtain protection pthreadAttrMusl that are very large compared to the real ones
168  *           and verify their equality. Observe if the real size changes after setting
169  * @tc.type: FUNC
170  * */
171 HWTEST_F(ThreadAttrTest, pthread_attr_setguardsize_004, TestSize.Level1)
172 {
173     size_t attrGuardSizeMusl;
174     EXPECT_EQ(0, pthread_attr_setguardsize(&pthreadAttrMusl, TEST_MULTIPLE_TWO * TEST_BASE * TEST_BASE));
175     EXPECT_EQ(0, pthread_attr_getguardsize(&pthreadAttrMusl, &attrGuardSizeMusl));
176     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE * TEST_BASE, attrGuardSizeMusl);
177     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE * TEST_BASE, GetAttrRealGuardSize(pthreadAttrMusl));
178 }
179 
180 /**
181  * @tc.name: pthread_attr_setstacksize_001
182  * @tc.desc: Get the default stack size and set too small stack.
183  * @tc.type: FUNC
184  * */
185 HWTEST_F(ThreadAttrTest, pthread_attr_setstacksize_001, TestSize.Level1)
186 {
187     size_t einvalStatckSize;
188     size_t attrStackSizeMusl;
189     EXPECT_EQ(0, pthread_attr_getstacksize(&pthreadAttrMusl, &einvalStatckSize));
190     EXPECT_EQ(EINVAL, pthread_attr_setstacksize(&pthreadAttrMusl, TEST_MULTIPLE_ONE * TEST_MULTIPLE_TWO));
191     EXPECT_EQ(0, pthread_attr_getstacksize(&pthreadAttrMusl, &attrStackSizeMusl));
192     EXPECT_EQ(einvalStatckSize, attrStackSizeMusl);
193     EXPECT_GE(GetAttrRealStackSize(pthreadAttrMusl), einvalStatckSize);
194 }
195 
196 /**
197  * @tc.name: pthread_attr_setstacksize_002
198  * @tc.desc: Set a larger size to be equal to the obtained one.
199  * @tc.type: FUNC
200  * */
201 HWTEST_F(ThreadAttrTest, pthread_attr_setstacksize_002, TestSize.Level1)
202 {
203     size_t attrStackSizeMusl;
204     EXPECT_EQ(0, pthread_attr_setstacksize(&pthreadAttrMusl, TEST_MULTIPLE_TWO * TEST_BASE));
205     EXPECT_EQ(0, pthread_attr_getstacksize(&pthreadAttrMusl, &attrStackSizeMusl));
206     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE, attrStackSizeMusl);
207     EXPECT_GE(GetAttrRealStackSize(pthreadAttrMusl), TEST_MULTIPLE_TWO * TEST_BASE);
208 }
209 
210 /**
211  * @tc.name: pthread_attr_setstacksize_003
212  * @tc.desc: Setting a larger size but not aligning to determine if the system will align.
213  * @tc.type: FUNC
214  * */
215 HWTEST_F(ThreadAttrTest, pthread_attr_setstacksize_003, TestSize.Level1)
216 {
217     size_t attrStackSizeMusl;
218     EXPECT_EQ(0, pthread_attr_setstacksize(&pthreadAttrMusl, TEST_MULTIPLE_TWO * TEST_BASE + 1));
219     EXPECT_EQ(0, pthread_attr_getstacksize(&pthreadAttrMusl, &attrStackSizeMusl));
220     EXPECT_EQ(TEST_MULTIPLE_TWO * TEST_BASE + 1, attrStackSizeMusl);
221 }
222 
223 /**
224  * @tc.name: pthread_attr_getscope_001
225  * @tc.desc: Judgment of successful acquisition
226  * @tc.type: FUNC
227  * */
228 HWTEST_F(ThreadAttrTest, pthread_attr_getscope_001, TestSize.Level1)
229 {
230     int attrScopeMusl;
231     EXPECT_EQ(0, pthread_attr_getscope(&pthreadAttrMusl, &attrScopeMusl));
232     EXPECT_EQ(PTHREAD_SCOPE_SYSTEM, attrScopeMusl);
233 }