• 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 "plugin_module.h"
21 #include "common_types.pb.h"
22 
23 using namespace testing::ext;
24 
25 namespace {
26 const std::string SUCCESS_PLUGIN_NAME = "libmemdataplugin.z.so";
27 constexpr size_t READ_BUFFER_SIZE = 4 * 1024 * 1024;
28 #if defined(__LP64__)
29 std::string g_testPluginDir("/system/lib64/");
30 #else
31 std::string g_testPluginDir("/system/lib/");
32 #endif
33 
34 class PluginModuleTest : public ::testing::Test {
35 protected:
36     static constexpr auto TEMP_DELAY = std::chrono::milliseconds(20);
SetUpTestCase()37     static void SetUpTestCase()
38     {
39 #if defined(__i386__) || defined(__x86_64__)
40         char pluginDir[PATH_MAX + 1] = {0};
41         if (readlink("/proc/self/exe", pluginDir, PATH_MAX) > 0) {
42             char* pos = strrchr(pluginDir, '/');
43             if (pos != nullptr) {
44                 *(pos++) = '\0';
45                 g_testPluginDir = pluginDir;
46             }
47         }
48 #endif
49 
50         std::this_thread::sleep_for(TEMP_DELAY);
51     }
52 
TearDownTestCase()53     static void TearDownTestCase() {}
54 };
55 
56 /**
57  * @tc.name: plugin
58  * @tc.desc: pluginmodule normal test.
59  * @tc.type: FUNC
60  */
61 HWTEST_F(PluginModuleTest, PluginModuleNormal, TestSize.Level1)
62 {
63     std::string pluginPath = g_testPluginDir + "/" + SUCCESS_PLUGIN_NAME;
64     PluginModuleInfo info;
65     auto plugin = std::make_shared<PluginModule>(pluginPath);
66     EXPECT_TRUE(plugin->Load());
67     EXPECT_TRUE(plugin->BindFunctions());
68     EXPECT_TRUE(plugin->GetInfo(info));
69     EXPECT_TRUE(plugin->IsLoaded());
70 
71     uint32_t size = 0;
72     plugin->GetBufferSizeHint(size);
73     EXPECT_EQ(size, READ_BUFFER_SIZE);
74 
75     std::string name;
76     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     EXPECT_TRUE(plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size()));
94     EXPECT_NE(plugin->ReportResult(buffer.get(), size), 0);
95     EXPECT_TRUE(plugin->StopSession());
96 
97     EXPECT_TRUE(plugin->StartSession(nullptr, 0));
98     EXPECT_EQ(plugin->ReportResult(buffer.get(), size), 0);
99     EXPECT_TRUE(plugin->StopSession());
100 
101     EXPECT_TRUE(plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size()));
102     EXPECT_NE(plugin->ReportResult(nullptr, 0), 0);
103     EXPECT_NE(plugin->ReportResult(nullptr, 0), -1);
104     EXPECT_TRUE(plugin->StopSession());
105 
106     EXPECT_TRUE(plugin->StartSession(nullptr, 0));
107     EXPECT_EQ(plugin->ReportResult(nullptr, 0), 0);
108     EXPECT_TRUE(plugin->StopSession());
109 
110     EXPECT_TRUE(plugin->Unload());
111     EXPECT_FALSE(plugin->IsLoaded());
112 }
113 
114 /**
115  * @tc.name: plugin
116  * @tc.desc: pluginmodule abnormal test.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(PluginModuleTest, PluginModuleAbnormal, TestSize.Level1)
120 {
121     std::string pluginPath = "invalid.z.so";
122     PluginModuleInfo info;
123     auto plugin = std::make_shared<PluginModule>(pluginPath);
124     EXPECT_FALSE(plugin->Load());
125     EXPECT_FALSE(plugin->BindFunctions());
126     EXPECT_FALSE(plugin->GetInfo(info));
127     EXPECT_FALSE(plugin->IsLoaded());
128 
129     uint32_t size = 0;
130     plugin->GetBufferSizeHint(size);
131     EXPECT_EQ(size, 0);
132 
133     std::string name;
134     plugin->GetPluginName(name);
135     EXPECT_STREQ(name.c_str(), "");
136 
137     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size);
138     EXPECT_NE(buffer, nullptr);
139     EXPECT_FALSE(plugin->StartSession(nullptr, 0));
140     EXPECT_EQ(plugin->ReportResult(buffer.get(), size), -1);
141     EXPECT_FALSE(plugin->StopSession());
142     EXPECT_FALSE(plugin->Unload());
143 }
144 } // namespace
145