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 <hwext/gtest-ext.h> 18 #include <hwext/gtest-tag.h> 19 20 #include "hook_socket_client.h" 21 #include "hook_service.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 HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1) 68 { 69 uint64_t config = FILTER_SIZE; 70 config <<= MOBILE_BIT; 71 config |= SMB_SIZE; 72 Sampling sampler; 73 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 74 SocketContext socketContext; 75 auto ptr = reinterpret_cast<const int8_t*>(&config); 76 auto size = sizeof(ClientConfig); 77 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 78 79 struct timespec ts = {}; 80 clock_gettime(CLOCK_REALTIME, &ts); 81 size_t metaSize = sizeof(ts); 82 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 83 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 84 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 85 } 86 metaSize = sizeof(ts); 87 hookClient.unixSocketClient_ = nullptr; 88 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 89 EXPECT_FALSE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 90 hookClient.unixSocketClient_ = std::make_shared<UnixSocketClient>(); 91 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 92 EXPECT_TRUE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 93 } 94 95 /* 96 * @tc.name: GetSmbFd 97 * @tc.desc: test HookSocketClient::GetSmbFd with normal case. 98 * @tc.type: FUNC 99 */ 100 HWTEST_F(HookSocketClientTest, GetSmbFd, TestSize.Level1) 101 { 102 ClientConfig clientConfig; 103 SocketContext socketContext; 104 Sampling sampler; 105 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 106 auto size = sizeof(clientConfig); 107 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 108 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 109 ASSERT_EQ(hookClient.GetSmbFd(), -1); 110 } 111 112 /* 113 * @tc.name: GetEventFd 114 * @tc.desc: test HookSocketClient::GetEventFd with normal case. 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(HookSocketClientTest, GetEventFd, TestSize.Level1) 118 { 119 ClientConfig clientConfig; 120 SocketContext socketContext; 121 Sampling sampler; 122 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 123 auto size = sizeof(clientConfig); 124 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 125 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 126 ASSERT_EQ(hookClient.GetEventFd(), -1); 127 } 128 129 /* 130 * @tc.name: SendNmdInfo 131 * @tc.desc: test HookSocketClient::SendNmdInfo with normal case. 132 * @tc.type: FUNC 133 */ 134 HWTEST_F(HookSocketClientTest, SendNmdInfo, TestSize.Level1) 135 { 136 uint64_t config = FILTER_SIZE; 137 config <<= MOBILE_BIT; 138 config |= SMB_SIZE; 139 Sampling sampler; 140 HookSocketClient hookClient(1, &g_ClientConfigTest, &sampler, nullptr); 141 SocketContext socketContext; 142 auto ptr = reinterpret_cast<const int8_t*>(&config); 143 auto size = sizeof(ClientConfig); 144 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 145 146 struct timespec ts = {}; 147 clock_gettime(CLOCK_REALTIME, &ts); 148 size_t metaSize = sizeof(ts); 149 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 150 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 151 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 152 } 153 ASSERT_EQ(hookClient.nmdFd_, -1); 154 ASSERT_FALSE(hookClient.SendNmdInfo()); 155 } 156 } // namespace 157