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