1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. 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 16 #include <dlfcn.h> 17 #include <gtest/gtest.h> 18 19 #include "hook_socket_client.h" 20 #include "hook_service.h" 21 #include "hook_common.h" 22 #include "service_entry.h" 23 #include "socket_context.h" 24 #include "unix_socket_client.h" 25 #include "logging.h" 26 #include "sampling.h" 27 28 using namespace testing::ext; 29 30 namespace { 31 constexpr int MOBILE_BIT = 32; 32 constexpr int32_t FILTER_SIZE = 100; 33 constexpr int32_t SMB_SIZE = 409600; 34 static ClientConfig g_ClientConfigTest = {0}; 35 36 class HookSocketClientTest : public ::testing::Test { 37 public: SetUpTestCase()38 static void SetUpTestCase() {} TearDownTestCase()39 static void TearDownTestCase() {} SetUp()40 void SetUp() {} TearDown()41 void TearDown() {} 42 }; 43 44 /* 45 * @tc.name: ProtocolProc 46 * @tc.desc: test HookSocketClient::ProtocolProc with normal case. 47 * @tc.type: FUNC 48 */ 49 HWTEST_F(HookSocketClientTest, ProtocolProc, TestSize.Level1) 50 { 51 uint64_t config = FILTER_SIZE; 52 config <<= MOBILE_BIT; 53 config |= SMB_SIZE; 54 Sampling sampler; 55 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 56 SocketContext socketContext; 57 auto ptr = reinterpret_cast<const int8_t*>(&config); 58 auto size = sizeof(uint64_t); 59 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 60 } 61 62 /* 63 * @tc.name: SendStack 64 * @tc.desc: test HookSocketClient::SendStack with normal case. 65 * @tc.type: FUNC 66 */ 67 #ifdef __aarch64__ 68 HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1) 69 { 70 uint64_t config = FILTER_SIZE; 71 config <<= MOBILE_BIT; 72 config |= SMB_SIZE; 73 Sampling sampler; 74 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 75 SocketContext socketContext; 76 auto ptr = reinterpret_cast<const int8_t*>(&config); 77 auto size = sizeof(ClientConfig); 78 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 79 80 struct timespec ts = {}; 81 clock_gettime(CLOCK_REALTIME, &ts); 82 size_t metaSize = sizeof(ts); 83 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 84 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 85 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 86 } 87 metaSize = sizeof(ts); 88 hookClient.unixSocketClient_ = nullptr; 89 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 90 EXPECT_FALSE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 91 hookClient.unixSocketClient_ = std::make_shared<UnixSocketClient>(); 92 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 93 EXPECT_TRUE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 94 } 95 #endif 96 97 /* 98 * @tc.name: FdListSize 99 * @tc.desc: test fd list size with normal case. 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(HookSocketClientTest, FdListSize, TestSize.Level1) 103 { 104 ClientConfig clientConfig; 105 SocketContext socketContext; 106 Sampling sampler; 107 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 108 auto size = sizeof(clientConfig); 109 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 110 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 111 ASSERT_EQ(hookClient.GetSmbFds().size(), 1); 112 ASSERT_EQ(hookClient.GetEventFds().size(), 1); 113 } 114 115 /* 116 * @tc.name: GetSmbFds 117 * @tc.desc: test HookSocketClient::GetSmbFds with normal case. 118 * @tc.type: FUNC 119 */ 120 HWTEST_F(HookSocketClientTest, GetSmbFds, TestSize.Level1) 121 { 122 ClientConfig clientConfig; 123 SocketContext socketContext; 124 Sampling sampler; 125 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 126 auto size = sizeof(clientConfig); 127 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 128 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 129 std::vector<int> smbFds = hookClient.GetSmbFds(); 130 for (size_t i = 0; i < smbFds.size(); ++i) { 131 ASSERT_EQ(smbFds[i], -1); 132 } 133 } 134 135 /* 136 * @tc.name: GetEventFds 137 * @tc.desc: test HookSocketClient::GetEventFds with normal case. 138 * @tc.type: FUNC 139 */ 140 HWTEST_F(HookSocketClientTest, GetEventFds, TestSize.Level1) 141 { 142 ClientConfig clientConfig; 143 SocketContext socketContext; 144 Sampling sampler; 145 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 146 auto size = sizeof(clientConfig); 147 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 148 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 149 std::vector<int> eventFds = hookClient.GetEventFds(); 150 for (size_t i = 0; i < eventFds.size(); ++i) { 151 ASSERT_EQ(eventFds[i], -1); 152 } 153 } 154 155 /* 156 * @tc.name: SendNmdInfo 157 * @tc.desc: test HookSocketClient::SendNmdInfo with normal case. 158 * @tc.type: FUNC 159 */ 160 #ifdef __aarch64__ 161 HWTEST_F(HookSocketClientTest, SendNmdInfo, TestSize.Level1) 162 { 163 uint64_t config = FILTER_SIZE; 164 config <<= MOBILE_BIT; 165 config |= SMB_SIZE; 166 Sampling sampler; 167 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 168 SocketContext socketContext; 169 auto ptr = reinterpret_cast<const int8_t*>(&config); 170 auto size = sizeof(ClientConfig); 171 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 172 173 struct timespec ts = {}; 174 clock_gettime(CLOCK_REALTIME, &ts); 175 size_t metaSize = sizeof(ts); 176 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 177 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 178 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 179 } 180 ASSERT_FALSE(hookClient.SendNmdInfo()); 181 } 182 183 /* 184 * @tc.name: SendSimplifiedNmdInfo 185 * @tc.desc: test HookSocketClient::SendSimplifiedNmdInfo with normal case. 186 * @tc.type: FUNC 187 */ 188 HWTEST_F(HookSocketClientTest, SendSimplifiedNmdInfo, TestSize.Level1) 189 { 190 uint64_t config = FILTER_SIZE; 191 config <<= MOBILE_BIT; 192 config |= SMB_SIZE; 193 Sampling sampler; 194 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 195 SocketContext socketContext; 196 auto ptr = reinterpret_cast<const int8_t*>(&config); 197 auto size = sizeof(ClientConfig); 198 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 199 200 struct timespec ts = {}; 201 clock_gettime(CLOCK_REALTIME, &ts); 202 size_t metaSize = sizeof(ts); 203 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 204 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 205 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 206 } 207 ASSERT_FALSE(hookClient.SendSimplifiedNmdInfo()); 208 } 209 #endif 210 } // namespace 211