• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22 
23 #include "net_manager_constants.h"
24 #include "net_manager_native.h"
25 
26 namespace OHOS {
27 namespace nmd {
28 namespace {
29 using namespace testing::ext;
30 }
31 
32 class NetManagerNativeTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38     static inline std::shared_ptr<NetManagerNative> instance_ = nullptr;
39 };
40 
SetUpTestCase()41 void NetManagerNativeTest::SetUpTestCase()
42 {
43     instance_ = std::make_shared<NetManagerNative>();
44 }
45 
TearDownTestCase()46 void NetManagerNativeTest::TearDownTestCase() {}
47 
SetUp()48 void NetManagerNativeTest::SetUp() {}
49 
TearDown()50 void NetManagerNativeTest::TearDown() {}
51 
52 HWTEST_F(NetManagerNativeTest, UpdateInterfaceIndex001, TestSize.Level1)
53 {
54     uint32_t interfaceIndex = 10090;
55     instance_->UpdateInterfaceIndex(interfaceIndex);
56     auto result = instance_->GetCurrentInterfaceIndex();
57     auto findResult = std::find(result.begin(), result.end(), interfaceIndex);
58     EXPECT_NE(findResult, result.end());
59 }
60 
61 HWTEST_F(NetManagerNativeTest, SetInternetPermission001, TestSize.Level1)
62 {
63     uint32_t uid = 0;
64     uint8_t allow = 1;
65     auto ret = instance_->SetInternetPermission(uid, allow);
66     EXPECT_EQ(ret, NETMANAGER_ERROR);
67 }
68 
69 HWTEST_F(NetManagerNativeTest, NetworkCreateVirtual001, TestSize.Level1)
70 {
71     int32_t netid = 12235;
72     bool hasDns = false;
73     auto ret = instance_->NetworkCreateVirtual(netid, hasDns);
74     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
75 }
76 
77 HWTEST_F(NetManagerNativeTest, NetworkAddUids001, TestSize.Level1)
78 {
79     int32_t netId = 12235;
80     std::vector<UidRange> uidRanges;
81     auto ret = instance_->NetworkAddUids(netId, uidRanges);
82     EXPECT_EQ(ret, 0);
83 }
84 
85 HWTEST_F(NetManagerNativeTest, NetworkDelUids001, TestSize.Level1)
86 {
87     int32_t netId = 12235;
88     std::vector<UidRange> uidRanges;
89     auto ret = instance_->NetworkDelUids(netId, uidRanges);
90     EXPECT_EQ(ret, 0);
91 }
92 
93 HWTEST_F(NetManagerNativeTest, NetworkSetPermissionForNetwork001, TestSize.Level1)
94 {
95     int32_t netId = 12235;
96     auto ret = instance_->NetworkSetPermissionForNetwork(netId, NetworkPermission::PERMISSION_NONE);
97     EXPECT_EQ(ret, NETMANAGER_ERROR);
98 }
99 
100 HWTEST_F(NetManagerNativeTest, GetCellularRxBytes001, TestSize.Level1)
101 {
102     auto ret = instance_->GetCellularRxBytes();
103     EXPECT_EQ(ret, 0);
104 }
105 
106 HWTEST_F(NetManagerNativeTest, GetCellularTxBytes001, TestSize.Level1)
107 {
108     auto ret = instance_->GetCellularTxBytes();
109     EXPECT_EQ(ret, 0);
110 }
111 
112 HWTEST_F(NetManagerNativeTest, GetAllRxBytes001, TestSize.Level1)
113 {
114     auto ret = instance_->GetAllRxBytes();
115     EXPECT_GE(ret, 0);
116 }
117 
118 HWTEST_F(NetManagerNativeTest, GetAllTxBytes001, TestSize.Level1)
119 {
120     auto ret = instance_->GetAllTxBytes();
121     EXPECT_GE(ret, 0);
122 }
123 
124 HWTEST_F(NetManagerNativeTest, GetUidTxBytes001, TestSize.Level1)
125 {
126     int32_t uid = 10012;
127     auto ret = instance_->GetUidTxBytes(uid);
128     EXPECT_EQ(ret, 0);
129 }
130 
131 HWTEST_F(NetManagerNativeTest, GetUidRxBytes001, TestSize.Level1)
132 {
133     int32_t uid = 10012;
134     auto ret = instance_->GetUidRxBytes(uid);
135     EXPECT_EQ(ret, 0);
136 }
137 
138 HWTEST_F(NetManagerNativeTest, GetIfaceRxBytes001, TestSize.Level1)
139 {
140     std::string testIface = "testIface";
141     auto ret = instance_->GetIfaceRxBytes(testIface);
142     EXPECT_EQ(ret, 0);
143 }
144 
145 HWTEST_F(NetManagerNativeTest, GetIfaceTxBytes001, TestSize.Level1)
146 {
147     std::string testIface = "testIface";
148     auto ret = instance_->GetIfaceTxBytes(testIface);
149     EXPECT_EQ(ret, 0);
150 }
151 
152 HWTEST_F(NetManagerNativeTest, FirewallSetUidRule001, TestSize.Level1)
153 {
154     uint32_t chain = NetManagerStandard::ChainType::CHAIN_NONE;
155     std::vector<uint32_t> uids;
156     uint32_t firewallRule = NetManagerStandard::FirewallRule::RULE_ALLOW;
157     auto ret = instance_->FirewallSetUidRule(chain, uids, firewallRule);
158     EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS);
159 }
160 
161 HWTEST_F(NetManagerNativeTest, DnsGetAddrInfo001, TestSize.Level1)
162 {
163     std::string hostName;
164     std::string serverName;
165     AddrInfo hints;
166     uint16_t netId = 0;
167     std::vector<AddrInfo> res;
168     auto ret = instance_->DnsGetAddrInfo(hostName, serverName, hints, netId, res);
169     EXPECT_EQ(ret, EAI_NONAME);
170 }
171 } // namespace nmd
172 } // namespace OHOS
173