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