• 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 "plugin_service.ipc.h"
23 #include "service_entry.h"
24 #include "socket_context.h"
25 
26 using namespace testing::ext;
27 
28 namespace {
29 constexpr int SLEEP_TIME = 30000;
30 constexpr int MOBILE_BIT = 32;
31 constexpr int32_t FILTER_SIZE = 100;
32 constexpr int32_t SMB_SIZE = 409600;
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: Connect
44  * @tc.desc: test HookSocketClient::Connect with normal case.
45  * @tc.type: FUNC
46  */
47 HWTEST_F(HookSocketClientTest, Connect, TestSize.Level1)
48 {
49     HookSocketClient hookClient(1);
50     ASSERT_FALSE(hookClient.Connect("test"));
51 
52     ServiceEntry serviceEntry;
53     IPluginServiceServer pluginService;
54     ASSERT_TRUE(serviceEntry.StartServer("test_unix_socket_service_entry"));
55     ASSERT_TRUE(serviceEntry.RegisterService(pluginService));
56     serviceEntry.FindServiceByName(pluginService.serviceName_);
57 
58     usleep(SLEEP_TIME);
59 
60     ASSERT_TRUE(hookClient.Connect("test_unix_socket_service_entry"));
61 }
62 
63 /*
64  * @tc.name: ProtocolProc
65  * @tc.desc: test HookSocketClient::ProtocolProc with normal case.
66  * @tc.type: FUNC
67  */
68 HWTEST_F(HookSocketClientTest, ProtocolProc, TestSize.Level1)
69 {
70     uint64_t config = FILTER_SIZE;
71     config <<= MOBILE_BIT;
72     config |= SMB_SIZE;
73     HookSocketClient hookClient(1);
74     SocketContext socketContext;
75     auto ptr = reinterpret_cast<const int8_t*>(&config);
76     auto size = sizeof(uint64_t);
77     ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
78 }
79 
80 /*
81  * @tc.name: SendStack
82  * @tc.desc: test HookSocketClient::SendStack with normal case.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1)
86 {
87     uint64_t config = FILTER_SIZE;
88     config <<= MOBILE_BIT;
89     config |= SMB_SIZE;
90     HookSocketClient hookClient(1);
91     SocketContext socketContext;
92     auto ptr = reinterpret_cast<const int8_t*>(&config);
93     auto size = sizeof(uint64_t);
94     ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
95 
96     struct timespec ts = {};
97     clock_gettime(CLOCK_REALTIME, &ts);
98     size_t metaSize = sizeof(ts);
99     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(metaSize);
100     if (memcpy_s(buffer.get(), metaSize, &ts, sizeof(ts)) != EOK) {
101         HILOG_ERROR(LOG_CORE, "memcpy_s ts failed");
102     }
103     metaSize = sizeof(ts);
104     ASSERT_TRUE(hookClient.SendStack(buffer.get(), metaSize));
105 }
106 
107 /*
108  * @tc.name: GetSmbFd
109  * @tc.desc: test HookSocketClient::GetSmbFd with normal case.
110  * @tc.type: FUNC
111  */
112 HWTEST_F(HookSocketClientTest, GetSmbFd, TestSize.Level1)
113 {
114     uint64_t config = FILTER_SIZE;
115     config <<= MOBILE_BIT;
116     config |= SMB_SIZE;
117     SocketContext socketContext;
118     auto ptr = reinterpret_cast<const int8_t*>(&config);
119     auto size = sizeof(uint64_t);
120     HookSocketClient hookClient(1);
121     ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
122     ASSERT_EQ(hookClient.GetSmbFd(), -1);
123 }
124 
125 /*
126  * @tc.name: GetEventFd
127  * @tc.desc: test HookSocketClient::GetEventFd with normal case.
128  * @tc.type: FUNC
129  */
130 HWTEST_F(HookSocketClientTest, GetEventFd, TestSize.Level1)
131 {
132     uint64_t config = FILTER_SIZE;
133     config <<= MOBILE_BIT;
134     config |= SMB_SIZE;
135     SocketContext socketContext;
136     auto ptr = reinterpret_cast<const int8_t*>(&config);
137     auto size = sizeof(uint64_t);
138     HookSocketClient hookClient(1);
139     ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
140     ASSERT_EQ(hookClient.GetEventFd(), -1);
141 }
142 } // namespace
143