• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "lnn_wifi_adpter_mock.h"
17 
18 #include <thread>
19 #include <securec.h>
20 
21 #include "lnn_lane_link.h"
22 #include "softbus_error_code.h"
23 #include "wifi_direct_error_code.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 constexpr uint32_t SLEEP_FOR_LOOP_COMPLETION_MS = 50;
29 
30 namespace OHOS {
31 void *g_wifiAdpterInterface;
LnnWifiAdpterInterfaceMock()32 LnnWifiAdpterInterfaceMock::LnnWifiAdpterInterfaceMock()
33 {
34     g_wifiAdpterInterface = reinterpret_cast<void *>(this);
35 }
36 
~LnnWifiAdpterInterfaceMock()37 LnnWifiAdpterInterfaceMock::~LnnWifiAdpterInterfaceMock()
38 {
39     g_wifiAdpterInterface = nullptr;
40 }
41 
GetWifiAdpterInterface()42 static LnnWifiAdpterInterface *GetWifiAdpterInterface()
43 {
44     return reinterpret_cast<LnnWifiAdpterInterface *>(g_wifiAdpterInterface);
45 }
46 
SetDefaultResult()47 void LnnWifiAdpterInterfaceMock::SetDefaultResult()
48 {
49     EXPECT_CALL(*this, SoftBusGetLinkBand).WillRepeatedly(Return(BAND_UNKNOWN));
50 }
51 
52 bool LnnWifiAdpterInterfaceMock::delayNotifyLinkSuccess = false;
ActionOfLnnConnectP2p(const LinkRequest * request,uint32_t laneLinkReqId,const LaneLinkCb * callback)53 int32_t LnnWifiAdpterInterfaceMock::ActionOfLnnConnectP2p(const LinkRequest *request, uint32_t laneLinkReqId,
54     const LaneLinkCb *callback)
55 {
56     GTEST_LOG_(INFO) << "ActionOfLnnConnectP2p enter";
57     if (request == nullptr || callback == nullptr) {
58         GTEST_LOG_(ERROR) << "invalid param";
59         return SOFTBUS_INVALID_PARAM;
60     }
61     LaneLinkInfo linkInfo;
62     if (memset_s(&linkInfo, sizeof(LaneLinkInfo), 0, sizeof(LaneLinkInfo)) != EOK) {
63         return SOFTBUS_MEM_ERR;
64     }
65     linkInfo.type = request->linkType;
66     char peerIp[] = "127.1.1.1";
67     char peerUdid[] = "222222222222222222";
68     if (strncpy_s(linkInfo.linkInfo.p2p.connInfo.peerIp, IP_LEN, peerIp, strlen(peerIp)) != EOK) {
69         return SOFTBUS_STRCPY_ERR;
70     }
71     if (strncpy_s(linkInfo.peerUdid, UDID_BUF_LEN, peerUdid, strlen(peerUdid)) != EOK) {
72         return SOFTBUS_STRCPY_ERR;
73     }
74     if (delayNotifyLinkSuccess) {
75         GTEST_LOG_(INFO) << "delay notify laneLinkSuccess after 50ms";
76         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_FOR_LOOP_COMPLETION_MS));
77     }
78     callback->onLaneLinkSuccess(laneLinkReqId, request->linkType, &linkInfo);
79     return SOFTBUS_OK;
80 }
81 
ActionOfOnConnectP2pFail(const LinkRequest * request,uint32_t laneLinkReqId,const LaneLinkCb * callback)82 int32_t LnnWifiAdpterInterfaceMock::ActionOfOnConnectP2pFail(const LinkRequest *request, uint32_t laneLinkReqId,
83     const LaneLinkCb *callback)
84 {
85     GTEST_LOG_(INFO) << "ActionOfOnConnectP2pFail enter";
86     callback->onLaneLinkFail(laneLinkReqId, ERROR_WIFI_OFF, request->linkType);
87     return SOFTBUS_OK;
88 }
89 
90 extern "C" {
SoftBusGetLinkedInfo(SoftBusWifiLinkedInfo * info)91 int32_t SoftBusGetLinkedInfo(SoftBusWifiLinkedInfo *info)
92 {
93     return GetWifiAdpterInterface()->SoftBusGetLinkedInfo(info);
94 }
95 
SoftBusGetLinkBand(void)96 SoftBusBand SoftBusGetLinkBand(void)
97 {
98     return GetWifiAdpterInterface()->SoftBusGetLinkBand();
99 }
100 
LnnDisconnectP2p(const char * networkId,uint32_t laneReqId)101 int32_t LnnDisconnectP2p(const char *networkId, uint32_t laneReqId)
102 {
103     return GetWifiAdpterInterface()->LnnDisconnectP2p(networkId, laneReqId);
104 }
105 
LnnDestroyP2p(void)106 void LnnDestroyP2p(void)
107 {
108     GetWifiAdpterInterface()->LnnDestroyP2p();
109 }
110 
LnnConnectP2p(const LinkRequest * request,uint32_t laneReqId,const LaneLinkCb * callback)111 int32_t LnnConnectP2p(const LinkRequest *request, uint32_t laneReqId, const LaneLinkCb *callback)
112 {
113     return GetWifiAdpterInterface()->LnnConnectP2p(request, laneReqId, callback);
114 }
115 
UpdateP2pLinkedInfo(uint32_t laneReqId,uint64_t laneId)116 int32_t UpdateP2pLinkedInfo(uint32_t laneReqId, uint64_t laneId)
117 {
118     return GetWifiAdpterInterface()->UpdateP2pLinkedInfo(laneReqId, laneId);
119 }
120 
LnnCancelWifiDirect(uint32_t laneReqId)121 void LnnCancelWifiDirect(uint32_t laneReqId)
122 {
123     return GetWifiAdpterInterface()->LnnCancelWifiDirect(laneReqId);
124 }
125 
LnnDisconnectP2pWithoutLnn(uint32_t laneReqId)126 void LnnDisconnectP2pWithoutLnn(uint32_t laneReqId)
127 {
128     return GetWifiAdpterInterface()->LnnDisconnectP2pWithoutLnn(laneReqId);
129 }
130 
SoftBusGetWifiState(void)131 SoftBusWifiDetailState SoftBusGetWifiState(void)
132 {
133     return GetWifiAdpterInterface()->SoftBusGetWifiState();
134 }
135 
SoftBusRegWlanChannelInfoCb(WlanChannelInfoCb * cb)136 int32_t SoftBusRegWlanChannelInfoCb(WlanChannelInfoCb *cb)
137 {
138     return GetWifiAdpterInterface()->SoftBusRegWlanChannelInfoCb(cb);
139 }
140 
SoftBusRegisterWifiEvent(ISoftBusScanResult * cb)141 int32_t SoftBusRegisterWifiEvent(ISoftBusScanResult *cb)
142 {
143     return GetWifiAdpterInterface()->SoftBusRegisterWifiEvent(cb);
144 }
145 
SoftBusUnRegisterWifiEvent(ISoftBusScanResult * cb)146 int32_t SoftBusUnRegisterWifiEvent(ISoftBusScanResult *cb)
147 {
148     return GetWifiAdpterInterface()->SoftBusUnRegisterWifiEvent(cb);
149 }
150 
SoftBusRequestWlanChannelInfo(int32_t * channelId,uint32_t num)151 int32_t SoftBusRequestWlanChannelInfo(int32_t *channelId, uint32_t num)
152 {
153     return GetWifiAdpterInterface()->SoftBusRequestWlanChannelInfo(channelId, num);
154 }
155 
SoftBusGetChannelListFor5G(int32_t * channelList,int32_t num)156 int32_t SoftBusGetChannelListFor5G(int32_t *channelList, int32_t num)
157 {
158     return GetWifiAdpterInterface()->SoftBusGetChannelListFor5G(channelList, num);
159 }
160 
SoftBusIsWifiActive(void)161 bool SoftBusIsWifiActive(void)
162 {
163     return GetWifiAdpterInterface()->SoftBusIsWifiActive();
164 }
165 
RemoveAuthSessionServer(const char * peerIp)166 int32_t RemoveAuthSessionServer(const char *peerIp)
167 {
168     return GetWifiAdpterInterface()->RemoveAuthSessionServer(peerIp);
169 }
170 }
171 }