• 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 "daemon_uart_test.h"
17 
18 #include <chrono>
19 #include <random>
20 #include <thread>
21 
22 using namespace std::chrono;
23 using namespace std::chrono_literals;
24 using namespace testing::ext;
25 using namespace testing;
26 
27 namespace Hdc {
28 class HdcDaemonUARTTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34 
35     constexpr static int PAIR = 2;
36     constexpr static int SENDER = 0;
37     constexpr static int RECEIVER = 1;
38 
39     std::default_random_engine rnd;
40     class MockHdcDaemon : public HdcDaemon {
MockHdcDaemon()41         MockHdcDaemon() : HdcDaemon(false) {};
42         MOCK_METHOD4(MallocSession, HSession(bool, const ConnType, void *, uint32_t));
43         MOCK_METHOD4(PushAsyncMessage,
44                      void(const uint32_t, const uint8_t, const void *, const int));
45         MOCK_METHOD1(FreeSession, void(const uint32_t));
46         MOCK_METHOD3(AdminSession, HSession(const uint8_t, const uint32_t, HSession));
47     } mockDaemon;
48     const std::string nullDevPath = "/dev/null";
49 
50     class MockBaseInterface : public ExternInterface {
51     public:
52         MOCK_METHOD4(StartWorkThread, int(uv_loop_t *, uv_work_cb, uv_after_work_cb, void *));
53         MOCK_METHOD2(TryCloseHandle, void(const uv_handle_t *, uv_close_cb));
54         MOCK_METHOD3(TimerUvTask, bool(uv_loop_t *, void *, uv_timer_cb));
55     } mockInterface;
56 
57     class MockHdcDaemonUART : public HdcDaemonUART {
MockHdcDaemonUART(HdcDaemon & daemonIn,ExternInterface & externInterface=HdcUARTBase::defaultInterface)58         explicit MockHdcDaemonUART(HdcDaemon &daemonIn,
59                                    ExternInterface &externInterface = HdcUARTBase::defaultInterface)
60             : HdcDaemonUART(daemonIn, externInterface)
61         {
62         }
63         MOCK_METHOD0(CloseUartDevice, int(void));
64         MOCK_METHOD0(OpenUartDevice, int(void));
65         MOCK_METHOD0(LoopUARTRead, int(void));
66         MOCK_METHOD0(LoopUARTWrite, int(void));
67         MOCK_METHOD0(PrepareBufForRead, int(void));
68         MOCK_METHOD0(WatcherTimerCallBack, void(void));
69         MOCK_METHOD0(DeamonReadThread, void(void));
70         MOCK_METHOD0(DeamonWriteThread, void(void));
71         MOCK_METHOD1(IsSendReady, bool(HSession));
72         MOCK_METHOD1(PrepareNewSession, HSession(uint32_t));
73         MOCK_METHOD2(PackageProcess, size_t(vector<uint8_t> &data, HSession hSession));
74         MOCK_METHOD3(SendUARTDev, size_t(HSession, uint8_t *, const size_t));
75         MOCK_METHOD3(UartSendToHdcStream, bool(HSession, uint8_t *, size_t));
76         MOCK_METHOD4(ValidateUartPacket,
77                      RetErrCode(vector<uint8_t> &, uint32_t &, uint32_t &, size_t &));
78     } mockDaemonUART;
79 
HdcDaemonUARTTest()80     HdcDaemonUARTTest() : mockDaemonUART(mockDaemon, mockInterface) {};
81 
MockUartDevSender(int fd,uint8_t * mockData,size_t length,milliseconds timeout,bool closeThreadAfterSend=true)82     void MockUartDevSender(int fd, uint8_t *mockData, size_t length, milliseconds timeout,
83                            bool closeThreadAfterSend = true)
84     {
85         std::this_thread::sleep_for(timeout);
86         ASSERT_EQ(write(fd, mockData, length), signed(length));
87         if (closeThreadAfterSend) {
88             std::this_thread::sleep_for(1000ms);
89             mockDaemonUART.isAlive = false; // break DeamonReadThread while
90             close(fd);                      // break select
91         }
92     }
93 
94     // input data(high level), append the head , prepare mockDaemonUART
PrepareDeamonReadThreadData(std::vector<uint8_t> & data,int fds[PAIR])95     void PrepareDeamonReadThreadData(std::vector<uint8_t> &data, int fds[PAIR])
96     {
97         ON_CALL(mockDaemonUART, DeamonReadThread).WillByDefault([&]() {
98             return mockDaemonUART.HdcDaemonUART::DeamonReadThread();
99         });
100         EXPECT_CALL(mockDaemonUART, DeamonReadThread).Times(AnyNumber());
101         ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds), 0);
102 
103         // copy to raw data
104         data.resize(sizeof(UartHead) + data.size());
105 
106         mockDaemonUART.isAlive = true;
107         mockDaemonUART.dataReadBuf.resize(MAX_SIZE_IOBUF);
108         mockDaemonUART.uartHandle = fds[RECEIVER];
109     }
110     HdcSession mySession;
111 };
112 
SetUpTestCase()113 void HdcDaemonUARTTest::SetUpTestCase()
114 {
115 #ifdef UT_DEBUG
116     Hdc::Base::SetLogLevel(LOG_ALL);
117 #else
118     Hdc::Base::SetLogLevel(LOG_OFF);
119 #endif
120 }
121 
TearDownTestCase()122 void HdcDaemonUARTTest::TearDownTestCase() {}
123 
SetUp()124 void HdcDaemonUARTTest::SetUp() {}
125 
TearDown()126 void HdcDaemonUARTTest::TearDown() {}
127 
128 /*
129  * @tc.name: HdcDaemonUART
130  * @tc.desc: Check the behavior of the HdcDaemonUART function
131  * @tc.type: FUNC
132  */
133 HWTEST_F(HdcDaemonUARTTest, HdcDaemonUART, TestSize.Level1)
134 {
135     const std::string testDevPath = "testDevPath";
136     HdcDaemon daemon(false);
137     HdcDaemonUART daemonUART(daemon);
138     EXPECT_EQ(daemonUART.uartHandle, -1);
139     EXPECT_EQ(daemonUART.currentSessionId, 0u);
140     EXPECT_EQ(daemonUART.isAlive, false);
141     EXPECT_EQ(daemonUART.dataReadBuf.size(), 0u);
142     EXPECT_EQ(&daemonUART.daemon, &daemon);
143     EXPECT_EQ(daemonUART.devPath.empty(), true);
144 }
145 
146 /*
147  * @tc.name: Initial
148  * @tc.desc: Check the behavior of the Initial function
149  * @tc.type: FUNC
150  */
151 HWTEST_F(HdcDaemonUARTTest, Initial, TestSize.Level1)
152 {
153     const std::string testDevPath = "testDevPath";
154     HdcDaemon daemon(false);
155     HdcDaemonUART daemonUARTFailed(daemon);
156     EXPECT_EQ(daemonUARTFailed.Initial(testDevPath), -1);
157 
158     // timeout + repeate
159     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).Times(1);
160     EXPECT_EQ(mockDaemonUART.Initial(nullDevPath), 0);
161     EXPECT_EQ(mockDaemonUART.checkSerialPort.data, &mockDaemonUART);
162     std::this_thread::sleep_for(100ms);
163 
164     // we only test once , because we have not stop the time ,so uv_run will NOT return 0;
165     EXPECT_NE(uv_run(&mockDaemon.loopMain, UV_RUN_NOWAIT), 0);
166 
167     // if we wait one more time ?
168     std::this_thread::sleep_for(2000ms);
169     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).Times(1);
170     EXPECT_NE(uv_run(&mockDaemon.loopMain, UV_RUN_NOWAIT), 0);
171     EXPECT_CALL(mockInterface, TryCloseHandle).Times(1);
172 
173     EXPECT_EQ(uv_timer_stop(&mockDaemonUART.checkSerialPort), 0);
174 }
175 
176 /*
177  * @tc.name: WatcherTimerCallBack
178  * @tc.desc: Check the behavior of the WatcherTimerCallBack function
179  * @tc.type: FUNC
180  */
181 HWTEST_F(HdcDaemonUARTTest, WatcherTimerCallBack, TestSize.Level1)
182 {
__anond4864eb80202null183     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).WillRepeatedly(Invoke([&] {
184         mockDaemonUART.HdcDaemonUART::WatcherTimerCallBack();
185     }));
186 
187     // case 1 isAlive not valid
188     mockDaemonUART.isAlive = true;
189     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(0);
190     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(0);
191     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(0);
192     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(0);
193     EXPECT_CALL(mockDaemonUART, LoopUARTWrite).Times(0);
194 
195     mockDaemonUART.WatcherTimerCallBack();
196     EXPECT_EQ(mockDaemonUART.isAlive, true);
197 
198     // case 2 uartHandle not valid
199     mockDaemonUART.isAlive = false;
200     mockDaemonUART.uartHandle = -1;
201     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(0);
202     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(1);
203     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(1);
204     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(1);
205     EXPECT_CALL(mockDaemonUART, LoopUARTWrite).Times(1);
206     mockDaemonUART.WatcherTimerCallBack();
207     EXPECT_EQ(mockDaemonUART.isAlive, true);
208 
209     // case 3 uartHandle valid
210     mockDaemonUART.isAlive = false;
211     mockDaemonUART.uartHandle = 0; // 0 is a valid fd !
212     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(1);
213     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(1);
214     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(1);
215     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(1);
216     EXPECT_CALL(mockDaemonUART, LoopUARTWrite).Times(1);
217     mockDaemonUART.WatcherTimerCallBack();
218     EXPECT_EQ(mockDaemonUART.isAlive, true);
219 }
220 /*
221  * @tc.name: UvWatchTimer
222  * @tc.desc: Check the behavior of the ConnectDaemonByUart function
223  * @tc.type: FUNC
224  */
225 HWTEST_F(HdcDaemonUARTTest, UvWatchTimer, TestSize.Level1)
226 {
227     uv_timer_t handle;
228 
229     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).Times(0);
230     HdcDaemonUART::UvWatchTimer(nullptr);
231 
232     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).Times(0);
233     handle.data = nullptr;
234     HdcDaemonUART::UvWatchTimer(&handle);
235 
236     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).Times(1);
237     handle.data = &mockDaemonUART;
238     HdcDaemonUART::UvWatchTimer(&handle);
239 }
240 
241 /*
242  * @tc.name: WatcherTimerCallBack
243  * @tc.desc: Check the behavior of the WatcherTimerCallBack function
244  * @tc.type: FUNC
245  */
246 HWTEST_F(HdcDaemonUARTTest, WatcherTimerCallBackDeviceFailed, TestSize.Level1)
247 {
__anond4864eb80302null248     ON_CALL(mockDaemonUART, WatcherTimerCallBack).WillByDefault([&] {
249         mockDaemonUART.HdcDaemonUART::WatcherTimerCallBack();
250     });
251     EXPECT_CALL(mockDaemonUART, WatcherTimerCallBack).Times(AnyNumber());
252 
253     // case 1 CloseUartDevice failed
254     mockDaemonUART.isAlive = false;
255     mockDaemonUART.uartHandle = 0;
256     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(1).WillOnce(Return(-1));
257     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(0);
258     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(0);
259     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(0);
260     mockDaemonUART.WatcherTimerCallBack();
261     EXPECT_EQ(mockDaemonUART.isAlive, false);
262 
263     // case 2 OpenUartDevice failed
264     mockDaemonUART.isAlive = false;
265     mockDaemonUART.uartHandle = 0;
266     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(1);
267     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(1).WillOnce(Return(-1));
268     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(0);
269     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(0);
270     mockDaemonUART.WatcherTimerCallBack();
271     EXPECT_EQ(mockDaemonUART.isAlive, false);
272 
273     // case 3 PrepareBufForRead failed
274     mockDaemonUART.isAlive = false;
275     mockDaemonUART.uartHandle = 0;
276     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(1);
277     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(1);
278     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(1).WillOnce(Return(-1));
279     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(0);
280     mockDaemonUART.WatcherTimerCallBack();
281     EXPECT_EQ(mockDaemonUART.isAlive, false);
282 
283     // case 4 LoopUARTRead failed
284     mockDaemonUART.isAlive = false;
285     mockDaemonUART.uartHandle = 0;
286     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(1);
287     EXPECT_CALL(mockDaemonUART, OpenUartDevice).Times(1);
288     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(1);
289     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(1).WillOnce(Return(-1));
290     mockDaemonUART.WatcherTimerCallBack();
291     EXPECT_EQ(mockDaemonUART.isAlive, false);
292 }
293 
294 /*
295  * @tc.name: CloseUartDevice
296  * @tc.desc: Check the behavior of the CloseUartDevice function
297  * @tc.type: FUNC
298  */
299 HWTEST_F(HdcDaemonUARTTest, CloseUartDevice, TestSize.Level1)
300 {
301     HdcDaemon daemon(false);
302     HdcDaemonUART daemonUART(daemon);
303 
304     daemonUART.uartHandle = -1;
305     EXPECT_LT(daemonUART.CloseUartDevice(), 0);
306     EXPECT_EQ(daemonUART.uartHandle, -1);
307 
308     daemonUART.uartHandle = open(nullDevPath.c_str(), O_RDWR);
309     EXPECT_EQ(daemonUART.CloseUartDevice(), 0);
310     EXPECT_EQ(daemonUART.uartHandle, -1);
311 }
312 
313 /*
314  * @tc.name: LoopUARTRead
315  * @tc.desc: Check the behavior of the LoopUARTRead function
316  * @tc.type: FUNC
317  */
318 HWTEST_F(HdcDaemonUARTTest, LoopUARTRead, TestSize.Level1)
319 {
__anond4864eb80402null320     ON_CALL(mockDaemonUART, LoopUARTRead).WillByDefault([&] {
321         return mockDaemonUART.HdcDaemonUART::LoopUARTRead();
322     });
323     EXPECT_CALL(mockDaemonUART, LoopUARTRead).Times(AnyNumber());
324     EXPECT_CALL(mockDaemonUART, DeamonReadThread).Times(1);
325     EXPECT_EQ(mockDaemonUART.LoopUARTRead(), 0);
326     // wait thread work
327     std::this_thread::sleep_for(1000ms);
328 }
329 
330 /*
331  * @tc.name: LoopUARTWrite
332  * @tc.desc: Check the behavior of the LoopUARTWrite function
333  * @tc.type: FUNC
334  */
335 HWTEST_F(HdcDaemonUARTTest, LoopUARTWrite, TestSize.Level1)
336 {
__anond4864eb80502null337     ON_CALL(mockDaemonUART, LoopUARTWrite).WillByDefault([&] {
338         return mockDaemonUART.HdcDaemonUART::LoopUARTWrite();
339     });
340     EXPECT_CALL(mockDaemonUART, LoopUARTWrite).Times(AnyNumber());
341     EXPECT_CALL(mockDaemonUART, DeamonWriteThread).Times(1);
342     EXPECT_EQ(mockDaemonUART.LoopUARTWrite(), 0);
343     // wait thread work
344     std::this_thread::sleep_for(1000ms);
345 }
346 
347 /*
348  * @tc.name: IsSendReady
349  * @tc.desc: Check the behavior of the IsSendReady function
350  * @tc.type: FUNC
351  */
352 HWTEST_F(HdcDaemonUARTTest, IsSendReady, TestSize.Level1)
353 {
__anond4864eb80602(HSession hSession) 354     ON_CALL(mockDaemonUART, IsSendReady).WillByDefault([&](HSession hSession) {
355         return mockDaemonUART.HdcDaemonUART::IsSendReady(hSession);
356     });
357     EXPECT_CALL(mockDaemonUART, IsSendReady).Times(AtLeast(1));
358 
359     HdcSession mySession;
360     HdcUART myUART;
361     mySession.hUART = &myUART;
362 
363     mockDaemonUART.isAlive = true;
364     mySession.isDead = false;
365     mySession.hUART->resetIO = false;
366     mockDaemonUART.uartHandle = 0;
367     EXPECT_EQ(mockDaemonUART.IsSendReady(&mySession), true);
368 
369     mockDaemonUART.isAlive = false;
370     EXPECT_EQ(mockDaemonUART.IsSendReady(&mySession), false);
371     mockDaemonUART.isAlive = true;
372 
373     mySession.isDead = true;
374     EXPECT_EQ(mockDaemonUART.IsSendReady(&mySession), false);
375     mySession.isDead = false;
376 
377     mySession.hUART->resetIO = true;
378     EXPECT_EQ(mockDaemonUART.IsSendReady(&mySession), false);
379     mySession.hUART->resetIO = false;
380 
381     mockDaemonUART.uartHandle = -1;
382     EXPECT_EQ(mockDaemonUART.IsSendReady(&mySession), false);
383     mockDaemonUART.uartHandle = 0;
384 }
385 
386 /*
387  * @tc.name: PrepareBufForRead
388  * @tc.desc: Check the behavior of the PrepareBufForRead function
389  * @tc.type: FUNC
390  */
391 HWTEST_F(HdcDaemonUARTTest, PrepareBufForRead, TestSize.Level1)
392 {
__anond4864eb80702() 393     ON_CALL(mockDaemonUART, PrepareBufForRead).WillByDefault([&]() {
394         return mockDaemonUART.HdcDaemonUART::PrepareBufForRead();
395     });
396     EXPECT_CALL(mockDaemonUART, PrepareBufForRead).Times(AtLeast(1));
397 
398     mockDaemonUART.PrepareBufForRead();
399     EXPECT_EQ(mockDaemonUART.PrepareBufForRead(), RET_SUCCESS);
400 }
401 
402 /*
403  * @tc.name: PrepareNewSession
404  * @tc.desc: Check the behavior of the PrepareNewSession function
405  * @tc.type: FUNC
406  */
407 HWTEST_F(HdcDaemonUARTTest, PrepareNewSession, TestSize.Level1)
408 {
409     constexpr uint32_t sessionTestId = 1234;
__anond4864eb80802(uint32_t sessionId) 410     ON_CALL(mockDaemonUART, PrepareNewSession).WillByDefault([&](uint32_t sessionId) {
411         return mockDaemonUART.HdcDaemonUART::PrepareNewSession(sessionId);
412     });
413     EXPECT_CALL(mockDaemonUART, PrepareNewSession).Times(AnyNumber());
414 
415     HdcSession mySession;
416 
417     mockDaemonUART.currentSessionId = 0;
418     EXPECT_CALL(mockDaemon, MallocSession).Times(1).WillOnce(Return(&mySession));
419     EXPECT_CALL(mockInterface, StartWorkThread).Times(1);
420     EXPECT_CALL(mockInterface, TimerUvTask).Times(1);
421     EXPECT_EQ(mockDaemonUART.PrepareNewSession(sessionTestId), &mySession);
422 
423     mockDaemonUART.currentSessionId = sessionTestId;
424     EXPECT_CALL(mockDaemon, MallocSession).Times(1).WillOnce(Return(&mySession));
425     EXPECT_CALL(mockDaemon, PushAsyncMessage).Times(1);
426     EXPECT_CALL(mockInterface, StartWorkThread).Times(1);
427     EXPECT_CALL(mockInterface, TimerUvTask).Times(1);
428     EXPECT_EQ(mockDaemonUART.PrepareNewSession(sessionTestId), &mySession);
429 
430     mockDaemonUART.currentSessionId = sessionTestId;
431     EXPECT_CALL(mockDaemon, MallocSession).Times(1).WillOnce(Return(nullptr));
432     EXPECT_EQ(mockDaemonUART.PrepareNewSession(sessionTestId), nullptr);
433 }
434 
435 /*
436  * @tc.name: DeamonReadThread
437  * @tc.desc: Check the behavior of the DeamonReadThread function
438  * @tc.type: FUNC
439  */
440 HWTEST_F(HdcDaemonUARTTest, DeamonReadThread, TestSize.Level1)
441 {
442     std::vector<uint8_t> data;
443     int fds[PAIR];
444 
445     // after this call , data will include head and data
446     PrepareDeamonReadThreadData(data, fds);
447 
448     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(1).WillOnce(Return(sizeof(UartHead)));
449 
450     ASSERT_EQ(mockDaemonUART.isAlive, true);
451     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
452     MockUartDevSender(fds[SENDER], data.data(), data.size(), 0ms, true);
453     deamonRead.join();
454     EXPECT_EQ(mockDaemonUART.isAlive, false);
455 
456     close(fds[SENDER]);
457     close(fds[RECEIVER]);
458 }
459 
460 /*
461  * @tc.name: DeamonReadThread
462  * @tc.desc: Check the behavior of the DeamonReadThread function
463  * @tc.type: FUNC
464  */
465 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadHaveData, TestSize.Level1)
466 {
467     constexpr int fewDataSize = 4;
468     std::vector<uint8_t> data(fewDataSize);
469     int fds[PAIR];
470 
471     // after this call , data will include head and data
472     PrepareDeamonReadThreadData(data, fds);
473 
474     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(1).WillOnce(Return(sizeof(UartHead)));
475 
476     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
477     MockUartDevSender(fds[SENDER], data.data(), data.size(), 0ms, true);
478     deamonRead.join();
479     EXPECT_EQ(mockDaemonUART.isAlive, false);
480 
481     close(fds[SENDER]);
482     close(fds[RECEIVER]);
483 }
484 
485 /*
486  * @tc.name: DeamonReadThread
487  * @tc.desc: Check the behavior of the DeamonReadThread function
488  * @tc.type: FUNC
489  */
490 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadDelay, TestSize.Level1)
491 {
492     constexpr int fewDataSize = 4;
493     std::vector<uint8_t> data(fewDataSize);
494     int fds[PAIR];
495 
496     // after this call , data will include head and data
497     PrepareDeamonReadThreadData(data, fds);
498 
499     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(1).WillOnce(Return(sizeof(UartHead)));
500 
501     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
502     MockUartDevSender(fds[SENDER], data.data(), data.size(), 1000ms, true);
503     deamonRead.join();
504     EXPECT_EQ(mockDaemonUART.isAlive, false);
505 
506     close(fds[SENDER]);
507     close(fds[RECEIVER]);
508 }
509 
510 /*
511  * @tc.name: DeamonReadThread
512  * @tc.desc: Check the behavior of the DeamonReadThread function
513  * @tc.type: FUNC
514  */
515 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadMorePackage, TestSize.Level1)
516 {
517     constexpr int fewDataSize = 4;
518     constexpr int PackageNumber = 3;
519     std::vector<uint8_t> data(fewDataSize);
520     int fds[PAIR];
521 
522     // after this call , data will include head and data
523     PrepareDeamonReadThreadData(data, fds);
524     // we need send much more package
525     data.resize(data.size() * PackageNumber);
526 
527     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(1).WillOnce(Return(sizeof(UartHead)));
528 
529     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
530     MockUartDevSender(fds[SENDER], data.data(), data.size(), 0ms, true);
531     deamonRead.join();
532     EXPECT_EQ(mockDaemonUART.isAlive, false);
533 
534     close(fds[SENDER]);
535     close(fds[RECEIVER]);
536 }
537 
538 /*
539  * @tc.name: DeamonReadThread
540  * @tc.desc: Check the behavior of the DeamonReadThread function
541  * @tc.type: FUNC
542  */
543 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadHaveLargeData, TestSize.Level1)
544 {
545     std::vector<uint8_t> data(MAX_SIZE_IOBUF);
546     int fds[PAIR];
547 
548     // after this call , data will include head and data
549     PrepareDeamonReadThreadData(data, fds);
550 
551     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(4);
552 
553     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
554     MockUartDevSender(fds[SENDER], data.data(), data.size(), 0ms, true);
555     deamonRead.join();
556     EXPECT_EQ(mockDaemonUART.isAlive, false);
557 
558     close(fds[SENDER]);
559     close(fds[RECEIVER]);
560 }
561 
562 /*
563  * @tc.name: DeamonReadThread
564  * @tc.desc: Check the behavior of the DeamonReadThread function
565  * @tc.type: FUNC
566  */
567 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadMoreRead, TestSize.Level1)
568 {
569     constexpr int firstReadSize = 10;
570     constexpr int secondReadSize = 20;
571     constexpr int thirdReadSize = 30;
572     std::vector<uint8_t> data(firstReadSize + secondReadSize + thirdReadSize);
573     int fds[PAIR];
574 
575     // after this call , data will include head and data
576     PrepareDeamonReadThreadData(data, fds);
577 
578     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(AnyNumber());
579 
580     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
581 
582     MockUartDevSender(fds[SENDER], data.data(), sizeof(UartHead), 1000ms, false);
583     MockUartDevSender(fds[SENDER], data.data(), firstReadSize, 1000ms, false);
584     MockUartDevSender(fds[SENDER], data.data(), secondReadSize, 1000ms, false);
585     MockUartDevSender(fds[SENDER], data.data(), thirdReadSize, 1000ms, true);
586     EXPECT_EQ(mockDaemonUART.isAlive, false);
587 
588     deamonRead.join();
589     EXPECT_EQ(mockDaemonUART.isAlive, false);
590 
591     close(fds[SENDER]);
592     close(fds[RECEIVER]);
593 }
594 
595 /*
596  * @tc.name: DeamonReadThread
597  * @tc.desc: Check the behavior of the DeamonReadThread function
598  * @tc.type: FUNC
599  */
600 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadPacketFailed, TestSize.Level1)
601 {
602     std::vector<uint8_t> data;
603     int fds[PAIR];
604 
605     // after this call , data will include head and data
606     PrepareDeamonReadThreadData(data, fds);
607 
608     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(1).WillOnce(Return(sizeof(UartHead)));
609 
610     ASSERT_EQ(mockDaemonUART.isAlive, true);
611     std::thread deamonRead(&MockHdcDaemonUART::DeamonReadThread, &mockDaemonUART);
612 
613     MockUartDevSender(fds[SENDER], data.data(), data.size(), 0ms, true);
614     deamonRead.join();
615     EXPECT_EQ(mockDaemonUART.isAlive, false);
616 
617     close(fds[SENDER]);
618     close(fds[RECEIVER]);
619 }
620 
621 /*
622  * @tc.name: DeamonReadThread
623  * @tc.desc: Check the behavior of the DeamonReadThread function
624  * @tc.type: FUNC
625  */
626 HWTEST_F(HdcDaemonUARTTest, DeamonReadThreadDispatchFailed, TestSize.Level1)
627 {
628     std::vector<uint8_t> data;
629     int fds[PAIR];
630 
631     // after this call , data will include head and data
632     PrepareDeamonReadThreadData(data, fds);
633 
634     EXPECT_CALL(mockDaemonUART, PackageProcess).Times(0);
635 
636     ASSERT_EQ(mockDaemonUART.isAlive, true);
637 
638     // will not set isAlive false , wait DeamonReadThread exit first
639     MockUartDevSender(fds[SENDER], data.data(), data.size(), 0ms, false);
640 
641     EXPECT_EQ(mockDaemonUART.isAlive, true);
642 
643     close(fds[SENDER]);
644     close(fds[RECEIVER]);
645 }
646 
647 /*
648  * @tc.name: ResetOldSession
649  * @tc.desc: Check the behavior of the ResetOldSession function
650  * @tc.type: FUNC
651  */
652 HWTEST_F(HdcDaemonUARTTest, ResetOldSession, TestSize.Level1)
653 {
654     constexpr uint32_t sessionId = 1234;
655     HdcSession mySession;
656     HdcUART myUART;
657     mySession.hUART = &myUART;
658 
659     EXPECT_CALL(mockDaemon, AdminSession(OP_QUERY, sessionId, nullptr))
660         .Times(1)
661         .WillOnce(Return(&mySession));
662     EXPECT_CALL(mockDaemon, FreeSession).Times(1);
663     mockDaemonUART.ResetOldSession(sessionId);
664     EXPECT_EQ(mySession.hUART->resetIO, true);
665     mySession.hUART->resetIO = false;
666 
667     EXPECT_CALL(mockDaemon, AdminSession(OP_QUERY, sessionId, nullptr))
668         .Times(1)
669         .WillOnce(Return(nullptr));
670     mockDaemonUART.ResetOldSession(sessionId);
671     EXPECT_EQ(mySession.hUART->resetIO, false);
672 }
673 
674 /*
675  * @tc.name: OnTransferError
676  * @tc.desc: Check the behavior of the OnTransferError function
677  * @tc.type: FUNC
678  */
679 HWTEST_F(HdcDaemonUARTTest, OnTransferError, TestSize.Level1)
680 {
681     HdcSession mySession;
682     HdcUART myUART;
683     mySession.hUART = &myUART;
684     EXPECT_CALL(mockDaemon, FreeSession).Times(1);
685     mockDaemonUART.OnTransferError(&mySession);
686 }
687 
688 /*
689  * @tc.name: GetSession
690  * @tc.desc: Check the behavior of the GetSession function
691  * @tc.type: FUNC
692  */
693 HWTEST_F(HdcDaemonUARTTest, GetSession, TestSize.Level1)
694 {
695     constexpr uint32_t sessionId = 1234;
696     HdcSession mySession;
697     HdcUART myUART;
698     mySession.hUART = &myUART;
699     EXPECT_CALL(mockDaemon, AdminSession(OP_QUERY, sessionId, nullptr))
700         .Times(1)
701         .WillOnce(Return(&mySession));
702     mockDaemonUART.GetSession(sessionId, false);
703 }
704 
705 /*
706  * @tc.name: OnNewHandshakeOK
707  * @tc.desc: Check the behavior of the OnNewHandshakeOK function
708  * @tc.type: FUNC
709  */
710 HWTEST_F(HdcDaemonUARTTest, OnNewHandshakeOK, TestSize.Level1)
711 {
712     uint32_t sessionId = 1234;
713     mockDaemonUART.OnNewHandshakeOK(sessionId);
714     EXPECT_EQ(mockDaemonUART.currentSessionId, sessionId);
715 
716     uint32_t rndId = rnd() % 100;
717     mockDaemonUART.OnNewHandshakeOK(rndId);
718     EXPECT_EQ(mockDaemonUART.currentSessionId, rndId);
719 }
720 
721 /*
722  * @tc.name: Stop
723  * @tc.desc: Check the behavior of the Stop function
724  * @tc.type: FUNC
725  */
726 HWTEST_F(HdcDaemonUARTTest, Stop, TestSize.Level1)
727 {
728     mockDaemonUART.checkSerialPort.data = this;
729     EXPECT_CALL(mockInterface, TryCloseHandle(reinterpret_cast<const uv_handle_t *>(
730                                                   &mockDaemonUART.checkSerialPort),
731                                               nullptr))
732         .Times(1);
733     EXPECT_CALL(mockDaemonUART, CloseUartDevice).Times(1);
734     mockDaemonUART.Stop();
735 }
736 } // namespace Hdc
737