• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "plugin_platform_test.h"
16 
17 #include <fstream>
18 #include <iostream>
19 #include <set>
20 #include <unistd.h>
21 
22 #include "event.h"
23 #include "file_util.h"
24 #include "hiview_platform.h"
25 
26 #include "platform_test_result_listener.h"
27 using namespace testing::ext;
28 using namespace OHOS::HiviewDFX;
29 
SetUp()30 void PluginPlatformTest::SetUp()
31 {
32     /**
33      * @tc.setup: create work directories
34      */
35     printf("SetUp.\n");
36     FileUtil::ForceCreateDirectory("/data/test/faultlog");
37 }
38 
39 /**
40  * @tc.name: PluginPlatformInit001
41  * @tc.desc: send multiple events to event loop
42  * @tc.type: FUNC
43  * @tc.require: AR000DPTT4
44  */
45 HWTEST_F(PluginPlatformTest, PluginPlatformInit001, TestSize.Level3)
46 {
47     /**
48      * @tc.steps: step1. init plugin platform
49      */
50     printf("PluginPlatformTest.\n");
51     OHOS::HiviewDFX::HiviewPlatform& platform = HiviewPlatform::GetInstance();
52     const int argc = 2;
53     char arg0[] = "/system/bin/hiview";
54     char arg1[] = "-a";
55     char* argv[argc] = {arg0, arg1};
56     platform.ProcessArgsRequest(argc, argv);
57 
58     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
59         printf("Fail to init environment. \n");
60     }
61     auto listener = std::make_shared<PlatformTestResultListener>("testListener");
62     platform.RegisterUnorderedEventListener(listener);
63     listener->AddListenerInfo(Event::MessageType::FAULT_EVENT, EventListener::EventIdRange(901000000, 901004006));
64     sleep(1);
65     std::ofstream fileA("/data/test/faultlog/aaa");
66     fileA << "create aaa event" << std::endl;
67     fileA.close();
68 
69     std::ofstream fileB("/data/test/faultlog/bbb");
70     fileB << "create bbb event" << std::endl;
71     fileB.close();
72     sleep(1);
73 
74     EXPECT_EQ(listener->unorderEventCount_, 2ul);
75 
76     auto& pluginMap= platform.GetPluginMap();
77     auto pluginSize = pluginMap.size();
78     EXPECT_EQ(pluginSize, 6ul);
79 
80     auto& pipelineMap = platform.GetPipelineMap();
81     auto pipelineSize = pipelineMap.size();
82     EXPECT_EQ(pipelineSize, 3ul);
83 
84     auto workLoopMap = platform.GetWorkLoopMap();
85     EXPECT_EQ(workLoopMap.size(), 5ul);
86 
87     for (auto& entry : workLoopMap) {
88         EXPECT_EQ(true, entry.second->IsRunning());
89     }
90 
91     auto sharedWorkLoop = platform.GetSharedWorkLoop();
92     EXPECT_NE(sharedWorkLoop, nullptr);
93     EXPECT_EQ(true, sharedWorkLoop->IsRunning());
94 
95     auto baseDir = platform.GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY);
96     EXPECT_EQ("/data/test/test_data/", baseDir);
97     auto workDir = platform.GetHiViewDirectory(HiviewContext::DirectoryType::WORK_DIRECTORY);
98 
99     auto persistDir = platform.GetHiViewDirectory(HiviewContext::DirectoryType::PERSIST_DIR);
100     EXPECT_EQ("/log/hiview/", persistDir);
101 }
102 
103 /**
104  * @tc.name: PluginPlatformRepackPipelienEventTest001
105  * @tc.desc: send multiple events to event loop
106  * @tc.type: FUNC
107  * @tc.require: AR000DPTT4
108  */
109 HWTEST_F(PluginPlatformTest, PluginPlatformRepackPipelienEventTest001, TestSize.Level3)
110 {
111     /**
112      * @tc.steps: step1. plugin platform
113      */
114     printf("PluginPlatformTest2.\n");
115     OHOS::HiviewDFX::HiviewPlatform& platform = HiviewPlatform::GetInstance();
116     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
117         printf("Fail to init environment. \n");
118     }
119 
120     auto listener = std::make_shared<PlatformTestResultListener>("testListener");
121     platform.RegisterUnorderedEventListener(listener);
122     listener->AddListenerInfo(Event::MessageType::FAULT_EVENT, EventListener::EventIdRange(901000000, 901000002));
123     sleep(1);
124     std::ofstream fileA("/data/test/faultlog/aaa");
125     fileA << "create aaa event" << std::endl;
126     fileA.close();
127 
128     std::ofstream fileB("/data/test/faultlog/bbb");
129     fileB << "create bbb event" << std::endl;
130     fileB.close();
131 
132     std::ofstream fileC("/data/test/faultlog/ccc");
133     fileC << "create ccc event" << std::endl;
134     fileC.close();
135     sleep(1);
136     ASSERT_EQ(listener->unorderEventCount_, 3ul);
137 }
138 
139 /**
140  * @tc.name: PluginPlatformPluginUnloadTest001
141  * @tc.desc: request unload plugin with name
142  * @tc.type: FUNC
143  * @tc.require: AR000DPTT4
144  */
145 HWTEST_F(PluginPlatformTest, PluginPlatformPluginUnloadTest001, TestSize.Level3)
146 {
147     /**
148      * @tc.steps: step1. plugin platform
149      */
150     printf("PluginPlatformTest2.\n");
151     OHOS::HiviewDFX::HiviewPlatform platform;
152     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
153         printf("Fail to init environment. \n");
154     }
155     auto listener = std::make_shared<PlatformTestResultListener>("testListener");
156     platform.RegisterUnorderedEventListener(listener);
157     listener->AddListenerInfo(Event::MessageType::FAULT_EVENT, EventListener::EventIdRange(901000000, 901000002));
158     sleep(1);
159     ASSERT_EQ(true, platform.IsReady());
160     auto& pluginList = platform.GetPluginMap();
161     auto size = pluginList.size();
162     ASSERT_EQ(size, 6ul);
163 
164     // the example4 plugin will be unloaded after receiving the ccc message
165     std::ofstream fileC("/data/test/faultlog/ccc");
166     fileC << "create ccc event" << std::endl;
167     fileC.close();
168     sleep(2);
169 
170     auto& pluginList2 = platform.GetPluginMap();
171     auto size2 = pluginList2.size();
172     ASSERT_EQ(size2, 5ul);
173 
174     std::ofstream fileC2("/data/test/faultlog/ccc");
175     fileC2 << "create ccc event" << std::endl;
176     fileC2.close();
177     sleep(2);
178     ASSERT_EQ(listener->unorderEventCount_, 1ul);
179 }
180 
181 /**
182  * @tc.name: PluginPlatformPluginPendLoadTest001
183  * @tc.desc: test pend loading plugin feature
184  * @tc.type: FUNC
185  * @tc.require: AR000DPTT4
186  */
187 HWTEST_F(PluginPlatformTest, PluginPlatformPluginPendLoadTest001, TestSize.Level3)
188 {
189     /**
190      * @tc.steps: step1. init plugin platform
191      */
192     printf("PluginPlatformTest2.\n");
193     OHOS::HiviewDFX::HiviewPlatform platform;
194     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config1")) {
195         printf("Fail to init environment. \n");
196     }
197 
198     sleep(1);
199     /**
200      * @tc.steps: step2. check current loaded plugin number
201      */
202     ASSERT_EQ(true, platform.IsReady());
203     auto& pluginList = platform.GetPluginMap();
204     auto size = pluginList.size();
205     ASSERT_EQ(size, 4ul);
206 
207     sleep(5);
208     /**
209      * @tc.steps: step3. check final loaded plugin number
210      */
211     auto& pluginList2 = platform.GetPluginMap();
212     auto size2 = pluginList2.size();
213     ASSERT_EQ(size2, 5ul);
214 }
215 
216 /**
217  * @tc.name: PluginPlatformPluginSyncEventCallTest001
218  * @tc.desc: test pend loading plugin feature
219  * @tc.type: FUNC
220  * @tc.require: AR000DPTT4
221  */
222 HWTEST_F(PluginPlatformTest, PluginPlatformPluginSyncEventCallTest001, TestSize.Level3)
223 {
224     /**
225      * @tc.steps: step1. init plugin platform
226      */
227     printf("PluginPlatformTest2.\n");
228     OHOS::HiviewDFX::HiviewPlatform& platform = HiviewPlatform::GetInstance();
229     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
230         printf("Fail to init environment. \n");
231     }
232     auto listener = std::make_shared<PlatformTestResultListener>("testListener");
233     platform.RegisterUnorderedEventListener(listener);
234     std::set<EventListener::EventIdRange> set;
235     set.emplace(EventListener::EventIdRange(901000000));
236     set.emplace(EventListener::EventIdRange(901000001));
237     set.emplace(EventListener::EventIdRange(901000002));
238     listener->AddListenerInfo(Event::MessageType::FAULT_EVENT, set);
239     sleep(1);
240     ASSERT_EQ(true, platform.IsReady());
241     auto& pluginList = platform.GetPluginMap();
242     auto size = pluginList.size();
243     ASSERT_EQ(size, 6ul);
244 
245     std::shared_ptr<Plugin> plugin = nullptr;
246     auto pos = pluginList.find("EventProcessorExample1");
247     if (pos == pluginList.end()) {
248         FAIL();
249     } else {
250         plugin = pos->second;
251     }
252     ASSERT_NE(plugin, nullptr);
253 
254     auto event = plugin->GetEvent(Event::MessageType::FAULT_EVENT);
255     event->eventId_ = 901000002;
256     bool ret = platform.PostSyncEventToTarget(plugin, "EventProcessorExample4", event);
257     ASSERT_EQ(ret, true);
258     auto str = event->GetValue("EventProcessorExample4");
259     ASSERT_EQ(str, "Done");
260     sleep(1);
261     ASSERT_EQ(listener->unorderEventCount_, 1ul);
262     platform.PostAsyncEventToTarget(plugin, "EventProcessorExample4", event);
263     sleep(1);
264     ASSERT_EQ(listener->unorderEventCount_, 2ul);
265 
266     auto pipeEvent = std::make_shared<PipelineEvent>("test", nullptr);
267     PipelineEvent::FillPipelineInfo(plugin, "NormalPipeline", pipeEvent, true);
268     ASSERT_EQ("NormalPipeline", pipeEvent->GetPipelineInfo());
269 }
270 
271 /**
272  * @tc.name: PluginPlatformServiceStartTest001
273  * @tc.desc: start fault detect service
274  * @tc.type: FUNC
275  * @tc.require: AR000DPTT4
276  */
277 HWTEST_F(PluginPlatformTest, PluginPlatformServiceStartTest001, TestSize.Level3)
278 {
279     /**
280      * @tc.steps: step1. init plugin platform
281      */
282     printf("PluginPlatformTest2.\n");
283     OHOS::HiviewDFX::HiviewPlatform& platform = HiviewPlatform::GetInstance();;
284     printf("PluginPlatformServiceStartTest001. called\n");
285     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
286         printf("Fail to init environment. \n");
287     }
288     sleep(1);
289     printf("PluginPlatformServiceStartTest001. end\n");
290     ASSERT_EQ(true, platform.IsReady());
291 }
292 
293 #ifndef TEST_LOCAL_SRC
294 /**
295  * @tc.name: PluginPlatformDynamicPluginUnloadTest001
296  * @tc.desc: test pend loading plugin feature
297  * @tc.type: FUNC
298  * @tc.require: AR000DPTT4
299  */
300 HWTEST_F(PluginPlatformTest, PluginPlatformDynamicPluginUnloadTest001, TestSize.Level3)
301 {
302     /**
303      * @tc.steps: step1. init plugin platform
304      */
305     printf("PluginPlatformTest2.\n");
306     OHOS::HiviewDFX::HiviewPlatform& platform = HiviewPlatform::GetInstance();;
307     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
308         printf("Fail to init environment. \n");
309     }
310 
311     if (access("/system/lib64/libdynamicloadpluginexample.z.so", F_OK) != 0) {
312         printf("dynamic plugin has not been installed.\n");
313         return;
314     }
315 
316     sleep(1);
317     ASSERT_EQ(true, platform.IsReady());
318     auto& pluginList = platform.GetPluginMap();
319     auto size = pluginList.size();
320     ASSERT_EQ(size, 6ul);
321 
322     std::shared_ptr<Plugin> plugin = nullptr;
323     auto pos = pluginList.find("EventProcessorExample1");
324     if (pos == pluginList.end()) {
325         FAIL();
326     } else {
327         plugin = pos->second;
328     }
329     ASSERT_NE(plugin, nullptr);
330 
331     auto event = plugin->GetEvent(Event::MessageType::FAULT_EVENT);
332     event->eventId_ = 901000002;
333     bool ret = platform.PostSyncEventToTarget(plugin, "DynamicLoadPluginExample", event);
334     ASSERT_EQ(ret, true);
335     auto str = event->GetValue("DynamicLoadPluginExample");
336     printf("event %p  str:%s \n", event.get(), str.c_str());
337     ASSERT_EQ(str, "Done");
338     auto unloadEvent = plugin->GetEvent(Event::MessageType::PLUGIN_MAINTENANCE);
339     unloadEvent->SetValue("DynamicLoadPluginExample", "Unload");
340     platform.PostUnorderedEvent(plugin, unloadEvent);
341     sleep(3);
342     size = pluginList.size();
343     ASSERT_EQ(size, 5ul);
344 }
345 #endif
346