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: SendRequestAsync001
95  * @tc.desc: report data stable test
96  * @tc.type: FUNC
97  */
98 HWTEST_F(ResSchedExeMgrTest, SendRequestAsync001, Function | MediumTest | Level0)
99 {
100     nlohmann::json context;
101     context["message"] = "test";
102     ResSchedExeMgr::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_EXECUTOR_PLUGIN_INIT, 0, context);
103     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
104     ResSchedExeMgr::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_DEBUG, 0, context);
105 }
106 
107 /**
108  * @tc.name: KillProcess001
109  * @tc.desc: kill process stable test
110  * @tc.type: FUNC
111  */
112 HWTEST_F(ResSchedExeMgrTest, KillProcess001, Function | MediumTest | Level0)
113 {
114     int32_t uid = 5555;
115     MockProcess(uid);
116     int32_t pid = 65535;
117     int32_t ret = ResSchedExeMgr::GetInstance().KillProcess(pid);
118     EXPECT_TRUE(ret <= ResErrCode::RSSEXE_NO_ERR);
119 }
120 
121 /**
122  * @tc.name: InitPluginMgr001
123  * @tc.desc: Verify if InitExecutorPlugin is success
124  * @tc.type: FUNC
125  */
126 HWTEST_F(ResSchedExeMgrTest, InitPluginMgr001, TestSize.Level1)
127 {
128     nlohmann::json context;
129     context["config"] = "test_config";
130     context["switch"] = "test_switch";
131     ResSchedExeMgr::GetInstance().InitPluginMgr(context);
132     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
133 }
134 
135 /**
136  * @tc.name: BuildTraceStr001
137  * @tc.desc: BuildTraceStr test
138  * @tc.type: FUNC
139  */
140 HWTEST_F(ResSchedExeMgrTest, BuildTraceStr001, Function | MediumTest | Level0)
141 {
142     std::string trace = ResSchedExeMgr::GetInstance().BuildTraceStr(__func__, ResExeType::RES_TYPE_DEBUG, 0);
143     EXPECT_FALSE(trace.empty());
144 }
145 
146 /**
147  * @tc.name: Init001
148  * @tc.desc: Init test
149  * @tc.type: FUNC
150  */
151 HWTEST_F(ResSchedExeMgrTest, Init001, Function | MediumTest | Level0)
152 {
153     ResSchedExeMgr::GetInstance().Init();
154     EXPECT_TRUE(PluginMgr::GetInstance().configReader_);
155 }
156 
157 /**
158  * @tc.name: Stop001
159  * @tc.desc: Stop test
160  * @tc.type: FUNC
161  */
162 HWTEST_F(ResSchedExeMgrTest, Stop001, Function | MediumTest | Level0)
163 {
164     ResSchedExeMgr::GetInstance().Stop();
165     EXPECT_TRUE(nullptr == PluginMgr::GetInstance().configReader_);
166 }
167 } // namespace ResourceSchedule
168 } // namespace OHOS
169