• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22 #include "dhcp_controller.h"
23 #include "notify_callback_stub.h"
24 
25 namespace OHOS {
26 namespace nmd {
27 namespace {
28 using namespace testing::ext;
29 using namespace NetsysNative;
30 class NotifyCallbackTest : public NotifyCallbackStub {
31 public:
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int flags,int scope)32     inline int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
33                                              int scope) override
34     {
35         return 0;
36     }
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)37     inline int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
38                                              int scope) override
39     {
40         return 0;
41     }
OnInterfaceAdded(const std::string & ifName)42     inline int32_t OnInterfaceAdded(const std::string &ifName) override
43     {
44         return 0;
45     }
OnInterfaceRemoved(const std::string & ifName)46     inline int32_t OnInterfaceRemoved(const std::string &ifName) override
47     {
48         return 0;
49     }
OnInterfaceChanged(const std::string & ifName,bool up)50     inline int32_t OnInterfaceChanged(const std::string &ifName, bool up) override
51     {
52         return 0;
53     }
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)54     inline int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override
55     {
56         return 0;
57     }
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)58     inline int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
59                                   const std::string &ifName) override
60     {
61         return 0;
62     }
OnDhcpSuccess(sptr<DhcpResultParcel> & dhcpResult)63     inline int32_t OnDhcpSuccess(sptr<DhcpResultParcel> &dhcpResult) override
64     {
65         return 0;
66     }
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)67     inline int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override
68     {
69         return 0;
70     }
71 };
72 } // namespace
73 
74 class DhcpControllerTest : public testing::Test {
75 public:
76     static void SetUpTestCase();
77     static void TearDownTestCase();
78     void SetUp();
79     void TearDown();
80     static inline auto instance_ = std::make_shared<DhcpController>();
81 };
82 
SetUpTestCase()83 void DhcpControllerTest::SetUpTestCase() {}
84 
TearDownTestCase()85 void DhcpControllerTest::TearDownTestCase() {}
86 
SetUp()87 void DhcpControllerTest::SetUp() {}
88 
TearDown()89 void DhcpControllerTest::TearDown() {}
90 
91 HWTEST_F(DhcpControllerTest, RegisterNotifyCallbackTest001, TestSize.Level1)
92 {
93     sptr<INotifyCallback> callback = new (std::nothrow) NotifyCallbackTest();
94     auto ret = instance_->RegisterNotifyCallback(callback);
95     ASSERT_EQ(ret, 0);
96 }
97 
98 HWTEST_F(DhcpControllerTest, StartDhcpTest001, TestSize.Level1)
99 {
100     std::string testInterfaceName = "eth0";
101     std::string testIpv4Addr = "112.254.154.415";
102     instance_->StartDhcpClient(testInterfaceName, false);
103     instance_->StopDhcpClient(testInterfaceName, false);
104     instance_->StartDhcpClient(testInterfaceName, true);
105     instance_->StopDhcpClient(testInterfaceName, true);
106     auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
107     ASSERT_FALSE(ret);
108     ret = instance_->StopDhcpService(testInterfaceName);
109     ASSERT_TRUE(ret);
110     ret = instance_->StartDhcpService(testInterfaceName, {});
111     ASSERT_FALSE(ret);
112     ret = instance_->StopDhcpService(testInterfaceName);
113     ASSERT_TRUE(ret);
114 
115     OHOS::Wifi::DhcpResult dhcpRet;
116     instance_->Process(testInterfaceName, dhcpRet);
117 }
118 
119 HWTEST_F(DhcpControllerTest, TestErr, TestSize.Level1)
120 {
121     std::unique_ptr<DhcpController::DhcpControllerResultNotify> notifier =
122         std::make_unique<DhcpController::DhcpControllerResultNotify>(*instance_);
123     int status = 0;
124     std::string ifname = "testIfaceName";
125     OHOS::Wifi::DhcpResult result;
126     notifier->OnSuccess(status, ifname, result);
127     std::string reason = "for test";
128     notifier->OnFailed(status, ifname, reason);
129     notifier->OnSerExitNotify(ifname);
130     std::string testInterfaceName = "dfsgagr";
131     std::string testIpv4Addr = "asgesag";
132     instance_->StartDhcpClient(testInterfaceName, false);
133     instance_->StopDhcpClient(testInterfaceName, false);
134     instance_->StartDhcpClient(testInterfaceName, true);
135     instance_->StopDhcpClient(testInterfaceName, true);
136     auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
137     ASSERT_FALSE(ret);
138     ret = instance_->StopDhcpService(testInterfaceName);
139     ASSERT_TRUE(ret);
140     ret = instance_->StartDhcpService(testInterfaceName, {});
141     ASSERT_FALSE(ret);
142     ret = instance_->StopDhcpService(testInterfaceName);
143     ASSERT_TRUE(ret);
144 }
145 
146 } // namespace nmd
147 } // namespace OHOS