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