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 "net_manager_constants.h"
19 #include "netsys_policy_wrapper.h"
20 #include "iptables_type.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 constexpr const char *POLICY_FILE_NAME = "/data/service/el1/public/netmanager/net_policy.json";
26 using namespace testing::ext;
27 } // namespace
28
29 class NetsysPolicyWrapperTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 static inline auto instance_ = DelayedSingleton<NetsysPolicyWrapper>::GetInstance();
36 };
37
SetUpTestCase()38 void NetsysPolicyWrapperTest::SetUpTestCase() {}
39
TearDownTestCase()40 void NetsysPolicyWrapperTest::TearDownTestCase() {}
41
SetUp()42 void NetsysPolicyWrapperTest::SetUp() {}
43
TearDown()44 void NetsysPolicyWrapperTest::TearDown() {}
45
46 HWTEST_F(NetsysPolicyWrapperTest, RegisterNetsysCallbackTest001, TestSize.Level1)
47 {
48 sptr<NetsysControllerCallback> callback = nullptr;
49 auto ret = instance_->RegisterNetsysCallback(callback);
50 EXPECT_GE(ret, NETMANAGER_SUCCESS);
51 }
52
53 HWTEST_F(NetsysPolicyWrapperTest, BandwidthEnableDataSaverTest001, TestSize.Level1)
54 {
55 auto ret = instance_->BandwidthEnableDataSaver(false);
56 EXPECT_LE(ret, 0);
57 }
58
59 HWTEST_F(NetsysPolicyWrapperTest, BandwidthSetIfaceQuotaTest001, TestSize.Level1)
60 {
61 std::string iface = "testIface";
62 int64_t bytes = 666;
63 auto ret = instance_->BandwidthSetIfaceQuota(iface, bytes);
64 EXPECT_GE(ret, NETMANAGER_SUCCESS);
65 }
66
67 HWTEST_F(NetsysPolicyWrapperTest, BandwidthRemoveIfaceQuotaTest001, TestSize.Level1)
68 {
69 std::string iface = "testIface";
70 auto ret = instance_->BandwidthRemoveIfaceQuota(iface);
71 EXPECT_GE(ret, NETMANAGER_SUCCESS);
72 }
73
74 HWTEST_F(NetsysPolicyWrapperTest, BandwidthAddDeniedListTest001, TestSize.Level1)
75 {
76 uint32_t uid = 666;
77 auto ret = instance_->BandwidthAddDeniedList(uid);
78 EXPECT_GE(ret, NETMANAGER_SUCCESS);
79 }
80
81 HWTEST_F(NetsysPolicyWrapperTest, BandwidthRemoveDeniedListTest001, TestSize.Level1)
82 {
83 uint32_t uid = 666;
84 auto ret = instance_->BandwidthRemoveDeniedList(uid);
85 EXPECT_GE(ret, NETMANAGER_SUCCESS);
86 }
87
88 HWTEST_F(NetsysPolicyWrapperTest, BandwidthAddAllowedListTest001, TestSize.Level1)
89 {
90 uint32_t uid = 666;
91 auto ret = instance_->BandwidthAddAllowedList(uid);
92 EXPECT_GE(ret, NETMANAGER_SUCCESS);
93 }
94
95 HWTEST_F(NetsysPolicyWrapperTest, BandwidthRemoveAllowedListTest001, TestSize.Level1)
96 {
97 uint32_t uid = 666;
98 auto ret = instance_->BandwidthRemoveAllowedList(uid);
99 EXPECT_GE(ret, NETMANAGER_SUCCESS);
100 }
101
102 HWTEST_F(NetsysPolicyWrapperTest, PowerSaveUpdataAllowedListTest001, TestSize.Level1)
103 {
104 uint32_t uid = 666;
105 auto ret = instance_->PowerSaveUpdataAllowedList(uid, FirewallRule::RULE_ALLOW);
106 EXPECT_GE(ret, NETMANAGER_SUCCESS);
107 ret = instance_->PowerSaveUpdataAllowedList(uid, FirewallRule::RULE_DENY);
108 EXPECT_GE(ret, NETMANAGER_SUCCESS);
109 }
110
111 HWTEST_F(NetsysPolicyWrapperTest, FirewallSetUidsAllowedListChainTest001, TestSize.Level1)
112 {
113 uint32_t chain = 2;
114 std::vector<uint32_t> uids;
115 auto ret = instance_->FirewallSetUidsAllowedListChain(chain, uids);
116 EXPECT_LE(ret, 0);
117 }
118
119 HWTEST_F(NetsysPolicyWrapperTest, FirewallSetUidsDeniedListChainTest001, TestSize.Level1)
120 {
121 uint32_t chain = 2;
122 std::vector<uint32_t> uids;
123 auto ret = instance_->FirewallSetUidsDeniedListChain(chain, uids);
124 EXPECT_LE(ret, 0);
125 }
126
127 HWTEST_F(NetsysPolicyWrapperTest, FirewallSetUidRuleTest001, TestSize.Level1)
128 {
129 uint32_t chain = 2;
130 uint32_t uid = 666;
131 uint32_t firewallRule = 2;
132 auto ret = instance_->FirewallSetUidRule(chain, {uid}, firewallRule);
133 EXPECT_LE(ret, 0);
134 }
135
136 HWTEST_F(NetsysPolicyWrapperTest, FirewallEnableChainTest001, TestSize.Level1)
137 {
138 uint32_t chain = 2;
139 auto ret = instance_->FirewallEnableChain(chain, false);
140 EXPECT_LE(ret, 0);
141 std::remove(POLICY_FILE_NAME);
142 }
143
144 HWTEST_F(NetsysPolicyWrapperTest, SetNetworkAccessPolicyTest001, TestSize.Level1)
145 {
146 uint32_t uid = 666;
147 NetworkAccessPolicy netAccessPolicy;
148 netAccessPolicy.wifiAllow = false;
149 netAccessPolicy.cellularAllow = false;
150 bool reconfirmFlag = true;
151 auto ret = instance_->SetNetworkAccessPolicy(uid, netAccessPolicy, reconfirmFlag);
152 EXPECT_GE(ret, NETMANAGER_SUCCESS);
153 }
154
155 HWTEST_F(NetsysPolicyWrapperTest, DeleteNetworkAccessPolicyTest001, TestSize.Level1)
156 {
157 uint32_t uid = 666;
158 auto ret = instance_->DeleteNetworkAccessPolicy(uid);
159 EXPECT_GE(ret, NETMANAGER_SUCCESS);
160 }
161 } // namespace NetManagerStandard
162 } // namespace OHOS