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_hichain_mock.h"
17
18 #include "auth_interface.h"
19 #include "auth_log.h"
20 #include "softbus_adapter_mem.h"
21 #include "softbus_adapter_json.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 const int32_t GRUOP_NUM1 = 10;
28 const int32_t GRUOP_NUM2 = 12;
29 const int32_t GRUOP_NUM3 = 100;
30
31 bool g_isFlage = false;
32 bool g_isReturnDeviceNum = false;
33 bool g_isReturnTrue = false;
34 const int32_t GROUP_TYPE_POINT_TO_POINT = 256;
35 const int32_t GROUP_VISIBILITY_INVALID = 26;
36
37 void *g_hichainInterface;
38
LnnHichainInterfaceMock()39 LnnHichainInterfaceMock::LnnHichainInterfaceMock()
40 {
41 AUTH_LOGI(AUTH_TEST, "construction");
42 g_hichainInterface = reinterpret_cast<void *>(this);
43 }
44
~LnnHichainInterfaceMock()45 LnnHichainInterfaceMock::~LnnHichainInterfaceMock()
46 {
47 AUTH_LOGI(AUTH_TEST, "delete");
48 g_hichainInterface = nullptr;
49 }
50
GetHichainInterface()51 static LnnHichainInterface *GetHichainInterface()
52 {
53 return reinterpret_cast<LnnHichainInterfaceMock *>(g_hichainInterface);
54 }
55
56 extern "C" {
InitDeviceAuthService(void)57 int32_t InitDeviceAuthService(void)
58 {
59 return GetHichainInterface()->InitDeviceAuthService();
60 }
61
DestroyDeviceAuthService(void)62 void DestroyDeviceAuthService(void)
63 {
64 return GetHichainInterface()->DestroyDeviceAuthService();
65 }
66
GetGaInstance(void)67 const GroupAuthManager *GetGaInstance(void)
68 {
69 return GetHichainInterface()->GetGaInstance();
70 }
71
GetGmInstance(void)72 const DeviceGroupManager *GetGmInstance(void)
73 {
74 AUTH_LOGI(AUTH_TEST, "GetGmInstance");
75 return GetHichainInterface()->GetGmInstance();
76 }
77 }
78
ActionOfProcessData(int64_t authSeq,const uint8_t * data,uint32_t len,const DeviceAuthCallback * gaCallback)79 int32_t LnnHichainInterfaceMock::ActionOfProcessData(
80 int64_t authSeq, const uint8_t *data, uint32_t len, const DeviceAuthCallback *gaCallback)
81 {
82 (void)authSeq;
83 (void)data;
84 (void)len;
85 g_devAuthCb.onTransmit = gaCallback->onTransmit;
86 g_devAuthCb.onSessionKeyReturned = gaCallback->onSessionKeyReturned;
87 g_devAuthCb.onFinish = gaCallback->onFinish;
88 g_devAuthCb.onError = gaCallback->onError;
89 g_devAuthCb.onRequest = gaCallback->onRequest;
90 return HC_SUCCESS;
91 }
92
InvokeAuthDevice(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * gaCallback)93 int32_t LnnHichainInterfaceMock::InvokeAuthDevice(int32_t osAccountId, int64_t authReqId, const char *authParams,
94 const DeviceAuthCallback *gaCallback)
95 {
96 return HC_SUCCESS;
97 }
98
AuthDeviceConnSend(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * gaCallback)99 int32_t LnnHichainInterfaceMock::AuthDeviceConnSend(int32_t osAccountId, int64_t authReqId, const char *authParams,
100 const DeviceAuthCallback *gaCallback)
101 {
102 AUTH_LOGI(AUTH_TEST, "AuthDeviceConnSend");
103 (void)SoftBusCondSignal(&LnnHichainInterfaceMock::cond);
104 return HC_SUCCESS;
105 }
106
InvokeDataChangeListener(const char * appId,const DataChangeListener * listener)107 int32_t LnnHichainInterfaceMock::InvokeDataChangeListener(const char *appId, const DataChangeListener *listener)
108 {
109 if (appId == nullptr || listener == nullptr) {
110 return SOFTBUS_INVALID_PARAM;
111 }
112 LnnHichainInterfaceMock::g_datachangelistener.emplace(appId, listener);
113 return SOFTBUS_OK;
114 }
115
InvokeGetJoinedGroups1(int32_t osAccountId,const char * appId,int groupType,char ** returnGroupVec,uint32_t * groupNum)116 int32_t LnnHichainInterfaceMock::InvokeGetJoinedGroups1(int32_t osAccountId, const char *appId, int groupType,
117 char **returnGroupVec, uint32_t *groupNum)
118 {
119 (void)osAccountId;
120 (void)appId;
121 *groupNum = 1;
122
123 if (groupType == AUTH_IDENTICAL_ACCOUNT_GROUP) {
124 *groupNum = GRUOP_NUM1;
125 }
126 if (groupType == AUTH_PEER_TO_PEER_GROUP) {
127 *groupNum = GRUOP_NUM2;
128 }
129 *returnGroupVec = reinterpret_cast<char *>(SoftBusCalloc(*groupNum));
130 if (*returnGroupVec == NULL) {
131 return HC_ERR_INVALID_PARAMS;
132 }
133 return HC_SUCCESS;
134 }
135
InvokeGetJoinedGroups2(int32_t osAccountId,const char * appId,int groupType,char ** returnGroupVec,uint32_t * groupNum)136 int32_t LnnHichainInterfaceMock::InvokeGetJoinedGroups2(int32_t osAccountId, const char *appId, int groupType,
137 char **returnGroupVec, uint32_t *groupNum)
138 {
139 (void)osAccountId;
140 (void)appId;
141 (void)groupType;
142 (void)returnGroupVec;
143 *groupNum = GRUOP_NUM3;
144
145 return HC_ERR_INVALID_PARAMS;
146 }
147
InvokeGetJoinedGroups3(int32_t osAccountId,const char * appId,int groupType,char ** returnGroupVec,uint32_t * groupNum)148 int32_t LnnHichainInterfaceMock::InvokeGetJoinedGroups3(int32_t osAccountId, const char *appId, int groupType,
149 char **returnGroupVec, uint32_t *groupNum)
150 {
151 (void)osAccountId;
152 (void)appId;
153 (void)groupType;
154 (void)returnGroupVec;
155 *groupNum = 0;
156
157 return HC_SUCCESS;
158 }
ActionofunRegDataChangeListener(const char * appId)159 int32_t LnnHichainInterfaceMock::ActionofunRegDataChangeListener(const char *appId)
160 {
161 (void)appId;
162 LnnHichainInterfaceMock::g_datachangelistener.clear();
163 return SOFTBUS_OK;
164 }
getRelatedGroups(int32_t accountId,const char * auth_appId,const char * groupId,char ** returnDevInfoVec,uint32_t * deviceNum)165 int32_t LnnHichainInterfaceMock::getRelatedGroups(
166 int32_t accountId, const char *auth_appId, const char *groupId, char **returnDevInfoVec, uint32_t *deviceNum)
167 {
168 (void)accountId;
169 (void)auth_appId;
170 (void)groupId;
171 AUTH_LOGI(AUTH_TEST, "getRelatedGroups test");
172 if (!g_isFlage) {
173 AUTH_LOGI(AUTH_TEST, "getRelatedGroups test return false");
174 g_isFlage = true;
175 return SOFTBUS_ERR;
176 }
177 char data = 'A';
178 if (g_isReturnDeviceNum) {
179 char* testChar = &data;
180 *deviceNum = strlen(testChar) + 1;
181 *returnDevInfoVec = testChar;
182 return SOFTBUS_OK;
183 }
184 g_isReturnDeviceNum = true;
185 return SOFTBUS_OK;
186 }
getRelatedGroups1(int32_t accountId,const char * auth_appId,const char * groupId,char ** returnDevInfoVec,uint32_t * deviceNum)187 int32_t LnnHichainInterfaceMock::getRelatedGroups1(
188 int32_t accountId, const char *auth_appId, const char *groupId, char **returnDevInfoVec, uint32_t *deviceNum)
189 {
190 (void)accountId;
191 (void)auth_appId;
192 (void)groupId;
193 *deviceNum = 1;
194 JsonObj *obj = JSON_CreateObject();
195 if (obj == NULL) {
196 AUTH_LOGI(AUTH_TEST, "create jsonObject err");
197 return SOFTBUS_ERR;
198 }
199 if (!JSON_AddStringToObject(obj, "groupName", "mygroup<256>E469") ||
200 !JSON_AddStringToObject(obj, "groupId", "1D77EBFF0349B27EED57014DD7B2449A") ||
201 !JSON_AddStringToObject(obj, "groupOwner", "com.hhhs.secueity") ||
202 !JSON_AddInt32ToObject(obj, "groupType", GROUP_TYPE_POINT_TO_POINT) ||
203 !JSON_AddInt32ToObject(obj, "groupVisibility", GROUP_VISIBILITY_INVALID)) {
204 JSON_Delete(obj);
205 return SOFTBUS_ERR;
206 }
207 char* jsons = JSON_PrintUnformatted(obj);
208 *returnDevInfoVec = jsons;
209
210 AUTH_LOGI(AUTH_TEST, "getRelatedGroups1 test");
211 JSON_Delete(obj);
212 return SOFTBUS_OK;
213 }
getTrustedDevices(int32_t osAccountId,const char * appId,const char * groupId,char ** returnDevInfoVec,uint32_t * deviceNum)214 int32_t LnnHichainInterfaceMock::getTrustedDevices(
215 int32_t osAccountId, const char *appId, const char *groupId, char **returnDevInfoVec, uint32_t *deviceNum)
216 {
217 (void)osAccountId;
218 (void)appId;
219 (void)groupId;
220 *deviceNum = 1;
221 JsonObj *obj = JSON_CreateObject();
222 if (obj == NULL) {
223 AUTH_LOGI(AUTH_TEST, "create jsonObject err");
224 return SOFTBUS_ERR;
225 }
226 if (!JSON_AddStringToObject(obj, "authId", "ABCDEDF00ABCDE0021DD55ACFF")) {
227 JSON_Delete(obj);
228 return SOFTBUS_ERR;
229 }
230 char* jsons = JSON_PrintUnformatted(obj);
231 *returnDevInfoVec = jsons;
232 if (!g_isReturnTrue) {
233 g_isReturnTrue = true;
234 JSON_Delete(obj);
235 return SOFTBUS_ERR;
236 }
237 JSON_Delete(obj);
238 return SOFTBUS_OK;
239 }
240
getTrustedDevices1(int32_t osAccountId,const char * appId,const char * groupId,char ** returnDevInfoVec,uint32_t * deviceNum)241 int32_t LnnHichainInterfaceMock::getTrustedDevices1(
242 int32_t osAccountId, const char *appId, const char *groupId, char **returnDevInfoVec, uint32_t *deviceNum)
243 {
244 (void)osAccountId;
245 (void)appId;
246 (void)groupId;
247
248 char jsonsStr[] = "{\"groupId\":\"1111\", \"groupType\":1}";
249 char* data = jsonsStr;
250 *returnDevInfoVec = data;
251 AUTH_LOGI(AUTH_TEST, "returnDevInfoVec is invalid");
252 if (g_isReturnDeviceNum) {
253 g_isReturnDeviceNum = false;
254 return SOFTBUS_OK;
255 }
256 *deviceNum = 1;
257 return SOFTBUS_OK;
258 }
259
destroyInfo(char ** returnDevInfoVec)260 void LnnHichainInterfaceMock::destroyInfo(char **returnDevInfoVec)
261 {
262 (void)returnDevInfoVec;
263 AUTH_LOGI(AUTH_TEST, "destroyInfo test");
264 }
265 } // namespace OHOS