1 /*
2 * Copyright (c) 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
18 #include "dhcp_controller.h"
19 #include "notify_callback_stub.h"
20
21 namespace OHOS {
22 namespace nmd {
23 namespace {
24 using namespace testing::ext;
25 using namespace NetsysNative;
26 class NotifyCallbackTest : public NotifyCallbackStub {
27 public:
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int flags,int scope)28 inline int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
29 int scope) override
30 {
31 return 0;
32 }
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)33 inline int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
34 int scope) override
35 {
36 return 0;
37 }
OnInterfaceAdded(const std::string & ifName)38 inline int32_t OnInterfaceAdded(const std::string &ifName) override
39 {
40 return 0;
41 }
OnInterfaceRemoved(const std::string & ifName)42 inline int32_t OnInterfaceRemoved(const std::string &ifName) override
43 {
44 return 0;
45 }
OnInterfaceChanged(const std::string & ifName,bool up)46 inline int32_t OnInterfaceChanged(const std::string &ifName, bool up) override
47 {
48 return 0;
49 }
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)50 inline int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override
51 {
52 return 0;
53 }
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)54 inline int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
55 const std::string &ifName) override
56 {
57 return 0;
58 }
OnDhcpSuccess(sptr<DhcpResultParcel> & dhcpResult)59 inline int32_t OnDhcpSuccess(sptr<DhcpResultParcel> &dhcpResult) override
60 {
61 return 0;
62 }
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)63 inline int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override
64 {
65 return 0;
66 }
67 };
68 } // namespace
69
70 class DhcpControllerTest : public testing::Test {
71 public:
72 static void SetUpTestCase();
73 static void TearDownTestCase();
74 void SetUp();
75 void TearDown();
76 static inline auto instance_ = std::make_shared<DhcpController>();
77 };
78
SetUpTestCase()79 void DhcpControllerTest::SetUpTestCase() {}
80
TearDownTestCase()81 void DhcpControllerTest::TearDownTestCase() {}
82
SetUp()83 void DhcpControllerTest::SetUp() {}
84
TearDown()85 void DhcpControllerTest::TearDown() {}
86
87 HWTEST_F(DhcpControllerTest, RegisterNotifyCallbackTest001, TestSize.Level1)
88 {
89 sptr<INotifyCallback> callback = new (std::nothrow) NotifyCallbackTest();
90 auto ret = instance_->RegisterNotifyCallback(callback);
91 ASSERT_EQ(ret, 0);
92 }
93
94 HWTEST_F(DhcpControllerTest, StartDhcpTest001, TestSize.Level1)
95 {
96 std::string testInterfaceName = "eth0";
97 std::string testIpv4Addr = "112.254.154.415";
98 instance_->StartDhcpClient(testInterfaceName, false);
99 instance_->StopDhcpClient(testInterfaceName, false);
100 instance_->StartDhcpClient(testInterfaceName, true);
101 instance_->StopDhcpClient(testInterfaceName, true);
102 auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
103 ASSERT_FALSE(ret);
104 ret = instance_->StopDhcpService(testInterfaceName);
105 ASSERT_TRUE(ret);
106 ret = instance_->StartDhcpService(testInterfaceName, {});
107 ASSERT_FALSE(ret);
108 ret = instance_->StopDhcpService(testInterfaceName);
109 ASSERT_TRUE(ret);
110 }
111 } // namespace nmd
112 } // namespace OHOS