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 namespace OHOS { 30 namespace Wifi { 31 class DhcpIpv4Test : 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(DhcpIpv4Test, ExecDhcpRenew_SUCCESS, TestSize.Level1) 44 { 45 MockSystemFunc::SetMockFlag(true); 46 47 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 48 49 SetIpv4State(DHCP_STATE_INIT); 50 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 51 SetIpv4State(DHCP_STATE_REQUESTING); 52 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 53 SetIpv4State(DHCP_STATE_BOUND); 54 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 55 SetIpv4State(DHCP_STATE_RENEWING); 56 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRenew()); 57 58 MockSystemFunc::SetMockFlag(false); 59 } 60 61 HWTEST_F(DhcpIpv4Test, ExecDhcpRelease_SUCCESS, TestSize.Level1) 62 { 63 MockSystemFunc::SetMockFlag(true); 64 65 EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).WillRepeatedly(Return(1)); 66 EXPECT_CALL(MockSystemFunc::GetInstance(), setsockopt(_, _, _, _, _)).WillRepeatedly(Return(0)); 67 EXPECT_CALL(MockSystemFunc::GetInstance(), bind(_, _, _)).WillRepeatedly(Return(0)); 68 EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillRepeatedly(Return(1)); 69 EXPECT_CALL(MockSystemFunc::GetInstance(), read(_, _, _)).WillRepeatedly(Return(500)); 70 EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0)); 71 EXPECT_CALL(MockSystemFunc::GetInstance(), connect(_, _, _)).WillRepeatedly(Return(0)); 72 EXPECT_CALL(MockSystemFunc::GetInstance(), write(_, _, _)).WillRepeatedly(Return(1)); 73 74 SetIpv4State(DHCP_STATE_BOUND); 75 EXPECT_EQ(DHCP_OPT_SUCCESS, ExecDhcpRelease()); 76 77 MockSystemFunc::SetMockFlag(false); 78 } 79 80 HWTEST_F(DhcpIpv4Test, TEST_FAILED, TestSize.Level1) 81 { 82 EXPECT_EQ(DHCP_OPT_FAILED, SetIpv4State(-1)); 83 EXPECT_EQ(DHCP_OPT_FAILED, GetPacketHeaderInfo(NULL, 0)); 84 EXPECT_EQ(DHCP_OPT_FAILED, GetPacketCommonInfo(NULL)); 85 } 86 /** 87 * @tc.name: DhcpRenew_FAILED 88 * @tc.desc: DhcpRenew() 89 * @tc.type: FUNC 90 * @tc.require: issue 91 */ 92 HWTEST_F(DhcpIpv4Test, DhcpRenew_FAILED, TestSize.Level1) 93 { 94 EXPECT_EQ(SOCKET_OPT_FAILED, DhcpRenew(1, 0, 0)); 95 } 96 /** 97 * @tc.name: DhcpRenew_SUCCESS 98 * @tc.desc: DhcpRenew() 99 * @tc.type: FUNC 100 * @tc.require: issue 101 */ 102 HWTEST_F(DhcpIpv4Test, DhcpRenew_SUCCESS, TestSize.Level1) 103 { 104 EXPECT_EQ(SOCKET_OPT_FAILED, DhcpRenew(1, 0, 1)); 105 } 106 /** 107 * @tc.name: DhcpRequest_SUCCESS 108 * @tc.desc: DhcpRequest() 109 * @tc.type: FUNC 110 * @tc.require: issue 111 */ 112 HWTEST_F(DhcpIpv4Test, DhcpRequest_SUCCESS, TestSize.Level1) 113 { 114 EXPECT_EQ(SOCKET_OPT_FAILED, DhcpRequest(1, 0, 1)); 115 } 116 /** 117 * @tc.name: DhcpDiscover_SUCCESS 118 * @tc.desc: DhcpDiscover() 119 * @tc.type: FUNC 120 * @tc.require: issue 121 */ 122 HWTEST_F(DhcpIpv4Test, DhcpDiscover_SUCCESS, TestSize.Level1) 123 { 124 EXPECT_EQ(SOCKET_OPT_FAILED, DhcpDiscover(1, 0)); 125 } 126 /** 127 * @tc.name: DhcpDiscover_SUCCESS2 128 * @tc.desc: DhcpDiscover() 129 * @tc.type: FUNC 130 * @tc.require: issue 131 */ 132 HWTEST_F(DhcpIpv4Test, DhcpDiscover_SUCCESS2, TestSize.Level1) 133 { 134 EXPECT_EQ(SOCKET_OPT_FAILED, DhcpDiscover(0, 1)); 135 } 136 /** 137 * @tc.name: PublishDhcpResultEvent_Fail1 138 * @tc.desc: PublishDhcpResultEvent() 139 * @tc.type: FUNC 140 * @tc.require: issue 141 */ 142 HWTEST_F(DhcpIpv4Test, PublishDhcpResultEvent_Fail1, TestSize.Level1) 143 { 144 DhcpResult result; 145 EXPECT_EQ(DHCP_OPT_FAILED, PublishDhcpResultEvent(nullptr, PUBLISH_CODE_SUCCESS, &result)); 146 } 147 /** 148 * @tc.name: PublishDhcpResultEvent_Fail2 149 * @tc.desc: PublishDhcpResultEvent() 150 * @tc.type: FUNC 151 * @tc.require: issue 152 */ 153 HWTEST_F(DhcpIpv4Test, PublishDhcpResultEvent_Fail2, TestSize.Level1) 154 { 155 DhcpResult result; 156 char ifname[] = "testcode//"; 157 EXPECT_EQ(DHCP_OPT_FAILED, PublishDhcpResultEvent(ifname, DHCP_HWADDR_LENGTH, &result)); 158 } 159 /** 160 * @tc.name: PublishDhcpResultEvent_Fail3 161 * @tc.desc: PublishDhcpResultEvent() 162 * @tc.type: FUNC 163 * @tc.require: issue 164 */ 165 HWTEST_F(DhcpIpv4Test, PublishDhcpResultEvent_Fail3, TestSize.Level1) 166 { 167 DhcpResult *result = NULL; 168 char ifname[] = "testcode//"; 169 EXPECT_EQ(DHCP_OPT_FAILED, PublishDhcpResultEvent(ifname, PUBLISH_CODE_SUCCESS, result)); 170 } 171 /** 172 * @tc.name: PublishDhcpResultEvent_Fail4 173 * @tc.desc: PublishDhcpResultEvent() 174 * @tc.type: FUNC 175 * @tc.require: issue 176 */ 177 HWTEST_F(DhcpIpv4Test, PublishDhcpResultEvent_Fail4, TestSize.Level1) 178 { 179 DhcpResult result; 180 char ifname[] = "testcode//"; 181 EXPECT_EQ(DHCP_OPT_SUCCESS, PublishDhcpResultEvent(ifname, PUBLISH_CODE_SUCCESS, &result)); 182 } 183 /** 184 * @tc.name: PublishDhcpResultEvent_Fail5 185 * @tc.desc: PublishDhcpResultEvent() 186 * @tc.type: FUNC 187 * @tc.require: issue 188 */ 189 HWTEST_F(DhcpIpv4Test, PublishDhcpResultEvent_Fail5, TestSize.Level1) 190 { 191 DhcpResult result; 192 char ifname[] = "testcode//"; 193 EXPECT_EQ(DHCP_OPT_SUCCESS, PublishDhcpResultEvent(ifname, PUBLISH_CODE_FAILED, &result)); 194 } 195 } 196 } 197