1 /*
2 * Copyright (c) 2025-2025 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 "ethernet_client.h"
19 #include "net_manager_constants.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23 using namespace testing::ext;
24 namespace {
25 using namespace testing::ext;
26 constexpr const char *DEV_NAME = "eth0";
27 constexpr const char *IFACE_NAME = "wlan0";
28 } // namespace
29
30 class EthernetClientTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 sptr<InterfaceConfiguration> GetIfaceConfig();
35 void SetUp();
36 void TearDown();
37 };
38
GetIfaceConfig()39 sptr<InterfaceConfiguration> EthernetClientTest::GetIfaceConfig()
40 {
41 sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
42 if (!ic) {
43 return ic;
44 }
45 INetAddr ipv4Addr;
46 ipv4Addr.type_ = INetAddr::IPV4;
47 ipv4Addr.family_ = 0x01;
48 ipv4Addr.prefixlen_ = 0x01;
49 ipv4Addr.address_ = "172.17.5.234";
50 ipv4Addr.netMask_ = "255.255.254.0";
51 ipv4Addr.hostName_ = "netAddr";
52 ic->ipStatic_.ipAddrList_.push_back(ipv4Addr);
53 INetAddr route;
54 route.type_ = INetAddr::IPV4;
55 route.family_ = 0x01;
56 route.prefixlen_ = 0x01;
57 route.address_ = "0.0.0.0";
58 route.netMask_ = "0.0.0.0";
59 route.hostName_ = "netAddr";
60 ic->ipStatic_.routeList_.push_back(route);
61 INetAddr gateway;
62 gateway.type_ = INetAddr::IPV4;
63 gateway.family_ = 0x01;
64 gateway.prefixlen_ = 0x01;
65 gateway.address_ = "172.17.4.1";
66 gateway.netMask_ = "0.0.0.0";
67 gateway.hostName_ = "netAddr";
68 ic->ipStatic_.gatewayList_.push_back(gateway);
69 INetAddr netMask;
70 netMask.type_ = INetAddr::IPV4;
71 netMask.family_ = 0x01;
72 netMask.address_ = "255.255.255.0";
73 netMask.hostName_ = "netAddr";
74 ic->ipStatic_.netMaskList_.push_back(netMask);
75 INetAddr dns1;
76 dns1.type_ = INetAddr::IPV4;
77 dns1.family_ = 0x01;
78 dns1.address_ = "8.8.8.8";
79 dns1.hostName_ = "netAddr";
80 INetAddr dns2;
81 dns2.type_ = INetAddr::IPV4;
82 dns2.family_ = 0x01;
83 dns2.address_ = "114.114.114.114";
84 dns2.hostName_ = "netAddr";
85 ic->ipStatic_.dnsServers_.push_back(dns1);
86 ic->ipStatic_.dnsServers_.push_back(dns2);
87 return ic;
88 }
89
SetUpTestCase()90 void EthernetClientTest::SetUpTestCase() {}
91
TearDownTestCase()92 void EthernetClientTest::TearDownTestCase() {}
93
SetUp()94 void EthernetClientTest::SetUp() {}
95
TearDown()96 void EthernetClientTest::TearDown() {}
97
98 /**
99 * @tc.name: GetMacAddressTest001
100 * @tc.desc: Test GetMacAddress.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(EthernetClientTest, GetMacAddressTest001, TestSize.Level1)
104 {
105 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
106 std::vector<MacAddressInfo> macAddrList;
107 int32_t ret = ethernetClient->GetMacAddress(macAddrList);
108 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
109 }
110
111 /**
112 * @tc.name: SetIfaceConfigTest001
113 * @tc.desc: Test SetIfaceConfig.
114 * @tc.type: FUNC
115 */
116 HWTEST_F(EthernetClientTest, SetIfaceConfigTest001, TestSize.Level1)
117 {
118 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
119 sptr<InterfaceConfiguration> ic = GetIfaceConfig();
120 int32_t ret = ethernetClient->SetIfaceConfig(DEV_NAME, ic);
121 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
122 }
123
124 /**
125 * @tc.name: GetIfaceConfigTest001
126 * @tc.desc: Test GetIfaceConfig.
127 * @tc.type: FUNC
128 */
129 HWTEST_F(EthernetClientTest, GetIfaceConfigTest001, TestSize.Level1)
130 {
131 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
132 std::string iface = DEV_NAME;
133 sptr<InterfaceConfiguration> ifaceConfig = GetIfaceConfig();
134 int32_t ret = ethernetClient->GetIfaceConfig(iface, ifaceConfig);
135 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
136 }
137
138 /**
139 * @tc.name: ResetFactoryTest001
140 * @tc.desc: Test ResetFactory.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(EthernetClientTest, ResetFactoryTest001, TestSize.Level1)
144 {
145 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
146 int32_t ret = ethernetClient->ResetFactory();
147 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
148 }
149
150 /**
151 * @tc.name: SetInterfaceUpTest001
152 * @tc.desc: Test SetInterfaceUp.
153 * @tc.type: FUNC
154 */
155 HWTEST_F(EthernetClientTest, SetInterfaceUpTest001, TestSize.Level1)
156 {
157 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
158 int32_t ret = ethernetClient->SetInterfaceUp(DEV_NAME);
159 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
160 }
161
162 /**
163 * @tc.name: SetInterfaceDownTest001
164 * @tc.desc: Test SetInterfaceDown.
165 * @tc.type: FUNC
166 */
167 HWTEST_F(EthernetClientTest, SetInterfaceDownTest001, TestSize.Level1)
168 {
169 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
170 int32_t ret = ethernetClient->SetInterfaceDown(DEV_NAME);
171 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
172 }
173
174 /**
175 * @tc.name: SetInterfaceConfigTest001
176 * @tc.desc: Test SetInterfaceConfig.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(EthernetClientTest, SetInterfaceConfigTest001, TestSize.Level1)
180 {
181 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
182 std::string iface = DEV_NAME;
183 OHOS::nmd::InterfaceConfigurationParcel cfg;
184 cfg.ifName = "eth0";
185 cfg.hwAddr = "";
186 cfg.ipv4Addr = "172.17.5.234";
187 cfg.prefixLength = 24;
188 cfg.flags.push_back("up");
189 cfg.flags.push_back("broadcast");
190 int32_t ret = ethernetClient->SetInterfaceConfig(iface, cfg);
191 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
192 }
193 } // namespace NetManagerStandard
194 } // namespace OHOS