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 
25 #include "platform_test_result_listener.h"
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28 
SetUp()29 void PluginPlatformTest::SetUp()
30 {
31     /**
32      * @tc.setup: create work directories
33      */
34     printf("SetUp.\n");
35     FileUtil::ForceCreateDirectory("/data/test/faultlog");
36     platform.GetPluginMap();
37 }
38 
39 /**
40  * @tc.name: PluginPlatformPluginPendLoadTest001
41  * @tc.desc: test pend loading plugin feature
42  * @tc.type: FUNC
43  * @tc.require: AR000DPTT4
44  */
45 HWTEST_F(PluginPlatformTest, PluginPlatformPluginPendLoadTest001, TestSize.Level3)
46 {
47     /**
48      * @tc.steps: step1. init plugin platform
49      */
50     printf("PluginPlatformTest2.\n");
51     OHOS::HiviewDFX::HiviewPlatform platform;
52     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config1")) {
53         printf("Fail to init environment. \n");
54     }
55 
56     sleep(1);
57     /**
58      * @tc.steps: step2. check current loaded plugin number
59      */
60     ASSERT_EQ(true, platform.IsReady());
61     auto& pluginList = platform.GetPluginMap();
62     auto size = pluginList.size();
63     ASSERT_EQ(size, 4ul);
64 
65     sleep(5);
66     /**
67      * @tc.steps: step3. check final loaded plugin number
68      */
69     auto& pluginList2 = platform.GetPluginMap();
70     auto size2 = pluginList2.size();
71     ASSERT_EQ(size2, 5ul);
72 }
73 
74 
75 /**
76  * @tc.name: PluginPlatformServiceStartTest001
77  * @tc.desc: start fault detect service
78  * @tc.type: FUNC
79  * @tc.require: AR000DPTT4
80  */
81 HWTEST_F(PluginPlatformTest, PluginPlatformServiceStartTest001, TestSize.Level3)
82 {
83     /**
84      * @tc.steps: step1. init plugin platform
85      */
86     printf("PluginPlatformTest2.\n");
87     OHOS::HiviewDFX::HiviewPlatform platform;
88     printf("PluginPlatformServiceStartTest001. called\n");
89     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
90         printf("Fail to init environment. \n");
91     }
92     sleep(1);
93     printf("PluginPlatformServiceStartTest001. end\n");
94     ASSERT_EQ(true, platform.IsReady());
95 }
96 
97 #ifndef TEST_LOCAL_SRC
98 /**
99  * @tc.name: PluginPlatformDynamicPluginUnloadTest001
100  * @tc.desc: test pend loading plugin feature
101  * @tc.type: FUNC
102  * @tc.require: AR000DPTT4
103  */
104 HWTEST_F(PluginPlatformTest, PluginPlatformDynamicPluginUnloadTest001, TestSize.Level3)
105 {
106     /**
107      * @tc.steps: step1. init plugin platform
108      */
109     printf("PluginPlatformTest2.\n");
110     OHOS::HiviewDFX::HiviewPlatform platform;
111     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
112         printf("Fail to init environment. \n");
113     }
114 
115     if (access("/system/lib64/libdynamicloadpluginexample.z.so", F_OK) != 0) {
116         printf("dynamic plugin has not been installed.\n");
117         return;
118     }
119 
120     sleep(1);
121     ASSERT_EQ(true, platform.IsReady());
122     auto& pluginList = platform.GetPluginMap();
123     auto size = pluginList.size();
124     ASSERT_EQ(size, 6ul);
125 
126     std::shared_ptr<Plugin> plugin = nullptr;
127     auto pos = pluginList.find("EventProcessorExample1");
128     if (pos == pluginList.end()) {
129         FAIL();
130     } else {
131         plugin = pos->second;
132     }
133     ASSERT_NE(plugin, nullptr);
134 
135     auto event = plugin->GetEvent(Event::MessageType::FAULT_EVENT);
136     event->eventId_ = 901000002;
137     bool ret = platform.PostSyncEventToTarget(plugin, "DynamicLoadPluginExample", event);
138     ASSERT_EQ(ret, true);
139     auto str = event->GetValue("DynamicLoadPluginExample");
140     printf("event %p  str:%s \n", event.get(), str.c_str());
141     ASSERT_EQ(str, "Done");
142     auto unloadEvent = plugin->GetEvent(Event::MessageType::PLUGIN_MAINTENANCE);
143     unloadEvent->SetValue("DynamicLoadPluginExample", "Unload");
144     platform.PostUnorderedEvent(plugin, unloadEvent);
145     sleep(3);
146     size = pluginList.size();
147     ASSERT_EQ(size, 5ul);
148 }
149 #endif
150