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 int 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)58 int32_t LnnDeleteSpecificTrustedDevInfo(const char *udid)
59 {
60 return GetNetLedgerInterface()->LnnDeleteSpecificTrustedDevInfo(udid);
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,
174 uint32_t targetLen)
175 {
176 return GetNetLedgerInterface()->GetJsonObjectStringItem(json, string, target, targetLen);
177 }
178
LnnGetRemoteNodeInfoById(const char * id,IdCategory type,NodeInfo * info)179 int32_t LnnGetRemoteNodeInfoById(const char *id, IdCategory type, NodeInfo *info)
180 {
181 return GetNetLedgerInterface()->LnnGetRemoteNodeInfoById(id, type, info);
182 }
183
LnnSetDlPtk(const char * networkId,const char * remotePtk)184 bool LnnSetDlPtk(const char *networkId, const char *remotePtk)
185 {
186 return GetNetLedgerInterface()->LnnSetDlPtk(networkId, remotePtk);
187 }
188
LnnDumpRemotePtk(const char * oldPtk,const char * newPtk,const char * log)189 void LnnDumpRemotePtk(const char *oldPtk, const char *newPtk, const char *log)
190 {
191 return GetNetLedgerInterface()->LnnDumpRemotePtk(oldPtk, newPtk, log);
192 }
193
LnnGetOnlineStateById(const char * id,IdCategory type)194 bool LnnGetOnlineStateById(const char *id, IdCategory type)
195 {
196 return GetNetLedgerInterface()->LnnGetOnlineStateById(id, type);
197 }
198
LnnGetLocalNodeInfoSafe(NodeInfo * info)199 int32_t LnnGetLocalNodeInfoSafe(NodeInfo *info)
200 {
201 return GetNetLedgerInterface()->LnnGetLocalNodeInfoSafe(info);
202 }
203 }
204
205
Pack(int64_t authSeq,const AuthSessionInfo * info,AuthDataHead & head)206 char *AuthNetLedgertInterfaceMock::Pack(int64_t authSeq, const AuthSessionInfo *info, AuthDataHead &head)
207 {
208 cJSON *obj = cJSON_CreateObject();
209 if (obj == nullptr) {
210 return nullptr;
211 }
212 char uuid[UUID_BUF_LEN] = "33654";
213 char udid[UDID_BUF_LEN] = "15464";
214 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && !info->isServer) {
215 if (!AddStringToJsonObject(obj, CMD_TAG.c_str(), CMD_GET_AUTH_INFO.c_str())) {
216 cJSON_Delete(obj);
217 return nullptr;
218 }
219 } else {
220 if (!AddStringToJsonObject(obj, CMD_TAG.c_str(), CMD_RET_AUTH_INFO.c_str())) {
221 cJSON_Delete(obj);
222 return nullptr;
223 }
224 }
225 if (!AddStringToJsonObject(obj, DATA_TAG.c_str(), uuid) ||
226 !AddStringToJsonObject(obj, DEVICE_ID_TAG.c_str(), udid) ||
227 !AddNumberToJsonObject(obj, DATA_BUF_SIZE_TAG.c_str(), PACKET_SIZE) ||
228 !AddNumberToJsonObject(obj, SOFT_BUS_VERSION_TAG.c_str(), SOFTBUS_NEW_V1)) {
229 AUTH_LOGE(AUTH_TEST, "add msg body fail.");
230 cJSON_Delete(obj);
231 return nullptr;
232 }
233 char *msg = cJSON_PrintUnformatted(obj);
234 if (msg == nullptr) {
235 cJSON_Delete(obj);
236 return nullptr;
237 }
238 cJSON_Delete(obj);
239 head.len = static_cast<uint32_t>(strlen(msg) + 1);
240 uint32_t size = GetAuthDataSize(head.len);
241 uint8_t *buf = reinterpret_cast<uint8_t *>(SoftBusMalloc(size));
242 if (buf == nullptr) {
243 cJSON_free(msg);
244 return nullptr;
245 }
246 int32_t ret = PackAuthData(&head, reinterpret_cast<uint8_t *>(msg), buf, size);
247 if (ret == SOFTBUS_OK) {
248 cJSON_free(msg);
249 AUTH_LOGI(AUTH_TEST, "PackAuthData success.");
250 return reinterpret_cast<char *>(buf);
251 }
252 SoftBusFree(buf);
253 cJSON_free(msg);
254 return nullptr;
255 }
256
OnDeviceVerifyPass(AuthHandle authHandle,const NodeInfo * info)257 void AuthNetLedgertInterfaceMock::OnDeviceVerifyPass(AuthHandle authHandle, const NodeInfo *info)
258 {
259 AUTH_LOGI(AUTH_TEST, "Device verify passed & send cond");
260 (void)authHandle;
261 (void)info;
262 if (SoftBusMutexLock(&LnnHichainInterfaceMock::mutex) != SOFTBUS_OK) {
263 AUTH_LOGE(AUTH_TEST, "Device verify Lock failed");
264 return;
265 }
266 isRuned = true;
267 SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
268 SoftBusMutexUnlock(&LnnHichainInterfaceMock::mutex);
269 }
270
OnDeviceNotTrusted(const char * peerUdid)271 void AuthNetLedgertInterfaceMock::OnDeviceNotTrusted(const char *peerUdid)
272 {
273 AUTH_LOGI(AUTH_TEST, "Device not trusted call back & send cond");
274 (void)peerUdid;
275 if (SoftBusMutexLock(&LnnHichainInterfaceMock::mutex) != SOFTBUS_OK) {
276 AUTH_LOGE(AUTH_TEST, "Device not trusted Lock failed");
277 return;
278 }
279 isRuned = true;
280 SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
281 SoftBusMutexUnlock(&LnnHichainInterfaceMock::mutex);
282 }
283
OnDeviceDisconnect(AuthHandle authHandle)284 void AuthNetLedgertInterfaceMock::OnDeviceDisconnect(AuthHandle authHandle)
285 {
286 AUTH_LOGI(AUTH_TEST, "Device disconnect call back & send cond");
287 (void)authHandle;
288 if (SoftBusMutexLock(&LnnHichainInterfaceMock::mutex) != SOFTBUS_OK) {
289 AUTH_LOGE(AUTH_TEST, "Device disconnect Lock failed");
290 return;
291 }
292 isRuned = true;
293 SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
294 SoftBusMutexUnlock(&LnnHichainInterfaceMock::mutex);
295 }
296 } // namespace OHOS