• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "logging.h"
25 
26 using namespace testing::ext;
27 
28 namespace {
29 constexpr int MOBILE_BIT = 32;
30 constexpr int32_t FILTER_SIZE = 100;
31 constexpr int32_t SMB_SIZE = 409600;
32 static ClientConfig g_ClientConfigTest = {0};
33 
34 class HookSocketClientTest : public ::testing::Test {
35 public:
SetUpTestCase()36     static void SetUpTestCase() {}
TearDownTestCase()37     static void TearDownTestCase() {}
SetUp()38     void SetUp() {}
TearDown()39     void TearDown() {}
40 };
41 
42 /*
43  * @tc.name: ProtocolProc
44  * @tc.desc: test HookSocketClient::ProtocolProc with normal case.
45  * @tc.type: FUNC
46  */
47 HWTEST_F(HookSocketClientTest, ProtocolProc, TestSize.Level1)
48 {
49     uint64_t config = FILTER_SIZE;
50     config <<= MOBILE_BIT;
51     config |= SMB_SIZE;
52     HookSocketClient hookClient(1, &g_ClientConfigTest);
53     SocketContext socketContext;
54     auto ptr = reinterpret_cast<const int8_t*>(&config);
55     auto size = sizeof(uint64_t);
56     ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
57 }
58 
59 /*
60  * @tc.name: SendStack
61  * @tc.desc: test HookSocketClient::SendStack with normal case.
62  * @tc.type: FUNC
63  */
64 HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1)
65 {
66     uint64_t config = FILTER_SIZE;
67     config <<= MOBILE_BIT;
68     config |= SMB_SIZE;
69     HookSocketClient hookClient(1, &g_ClientConfigTest);
70     SocketContext socketContext;
71     auto ptr = reinterpret_cast<const int8_t*>(&config);
72     auto size = sizeof(uint64_t);
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     ASSERT_TRUE(hookClient.SendStack(buffer.get(), metaSize));
84 }
85 
86 /*
87  * @tc.name: GetSmbFd
88  * @tc.desc: test HookSocketClient::GetSmbFd with normal case.
89  * @tc.type: FUNC
90  */
91 HWTEST_F(HookSocketClientTest, GetSmbFd, TestSize.Level1)
92 {
93     uint64_t config = FILTER_SIZE;
94     config <<= MOBILE_BIT;
95     config |= SMB_SIZE;
96     SocketContext socketContext;
97     auto ptr = reinterpret_cast<const int8_t*>(&config);
98     auto size = sizeof(uint64_t);
99     HookSocketClient hookClient(1, &g_ClientConfigTest);
100     ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
101     ASSERT_EQ(hookClient.GetSmbFd(), -1);
102 }
103 
104 /*
105  * @tc.name: GetEventFd
106  * @tc.desc: test HookSocketClient::GetEventFd with normal case.
107  * @tc.type: FUNC
108  */
109 HWTEST_F(HookSocketClientTest, GetEventFd, TestSize.Level1)
110 {
111     uint64_t config = FILTER_SIZE;
112     config <<= MOBILE_BIT;
113     config |= SMB_SIZE;
114     SocketContext socketContext;
115     auto ptr = reinterpret_cast<const int8_t*>(&config);
116     auto size = sizeof(uint64_t);
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