1 /*
2 * Copyright (c) 2023-2025 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 }
AuthDeviceRequest(int64_t requestId,int operationCode,const char * reqParams)70 char *AuthDeviceRequest(int64_t requestId, int operationCode, const char *reqParams) override
71 {
72 (void)requestId;
73 (void)operationCode;
74 (void)reqParams;
75 return nullptr;
76 }
77 private:
78 int32_t pinCode = 0;
79 };
80
81 HWTEST_F(HiChainAuthConnectorTest, RegisterHiChainAuthCallback_001, testing::ext::TestSize.Level1)
82 {
83 std::shared_ptr<IDmDeviceAuthCallback> callback = nullptr;
84 int32_t ret = hiChain_->RegisterHiChainAuthCallback(callback);
85 EXPECT_EQ(ret, DM_OK);
86 }
87
88 HWTEST_F(HiChainAuthConnectorTest, AuthDevice_001, testing::ext::TestSize.Level1)
89 {
90 std::string pinCode = "0";
91 int32_t osAccountId = 0;
92 std::string udid;
93 int64_t requestId = 0;
94 int32_t ret = hiChain_->AuthDevice(pinCode, osAccountId, udid, requestId);
95 EXPECT_EQ(ret, ERR_DM_FAILED);
96 }
97
98 HWTEST_F(HiChainAuthConnectorTest, ProcessAuthData_001, testing::ext::TestSize.Level1)
99 {
100 int32_t requestId = 0;
101 std::string authData;
102 int32_t osAccountId = 0;
103 int32_t ret = hiChain_->ProcessAuthData(requestId, authData, osAccountId);
104 EXPECT_EQ(ret, ERR_DM_FAILED);
105 }
106
107 HWTEST_F(HiChainAuthConnectorTest, onTransmit_001, testing::ext::TestSize.Level1)
108 {
109 int32_t requestId = 0;
110 uint8_t *data = nullptr;
111 uint32_t dataLen = 0;
112 hiChain_->dmDeviceAuthCallback_ = nullptr;
113 bool ret = hiChain_->onTransmit(requestId, data, dataLen);
114 EXPECT_EQ(ret, false);
115 }
116
117 HWTEST_F(HiChainAuthConnectorTest, onTransmit_002, testing::ext::TestSize.Level1)
118 {
119 int32_t requestId = 0;
120 uint8_t *data = nullptr;
121 uint32_t dataLen = 0;
122 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
123 bool ret = hiChain_->onTransmit(requestId, data, dataLen);
124 EXPECT_EQ(ret, true);
125 }
126
127 HWTEST_F(HiChainAuthConnectorTest, onRequest_001, testing::ext::TestSize.Level1)
128 {
129 int64_t requestId = 0;
130 int operationCode = 0;
131 char *reqParams = nullptr;
132 hiChain_->dmDeviceAuthCallback_ = nullptr;
133 char *ret = hiChain_->onRequest(requestId, operationCode, reqParams);
134 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
135 if (ret != nullptr) {
136 free(ret);
137 ret = nullptr;
138 }
139 }
140
141 HWTEST_F(HiChainAuthConnectorTest, onRequest_002, testing::ext::TestSize.Level1)
142 {
143 int64_t requestId = 0;
144 int operationCode = 0;
145 char *reqParams = nullptr;
146 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
147 char *ret = hiChain_->onRequest(requestId, operationCode, reqParams);
148 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
149 if (ret != nullptr) {
150 free(ret);
151 ret = nullptr;
152 }
153 }
154
155 HWTEST_F(HiChainAuthConnectorTest, onRequest_003, testing::ext::TestSize.Level1)
156 {
157 int64_t requestId = 0;
158 int operationCode = 0;
159 int32_t errorCode = -20024;
160 char *reqParams = nullptr;
161 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
162 hiChain_->dmDeviceAuthCallback_->AuthDeviceError(requestId, errorCode);
163 char *ret = hiChain_->onRequest(requestId, operationCode, reqParams);
164 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
165 if (ret != nullptr) {
166 free(ret);
167 ret = nullptr;
168 }
169 }
170
171 HWTEST_F(HiChainAuthConnectorTest, onFinish_001, testing::ext::TestSize.Level1)
172 {
173 int64_t requestId = 0;
174 int operationCode = 0;
175 char *returnData = nullptr;
176 hiChain_->dmDeviceAuthCallback_ = nullptr;
177 hiChain_->onFinish(requestId, operationCode, returnData);
178 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
179 }
180
181 HWTEST_F(HiChainAuthConnectorTest, onFinish_002, testing::ext::TestSize.Level1)
182 {
183 int64_t requestId = 0;
184 int operationCode = 0;
185 char *returnData = nullptr;
186 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
187 hiChain_->onFinish(requestId, operationCode, returnData);
188 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
189 }
190
191 HWTEST_F(HiChainAuthConnectorTest, onError_001, testing::ext::TestSize.Level1)
192 {
193 int64_t requestId = 0;
194 int operationCode = 0;
195 int errorCode = 0;
196 char *errorReturn = nullptr;
197 hiChain_->dmDeviceAuthCallback_ = nullptr;
198 hiChain_->onError(requestId, operationCode, errorCode, errorReturn);
199 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
200 }
201
202 HWTEST_F(HiChainAuthConnectorTest, onError_002, testing::ext::TestSize.Level1)
203 {
204 int64_t requestId = 0;
205 int operationCode = 0;
206 int errorCode = 0;
207 char *errorReturn = nullptr;
208 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
209 hiChain_->onError(requestId, operationCode, errorCode, errorReturn);
210 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
211 }
212
213 HWTEST_F(HiChainAuthConnectorTest, onSessionKeyReturned_001, testing::ext::TestSize.Level1)
214 {
215 int64_t requestId = 0;
216 uint8_t *sessionKey = nullptr;
217 uint32_t sessionKeyLen = 0;
218 hiChain_->dmDeviceAuthCallback_ = nullptr;
219 hiChain_->onSessionKeyReturned(requestId, sessionKey, sessionKeyLen);
220 EXPECT_EQ(hiChain_->dmDeviceAuthCallback_, nullptr);
221 }
222
223 HWTEST_F(HiChainAuthConnectorTest, onSessionKeyReturned_003, testing::ext::TestSize.Level1)
224 {
225 int64_t requestId = 0;
226 uint8_t *sessionKey = nullptr;
227 uint32_t sessionKeyLen = 0;
228 hiChain_->dmDeviceAuthCallback_ = std::make_shared<DmDeviceAuthCallbackTest>();
229 hiChain_->onSessionKeyReturned(requestId, sessionKey, sessionKeyLen);
230 EXPECT_NE(hiChain_->dmDeviceAuthCallback_, nullptr);
231 }
232
233 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_001, testing::ext::TestSize.Level1)
234 {
235 std::string localUdid;
236 int32_t osAccountId = 0;
237 std::string publicKey;
238 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
239 EXPECT_NE(ret, DM_OK);
240 }
241
242 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_002, testing::ext::TestSize.Level1)
243 {
244 std::string localUdid;
245 int32_t osAccountId = 0;
246 std::string publicKey;
247 g_processCredentialResultCode = HC_SUCCESS;
248 g_processCredentialReturnDataStr = "{invalid_json}";
249 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
250 EXPECT_NE(ret, DM_OK);
251 }
252
253 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_003, testing::ext::TestSize.Level1)
254 {
255 std::string localUdid;
256 int32_t osAccountId = 0;
257 std::string publicKey;
258 g_processCredentialResultCode = HC_SUCCESS;
259 g_processCredentialReturnDataStr = R"({"result": "not_an_int", "publicKey": "key"})";
260 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
261 EXPECT_EQ(ret, DM_OK);
262 }
263
264 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_004, testing::ext::TestSize.Level1)
265 {
266 std::string localUdid;
267 int32_t osAccountId = 0;
268 std::string publicKey;
269 g_processCredentialResultCode = HC_SUCCESS;
270 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": 1)";
271 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
272 EXPECT_NE(ret, DM_OK);
273 }
274
275 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_005, testing::ext::TestSize.Level1)
276 {
277 std::string localUdid;
278 int32_t osAccountId = 0;
279 std::string publicKey;
280 g_processCredentialResultCode = HC_SUCCESS;
281 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": "key"})";
282 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
283 EXPECT_EQ(ret, DM_OK);
284 }
285
286 HWTEST_F(HiChainAuthConnectorTest, GenerateCredential_006, testing::ext::TestSize.Level1)
287 {
288 std::string localUdid;
289 int32_t osAccountId = 0;
290 std::string publicKey;
291 g_processCredentialResultCode = HC_SUCCESS;
292 g_processCredentialReturnDataStr = R"({"result": 0, "publicKey": "key"})";
293 int32_t ret = hiChain_->GenerateCredential(localUdid, osAccountId, publicKey);
294 EXPECT_EQ(ret, DM_OK);
295 }
296
297 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_001, testing::ext::TestSize.Level1)
298 {
299 std::string localUdid = "2131351352";
300 int32_t osAccountId = 0;
301 int32_t peerOsAccountId = -1;
302 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
303 EXPECT_EQ(ret, false);
304 }
305
306 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_002, testing::ext::TestSize.Level1)
307 {
308 JsonObject jsonObject;
309 jsonObject["result"] = 15;
310 jsonObject["publicKey"] = 0;
311 std::string localUdid = jsonObject.Dump();
312 int32_t osAccountId = 1245;
313 int32_t peerOsAccountId = -1;
314 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
315 EXPECT_EQ(ret, false);
316 }
317
318 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_003, testing::ext::TestSize.Level1)
319 {
320 JsonObject jsonObject;
321 jsonObject["result"] = 15;
322 jsonObject["publicKey"] = 0;
323 std::string localUdid = jsonObject.Dump();
324 g_processCredentialResultCode = HC_SUCCESS;
325 g_processCredentialReturnDataStr = "{invalid_json}";
326 int32_t osAccountId = 1245;
327 int32_t peerOsAccountId = -1;
328 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
329 EXPECT_FALSE(ret);
330 }
331
332 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_004, testing::ext::TestSize.Level1)
333 {
334 JsonObject jsonObject;
335 jsonObject["result"] = 15;
336 jsonObject["publicKey"] = 0;
337 std::string localUdid = jsonObject.Dump();
338 g_processCredentialResultCode = HC_SUCCESS;
339 g_processCredentialReturnDataStr = R"({"result": "not_an_int", "publicKey": "key"})";
340 int32_t osAccountId = 1245;
341 int32_t peerOsAccountId = -1;
342 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
343 EXPECT_TRUE(ret);
344 }
345
346 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_005, testing::ext::TestSize.Level1)
347 {
348 JsonObject jsonObject;
349 jsonObject["result"] = 15;
350 jsonObject["publicKey"] = 0;
351 std::string localUdid = jsonObject.Dump();
352 g_processCredentialResultCode = HC_SUCCESS;
353 g_processCredentialReturnDataStr = R"({"result": -1, "publicKey": "key"})";
354 int32_t osAccountId = 1245;
355 int32_t peerOsAccountId = -1;
356 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
357 EXPECT_TRUE(ret);
358 }
359
360 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_006, testing::ext::TestSize.Level1)
361 {
362 JsonObject jsonObject;
363 jsonObject["result"] = 15;
364 jsonObject["publicKey"] = 0;
365 std::string localUdid = jsonObject.Dump();
366 g_processCredentialResultCode = HC_SUCCESS;
367 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": 0})";
368 int32_t osAccountId = 1245;
369 int32_t peerOsAccountId = -1;
370 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
371 EXPECT_FALSE(ret);
372 }
373
374 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_007, testing::ext::TestSize.Level1)
375 {
376 JsonObject jsonObject;
377 jsonObject["result"] = 15;
378 jsonObject["publicKey"] = 0;
379 std::string localUdid = jsonObject.Dump();
380 g_processCredentialResultCode = HC_SUCCESS;
381 g_processCredentialReturnDataStr = R"({"result": 1, "publicKey": "string"})";
382 int32_t osAccountId = 1245;
383 int32_t peerOsAccountId = -1;
384 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
385 EXPECT_TRUE(ret);
386 }
387
388 HWTEST_F(HiChainAuthConnectorTest, QueryCredential_008, testing::ext::TestSize.Level1)
389 {
390 JsonObject jsonObject;
391 jsonObject["result"] = 15;
392 jsonObject["publicKey"] = 0;
393 std::string localUdid = jsonObject.Dump();
394 g_processCredentialResultCode = HC_SUCCESS;
395 g_processCredentialReturnDataStr = R"({"result": 2, "publicKey": "string"})";
396 int32_t osAccountId = 1245;
397 int32_t peerOsAccountId = -1;
398 bool ret = hiChain_->QueryCredential(localUdid, osAccountId, peerOsAccountId);
399 EXPECT_TRUE(ret);
400 }
401
402 HWTEST_F(HiChainAuthConnectorTest, GetCredential_001, testing::ext::TestSize.Level1)
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.Level1)
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.Level1)
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.Level1)
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, DM_OK);
440 }
441
442 HWTEST_F(HiChainAuthConnectorTest, GetCredential_005, testing::ext::TestSize.Level1)
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, DM_OK);
451 }
452
453 HWTEST_F(HiChainAuthConnectorTest, GetCredential_006, testing::ext::TestSize.Level1)
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.Level1)
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, DM_OK);
473 }
474
475 HWTEST_F(HiChainAuthConnectorTest, GetCredential_008, testing::ext::TestSize.Level1)
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.Level1)
487 {
488 int32_t localUdid = 0;
489 std::string deviceId;
490 std::string publicKey;
491 int32_t peerUserId = 0;
492 int32_t ret = hiChain_->ImportCredential(localUdid, peerUserId, deviceId, publicKey);
493 EXPECT_NE(ret, DM_OK);
494 }
495
496 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_002, testing::ext::TestSize.Level1)
497 {
498 int32_t localUdid = 0;
499 std::string deviceId = "4513541351";
500 std::string publicKey = "42125143613";
501 int32_t peerUserId = 0;
502 int32_t ret = hiChain_->ImportCredential(localUdid, peerUserId, deviceId, publicKey);
503 EXPECT_NE(ret, DM_OK);
504 }
505
506 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_003, testing::ext::TestSize.Level1)
507 {
508 int32_t localUdid = 0;
509 std::string deviceId = "test";
510 std::string publicKey = "test";
511 g_processCredentialResultCode = HC_SUCCESS;
512 g_processCredentialReturnDataStr = "{invalid_json}";
513 int32_t peerUserId = 0;
514 int32_t ret = hiChain_->ImportCredential(localUdid, peerUserId, deviceId, publicKey);
515 EXPECT_EQ(ret, DM_OK);
516 }
517
518 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_004, testing::ext::TestSize.Level1)
519 {
520 int32_t localUdid = 0;
521 std::string deviceId = "test";
522 std::string publicKey = "test";
523 g_processCredentialResultCode = HC_SUCCESS;
524 g_processCredentialReturnDataStr = R"({"result": "not_an_int"})";
525 int32_t peerUserId = 0;
526 int32_t ret = hiChain_->ImportCredential(localUdid, peerUserId, deviceId, publicKey);
527 EXPECT_EQ(ret, DM_OK);
528 }
529
530 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_005, testing::ext::TestSize.Level1)
531 {
532 int32_t localUdid = 0;
533 std::string deviceId = "test";
534 std::string publicKey = "test";
535 g_processCredentialResultCode = HC_SUCCESS;
536 g_processCredentialReturnDataStr = R"({"result": -1})";
537 int32_t peerUserId = 0;
538 int32_t ret = hiChain_->ImportCredential(localUdid, peerUserId, deviceId, publicKey);
539 EXPECT_EQ(ret, DM_OK);
540 }
541
542 HWTEST_F(HiChainAuthConnectorTest, ImportCredential_006, testing::ext::TestSize.Level1)
543 {
544 int32_t localUdid = 0;
545 std::string deviceId = "test";
546 std::string publicKey = "test";
547 g_processCredentialResultCode = HC_SUCCESS;
548 g_processCredentialReturnDataStr = R"({"result": 0})";
549 int32_t peerUserId = 0;
550 int32_t ret = hiChain_->ImportCredential(localUdid, peerUserId, deviceId, publicKey);
551 EXPECT_EQ(ret, DM_OK);
552 }
553
554 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_001, testing::ext::TestSize.Level1)
555 {
556 std::string deviceId;
557 int32_t userId = 0;
558 int32_t peerUserId = 0;
559 int32_t ret = hiChain_->DeleteCredential(deviceId, userId, peerUserId);
560 EXPECT_NE(ret, DM_OK);
561 }
562
563 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_002, testing::ext::TestSize.Level1)
564 {
565 std::string deviceId = "864513535";
566 int32_t userId = 0;
567 int32_t peerUserId = 0;
568 int32_t ret = hiChain_->DeleteCredential(deviceId, userId, peerUserId);
569 EXPECT_NE(ret, DM_OK);
570 }
571
572 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_003, testing::ext::TestSize.Level1)
573 {
574 std::string deviceId = "test";
575 int32_t userId = 0;
576 g_processCredentialResultCode = HC_SUCCESS;
577 g_processCredentialReturnDataStr = "{invalid_json}";
578 int32_t peerUserId = 0;
579 int32_t ret = hiChain_->DeleteCredential(deviceId, userId, peerUserId);
580 EXPECT_EQ(ret, 0);
581 }
582
583 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_004, testing::ext::TestSize.Level1)
584 {
585 std::string deviceId = "test";
586 int32_t userId = 0;
587 g_processCredentialResultCode = HC_SUCCESS;
588 g_processCredentialReturnDataStr = R"({"result": "not_an_int"})";
589 int32_t peerUserId = 0;
590 int32_t ret = hiChain_->DeleteCredential(deviceId, userId, peerUserId);
591 EXPECT_EQ(ret, DM_OK);
592 }
593
594 HWTEST_F(HiChainAuthConnectorTest, DeleteCredential_005, testing::ext::TestSize.Level1)
595 {
596 std::string deviceId = "test";
597 int32_t userId = 0;
598 g_processCredentialResultCode = HC_SUCCESS;
599 g_processCredentialReturnDataStr = R"({"result": 100})";
600 int32_t peerUserId = 0;
601 int32_t ret = hiChain_->DeleteCredential(deviceId, userId, peerUserId);
602 EXPECT_EQ(ret, DM_OK);
603 }
604
605 HWTEST_F(HiChainAuthConnectorTest, ProcessCredData_001, testing::ext::TestSize.Level1)
606 {
607 int64_t authReqId = 0;
608 std::string data;
609 int32_t ret = hiChain_->ProcessCredData(authReqId, data);
610 EXPECT_EQ(ret, ERR_DM_FAILED);
611 }
612
613 HWTEST_F(HiChainAuthConnectorTest, AddCredential_001, testing::ext::TestSize.Level1)
614 {
615 int32_t osAccountId = 0;
616 std::string authParams = "authParamsTest";
617 std::string credId;
618 int32_t ret = hiChain_->AddCredential(osAccountId, authParams, credId);
619 EXPECT_EQ(ret, ERR_DM_FAILED);
620 }
621
622 HWTEST_F(HiChainAuthConnectorTest, ExportCredential_001, testing::ext::TestSize.Level1)
623 {
624 int32_t osAccountId = 0;
625 std::string credId = "credIdTest";
626 std::string publicKey;
627 int32_t ret = hiChain_->ExportCredential(osAccountId, credId, publicKey);
628 EXPECT_EQ(ret, ERR_DM_FAILED);
629 }
630
631 HWTEST_F(HiChainAuthConnectorTest, AgreeCredential_001, testing::ext::TestSize.Level1)
632 {
633 int32_t osAccountId = 0;
634 std::string selfCredId = "selfCredIdTest";
635 std::string authParams;
636 std::string credId;
637 int32_t ret = hiChain_->AgreeCredential(osAccountId, selfCredId, authParams, credId);
638 EXPECT_EQ(ret, ERR_DM_FAILED);
639 }
640
641 HWTEST_F(HiChainAuthConnectorTest, AuthCredential_001, testing::ext::TestSize.Level1)
642 {
643 int32_t osAccountId = 0;
644 int64_t authReqId = 1;
645 std::string credId = "credIdTest";
646 std::string pinCode = "146894";
647 int32_t ret = hiChain_->AuthCredential(osAccountId, authReqId, credId, pinCode);
648 EXPECT_EQ(ret, ERR_DM_FAILED);
649 }
650
651 HWTEST_F(HiChainAuthConnectorTest, AuthCredentialPinCode_001, testing::ext::TestSize.Level1)
652 {
653 int32_t osAccountId = 0;
654 int64_t authReqId = 1;
655 std::string pinCode = "233";
656 int32_t ret = hiChain_->AuthCredentialPinCode(osAccountId, authReqId, pinCode);
657 EXPECT_EQ(ret, ERR_DM_FAILED);
658 }
659
660 HWTEST_F(HiChainAuthConnectorTest, AuthCredentialPinCode_002, testing::ext::TestSize.Level1)
661 {
662 int32_t osAccountId = 0;
663 int64_t authReqId = 1;
664 std::string pinCode = "369528";
665 int32_t ret = hiChain_->AuthCredentialPinCode(osAccountId, authReqId, pinCode);
666 EXPECT_EQ(ret, ERR_DM_FAILED);
667 }
668
669 HWTEST_F(HiChainAuthConnectorTest, QueryCredentialInfo_001, testing::ext::TestSize.Level1)
670 {
671 int32_t userId = 0;
672 JsonObject queryParam;
673 JsonObject resultJson;
674 int32_t ret = hiChain_->QueryCredentialInfo(userId, queryParam, resultJson);
675 EXPECT_EQ(ret, ERR_DM_FAILED);
676 }
677
678 HWTEST_F(HiChainAuthConnectorTest, QueryCredInfoByCredId_001, testing::ext::TestSize.Level1)
679 {
680 int32_t userId = 0;
681 std::string credId;
682 JsonObject resultJson;
683 int32_t ret = hiChain_->QueryCredInfoByCredId(userId, credId, resultJson);
684 EXPECT_NE(ret, DM_OK);
685 }
686 } // namespace DistributedHardware
687 } // namespace OHOS
688
ProcessCredential(int32_t operationCode,const char * requestParams,char ** returnData)689 extern "C" __attribute__((constructor)) DEVICE_AUTH_API_PUBLIC int32_t ProcessCredential(int32_t operationCode,
690 const char *requestParams, char **returnData)
691 {
692 if (requestParams == nullptr || returnData == nullptr) {
693 return -1;
694 }
695
696 if (g_processCredentialReturnDataStr.empty()) {
697 *returnData = nullptr;
698 return -1;
699 }
700
701 char *charArray = strdup(g_processCredentialReturnDataStr.c_str());
702 if (charArray == nullptr) {
703 return -1;
704 }
705
706 *returnData = charArray;
707 return g_processCredentialResultCode;
708 }
709