1 /* 2 * Copyright (c) 2021-2023 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 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 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 HookSocketClient hookClient(1, &g_ClientConfigTest); 54 SocketContext socketContext; 55 auto ptr = reinterpret_cast<const int8_t*>(&config); 56 auto size = sizeof(uint64_t); 57 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 58 } 59 60 /* 61 * @tc.name: SendStack 62 * @tc.desc: test HookSocketClient::SendStack with normal case. 63 * @tc.type: FUNC 64 */ 65 HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1) 66 { 67 ClientConfig config = {0}; 68 config.shareMemroySize = SMB_SIZE; 69 HookSocketClient hookClient(1, &g_ClientConfigTest); 70 SocketContext socketContext; 71 auto ptr = reinterpret_cast<const int8_t*>(&config); 72 auto size = sizeof(ClientConfig); 73 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 74 75 struct timespec ts = {}; 76 clock_gettime(CLOCK_REALTIME, &ts); 77 size_t metaSize = sizeof(ts); 78 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize); 79 if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) { 80 HILOG_ERROR(LOG_CORE, "memcpy_s ts failed"); 81 } 82 metaSize = sizeof(ts); 83 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 84 EXPECT_FALSE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 85 hookClient.unixSocketClient_ = std::make_shared<UnixSocketClient>(); 86 EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize)); 87 EXPECT_TRUE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize)); 88 } 89 90 /* 91 * @tc.name: GetSmbFd 92 * @tc.desc: test HookSocketClient::GetSmbFd with normal case. 93 * @tc.type: FUNC 94 */ 95 HWTEST_F(HookSocketClientTest, GetSmbFd, TestSize.Level1) 96 { 97 ClientConfig clientConfig; 98 SocketContext socketContext; 99 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 100 auto size = sizeof(clientConfig); 101 HookSocketClient hookClient(1, &g_ClientConfigTest); 102 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 103 ASSERT_EQ(hookClient.GetSmbFd(), -1); 104 } 105 106 /* 107 * @tc.name: GetEventFd 108 * @tc.desc: test HookSocketClient::GetEventFd with normal case. 109 * @tc.type: FUNC 110 */ 111 HWTEST_F(HookSocketClientTest, GetEventFd, TestSize.Level1) 112 { 113 ClientConfig clientConfig; 114 SocketContext socketContext; 115 auto ptr = reinterpret_cast<const int8_t*>(&clientConfig); 116 auto size = sizeof(clientConfig); 117 HookSocketClient hookClient(1, &g_ClientConfigTest); 118 ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size)); 119 ASSERT_EQ(hookClient.GetEventFd(), -1); 120 } 121 } // namespace 122