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