• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "ability_manager_errors.h"
19 #include "mock_my_flag.h"
20 #include "udmf_client.h"
21 #define private public
22 #include "upms_udmf_utils.h"
23 #undef private
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace AAFwk {
30 class UriPermissionImplUdmfUtilsTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 };
37 
SetUpTestCase(void)38 void UriPermissionImplUdmfUtilsTest::SetUpTestCase(void)
39 {}
40 
TearDownTestCase(void)41 void UriPermissionImplUdmfUtilsTest::TearDownTestCase(void)
42 {}
43 
SetUp()44 void UriPermissionImplUdmfUtilsTest::SetUp()
45 {
46     UDMF::UdmfClient::Init();
47     MyFlag::Init();
48 }
49 
TearDown()50 void UriPermissionImplUdmfUtilsTest::TearDown()
51 {}
52 
53 /**
54  * @tc.number: AddPrivilege_0100
55  * @tc.desc: Test AddPrivilege works
56  * @tc.type: FUNC
57  */
58 HWTEST_F(UriPermissionImplUdmfUtilsTest, AddPrivilege_0100, TestSize.Level1)
59 {
60     std::string key = "udmfKey";
61     uint32_t targetTokenId = 100001;
62     std::string readPermission = "";
63     auto ret = UDMFUtils::AddPrivilege(key, targetTokenId, readPermission);
64     EXPECT_EQ(ret, ERR_OK);
65 }
66 
67 /**
68  * @tc.number: AddPrivilege_0200
69  * @tc.desc: Test AddPrivilege works
70  * @tc.type: FUNC
71  */
72 HWTEST_F(UriPermissionImplUdmfUtilsTest, AddPrivilege_0200, TestSize.Level1)
73 {
74     std::string key = "udmfKey";
75     uint32_t targetTokenId = 100001;
76     std::string readPermission = "";
77     UDMF::UdmfClient::addPrivilegeRet_ = INNER_ERR;
78     auto ret = UDMFUtils::AddPrivilege(key, targetTokenId, readPermission);
79     EXPECT_EQ(ret, INNER_ERR);
80 }
81 
82 /**
83  * @tc.number: GetBatchData_0100
84  * @tc.desc: Test GetBatchData works
85  * @tc.type: FUNC
86  */
87 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0100, TestSize.Level1)
88 {
89     std::string key = "";
90     std::vector<std::string> uris;
91     UDMF::UdmfClient::getBatchDataRet_ = INNER_ERR;
92     auto ret = UDMFUtils::GetBatchData(key, uris);
93     EXPECT_EQ(ret, INNER_ERR);
94 }
95 
96 /**
97  * @tc.number: GetBatchData_0200
98  * @tc.desc: Test GetBatchData works
99  * @tc.type: FUNC
100  */
101 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0200, TestSize.Level1)
102 {
103     std::string key = "";
104     std::vector<std::string> uris;
105     UDMF::UdmfClient::getBatchDataRet_ = 0;
106     UDMF::UdmfClient::unifiedData_= {};
107     auto ret = UDMFUtils::GetBatchData(key, uris);
108     EXPECT_EQ(ret, ERR_UPMS_GET_FILE_URIS_BY_KEY_FAILED);
109 }
110 
111 /**
112  * @tc.number: GetBatchData_0300
113  * @tc.desc: Test GetBatchData works, unified record GetEntry is nullptr.
114  * @tc.type: FUNC
115  */
116 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0300, TestSize.Level1)
117 {
118     // null entry
119     std::string uri = "null";
120     std::string key = "udmfKey";
121     // create dataset
122     std::vector<UDMF::UnifiedData> unifiedDataset;
123     std::vector<std::shared_ptr<UDMF::UnifiedRecord>> unifiedRecords;
124     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri));
125     unifiedDataset.emplace_back(UDMF::UnifiedData(unifiedRecords));
126     UDMF::UdmfClient::unifiedData_ = unifiedDataset;
127     // GetEntry is nullptr
128     std::vector<std::string> uris;
129     auto ret = UDMFUtils::GetBatchData(key, uris);
130     EXPECT_EQ(ret, ERR_UPMS_GET_ORI_URI_FAILED);
131 }
132 
133 /**
134  * @tc.number: GetBatchData_0400
135  * @tc.desc: Test GetBatchData works, unified record GetValue is empty.
136  * @tc.type: FUNC
137  */
138 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0400, TestSize.Level1)
139 {
140     std::string uri = "";
141     std::string key = "udmfKey";
142     // create dataset
143     std::vector<UDMF::UnifiedData> unifiedDataset;
144     std::vector<std::shared_ptr<UDMF::UnifiedRecord>> unifiedRecords;
145     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri));
146     unifiedDataset.emplace_back(UDMF::UnifiedData(unifiedRecords));
147     UDMF::UdmfClient::unifiedData_ = unifiedDataset;
148     // GetValue oriUri is empty
149     std::vector<std::string> uris;
150     auto ret = UDMFUtils::GetBatchData(key, uris);
151     EXPECT_EQ(ret, ERR_UPMS_GET_ORI_URI_FAILED);
152 }
153 
154 /**
155  * @tc.number: GetBatchData_0500
156  * @tc.desc: Test GetBatchData works, unified record oriUri is not file uri.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0500, TestSize.Level1)
160 {
161     std::string uri = "http://temp.txt";
162     std::string key = "udmfKey";
163     // create dataset
164     std::vector<UDMF::UnifiedData> unifiedDataset;
165     std::vector<std::shared_ptr<UDMF::UnifiedRecord>> unifiedRecords;
166     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri));
167     unifiedDataset.emplace_back(UDMF::UnifiedData(unifiedRecords));
168     UDMF::UdmfClient::unifiedData_ = unifiedDataset;
169     // GetValue oriUri is not file uri
170     std::vector<std::string> uris;
171     auto ret = UDMFUtils::GetBatchData(key, uris);
172     EXPECT_EQ(ret, ERR_UPMS_NOT_FILE_URI);
173 }
174 
175 /**
176  * @tc.number: GetBatchData_0600
177  * @tc.desc: Test GetBatchData works, unified record GetValue is file.
178  * @tc.type: FUNC
179  */
180 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0600, TestSize.Level1)
181 {
182     std::string uri = "file://com.example.test/temp.txt";
183     std::string key = "udmfKey";
184     // create dataset
185     std::vector<UDMF::UnifiedData> unifiedDataset;
186     std::vector<std::shared_ptr<UDMF::UnifiedRecord>> unifiedRecords;
187     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri));
188     unifiedDataset.emplace_back(UDMF::UnifiedData(unifiedRecords));
189     UDMF::UdmfClient::unifiedData_ = unifiedDataset;
190     // GetValue oriUri is file uri
191     std::vector<std::string> uris;
192     auto ret = UDMFUtils::GetBatchData(key, uris);
193     EXPECT_EQ(ret, ERR_OK);
194     EXPECT_EQ(uris.size(), 1);
195 }
196 
197 /**
198  * @tc.number: GetBatchData_0700
199  * @tc.desc: Test GetBatchData works, some uri is invalid.
200  * @tc.type: FUNC
201  */
202 HWTEST_F(UriPermissionImplUdmfUtilsTest, GetBatchData_0700, TestSize.Level1)
203 {
204     std::string uri1 = "file://com.example.test/temp.txt";
205     std::string uri2 = "http://com.example.test/temp.txt";
206     std::string key = "udmfKey";
207     // create dataset
208     std::vector<UDMF::UnifiedData> unifiedDataset;
209     std::vector<std::shared_ptr<UDMF::UnifiedRecord>> unifiedRecords;
210     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri1));
211     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri2));
212     unifiedDataset.emplace_back(UDMF::UnifiedData(unifiedRecords));
213     UDMF::UdmfClient::unifiedData_ = unifiedDataset;
214     // one uri is invalid
215     std::vector<std::string> uris;
216     auto ret = UDMFUtils::GetBatchData(key, uris);
217     EXPECT_EQ(ret, ERR_UPMS_NOT_FILE_URI);
218 }
219 
220 /**
221  * @tc.number: ProcessUdmfKey_0100
222  * @tc.desc: Test ProcessUdmfKey works, AddPrivilege failed.
223  * @tc.type: FUNC
224  */
225 HWTEST_F(UriPermissionImplUdmfUtilsTest, ProcessUdmfKey_0100, TestSize.Level1)
226 {
227     std::string key = "";
228     uint32_t callerTokenId = 100001;
229     uint32_t targetTokenId = 100002;
230     std::vector<std::string> uris;
231     UDMF::UdmfClient::addPrivilegeRet_ = INNER_ERR;
232     auto ret = UDMFUtils::ProcessUdmfKey(key, callerTokenId, targetTokenId, uris);
233     EXPECT_EQ(ret, ERR_UPMS_ADD_PRIVILEGED_FAILED);
234 }
235 
236 /**
237  * @tc.number: ProcessUdmfKey_0200
238  * @tc.desc: Test ProcessUdmfKey works, AddPrivilege first, success second failed.
239  * @tc.type: FUNC
240  */
241 HWTEST_F(UriPermissionImplUdmfUtilsTest, ProcessUdmfKey_0200, TestSize.Level1)
242 {
243     std::string key = "";
244     uint32_t callerTokenId = 100001;
245     // AddPrivileged success
246     uint32_t targetTokenId = UDMF::UdmfClient::privilegeTokenId_;
247     std::vector<std::string> uris;
248     UDMF::UdmfClient::addPrivilegeRet_ = INNER_ERR;
249     auto ret = UDMFUtils::ProcessUdmfKey(key, callerTokenId, targetTokenId, uris);
250     EXPECT_EQ(ret, ERR_UPMS_ADD_PRIVILEGED_FAILED);
251 }
252 
253 /**
254  * @tc.number: ProcessUdmfKey_0300
255  * @tc.desc: Test ProcessUdmfKey works, GetBatchData failed.
256  * @tc.type: FUNC
257  */
258 HWTEST_F(UriPermissionImplUdmfUtilsTest, ProcessUdmfKey_0300, TestSize.Level1)
259 {
260     std::string key = "";
261     uint32_t callerTokenId = 100001;
262     uint32_t targetTokenId = 100002;
263     std::vector<std::string> uris;
264     UDMF::UdmfClient::addPrivilegeRet_ = ERR_OK;
265     UDMF::UdmfClient::getBatchDataRet_ = INNER_ERR;
266     auto ret = UDMFUtils::ProcessUdmfKey(key, callerTokenId, targetTokenId, uris);
267     EXPECT_EQ(ret, ERR_UPMS_GET_FILE_URIS_BY_KEY_FAILED);
268 }
269 
270 /**
271  * @tc.number: ProcessUdmfKey_0400
272  * @tc.desc: Test ProcessUdmfKey works, ProcessUdmfKey success.
273  * @tc.type: FUNC
274  */
275 HWTEST_F(UriPermissionImplUdmfUtilsTest, ProcessUdmfKey_0400, TestSize.Level1)
276 {
277     std::string uri = "file://com.example.test/temp.txt";
278     std::string key = "udmfKey";
279     uint32_t callerTokenId = 100001;
280     uint32_t targetTokenId = 100002;
281     std::vector<std::string> uris;
282     // create dataset
283     std::vector<UDMF::UnifiedData> unifiedDataset;
284     std::vector<std::shared_ptr<UDMF::UnifiedRecord>> unifiedRecords;
285     unifiedRecords.emplace_back(std::make_shared<UDMF::UnifiedRecord>(uri));
286     unifiedDataset.emplace_back(UDMF::UnifiedData(unifiedRecords));
287     UDMF::UdmfClient::unifiedData_ = unifiedDataset;
288     // processUdmfKey
289     auto ret = UDMFUtils::ProcessUdmfKey(key, callerTokenId, targetTokenId, uris);
290     EXPECT_EQ(ret, ERR_OK);
291 }
292 
293 /**
294  * @tc.number: ProcessUdmfKey_0500
295  * @tc.desc: Test ProcessUdmfKey works, ProcessUdmfKey failed, key is not create by caller.
296  * @tc.type: FUNC
297  */
298 HWTEST_F(UriPermissionImplUdmfUtilsTest, ProcessUdmfKey_0500, TestSize.Level1)
299 {
300     std::string uri = "file://com.example.test/temp.txt";
301     std::string key = "udmfKey";
302     uint32_t callerTokenId = 100001;
303     uint32_t targetTokenId = 100002;
304     std::vector<std::string> uris;
305     // callerAuthority
306     MyFlag::upmsUtilsGetAlterBundleNameByTokenIdRet_ = 0;
307     MyFlag::upmsUtilsAlterBundleName_ = "com.example.test";
308     // keyAuthority
309     UDMF::UdmfClient::keyAuthority = "com.example.test1";
310     // processUdmfKey
311     auto ret = UDMFUtils::ProcessUdmfKey(key, callerTokenId, targetTokenId, uris);
312     EXPECT_EQ(ret, ERR_UPMS_KEY_IS_NOT_CREATE_BY_CALLER);
313 }
314 
315 /**
316  * @tc.number: UriPermissionImplUdmfUtilsTest_IsUdKeyCreateByCaller_001
317  * @tc.desc: callerAuthority equal to keyAuthority.
318  * @tc.type: FUNC
319  */
320 HWTEST_F(UriPermissionImplUdmfUtilsTest, IsUdKeyCreateByCaller_001, TestSize.Level1)
321 {
322     std::string key = "udmf://Picker/com.example.test/aaa";
323     uint32_t callerTokenId = 100001;
324     // callerAuthority
325     MyFlag::upmsUtilsGetAlterBundleNameByTokenIdRet_ = 0;
326     MyFlag::upmsUtilsAlterBundleName_ = "com.example.test";
327     // keyAuthority
328     UDMF::UdmfClient::keyAuthority = "com.example.test";
329     bool ret = UDMFUtils::IsUdKeyCreateByCaller(callerTokenId, key);
330     EXPECT_EQ(ret, true);
331 }
332 
333 /**
334  * @tc.number: UriPermissionImplUdmfUtilsTest_IsUdKeyCreateByCaller_002
335  * @tc.desc: callerAuthority do not equal to keyAuthority.
336  * @tc.type: FUNC
337  */
338 HWTEST_F(UriPermissionImplUdmfUtilsTest, IsUdKeyCreateByCaller_002, TestSize.Level1)
339 {
340     std::string key = "udmf://Picker/com.example.test/aaa";
341     uint32_t callerTokenId = 100001;
342     // callerAuthority
343     MyFlag::upmsUtilsGetAlterBundleNameByTokenIdRet_ = 0;
344     MyFlag::upmsUtilsAlterBundleName_ = "com.example.test";
345     // keyAuthority
346     UDMF::UdmfClient::keyAuthority = "com.example.test1";
347     bool ret = UDMFUtils::IsUdKeyCreateByCaller(callerTokenId, key);
348     EXPECT_EQ(ret, false);
349 }
350 }  // namespace AAFwk
351 }  // namespace OHOS