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
57 HWTEST_F(DhcpControllerTest, StartDhcpTest001, TestSize.Level1)
58 {
59 std::string testInterfaceName = "eth0";
60 std::string testIpv4Addr = "";
61 instance_->StartClient(testInterfaceName, false);
62 instance_->StopClient(testInterfaceName, false);
63 instance_->StartClient(testInterfaceName, true);
64 instance_->StopClient(testInterfaceName, true);
65 auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
66 ASSERT_FALSE(ret);
67 ret = instance_->StopDhcpService(testInterfaceName);
68 ASSERT_TRUE(ret);
69 ret = instance_->StartDhcpService(testInterfaceName, {});
70 ASSERT_FALSE(ret);
71 ret = instance_->StopDhcpService(testInterfaceName);
72 ASSERT_TRUE(ret);
73
74 DhcpResult dhcpRet;
75 instance_->Process(testInterfaceName, &dhcpRet);
76 }
77
78 HWTEST_F(DhcpControllerTest, TestErr, TestSize.Level1)
79 {
80 std::unique_ptr<DhcpController::DhcpControllerResultNotify> notifier =
81 std::make_unique<DhcpController::DhcpControllerResultNotify>();
82 int status = 0;
83 std::string ifname = "testIfaceName";
84 DhcpResult result;
85 notifier->OnSuccess(status, ifname.c_str(), &result);
86 std::string reason = "for test";
87 notifier->OnFailed(status, ifname.c_str(), reason.c_str());
88 std::string testInterfaceName = "dfsgagr";
89 std::string testIpv4Addr = "asgesag";
90 instance_->StartClient(testInterfaceName, false);
91 instance_->StopClient(testInterfaceName, false);
92 instance_->StartClient(testInterfaceName, true);
93 instance_->StopClient(testInterfaceName, true);
94 auto ret = instance_->StartDhcpService(testInterfaceName, testIpv4Addr);
95 ASSERT_FALSE(ret);
96 ret = instance_->StopDhcpService(testInterfaceName);
97 ASSERT_TRUE(ret);
98 ret = instance_->StartDhcpService(testInterfaceName, {});
99 ASSERT_FALSE(ret);
100 ret = instance_->StopDhcpService(testInterfaceName);
101 ASSERT_TRUE(ret);
102 }
103
104 } // namespace nmd
105 } // namespace OHOS