• 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 
16 #include <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 #include <thread>
19 
20 #include "common.h"
21 #include "plugin_module.h"
22 #include "common_types.pb.h"
23 
24 using namespace testing::ext;
25 
26 namespace {
27 const std::string SUCCESS_PLUGIN_NAME = "libmemdataplugin.z.so";
28 constexpr size_t READ_BUFFER_SIZE = 4 * 1024 * 1024;
29 #if defined(__LP64__)
30 std::string g_testPluginDir("/system/lib64/");
31 #else
32 std::string g_testPluginDir("/system/lib/");
33 #endif
34 
35 class PluginModuleTest : public ::testing::Test {
36 protected:
37     static constexpr auto TEMP_DELAY = std::chrono::milliseconds(20);
SetUpTestCase()38     static void SetUpTestCase()
39     {
40 #if defined(__i386__) || defined(__x86_64__)
41         char pluginDir[PATH_MAX + 1] = {0};
42         if (readlink("/proc/self/exe", pluginDir, PATH_MAX) > 0) {
43             char* pos = strrchr(pluginDir, '/');
44             if (pos != nullptr) {
45                 *(pos++) = '\0';
46                 g_testPluginDir = pluginDir;
47             }
48         }
49 #endif
50 
51         std::this_thread::sleep_for(TEMP_DELAY);
52     }
53 
TearDownTestCase()54     static void TearDownTestCase() {}
55 };
56 
57 /**
58  * @tc.name: plugin
59  * @tc.desc: pluginmodule normal test.
60  * @tc.type: FUNC
61  */
62 HWTEST_F(PluginModuleTest, PluginModuleNormal, TestSize.Level1)
63 {
64     std::string pluginPath = g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME;
65     PluginModuleInfo info;
66     auto plugin = std::make_shared<PluginModule>(pluginPath);
67     EXPECT_TRUE(plugin->Load());
68     EXPECT_TRUE(plugin->BindFunctions());
69     EXPECT_TRUE(plugin->GetInfo(info));
70     EXPECT_TRUE(plugin->IsLoaded());
71 
72     uint32_t size = 0;
73     plugin->GetBufferSizeHint(size);
74     EXPECT_EQ(size, READ_BUFFER_SIZE);
75     std::string name;
76     EXPECT_TRUE(plugin->GetPluginName(name));
77     EXPECT_STREQ(name.c_str(), "memory-plugin");
78 
79     const uint8_t configData[] = {74, 1, 10};
80     ProfilerPluginConfig config;
81     config.set_name(g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME);
82     config.set_config_data((const void*)configData, 3);
83     config.set_sample_interval(1000);
84     plugin->SetConfigData(config.config_data());
85 
86     std::string cfgData = plugin->GetConfigData();
87     EXPECT_EQ(cfgData.c_str()[0], 74);
88     EXPECT_EQ(cfgData.c_str()[1], 1);
89     EXPECT_EQ(cfgData.c_str()[2], 10);
90 
91     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size);
92     EXPECT_NE(buffer, nullptr);
93     plugin->SetClockId(COMMON::GetClockId("realtime"));
94     (void)plugin->GetClockId();
95     EXPECT_FALSE(plugin->GetPath().empty());
96     EXPECT_TRUE(plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size()));
97     EXPECT_FALSE(plugin->GetStandaloneFileData());
98     EXPECT_TRUE(plugin->IsRunning());
99     EXPECT_NE(plugin->ReportResult(buffer.get(), size), 0);
100     EXPECT_TRUE(plugin->StopSession());
101 
102     EXPECT_TRUE(plugin->StartSession(nullptr, 0));
103     EXPECT_EQ(plugin->ReportResult(buffer.get(), size), 0);
104     EXPECT_TRUE(plugin->StopSession());
105 
106     EXPECT_TRUE(plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size()));
107     EXPECT_NE(plugin->ReportResult(nullptr, 0), 0);
108     EXPECT_NE(plugin->ReportResult(nullptr, 0), -1);
109     EXPECT_TRUE(plugin->StopSession());
110 
111     EXPECT_TRUE(plugin->StartSession(nullptr, 0));
112     EXPECT_EQ(plugin->ReportResult(nullptr, 0), 0);
113     EXPECT_TRUE(plugin->StopSession());
114 
115     EXPECT_TRUE(plugin->Unload());
116     EXPECT_FALSE(plugin->IsLoaded());
117 }
118 
119 /**
120  * @tc.name: plugin
121  * @tc.desc: pluginmodule abnormal test.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(PluginModuleTest, PluginModuleAbnormal, TestSize.Level1)
125 {
126     std::string pluginPath = "invalid.z.so";
127     PluginModuleInfo info;
128     auto plugin = std::make_shared<PluginModule>(pluginPath);
129     EXPECT_FALSE(plugin->Load());
130     EXPECT_FALSE(plugin->BindFunctions());
131     EXPECT_FALSE(plugin->GetInfo(info));
132     EXPECT_FALSE(plugin->IsLoaded());
133 
134     uint32_t size = 0;
135     plugin->GetBufferSizeHint(size);
136     EXPECT_EQ(size, 0);
137 
138     std::string name;
139     EXPECT_FALSE(plugin->GetPluginName(name));
140     EXPECT_STREQ(name.c_str(), "");
141 
142     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size);
143     EXPECT_FALSE(plugin->StartSession(nullptr, 0));
144     EXPECT_FALSE(plugin->GetStandaloneFileData());
145     EXPECT_FALSE(plugin->IsRunning());
146     EXPECT_EQ(plugin->ReportResult(buffer.get(), size), -1);
147     EXPECT_FALSE(plugin->StopSession());
148     EXPECT_FALSE(plugin->Unload());
149 }
150 
151 /**
152  * @tc.name: plugin
153  * @tc.desc: pluginmodule  test.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(PluginModuleTest, PluginModuleTest, TestSize.Level1)
157 {
158     std::string pluginPath = g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME;
159     PluginModuleInfo info;
160     auto plugin = std::make_shared<PluginModule>(pluginPath);
161 
162     std::string outFileName;
163     EXPECT_FALSE(plugin->GetOutFileName(outFileName));
164     std::string pluginVersion;
165     EXPECT_FALSE(plugin->GetPluginVersion(pluginVersion));
166     EXPECT_EQ(plugin->GetWriter(), nullptr);
167 
168     EXPECT_TRUE(plugin->Load());
169     EXPECT_TRUE(plugin->BindFunctions());
170     EXPECT_TRUE(plugin->GetInfo(info));
171     EXPECT_TRUE(plugin->IsLoaded());
172     EXPECT_TRUE(plugin->GetOutFileName(outFileName));
173     EXPECT_EQ(outFileName, "");
174     EXPECT_TRUE(plugin->GetPluginVersion(pluginVersion));
175     EXPECT_EQ(pluginVersion, "1.02");
176     BufferWriterPtr writer;
177     EXPECT_TRUE(plugin->RegisterWriter(writer));
178 }
179 } // namespace
180