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 #include <iostream>
18
19 #include "net_manager_constants.h"
20 #include "net_policy_callback_stub.h"
21 #include "net_policy_inner_define.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 namespace {
26 using namespace testing::ext;
27 constexpr uint32_t TEST_UID = 4454;
28 constexpr uint32_t TEST_POLICY = 2121;
29 constexpr uint32_t TEST_RULE = 441;
30 class NetPolicyCbStubTest : public NetPolicyCallbackStub {
31 public:
32 NetPolicyCbStubTest() = default;
~NetPolicyCbStubTest()33 ~NetPolicyCbStubTest() override {}
NetUidPolicyChange(uint32_t uid,uint32_t policy)34 int32_t NetUidPolicyChange(uint32_t uid, uint32_t policy) override
35 {
36 return 0;
37 }
38
NetUidRuleChange(uint32_t uid,uint32_t rule)39 int32_t NetUidRuleChange(uint32_t uid, uint32_t rule) override
40 {
41 std::cout << "licheng--Stub NetUidRuleChange" << std::endl;
42 return 0;
43 }
44
NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> & quotaPolicies)45 int32_t NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> "aPolicies) override
46 {
47 return 0;
48 }
49
NetStrategySwitch(const std::string & simId,bool enable)50 int32_t NetStrategySwitch(const std::string &simId, bool enable) override
51 {
52 return 0;
53 }
54
NetMeteredIfacesChange(std::vector<std::string> & ifaces)55 int32_t NetMeteredIfacesChange(std::vector<std::string> &ifaces) override
56 {
57 return 0;
58 }
59
NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)60 int32_t NetBackgroundPolicyChange(bool isBackgroundPolicyAllow) override
61 {
62 return 0;
63 }
64 };
65 } // namespace
66
67 class UtNetPolicyCallbackStubTest : public testing::Test {
68 public:
69 static void SetUpTestCase();
70 static void TearDownTestCase();
71 void SetUp();
72 void TearDown();
73 static inline std::shared_ptr<NetPolicyCbStubTest> instance_ = std::make_shared<NetPolicyCbStubTest>();
74 };
75
SetUpTestCase()76 void UtNetPolicyCallbackStubTest::SetUpTestCase() {}
77
TearDownTestCase()78 void UtNetPolicyCallbackStubTest::TearDownTestCase() {}
79
SetUp()80 void UtNetPolicyCallbackStubTest::SetUp() {}
81
TearDown()82 void UtNetPolicyCallbackStubTest::TearDown() {}
83
84 HWTEST_F(UtNetPolicyCallbackStubTest, OnNetUidPolicyChangeTest001, TestSize.Level1)
85 {
86 uint32_t uid = TEST_UID;
87 uint32_t policy = TEST_POLICY;
88 MessageParcel data;
89 if (!data.WriteInterfaceToken(NetPolicyCallbackStub::GetDescriptor())) {
90 return;
91 }
92 if (!data.WriteUint32(uid)) {
93 return;
94 }
95 if (!data.WriteUint32(policy)) {
96 return;
97 }
98 MessageParcel reply;
99 MessageOption option;
100 int32_t ret = instance_->OnRemoteRequest(100, data, reply, option);
101 EXPECT_NE(ret, 0);
102
103 ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_POLICY_CHANGE),
104 data, reply, option);
105 EXPECT_NE(ret, 0);
106
107 MessageParcel dataOk;
108 if (!dataOk.WriteInterfaceToken(NetPolicyCallbackStub::GetDescriptor())) {
109 return;
110 }
111 if (!dataOk.WriteUint32(uid)) {
112 return;
113 }
114 if (!dataOk.WriteUint32(policy)) {
115 return;
116 }
117 ret = instance_->OnRemoteRequest(static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_POLICY_CHANGE),
118 dataOk, reply, option);
119 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
120 }
121
122 HWTEST_F(UtNetPolicyCallbackStubTest, OnNetUidRuleChangeTest001, TestSize.Level1)
123 {
124 uint32_t uid = TEST_UID;
125 uint32_t rule = TEST_RULE;
126 MessageParcel data;
127 if (!data.WriteInterfaceToken(NetPolicyCallbackStub::GetDescriptor())) {
128 return;
129 }
130 if (!data.WriteUint32(uid)) {
131 return;
132 }
133 if (!data.WriteUint32(rule)) {
134 return;
135 }
136 MessageParcel reply;
137 MessageOption option;
138 int32_t ret = instance_->OnRemoteRequest(
139 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_RULE_CHANGE), data, reply, option);
140 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
141 }
142
143 HWTEST_F(UtNetPolicyCallbackStubTest, OnNetQuotaPolicyChangeTest001, TestSize.Level1)
144 {
145 std::vector<NetQuotaPolicy> cellularPolicies;
146 NetQuotaPolicy policy;
147 policy.networkmatchrule.simId = "testIccid";
148 policy.networkmatchrule.ident = "testIdent";
149 policy.quotapolicy.title = "testTitle";
150 cellularPolicies.push_back(policy);
151 MessageParcel data;
152 if (!data.WriteInterfaceToken(NetPolicyCallbackStub::GetDescriptor())) {
153 return;
154 }
155 if (!NetQuotaPolicy::Marshalling(data, cellularPolicies)) {
156 return;
157 }
158
159 MessageParcel reply;
160 MessageOption option;
161 int32_t ret = instance_->OnRemoteRequest(
162 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_QUOTA_POLICY_CHANGE), data, reply, option);
163 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
164 }
165
166 HWTEST_F(UtNetPolicyCallbackStubTest, OnNetMeteredIfacesChangeTest001, TestSize.Level1)
167 {
168 std::vector<std::string> ifNames;
169 ifNames.push_back("test0");
170 ifNames.push_back("test1");
171 MessageParcel data;
172 if (!data.WriteInterfaceToken(NetPolicyCallbackStub::GetDescriptor())) {
173 return;
174 }
175 if (!data.WriteUint32(ifNames.size())) {
176 return;
177 }
178 for (int32_t idx = 0; idx < ifNames.size(); idx++) {
179 if (!data.WriteString(ifNames[idx])) {
180 return;
181 }
182 }
183
184 MessageParcel reply;
185 MessageOption option;
186 int32_t ret = instance_->OnRemoteRequest(
187 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_METERED_IFACES_CHANGE), data, reply, option);
188 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
189 }
190
191 HWTEST_F(UtNetPolicyCallbackStubTest, NotifyNetQuotaPolicyChangeTest002, TestSize.Level1)
192 {
193 bool isBackgroundPolicyAllow = false;
194 MessageParcel data;
195 if (!data.WriteInterfaceToken(NetPolicyCallbackStub::GetDescriptor())) {
196 return;
197 }
198 if (!data.WriteBool(isBackgroundPolicyAllow)) {
199 return;
200 }
201
202 MessageParcel reply;
203 MessageOption option;
204 int32_t ret = instance_->OnRemoteRequest(
205 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_BACKGROUND_POLICY_CHANGE), data, reply, option);
206 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
207 }
208 } // namespace NetManagerStandard
209 } // namespace OHOS
210