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