• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
18 #include <unordered_map>
19 #include <vector>
20 
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 
24 #include "plugin_mgr.h"
25 #include "res_exe_type.h"
26 #include "res_sched_exe_constants.h"
27 #include "res_sched_exe_mgr.h"
28 
29 namespace OHOS {
30 namespace ResourceSchedule {
31 using namespace std;
32 using namespace testing::ext;
33 
34 class ResSchedExeMgrTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37     static void TearDownTestCase(void);
38     void SetUp();
39     void TearDown();
40     void MockProcess(int32_t uid);
41 };
42 
43 
SetUpTestCase(void)44 void ResSchedExeMgrTest::SetUpTestCase(void)
45 {
46     ResSchedExeMgr::GetInstance().Init();
47 }
48 
TearDownTestCase()49 void ResSchedExeMgrTest::TearDownTestCase()
50 {
51     ResSchedExeMgr::GetInstance().Stop();
52 }
53 
SetUp()54 void ResSchedExeMgrTest::SetUp() {}
55 
TearDown()56 void ResSchedExeMgrTest::TearDown() {}
57 
MockProcess(int32_t uid)58 void ResSchedExeMgrTest::MockProcess(int32_t uid)
59 {
60     static const char *perms[] = {
61         "ohos.permission.DISTRIBUTED_DATASYNC"
62     };
63     uint64_t tokenId;
64     NativeTokenInfoParams infoInstance = {
65         .dcapsNum = 0,
66         .permsNum = 1,
67         .aclsNum = 0,
68         .dcaps = nullptr,
69         .perms = perms,
70         .acls = nullptr,
71         .processName = "samgr",
72         .aplStr = "system_core",
73     };
74     tokenId = GetAccessTokenId(&infoInstance);
75     SetSelfTokenID(tokenId);
76     setuid(uid);
77 }
78 
79 /**
80  * @tc.name: SendRequestSync001
81  * @tc.desc: send res request stable test
82  * @tc.type: FUNC
83  */
84 HWTEST_F(ResSchedExeMgrTest, SendRequestSync001, Function | MediumTest | Level0)
85 {
86     nlohmann::json context;
87     context["message"] = "test";
88     nlohmann::json reply;
89     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(ResExeType::RES_TYPE_DEBUG, 0, context, reply);
90     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
91 }
92 
93 /**
94  * @tc.name: SendRequestSync002
95  * @tc.desc: send res request stable test
96  * @tc.type: FUNC
97  */
98 HWTEST_F(ResSchedExeMgrTest, SendRequestSync002, Function | MediumTest | Level0)
99 {
100     nlohmann::json context;
101     context["message"] = "test";
102     nlohmann::json reply;
103     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(
104         ResExeType::RES_TYPE_EXECUTOR_PLUGIN_INIT, 0, context, reply);
105     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
106 }
107 
108 /**
109  * @tc.name: SendRequestSync003
110  * @tc.desc: send res request stable test
111  * @tc.type: FUNC
112  */
113 HWTEST_F(ResSchedExeMgrTest, SendRequestSync003, Function | MediumTest | Level0)
114 {
115     nlohmann::json context;
116     context["message"] = "test";
117     nlohmann::json reply;
118     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(
119         ResExeType::RES_TYPE_CGROUP_SYNC_EVENT, 0, context, reply);
120     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
121 }
122 
123 /**
124  * @tc.name: SendRequestSync004
125  * @tc.desc: send res request stable test
126  * @tc.type: FUNC
127  */
128 HWTEST_F(ResSchedExeMgrTest, SendRequestSync004, Function | MediumTest | Level0)
129 {
130     nlohmann::json context;
131     context["message"] = "test";
132     nlohmann::json reply;
133     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(
134         ResExeType::RES_TYPE_CGROUP_PROC_TASK_SYNC_EVENT, 0, context, reply);
135     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
136 }
137 
138 /**
139  * @tc.name: SendRequestSync005
140  * @tc.desc: send res request stable test
141  * @tc.type: FUNC
142  */
143 HWTEST_F(ResSchedExeMgrTest, SendRequestSync005, Function | MediumTest | Level0)
144 {
145     nlohmann::json context;
146     context["message"] = "test";
147     nlohmann::json reply;
148     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(
149         ResExeType::RES_TYPE_SET_THREAD_SCHED_POLICY_SYNC_EVENT, 0, context, reply);
150     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
151 }
152 
153 /**
154  * @tc.name: SendRequestSync006
155  * @tc.desc: send res request stable test
156  * @tc.type: FUNC
157  */
158 HWTEST_F(ResSchedExeMgrTest, SendRequestSync006, Function | MediumTest | Level0)
159 {
160     nlohmann::json context;
161     context["tid"] = 1;
162     context["policy"] = 1;
163     nlohmann::json reply;
164     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(
165         ResExeType::RES_TYPE_SET_THREAD_SCHED_POLICY_SYNC_EVENT, 0, context, reply);
166     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
167 }
168 
169 /**
170  * @tc.name: SendRequestSync007
171  * @tc.desc: send res request stable test
172  * @tc.type: FUNC
173  */
174 HWTEST_F(ResSchedExeMgrTest, SendRequestSync007, Function | MediumTest | Level0)
175 {
176     nlohmann::json context;
177     context["tid"] = 1;
178     context["policy"] = 1;
179     nlohmann::json reply;
180     int32_t ret = ResSchedExeMgr::GetInstance().SendRequestSync(
181         ResExeType::RES_TYPE_SET_THREAD_GROUP_SCHED_POLICY_SYNC_EVENT, 0, context, reply);
182     EXPECT_EQ(ret, ResErrCode::RSSEXE_NO_ERR);
183 }
184 
185 /**
186  * @tc.name: SendRequestAsync001
187  * @tc.desc: report data stable test
188  * @tc.type: FUNC
189  */
190 HWTEST_F(ResSchedExeMgrTest, SendRequestAsync001, Function | MediumTest | Level0)
191 {
192     nlohmann::json context;
193     context["message"] = "test";
194     ResSchedExeMgr::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_EXECUTOR_PLUGIN_INIT, 0, context);
195     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
196     ResSchedExeMgr::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_DEBUG, 0, context);
197 }
198 
199 /**
200  * @tc.name: KillProcess001
201  * @tc.desc: kill process stable test
202  * @tc.type: FUNC
203  */
204 HWTEST_F(ResSchedExeMgrTest, KillProcess001, Function | MediumTest | Level0)
205 {
206     int32_t uid = 5555;
207     MockProcess(uid);
208     int32_t pid = 65535;
209     int32_t ret = ResSchedExeMgr::GetInstance().KillProcess(pid);
210     EXPECT_TRUE(ret <= ResErrCode::RSSEXE_NO_ERR);
211 }
212 
213 /**
214  * @tc.name: InitPluginMgr001
215  * @tc.desc: Verify if InitExecutorPlugin is success
216  * @tc.type: FUNC
217  */
218 HWTEST_F(ResSchedExeMgrTest, InitPluginMgr001, TestSize.Level1)
219 {
220     nlohmann::json context;
221     context["config"] = "test_config";
222     context["switch"] = "test_switch";
223     ResSchedExeMgr::GetInstance().InitPluginMgr(context);
224     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
225 }
226 
227 /**
228  * @tc.name: InitPluginMgrPretreatment001
229  * @tc.desc: Verify if InitPluginMgrPretreatment is success
230  * @tc.type: FUNC
231  */
232 HWTEST_F(ResSchedExeMgrTest, InitPluginMgrPretreatment001, TestSize.Level1)
233 {
234     nlohmann::json context;
235     context["MESSAGE_INDEX"] = 0;
236     context["MESSAGE_NUMBER"] = 1;
237     context["IPC_MESSAGE"] = "test_switch";
238     ResSchedExeMgr::GetInstance().InitPluginMgrPretreatment(context);
239     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
240 }
241 
242 /**
243  * @tc.name: BuildTraceStr001
244  * @tc.desc: BuildTraceStr test
245  * @tc.type: FUNC
246  */
247 HWTEST_F(ResSchedExeMgrTest, BuildTraceStr001, Function | MediumTest | Level0)
248 {
249     std::string trace = ResSchedExeMgr::GetInstance().BuildTraceStr(__func__, ResExeType::RES_TYPE_DEBUG, 0);
250     EXPECT_FALSE(trace.empty());
251 }
252 
253 /**
254  * @tc.name: Init001
255  * @tc.desc: Init test
256  * @tc.type: FUNC
257  */
258 HWTEST_F(ResSchedExeMgrTest, Init001, Function | MediumTest | Level0)
259 {
260     ResSchedExeMgr::GetInstance().Init();
261     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
262 }
263 
264 /**
265  * @tc.name: Stop001
266  * @tc.desc: Stop test
267  * @tc.type: FUNC
268  */
269 HWTEST_F(ResSchedExeMgrTest, Stop001, Function | MediumTest | Level0)
270 {
271     ResSchedExeMgr::GetInstance().Stop();
272     EXPECT_TRUE(nullptr == PluginMgr::GetInstance().configReader_);
273 }
274 } // namespace ResourceSchedule
275 } // namespace OHOS
276