1 /*
2 * Copyright (c) 2022-2024 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 "common_notify_callback_test.h"
23 #include "dhcp_controller.h"
24 #include "notify_callback_stub.h"
25
26 namespace OHOS {
27 namespace nmd {
28 namespace {
29 using namespace testing::ext;
30 using namespace NetsysNative;
31 } // namespace
32
33 class DhcpControllerTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 static inline auto instance_ = std::make_shared<DhcpController>();
40 };
41
SetUpTestCase()42 void DhcpControllerTest::SetUpTestCase() {}
43
TearDownTestCase()44 void DhcpControllerTest::TearDownTestCase() {}
45
SetUp()46 void DhcpControllerTest::SetUp() {}
47
TearDown()48 void DhcpControllerTest::TearDown() {}
49
50 HWTEST_F(DhcpControllerTest, RegisterNotifyCallbackTest001, TestSize.Level1)
51 {
52 sptr<INotifyCallback> callback = new (std::nothrow) NotifyCallbackTest();
53 auto ret = instance_->RegisterNotifyCallback(callback);
54 ASSERT_EQ(ret, 0);
55
56 ret = instance_->UnregisterNotifyCallback(callback);
57 ASSERT_EQ(ret, 0);
58 }
59
60 HWTEST_F(DhcpControllerTest, RegisterNotifyCallbackTest002, TestSize.Level1)
61 {
62 sptr<INotifyCallback> callback = nullptr;
63 auto ret = instance_->RegisterNotifyCallback(callback);
64 ASSERT_EQ(ret, 0);
65
66 ret = instance_->UnregisterNotifyCallback(callback);
67 ASSERT_EQ(ret, 0);
68 }
69
70 HWTEST_F(DhcpControllerTest, StartDhcpTest001, TestSize.Level1)
71 {
72 std::string testInterfaceName = "eth0";
73 std::string testIpv4Addr = "";
74 instance_->StartClient(testInterfaceName, false);
75 instance_->StopClient(testInterfaceName, false);
76 instance_->StartClient(testInterfaceName, true);
77 instance_->StopClient(testInterfaceName, true);
78 auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
79 ASSERT_FALSE(ret);
80 ret = instance_->StopDhcpService(testInterfaceName);
81 ASSERT_TRUE(ret);
82 ret = instance_->StartDhcpService(testInterfaceName, {});
83 ASSERT_FALSE(ret);
84 ret = instance_->StopDhcpService(testInterfaceName);
85 ASSERT_TRUE(ret);
86
87 DhcpResult dhcpRet;
88 instance_->Process(testInterfaceName, &dhcpRet);
89 }
90
91 HWTEST_F(DhcpControllerTest, TestErr, TestSize.Level1)
92 {
93 std::unique_ptr<DhcpController::DhcpControllerResultNotify> notifier =
94 std::make_unique<DhcpController::DhcpControllerResultNotify>();
95 int status = 0;
96 std::string ifname = "testIfaceName";
97 DhcpResult result;
98 notifier->OnSuccess(status, ifname.c_str(), &result);
99 std::string reason = "for test";
100 notifier->OnFailed(status, ifname.c_str(), reason.c_str());
101 std::string testInterfaceName = "dfsgagr";
102 std::string testIpv4Addr = "asgesag";
103 instance_->StartClient(testInterfaceName, false);
104 instance_->StopClient(testInterfaceName, false);
105 instance_->StartClient(testInterfaceName, true);
106 instance_->StopClient(testInterfaceName, true);
107 auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
108 ASSERT_FALSE(ret);
109 ret = instance_->StopDhcpService(testInterfaceName);
110 ASSERT_TRUE(ret);
111 ret = instance_->StartDhcpService(testInterfaceName, {});
112 ASSERT_FALSE(ret);
113 ret = instance_->StopDhcpService(testInterfaceName);
114 ASSERT_TRUE(ret);
115 }
116
117 } // namespace nmd
118 } // namespace OHOS