1 /*
2 * Copyright (c) 2021 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 #include "holistic_platform_test.h"
16
17 #include <fstream>
18 #include <iostream>
19 #include <memory>
20 #include <set>
21 #include <unistd.h>
22
23 #include "event.h"
24 #include "file_util.h"
25
26 #include "bundle_event_source_example.h"
27 #include "event_source_example.h"
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30
SetUp()31 void HolisticPlatformTest::SetUp()
32 {
33 /**
34 * @tc.setup: create work directories
35 */
36 printf("SetUp.\n");
37 }
38 OHOS::HiviewDFX::HiviewPlatform HolisticPlatformTest::platform_;
39 std::set<std::string>* HolisticPlatformTest::count1_;
40 std::set<std::string>* HolisticPlatformTest::count2_;
SetUpTestCase()41 void HolisticPlatformTest::SetUpTestCase()
42 {
43 printf("SetUpTestCase.\n");
44 FileUtil::ForceCreateDirectory("/data/test/faultlog/");
45 FileUtil::ForceCreateDirectory("/data/test/faultlog2/");
46
47 const time_t IDLE_TIME = 10; // 10 seconds
48 const time_t CHECK_TIME = IDLE_TIME / 2; // 5 seconds
49 platform_.SetMaxProxyIdleTime(IDLE_TIME);
50 platform_.SetCheckProxyIdlePeriod(CHECK_TIME);
51 if (!platform_.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
52 printf("Fail to init environment. \n");
53 FAIL();
54 }
55 auto& pluginMap = platform_.GetPluginMap();
56 for (auto& i : pluginMap) {
57 printf("name:%s, \n", i.first.c_str());
58 }
59
60 auto itaa = pluginMap.find("EventSourceExample");
61 if (itaa == pluginMap.end()) {
62 FAIL();
63 }
64 auto eventSourceExample = std::static_pointer_cast<EventSourceExample>(itaa->second);
65
66 auto itbb = pluginMap.find("BundleEventSourceExample");
67 if (itbb == pluginMap.end()) {
68 FAIL();
69 }
70 auto bundleEventSourceExample = std::static_pointer_cast<BundleEventSourceExample>(itbb->second);
71 count1_ = &(eventSourceExample->count);
72 count2_ = &(bundleEventSourceExample->count);
73 }
74
TearDownTestCase()75 void HolisticPlatformTest::TearDownTestCase()
76 {
77 printf("TearDownTestCase.\n");
78 }
79
TearDown()80 void HolisticPlatformTest::TearDown()
81 {
82 printf("TearDown.\n");
83 }
84
85 /**
86 * @tc.name: HolisticPlatformLoadingPlugins001
87 * @tc.desc: 平台插件与插件包一起加载,动态卸载查询插件数目
88 * @tc.type: FUNC
89 * @tc.require: AR000GICT3
90 */
91 HWTEST_F(HolisticPlatformTest, HolisticPlatformLoadingPlugins001, TestSize.Level3)
92 {
93 /**
94 * @tc.steps: step1. init plugin platform
95 */
96 printf("HolisticPlatformLoadingPlugins001.\n");
97 auto& pluginMap = platform_.GetPluginMap();
98 for (auto& i : pluginMap) {
99 printf("name:%s, \n", i.first.c_str());
100 }
101
102 auto pluginSize = pluginMap.size();
103 ASSERT_EQ(pluginSize, 11ul);
104
105 for (auto& i : *count1_) {
106 printf("1eventSourceExample name:%s, \n", i.c_str());
107 }
108 ASSERT_EQ(count1_->size(), 4ul);
109 for (auto& i : *count2_) {
110 printf("1bundleEventSourceExample name:%s, \n", i.c_str());
111 }
112 ASSERT_EQ(count2_->size(), 3ul);
113 auto ita1 = count1_->find("EventProcessorExample3");
114 if (ita1 != count1_->end()) {
115 FAIL();
116 }
117 auto itb2 = count1_->find("EventProcessorExample4");
118 if (itb2 == count1_->end()) {
119 FAIL();
120 }
121 auto itc3 = count2_->find("BundlePluginExample2");
122 if (itc3 == count2_->end()) {
123 FAIL();
124 }
125 // 等时间动态插件卸载
126 sleep(16);
127
128 for (auto& i : *count1_) {
129 printf("2eventSourceExample name:%s, \n", i.c_str());
130 }
131 ASSERT_EQ(count1_->size(), 3ul);
132 for (auto& i : *count2_) {
133 printf("2bundleEventSourceExample name:%s, \n", i.c_str());
134 }
135 ASSERT_EQ(count2_->size(), 1ul);
136 ita1 = count1_->find("EventProcessorExample3");
137 if (ita1 != count1_->end()) {
138 FAIL();
139 }
140 itb2 = count1_->find("EventProcessorExample4");
141 if (itb2 != count1_->end()) {
142 FAIL();
143 }
144 itc3 = count2_->find("BundlePluginExample2");
145 if (itc3 != count2_->end()) {
146 FAIL();
147 }
148 }
149
LoadingPlugins002Check()150 void HolisticPlatformTest::LoadingPlugins002Check()
151 {
152 ASSERT_EQ((count1_->size() + count2_->size()), 4ul);
153 auto ita = count1_->find("EventProcessorExample3");
154 if (ita != count1_->end()) {
155 FAIL();
156 }
157 auto itb = count1_->find("EventProcessorExample4");
158 if (itb != count1_->end()) {
159 FAIL();
160 }
161
162 auto itc = count2_->find("BundlePluginExample2");
163 if (itc != count2_->end()) {
164 FAIL();
165 }
166 }
167
168 /**
169 * @tc.name: HolisticPlatformLoadingPlugins002
170 * @tc.desc: 平台插件与插件包一起加载,运行加载查询插件数目
171 * @tc.type: FUNC
172 * @tc.require: AR000GICT3
173 */
174 HWTEST_F(HolisticPlatformTest, HolisticPlatformLoadingPlugins002, TestSize.Level3)
175 {
176 printf("HolisticPlatformLoadingPlugins002.\n");
177 auto& pluginMap = platform_.GetPluginMap();
178 for (auto& i : pluginMap) {
179 printf("name:%s, \n", i.first.c_str());
180 }
181 ASSERT_EQ(pluginMap.size(), 11ul);
182
183 const std::map<std::string, std::shared_ptr<Pipeline>>& pipelines = platform_.GetPipelineMap();
184 auto it = pipelines.find("NormalPipeline");
185 if (it == pipelines.end()) {
186 printf("Fail to find pipeline with name :NormalPipeline");
187 FAIL();
188 }
189 ASSERT_EQ(it->second->GetProcessSequence().size(), 5ul);
190 for (auto& ita : it->second->GetProcessSequence()) {
191 auto ptr = ita.lock();
192 if (ptr != nullptr) {
193 printf("pipelines : %s\n", ptr->GetName().c_str());
194 } else {
195 printf("ptr != nullptr\n");
196 }
197 }
198
199 LoadingPlugins002Check();
200
201 // 发生事件,启动流水线
202 std::ofstream fileA("/data/test/faultlog/testbb");
203 fileA << "create testbb event" << std::endl;
204 fileA.close();
205 sleep(3);
206 ASSERT_EQ(count1_->size(), 5ul);
207 auto ita = count1_->find("EventProcessorExample3");
208 if (ita == count1_->end()) {
209 FAIL();
210 }
211 auto itb = count1_->find("EventProcessorExample4");
212 if (itb == count1_->end()) {
213 FAIL();
214 }
215
216 ASSERT_EQ(count2_->size(), 2ul);
217 auto itc = count2_->find("BundlePluginExample2");
218 if (itc == count2_->end()) {
219 FAIL();
220 }
221 }
222
223 /**
224 * @tc.name: HolisticPlatformLoadingPlugins003
225 * @tc.desc: 平台插件与插件包一起加载,运行加载查询插件数目
226 * @tc.type: FUNC
227 * @tc.require: AR000GICT3
228 */
229 HWTEST_F(HolisticPlatformTest, HolisticPlatformLoadingPlugins003, TestSize.Level3)
230 {
231 printf("HolisticPlatformLoadingPlugins003.\n");
232
233 auto& pluginMap = platform_.GetPluginMap();
234 for (auto& i : pluginMap) {
235 printf("name:%s, \n", i.first.c_str());
236 }
237
238 auto pluginSize = pluginMap.size();
239 ASSERT_EQ(pluginSize, 11ul);
240 sleep(16); // 等待动态插件被卸载
241 auto count = count1_->size() + count2_->size();
242 ASSERT_EQ(count, 4ul);
243 auto itb = count1_->find("EventProcessorExample4");
244 if (itb != count1_->end()) {
245 FAIL();
246 }
247
248 auto itc = count2_->find("BundlePluginExample3");
249 if (itc != count2_->end()) {
250 FAIL();
251 }
252
253 // 发生监听事件
254 std::shared_ptr<Event> event = std::make_shared<Event>("HolisticPlatformLoadingPlugins003", "testaa");
255 event->messageType_ = OHOS::HiviewDFX::Event::MessageType::SYS_EVENT;
256 event->SetValue("HolisticPlatformLoadingPlugins003", "SYS_EVENT testaa");
257 platform_.PostUnorderedEvent(nullptr, event);
258 sleep(3);
259 ASSERT_EQ(count1_->size(), 4ul);
260 itb = count1_->find("EventProcessorExample4");
261 if (itb == count1_->end()) {
262 FAIL();
263 }
264
265 ASSERT_EQ(count2_->size(), 2ul);
266 itc = count2_->find("BundlePluginExample3");
267 if (itc == count2_->end()) {
268 FAIL();
269 }
270 }
271
Run001Check01()272 void HolisticPlatformTest::Run001Check01()
273 {
274 std::string be3t = platform_.GetHiviewProperty("BE3_Listening", "");
275 printf("be3t %s\n", be3t.c_str());
276
277 std::string be4t = platform_.GetHiviewProperty("EPE4_Listening", "");
278 printf("be4t %s\n", be4t.c_str());
279
280 std::string dpet = platform_.GetHiviewProperty("DPE_Listening", "");
281 printf("dpet %s\n", dpet.c_str());
282
283 std::string be2t = platform_.GetHiviewProperty("EPE2_Listening", "");
284 printf("be2t %s\n", be2t.c_str());
285 if (be3t.find("testbb") == be3t.npos) {
286 FAIL();
287 }
288 if (be4t.find("testbb") == be4t.npos) {
289 FAIL();
290 }
291 if (dpet.find("testbb") == dpet.npos) {
292 FAIL();
293 }
294 if (be2t.find("testbb") != be2t.npos) {
295 FAIL();
296 }
297 }
298
Run001Check02()299 void HolisticPlatformTest::Run001Check02()
300 {
301 std::string be3tt = platform_.GetHiviewProperty("BE3_Listening", "");
302 printf("be3tt %s\n", be3tt.c_str());
303 if (be3tt.find("testRun001") == be3tt.npos) {
304 FAIL();
305 }
306 std::string be2tt = platform_.GetHiviewProperty("EPE2_Listening", "");
307 printf("be2tt %s\n", be2tt.c_str());
308 if (be2tt.find("testRun001") == be2tt.npos) {
309 FAIL();
310 }
311 std::string dpett = platform_.GetHiviewProperty("DPE_Listening", "");
312 printf("dpett %s\n", dpett.c_str());
313 if (dpett.find("testRun001") == dpett.npos) {
314 FAIL();
315 }
316 std::string be4tt = platform_.GetHiviewProperty("EPE4_Listening", "");
317 printf("be4tt %s\n", be4tt.c_str());
318 if (be4tt.find("testRun001") != be4tt.npos) {
319 FAIL();
320 }
321 }
322 /**
323 * @tc.name: HolisticPlatformRun001
324 * @tc.desc: 插件平台订阅运行检查
325 * @tc.type: FUNC
326 * @tc.require: AR000GICT3
327 */
328 HWTEST_F(HolisticPlatformTest, HolisticPlatformRun001, TestSize.Level3)
329 {
330 printf("HolisticPlatformRun001.\n");
331
332 sleep(16); // 等待动态插件被卸载
333 auto count = count1_->size() + count2_->size();
334 ASSERT_EQ(count, 4ul);
335 std::string be3 = platform_.GetHiviewProperty("BE3_Listening", "");
336 if ((be3.find("testbb") != be3.npos) || (be3.find("testRun001") != be3.npos)) {
337 FAIL();
338 }
339
340 std::string epe4 = platform_.GetHiviewProperty("EPE4_Listening", "");
341 if ((epe4.find("testbb") != epe4.npos) || (epe4.find("testRun001") != epe4.npos)) {
342 FAIL();
343 }
344
345 std::string epe2 = platform_.GetHiviewProperty("EPE2_Listening", "");
346 if ((epe2.find("testbb") != epe2.npos) || (epe2.find("testRun001") != epe2.npos)) {
347 FAIL();
348 }
349
350 std::string dpe = platform_.GetHiviewProperty("DPE_Listening", "");
351 if ((dpe.find("testbb") != dpe.npos) || (dpe.find("testRun001") != dpe.npos)) {
352 FAIL();
353 }
354
355 // 发生监听事件1
356 std::shared_ptr<Event> event1 = std::make_shared<Event>("HolisticPlatformRun001", "testbb");
357 event1->messageType_ = OHOS::HiviewDFX::Event::MessageType::SYS_EVENT;
358 event1->SetValue("HolisticPlatformLoadingPlugins003", "SYS_EVENT testaa");
359 platform_.PostUnorderedEvent(nullptr, event1);
360 sleep(3);
361 Run001Check01();
362 sleep(1);
363
364 // 发生监听事件2
365 std::shared_ptr<Event> event2 = std::make_shared<Event>("HolisticPlatformRun001", "testRun001");
366 event2->messageType_ = OHOS::HiviewDFX::Event::MessageType::RAW_EVENT;
367 const int EVENT_ID_1 = 901000111;
368 event2->eventId_ = EVENT_ID_1;
369 event2->SetValue("HolisticPlatformLoadingPlugins003", "SYS_EVENT testRun001");
370 platform_.PostUnorderedEvent(nullptr, event2);
371 sleep(3);
372 Run001Check02();
373 sleep(1);
374 }
375
Run002Check01()376 void HolisticPlatformTest::Run002Check01()
377 {
378 int count = count1_->size() + count2_->size();
379 ASSERT_EQ(count, 4ul);
380 std::string be1 = platform_.GetHiviewProperty("BE1_OnEvent", "");
381 if (be1.find("testbbbb") != be1.npos) {
382 FAIL();
383 }
384 std::string be2 = platform_.GetHiviewProperty("BE2_OnEvent", "");
385 if ((be2.find("testbbbb") != be2.npos) || (be2.find("testcc") != be2.npos)) {
386 FAIL();
387 }
388
389 std::string epe1 = platform_.GetHiviewProperty("EPE1_OnEvent", "");
390 if (epe1.find("testcc") != epe1.npos) {
391 FAIL();
392 }
393
394 std::string epe2 = platform_.GetHiviewProperty("EPE2_OnEvent", "");
395 if (epe2.find("testcc") != epe2.npos) {
396 FAIL();
397 }
398
399 std::string epe3 = platform_.GetHiviewProperty("EPE3_OnEvent", "");
400 if (epe3.find("testcc") != epe3.npos) {
401 FAIL();
402 }
403
404 std::string epe4 = platform_.GetHiviewProperty("EPE4_OnEvent", "");
405 if (epe4.find("testcc") != epe4.npos) {
406 FAIL();
407 }
408 }
409
Run002Check02()410 void HolisticPlatformTest::Run002Check02()
411 {
412 std::string epe1t = platform_.GetHiviewProperty("EPE1_OnEvent", "");
413 printf("epe1t %s\n", epe1t.c_str());
414 if (epe1t.find("testcc") == epe1t.npos) {
415 FAIL();
416 }
417
418 std::string epe2t = platform_.GetHiviewProperty("EPE2_OnEvent", "");
419 printf("epe2t %s\n", epe2t.c_str());
420 if (epe2t.find("testcc") == epe2t.npos) {
421 FAIL();
422 }
423
424 std::string epe3t = platform_.GetHiviewProperty("EPE3_OnEvent", "");
425 printf("epe3t %s\n", epe3t.c_str());
426 if (epe3t.find("testcc") == epe3t.npos) {
427 FAIL();
428 }
429
430 std::string epe4t = platform_.GetHiviewProperty("EPE4_OnEvent", "");
431 printf("epe4t %s\n", epe4t.c_str());
432 if (epe4t.find("testcc") == epe4t.npos) {
433 FAIL();
434 }
435
436 std::string be2t = platform_.GetHiviewProperty("BE2_OnEvent", "");
437 printf("be2t %s\n", be2t.c_str());
438 if (be2t.find("testcc") == be2t.npos) {
439 FAIL();
440 }
441 }
442 /**
443 * @tc.name: HolisticPlatformRun002
444 * @tc.desc: 插件平台流水线运行检查
445 * @tc.type: FUNC
446 * @tc.require: AR000GICT3
447 */
448 HWTEST_F(HolisticPlatformTest, HolisticPlatformRun002, TestSize.Level3)
449 {
450 printf("HolisticPlatformRun002.\n");
451
452 sleep(16); // 等待动态插件被卸载
453 Run002Check01();
454
455 // 发生事件1,启动流水线
456 std::ofstream fileC("/data/test/faultlog/testcc");
457 fileC << "create testcc event" << std::endl;
458 fileC.close();
459 sleep(2);
460 Run002Check02();
461 sleep(12); // 等待动态插件被卸载
462
463 // 发生事件2,启动流水线
464 std::ofstream fileB("/data/test/faultlog2/testbb");
465 fileB << "create testbbbb event" << std::endl;
466 fileB.close();
467 sleep(2);
468 std::string be1tt = platform_.GetHiviewProperty("BE1_OnEvent", "");
469 printf("be1tt %s\n", be1tt.c_str());
470 if (be1tt.find("testbbbb") == be1tt.npos) {
471 FAIL();
472 }
473 std::string be2tt = platform_.GetHiviewProperty("BE2_OnEvent", "");
474 printf("be2tt %s\n", be2tt.c_str());
475 if (be2tt.find("testbbbb") == be2tt.npos) {
476 FAIL();
477 }
478 sleep(1);
479 }