• 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 "auth_net_ledger_mock.h"
17 #include "auth_connection.h"
18 #include "auth_manager.h"
19 #include "softbus_adapter_mem.h"
20 #include "string"
21 
22 static const std::string CMD_TAG = "TECmd";
23 static const std::string CMD_GET_AUTH_INFO = "getAuthInfo";
24 static const std::string CMD_RET_AUTH_INFO = "retAuthInfo";
25 static const std::string DATA_TAG = "TEData";
26 static const std::string DEVICE_ID_TAG = "TEDeviceId";
27 static const std::string DATA_BUF_SIZE_TAG = "DataBufSize";
28 static const std::string SOFT_BUS_VERSION_TAG = "softbusVersion";
29 static const int PACKET_SIZE = (64 * 1024);
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace OHOS {
35 void *g_netLedgerinterface;
AuthNetLedgertInterfaceMock()36 AuthNetLedgertInterfaceMock::AuthNetLedgertInterfaceMock()
37 {
38     g_netLedgerinterface = reinterpret_cast<void *>(this);
39 }
40 
~AuthNetLedgertInterfaceMock()41 AuthNetLedgertInterfaceMock::~AuthNetLedgertInterfaceMock()
42 {
43     g_netLedgerinterface = nullptr;
44 }
45 
GetNetLedgerInterface()46 static AuthNetLedgerInterface *GetNetLedgerInterface()
47 {
48     return reinterpret_cast<AuthNetLedgertInterfaceMock *>(g_netLedgerinterface);
49 }
50 
51 extern "C" {
LnnGetLocalStrInfo(InfoKey key,char * info,uint32_t len)52 int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len)
53 {
54     return GetNetLedgerInterface()->LnnGetLocalStrInfo(key, info, len);
55 }
56 
LnnDeleteSpecificTrustedDevInfo(const char * udid)57 int32_t LnnDeleteSpecificTrustedDevInfo(const char *udid)
58 {
59     return GetNetLedgerInterface()->LnnDeleteSpecificTrustedDevInfo(udid);
60 }
61 
LnnGetLocalNodeInfo(void)62 const NodeInfo *LnnGetLocalNodeInfo(void)
63 {
64     return GetNetLedgerInterface()->LnnGetLocalNodeInfo();
65 }
66 
LnnGetAuthPort(const NodeInfo * info)67 int32_t LnnGetAuthPort(const NodeInfo *info)
68 {
69     return GetNetLedgerInterface()->LnnGetAuthPort(info);
70 }
71 
LnnGetSessionPort(const NodeInfo * info)72 int32_t LnnGetSessionPort(const NodeInfo *info)
73 {
74     return GetNetLedgerInterface()->LnnGetSessionPort(info);
75 }
76 
LnnGetProxyPort(const NodeInfo * info)77 int32_t LnnGetProxyPort(const NodeInfo *info)
78 {
79     return GetNetLedgerInterface()->LnnGetProxyPort(info);
80 }
81 
LnnGetBtMac(const NodeInfo * info)82 const char *LnnGetBtMac(const NodeInfo *info)
83 {
84     return GetNetLedgerInterface()->LnnGetBtMac(info);
85 }
86 
LnnGetDeviceName(const DeviceBasicInfo * info)87 const char *LnnGetDeviceName(const DeviceBasicInfo *info)
88 {
89     return GetNetLedgerInterface()->LnnGetDeviceName(info);
90 }
91 
LnnConvertIdToDeviceType(uint16_t typeId)92 char *LnnConvertIdToDeviceType(uint16_t typeId)
93 {
94     return GetNetLedgerInterface()->LnnConvertIdToDeviceType(typeId);
95 }
96 
LnnGetDeviceUdid(const NodeInfo * info)97 const char *LnnGetDeviceUdid(const NodeInfo *info)
98 {
99     return GetNetLedgerInterface()->LnnGetDeviceUdid(info);
100 }
101 
LnnGetP2pRole(const NodeInfo * info)102 int32_t LnnGetP2pRole(const NodeInfo *info)
103 {
104     return GetNetLedgerInterface()->LnnGetP2pRole(info);
105 }
106 
LnnGetP2pMac(const NodeInfo * info)107 const char *LnnGetP2pMac(const NodeInfo *info)
108 {
109     return GetNetLedgerInterface()->LnnGetP2pMac(info);
110 }
111 
LnnGetSupportedProtocols(const NodeInfo * info)112 uint64_t LnnGetSupportedProtocols(const NodeInfo *info)
113 {
114     return GetNetLedgerInterface()->LnnGetSupportedProtocols(info);
115 }
116 
LnnConvertDeviceTypeToId(const char * deviceType,uint16_t * typeId)117 int32_t LnnConvertDeviceTypeToId(const char *deviceType, uint16_t *typeId)
118 {
119     return GetNetLedgerInterface()->LnnConvertDeviceTypeToId(deviceType, typeId);
120 }
121 
LnnGetLocalNumInfo(InfoKey key,int32_t * info)122 int32_t LnnGetLocalNumInfo(InfoKey key, int32_t *info)
123 {
124     return GetNetLedgerInterface()->LnnGetLocalNumInfo(key, info);
125 }
126 
LnnGetNodeInfoById(const char * id,IdCategory type)127 NodeInfo *LnnGetNodeInfoById(const char *id, IdCategory type)
128 {
129     return GetNetLedgerInterface()->LnnGetNodeInfoById(id, type);
130 }
131 
LnnHasDiscoveryType(const NodeInfo * info,DiscoveryType type)132 bool LnnHasDiscoveryType(const NodeInfo *info, DiscoveryType type)
133 {
134     return GetNetLedgerInterface()->LnnHasDiscoveryType(info, type);
135 }
136 
LnnGetNetworkIdByUdid(const char * udid,char * buf,uint32_t len)137 int32_t LnnGetNetworkIdByUdid(const char *udid, char *buf, uint32_t len)
138 {
139     return GetNetLedgerInterface()->LnnGetNetworkIdByUdid(udid, buf, len);
140 }
141 
LnnGetRemoteNumInfo(const char * netWorkId,InfoKey key,int32_t * info)142 int32_t LnnGetRemoteNumInfo(const char *netWorkId, InfoKey key, int32_t *info)
143 {
144     return GetNetLedgerInterface()->LnnGetRemoteNumInfo(netWorkId, key, info);
145 }
146 
LnnSetSupportDiscoveryType(char * info,const char * type)147 int32_t LnnSetSupportDiscoveryType(char *info, const char *type)
148 {
149     return GetNetLedgerInterface()->LnnSetSupportDiscoveryType(info, type);
150 }
151 
LnnHasSupportDiscoveryType(const char * destType,const char * type)152 bool LnnHasSupportDiscoveryType(const char *destType, const char *type)
153 {
154     return GetNetLedgerInterface()->LnnHasSupportDiscoveryType(destType, type);
155 }
156 
LnnPeerHasExchangeDiscoveryType(const NodeInfo * info,DiscoveryType type)157 bool LnnPeerHasExchangeDiscoveryType(const NodeInfo *info, DiscoveryType type)
158 {
159     return GetNetLedgerInterface()->LnnPeerHasExchangeDiscoveryType(info, type);
160 }
161 
RouteBuildClientAuthManager(int32_t cfd)162 void RouteBuildClientAuthManager(int32_t cfd)
163 {
164     return GetNetLedgerInterface()->RouteBuildClientAuthManager(cfd);
165 }
166 
RouteClearAuthChannelId(int32_t cfd)167 void RouteClearAuthChannelId(int32_t cfd)
168 {
169     return GetNetLedgerInterface()->RouteClearAuthChannelId(cfd);
170 }
171 }
172 
Pack(int64_t authSeq,const AuthSessionInfo * info,AuthDataHead & head)173 char *AuthNetLedgertInterfaceMock::Pack(int64_t authSeq, const AuthSessionInfo *info, AuthDataHead &head)
174 {
175     cJSON *obj = cJSON_CreateObject();
176     if (obj == nullptr) {
177         return nullptr;
178     }
179     char uuid[UUID_BUF_LEN] = "33654";
180     char udid[UDID_BUF_LEN] = "15464";
181     if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && !info->isServer) {
182         if (!AddStringToJsonObject(obj, CMD_TAG.c_str(), CMD_GET_AUTH_INFO.c_str())) {
183             cJSON_Delete(obj);
184             return nullptr;
185         }
186     } else {
187         if (!AddStringToJsonObject(obj, CMD_TAG.c_str(), CMD_RET_AUTH_INFO.c_str())) {
188             cJSON_Delete(obj);
189             return nullptr;
190         }
191     }
192     if (!AddStringToJsonObject(obj, DATA_TAG.c_str(), uuid) ||
193         !AddStringToJsonObject(obj, DEVICE_ID_TAG.c_str(), udid) ||
194         !AddNumberToJsonObject(obj, DATA_BUF_SIZE_TAG.c_str(), PACKET_SIZE) ||
195         !AddNumberToJsonObject(obj, SOFT_BUS_VERSION_TAG.c_str(), SOFTBUS_NEW_V1)) {
196         SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "add msg body fail.");
197         cJSON_Delete(obj);
198         return nullptr;
199     }
200     char *msg = cJSON_PrintUnformatted(obj);
201     if (msg == nullptr) {
202         cJSON_Delete(obj);
203         return nullptr;
204     }
205     cJSON_Delete(obj);
206     head.len = static_cast<uint32_t>(strlen(msg) + 1);
207     uint32_t size = GetAuthDataSize(head.len);
208     uint8_t *buf = reinterpret_cast<uint8_t *>(SoftBusMalloc(size));
209     if (buf == nullptr) {
210         cJSON_free(msg);
211         return nullptr;
212     }
213     int32_t ret = PackAuthData(&head, reinterpret_cast<uint8_t *>(msg), buf, size);
214     if (ret == SOFTBUS_OK) {
215         cJSON_free(msg);
216         ALOGI("PackAuthData success.");
217         return reinterpret_cast<char *>(buf);
218     }
219     SoftBusFree(buf);
220     cJSON_free(msg);
221     return nullptr;
222 }
223 
OnDeviceVerifyPass(int64_t authId,const NodeInfo * info)224 void AuthNetLedgertInterfaceMock::OnDeviceVerifyPass(int64_t authId, const NodeInfo *info)
225 {
226     ALOGI("Device verify passed & send cond");
227     (void)authId;
228     (void)info;
229     if (SoftBusMutexLock(&LnnHichainInterfaceMock::mutex) != SOFTBUS_OK) {
230         ALOGE("Device verify Lock failed");
231         return;
232     }
233     isRuned = true;
234     SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
235     SoftBusMutexUnlock(&LnnHichainInterfaceMock::mutex);
236 }
237 
OnDeviceNotTrusted(const char * peerUdid)238 void AuthNetLedgertInterfaceMock::OnDeviceNotTrusted(const char *peerUdid)
239 {
240     ALOGI("Device not trusted call back & send cond");
241     (void)peerUdid;
242     if (SoftBusMutexLock(&LnnHichainInterfaceMock::mutex) != SOFTBUS_OK) {
243         ALOGE("Device not trusted Lock failed");
244         return;
245     }
246     isRuned = true;
247     SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
248     SoftBusMutexUnlock(&LnnHichainInterfaceMock::mutex);
249 }
250 
OnDeviceDisconnect(int64_t authId)251 void AuthNetLedgertInterfaceMock::OnDeviceDisconnect(int64_t authId)
252 {
253     ALOGI("Device disconnect call back & send cond");
254     (void)authId;
255     if (SoftBusMutexLock(&LnnHichainInterfaceMock::mutex) != SOFTBUS_OK) {
256         ALOGE("Device disconnect Lock failed");
257         return;
258     }
259     isRuned = true;
260     SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
261     SoftBusMutexUnlock(&LnnHichainInterfaceMock::mutex);
262 }
263 } // namespace OHOS