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_bundle_test.h"
16
17 #include <fstream>
18 #include <iostream>
19
20 #include <unistd.h>
21
22 #include "event.h"
23 #include "file_util.h"
24 #include "hiview_platform.h"
25 #include "plugin.h"
26 #include "plugin_proxy.h"
27
28 #include "platform_test_result_listener.h"
29 using namespace testing::ext;
30 using namespace OHOS::HiviewDFX;
31
SetUp()32 void PluginBundleTest::SetUp()
33 {
34 /**
35 * @tc.setup: create work directories
36 */
37 printf("SetUp.\n");
38 FileUtil::ForceCreateDirectory("/data/test/faultlog");
39 }
40
41 /**
42 * @tc.name: PluginBundleLoadTest001
43 * @tc.desc: load plugin bundle from bundle config
44 * @tc.type: FUNC
45 */
46 HWTEST_F(PluginBundleTest, PluginBundleLoadTest001, TestSize.Level3)
47 {
48 /**
49 * @tc.steps: step1. init plugin platform and set proxy idle time
50 */
51 printf("PluginBundleTest.\n");
52 OHOS::HiviewDFX::HiviewPlatform& platform = HiviewPlatform::GetInstance();
53 platform.SetMaxProxyIdleTime(10); // 10 seconds
54 platform.SetCheckProxyIdlePeriod(5); // 5 seconds
55 if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
56 printf("Fail to init environment. \n");
57 }
58
59 sleep(1);
60
61 /**
62 * @tc.steps: step2. check whether the bundle plugins have been loaded.
63 */
64 ASSERT_EQ(true, platform.IsReady());
65 auto& pluginList = platform.GetPluginMap();
66 auto size = pluginList.size();
67 ASSERT_EQ(size, 6ul);
68
69 auto& bundleMap = platform.GetPluginBundleInfoMap();
70 if (bundleMap.find("bundletest") == bundleMap.end()) {
71 printf("Failed to find bundletest bundle.\n");
72 FAIL();
73 }
74
75 /**
76 * @tc.steps: step3. check whether the proxy plugin has been created
77 */
78 auto pluginPair = pluginList.find("EventProcessorExample5");
79 if (pluginPair == pluginList.end()) {
80 printf("Failed to find EventProcessorExample5.\n");
81 FAIL();
82 }
83
84 if (pluginPair->second->GetType() != Plugin::PluginType::PROXY) {
85 printf("Failed to proxy EventProcessorExample5.\n");
86 FAIL();
87 }
88
89 /**
90 * @tc.steps: step4. check whether the proxy plugin has been instaniated
91 */
92 if (std::static_pointer_cast<PluginProxy>(pluginPair->second)->HoldInstance()) {
93 printf("Proxy should not be instaniated.\n");
94 FAIL();
95 }
96
97 std::ofstream fileC("/data/test/faultlog/ccc");
98 fileC << "create ccc event" << std::endl;
99 fileC.close();
100 sleep(1);
101
102 if (!std::static_pointer_cast<PluginProxy>(pluginPair->second)->HoldInstance()) {
103 printf("Proxy should be instaniated.\n");
104 FAIL();
105 }
106
107 sleep(15); // destroy instance after 15 seconds
108 if (std::static_pointer_cast<PluginProxy>(pluginPair->second)->HoldInstance()) {
109 printf("Proxy should not be instaniated.\n");
110 FAIL();
111 }
112 }
113
114 /**
115 * @tc.name: PluginBundleLoadTest002
116 * @tc.desc: test ReleaseDynamicModule/Destructor
117 * @tc.type: FUNC
118 */
119 HWTEST_F(PluginBundleTest, PluginBundleLoadTest002, TestSize.Level3)
120 {
121 PluginConfig config;
122 DynamicModule dynamicModule1 = LoadModule("test1");
123 PluginBundle bundle1("test1", config, dynamicModule1);
124 bundle1.ReleaseDynamicModule();
125 DynamicModule dynamicModule2 = LoadModule("libhiviewbase.z.so");
126 PluginBundle bundle2("test2", config, dynamicModule2);
127 bundle2.ReleaseDynamicModule();
128 }