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