1 /*
2 * Copyright (c) 2022 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 <securec.h>
18
19 #include "lnn_ip_network_impl_mock.h"
20 #include "lnn_log.h"
21 #include "softbus_adapter_mem.h"
22 #include "softbus_errcode.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;
LnnIpNetworkImplInterfaceMock()30 LnnIpNetworkImplInterfaceMock::LnnIpNetworkImplInterfaceMock()
31 {
32 g_ipNetworkImplInterface = reinterpret_cast<void *>(this);
33 }
34
~LnnIpNetworkImplInterfaceMock()35 LnnIpNetworkImplInterfaceMock::~LnnIpNetworkImplInterfaceMock()
36 {
37 g_ipNetworkImplInterface = nullptr;
38 }
39
GetLnnIpNetworkImplInterface()40 static LnnIpNetworkImplInterface *GetLnnIpNetworkImplInterface()
41 {
42 return reinterpret_cast<LnnIpNetworkImplInterface *>(g_ipNetworkImplInterface);
43 }
44
ActionOfGetNetworkIpByIfName(const char * ifName,char * ip,char * netmask,uint32_t len)45 int32_t LnnIpNetworkImplInterfaceMock::ActionOfGetNetworkIpByIfName(
46 const char *ifName, char *ip, char *netmask, uint32_t len)
47 {
48 if (ifName == nullptr || netmask == nullptr || len == 0) {
49 LNN_LOGI(LNN_TEST, "invalid para");
50 return SOFTBUS_ERR;
51 }
52 if (memcpy_s(ip, strlen("127.0.0.2") + 1, "127.0.0.2", strlen("127.0.0.2") + 1) != EOK) {
53 LNN_LOGI(LNN_TEST, "memcpy networkId fail");
54 return SOFTBUS_ERR;
55 }
56 return SOFTBUS_OK;
57 }
58
59 extern "C" {
LnnRegisterEventHandler(LnnEventType event,LnnEventHandler handler)60 int32_t LnnRegisterEventHandler(LnnEventType event, LnnEventHandler handler)
61 {
62 return GetLnnIpNetworkImplInterface()->LnnRegisterEventHandler(event, handler);
63 }
64
LnnRegistPhysicalSubnet(LnnPhysicalSubnet * manager)65 int32_t LnnRegistPhysicalSubnet(LnnPhysicalSubnet *manager)
66 {
67 return GetLnnIpNetworkImplInterface()->LnnRegistPhysicalSubnet(manager);
68 }
69
DiscLinkStatusChanged(LinkStatus status,ExchangeMedium medium)70 void DiscLinkStatusChanged(LinkStatus status, ExchangeMedium medium)
71 {
72 return GetLnnIpNetworkImplInterface()->DiscLinkStatusChanged(status, medium);
73 }
74
LnnVisitPhysicalSubnet(LnnVisitPhysicalSubnetCallback callback,void * data)75 bool LnnVisitPhysicalSubnet(LnnVisitPhysicalSubnetCallback callback, void *data)
76 {
77 return GetLnnIpNetworkImplInterface()->LnnVisitPhysicalSubnet(callback, data);
78 }
79
LnnStopPublish(void)80 void LnnStopPublish(void)
81 {
82 return GetLnnIpNetworkImplInterface()->LnnStopPublish();
83 }
84
LnnStopDiscovery(void)85 void LnnStopDiscovery(void)
86 {
87 return GetLnnIpNetworkImplInterface()->LnnStopDiscovery();
88 }
89
LnnIpAddrChangeEventHandler(void)90 void LnnIpAddrChangeEventHandler(void)
91 {
92 return GetLnnIpNetworkImplInterface()->LnnIpAddrChangeEventHandler();
93 }
94
AuthStopListening(AuthLinkType type)95 void AuthStopListening(AuthLinkType type)
96 {
97 return GetLnnIpNetworkImplInterface()->AuthStopListening(type);
98 }
99
TransTdcStopSessionListener(ListenerModule module)100 int32_t TransTdcStopSessionListener(ListenerModule module)
101 {
102 return GetLnnIpNetworkImplInterface()->TransTdcStopSessionListener(module);
103 }
104
ConnStopLocalListening(const LocalListenerInfo * info)105 int32_t ConnStopLocalListening(const LocalListenerInfo *info)
106 {
107 return GetLnnIpNetworkImplInterface()->ConnStopLocalListening(info);
108 }
109
LnnGetAddrTypeByIfName(const char * ifName,ConnectionAddrType * type)110 int32_t LnnGetAddrTypeByIfName(const char *ifName, ConnectionAddrType *type)
111 {
112 return GetLnnIpNetworkImplInterface()->LnnGetAddrTypeByIfName(ifName, type);
113 }
114
LnnStartPublish(void)115 int32_t LnnStartPublish(void)
116 {
117 return GetLnnIpNetworkImplInterface()->LnnStartPublish();
118 }
119
LnnIsAutoNetWorkingEnabled(void)120 bool LnnIsAutoNetWorkingEnabled(void)
121 {
122 return GetLnnIpNetworkImplInterface()->LnnIsAutoNetWorkingEnabled();
123 }
124
LnnStartDiscovery(void)125 int32_t LnnStartDiscovery(void)
126 {
127 return GetLnnIpNetworkImplInterface()->LnnStartDiscovery();
128 }
129
AuthStartListening(AuthLinkType type,const char * ip,int32_t port)130 int32_t AuthStartListening(AuthLinkType type, const char *ip, int32_t port)
131 {
132 return GetLnnIpNetworkImplInterface()->AuthStartListening(type, ip, port);
133 }
134
TransTdcStartSessionListener(ListenerModule module,const LocalListenerInfo * info)135 int32_t TransTdcStartSessionListener(ListenerModule module, const LocalListenerInfo *info)
136 {
137 return GetLnnIpNetworkImplInterface()->TransTdcStartSessionListener(module, info);
138 }
139
ConnStartLocalListening(const LocalListenerInfo * info)140 int32_t ConnStartLocalListening(const LocalListenerInfo *info)
141 {
142 return GetLnnIpNetworkImplInterface()->ConnStartLocalListening(info);
143 }
144
LnnIsLinkReady(const char * iface)145 bool LnnIsLinkReady(const char *iface)
146 {
147 return GetLnnIpNetworkImplInterface()->LnnIsLinkReady(iface);
148 }
149
LnnNotifyPhysicalSubnetStatusChanged(const char * ifName,ProtocolType protocolType,void * status)150 void LnnNotifyPhysicalSubnetStatusChanged(const char *ifName, ProtocolType protocolType, void *status)
151 {
152 return GetLnnIpNetworkImplInterface()->LnnNotifyPhysicalSubnetStatusChanged(ifName, protocolType, status);
153 }
154
LnnVisitNetif(VisitNetifCallback callback,void * data)155 bool LnnVisitNetif(VisitNetifCallback callback, void *data)
156 {
157 return GetLnnIpNetworkImplInterface()->LnnVisitNetif(callback, data);
158 }
159
LnnRequestLeaveByAddrType(const bool * type,uint32_t typeLen)160 int32_t LnnRequestLeaveByAddrType(const bool *type, uint32_t typeLen)
161 {
162 return GetLnnIpNetworkImplInterface()->LnnRequestLeaveByAddrType(type, typeLen);
163 }
164
GetNetworkIpByIfName(const char * ifName,char * ip,char * netmask,uint32_t len)165 int32_t GetNetworkIpByIfName(const char *ifName, char *ip, char *netmask, uint32_t len)
166 {
167 return GetLnnIpNetworkImplInterface()->GetNetworkIpByIfName(ifName, ip, netmask, len);
168 }
169 }
170 }