1 /*
2  * Copyright (c) 2022 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 <gtest/gtest.h>
17 #include <thread>
18 #include <sched/workgroup_internal.h>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22 #if defined(QOS_FRAME_RTG)
23 #include "rtg_interface.h"
24 #include "concurrent_task_client.h"
25 #endif
26 #include "dfx/log/ffrt_log_api.h"
27 #include "common.h"
28 
29 #define GET_TID() syscall(SYS_gettid)
30 
31 using namespace testing;
32 #ifdef HWTEST_TESTING_EXT_ENABLE
33 using namespace testing::ext;
34 #endif
35 using namespace ffrt;
36 
37 class WorkgroupInternalTest : public testing::Test {
38 protected:
SetUpTestCase()39     static void SetUpTestCase()
40     {
41     }
42 
TearDownTestCase()43     static void TearDownTestCase()
44     {
45     }
46 
SetUp()47     virtual void SetUp()
48     {
49     }
50 
TearDown()51     virtual void TearDown()
52     {
53     }
54 };
55 
StartApp(int uid)56 void StartApp(int uid)
57 {
58 #if defined(QOS_FRAME_RTG)
59     std::unordered_map<std::string, std::string> payload;
60     payload["uid"] = std::to_string(uid);
61     payload["type"] = "appStart";
62     OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().ReportData(0,
63         uid, payload);
64 #endif
65 }
66 
SwapToFront(int uid)67 void SwapToFront(int uid)
68 {
69 #if defined(QOS_FRAME_RTG)
70     std::unordered_map<std::string, std::string> payload;
71     payload["uid"] = std::to_string(uid);
72     payload["type"] = "foreground";
73     OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().ReportData(0,
74         uid, payload);
75 #endif
76 }
77 
78 /**
79  * @tc.name: JoinWGTest
80  * @tc.desc: Test whether the JoinWG interface are normal.
81  * @tc.type: FUNC
82  */
HWTEST_F(WorkgroupInternalTest, JoinWGTest, TestSize.Level1)83 HWTEST_F(WorkgroupInternalTest, JoinWGTest, TestSize.Level1)
84 {
85     int tid = GET_TID();
86     bool ret = JoinWG(tid);
87 #if defined(QOS_FRAME_RTG)
88     EXPECT_FALSE(ret);
89 #else
90     EXPECT_TRUE(ret);
91 #endif
92 }
93 
94 /**
95  * @tc.name: WorkgroupCreateTest
96  * @tc.desc: Test whether the WorkgroupCreate interface are normal.
97  * @tc.type: FUNC
98  */
HWTEST_F(WorkgroupInternalTest, WorkgroupCreateTest, TestSize.Level1)99 HWTEST_F(WorkgroupInternalTest, WorkgroupCreateTest, TestSize.Level1)
100 {
101     long interval = 10000;
102     (void)setuid(1000);
103     struct WorkGroup *ret = WorkgroupCreate(interval);
104     EXPECT_NE(ret, nullptr);
105 
106     (void)setuid(3039);
107     struct WorkGroup *ret1 = WorkgroupCreate(interval);
108     EXPECT_NE(ret1, nullptr);
109 
110     (void)setuid(0);
111     struct WorkGroup *ret2 = WorkgroupCreate(interval);
112     EXPECT_NE(ret2, nullptr);
113 }
114 
115 /**
116  * @tc.name: WorkgroupStartIntervalTest
117  * @tc.desc: Test whether the WorkgroupStartInterval interface are normal.
118  * @tc.type: FUNC
119  */
HWTEST_F(WorkgroupInternalTest, WorkgroupStartIntervalTest, TestSize.Level1)120 HWTEST_F(WorkgroupInternalTest, WorkgroupStartIntervalTest, TestSize.Level1)
121 {
122 #if defined(QOS_FRAME_RTG)
123     struct WorkGroup* wg = nullptr;
124     struct WorkGroup wg1 = {true, 0, {0}, 0, WgType(0)};
125     WorkgroupStartInterval(wg);
126 
127     struct WorkGroup* p = &wg1;
128     p->started = true;
129     WorkgroupStartInterval(p);
130 
131     int SYS_UID = 1000;
132     int TEST_UID = 10087;
133 
134     (void)setuid(SYS_UID);
135     StartApp(TEST_UID);
136     SwapToFront(TEST_UID);
137     p->started = false;
138     p->rtgId = 10;
139     WorkgroupStartInterval(p);
140     EXPECT_TRUE(p->started);
141 #endif
142 }
143 
144 /**
145  * @tc.name: WorkgroupStopIntervalTest
146  * @tc.desc: Test whether the WorkgroupStopInterval interface are normal.
147  * @tc.type: FUNC
148  */
HWTEST_F(WorkgroupInternalTest, WorkgroupStopIntervalTest, TestSize.Level1)149 HWTEST_F(WorkgroupInternalTest, WorkgroupStopIntervalTest, TestSize.Level1)
150 {
151 #if defined(QOS_FRAME_RTG)
152     struct WorkGroup* wg = nullptr;
153     struct WorkGroup wg1 = {true, 0, {0}, 0, WgType(0)};
154     WorkgroupStopInterval(wg);
155 
156     struct WorkGroup* p = &wg1;
157     p->started = false;
158     WorkgroupStopInterval(p);
159 
160     int SYS_UID = 1000;
161     int TEST_UID = 10087;
162 
163     (void)setuid(SYS_UID);
164     StartApp(TEST_UID);
165     SwapToFront(TEST_UID);
166     p->started = true;
167     p->rtgId = 10;
168     WorkgroupStopInterval(p);
169     EXPECT_FALSE(p->started);
170 #endif
171 }
172 
173 /**
174  * @tc.name: WorkgroupJoinTest
175  * @tc.desc: Test whether the WorkgroupJoin interface are normal.
176  * @tc.type: FUNC
177  */
HWTEST_F(WorkgroupInternalTest, WorkgroupJoinTest, TestSize.Level1)178 HWTEST_F(WorkgroupInternalTest, WorkgroupJoinTest, TestSize.Level1)
179 {
180     long tid = 10086;
181     struct WorkGroup wg1 = {true, 0, {0}, 0, WgType(0)};
182     struct WorkGroup* wg = nullptr;
183 
184     WorkgroupJoin(wg, tid);
185     struct WorkGroup* p = &wg1;
186     p->rtgId = 10;
187     WorkgroupJoin(p, tid);
188 }
189 
190 /**
191  * @tc.name: WorkgroupClearTest
192  * @tc.desc: Test whether the WorkgroupClear interface are normal.
193  * @tc.type: FUNC
194  */
HWTEST_F(WorkgroupInternalTest, WorkgroupClearTest, TestSize.Level1)195 HWTEST_F(WorkgroupInternalTest, WorkgroupClearTest, TestSize.Level1)
196 {
197     struct WorkGroup* wg = nullptr;
198 
199     int ret = WorkgroupClear(wg);
200     EXPECT_EQ(0, ret);
201 
202     long interval = 10000;
203     (void)setuid(1000);
204     struct WorkGroup *wg2 = WorkgroupCreate(interval);
205     int ret2 = WorkgroupClear(wg2);
206 #if defined(QOS_FRAME_RTG)
207     EXPECT_EQ(-1, ret2);
208 #else
209     EXPECT_EQ(0, ret2);
210 #endif
211 
212     (void)setuid(3039);
213     struct WorkGroup *wg3 = WorkgroupCreate(interval);
214     int ret3 = WorkgroupClear(wg3);
215 #if defined(QOS_FRAME_RTG)
216     EXPECT_EQ(-1, ret3);
217 #else
218     EXPECT_EQ(0, ret3);
219 #endif
220 
221     (void)setuid(0);
222     struct WorkGroup *wg4 = WorkgroupCreate(interval);
223     int ret4 = WorkgroupClear(wg4);
224     EXPECT_EQ(0, ret4);
225 }
226