1 /*
2 * Copyright (c) 2023-2024 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 #include <securec.h>
16 #include "UTTest_hichain_auth_connector.h"
17 #include "dm_anonymous.h"
18 #include "dm_constants.h"
19 #include "hichain_auth_connector.h"
20
21 static int32_t g_processCredentialResultCode = -1;
22 static std::string g_processCredentialReturnDataStr = "";
23
24 namespace OHOS {
25 namespace DistributedHardware {
SetUp()26 void HiChainAuthConnectorTest::SetUp()
27 {
28 g_processCredentialResultCode = -1;
29 g_processCredentialReturnDataStr = "";
30 }
31
TearDown()32 void HiChainAuthConnectorTest::TearDown()
33 {
34 }
35
SetUpTestCase()36 void HiChainAuthConnectorTest::SetUpTestCase()
37 {
38 }
39
TearDownTestCase()40 void HiChainAuthConnectorTest::TearDownTestCase()
41 {
42 }
43
44 class DmDeviceAuthCallbackTest : public IDmDeviceAuthCallback {
45 public:
DmDeviceAuthCallbackTest()46 DmDeviceAuthCallbackTest() {}
~DmDeviceAuthCallbackTest()47 virtual ~DmDeviceAuthCallbackTest() {}
AuthDeviceTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)48 bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) override
49 {
50 (void)requestId;
51 (void)data;
52 (void)dataLen;
53 return true;
54 }
AuthDeviceFinish(int64_t requestId)55 void AuthDeviceFinish(int64_t requestId) override
56 {
57 (void)requestId;
58 }
AuthDeviceError(int64_t requestId,int32_t errorCode)59 void AuthDeviceError(int64_t requestId, int32_t errorCode) override
60 {
61 (void)requestId;
62 pinCode = errorCode;
63 }
AuthDeviceSessionKey(int64_t requestId,const uint8_t * sessionKey,uint32_t sessionKeyLen)64 void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) override
65 {
66 (void)requestId;
67 (void)sessionKey;
68 (void)sessionKeyLen;
69 }
GetPinCode(int32_t & code)70 int32_t GetPinCode(int32_t &code) override
71 {
72 if (pinCode == 0) {
73 code = 0;
74 return DM_OK;
75 }
76 code = ERR_DM_AUTH_NOT_START;
77 return ERR_DM_AUTH_NOT_START;
78 }
GetRemoteDeviceId(std::string & deviceId)79 void GetRemoteDeviceId(std::string &deviceId) override
80 {
81 (void)deviceId;
82 }
83 private:
84 int32_t pinCode = 0;
85 };
86
87 HWTEST_F(HiChainAuthConnectorTest, RegisterHiChainAuthCallback_001, testing::ext::TestSize.Level0)
88 {
89 std::shared_ptr<IDmDeviceAuthCallback> callback = nullptr;
90 int32_t ret = hiChain_->RegisterHiChainAuthCallback(callback);
91 EXPECT_EQ(ret, DM_OK);
92 }
93
94 HWTEST_F(HiChainAuthConnectorTest, AuthDevice_001, testing::ext::TestSize.Level0)
95 {
96 int32_t pinCode = 0;
97 int32_t osAccountId = 0;
98 std::string udid;
99 int64_t requestId = 0;
100 int32_t ret = hiChain_->AuthDevice(pinCode, osAccountId, udid, requestId);
101 EXPECT_EQ(ret, ERR_DM_FAILED);
102 }
103
104 HWTEST_F(HiChainAuthConnectorTest, ProcessAuthData_001, testing::ext::TestSize.Level0)
105 {
106 int32_t requestId = 0;
107 std::string authData;
108 int32_t osAccountId = 0;
109 int32_t ret = hiChain_->ProcessAuthData(requestId, authData, osAccountId);
110 EXPECT_EQ(ret, ERR_DM_FAILED);
111 }
112
113 HWTEST_F(HiChainAuthConnectorTest, onTransmit_001, testing::ext::TestSize.Level0)
114 {
115 int32_t requestId = 0;
116 uint8_t *data = nullptr;
117 uint32_t dataLen = 0;
118 hiChain_->dmDeviceAuthCallback_ = nullptr;
119 bool ret = hiChain_->onTransmit(requestId, data, dataLen);
120 EXPECT_EQ(ret, false);
121 }
122
123 HWTEST_F(HiChainAuthConnectorTest, onTransmit_002, testing::ext::TestSize.Level0)
124 {
125 int32_t requestId = 0;
126 uint8_t *data = nullptr;
127 uint32_t dataLen = 0;
128 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
129 bool ret = hiChain_->onTransmit(requestId, data, dataLen);
130 EXPECT_EQ(ret, true);
131 }
132
133 HWTEST_F(HiChainAuthConnectorTest, onRequest_001, testing::ext::TestSize.Level0)
134 {
135 int64_t requestId = 0;
136 int operationCode = 0;
137 char *reqParams = nullptr;
138 hiChain_->dmDeviceAuthCallback_ = nullptr;
139 hiChain_->onRequest(requestId, operationCode, reqParams);
140 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
141 }
142
143 HWTEST_F(HiChainAuthConnectorTest, onRequest_002, testing::ext::TestSize.Level0)
144 {
145 int64_t requestId = 0;
146 int operationCode = 0;
147 char *reqParams = nullptr;
148 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
149 hiChain_->onRequest(requestId, operationCode, reqParams);
150 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
151 }
152
153 HWTEST_F(HiChainAuthConnectorTest, onRequest_003, testing::ext::TestSize.Level0)
154 {
155 int64_t requestId = 0;
156 int operationCode = 0;
157 int32_t errorCode = -20024;
158 char *reqParams = nullptr;
159 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
160 hiChain_->dmDeviceAuthCallback_->AuthDeviceError(requestId, errorCode);
161 hiChain_->onRequest(requestId, operationCode, reqParams);
162 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
163 }
164
165 HWTEST_F(HiChainAuthConnectorTest, onRequest_004, testing::ext::TestSize.Level0)
166 {
167 int64_t requestId = 0;
168 int operationCode = 0;
169 char *reqParams = nullptr;
170 std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
171 std::shared_ptr<MockIDmDeviceAuthCallback> mockCallback = std::make_shared<MockIDmDeviceAuthCallback>();
172 hiChainAuthConnector->dmDeviceAuthCallback_ = mockCallback;
173 EXPECT_CALL(*mockCallback, GetPinCode(testing::_))
174 .Times(1)
175 .WillOnce(testing::Return(ERR_DM_FAILED));
176 EXPECT_NE(hiChainAuthConnector->onRequest(requestId, operationCode, reqParams), nullptr);
177 }
178
179 HWTEST_F(HiChainAuthConnectorTest, onFinish_001, testing::ext::TestSize.Level0)
180 {
181 int64_t requestId = 0;
182 int operationCode = 0;
183 char *returnData = nullptr;
184 hiChain_->dmDeviceAuthCallback_ = nullptr;
185 hiChain_->onFinish(requestId, operationCode, returnData);
186 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
187 }
188
189 HWTEST_F(HiChainAuthConnectorTest, onFinish_002, testing::ext::TestSize.Level0)
190 {
191 int64_t requestId = 0;
192 int operationCode = 0;
193 char *returnData = nullptr;
194 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
195 hiChain_->onFinish(requestId, operationCode, returnData);
196 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
197 }
198
199 HWTEST_F(HiChainAuthConnectorTest, onError_001, testing::ext::TestSize.Level0)
200 {
201 int64_t requestId = 0;
202 int operationCode = 0;
203 int errorCode = 0;
204 char *errorReturn = nullptr;
205 hiChain_->dmDeviceAuthCallback_ = nullptr;
206 hiChain_->onError(requestId, operationCode, errorCode, errorReturn);
207 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
208 }
209
210 HWTEST_F(HiChainAuthConnectorTest, onError_002, testing::ext::TestSize.Level0)
211 {
212 int64_t requestId = 0;
213 int operationCode = 0;
214 int errorCode = 0;
215 char *errorReturn = nullptr;
216 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
217 hiChain_->onError(requestId, operationCode, errorCode, errorReturn);
218 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
219 }
220
221 HWTEST_F(HiChainAuthConnectorTest, onSessionKeyReturned_001, testing::ext::TestSize.Level0)
222 {
223 int64_t requestId = 0;
224 uint8_t *sessionKey = nullptr;
225 uint32_t sessionKeyLen = 0;
226 hiChain_->dmDeviceAuthCallback_ = nullptr;
227 hiChain_->onSessionKeyReturned(requestId, sessionKey, sessionKeyLen);
228 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
229 }
230
231 HWTEST_F(HiChainAuthConnectorTest, onSessionKeyReturned_003, testing::ext::TestSize.Level0)
232 {
233 int64_t requestId = 0;
234 uint8_t *sessionKey = nullptr;
235 uint32_t sessionKeyLen = 0;
236 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
237 hiChain_->onSessionKeyReturned(requestId, sessionKey, sessionKeyLen);
238 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
239 }
240
241 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_001, testing::ext::TestSize.Level0)
242 {
243 std::string localUdid;
244 int32_t osAccountId = 0;
245 std::string publicKey;
246 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
247 EXPECT_NE(ret, DM_OK);
248 }
249
250 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_002, testing::ext::TestSize.Level0)
251 {
252 std::string localUdid;
253 int32_t osAccountId = 0;
254 std::string publicKey;
255 g_processCredentialResultCode = HC_SUCCESS;
256 g_processCredentialReturnDataStr = "{invalid_json}";;
257 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
258 EXPECT_NE(ret, DM_OK);
259 }
260
261 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_003, testing::ext::TestSize.Level0)
262 {
263 std::string localUdid;
264 int32_t osAccountId = 0;
265 std::string publicKey;
266 g_processCredentialResultCode = HC_SUCCESS;
267 g_processCredentialReturnDataStr = R"({"result": "not_an_int", "publicKey": "key"})";
268 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
269 EXPECT_NE(ret, DM_OK);
270 }
271
272 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_004, testing::ext::TestSize.Level0)
273 {
274 std::string localUdid;
275 int32_t osAccountId = 0;
276 std::string publicKey;
277 g_processCredentialResultCode = HC_SUCCESS;
278 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": 1)";
279 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
280 EXPECT_NE(ret, DM_OK);
281 }
282
283 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_005, testing::ext::TestSize.Level0)
284 {
285 std::string localUdid;
286 int32_t osAccountId = 0;
287 std::string publicKey;
288 g_processCredentialResultCode = HC_SUCCESS;
289 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": "key"})";
290 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
291 EXPECT_NE(ret, DM_OK);
292 }
293
294 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_006, testing::ext::TestSize.Level0)
295 {
296 std::string localUdid;
297 int32_t osAccountId = 0;
298 std::string publicKey;
299 g_processCredentialResultCode = HC_SUCCESS;
300 g_processCredentialReturnDataStr = R"({"result": 0, "publicKey": "key"})";
301 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
302 EXPECT_EQ(ret, DM_OK);
303 }
304
305 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_001, testing::ext::TestSize.Level0)
306 {
307 std::string localUdid = "2131351352";
308 int32_t osAccountId = 0;
309 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
310 EXPECT_EQ(ret, false);
311 }
312
313 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_002, testing::ext::TestSize.Level0)
314 {
315 JsonObject jsonObject;
316 jsonObject["result"] = 15;
317 jsonObject["publicKey"] = 0;
318 std::string localUdid = SafetyDump(jsonObject);
319 int32_t osAccountId = 1245;
320 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
321 EXPECT_EQ(ret, false);
322 }
323
324 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_003, testing::ext::TestSize.Level0)
325 {
326 JsonObject jsonObject;
327 jsonObject["result"] = 15;
328 jsonObject["publicKey"] = 0;
329 std::string localUdid = SafetyDump(jsonObject);
330 g_processCredentialResultCode = HC_SUCCESS;
331 g_processCredentialReturnDataStr = "{invalid_json}";
332 int32_t osAccountId = 1245;
333 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
334 EXPECT_FALSE(ret);
335 }
336
337 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_004, testing::ext::TestSize.Level0)
338 {
339 JsonObject jsonObject;
340 jsonObject["result"] = 15;
341 jsonObject["publicKey"] = 0;
342 std::string localUdid = SafetyDump(jsonObject);
343 g_processCredentialResultCode = HC_SUCCESS;
344 g_processCredentialReturnDataStr = R"({"result": "not_an_int", "publicKey": "key"})";
345 int32_t osAccountId = 1245;
346 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
347 EXPECT_FALSE(ret);
348 }
349
350 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_005, testing::ext::TestSize.Level0)
351 {
352 JsonObject jsonObject;
353 jsonObject["result"] = 15;
354 jsonObject["publicKey"] = 0;
355 std::string localUdid = SafetyDump(jsonObject);
356 g_processCredentialResultCode = HC_SUCCESS;
357 g_processCredentialReturnDataStr = R"({"result": -1, "publicKey": "key"})";
358 int32_t osAccountId = 1245;
359 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
360 EXPECT_FALSE(ret);
361 }
362
363 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_006, testing::ext::TestSize.Level0)
364 {
365 JsonObject jsonObject;
366 jsonObject["result"] = 15;
367 jsonObject["publicKey"] = 0;
368 std::string localUdid = SafetyDump(jsonObject);
369 g_processCredentialResultCode = HC_SUCCESS;
370 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": 0})";
371 int32_t osAccountId = 1245;
372 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
373 EXPECT_FALSE(ret);
374 }
375
376 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_007, testing::ext::TestSize.Level0)
377 {
378 JsonObject jsonObject;
379 jsonObject["result"] = 15;
380 jsonObject["publicKey"] = 0;
381 std::string localUdid = SafetyDump(jsonObject);
382 g_processCredentialResultCode = HC_SUCCESS;
383 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": "string"})";
384 int32_t osAccountId = 1245;
385 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
386 EXPECT_FALSE(ret);
387 }
388
389 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_008, testing::ext::TestSize.Level0)
390 {
391 JsonObject jsonObject;
392 jsonObject["result"] = 15;
393 jsonObject["publicKey"] = 0;
394 std::string localUdid = SafetyDump(jsonObject);
395 g_processCredentialResultCode = HC_SUCCESS;
396 g_processCredentialReturnDataStr = R"({"result": 2, "publicKey": "string"})";
397 int32_t osAccountId = 1245;
398 bool ret = hiChain_->QueryCredential(localUdid, osAccountId);
399 EXPECT_TRUE(ret);
400 }
401
402 HWTEST_F(HiChainAuthConnectorTest, GetCredential_001, testing::ext::TestSize.Level0)
403 {
404 std::string localUdid;
405 int32_t osAccountId = 0;
406 std::string publicKey;
407 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
408 EXPECT_EQ(ret, ERR_DM_FAILED);
409 }
410
411 HWTEST_F(HiChainAuthConnectorTest, GetCredential_002, testing::ext::TestSize.Level0)
412 {
413 std::string localUdid = "GADGFADEFGSDA";
414 int32_t osAccountId = 0;
415 std::string publicKey = "4165145615";
416 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
417 EXPECT_EQ(ret, ERR_DM_FAILED);
418 }
419
420 HWTEST_F(HiChainAuthConnectorTest, GetCredential_003, testing::ext::TestSize.Level0)
421 {
422 std::string publicKey = "test";
423 std::string localUdid = "test";
424 int32_t osAccountId = 0;
425 g_processCredentialResultCode = HC_SUCCESS;
426 g_processCredentialReturnDataStr = "{invalid_json}";
427 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
428 EXPECT_EQ(ret, ERR_DM_FAILED);
429 }
430
431 HWTEST_F(HiChainAuthConnectorTest, GetCredential_004, testing::ext::TestSize.Level0)
432 {
433 std::string publicKey = "test";
434 std::string localUdid = "test";
435 int32_t osAccountId = 0;
436 g_processCredentialResultCode = HC_SUCCESS;
437 g_processCredentialReturnDataStr = R"({"result": "not_an_int", "publicKey": "key"})";
438 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
439 EXPECT_EQ(ret, ERR_DM_FAILED);
440 }
441
442 HWTEST_F(HiChainAuthConnectorTest, GetCredential_005, testing::ext::TestSize.Level0)
443 {
444 std::string publicKey = "test";
445 std::string localUdid = "test";
446 int32_t osAccountId = 0;
447 g_processCredentialResultCode = HC_SUCCESS;
448 g_processCredentialReturnDataStr = R"({"result": -1, "publicKey": "key"})";
449 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
450 EXPECT_EQ(ret, ERR_DM_FAILED);
451 }
452
453 HWTEST_F(HiChainAuthConnectorTest, GetCredential_006, testing::ext::TestSize.Level0)
454 {
455 std::string publicKey = "test";
456 std::string localUdid = "test";
457 int32_t osAccountId = 0;
458 g_processCredentialResultCode = HC_SUCCESS;
459 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": 0})";
460 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
461 EXPECT_EQ(ret, ERR_DM_FAILED);
462 }
463
464 HWTEST_F(HiChainAuthConnectorTest, GetCredential_007, testing::ext::TestSize.Level0)
465 {
466 std::string publicKey = "test";
467 std::string localUdid = "test";
468 int32_t osAccountId = 0;
469 g_processCredentialResultCode = HC_SUCCESS;
470 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": "string"})";
471 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
472 EXPECT_EQ(ret, ERR_DM_FAILED);
473 }
474
475 HWTEST_F(HiChainAuthConnectorTest, GetCredential_008, testing::ext::TestSize.Level0)
476 {
477 std::string publicKey = "test";
478 std::string localUdid = "test";
479 int32_t osAccountId = 0;
480 g_processCredentialResultCode = HC_SUCCESS;
481 g_processCredentialReturnDataStr = R"({"result": 2, "publicKey": "string"})";
482 int32_t ret = hiChain_->GetCredential(localUdid, osAccountId, publicKey);
483 EXPECT_EQ(ret, DM_OK);
484 }
485
486 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_001, testing::ext::TestSize.Level0)
487 {
488 int32_t localUdid = 0;
489 std::string deviceId;
490 std::string publicKey;
491 int32_t ret = hiChain_->ImportCredential(localUdid, deviceId, publicKey);
492 EXPECT_NE(ret, DM_OK);
493 }
494
495 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_002, testing::ext::TestSize.Level0)
496 {
497 int32_t localUdid = 0;
498 std::string deviceId = "4513541351";
499 std::string publicKey = "42125143613";
500 int32_t ret = hiChain_->ImportCredential(localUdid, deviceId, publicKey);
501 EXPECT_NE(ret, DM_OK);
502 }
503
504 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_003, testing::ext::TestSize.Level0)
505 {
506 int32_t localUdid = 0;
507 std::string deviceId = "test";
508 std::string publicKey = "test";
509 g_processCredentialResultCode = HC_SUCCESS;
510 g_processCredentialReturnDataStr = "{invalid_json}";
511 int32_t ret = hiChain_->ImportCredential(localUdid, deviceId, publicKey);
512 EXPECT_EQ(ret, ERR_DM_FAILED);
513 }
514
515 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_004, testing::ext::TestSize.Level0)
516 {
517 int32_t localUdid = 0;
518 std::string deviceId = "test";
519 std::string publicKey = "test";
520 g_processCredentialResultCode = HC_SUCCESS;
521 g_processCredentialReturnDataStr = R"({"result": "not_an_int"})";
522 int32_t ret = hiChain_->ImportCredential(localUdid, deviceId, publicKey);
523 EXPECT_EQ(ret, ERR_DM_FAILED);
524 }
525
526 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_005, testing::ext::TestSize.Level0)
527 {
528 int32_t localUdid = 0;
529 std::string deviceId = "test";
530 std::string publicKey = "test";
531 g_processCredentialResultCode = HC_SUCCESS;
532 g_processCredentialReturnDataStr = R"({"result": -1})";
533 int32_t ret = hiChain_->ImportCredential(localUdid, deviceId, publicKey);
534 EXPECT_EQ(ret, ERR_DM_FAILED);
535 }
536
537 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_006, testing::ext::TestSize.Level0)
538 {
539 int32_t localUdid = 0;
540 std::string deviceId = "test";
541 std::string publicKey = "test";
542 g_processCredentialResultCode = HC_SUCCESS;
543 g_processCredentialReturnDataStr = R"({"result": 0})";
544 int32_t ret = hiChain_->ImportCredential(localUdid, deviceId, publicKey);
545 EXPECT_EQ(ret, DM_OK);
546 }
547
548 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_001, testing::ext::TestSize.Level0)
549 {
550 std::string deviceId;
551 int32_t userId = 0;
552 int32_t ret = hiChain_->DeleteCredential(deviceId, userId);
553 EXPECT_EQ(ret, DM_OK);
554 }
555
556 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_002, testing::ext::TestSize.Level0)
557 {
558 std::string deviceId = "864513535";
559 int32_t userId = 0;
560 int32_t ret = hiChain_->DeleteCredential(deviceId, userId);
561 EXPECT_EQ(ret, DM_OK);
562 }
563
564 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_003, testing::ext::TestSize.Level0)
565 {
566 std::string deviceId = "test";
567 int32_t userId = 0;
568 g_processCredentialResultCode = HC_SUCCESS;
569 g_processCredentialReturnDataStr = "{invalid_json}";
570 int32_t ret = hiChain_->DeleteCredential(deviceId, userId);
571 EXPECT_EQ(ret, 0);
572 }
573
574 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_004, testing::ext::TestSize.Level0)
575 {
576 std::string deviceId = "test";
577 int32_t userId = 0;
578 g_processCredentialResultCode = HC_SUCCESS;
579 g_processCredentialReturnDataStr = R"({"result": "not_an_int"})";
580 int32_t ret = hiChain_->DeleteCredential(deviceId, userId);
581 EXPECT_EQ(ret, ERR_DM_FAILED);
582 }
583
584 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_005, testing::ext::TestSize.Level0)
585 {
586 std::string deviceId = "test";
587 int32_t userId = 0;
588 g_processCredentialResultCode = HC_SUCCESS;
589 g_processCredentialReturnDataStr = R"({"result": 100})";
590 int32_t ret = hiChain_->DeleteCredential(deviceId, userId);
591 EXPECT_EQ(ret, 100);
592 }
593 } // namespace DistributedHardware
594 } // namespace OHOS
595
ProcessCredential(int32_t operationCode,const char * requestParams,char ** returnData)596 extern "C" __attribute__((constructor)) DEVICE_AUTH_API_PUBLIC int32_t ProcessCredential(int32_t operationCode,
597 const char *requestParams, char **returnData)
598 {
599 if (requestParams == nullptr || returnData == nullptr) {
600 return -1;
601 }
602
603 if (g_processCredentialReturnDataStr.empty()) {
604 *returnData = nullptr;
605 return -1;
606 }
607
608 char *charArray = strdup(g_processCredentialReturnDataStr.c_str());
609 if (charArray == nullptr) {
610 return -1;
611 }
612
613 *returnData = charArray;
614 return g_processCredentialResultCode;
615 }
616