• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace testing::ext;
25 class NetLinkInfoTest : public testing::Test {
26 public:
27     static void SetUpTestCase();
28     static void TearDownTestCase();
29     void SetUp();
30     void TearDown();
31 };
32 
SetUpTestCase()33 void NetLinkInfoTest::SetUpTestCase() {}
34 
TearDownTestCase()35 void NetLinkInfoTest::TearDownTestCase() {}
36 
SetUp()37 void NetLinkInfoTest::SetUp() {}
38 
TearDown()39 void NetLinkInfoTest::TearDown() {}
40 
GetNetLinkInfo()41 sptr<NetLinkInfo> GetNetLinkInfo()
42 {
43     sptr<NetLinkInfo> netLinkInfo = (std::make_unique<NetLinkInfo>()).release();
44     netLinkInfo->ifaceName_ = "test";
45     netLinkInfo->domain_ = "test";
46 
47     sptr<INetAddr> netAddr = (std::make_unique<INetAddr>()).release();
48     netAddr->type_ = INetAddr::IPV4;
49     netAddr->family_ = 0x10;
50     netAddr->prefixlen_ = 0x17;
51     netAddr->address_ = "0.0.0.0";
52     netAddr->netMask_ = "0.0.0.0";
53     netAddr->hostName_ = "netAddr";
54     netLinkInfo->netAddrList_.push_back(*netAddr);
55 
56     sptr<Route> route = (std::make_unique<Route>()).release();
57     route->iface_ = "iface0";
58     route->destination_.type_ = INetAddr::IPV4;
59     route->destination_.family_ = 0x10;
60     route->destination_.prefixlen_ = 0x17;
61     route->destination_.address_ = "0.0.0.0";
62     route->destination_.netMask_ = "0.0.0.0";
63     route->destination_.hostName_ = "netAddr";
64     route->gateway_.type_ = INetAddr::IPV4;
65     route->gateway_.family_ = 0x10;
66     route->gateway_.prefixlen_ = 0x17;
67     route->gateway_.address_ = "0.0.0.0";
68     route->gateway_.netMask_ = "0.0.0.0";
69     route->gateway_.hostName_ = "netAddr";
70     netLinkInfo->routeList_.push_back(*route);
71 
72     netLinkInfo->mtu_ = 0x5DC;
73     return netLinkInfo;
74 }
75 
76 /**
77  * @tc.name: UnmarshallingTest
78  * @tc.desc: Test NetLinkInfo::Marshalling
79  * @tc.type: FUNC
80  */
81 HWTEST_F(NetLinkInfoTest, UnmarshallingTest, TestSize.Level1)
82 {
83     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
84     ASSERT_TRUE(netLinkInfo != nullptr);
85 
86     MessageParcel data;
87     sptr<NetLinkInfo> netLinkInfo_ptr = nullptr;
88     bool bRet = NetLinkInfo::Marshalling(data, netLinkInfo);
89     ASSERT_TRUE(bRet == true);
90 
91     netLinkInfo_ptr = NetLinkInfo::Unmarshalling(data);
92     ASSERT_TRUE(netLinkInfo_ptr != nullptr);
93 }
94 
95 /**
96  * @tc.name: InitializeTest
97  * @tc.desc: Test NetLinkInfo::Initialize
98  * @tc.type: FUNC
99  */
100 HWTEST_F(NetLinkInfoTest, InitializeTest, TestSize.Level1)
101 {
102     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
103     ASSERT_TRUE(netLinkInfo != nullptr);
104     netLinkInfo->Initialize();
105 }
106 
107 /**
108  * @tc.name: ToStringTest
109  * @tc.desc: Test NetLinkInfo::ToString
110  * @tc.type: FUNC
111  */
112 HWTEST_F(NetLinkInfoTest, ToStringTest, TestSize.Level1)
113 {
114     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
115     std::string str = netLinkInfo->ToString("testTab");
116     NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
117 }
118 
119 /**
120  * @tc.name: ToStringAddrTest
121  * @tc.desc: Test NetLinkInfo::ToStringAddr
122  * @tc.type: FUNC
123  */
124 HWTEST_F(NetLinkInfoTest, ToStringAddrTest, TestSize.Level1)
125 {
126     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
127     std::string str = netLinkInfo->ToStringAddr("testAddrTab");
128     NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
129 }
130 
131 /**
132  * @tc.name: ToStringDnsTest
133  * @tc.desc: Test NetLinkInfo::ToStringDns
134  * @tc.type: FUNC
135  */
136 HWTEST_F(NetLinkInfoTest, ToStringDnsTest, TestSize.Level1)
137 {
138     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
139     std::string str = netLinkInfo->ToStringDns("testDnsTab");
140     NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
141 }
142 
143 /**
144  * @tc.name: ToStringRouteTest
145  * @tc.desc: Test NetLinkInfo::ToStringRoute
146  * @tc.type: FUNC
147  */
148 HWTEST_F(NetLinkInfoTest, ToStringRouteTest, TestSize.Level1)
149 {
150     sptr<NetLinkInfo> netLinkInfo = GetNetLinkInfo();
151     std::string str = netLinkInfo->ToStringRoute("testRouteTab");
152     NETMGR_LOG_D("netLinkInfo.ToString string is : [%{public}s]", str.c_str());
153 }
154 } // namespace NetManagerStandard
155 } // namespace OHOS
156