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 <sys/stat.h> 18 #include <fcntl.h> 19 20 #include "mock_system_func.h" 21 #include "mock_custom_func.h" 22 #include "wifi_log.h" 23 #include "dhcp_ipv4.h" 24 #include "dhcp_client.h" 25 #include "dhcp_function.h" 26 27 using namespace testing::ext; 28 using namespace OHOS::Wifi; 29 30 class DhcpIpv4Test : 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(DhcpIpv4Test, ExecDhcpRenew_SUCCESS, TestSize.Level1) 43 { 44 MockSystemFunc::SetMockFlag(true); 45 46 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 47 48 SetIpv4State(DHCP_STATE_INIT); 49 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 50 SetIpv4State(DHCP_STATE_REQUESTING); 51 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 52 SetIpv4State(DHCP_STATE_BOUND); 53 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 54 SetIpv4State(DHCP_STATE_RENEWING); 55 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 56 57 MockSystemFunc::SetMockFlag(false); 58 } 59 60 HWTEST_F(DhcpIpv4Test, ExecDhcpRelease_SUCCESS, TestSize.Level1) 61 { 62 MockSystemFunc::SetMockFlag(true); 63 64 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillRepeatedly(Return(1)); 65 EXPECT_CALL(MockSystemFunc::GetInstance(), setsockopt(_, _, _, _, _)).WillRepeatedly(Return(0)); 66 EXPECT_CALL(MockSystemFunc::GetInstance(), bind(_, _, _)).WillRepeatedly(Return(0)); 67 EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillRepeatedly(Return(1)); 68 EXPECT_CALL(MockSystemFunc::GetInstance(), read(_, _, _)).WillRepeatedly(Return(500)); 69 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 70 EXPECT_CALL(MockSystemFunc::GetInstance(), connect(_, _, _)).WillRepeatedly(Return(0)); 71 EXPECT_CALL(MockSystemFunc::GetInstance(), write(_, _, _)).WillRepeatedly(Return(1)); 72 73 SetIpv4State(DHCP_STATE_BOUND); 74 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRelease()); 75 76 MockSystemFunc::SetMockFlag(false); 77 } 78 79 HWTEST_F(DhcpIpv4Test, TEST_FAILED, TestSize.Level1) 80 { 81 EXPECT_EQ(DHCP_OPT_FAILED, SetIpv4State(-1)); 82 EXPECT_EQ(DHCP_OPT_FAILED, GetPacketHeaderInfo(NULL, 0)); 83 EXPECT_EQ(DHCP_OPT_FAILED, GetPacketCommonInfo(NULL)); 84 } 85