1 /*
2 * Copyright (c) 2022 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 "message_parcel.h"
19 #include "net_link_info.h"
20 #include "net_mgr_log_wrapper.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing::ext;
26 constexpr const char *TEST_IPV4_ADDR = "127.0.0.1";
27 } // namespace
28 class NetLinkInfoTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase()36 void NetLinkInfoTest::SetUpTestCase() {}
37
TearDownTestCase()38 void NetLinkInfoTest::TearDownTestCase() {}
39
SetUp()40 void NetLinkInfoTest::SetUp() {}
41
TearDown()42 void NetLinkInfoTest::TearDown() {}
43
GetNetLinkInfo()44 sptr<NetLinkInfo> GetNetLinkInfo()
45 {
46 sptr<NetLinkInfo> netLinkInfo = (std::make_unique<NetLinkInfo>()).release();
47 netLinkInfo->ifaceName_ = "test";
48 netLinkInfo->domain_ = "test";
49
50 sptr<INetAddr> netAddr = (std::make_unique<INetAddr>()).release();
51 netAddr->type_ = INetAddr::IPV4;
52 netAddr->family_ = 0x10;
53 netAddr->prefixlen_ = 0x17;
54 netAddr->address_ = "0.0.0.0";
55 netAddr->netMask_ = "0.0.0.0";
56 netAddr->hostName_ = "netAddr";
57 netLinkInfo->netAddrList_.push_back(*netAddr);
58
59 sptr<Route> route = (std::make_unique<Route>()).release();
60 route->iface_ = "iface0";
61 route->destination_.type_ = INetAddr::IPV4;
62 route->destination_.family_ = 0x10;
63 route->destination_.prefixlen_ = 0x17;
64 route->destination_.address_ = "0.0.0.0";
65 route->destination_.netMask_ = "0.0.0.0";
66 route->destination_.hostName_ = "netAddr";
67 route->gateway_.type_ = INetAddr::IPV4;
68 route->gateway_.family_ = 0x10;
69 route->gateway_.prefixlen_ = 0x17;
70 route->gateway_.address_ = "0.0.0.0";
71 route->gateway_.netMask_ = "0.0.0.0";
72 route->gateway_.hostName_ = "netAddr";
73 netLinkInfo->routeList_.push_back(*route);
74
75 netLinkInfo->mtu_ = 0x5DC;
76
77 netLinkInfo->httpProxy_ = {TEST_IPV4_ADDR, 80, {"localhost"}};
78 return netLinkInfo;
79 }
80
81 /**
82 * @tc.name: UnmarshallingTest
83 * @tc.desc: Test NetLinkInfo::Marshalling
84 * @tc.type: FUNC
85 */
86 HWTEST_F(NetLinkInfoTest, UnmarshallingTest, TestSize.Level1)
87 {
88 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
89 ASSERT_TRUE(netLinkInfo != nullptr);
90
91 MessageParcel data;
92 sptr<NetLinkInfo> netLinkInfo_ptr = nullptr;
93 bool bRet = NetLinkInfo::Marshalling(data, netLinkInfo);
94 ASSERT_TRUE(bRet == true);
95
96 netLinkInfo_ptr = NetLinkInfo::Unmarshalling(data);
97 ASSERT_TRUE(netLinkInfo_ptr != nullptr);
98 }
99
100 /**
101 * @tc.name: InitializeTest
102 * @tc.desc: Test NetLinkInfo::Initialize
103 * @tc.type: FUNC
104 */
105 HWTEST_F(NetLinkInfoTest, InitializeTest, TestSize.Level1)
106 {
107 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
108 ASSERT_TRUE(netLinkInfo != nullptr);
109 netLinkInfo->Initialize();
110 }
111
112 /**
113 * @tc.name: ToStringTest
114 * @tc.desc: Test NetLinkInfo::ToString
115 * @tc.type: FUNC
116 */
117 HWTEST_F(NetLinkInfoTest, ToStringTest, TestSize.Level1)
118 {
119 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
120 std::string str = netLinkInfo->ToString("testTab");
121 int32_t ret = 0;
122 NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
123 if (str.c_str() != nullptr) {
124 ret = 1;
125 }
126 EXPECT_EQ(ret, 1);
127 }
128
129 /**
130 * @tc.name: ToStringAddrTest
131 * @tc.desc: Test NetLinkInfo::ToStringAddr
132 * @tc.type: FUNC
133 */
134 HWTEST_F(NetLinkInfoTest, ToStringAddrTest, TestSize.Level1)
135 {
136 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
137 std::string str = netLinkInfo->ToStringAddr("testAddrTab");
138 int32_t ret = 0;
139 NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
140 if (str.c_str() != nullptr) {
141 ret = 1;
142 }
143 EXPECT_EQ(ret, 1);
144 }
145
146 /**
147 * @tc.name: ToStringDnsTest
148 * @tc.desc: Test NetLinkInfo::ToStringDns
149 * @tc.type: FUNC
150 */
151 HWTEST_F(NetLinkInfoTest, ToStringDnsTest, TestSize.Level1)
152 {
153 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
154 std::string str = netLinkInfo->ToStringDns("testDnsTab");
155 int32_t ret = 0;
156 NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
157 if (str.c_str() != nullptr) {
158 ret = 1;
159 }
160 EXPECT_EQ(ret, 1);
161 }
162
163 /**
164 * @tc.name: ToStringRouteTest
165 * @tc.desc: Test NetLinkInfo::ToStringRoute
166 * @tc.type: FUNC
167 */
168 HWTEST_F(NetLinkInfoTest, ToStringRouteTest, TestSize.Level1)
169 {
170 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
171 std::string str = netLinkInfo->ToStringRoute("testRouteTab");
172 int32_t ret = 0;
173 NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
174 if (str.c_str() != nullptr) {
175 ret = 1;
176 }
177 EXPECT_EQ(ret, 1);
178 }
179
180 /**
181 * @tc.name: operatorAndMarshalling
182 * @tc.desc: Test NetLinkInfo::operatorAndMarshalling
183 * @tc.type: FUNC
184 */
185 HWTEST_F(NetLinkInfoTest, operatorAndMarshallingTest, TestSize.Level1)
186 {
187 sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
188 ASSERT_TRUE(netLinkInfo != nullptr);
189 NetLinkInfo netLinkInfoa = *netLinkInfo;
190 EXPECT_EQ(netLinkInfoa.domain_, "test");
191 Parcel data;
192 bool bRet = netLinkInfo->Marshalling(data);
193 ASSERT_TRUE(bRet == true);
194 }
195 } // namespace NetManagerStandard
196 } // namespace OHOS
197