• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_client_service_impl.h"
19 #include "dhcp_result_notify.h"
20 #include "mock_system_func.h"
21 #include "dhcp_func.h"
22 #include "securec.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::Wifi;
27 namespace OHOS {
28 namespace Wifi {
29 class DhcpClientServiceTest : public testing::Test {
30 public:
SetUpTestCase()31     static void SetUpTestCase()
32     {}
TearDownTestCase()33     static void TearDownTestCase()
34     {}
SetUp()35     virtual void SetUp()
36     {
37         printf("DhcpClientServiceTest SetUp()...\n");
38         pClientService = std::make_unique<DhcpClientServiceImpl>();
39     }
TearDown()40     virtual void TearDown()
41     {
42         printf("DhcpClientServiceTest TearDown()...\n");
43         if (pClientService != nullptr) {
44             pClientService.reset(nullptr);
45         }
46     }
47 public:
48     std::unique_ptr<DhcpClientServiceImpl> pClientService;
49 };
50 
51 HWTEST_F(DhcpClientServiceTest, DhcpClientService_Test2, TestSize.Level1)
52 {
53     ASSERT_TRUE(pClientService != nullptr);
54 
55     MockSystemFunc::SetMockFlag(true);
56 
57     EXPECT_CALL(MockSystemFunc::GetInstance(), vfork())
58         .WillOnce(Return(-1)).WillOnce(Return(1))
59         .WillOnce(Return(-1)).WillOnce(Return(1))
60         .WillRepeatedly(Return(1));
61     EXPECT_CALL(MockSystemFunc::GetInstance(), waitpid(_, _, _)).WillRepeatedly(Return(0));
62     EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillRepeatedly(Return(1));
63     EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0));
64     EXPECT_CALL(MockSystemFunc::GetInstance(), bind(_, _, _)).Times(testing::AtLeast(0));
65     EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).Times(testing::AtLeast(0));
66     EXPECT_CALL(MockSystemFunc::GetInstance(), select(_, _, _, _, _)).Times(testing::AtLeast(0));
67     EXPECT_CALL(MockSystemFunc::GetInstance(), setsockopt(_, _, _, _, _)).Times(testing::AtLeast(0));
68 
69     std::string ifname = "wlan0";
70     std::string strFile4 = DHCP_WORK_DIR + ifname + DHCP_RESULT_FILETYPE;
71     std::string strData4 = "IP4 0 * * * * * * * * 0";
72     ASSERT_TRUE(DhcpFunc::CreateFile(strFile4, strData4));
73     DhcpResultNotify dhcpResultNotify;
74     EXPECT_EQ(DHCP_OPT_FAILED, pClientService->GetDhcpResult("", &dhcpResultNotify, 0));
75     EXPECT_EQ(DHCP_OPT_FAILED, pClientService->GetDhcpResult(ifname, nullptr, 0));
76     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult(ifname, &dhcpResultNotify, 0));
77     DhcpResultNotify dhcpResultNotify1;
78     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult(ifname, &dhcpResultNotify1, 10));
79     DhcpResultNotify dhcpResultNotify2;
80     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult(ifname, &dhcpResultNotify2, 20));
81     DhcpResultNotify dhcpResultNotify3;
82     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->GetDhcpResult("wlan1", &dhcpResultNotify3, 30));
83 
84     sleep(DHCP_NUM_ONE);
85 
86     EXPECT_EQ(DHCP_OPT_FAILED, pClientService->StopDhcpClient(ifname, true));
87     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StopDhcpClient(ifname, true));
88     ASSERT_TRUE(DhcpFunc::RemoveFile(strFile4));
89 
90     MockSystemFunc::SetMockFlag(false);
91 }
92 
93 HWTEST_F(DhcpClientServiceTest, DhcpClientService_Test3, TestSize.Level1)
94 {
95     ASSERT_TRUE(pClientService != nullptr);
96 
97     MockSystemFunc::SetMockFlag(true);
98 
99     EXPECT_CALL(MockSystemFunc::GetInstance(), vfork()).WillRepeatedly(Return(1));
100     EXPECT_CALL(MockSystemFunc::GetInstance(), waitpid(_, _, _)).WillRepeatedly(Return(0));
101     EXPECT_CALL(MockSystemFunc::GetInstance(), kill(_, _))
102         .WillOnce(Return(-1)).WillOnce(Return(0))
103         .WillOnce(Return(-1)).WillOnce(Return(0))
104         .WillRepeatedly(Return(0));
105     EXPECT_CALL(MockSystemFunc::GetInstance(), open(_, _)).WillRepeatedly(Return(1));
106     EXPECT_CALL(MockSystemFunc::GetInstance(), close(_)).WillRepeatedly(Return(0));
107     EXPECT_CALL(MockSystemFunc::GetInstance(), bind(_, _, _)).Times(testing::AtLeast(0));
108     EXPECT_CALL(MockSystemFunc::GetInstance(), socket(_, _, _)).Times(testing::AtLeast(0));
109     EXPECT_CALL(MockSystemFunc::GetInstance(), select(_, _, _, _, _)).Times(testing::AtLeast(0));
110     EXPECT_CALL(MockSystemFunc::GetInstance(), setsockopt(_, _, _, _, _)).Times(testing::AtLeast(0));
111 
112     std::string ifname = "wlan0";
113     EXPECT_EQ(0, pClientService->GetDhcpClientProPid(""));
114     EXPECT_EQ(0, pClientService->GetDhcpClientProPid(ifname));
115 
116     std::string strFile4 = DHCP_WORK_DIR + ifname + DHCP_RESULT_FILETYPE;
117     std::string strData4 = "IP4 0 * * * * * * * * 0";
118     ASSERT_TRUE(DhcpFunc::CreateFile(strFile4, strData4));
119     bool bIpv6 = true;
120     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StopDhcpClient(ifname, bIpv6));
121     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StartDhcpClient(ifname, bIpv6));
122 
123     EXPECT_EQ(DHCP_OPT_FAILED, pClientService->RenewDhcpClient(ifname));
124     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->RenewDhcpClient(ifname));
125     EXPECT_EQ(DHCP_OPT_FAILED, pClientService->ReleaseDhcpClient(ifname));
126     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->ReleaseDhcpClient(ifname));
127 
128     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StopDhcpClient(ifname, bIpv6));
129     ASSERT_TRUE(DhcpFunc::RemoveFile(strFile4));
130 
131     EXPECT_EQ(DHCP_OPT_SUCCESS, pClientService->StartDhcpClient("wlan1", false));
132 
133     MockSystemFunc::SetMockFlag(false);
134 }
135 }
136 }