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 <gtest/gtest.h> 17 18 #include "dhcp_service.h" 19 #include "dhcp_result_notify.h" 20 #include "dhcp_func.h" 21 22 using namespace testing::ext; 23 using namespace OHOS; 24 using namespace OHOS::Wifi; 25 26 class DhcpServiceTest : public testing::Test { 27 public: SetUpTestCase()28 static void SetUpTestCase(){} TearDownTestCase()29 static void TearDownTestCase(){} SetUp()30 virtual void SetUp() 31 { 32 printf("DhcpServiceTest SetUp()...\n"); 33 pDhcpService = std::make_unique<DhcpService>(); 34 if (pDhcpService == nullptr) { 35 printf("DhcpServiceTest SetUp() make_unique DhcpService failed\n"); 36 } 37 } TearDown()38 virtual void TearDown() 39 { 40 printf("DhcpServiceTest TearDown()...\n"); 41 if (pDhcpService != nullptr) { 42 pDhcpService.reset(nullptr); 43 } 44 } 45 public: 46 std::unique_ptr<IDhcpService> pDhcpService; 47 }; 48 49 HWTEST_F(DhcpServiceTest, DhcpClientService_Test1, TestSize.Level1) 50 { 51 ASSERT_TRUE(pDhcpService != nullptr); 52 53 std::string ifname = ""; 54 DhcpResultNotify dhcpResultNotify; 55 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->GetDhcpResult(ifname, &dhcpResultNotify, 0)); 56 DhcpServiceInfo dhcpInfo; 57 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->GetDhcpInfo(ifname, dhcpInfo)); 58 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->RenewDhcpClient(ifname)); 59 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->ReleaseDhcpClient(ifname)); 60 61 bool bIpv6 = false; 62 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->StartDhcpClient(ifname, bIpv6)); 63 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->GetDhcpResult(ifname, &dhcpResultNotify, 0)); 64 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->GetDhcpInfo(ifname, dhcpInfo)); 65 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->RenewDhcpClient(ifname)); 66 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->ReleaseDhcpClient(ifname)); 67 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->StopDhcpClient(ifname, bIpv6)); 68 } 69 70 HWTEST_F(DhcpServiceTest, DhcpServerService_Test1, TestSize.Level1) 71 { 72 ASSERT_TRUE(pDhcpService != nullptr); 73 74 std::string ifname = ""; 75 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->StartDhcpServer(ifname)); 76 EXPECT_EQ(0, pDhcpService->GetServerStatus()); 77 78 std::string tagName = ""; 79 DhcpRange putRange; 80 EXPECT_EQ(DHCP_OPT_ERROR, pDhcpService->PutDhcpRange(tagName, putRange)); 81 EXPECT_EQ(DHCP_OPT_ERROR, pDhcpService->RemoveDhcpRange(tagName, putRange)); 82 EXPECT_EQ(DHCP_OPT_ERROR, pDhcpService->RemoveAllDhcpRange(tagName)); 83 DhcpRange setRange; 84 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->SetDhcpRange(ifname, setRange)); 85 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->SetDhcpRange(ifname, tagName)); 86 87 unlink(DHCP_SERVER_LEASES_FILE.c_str()); 88 std::vector<std::string> vecLeases; 89 EXPECT_EQ(DHCP_OPT_ERROR, pDhcpService->GetLeases(ifname, vecLeases)); 90 DhcpResultNotify dhcpResultNotify; 91 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->GetDhcpSerProExit(ifname, &dhcpResultNotify)); 92 EXPECT_EQ(DHCP_OPT_FAILED, pDhcpService->StopDhcpServer(ifname)); 93 } 94