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 "service_entry.h" 22 #include "socket_context.h" 23 #include "unix_socket_client.h" 24 #include "logging.h" 25 #include "sampling.h" 26 27 using namespace testing::ext; 28 29 namespace { 30 constexpr int MOBILE_BIT = 32; 31 constexpr int32_t FILTER_SIZE = 100; 32 constexpr int32_t SMB_SIZE = 409600; 33 static ClientConfig g_ClientConfigTest = {0}; 34 35 class HookSocketClientTest : public ::testing::Test { 36 public: SetUpTestCase()37 static void SetUpTestCase() {} TearDownTestCase()38 static void TearDownTestCase() {} SetUp()39 void SetUp() {} TearDown()40 void TearDown() {} 41 }; 42 43 /* 44 * @tc.name: ProtocolProc 45 * @tc.desc: test HookSocketClient::ProtocolProc with normal case. 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(HookSocketClientTest, ProtocolProc, TestSize.Level1) 49 { 50 uint64_t config = FILTER_SIZE; 51 config <<= MOBILE_BIT; 52 config |= SMB_SIZE; 53 Sampling sampler; 54 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 55 SocketContext socketContext; 56 auto ptr = reinterpret_cast<const int8_t*>(&config); 57 auto size = sizeof(uint64_t); 58 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 59 } 60 61 /* 62 * @tc.name: SendStack 63 * @tc.desc: test HookSocketClient::SendStack with normal case. 64 * @tc.type: FUNC 65 */ 66 HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1) 67 { 68 uint64_t config = FILTER_SIZE; 69 config <<= MOBILE_BIT; 70 config |= SMB_SIZE; 71 Sampling sampler; 72 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 73 SocketContext socketContext; 74 auto ptr = reinterpret_cast<const int8_t*>(&config); 75 auto size = sizeof(ClientConfig); 76 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 77 78 struct timespec ts = {}; 79 clock_gettime(CLOCK_REALTIME, &ts); 80 size_t metaSize = sizeof(ts); 81 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 82 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 83 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 84 } 85 metaSize = sizeof(ts); 86 hookClient.unixSocketClient_ = nullptr; 87 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 88 EXPECT_FALSE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 89 hookClient.unixSocketClient_ = std::make_shared<UnixSocketClient>(); 90 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 91 EXPECT_TRUE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 92 } 93 94 /* 95 * @tc.name: GetSmbFd 96 * @tc.desc: test HookSocketClient::GetSmbFd with normal case. 97 * @tc.type: FUNC 98 */ 99 HWTEST_F(HookSocketClientTest, GetSmbFd, TestSize.Level1) 100 { 101 ClientConfig clientConfig; 102 SocketContext socketContext; 103 Sampling sampler; 104 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 105 auto size = sizeof(clientConfig); 106 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 107 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 108 ASSERT_EQ(hookClient.GetSmbFd(), -1); 109 } 110 111 /* 112 * @tc.name: GetEventFd 113 * @tc.desc: test HookSocketClient::GetEventFd with normal case. 114 * @tc.type: FUNC 115 */ 116 HWTEST_F(HookSocketClientTest, GetEventFd, TestSize.Level1) 117 { 118 ClientConfig clientConfig; 119 SocketContext socketContext; 120 Sampling sampler; 121 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 122 auto size = sizeof(clientConfig); 123 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 124 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 125 ASSERT_EQ(hookClient.GetEventFd(), -1); 126 } 127 128 /* 129 * @tc.name: SendNmdInfo 130 * @tc.desc: test HookSocketClient::SendNmdInfo with normal case. 131 * @tc.type: FUNC 132 */ 133 HWTEST_F(HookSocketClientTest, SendNmdInfo, TestSize.Level1) 134 { 135 uint64_t config = FILTER_SIZE; 136 config <<= MOBILE_BIT; 137 config |= SMB_SIZE; 138 Sampling sampler; 139 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 140 SocketContext socketContext; 141 auto ptr = reinterpret_cast<const int8_t*>(&config); 142 auto size = sizeof(ClientConfig); 143 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 144 145 struct timespec ts = {}; 146 clock_gettime(CLOCK_REALTIME, &ts); 147 size_t metaSize = sizeof(ts); 148 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 149 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 150 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 151 } 152 ASSERT_FALSE(hookClient.SendNmdInfo()); 153 } 154 } // namespace 155