• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 /**
98  * @tc.name: PluginPlatformApiTest001
99  * @tc.desc: test GetMainWorkLoop
100  * @tc.type: FUNC
101  */
102 HWTEST_F(PluginPlatformTest, PluginPlatformApiTest001, TestSize.Level3)
103 {
104     /**
105      * @tc.steps: step1. init plugin platform
106      */
107     OHOS::HiviewDFX::HiviewPlatform platform;
108     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
109         printf("Fail to init environment. \n");
110         ASSERT_TRUE(platform.GetMainWorkLoop() == nullptr);
111     } else {
112         ASSERT_TRUE(platform.GetMainWorkLoop() != nullptr);
113     }
114 }
115 
116 #ifndef TEST_LOCAL_SRC
117 /**
118  * @tc.name: PluginPlatformDynamicPluginUnloadTest001
119  * @tc.desc: test pend loading plugin feature
120  * @tc.type: FUNC
121  * @tc.require: AR000DPTT4
122  */
123 HWTEST_F(PluginPlatformTest, PluginPlatformDynamicPluginUnloadTest001, TestSize.Level3)
124 {
125     /**
126      * @tc.steps: step1. init plugin platform
127      */
128     printf("PluginPlatformTest2.\n");
129     OHOS::HiviewDFX::HiviewPlatform platform;
130     ASSERT_TRUE(platform.InitEnvironment("/data/test/test_data/hiview_platform_config"));
131     ASSERT_EQ(access("/system/lib64/libdynamicloadpluginexample.z.so", F_OK), 0);
132 
133     sleep(1);
134     ASSERT_EQ(true, platform.IsReady());
135     auto& pluginList = platform.GetPluginMap();
136     auto size = pluginList.size();
137     ASSERT_EQ(size, 6ul);
138 
139     std::shared_ptr<Plugin> plugin = nullptr;
140     auto pos = pluginList.find("EventProcessorExample1");
141     if (pos == pluginList.end()) {
142         FAIL();
143     } else {
144         plugin = pos->second;
145     }
146     ASSERT_NE(plugin, nullptr);
147 
148     auto event = plugin->GetEvent(Event::MessageType::FAULT_EVENT);
149     event->eventId_ = 901000002;
150     bool ret = platform.PostSyncEventToTarget(plugin, "DynamicLoadPluginExample", event);
151     ASSERT_EQ(ret, true);
152     auto str = event->GetValue("DynamicLoadPluginExample");
153     printf("event %p  str:%s \n", event.get(), str.c_str());
154     ASSERT_EQ(str, "Done");
155     auto unloadEvent = plugin->GetEvent(Event::MessageType::PLUGIN_MAINTENANCE);
156     unloadEvent->SetValue("DynamicLoadPluginExample", "Unload");
157     platform.PostUnorderedEvent(plugin, unloadEvent);
158     sleep(3);
159     size = pluginList.size();
160     ASSERT_EQ(size, 5ul);
161 }
162 #endif
163