• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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  */
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  */
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  */
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: WorkgroupClearTest
175  * @tc.desc: Test whether the WorkgroupClear interface are normal.
176  * @tc.type: FUNC
177  */
178 HWTEST_F(WorkgroupInternalTest, WorkgroupClearTest, TestSize.Level1)
179 {
180     struct WorkGroup* wg = nullptr;
181 
182     int ret = WorkgroupClear(wg);
183     EXPECT_EQ(0, ret);
184 
185     long interval = 10000;
186     (void)setuid(1000);
187     struct WorkGroup *wg2 = WorkgroupCreate(interval);
188     int ret2 = WorkgroupClear(wg2);
189 #if defined(QOS_FRAME_RTG)
190     EXPECT_EQ(-1, ret2);
191 #else
192     EXPECT_EQ(0, ret2);
193 #endif
194 
195     (void)setuid(3039);
196     struct WorkGroup *wg3 = WorkgroupCreate(interval);
197     int ret3 = WorkgroupClear(wg3);
198 #if defined(QOS_FRAME_RTG)
199     EXPECT_EQ(-1, ret3);
200 #else
201     EXPECT_EQ(0, ret3);
202 #endif
203 
204     (void)setuid(0);
205     struct WorkGroup *wg4 = WorkgroupCreate(interval);
206     int ret4 = WorkgroupClear(wg4);
207     EXPECT_EQ(0, ret4);
208 }
209