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 18 #include "dhcp_logger.h" 19 #include "dhcp_socket.h" 20 #include "dhcp_function.h" 21 #include "dhcp_thread.h" 22 #include "securec.h" 23 #include "mock_system_func.h" 24 25 DEFINE_DHCPLOG_DHCP_LABEL("DhcpThreadTest"); 26 27 using namespace testing::ext; 28 using namespace OHOS::DHCP; 29 namespace OHOS { 30 class DhcpThreadTest : public testing::Test { 31 public: SetUpTestCase()32 static void SetUpTestCase() 33 {} TearDownTestCase()34 static void TearDownTestCase() 35 {} SetUp()36 virtual void SetUp() 37 {} TearDown()38 virtual void TearDown() 39 {} 40 }; 41 42 HWTEST_F(DhcpThreadTest, PostSyncTask_SUCCESS, TestSize.Level1) 43 { 44 DHCP_LOGE("enter PostSyncTask_SUCCESS"); 45 DhcpThread dhcpThread("TestThread"); __anonfd83fbbd0102() 46 bool result = dhcpThread.PostSyncTask([]() { 47 // Task implementation 48 }); 49 50 EXPECT_TRUE(result); 51 } 52 53 HWTEST_F(DhcpThreadTest, PostAsyncTask_SUCCESS, TestSize.Level1) 54 { 55 DHCP_LOGE("enter PostAsyncTask_SUCCESS"); 56 DhcpThread dhcpThread("TestThread"); __anonfd83fbbd0202() 57 bool result = dhcpThread.PostAsyncTask([]() { 58 // Task implementation 59 }); 60 61 EXPECT_TRUE(result); 62 } 63 64 HWTEST_F(DhcpThreadTest, PostAsyncTaskWithName_SUCCESS, TestSize.Level1) 65 { 66 DHCP_LOGE("enter PostAsyncTaskWithName_SUCCESS"); 67 DhcpThread dhcpThread("TestThread"); __anonfd83fbbd0302() 68 bool result = dhcpThread.PostAsyncTask([]() { 69 // Task implementation 70 }, "TaskName"); 71 72 EXPECT_TRUE(result); 73 } 74 75 HWTEST_F(DhcpThreadTest, RemoveAsyncTask_SUCCESS, TestSize.Level1) 76 { 77 DHCP_LOGE("enter RemoveAsyncTask_SUCCESS"); 78 DhcpThread dhcpThread("TestThread"); __anonfd83fbbd0402() 79 bool result = dhcpThread.PostAsyncTask([]() { 80 // Task implementation 81 }, "TaskName"); 82 EXPECT_TRUE(result); 83 dhcpThread.RemoveAsyncTask("TaskName"); 84 } 85 86 HWTEST_F(DhcpThreadTest, Register_SUCCESS, TestSize.Level1) 87 { 88 DHCP_LOGE("enter Register_SUCCESS"); 89 DhcpTimer *dhcpTimer = DhcpTimer::GetInstance(); 90 uint32_t timerId; __anonfd83fbbd0502() 91 EnumErrCode result = dhcpTimer->Register([]() { 92 // Timer callback implementation 93 }, timerId); 94 95 EXPECT_EQ(result, EnumErrCode::DHCP_OPT_SUCCESS); 96 } 97 98 HWTEST_F(DhcpThreadTest, UnRegister_SUCCESS, TestSize.Level1) 99 { 100 DHCP_LOGE("enter UnRegister_SUCCESS"); 101 DhcpTimer *dhcpTimer = DhcpTimer::GetInstance(); 102 uint32_t timerId; __anonfd83fbbd0602() 103 EnumErrCode result = dhcpTimer->Register([]() { 104 // Timer callback implementation 105 }, timerId); 106 EXPECT_EQ(result, EnumErrCode::DHCP_OPT_SUCCESS); 107 dhcpTimer->UnRegister(timerId); 108 } 109 110 HWTEST_F(DhcpThreadTest, GetInstance_SUCCESS, TestSize.Level1) 111 { 112 DhcpTimer *dhcpTimer = DhcpTimer::GetInstance(); 113 EXPECT_NE(dhcpTimer, nullptr); 114 } 115 116 HWTEST_F(DhcpThreadTest, RegisterWithInterval_SUCCESS, TestSize.Level1) 117 { 118 DhcpTimer *dhcpTimer = DhcpTimer::GetInstance(); 119 uint32_t timerId; __anonfd83fbbd0702() 120 EnumErrCode result = dhcpTimer->Register([]() { 121 }, timerId, 5000); 122 123 EXPECT_EQ(result, EnumErrCode::DHCP_OPT_SUCCESS); 124 } 125 }