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