• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "dslm_test.h"
17 
18 #include <chrono>
19 #include <condition_variable>
20 #include <gtest/gtest.h>
21 #include <iostream>
22 #include <mutex>
23 #include <thread>
24 
25 #include "securec.h"
26 
27 #include "device_security_defines.h"
28 #include "device_security_info.h"
29 
30 #include "dslm_core_defines.h"
31 #include "dslm_core_process.h"
32 #include "dslm_credential.h"
33 #include "dslm_crypto.h"
34 #include "dslm_device_list.h"
35 #include "dslm_fsm_process.h"
36 #include "dslm_messenger_wrapper.h"
37 #include "dslm_msg_interface_mock.h"
38 #include "dslm_msg_serialize.h"
39 #include "dslm_msg_utils.h"
40 #include "dslm_request_callback_mock.h"
41 
42 using namespace std;
43 using namespace std::chrono;
44 using namespace testing;
45 using namespace testing::ext;
46 
47 namespace OHOS {
48 namespace Security {
49 namespace DslmUnitTest {
SetUpTestCase()50 void DslmTest::SetUpTestCase()
51 {
52 }
TearDownTestCase()53 void DslmTest::TearDownTestCase()
54 {
55 }
SetUp()56 void DslmTest::SetUp()
57 {
58 }
TearDown()59 void DslmTest::TearDown()
60 {
61 }
62 
BlockCheckDeviceStatus(const DeviceIdentify * device,uint32_t status,uint64_t millisec)63 static void BlockCheckDeviceStatus(const DeviceIdentify *device, uint32_t status, uint64_t millisec)
64 {
65     static int sleepTick = 10;
66     uint64_t cnt = millisec / sleepTick + 1;
67     do {
68         const DslmDeviceInfo *info = GetDslmDeviceInfo(device);
69         if (info == nullptr) {
70             continue;
71         }
72         if (info->machine.currState == status) {
73             break;
74         }
75         if (cnt == 0) {
76             break;
77         }
78         this_thread::sleep_for(milliseconds(sleepTick));
79         cnt--;
80     } while (true);
81 }
82 
83 HWTEST_F(DslmTest, BuildDeviceSecInfoRequest_case1, TestSize.Level1)
84 {
85     uint64_t random = 0x0807060504030201;
86     MessageBuff *msg = nullptr;
87     // 0d196608 = 0x030000
88     const char *except =
89         "{\"message\":1,\"payload\":{\"version\":196608,\"challenge\":\"0102030405060708\",\"support\":[2000,3000]}}";
90     int32_t ret = BuildDeviceSecInfoRequest(random, &msg);
91     ASSERT_EQ(0, ret);
92     EXPECT_STREQ(except, (const char *)msg->buff);
93     FreeMessageBuff(msg);
94 }
95 
96 HWTEST_F(DslmTest, BuildDeviceSecInfoResponse_case1, TestSize.Level1)
97 {
98     uint64_t random = 0x0807060504030201;
99     uint8_t info[] = {'a', 'b', 'c', 'd', 1, 3, 5, 7, 9};
100     DslmCredBuff cred = {(CredType)3, 9, info};
101 
102     // 0d196608 = 0x030000
103     const char *except = "{\"message\":2,\"payload\":{\"version\":196608,\"type\":3,\"challenge\":\"0102030405060708\","
104                          "\"info\":\"YWJjZAEDBQcJ\"}}";
105 
106     MessageBuff *msg = nullptr;
107     int32_t ret = BuildDeviceSecInfoResponse(random, (DslmCredBuff *)&cred, &msg);
108     ASSERT_EQ(0, ret);
109 
110     EXPECT_STREQ(except, (const char *)msg->buff);
111     FreeMessageBuff(msg);
112 }
113 
114 HWTEST_F(DslmTest, ParseMessage_case1, TestSize.Level1)
115 {
116     const char *message = "{\"message\":1,\"payload\":{\"version\":131072,\"challenge\":\"0102030405060708\"}}";
117     const char *except = "{\"version\":131072,\"challenge\":\"0102030405060708\"}";
118 
119     uint32_t messageLen = strlen(message) + 1;
120     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
121 
122     MessagePacket *packet = ParseMessage(&msg);
123     ASSERT_NE(nullptr, packet);
124 
125     EXPECT_EQ(1, (int32_t)packet->type);
126     EXPECT_STREQ(except, (const char *)packet->payload);
127 
128     FreeMessagePacket(packet);
129 }
130 
131 HWTEST_F(DslmTest, ParseMessage_case2, TestSize.Level1)
132 {
133     const char *message = "{\"mege\":1,\"payload\":{\"version\":131072,\"challenge\":\"0102030405060708\"}}";
134 
135     uint32_t messageLen = strlen(message) + 1;
136     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
137 
138     MessagePacket *packet = ParseMessage(&msg);
139     EXPECT_EQ(nullptr, packet);
140     FreeMessagePacket(packet);
141 }
142 
143 HWTEST_F(DslmTest, ParseMessage_case3, TestSize.Level1)
144 {
145     const char *message = "{\"message\":1,\"pay\":{\"version\":131072,\"challenge\":\"0102030405060708\"}}";
146 
147     uint32_t messageLen = strlen(message) + 1;
148     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
149 
150     MessagePacket *packet = ParseMessage(&msg);
151     EXPECT_EQ(nullptr, packet);
152     FreeMessagePacket(packet);
153 }
154 
155 HWTEST_F(DslmTest, ParseDeviceSecInfoRequest_case1, TestSize.Level1)
156 {
157     const char *message = "{\"version\":3351057,\"challenge\":\"010203040a0b0c0d\"}";
158 
159     uint32_t messageLen = strlen(message) + 1;
160     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
161 
162     RequestObject obj;
163     memset_s(&obj, sizeof(RequestObject), 0, sizeof(RequestObject));
164 
165     // 3351057 = 0x332211
166     int32_t ret = ParseDeviceSecInfoRequest(&msg, &obj);
167     EXPECT_EQ(0, ret);
168 
169     EXPECT_EQ((uint32_t)0x332211, obj.version);
170     EXPECT_EQ((uint64_t)0x0d0c0b0a04030201, obj.challenge);
171     EXPECT_EQ((uint32_t)0, obj.arraySize);
172 }
173 
174 HWTEST_F(DslmTest, ParseDeviceSecInfoRequest_case2, TestSize.Level1)
175 {
176     const char *message = "{\"version\":3351057,\"challenge\":\"z\"}";
177 
178     uint32_t messageLen = strlen(message) + 1;
179     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
180 
181     RequestObject obj;
182     memset_s(&obj, sizeof(RequestObject), 0, sizeof(RequestObject));
183 
184     int32_t ret = ParseDeviceSecInfoRequest(&msg, &obj);
185     EXPECT_EQ(ERR_NO_CHALLENGE, ret);
186 }
187 
188 HWTEST_F(DslmTest, ParseDeviceSecInfoRequest_case3, TestSize.Level1)
189 {
190     const char *message = "{\"version\":3351057,\"challenge\":1}";
191 
192     uint32_t messageLen = strlen(message) + 1;
193     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
194 
195     RequestObject obj;
196     memset_s(&obj, sizeof(RequestObject), 0, sizeof(RequestObject));
197     int32_t ret = ParseDeviceSecInfoRequest(&msg, &obj);
198     EXPECT_EQ(ERR_NO_CHALLENGE, ret);
199 }
200 
201 HWTEST_F(DslmTest, ParseDeviceSecInfoRequest_case4, TestSize.Level1)
202 {
203     const char *message = "{\"version\":3351057,\"challssenge\":\"z\"}";
204 
205     uint32_t messageLen = strlen(message) + 1;
206     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
207 
208     RequestObject obj;
209     memset_s(&obj, sizeof(RequestObject), 0, sizeof(RequestObject));
210     int32_t ret = ParseDeviceSecInfoRequest(&msg, &obj);
211     EXPECT_EQ(ERR_NO_CHALLENGE, ret);
212 }
213 
214 HWTEST_F(DslmTest, ParseDeviceSecInfoRequest_case5, TestSize.Level1)
215 {
216     const char *message = "{\"version\":3351057,\"challenge\":\"010203040a0b0c0d\",\"support\":[33,44,55]}";
217 
218     uint32_t messageLen = strlen(message) + 1;
219     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
220 
221     RequestObject obj;
222     memset_s(&obj, sizeof(RequestObject), 0, sizeof(RequestObject));
223 
224     // 3351057 = 0x332211
225     int32_t ret = ParseDeviceSecInfoRequest(&msg, &obj);
226     EXPECT_EQ(0, ret);
227     EXPECT_EQ((uint32_t)0x332211, obj.version);
228     EXPECT_EQ((uint64_t)0x0d0c0b0a04030201, obj.challenge);
229     // add support
230     EXPECT_EQ((uint32_t)3, obj.arraySize);
231     EXPECT_EQ((uint32_t)33, obj.credArray[0]);
232     EXPECT_EQ((uint32_t)44, obj.credArray[1]);
233     EXPECT_EQ((uint32_t)55, obj.credArray[2]);
234 }
235 
236 HWTEST_F(DslmTest, ParseDeviceSecInfoRequest_case6, TestSize.Level1)
237 {
238     const char *message = "{\"version\":3351057,\"challenge\":\"010203040a0b0c0d\",\"support\":[]}";
239 
240     uint32_t messageLen = strlen(message) + 1;
241     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
242 
243     RequestObject obj;
244     memset_s(&obj, sizeof(RequestObject), 0, sizeof(RequestObject));
245 
246     // 3351057 = 0x332211
247     int32_t ret = ParseDeviceSecInfoRequest(&msg, &obj);
248     EXPECT_EQ(0, ret);
249     EXPECT_EQ((uint32_t)0x332211, obj.version);
250     EXPECT_EQ((uint64_t)0x0d0c0b0a04030201, obj.challenge);
251     // add support
252     EXPECT_EQ((uint32_t)0, obj.arraySize);
253 }
254 
255 HWTEST_F(DslmTest, ParseDeviceSecInfoResponse_case1, TestSize.Level1)
256 {
257     const char *message = "{\"version\":131072,\"challenge\":\"3C1F21EE53D3C4E2\",\"type\":2,\"info\":"
258                           "\"SkFERS1BTDAwOjg3QUQyOEQzQjFCLi4u\"}";
259 
260     uint32_t messageLen = strlen(message) + 1;
261     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
262 
263     uint64_t challenge;
264     uint32_t version;
265     DslmCredBuff *cred = nullptr;
266 
267     // 131072 = 0x020000
268     int32_t ret = ParseDeviceSecInfoResponse(&msg, &challenge, &version, &cred);
269     EXPECT_EQ(0, ret);
270     EXPECT_EQ((uint32_t)0x020000, version);
271 
272     EXPECT_EQ((uint64_t)0xE2C4D353EE211F3C, challenge);
273 
274     const char *except = "JADE-AL00:87AD28D3B1B...";
275     EXPECT_NE(nullptr, cred);
276     EXPECT_EQ(2, (int32_t)cred->type);
277     EXPECT_EQ(strlen(except), cred->credLen);
278     EXPECT_EQ(0, strncmp(except, (const char *)cred->credVal, cred->credLen));
279     DestroyDslmCred(cred);
280 }
281 
282 HWTEST_F(DslmTest, ParseDeviceSecInfoResponse_case2, TestSize.Level1)
283 {
284     const char *message = "{\"version\":3351057,\"challssenge\":\"z\"}";
285 
286     uint32_t messageLen = strlen(message) + 1;
287     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
288 
289     uint64_t challenge;
290     uint32_t ver;
291     DslmCredBuff *cred = nullptr;
292 
293     int32_t ret = ParseDeviceSecInfoResponse(&msg, &challenge, &ver, &cred);
294     EXPECT_EQ(ERR_NO_CHALLENGE, ret);
295 }
296 
297 HWTEST_F(DslmTest, ParseDeviceSecInfoResponse_case3, TestSize.Level1)
298 {
299     const char *message =
300         "{\"version\":131072,\"challenge\":\"3C1F21EE53D3C4E2\",\"type\":2,\"infod\":\"JADE-AL00:87AD28D3B1B...\"}";
301 
302     uint32_t messageLen = strlen(message) + 1;
303     MessageBuff msg = {.length = messageLen, .buff = (uint8_t *)message};
304 
305     uint64_t challenge;
306     uint32_t ver;
307     DslmCredBuff *cred = nullptr;
308 
309     int32_t ret = ParseDeviceSecInfoResponse(&msg, &challenge, &ver, &cred);
310     EXPECT_EQ(ERR_NO_CRED, ret);
311 }
312 
313 HWTEST_F(DslmTest, RandomValue_case1, TestSize.Level1)
314 {
315     RandomValue rand1 = {0, {0}};
316     memset_s(&rand1, sizeof(RandomValue), 0, sizeof(RandomValue));
317     GenerateRandom(&rand1, sizeof(uint64_t));
318 
319     RandomValue rand2 = {0, {0}};
320     memset_s(&rand2, sizeof(RandomValue), 0, sizeof(RandomValue));
321     GenerateRandom(&rand2, sizeof(uint64_t));
322 
323     EXPECT_EQ(sizeof(uint64_t), rand1.length);
324     EXPECT_EQ(sizeof(uint64_t), rand2.length);
325 
326     EXPECT_GT(rand1.value[0] + rand1.value[1] + rand1.value[2] + rand1.value[3], 0);
327     EXPECT_EQ(rand1.value[31] + rand1.value[30] + rand1.value[29] + rand1.value[28], 0);
328     EXPECT_NE(0, memcmp(rand1.value, rand2.value, sizeof(uint64_t)));
329 }
330 
331 HWTEST_F(DslmTest, RandomValue_case2, TestSize.Level1)
332 {
333     RandomValue rand = {0, {0}};
334     memset_s(&rand, sizeof(RandomValue), 0, sizeof(RandomValue));
335 
336     GenerateRandom(&rand, 1024);
337     EXPECT_EQ(RAMDOM_MAX_LEN, (int32_t)rand.length);
338 }
339 
340 /*
341 HWTEST_F(DslmTest, OhosDslmCred_case1, TestSize.Level1)
342 {
343     const DeviceIdentify identiy = {DEVICE_ID_MAX_LEN, {0}};
344     RequestObject object;
345 
346     object.arraySize = 1;
347     object.credArray[0] = CRED_TYPE_STANDARD;
348     object.challenge = 0x1234567812345678;
349     object.version = 0x112234;
350 
351     DslmCredBuff *cred = nullptr;
352 
353     int32_t ret = DefaultRequestDslmCred(&identiy, &object, &cred);
354     ASSERT_EQ(SUCCESS, (int32_t)ret);
355 
356     DslmCredInfo info;
357     memset_s(&info, sizeof(DslmCredInfo), 0, sizeof(DslmCredInfo));
358 
359     ret = DefaultVerifyDslmCred(&identiy, object.challenge, cred, &info);
360     EXPECT_EQ(SUCCESS, ret);
361     EXPECT_GE(info.credLevel, (uint32_t)1);
362 
363     DestroyDslmCred(cred);
364 }
365 
366 HWTEST_F(DslmTest, OnRequestDeviceSecLevelInfo_case1, TestSize.Level1)
367 {
368     const DeviceIdentify device = {DEVICE_ID_MAX_LEN, {'a', 'b', 'c', 'd', 'e', 'f', 'g'}};
369 
370     const RequestOption option = {
371         .challenge = 0xffffffffffffffff,
372         .timeout = 3,
373     };
374 
375     {
376         uint32_t cookie = 1234;
377         DslmRequestCallbackMock mockCallback;
378         EXPECT_CALL(mockCallback, RequestCallback(_, _, _)).Times(Exactly(0));
379         int32_t ret = OnRequestDeviceSecLevelInfo(&device, &option, 0, cookie, DslmRequestCallbackMock::MockedCallback);
380         EXPECT_EQ((int32_t)ret, ERR_MSG_NOT_INIT);
381     }
382 
383     {
384         uint32_t cookie = 5678;
385         DslmMsgInterfaceMock mockMsg;
386         DslmRequestCallbackMock mockCallback;
387         EXPECT_CALL(mockMsg, IsMessengerReady(_)).Times(AtLeast(1));
388         EXPECT_CALL(mockMsg, GetDeviceOnlineStatus(_, _, _)).Times(AtLeast(1)).WillRepeatedly(Return(false));
389         EXPECT_CALL(mockCallback, RequestCallback(_, _, _)).Times(Exactly(0));
390         int32_t ret = OnRequestDeviceSecLevelInfo(&device, &option, 0, cookie, DslmRequestCallbackMock::MockedCallback);
391         EXPECT_EQ((int32_t)ret, ERR_NOEXIST_DEVICE);
392 
393         EXPECT_CALL(mockMsg, SendMsgTo(_, _, _, _, _)).Times(AtLeast(2));
394         mockMsg.MakeMsgLoopback();
395         mockMsg.MakeDeviceOnline(&device);
396         BlockCheckDeviceStatus(&device, STATE_SUCCESS, 10000);
397         mockMsg.MakeDeviceOffline(&device);
398     }
399 
400     {
401         uint32_t cookie = 0xabcd;
402         DslmMsgInterfaceMock mockMsg;
403         EXPECT_CALL(mockMsg, IsMessengerReady(_)).Times(AtLeast(1));
404         EXPECT_CALL(mockMsg, GetDeviceOnlineStatus(_, _, _)).Times(AtLeast(1)).WillRepeatedly(Return(true));
405         EXPECT_CALL(mockMsg, SendMsgTo(_, _, _, _, _)).Times(Exactly(1));
406         DslmRequestCallbackMock mockCallback;
407         auto IsRightLevel = [](const DslmCallbackInfo *info) { return info->level >= 1; };
408         EXPECT_CALL(mockCallback, RequestCallback(cookie, 0, Truly(IsRightLevel))).Times(Exactly(1));
409 
410         int32_t ret = OnRequestDeviceSecLevelInfo(&device, &option, 0, cookie, DslmRequestCallbackMock::MockedCallback);
411         EXPECT_EQ(ret, (int32_t)0);
412         mockMsg.MakeDeviceOffline(&device);
413     }
414 }
415 */
416 
417 HWTEST_F(DslmTest, OnRequestDeviceSecLevelInfo_case2, TestSize.Level1)
418 {
419     const DeviceIdentify device = {DEVICE_ID_MAX_LEN, {'a'}};
420     const RequestOption option = {
421         .challenge = 0xffabcdffffffffee,
422         .timeout = 3,
423         .extra = 0,
424     };
425 
426     DslmMsgInterfaceMock mockMsg;
427     EXPECT_CALL(mockMsg, IsMessengerReady(_)).Times(AtLeast(1));
428     EXPECT_CALL(mockMsg, GetDeviceOnlineStatus(_, _, _)).Times(AtLeast(1)).WillRepeatedly(Return(true));
__anon35389b7b0102(const uint8_t *message) 429     auto isSendRequestOut = [](const uint8_t *message) {
430         const char *prefix = "{\"message\":1,\"payload\":{\"version\":196608,\"challenge\":\"";
431         string msg = string((char *)message);
432         EXPECT_EQ((int)msg.rfind(prefix, 0), 0);
433         return true;
434     };
435 
436     uint32_t cookie = 0x4567;
437     EXPECT_CALL(mockMsg, SendMsgTo(_, _, _, Truly(isSendRequestOut), _)).Times(AtLeast(1)).WillRepeatedly(Return(true));
438     int32_t ret = OnRequestDeviceSecLevelInfo(&device, &option, 0, cookie, DslmRequestCallbackMock::MockedCallback);
439     EXPECT_EQ((int32_t)ret, (int32_t)0);
440     mockMsg.MakeDeviceOffline(&device);
441 }
442 
443 HWTEST_F(DslmTest, OnRequestDeviceSecLevelInfo_case3, TestSize.Level1)
444 {
445     DslmMsgInterfaceMock mockMsg;
446     DslmRequestCallbackMock mockCallback;
447 
448     EXPECT_CALL(mockMsg, IsMessengerReady(_)).Times(AtLeast(1));
449     EXPECT_CALL(mockMsg, GetDeviceOnlineStatus(_, _, _)).Times(AtLeast(1)).WillRepeatedly(Return(true));
450     EXPECT_CALL(mockMsg, SendMsgTo(_, _, _, _, _)).Times(AtLeast(1)).WillRepeatedly(Return(true));
451 
452     mutex mtx;
453     condition_variable cv;
454     int32_t cnt = 0;
455     const time_point<system_clock> start = system_clock::now();
456     const int32_t reqTimes = 3;
457 
458     uint32_t cookies[] = {0, 0x1234, 0x5678, 0xabcd};
459     uint32_t timeouts[] = {0, 1, 5, 9};
460 
__anon35389b7b0202(uint32_t cookie) 461     auto checkCookie = [&mtx, &cv, &cnt, &start, &cookies, &timeouts](uint32_t cookie) {
462         unique_lock<mutex> lck(mtx);
463         cnt++;
464         cv.notify_one();
465         time_point<system_clock> curr = system_clock::now();
466         auto cost = duration_cast<seconds>(curr - start).count();
467         EXPECT_EQ(cookie, cookies[cnt]);
468         EXPECT_EQ(cost, timeouts[cnt]);
469         return true;
470     };
471 
472     EXPECT_CALL(mockCallback, RequestCallback(Truly(checkCookie), ERR_TIMEOUT, _)).Times(Exactly(3));
473 
474     const DeviceIdentify device = {DEVICE_ID_MAX_LEN, {'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b'}};
475     RequestOption option;
476     for (int i = 1; i <= reqTimes; i++) {
477         option.timeout = timeouts[i];
478         int32_t ret =
479             OnRequestDeviceSecLevelInfo(&device, &option, i, cookies[i], DslmRequestCallbackMock::MockedCallback);
480         EXPECT_EQ((int32_t)ret, (int32_t)0);
481     }
482 
483     unique_lock<mutex> lck(mtx);
__anon35389b7b0302() 484     cv.wait(lck, [&cnt]() { return (cnt == reqTimes); });
485     mockMsg.MakeDeviceOffline(&device);
486 }
487 
488 /*
489 HWTEST_F(DslmTest, OnPeerMsgRequestInfoReceived_case1, TestSize.Level1)
490 {
491     const char *input = "{\"version\":65536,\"challenge\":\"0102030405060708\"}";
492     uint32_t len = strlen(input) + 1;
493 
494     const DeviceIdentify device = {DEVICE_ID_MAX_LEN, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}};
495 
496     DslmMsgInterfaceMock mockMsg;
497 
498     auto isSendResponseOut = [](const uint8_t *message) {
499         const string msg = string((char *)message);
500         EXPECT_EQ((int)msg.find("{\"message\":2,\"payload\":{"), 0);
501         EXPECT_GT((int)msg.find("\"version\":"), 0);
502         EXPECT_GT((int)msg.find("\"challenge\":"), 0);
503         EXPECT_GT((int)msg.find("\"type\":"), 0);
504         EXPECT_GT((int)msg.find("\"info\":"), 0);
505         return true;
506     };
507 
508     EXPECT_CALL(mockMsg, SendMsgTo(_, _, _, Truly(isSendResponseOut), _)).Times(Exactly(1));
509 
510     int32_t ret = OnPeerMsgRequestInfoReceived(&device, (const uint8_t *)input, len);
511     EXPECT_EQ(0, (int32_t)ret);
512 }
513 */
514 
515 HWTEST_F(DslmTest, OnPeerMsgResponseInfoReceived_case2, TestSize.Level1)
516 {
517     const char *input = "{\"version\":65536,\"type\":0,\"challenge\":\"EEFFFFFFFFCDABFF\",\"info\":"
518                         "\"MDAwMTAyMDMwNDA1MDYwNzA4MDkwQTBCMEMwRDBFMEYxMDExMTIxMzE0MTUxNkFBQkJDQ0RE\"}";
519     uint32_t len = strlen(input) + 1;
520 
521     DeviceIdentify device = {8, {'a', 'b', 'c', 'd', 'e', 'f', 'g'}};
522 
523     int32_t ret = OnPeerMsgResponseInfoReceived(&device, (const uint8_t *)input, len);
524     EXPECT_EQ(ERR_NOEXIST_DEVICE, (int32_t)ret);
525 }
526 
527 HWTEST_F(DslmTest, InitSelfDeviceSecureLevel_case1, TestSize.Level1)
528 {
529     const DeviceIdentify device = {DEVICE_ID_MAX_LEN, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}};
530     DslmDeviceInfo *info = GetDslmDeviceInfo(&device);
531     EXPECT_EQ(nullptr, info);
532 
533     DslmMsgInterfaceMock mockMsg;
534     mockMsg.MakeSelfDeviceId(&device);
535     mockMsg.MakeMsgLoopback();
536     EXPECT_CALL(mockMsg, GetSelfDeviceIdentify(_, _, _)).Times(AtLeast(1));
537     InitSelfDeviceSecureLevel();
538 
539     info = GetDslmDeviceInfo(&device);
540     ASSERT_NE(nullptr, info);
541 
542     BlockCheckDeviceStatus(&device, STATE_SUCCESS, 10000);
543     EXPECT_GE(info->credInfo.credLevel, (uint32_t)1);
544     mockMsg.MakeDeviceOffline(&device);
545 }
546 
547 HWTEST_F(DslmTest, InitSelfDeviceSecureLevel_case2, TestSize.Level1)
548 {
549     const DeviceIdentify device = {DEVICE_ID_MAX_LEN, {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}};
550 
551     DslmDeviceInfo *info = GetDslmDeviceInfo(&device);
552     EXPECT_EQ(nullptr, info);
553 
554     DslmMsgInterfaceMock mockMsg;
555     EXPECT_CALL(mockMsg, SendMsgTo(_, _, _, _, _)).Times(Exactly(6));
556     mockMsg.MakeDeviceOnline(&device);
557 
558     info = GetDslmDeviceInfo(&device);
559     ASSERT_NE(nullptr, info);
560     EXPECT_EQ((uint32_t)1, info->queryTimes);
561     EXPECT_EQ((uint32_t)STATE_WAITING_CRED_RSP, info->machine.currState);
562 
563     BlockCheckDeviceStatus(&device, STATE_SUCCESS, 5000);
564     EXPECT_EQ((uint32_t)STATE_FAILED, info->machine.currState);
565     EXPECT_LT((uint32_t)5, info->queryTimes);
566     mockMsg.MakeDeviceOffline(&device);
567 }
568 
569 HWTEST_F(DslmTest, InnerKitsTest_case1, TestSize.Level1)
570 {
571     DeviceIdentify device = {DEVICE_ID_MAX_LEN, {0}};
572 
573     DeviceSecurityInfo *info = NULL;
574     int32_t ret = RequestDeviceSecurityInfo(&device, NULL, &info);
575     EXPECT_EQ(ret, 0);
576     int32_t level = 0;
577     ret = GetDeviceSecurityLevelValue(info, &level);
578     FreeDeviceSecurityInfo(info);
579     EXPECT_EQ(ret, 0);
580     EXPECT_GE(level, 1);
581 }
582 
583 static int32_t g_cnt = 0;
584 static mutex g_mtx;
585 static condition_variable g_cv;
586 
TestDeviceSecurityInfoCallback(const DeviceIdentify * identify,struct DeviceSecurityInfo * info)587 void TestDeviceSecurityInfoCallback(const DeviceIdentify *identify, struct DeviceSecurityInfo *info)
588 {
589     unique_lock<mutex> lck(g_mtx);
590     int32_t level = 0;
591     int32_t ret = GetDeviceSecurityLevelValue(info, &level);
592     FreeDeviceSecurityInfo(info);
593     EXPECT_EQ(ret, 0);
594     EXPECT_GE(level, 1);
595     g_cnt++;
596     g_cv.notify_one();
597 }
598 
599 HWTEST_F(DslmTest, InnerKitsTest_case2, TestSize.Level1)
600 {
601     DeviceIdentify device = {DEVICE_ID_MAX_LEN, {0}};
602 
603     g_cnt = 0;
604     int ret = RequestDeviceSecurityInfoAsync(&device, NULL, TestDeviceSecurityInfoCallback);
605     EXPECT_EQ(ret, 0);
606 
607     ret = RequestDeviceSecurityInfoAsync(&device, NULL, TestDeviceSecurityInfoCallback);
608     EXPECT_EQ(ret, 0);
609 
610     ret = RequestDeviceSecurityInfoAsync(&device, NULL, TestDeviceSecurityInfoCallback);
611     EXPECT_EQ(ret, 0);
612 
613     unique_lock<mutex> lck(g_mtx);
__anon35389b7b0402() 614     g_cv.wait_for(lck, std::chrono::milliseconds(2000), []() { return (g_cnt == 3); });
615     EXPECT_EQ(g_cnt, 3);
616 }
617 } // namespace DslmUnitTest
618 } // namespace Security
619 } // namespace OHOS
620