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