1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17 #include <netinet/in.h>
18 #include <securec.h>
19
20 #include "lnn_usb_network_impl_mock.h"
21 #include "lnn_log.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_error_code.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 void *g_ipNetworkImplInterface;
LnnUsbNetworkImplInterfaceMock()30 LnnUsbNetworkImplInterfaceMock::LnnUsbNetworkImplInterfaceMock()
31 {
32 g_ipNetworkImplInterface = reinterpret_cast<void *>(this);
33 }
34
~LnnUsbNetworkImplInterfaceMock()35 LnnUsbNetworkImplInterfaceMock::~LnnUsbNetworkImplInterfaceMock()
36 {
37 g_ipNetworkImplInterface = nullptr;
38 }
39
GetLnnUsbNetworkImplInterface()40 static LnnUsbNetworkImplInterface *GetLnnUsbNetworkImplInterface()
41 {
42 return reinterpret_cast<LnnUsbNetworkImplInterface *>(g_ipNetworkImplInterface);
43 }
44
ActionOfGetNetworkIpv6ByIfName(const char * ifName,char * ip,uint32_t len)45 int32_t LnnUsbNetworkImplInterfaceMock::ActionOfGetNetworkIpv6ByIfName(const char *ifName, char *ip, uint32_t len)
46 {
47 if (ifName == nullptr || ip == nullptr) {
48 LNN_LOGE(LNN_TEST, "ifName or ip buffer is NULL!");
49 return SOFTBUS_INVALID_PARAM;
50 }
51 if (len < INET6_ADDRSTRLEN) {
52 LNN_LOGE(LNN_TEST, "len value is not long enough !");
53 return SOFTBUS_INVALID_PARAM;
54 }
55 if (memcpy_s(ip, strlen("::2") + 1, "::2", strlen("::2") + 1) != EOK) {
56 LNN_LOGE(LNN_TEST, "memcpy networkId fail");
57 return SOFTBUS_MEM_ERR;
58 }
59 return SOFTBUS_OK;
60 }
61
ActionOfGetNetworkIpv6ByIfName2(const char * ifName,char * ip,uint32_t len)62 int32_t LnnUsbNetworkImplInterfaceMock::ActionOfGetNetworkIpv6ByIfName2(const char *ifName, char *ip, uint32_t len)
63 {
64 if (ifName == nullptr || ip == nullptr) {
65 LNN_LOGE(LNN_TEST, "ifName or ip buffer is NULL!");
66 return SOFTBUS_INVALID_PARAM;
67 }
68 if (len < INET6_ADDRSTRLEN) {
69 LNN_LOGE(LNN_TEST, "len value is not long enough !");
70 return SOFTBUS_INVALID_PARAM;
71 }
72 if (memcpy_s(ip, strlen("::3") + 1, "::3", strlen("::3") + 1) != EOK) {
73 LNN_LOGE(LNN_TEST, "memcpy networkId fail");
74 return SOFTBUS_MEM_ERR;
75 }
76 return SOFTBUS_OK;
77 }
78
ActionOfLnnGetLocalStrInfoByIfnameIdx(InfoKey key,char * info,uint32_t len,int32_t ifIdx)79 int32_t LnnUsbNetworkImplInterfaceMock::ActionOfLnnGetLocalStrInfoByIfnameIdx(
80 InfoKey key, char *info, uint32_t len, int32_t ifIdx)
81 {
82 (void)ifIdx;
83 if (info == nullptr) {
84 LNN_LOGE(LNN_TEST, "para error");
85 return SOFTBUS_INVALID_PARAM;
86 }
87 if (key == STRING_KEY_NET_IF_NAME) {
88 if (strcpy_s(info, len, "deviceName") != EOK) {
89 return SOFTBUS_STRCPY_ERR;
90 }
91 return SOFTBUS_OK;
92 }
93 if (key == STRING_KEY_IP) {
94 if (strcpy_s(info, len, "::2") != EOK) {
95 return SOFTBUS_STRCPY_ERR;
96 }
97 return SOFTBUS_OK;
98 }
99 if (key == STRING_KEY_IP6_WITH_IF) {
100 if (strcpy_s(info, len, "::2%ncm0") != EOK) {
101 return SOFTBUS_STRCPY_ERR;
102 }
103 return SOFTBUS_OK;
104 }
105 return SOFTBUS_INVALID_PARAM;
106 }
107
108 extern "C" {
LnnRegisterEventHandler(LnnEventType event,LnnEventHandler handler)109 int32_t LnnRegisterEventHandler(LnnEventType event, LnnEventHandler handler)
110 {
111 return GetLnnUsbNetworkImplInterface()->LnnRegisterEventHandler(event, handler);
112 }
113
LnnRegistPhysicalSubnet(LnnPhysicalSubnet * manager)114 int32_t LnnRegistPhysicalSubnet(LnnPhysicalSubnet *manager)
115 {
116 return GetLnnUsbNetworkImplInterface()->LnnRegistPhysicalSubnet(manager);
117 }
118
DiscLinkStatusChanged(LinkStatus status,ExchangeMedium medium,int32_t ifnameIdx)119 void DiscLinkStatusChanged(LinkStatus status, ExchangeMedium medium, int32_t ifnameIdx)
120 {
121 return GetLnnUsbNetworkImplInterface()->DiscLinkStatusChanged(status, medium, ifnameIdx);
122 }
123
LnnVisitPhysicalSubnet(LnnVisitPhysicalSubnetCallback callback,void * data)124 bool LnnVisitPhysicalSubnet(LnnVisitPhysicalSubnetCallback callback, void *data)
125 {
126 return GetLnnUsbNetworkImplInterface()->LnnVisitPhysicalSubnet(callback, data);
127 }
128
LnnIpAddrChangeEventHandler(void)129 void LnnIpAddrChangeEventHandler(void)
130 {
131 return GetLnnUsbNetworkImplInterface()->LnnIpAddrChangeEventHandler();
132 }
133
AuthStopListening(AuthLinkType type)134 void AuthStopListening(AuthLinkType type)
135 {
136 return GetLnnUsbNetworkImplInterface()->AuthStopListening(type);
137 }
138
TransTdcStopSessionListener(ListenerModule module)139 int32_t TransTdcStopSessionListener(ListenerModule module)
140 {
141 return GetLnnUsbNetworkImplInterface()->TransTdcStopSessionListener(module);
142 }
143
LnnGetAddrTypeByIfName(const char * ifName,ConnectionAddrType * type)144 int32_t LnnGetAddrTypeByIfName(const char *ifName, ConnectionAddrType *type)
145 {
146 return GetLnnUsbNetworkImplInterface()->LnnGetAddrTypeByIfName(ifName, type);
147 }
148
AuthStartListening(AuthLinkType type,const char * ip,int32_t port)149 int32_t AuthStartListening(AuthLinkType type, const char *ip, int32_t port)
150 {
151 return GetLnnUsbNetworkImplInterface()->AuthStartListening(type, ip, port);
152 }
153
TransTdcStartSessionListener(ListenerModule module,const LocalListenerInfo * info)154 int32_t TransTdcStartSessionListener(ListenerModule module, const LocalListenerInfo *info)
155 {
156 return GetLnnUsbNetworkImplInterface()->TransTdcStartSessionListener(module, info);
157 }
158
LnnNotifyPhysicalSubnetStatusChanged(const char * ifName,ProtocolType protocolType,void * status)159 void LnnNotifyPhysicalSubnetStatusChanged(const char *ifName, ProtocolType protocolType, void *status)
160 {
161 return GetLnnUsbNetworkImplInterface()->LnnNotifyPhysicalSubnetStatusChanged(ifName, protocolType, status);
162 }
163
LnnVisitNetif(VisitNetifCallback callback,void * data)164 bool LnnVisitNetif(VisitNetifCallback callback, void *data)
165 {
166 return GetLnnUsbNetworkImplInterface()->LnnVisitNetif(callback, data);
167 }
168
LnnRequestLeaveByAddrType(const bool * type,uint32_t typeLen)169 int32_t LnnRequestLeaveByAddrType(const bool *type, uint32_t typeLen)
170 {
171 return GetLnnUsbNetworkImplInterface()->LnnRequestLeaveByAddrType(type, typeLen);
172 }
173
GetNetworkIpv6ByIfName(const char * ifName,char * ip,uint32_t len)174 int32_t GetNetworkIpv6ByIfName(const char *ifName, char *ip, uint32_t len)
175 {
176 return GetLnnUsbNetworkImplInterface()->GetNetworkIpv6ByIfName(ifName, ip, len);
177 }
178
lnnRegistProtocol(LnnProtocolManager * protocolMgr)179 int32_t lnnRegistProtocol(LnnProtocolManager *protocolMgr)
180 {
181 return GetLnnUsbNetworkImplInterface()->LnnRegistProtocol(protocolMgr);
182 }
183
LnnGetLocalNumU32Info(InfoKey key,uint32_t * info)184 int32_t LnnGetLocalNumU32Info(InfoKey key, uint32_t *info)
185 {
186 return GetLnnUsbNetworkImplInterface()->LnnGetLocalNumU32Info(key, info);
187 }
188
LnnSetNetCapability(uint32_t * capability,NetCapability type)189 int32_t LnnSetNetCapability(uint32_t *capability, NetCapability type)
190 {
191 return GetLnnUsbNetworkImplInterface()->LnnSetNetCapability(capability, type);
192 }
193
LnnClearNetCapability(uint32_t * capability,NetCapability type)194 int32_t LnnClearNetCapability(uint32_t *capability, NetCapability type)
195 {
196 return GetLnnUsbNetworkImplInterface()->LnnClearNetCapability(capability, type);
197 }
198
LnnSetLocalNumInfo(InfoKey key,int32_t info)199 int32_t LnnSetLocalNumInfo(InfoKey key, int32_t info)
200 {
201 return GetLnnUsbNetworkImplInterface()->LnnSetLocalNumInfo(key, info);
202 }
203
LnnGetLocalStrInfoByIfnameIdx(InfoKey key,char * info,uint32_t len,int32_t ifIdx)204 int32_t LnnGetLocalStrInfoByIfnameIdx(InfoKey key, char *info, uint32_t len, int32_t ifIdx)
205 {
206 return GetLnnUsbNetworkImplInterface()->LnnGetLocalStrInfoByIfnameIdx(key, info, len, ifIdx);
207 }
208
LnnGetLocalNumInfoByIfnameIdx(InfoKey key,int32_t * info,int32_t ifIdx)209 int32_t LnnGetLocalNumInfoByIfnameIdx(InfoKey key, int32_t *info, int32_t ifIdx)
210 {
211 return GetLnnUsbNetworkImplInterface()->LnnGetLocalNumInfoByIfnameIdx(key, info, ifIdx);
212 }
213
LnnSetLocalStrInfoByIfnameIdx(InfoKey key,const char * info,int32_t ifIdx)214 int32_t LnnSetLocalStrInfoByIfnameIdx(InfoKey key, const char *info, int32_t ifIdx)
215 {
216 return GetLnnUsbNetworkImplInterface()->LnnSetLocalStrInfoByIfnameIdx(key, info, ifIdx);
217 }
218
LnnSetLocalNumInfoByIfnameIdx(InfoKey key,int32_t info,int32_t ifIdx)219 int32_t LnnSetLocalNumInfoByIfnameIdx(InfoKey key, int32_t info, int32_t ifIdx)
220 {
221 return GetLnnUsbNetworkImplInterface()->LnnSetLocalNumInfoByIfnameIdx(key, info, ifIdx);
222 }
223 }
224 } // namespace OHOS
225