1 /*
2 * Copyright (c) 2023-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 #include "gtest/gtest-message.h"
19 #include "gtest/gtest-test-part.h"
20 #include "gtest/hwext/gtest-ext.h"
21 #include "gtest/hwext/gtest-tag.h"
22 #define private public
23 #include "dev_interface_state.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 using namespace testing::ext;
29 constexpr const char *DEVICE_NAME = "ahaha";
30 } // namespace
31
32 class DevInterfaceStateTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase()40 void DevInterfaceStateTest::SetUpTestCase() {}
41
TearDownTestCase()42 void DevInterfaceStateTest::TearDownTestCase() {}
43
SetUp()44 void DevInterfaceStateTest::SetUp() {}
45
TearDown()46 void DevInterfaceStateTest::TearDown() {}
47
48 HWTEST_F(DevInterfaceStateTest, DevInterfaceStateTest001, TestSize.Level1)
49 {
50 DevInterfaceState devInterfaceState;
51 devInterfaceState.SetDevName(DEVICE_NAME);
52 std::set<NetCap> netCaps;
53 netCaps.insert(NET_CAPABILITY_MMS);
54 devInterfaceState.SetNetCaps(netCaps);
55 devInterfaceState.SetLinkUp(true);
56 sptr<NetLinkInfo> linkInfo;
57 devInterfaceState.SetlinkInfo(linkInfo);
58 devInterfaceState.SetDhcpReqState(true);
59 bool getDhcpReqState = devInterfaceState.GetDhcpReqState();
60 EXPECT_TRUE(getDhcpReqState);
61
62 sptr<StaticConfiguration> config = nullptr;
63 devInterfaceState.UpdateLinkInfo(config);
64 config = new (std::nothrow) StaticConfiguration();
65 devInterfaceState.linkInfo_ = nullptr;
66 devInterfaceState.UpdateLinkInfo(config);
67 devInterfaceState.linkInfo_ = new (std::nothrow) NetLinkInfo();
68 devInterfaceState.UpdateLinkInfo(config);
69 std::string devName = devInterfaceState.GetDevName();
70 EXPECT_STREQ(devName.c_str(), "ahaha");
71 std::set<NetCap> getNetCaps = devInterfaceState.GetNetCaps();
72 EXPECT_FALSE(getNetCaps.empty());
73 bool linkUp = devInterfaceState.GetLinkUp();
74 EXPECT_TRUE(linkUp);
75 sptr<NetLinkInfo> getLinkInfo = devInterfaceState.GetLinkInfo();
76 EXPECT_NE(getLinkInfo, nullptr);
77 }
78
79 HWTEST_F(DevInterfaceStateTest, DevInterfaceStateTest002, TestSize.Level1)
80 {
81 DevInterfaceState devInterfaceState;
82 devInterfaceState.ifCfg_ = nullptr;
83 IPSetMode ipSetMod = devInterfaceState.GetIPSetMode();
84 EXPECT_EQ(ipSetMod, IPSetMode::STATIC);
85 devInterfaceState.connLinkState_ = DevInterfaceState::ConnLinkState::UNREGISTERED;
86 devInterfaceState.RemoteRegisterNetSupplier();
87 devInterfaceState.connLinkState_ = DevInterfaceState::ConnLinkState::REGISTERED;
88 devInterfaceState.RemoteUnregisterNetSupplier();
89 devInterfaceState.connLinkState_ = DevInterfaceState::ConnLinkState::LINK_UNAVAILABLE;
90 devInterfaceState.RemoteUpdateNetLinkInfo();
91 devInterfaceState.connLinkState_ = DevInterfaceState::ConnLinkState::LINK_AVAILABLE;
92 devInterfaceState.linkInfo_ = nullptr;
93 devInterfaceState.RemoteUpdateNetLinkInfo();
94 sptr<NetLinkInfo> linkInfo;
95 devInterfaceState.linkInfo_ = linkInfo;
96 devInterfaceState.RemoteUpdateNetLinkInfo();
97 devInterfaceState.connLinkState_ = DevInterfaceState::ConnLinkState::UNREGISTERED;
98 devInterfaceState.RemoteUpdateNetSupplierInfo();
99 devInterfaceState.connLinkState_ = DevInterfaceState::ConnLinkState::REGISTERED;
100 devInterfaceState.netSupplierInfo_ = nullptr;
101 devInterfaceState.RemoteUpdateNetSupplierInfo();
102 sptr<NetSupplierInfo> netSupplierInfo;
103 devInterfaceState.netSupplierInfo_ = netSupplierInfo;
104 devInterfaceState.RemoteUpdateNetSupplierInfo();
105 }
106
107 HWTEST_F(DevInterfaceStateTest, SetIfcfgTest002, TestSize.Level1)
108 {
109 DevInterfaceState devInterfaceState;
110 devInterfaceState.ifCfg_ = nullptr;
111 sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
112 ifCfg->mode_ = STATIC;
113 devInterfaceState.SetIfcfg(ifCfg);
114 EXPECT_NE(devInterfaceState.GetIfcfg(), nullptr);
115 ifCfg->mode_ = DHCP;
116 devInterfaceState.SetIfcfg(ifCfg);
117 devInterfaceState.UpdateLinkInfo();
118 EXPECT_EQ(devInterfaceState.GetIfcfg()->mode_, DHCP);
119 }
120
121 HWTEST_F(DevInterfaceStateTest, DevInterfaceStateBranchTest001, TestSize.Level1)
122 {
123 DevInterfaceState devInterfaceState;
124 sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
125 ifCfg->mode_ = STATIC;
126 devInterfaceState.SetLinkUp(true);
127 devInterfaceState.UpdateSupplierAvailable();
128 devInterfaceState.SetIfcfg(ifCfg);
129 EXPECT_TRUE(devInterfaceState.linkUp_);
130
131 IPSetMode mode = devInterfaceState.GetIPSetMode();
132 EXPECT_EQ(mode, STATIC);
133
134 devInterfaceState.SetLinkUp(false);
135 devInterfaceState.UpdateSupplierAvailable();
136 devInterfaceState.SetIfcfg(ifCfg);
137 EXPECT_FALSE(devInterfaceState.linkUp_);
138
139 devInterfaceState.RemoteRegisterNetSupplier();
140 devInterfaceState.RemoteUnregisterNetSupplier();
141 devInterfaceState.RemoteUpdateNetSupplierInfo();
142 HttpProxy httpProxy;
143 devInterfaceState.UpdateNetHttpProxy(httpProxy);
144
145 sptr<NetLinkInfo> linkInfo = nullptr;
146 devInterfaceState.SetlinkInfo(linkInfo);
147 devInterfaceState.UpdateNetHttpProxy(httpProxy);
148 EXPECT_TRUE(devInterfaceState.linkInfo_ == nullptr);
149
150 std::vector<INetAddr> ipAddrList;
151 std::vector<INetAddr> netMaskList;
152 devInterfaceState.CreateLocalRoute("", ipAddrList, netMaskList);
153 EXPECT_TRUE(devInterfaceState.linkInfo_ == nullptr);
154
155 std::string testString = "";
156 auto result = devInterfaceState.GetIpv4Prefix(testString, netMaskList);
157 EXPECT_TRUE(result.empty());
158
159 sptr<StaticConfiguration> config = nullptr;
160 devInterfaceState.UpdateLanLinkInfo(config);
161
162 INetAddr targetNetAddr = {};
163 devInterfaceState.GetTargetNetAddrWithSameFamily("", ipAddrList, targetNetAddr);
164 devInterfaceState.GetDumpInfo(testString);
165 EXPECT_TRUE(result.empty());
166 }
167
168 HWTEST_F(DevInterfaceStateTest, DevInterfaceStateBranchTest002, TestSize.Level1)
169 {
170 DevInterfaceState devInterfaceState;
171 devInterfaceState.GetNetCaps();
172
173 sptr<NetLinkInfo> linkInfo = nullptr;
174 devInterfaceState.SetlinkInfo(linkInfo);
175 sptr<StaticConfiguration> config = new (std::nothrow) StaticConfiguration();
176 devInterfaceState.UpdateLanLinkInfo(config);
177
178 sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
179 ifCfg->mode_ = STATIC;
180 devInterfaceState.SetLancfg(ifCfg);
181 bool ret = devInterfaceState.IsLanIface();
182 EXPECT_FALSE(ret);
183
184 ifCfg->mode_ = LAN_STATIC;
185 devInterfaceState.SetLancfg(ifCfg);
186 ret = devInterfaceState.IsLanIface();
187 EXPECT_TRUE(ret);
188 }
189 } // namespace NetManagerStandard
190 } // namespace OHOS