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 "ethernet_client.h"
19 #include "gtest/gtest-message.h"
20 #include "gtest/gtest-test-part.h"
21 #include "gtest/hwext/gtest-ext.h"
22 #include "gtest/hwext/gtest-tag.h"
23 #include "http_proxy.h"
24 #include "interface_configuration.h"
25 #include "interface_type.h"
26 #include "mac_address_info.h"
27 #include "net_manager_constants.h"
28 #include "netmanager_ext_test_security.h"
29 #include "netmgr_ext_log_wrapper.h"
30 #include "refbase.h"
31 #include "static_configuration.h"
32 
33 #define private public
34 #define protected public
35 #include "ethernet_client.h"
36 #include "ethernet_dhcp_controller.h"
37 #include "ethernet_service.h"
38 #include "ethernet_management.h"
39 #include "ethernet_service_proxy.h"
40 
41 namespace OHOS {
42 namespace NetManagerStandard {
43 namespace {
44 using namespace testing::ext;
45 constexpr const char *DEV_NAME = "eth0";
46 constexpr const char *TEST_PROXY_HOST = "127.0.0.1";
47 constexpr const char *TEST_MAC_ADDRESS = "a0:0b:c1:d0:02:03";
48 constexpr uint16_t TEST_PROXY_PORT = 8080;
49 }
50 
51 class EtherNetServiceProxyTest : public testing::Test {
52 public:
53     static void SetUpTestCase();
54     static void TearDownTestCase();
55     sptr<InterfaceConfiguration> GetIfaceConfig();
56     void SetUp();
57     void TearDown();
58 };
59 
GetIfaceConfig()60 sptr<InterfaceConfiguration> EtherNetServiceProxyTest::GetIfaceConfig()
61 {
62     sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
63     if (!ic) {
64         return ic;
65     }
66     INetAddr ipv4Addr;
67     ipv4Addr.type_ = INetAddr::IPV4;
68     ipv4Addr.family_ = 0x01;
69     ipv4Addr.prefixlen_ = 0x01;
70     ipv4Addr.address_ = "172.17.5.234";
71     ipv4Addr.netMask_ = "255.255.254.0";
72     ipv4Addr.hostName_ = "netAddr";
73     ic->ipStatic_.ipAddrList_.push_back(ipv4Addr);
74     INetAddr route;
75     route.type_ = INetAddr::IPV4;
76     route.family_ = 0x01;
77     route.prefixlen_ = 0x01;
78     route.address_ = "0.0.0.0";
79     route.netMask_ = "0.0.0.0";
80     route.hostName_ = "netAddr";
81     ic->ipStatic_.routeList_.push_back(route);
82     INetAddr gateway;
83     gateway.type_ = INetAddr::IPV4;
84     gateway.family_ = 0x01;
85     gateway.prefixlen_ = 0x01;
86     gateway.address_ = "172.17.4.1";
87     gateway.netMask_ = "0.0.0.0";
88     gateway.hostName_ = "netAddr";
89     ic->ipStatic_.gatewayList_.push_back(gateway);
90     INetAddr netMask;
91     netMask.type_ = INetAddr::IPV4;
92     netMask.family_ = 0x01;
93     netMask.address_ = "255.255.255.0";
94     netMask.hostName_ = "netAddr";
95     ic->ipStatic_.netMaskList_.push_back(netMask);
96     ic->httpProxy_ = {TEST_PROXY_HOST, TEST_PROXY_PORT, {}};
97     INetAddr dns1;
98     dns1.type_ = INetAddr::IPV4;
99     dns1.family_ = 0x01;
100     dns1.address_ = "8.8.8.8";
101     dns1.hostName_ = "netAddr";
102     INetAddr dns2;
103     dns2.type_ = INetAddr::IPV4;
104     dns2.family_ = 0x01;
105     dns2.address_ = "114.114.114.114";
106     dns2.hostName_ = "netAddr";
107     ic->ipStatic_.dnsServers_.push_back(dns1);
108     ic->ipStatic_.dnsServers_.push_back(dns2);
109     return ic;
110 }
111 
SetUpTestCase()112 void EtherNetServiceProxyTest::SetUpTestCase() {}
113 
TearDownTestCase()114 void EtherNetServiceProxyTest::TearDownTestCase() {}
115 
SetUp()116 void EtherNetServiceProxyTest::SetUp() {}
117 
TearDown()118 void EtherNetServiceProxyTest::TearDown() {}
119 
120 HWTEST_F(EtherNetServiceProxyTest, GetMacAddressTest001, TestSize.Level1)
121 {
122     NetManagerExtAccessToken token;
123     EthernetServiceProxy ethernetServiceProxy(nullptr);
124     std::vector<MacAddressInfo> mai;
125     auto ret = ethernetServiceProxy.GetMacAddress(mai);
126     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
127 }
128 
129 HWTEST_F(EtherNetServiceProxyTest, SetIfaceConfigTest001, TestSize.Level1)
130 {
131     NetManagerExtAccessToken token;
132     EthernetServiceProxy ethernetServiceProxy(nullptr);
133     sptr<InterfaceConfiguration> ic = GetIfaceConfig();
134     auto ret = ethernetServiceProxy.SetIfaceConfig(DEV_NAME, ic);
135     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
136 }
137 
138 HWTEST_F(EtherNetServiceProxyTest, GetIfaceConfigTest001, TestSize.Level1)
139 {
140     NetManagerExtAccessToken token;
141     EthernetServiceProxy ethernetServiceProxy(nullptr);
142     sptr<InterfaceConfiguration> ifaceConfig = new (std::nothrow) InterfaceConfiguration();
143     int32_t ret = ethernetServiceProxy.GetIfaceConfig(DEV_NAME, ifaceConfig);
144     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
145 }
146 
147 HWTEST_F(EtherNetServiceProxyTest, IsIfaceActiveTest001, TestSize.Level1)
148 {
149     NetManagerExtAccessToken token;
150     EthernetServiceProxy ethernetServiceProxy(nullptr);
151     sptr<InterfaceConfiguration> ifaceConfig = new (std::nothrow) InterfaceConfiguration();
152     std::string ifcaeName = "eth0";
153     int32_t activeStatus = -1;
154     int32_t ret = ethernetServiceProxy.IsIfaceActive(ifcaeName, activeStatus);
155     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
156 }
157 
158 HWTEST_F(EtherNetServiceProxyTest, GetAllActiveIfacesTest001, TestSize.Level1)
159 {
160     NetManagerExtAccessToken token;
161     EthernetServiceProxy ethernetServiceProxy(nullptr);
162     std::vector<std::string> result;
163     int32_t ret = ethernetServiceProxy.GetAllActiveIfaces(result);
164     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
165 }
166 
167 HWTEST_F(EtherNetServiceProxyTest, ResetFactoryTest001, TestSize.Level1)
168 {
169     EthernetServiceProxy ethernetServiceProxy(nullptr);
170     int32_t ret = ethernetServiceProxy.ResetFactory();
171     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
172 }
173 
174 HWTEST_F(EtherNetServiceProxyTest, SetInterfaceUpTest001, TestSize.Level1)
175 {
176     EthernetServiceProxy ethernetServiceProxy(nullptr);
177     NetManagerExtAccessToken token;
178     int32_t ret = ethernetServiceProxy.SetInterfaceUp(DEV_NAME);
179     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
180     OHOS::nmd::InterfaceConfigurationParcel cfg;
181     ret = ethernetServiceProxy.GetInterfaceConfig(DEV_NAME, cfg);
182     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
183 }
184 
185 HWTEST_F(EtherNetServiceProxyTest, SetInterfaceDownTest001, TestSize.Level1)
186 {
187     EthernetServiceProxy ethernetServiceProxy(nullptr);
188     NetManagerExtAccessToken token;
189     int32_t ret = ethernetServiceProxy.SetInterfaceDown(DEV_NAME);
190     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
191 }
192 
193 HWTEST_F(EtherNetServiceProxyTest, SetInterfaceConfigTest001, TestSize.Level1)
194 {
195     EthernetServiceProxy ethernetServiceProxy(nullptr);
196     NetManagerExtAccessToken token;
197     OHOS::nmd::InterfaceConfigurationParcel config;
198     config.ifName = "eth0";
199     config.hwAddr = "";
200     config.ipv4Addr = "172.17.5.234";
201     config.prefixLength = 24;
202     config.flags.push_back("up");
203     config.flags.push_back("broadcast");
204     int32_t ret = ethernetServiceProxy.SetInterfaceConfig(DEV_NAME, config);
205     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
206 }
207 } // namespace NetManagerStandard
208 } // namespace OHOS