1 /*
2 * Copyright (C) 2021-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 "net_policy_db_clone.h"
17
18 #include <gtest/gtest.h>
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "net_policy_rule.h"
22 #include "net_bundle_impl.h"
23 #include "bundle_mgr_interface.h"
24 #include "bundle_mgr_proxy.h"
25 #include "net_access_policy_rdb.h"
26 #define private public
27
28 namespace OHOS {
29 namespace NetManagerStandard {
30 using namespace testing::ext;
31 namespace {
GetBundleMgrProxy()32 sptr<AppExecFwk::BundleMgrProxy> GetBundleMgrProxy()
33 {
34 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35 if (!systemAbilityManager) {
36 return nullptr;
37 }
38
39 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
40 if (!remoteObject) {
41 return nullptr;
42 }
43
44 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
45 if (bundleMgrProxy == nullptr) {
46 return nullptr;
47 }
48 return bundleMgrProxy;
49 }
50 constexpr const char* POLICY_DATABASE_BACKUP_FILE_TEST =
51 "/data/service/el1/public/netmanager/net_uid_access_policy_backup_test.txt";
52 constexpr int32_t MAIN_USER_ID = 100;
53 }
54 class NetPolicyDBCloneTest : public testing::Test {
55 public:
56 static void SetUpTestCase();
57 static void TearDownTestCase();
58 void SetUp();
59 void TearDown();
60 };
61
SetUpTestCase()62 void NetPolicyDBCloneTest::SetUpTestCase() {}
63
TearDownTestCase()64 void NetPolicyDBCloneTest::TearDownTestCase() {}
65
SetUp()66 void NetPolicyDBCloneTest::SetUp() {}
67
TearDown()68 void NetPolicyDBCloneTest::TearDown() {}
69
70 HWTEST_F(NetPolicyDBCloneTest, OnBackupTest001, TestSize.Level1)
71 {
72 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
73 if (bundleMgrProxy == nullptr) {
74 return;
75 }
76 NetAccessPolicyRDB netAccessPolicyRdb;
77 int32_t uid1 = bundleMgrProxy->GetUidByBundleName("com.ss.hm.artical.news", MAIN_USER_ID);
78 NetAccessPolicyData policyData1;
79 policyData1.wifiPolicy = 1;
80 policyData1.cellularPolicy = 0;
81 policyData1.setFromConfigFlag = 1;
82 policyData1.uid = uid1;
83 netAccessPolicyRdb.InsertData(policyData1);
84 int32_t uid2 = bundleMgrProxy->GetUidByBundleName("com.youku.next", MAIN_USER_ID);
85 NetAccessPolicyData policyData2;
86 policyData2.wifiPolicy = 0;
87 policyData2.cellularPolicy = 1;
88 policyData2.setFromConfigFlag = 1;
89 policyData2.uid = uid2;
90 netAccessPolicyRdb.InsertData(policyData2);
91 int32_t uid3 = bundleMgrProxy->GetUidByBundleName("com.xingin.xhs_hos", MAIN_USER_ID);
92 NetAccessPolicyData policyData3;
93 policyData3.wifiPolicy = 0;
94 policyData3.cellularPolicy = 1;
95 policyData3.setFromConfigFlag = 1;
96 policyData3.uid = uid3;
97 netAccessPolicyRdb.InsertData(policyData3);
98 int32_t uid4 = bundleMgrProxy->GetUidByBundleName("com.ctrip.harmonynext", MAIN_USER_ID);
99 NetAccessPolicyData policyData4;
100 policyData4.wifiPolicy = 1;
101 policyData4.cellularPolicy = 1;
102 policyData4.setFromConfigFlag = 1;
103 policyData4.uid = uid4;
104 netAccessPolicyRdb.InsertData(policyData4);
105 NetAccessPolicyData policyData5;
106 policyData5.wifiPolicy = 1;
107 policyData5.cellularPolicy = 1;
108 policyData5.setFromConfigFlag = 1;
109 policyData5.uid = 156161616;
110 netAccessPolicyRdb.InsertData(policyData5);
111
112 NetPolicyDBClone netpolicyClone;
113 UniqueFd fd1(-1);
114 netpolicyClone.OnBackup(fd1, "");
115 EXPECT_NE(fd1, 1);
116 close(fd1.Release());
117 }
118
119 HWTEST_F(NetPolicyDBCloneTest, OnRestoreTest001, TestSize.Level1)
120 {
121 std::string content;
122 std::ostringstream ss;
123 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
124 if (bundleMgrProxy == nullptr) {
125 NETMGR_LOG_E("Failed to get bundle manager proxy.");
126 return;
127 }
128 ss << "com.xingin.xhs_hos" << " " << "0" << " " << "1" << std::endl;
129 ss << "com.sina.weibo.stage" << " " << "1" << " " << "1" << std::endl;
130 ss << "com.xxx.test.test1" << " " << "1" << " " << "0" << std::endl;
131 ss << "com.xxx.test.test2" << " " << "abc" << " " << "0" << std::endl;
132 content = ss.str();
133 CommonUtils::WriteFile(POLICY_DATABASE_BACKUP_FILE_TEST, content);
134
135 UniqueFd fd = UniqueFd(open(POLICY_DATABASE_BACKUP_FILE_TEST, O_RDONLY));
136 lseek(fd.Get(), 0, SEEK_SET);
137 auto netpolicyClonePtr = std::make_shared<NetPolicyDBClone>();
138 if (netpolicyClonePtr == nullptr) {
139 return;
140 }
141 netpolicyClonePtr->OnRestore(fd, "");
142 close(fd.Release());
143 EXPECT_NE(netpolicyClonePtr, nullptr);
144 }
145
146 HWTEST_F(NetPolicyDBCloneTest, FdCloneTest001, TestSize.Level1)
147 {
148 auto netpolicyClonePtr = std::make_shared<NetPolicyDBClone>();
149 if (netpolicyClonePtr == nullptr) {
150 return;
151 }
152 UniqueFd fdErr1;
153 bool ret = netpolicyClonePtr->FdClone(fdErr1);
154 EXPECT_EQ(ret, false);
155 UniqueFd fdErr4(-1);
156 ret = netpolicyClonePtr->FdClone(fdErr4);
157 EXPECT_EQ(ret, false);
158 UniqueFd fdErr5(0);
159 ret = netpolicyClonePtr->FdClone(fdErr5);
160 EXPECT_EQ(ret, false);
161 UniqueFd fdErr2(111);
162 ret = netpolicyClonePtr->FdClone(fdErr2);
163 EXPECT_EQ(ret, false);
164 UniqueFd fd = UniqueFd(open(POLICY_DATABASE_BACKUP_FILE_TEST, O_RDONLY));
165 ret = netpolicyClonePtr->FdClone(fd);
166 EXPECT_EQ(ret, true);
167 }
168 }
169 }
170