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 "res_exe_type.h"
25 #include "res_sched_exe_client.h"
26 #include "res_sched_exe_constants.h"
27
28 namespace OHOS {
29 namespace ResourceSchedule {
30 using namespace std;
31 using namespace testing::ext;
32
33 namespace {
34 constexpr int32_t SYNC_THREAD_NUM = 100;
35 constexpr int32_t SYNC_INTERNAL_TIME = 200;
36 }
37
38 class ResSchedExeClientTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 void MockProcess(int32_t uid);
45 };
46
47
SetUpTestCase(void)48 void ResSchedExeClientTest::SetUpTestCase(void) {}
49
TearDownTestCase()50 void ResSchedExeClientTest::TearDownTestCase() {}
51
SetUp()52 void ResSchedExeClientTest::SetUp() {}
53
TearDown()54 void ResSchedExeClientTest::TearDown() {}
55
MockProcess(int32_t uid)56 void ResSchedExeClientTest::MockProcess(int32_t uid)
57 {
58 static const char *perms[] = {
59 "ohos.permission.DISTRIBUTED_DATASYNC"
60 };
61 uint64_t tokenId;
62 NativeTokenInfoParams infoInstance = {
63 .dcapsNum = 0,
64 .permsNum = 1,
65 .aclsNum = 0,
66 .dcaps = nullptr,
67 .perms = perms,
68 .acls = nullptr,
69 .processName = "samgr",
70 .aplStr = "system_core",
71 };
72 tokenId = GetAccessTokenId(&infoInstance);
73 SetSelfTokenID(tokenId);
74 setuid(uid);
75 }
76
77 /**
78 * @tc.name: SendRequestSync001
79 * @tc.desc: send res request stable test
80 * @tc.type: FUNC
81 */
82 HWTEST_F(ResSchedExeClientTest, SendRequestSync001, Function | MediumTest | Level0)
83 {
84 nlohmann::json context;
85 context["message"] = "test";
86 nlohmann::json reply;
87 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
88 ResSchedExeClient::GetInstance().SendRequestSync(ResExeType::RES_TYPE_DEBUG, 0, context, reply);
89 usleep(SYNC_INTERNAL_TIME);
90 }
91 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
92 }
93
94 /**
95 * @tc.name: SendRequestAsync001
96 * @tc.desc: report data stable test
97 * @tc.type: FUNC
98 */
99 HWTEST_F(ResSchedExeClientTest, SendRequestAsync001, Function | MediumTest | Level0)
100 {
101 nlohmann::json context;
102 context["message"] = "test";
103 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
104 ResSchedExeClient::GetInstance().SendRequestAsync(ResExeType::RES_TYPE_DEBUG, 0, context);
105 usleep(SYNC_INTERNAL_TIME);
106 }
107 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
108 }
109
110 /**
111 * @tc.name: KillProcess001
112 * @tc.desc: kill process stable test
113 * @tc.type: FUNC
114 */
115 HWTEST_F(ResSchedExeClientTest, KillProcess001, Function | MediumTest | Level0)
116 {
117 int32_t uid = 5555;
118 MockProcess(uid);
119 int32_t pid = 65535;
120 for (int i = 0; i < 100; i++) {
121 ResSchedExeClient::GetInstance().KillProcess(pid);
122 usleep(SYNC_INTERNAL_TIME);
123 }
124 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
125 }
126
127 /**
128 * @tc.name: KillProcess002
129 * @tc.desc: kill process error test
130 * @tc.type: FUNC
131 */
132 HWTEST_F(ResSchedExeClientTest, KillProcess002, Function | MediumTest | Level0)
133 {
134 int32_t uid = 5555;
135 MockProcess(uid);
136
137 int32_t pid = -1;
138 std::unordered_map<std::string, std::string> mapPayload;
139 ResSchedExeClient::GetInstance().KillProcess(pid);
140 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
141
142 pid = 65535;
143 ResSchedExeClient::GetInstance().KillProcess(pid);
144 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
145 }
146
147 /**
148 * @tc.name: SendDebugCommand001
149 * @tc.desc: send debug command stable test
150 * @tc.type: FUNC
151 */
152 HWTEST_F(ResSchedExeClientTest, SendDebugCommand001, Function | MediumTest | Level0)
153 {
154 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
155 ResSchedExeClient::GetInstance().SendDebugCommand(true);
156 usleep(SYNC_INTERNAL_TIME);
157 }
158 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
159 }
160
161 /**
162 * @tc.name: SendDebugCommand002
163 * @tc.desc: send debug command stable test
164 * @tc.type: FUNC
165 */
166 HWTEST_F(ResSchedExeClientTest, SendDebugCommand002, Function | MediumTest | Level0)
167 {
168 for (int i = 0; i < SYNC_THREAD_NUM; i++) {
169 ResSchedExeClient::GetInstance().SendDebugCommand(false);
170 usleep(SYNC_INTERNAL_TIME);
171 }
172 EXPECT_TRUE(ResSchedExeClient::GetInstance().resSchedExe_);
173 }
174
175 /**
176 * @tc.name: StopRemoteObject001
177 * @tc.desc: Stop Remote Object
178 * @tc.type: FUNC
179 */
180 HWTEST_F(ResSchedExeClientTest, StopRemoteObject001, Function | MediumTest | Level0)
181 {
182 ResSchedExeClient::GetInstance().StopRemoteObject();
183 EXPECT_TRUE(nullptr == ResSchedExeClient::GetInstance().resSchedExe_);
184 }
185 } // namespace ResourceSchedule
186 } // namespace OHOS
187