• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 } // namespace
128