1 /*
2 * Copyright (c) 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 #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 } // namespace NetManagerStandard
121 } // namespace OHOS