• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <hwext/gtest-ext.h>
16 #include <hwext/gtest-tag.h>
17 
18 #include "profiler_capability_manager.h"
19 
20 using namespace testing::ext;
21 
22 namespace {
23 #if defined(__LP64__)
24 const std::string DEFAULT_TEST_PATH("/system/lib64/");
25 #else
26 const std::string DEFAULT_TEST_PATH("/system/lib/");
27 #endif
28 class ProfilerCapabilityManagerTest : public ::testing::Test {
29 protected:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
32 
SetUp()33     void SetUp() override {}
TearDown()34     void TearDown() override
35     {
36         ProfilerCapabilityManager::GetInstance().pluginCapabilities_.clear();
37     }
38 };
39 
40 /**
41  * @tc.name: memory plugin
42  * @tc.desc: Add plugin capability.
43  * @tc.type: FUNC
44  */
45 HWTEST_F(ProfilerCapabilityManagerTest, AddCapability, TestSize.Level1)
46 {
47     ProfilerPluginCapability cap1;
48     cap1.set_path(DEFAULT_TEST_PATH + "libcap1.so");
49     cap1.set_name("cap1");
50 
51     EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap1));
52 }
53 
54 /**
55  * @tc.name: memory plugin
56  * @tc.desc: get plugin capability.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(ProfilerCapabilityManagerTest, GetCapabilities, TestSize.Level1)
60 {
61     EXPECT_EQ(ProfilerCapabilityManager::GetInstance().GetCapabilities().size(), 0);
62 
63     std::vector<ProfilerPluginCapability> caps = ProfilerCapabilityManager::GetInstance().GetCapabilities();
64     EXPECT_EQ(caps.size(), 0);
65 
66     const int cnt = 10;
67     for (int i = 0; i < cnt; i++) {
68         ProfilerPluginCapability cap;
69         cap.set_path(DEFAULT_TEST_PATH + "libcap_" + std::to_string(i) + ".so");
70         cap.set_name("cap_" + std::to_string(i));
71         EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap));
72         caps = ProfilerCapabilityManager::GetInstance().GetCapabilities();
73         EXPECT_EQ(caps.size(), i + 1);
74     }
75 }
76 
77 /**
78  * @tc.name: memory plugin
79  * @tc.desc: get plugin capability.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(ProfilerCapabilityManagerTest, GetCapability, TestSize.Level1)
83 {
84     EXPECT_EQ(ProfilerCapabilityManager::GetInstance().GetCapability("xxx"), nullptr);
85 
86     const int cnt = 10;
87     for (int i = 0; i < cnt; i++) {
88         ProfilerPluginCapability cap;
89         cap.set_path(DEFAULT_TEST_PATH + "libcap_" + std::to_string(i) + ".so");
90         cap.set_name("cap_" + std::to_string(i));
91         EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap));
92     }
93 
94     for (int i = 0; i < cnt; i++) {
95         ProfilerPluginCapability cap;
96         cap.set_name("cap_" + std::to_string(i));
97         auto capPtr = ProfilerCapabilityManager::GetInstance().GetCapability(cap.name());
98         ASSERT_NE(capPtr, nullptr);
99         EXPECT_EQ(capPtr->name(), cap.name());
100     }
101 }
102 
103 /**
104  * @tc.name: memory plugin
105  * @tc.desc: update plugin capability.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(ProfilerCapabilityManagerTest, UpdateCapability, TestSize.Level1)
109 {
110     ProfilerPluginCapability cap1;
111     cap1.set_path(DEFAULT_TEST_PATH + "libcap1.so");
112     cap1.set_name("cap1");
113     EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap1));
114 
115     ProfilerPluginCapability cap2(cap1);
116     cap2.set_path(DEFAULT_TEST_PATH + "libcap2.so");
117     EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().UpdateCapability("cap1", cap2));
118 
119     std::vector<ProfilerPluginCapability> caps = ProfilerCapabilityManager::GetInstance().GetCapabilities();
120     ASSERT_EQ(caps.size(), 1);
121     EXPECT_EQ(caps[0].path(), cap2.path());
122 }
123 
124 /**
125  * @tc.name: server
126  * @tc.desc: remove plugin capability.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(ProfilerCapabilityManagerTest, RemoveCapability, TestSize.Level1)
130 {
131     const int cnt = 10;
132     std::vector<ProfilerPluginCapability> caps;
133     for (int i = 0; i < cnt; i++) {
134         ProfilerPluginCapability cap;
135         cap.set_path(DEFAULT_TEST_PATH + "libcap_" + std::to_string(i) + ".so");
136         cap.set_name("cap_" + std::to_string(i));
137         EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(cap));
138         caps = ProfilerCapabilityManager::GetInstance().GetCapabilities();
139         EXPECT_EQ(caps.size(), i + 1);
140     }
141 
142     for (int i = 0; i < cnt; i++) {
143         auto cap = caps[i];
144         EXPECT_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(cap.name()));
145 
146         std::vector<ProfilerPluginCapability> tmpCaps;
147         tmpCaps = ProfilerCapabilityManager::GetInstance().GetCapabilities();
148         EXPECT_EQ(tmpCaps.size(), cnt - (i + 1));
149     }
150 }
151 } // namespace