• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 <gtest/gtest.h>
17 #include <chrono>
18 #include <thread>
19 #include "dhcp_logger.h"
20 #include "dhcp_socket.h"
21 #include "dhcp_function.h"
22 #include "dhcp_thread.h"
23 #include "securec.h"
24 #include "mock_system_func.h"
25 
26 DEFINE_DHCPLOG_DHCP_LABEL("DhcpThreadTest");
27 
28 using namespace testing::ext;
29 using namespace OHOS::DHCP;
30 namespace OHOS {
31 class DhcpThreadTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {}
TearDownTestCase()35     static void TearDownTestCase()
36     {}
SetUp()37     virtual void SetUp()
38     {}
TearDown()39     virtual void TearDown()
40     {}
41 };
42 
43 HWTEST_F(DhcpThreadTest, PostSyncTask_SUCCESS, TestSize.Level1)
44 {
45     DHCP_LOGE("enter PostSyncTask_SUCCESS");
46     DhcpThread dhcpThread("TestThread");
__anonf70cce9b0102() 47     bool result = dhcpThread.PostSyncTask([]() {
48         // Task implementation
49     });
50 
51     EXPECT_TRUE(result);
52 }
53 
54 HWTEST_F(DhcpThreadTest, PostAsyncTask_SUCCESS, TestSize.Level1)
55 {
56     DHCP_LOGE("enter PostAsyncTask_SUCCESS");
57     DhcpThread dhcpThread("TestThread");
__anonf70cce9b0202() 58     bool result = dhcpThread.PostAsyncTask([]() {
59         // Task implementation
60     });
61 
62     EXPECT_TRUE(result);
63 }
64 
65 HWTEST_F(DhcpThreadTest, PostAsyncTaskWithName_SUCCESS, TestSize.Level1)
66 {
67     DHCP_LOGE("enter PostAsyncTaskWithName_SUCCESS");
68     DhcpThread dhcpThread("TestThread");
__anonf70cce9b0302() 69     bool result = dhcpThread.PostAsyncTask([]() {
70         // Task implementation
71     }, "TaskName");
72 
73     EXPECT_TRUE(result);
74 }
75 
76 HWTEST_F(DhcpThreadTest, PostSyncTimeOutTask_001, TestSize.Level1)
77 {
78     DHCP_LOGE("enter PostSyncTimeOutTask_001");
79     DhcpThread dhcpThread("TestThread");
80     int timeOut = 500;
__anonf70cce9b0402() 81     bool result = dhcpThread.PostSyncTimeOutTask([]() -> int32_t {
82         std::this_thread::sleep_for(std::chrono::milliseconds(300));
83         return 0;
84     }, timeOut);
85 
86     EXPECT_TRUE(result);
87 }
88 
89 HWTEST_F(DhcpThreadTest, PostSyncTimeOutTask_002, TestSize.Level1)
90 {
91     DHCP_LOGE("enter PostSyncTimeOutTask_002");
92     DhcpThread dhcpThread("TestThread");
93     int timeOut = 500;
__anonf70cce9b0502() 94     bool result = dhcpThread.PostSyncTimeOutTask([]() -> int32_t {
95         std::this_thread::sleep_for(std::chrono::milliseconds(600));
96         return 0;
97     }, timeOut);
98 
99     EXPECT_FALSE(result);
100 }
101 
102 HWTEST_F(DhcpThreadTest, RemoveAsyncTask_SUCCESS, TestSize.Level1)
103 {
104     DHCP_LOGE("enter RemoveAsyncTask_SUCCESS");
105     DhcpThread dhcpThread("TestThread");
__anonf70cce9b0602() 106     bool result = dhcpThread.PostAsyncTask([]() {
107         // Task implementation
108     }, "TaskName");
109     EXPECT_TRUE(result);
110     dhcpThread.RemoveAsyncTask("TaskName");
111 }
112 
113 HWTEST_F(DhcpThreadTest, Register_SUCCESS, TestSize.Level1)
114 {
115     DHCP_LOGE("enter Register_SUCCESS");
116     DhcpTimer *dhcpTimer = DhcpTimer::GetInstance();
117     uint32_t timerId;
__anonf70cce9b0702() 118     EnumErrCode result = dhcpTimer->Register([]() {
119         // Timer callback implementation
120     }, timerId);
121 
122     EXPECT_EQ(result, EnumErrCode::DHCP_OPT_SUCCESS);
123 }
124 
125 HWTEST_F(DhcpThreadTest, UnRegister_SUCCESS, TestSize.Level1)
126 {
127     DHCP_LOGE("enter UnRegister_SUCCESS");
128     DhcpTimer *dhcpTimer = DhcpTimer::GetInstance();
129     uint32_t timerId;
__anonf70cce9b0802() 130     EnumErrCode result = dhcpTimer->Register([]() {
131         // Timer callback implementation
132     }, timerId);
133     EXPECT_EQ(result, EnumErrCode::DHCP_OPT_SUCCESS);
134     dhcpTimer->UnRegister(timerId);
135 }
136 
137 HWTEST_F(DhcpThreadTest, GetInstance_SUCCESS, TestSize.Level1)
138 {
139     DhcpTimer *dhcpTimer = DhcpTimer::GetInstance();
140     EXPECT_NE(dhcpTimer, nullptr);
141 }
142 
143 HWTEST_F(DhcpThreadTest, RegisterWithInterval_SUCCESS, TestSize.Level1)
144 {
145     DhcpTimer *dhcpTimer = DhcpTimer::GetInstance();
146     uint32_t timerId;
__anonf70cce9b0902() 147     EnumErrCode result = dhcpTimer->Register([]() {
148     }, timerId, 5000);
149 
150     EXPECT_EQ(result, EnumErrCode::DHCP_OPT_SUCCESS);
151 }
152 }