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 void SetUp() override
48 {
49 }
50
TearDown()51 void TearDown() override
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 */
83 HWTEST_F(WorkgroupInternalTest, JoinWGTest, TestSize.Level0)
84 {
85 int tid = GET_TID();
86 bool ret = JoinWG(tid, 0);
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 */
99 HWTEST_F(WorkgroupInternalTest, WorkgroupCreateTest, TestSize.Level0)
100 {
101 long interval = 10000;
102 (void)setuid(1000);
103 struct WorkGroup *ret = WorkgroupCreate(interval, 0);
104 EXPECT_NE(ret, nullptr);
105 WorkgroupClear(ret);
106
107 (void)setuid(3039);
108 struct WorkGroup *ret1 = WorkgroupCreate(interval, 0);
109 EXPECT_NE(ret1, nullptr);
110 WorkgroupClear(ret1);
111
112 (void)setuid(0);
113 struct WorkGroup *ret2 = WorkgroupCreate(interval, 0);
114 EXPECT_NE(ret2, nullptr);
115 WorkgroupClear(ret2);
116 }
117
118 /**
119 * @tc.name: WorkgroupStartIntervalTest
120 * @tc.desc: Test whether the WorkgroupStartInterval interface are normal.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(WorkgroupInternalTest, WorkgroupStartIntervalTest, TestSize.Level0)
124 {
125 #if defined(QOS_FRAME_RTG)
126 struct WorkGroup* wg = nullptr;
127 struct WorkGroup wg1 = {true, 0, {0}, 0, WgType(0)};
128 WorkgroupStartInterval(wg);
129
130 struct WorkGroup* p = &wg1;
131 p->started = true;
132 WorkgroupStartInterval(p);
133
134 int SYS_UID = 1000;
135 int TEST_UID = 10087;
136
137 (void)setuid(SYS_UID);
138 StartApp(TEST_UID);
139 SwapToFront(TEST_UID);
140 p->started = false;
141 p->rtgId = 10;
142 WorkgroupStartInterval(p);
143 EXPECT_TRUE(p->started);
144 #endif
145 }
146
147 /**
148 * @tc.name: WorkgroupStopIntervalTest
149 * @tc.desc: Test whether the WorkgroupStopInterval interface are normal.
150 * @tc.type: FUNC
151 */
152 HWTEST_F(WorkgroupInternalTest, WorkgroupStopIntervalTest, TestSize.Level0)
153 {
154 #if defined(QOS_FRAME_RTG)
155 struct WorkGroup* wg = nullptr;
156 struct WorkGroup wg1 = {true, 0, {0}, 0, WgType(0)};
157 WorkgroupStopInterval(wg);
158
159 struct WorkGroup* p = &wg1;
160 p->started = false;
161 WorkgroupStopInterval(p);
162
163 int SYS_UID = 1000;
164 int TEST_UID = 10087;
165
166 (void)setuid(SYS_UID);
167 StartApp(TEST_UID);
168 SwapToFront(TEST_UID);
169 p->started = true;
170 p->rtgId = 10;
171 WorkgroupStopInterval(p);
172 EXPECT_FALSE(p->started);
173 #endif
174 }
175
176 /**
177 * @tc.name: WorkgroupClearTest
178 * @tc.desc: Test whether the WorkgroupClear interface are normal.
179 * @tc.type: FUNC
180 */
181 HWTEST_F(WorkgroupInternalTest, WorkgroupClearTest, TestSize.Level0)
182 {
183 struct WorkGroup* wg = nullptr;
184
185 int ret = WorkgroupClear(wg);
186 EXPECT_EQ(0, ret);
187
188 long interval = 10000;
189 (void)setuid(1000);
190 struct WorkGroup *wg2 = WorkgroupCreate(interval, 0);
191 int ret2 = WorkgroupClear(wg2);
192 #if defined(QOS_FRAME_RTG)
193 EXPECT_EQ(-1, ret2);
194 #else
195 EXPECT_EQ(0, ret2);
196 #endif
197
198 (void)setuid(3039);
199 struct WorkGroup *wg3 = WorkgroupCreate(interval, 0);
200 int ret3 = WorkgroupClear(wg3);
201 #if defined(QOS_FRAME_RTG)
202 EXPECT_EQ(-1, ret3);
203 #else
204 EXPECT_EQ(0, ret3);
205 #endif
206
207 (void)setuid(0);
208 struct WorkGroup *wg4 = WorkgroupCreate(interval, 0);
209 int ret4 = WorkgroupClear(wg4);
210 EXPECT_EQ(0, ret4);
211 }
212