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 
Run001Check01()254 void HolisticPlatformTest::Run001Check01()
255 {
256     std::string dpet = platform_.GetHiviewProperty("DPE_Listening", "");
257     printf("dpet %s\n", dpet.c_str());
258     std::string be2t = platform_.GetHiviewProperty("EPE2_Listening", "");
259     printf("be2t %s\n", be2t.c_str());
260     if (dpet.find("testbb") == dpet.npos) {
261         FAIL();
262     }
263     if (be2t.find("testbb") != be2t.npos) {
264         FAIL();
265     }
266 }
267 
Run001Check02()268 void HolisticPlatformTest::Run001Check02()
269 {
270     std::string be2tt = platform_.GetHiviewProperty("EPE2_Listening", "");
271     printf("be2tt %s\n", be2tt.c_str());
272     if (be2tt.find("testRun001") == be2tt.npos) {
273         FAIL();
274     }
275     std::string dpett = platform_.GetHiviewProperty("DPE_Listening", "");
276     printf("dpett %s\n", dpett.c_str());
277     if (dpett.find("testRun001") == dpett.npos) {
278         FAIL();
279     }
280 }
281 /**
282  * @tc.name: HolisticPlatformRun001
283  * @tc.desc: 插件平台订阅运行检查
284  * @tc.type: FUNC
285  * @tc.require: AR000GICT3
286  */
287 HWTEST_F(HolisticPlatformTest, HolisticPlatformRun001, TestSize.Level3)
288 {
289     printf("HolisticPlatformRun001.\n");
290 
291     sleep(16); // 等待动态插件被卸载
292     auto count = count1_->size() + count2_->size();
293     ASSERT_EQ(count, 4ul);
294     std::string epe2 = platform_.GetHiviewProperty("EPE2_Listening", "");
295     if ((epe2.find("testbb") != epe2.npos) || (epe2.find("testRun001") != epe2.npos)) {
296         FAIL();
297     }
298 
299     std::string dpe = platform_.GetHiviewProperty("DPE_Listening", "");
300     if ((dpe.find("testbb") != dpe.npos) || (dpe.find("testRun001") != dpe.npos)) {
301         FAIL();
302     }
303 
304     // 发生监听事件1
305     std::shared_ptr<Event> event1 = std::make_shared<Event>("HolisticPlatformRun001", "testbb");
306     event1->messageType_ = OHOS::HiviewDFX::Event::MessageType::SYS_EVENT;
307     event1->SetValue("HolisticPlatformLoadingPlugins003", "SYS_EVENT testaa");
308     platform_.PostUnorderedEvent(nullptr, event1);
309     sleep(3);
310     Run001Check01();
311     sleep(1);
312 
313     // 发生监听事件2
314     std::shared_ptr<Event> event2 = std::make_shared<Event>("HolisticPlatformRun001", "testRun001");
315     event2->messageType_ = OHOS::HiviewDFX::Event::MessageType::RAW_EVENT;
316     const int EVENT_ID_1 = 901000111;
317     event2->eventId_ = EVENT_ID_1;
318     event2->SetValue("HolisticPlatformLoadingPlugins003", "SYS_EVENT testRun001");
319     platform_.PostUnorderedEvent(nullptr, event2);
320     sleep(3);
321     Run001Check02();
322     sleep(1);
323 }
324 
Run002Check01()325 void HolisticPlatformTest::Run002Check01()
326 {
327     int count = count1_->size() + count2_->size();
328     ASSERT_EQ(count, 4UL);
329     std::string be1 = platform_.GetHiviewProperty("BE1_OnEvent", "");
330     if (be1.find("testbbbb") != be1.npos) {
331         FAIL();
332     }
333     std::string be2 = platform_.GetHiviewProperty("BE2_OnEvent", "");
334     if ((be2.find("testbbbb") != be2.npos) || (be2.find("testcc") != be2.npos)) {
335         FAIL();
336     }
337 
338     std::string epe1 = platform_.GetHiviewProperty("EPE1_OnEvent", "");
339     if (epe1.find("testcc") != epe1.npos) {
340         FAIL();
341     }
342 
343     std::string epe2 = platform_.GetHiviewProperty("EPE2_OnEvent", "");
344     if (epe2.find("testcc") != epe2.npos) {
345         FAIL();
346     }
347 
348     std::string epe3 = platform_.GetHiviewProperty("EPE3_OnEvent", "");
349     if (epe3.find("testcc") != epe3.npos) {
350         FAIL();
351     }
352 
353     std::string epe4 = platform_.GetHiviewProperty("EPE4_OnEvent", "");
354     if (epe4.find("testcc") != epe4.npos) {
355         FAIL();
356     }
357 }
358 
Run002Check02()359 void HolisticPlatformTest::Run002Check02()
360 {
361     std::string epe1t = platform_.GetHiviewProperty("EPE1_OnEvent", "");
362     printf("epe1t %s\n", epe1t.c_str());
363     if (epe1t.find("testcc") == epe1t.npos) {
364         FAIL();
365     }
366 
367     std::string epe2t = platform_.GetHiviewProperty("EPE2_OnEvent", "");
368     printf("epe2t %s\n", epe2t.c_str());
369     if (epe2t.find("testcc") == epe2t.npos) {
370         FAIL();
371     }
372 
373     std::string epe3t = platform_.GetHiviewProperty("EPE3_OnEvent", "");
374     printf("epe3t %s\n", epe3t.c_str());
375     if (epe3t.find("testcc") == epe3t.npos) {
376         FAIL();
377     }
378 
379     std::string epe4t = platform_.GetHiviewProperty("EPE4_OnEvent", "");
380     printf("epe4t %s\n", epe4t.c_str());
381     if (epe4t.find("testcc") == epe4t.npos) {
382         FAIL();
383     }
384 
385     std::string be2t = platform_.GetHiviewProperty("BE2_OnEvent", "");
386     printf("be2t %s\n", be2t.c_str());
387     if (be2t.find("testcc") == be2t.npos) {
388         FAIL();
389     }
390 }
391 /**
392  * @tc.name: HolisticPlatformRun002
393  * @tc.desc: 插件平台流水线运行检查
394  * @tc.type: FUNC
395  * @tc.require: AR000GICT3
396  */
397 HWTEST_F(HolisticPlatformTest, HolisticPlatformRun002, TestSize.Level3)
398 {
399     printf("HolisticPlatformRun002.\n");
400 
401     sleep(16); // 等待动态插件被卸载
402     Run002Check01();
403 
404     // 发生事件1,启动流水线
405     std::ofstream fileC("/data/test/faultlog/testcc");
406     fileC << "create testcc event" << std::endl;
407     fileC.close();
408     sleep(2);
409     Run002Check02();
410     sleep(12); // 等待动态插件被卸载
411 
412     // 发生事件2,启动流水线
413     std::ofstream fileB("/data/test/faultlog2/testbb");
414     fileB << "create testbbbb event" << std::endl;
415     fileB.close();
416     sleep(2);
417     std::string be1tt = platform_.GetHiviewProperty("BE1_OnEvent", "");
418     printf("be1tt %s\n", be1tt.c_str());
419     if (be1tt.find("testbbbb") == be1tt.npos) {
420         FAIL();
421     }
422     std::string be2tt = platform_.GetHiviewProperty("BE2_OnEvent", "");
423     printf("be2tt %s\n", be2tt.c_str());
424     if (be2tt.find("testbbbb") == be2tt.npos) {
425         FAIL();
426     }
427     sleep(1);
428 }