1 /*
2 * Copyright (c) 2021 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 "netmgr_ext_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23 using namespace testing::ext;
24 class EthernetManagerTest : public testing::Test {
25 public:
26 static void SetUpTestCase();
27 static void TearDownTestCase();
28 void SetUp();
29 void TearDown();
30 sptr<InterfaceConfiguration> GetIfaceConfig();
31 };
32
SetUpTestCase()33 void EthernetManagerTest::SetUpTestCase() {}
34
TearDownTestCase()35 void EthernetManagerTest::TearDownTestCase() {}
36
SetUp()37 void EthernetManagerTest::SetUp() {}
38
TearDown()39 void EthernetManagerTest::TearDown() {}
40
GetIfaceConfig()41 sptr<InterfaceConfiguration> EthernetManagerTest::GetIfaceConfig()
42 {
43 sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
44 if (!ic) {
45 return ic;
46 }
47 ic->ipStatic_.ipAddr_.type_ = INetAddr::IPV4;
48 ic->ipStatic_.ipAddr_.family_ = 0x01;
49 ic->ipStatic_.ipAddr_.prefixlen_ = 0x01;
50 ic->ipStatic_.ipAddr_.address_ = "172.17.5.234";
51 ic->ipStatic_.ipAddr_.netMask_ = "255.255.254.0";
52 ic->ipStatic_.ipAddr_.hostName_ = "netAddr";
53 ic->ipStatic_.route_.type_ = INetAddr::IPV4;
54 ic->ipStatic_.route_.family_ = 0x01;
55 ic->ipStatic_.route_.prefixlen_ = 0x01;
56 ic->ipStatic_.route_.address_ = "0.0.0.0";
57 ic->ipStatic_.route_.netMask_ = "0.0.0.0";
58 ic->ipStatic_.route_.hostName_ = "netAddr";
59 ic->ipStatic_.gateway_.type_ = INetAddr::IPV4;
60 ic->ipStatic_.gateway_.family_ = 0x01;
61 ic->ipStatic_.gateway_.prefixlen_ = 0x01;
62 ic->ipStatic_.gateway_.address_ = "172.17.4.1";
63 ic->ipStatic_.gateway_.netMask_ = "0.0.0.0";
64 ic->ipStatic_.gateway_.hostName_ = "netAddr";
65 ic->ipStatic_.netMask_.type_ = INetAddr::IPV4;
66 ic->ipStatic_.netMask_.family_ = 0x01;
67 ic->ipStatic_.netMask_.netMask_ = "255.255.255.0";
68 ic->ipStatic_.netMask_.hostName_ = "netAddr";
69 INetAddr dns1;
70 dns1.type_ = INetAddr::IPV4;
71 dns1.family_ = 0x01;
72 dns1.address_ = "8.8.8.8";
73 dns1.hostName_ = "netAddr";
74 INetAddr dns2;
75 dns2.type_ = INetAddr::IPV4;
76 dns2.family_ = 0x01;
77 dns2.address_ = "114.114.114.114";
78 dns2.hostName_ = "netAddr";
79 ic->ipStatic_.dnsServers_.push_back(dns1);
80 ic->ipStatic_.dnsServers_.push_back(dns2);
81 return ic;
82 }
83
84 /**
85 * @tc.name: EthernetManager001
86 * @tc.desc: Test EthernetManager SetIfaceConfig.
87 * @tc.type: FUNC
88 */
89 HWTEST_F(EthernetManagerTest, EthernetManager001, TestSize.Level1)
90 {
91 std::string iface = "eth0";
92 sptr<InterfaceConfiguration> ic = GetIfaceConfig();
93 DelayedSingleton<EthernetClient>::GetInstance()->SetIfaceConfig(iface, ic);
94 ASSERT_TRUE(true);
95 }
96
97 /**
98 * @tc.name: EthernetManager002
99 * @tc.desc: Test EthernetManager GetIfaceConfig.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(EthernetManagerTest, EthernetManager002, TestSize.Level1)
103 {
104 std::string iface = "eth0";
105 sptr<InterfaceConfiguration> ic =
106 DelayedSingleton<EthernetClient>::GetInstance()->GetIfaceConfig(iface);
107 ASSERT_TRUE(ic != nullptr);
108 }
109
110 /**
111 * @tc.name: EthernetManager003
112 * @tc.desc: Test EthernetManager IsIfaceActive.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(EthernetManagerTest, EthernetManager003, TestSize.Level1)
116 {
117 std::string iface = "eth0";
118 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->IsIfaceActive(iface);
119 ASSERT_TRUE(result != -1);
120 }
121
122 /**
123 * @tc.name: EthernetManager004
124 * @tc.desc: Test EthernetManager GetAllActiveIfaces.
125 * @tc.type: FUNC
126 */
127 HWTEST_F(EthernetManagerTest, EthernetManager004, TestSize.Level1)
128 {
129 std::vector<std::string> result = DelayedSingleton<EthernetClient>::GetInstance()->GetAllActiveIfaces();
130 for (std::string& s : result) {
131 std::cout << s << ","<< std::endl;
132 }
133 }
134
135 /**
136 * @tc.name: EthernetManager005
137 * @tc.desc: Test EthernetManager ResetFactory.
138 * @tc.type: FUNC
139 */
140 HWTEST_F(EthernetManagerTest, EthernetManager005, TestSize.Level1)
141 {
142 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->ResetFactory();
143 ASSERT_TRUE(result == 0);
144 }
145 } // namespace NetManagerStandard
146 } // namespace OHOS
147