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 #define LOG_TAG "UdmfClientSystemHapTest"
16 #include <gtest/gtest.h>
17
18 #include <unistd.h>
19 #include <thread>
20 #include <chrono>
21
22 #include "token_setproc.h"
23 #include "accesstoken_kit.h"
24 #include "directory_ex.h"
25 #include "nativetoken_kit.h"
26
27 #include "logger.h"
28 #include "udmf_client.h"
29 #include "application_defined_record.h"
30 #include "audio.h"
31 #include "file.h"
32 #include "folder.h"
33 #include "html.h"
34 #include "image.h"
35 #include "link.h"
36 #include "plain_text.h"
37 #include "system_defined_appitem.h"
38 #include "system_defined_form.h"
39 #include "system_defined_pixelmap.h"
40 #include "system_defined_record.h"
41 #include "text.h"
42 #include "unified_data_helper.h"
43 #include "video.h"
44
45 using namespace testing::ext;
46 using namespace OHOS::Security::AccessToken;
47 using namespace OHOS::UDMF;
48 using namespace OHOS;
49 namespace OHOS::Test {
50 static constexpr int USER_ID = 100;
51 static constexpr int INST_INDEX = 0;
52 class UdmfClientSystemHapTest : public testing::Test {
53 public:
54 static void SetUpTestCase();
55 static void TearDownTestCase();
56 void SetUp() override;
57 void TearDown() override;
58
59 void SetNativeToken(const std::string &processName);
60 static void AllocHapToken1();
61 static void AllocHapToken2();
62 void SetHapToken1();
63 void SetHapToken2();
64 void AddPrivilege(QueryOption &option);
65 };
66
SetUpTestCase()67 void UdmfClientSystemHapTest::SetUpTestCase()
68 {
69 AllocHapToken1();
70 AllocHapToken2();
71 }
72
TearDownTestCase()73 void UdmfClientSystemHapTest::TearDownTestCase()
74 {
75 auto tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo1", INST_INDEX);
76 AccessTokenKit::DeleteToken(tokenId);
77 tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo2", INST_INDEX);
78 AccessTokenKit::DeleteToken(tokenId);
79 }
80
SetUp()81 void UdmfClientSystemHapTest::SetUp()
82 {
83 SetHapToken1();
84 }
85
TearDown()86 void UdmfClientSystemHapTest::TearDown()
87 {
88 }
89
SetNativeToken(const std::string & processName)90 void UdmfClientSystemHapTest::SetNativeToken(const std::string &processName)
91 {
92 auto tokenId = AccessTokenKit::GetNativeTokenId(processName);
93 SetSelfTokenID(tokenId);
94 }
95
AllocHapToken1()96 void UdmfClientSystemHapTest::AllocHapToken1()
97 {
98 HapInfoParams info = {
99 .userID = USER_ID,
100 .bundleName = "ohos.test.demo1",
101 .instIndex = INST_INDEX,
102 .appIDDesc = "ohos.test.demo1",
103 .isSystemApp = true,
104 };
105
106 HapPolicyParams policy = {
107 .apl = APL_SYSTEM_BASIC,
108 .domain = "test.domain",
109 .permList = {
110 {
111 .permissionName = "ohos.permission.test",
112 .bundleName = "ohos.test.demo1",
113 .grantMode = 1,
114 .availableLevel = APL_SYSTEM_BASIC,
115 .label = "label",
116 .labelId = 1,
117 .description = "test1",
118 .descriptionId = 1
119 }
120 },
121 .permStateList = {
122 {
123 .permissionName = "ohos.permission.test",
124 .isGeneral = true,
125 .resDeviceID = { "local" },
126 .grantStatus = { PermissionState::PERMISSION_GRANTED },
127 .grantFlags = { 1 }
128 }
129 }
130 };
131 auto tokenID = AccessTokenKit::AllocHapToken(info, policy);
132 SetSelfTokenID(tokenID.tokenIDEx);
133 }
134
AllocHapToken2()135 void UdmfClientSystemHapTest::AllocHapToken2()
136 {
137 HapInfoParams info = {
138 .userID = USER_ID,
139 .bundleName = "ohos.test.demo2",
140 .instIndex = INST_INDEX,
141 .appIDDesc = "ohos.test.demo2",
142 .isSystemApp = true,
143 };
144
145 HapPolicyParams policy = {
146 .apl = APL_SYSTEM_BASIC,
147 .domain = "test.domain",
148 .permList = {
149 {
150 .permissionName = "ohos.permission.test",
151 .bundleName = "ohos.test.demo2",
152 .grantMode = 1,
153 .availableLevel = APL_SYSTEM_BASIC,
154 .label = "label",
155 .labelId = 1,
156 .description = "test2",
157 .descriptionId = 1
158 }
159 },
160 .permStateList = {
161 {
162 .permissionName = "ohos.permission.test",
163 .isGeneral = true,
164 .resDeviceID = { "local" },
165 .grantStatus = { PermissionState::PERMISSION_GRANTED },
166 .grantFlags = { 1 }
167 }
168 }
169 };
170 auto tokenID = AccessTokenKit::AllocHapToken(info, policy);
171 SetSelfTokenID(tokenID.tokenIDEx);
172 }
173
SetHapToken1()174 void UdmfClientSystemHapTest::SetHapToken1()
175 {
176 auto tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo1", INST_INDEX);
177 SetSelfTokenID(tokenId);
178 }
179
SetHapToken2()180 void UdmfClientSystemHapTest::SetHapToken2()
181 {
182 auto tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo2", INST_INDEX);
183 SetSelfTokenID(tokenId);
184 }
185
AddPrivilege(QueryOption & option)186 void UdmfClientSystemHapTest::AddPrivilege(QueryOption &option)
187 {
188 Privilege privilege;
189 privilege.tokenId = AccessTokenKit::GetHapTokenID(USER_ID, "ohos.test.demo2", INST_INDEX);
190 privilege.readPermission = "readPermission";
191 privilege.writePermission = "writePermission";
192 SetNativeToken("msdp_sa");
193 auto status = UdmfClient::GetInstance().AddPrivilege(option, privilege);
194 ASSERT_EQ(status, E_OK);
195 }
196
197 /**
198 * @tc.name: SetAppShareOption001
199 * @tc.desc: SetAppShareOption
200 * @tc.type: FUNC
201 */
202 HWTEST_F(UdmfClientSystemHapTest, SetAppShareOption001, TestSize.Level1)
203 {
204 AllocHapToken1();
205 auto status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
206 LOG_INFO(UDMF_TEST, "SetAppShareOption001 begin.");
207 status = UdmfClient::GetInstance().SetAppShareOption("drag", ShareOptions::IN_APP);
208 EXPECT_EQ(status, E_OK);
209 status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
210 EXPECT_EQ(status, E_OK);
211 LOG_INFO(UDMF_TEST, "SetAppShareOption001 end.");
212 }
213
214 /**
215 * @tc.name: SetAppShareOption002
216 * @tc.desc: SetAppShareOption
217 * @tc.type: FUNC
218 */
219 HWTEST_F(UdmfClientSystemHapTest, SetAppShareOption002, TestSize.Level1)
220 {
221 LOG_INFO(UDMF_TEST, "SetAppShareOption002 begin.");
222 AllocHapToken1();
223 auto status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
224 status = UdmfClient::GetInstance().SetAppShareOption("drag", ShareOptions::IN_APP);
225 EXPECT_EQ(status, E_OK);
226 ShareOptions appShareOptions;
227 status = UdmfClient::GetInstance().GetAppShareOption("drag", appShareOptions);
228 EXPECT_EQ(status, E_OK);
229 EXPECT_EQ(appShareOptions, ShareOptions::IN_APP);
230 status = UdmfClient::GetInstance().SetAppShareOption("drag", ShareOptions::IN_APP);
231 EXPECT_EQ(status, E_SETTINGS_EXISTED);
232 status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
233 EXPECT_EQ(status, E_OK);
234 LOG_INFO(UDMF_TEST, "SetAppShareOption002 end.");
235 }
236
237 /**
238 * @tc.name: RemoveAppShareOption001
239 * @tc.desc: RemoveAppShareOption
240 * @tc.type: FUNC
241 */
242 HWTEST_F(UdmfClientSystemHapTest, RemoveAppShareOption001, TestSize.Level1)
243 {
244 AllocHapToken1();
245 LOG_INFO(UDMF_TEST, "RemoveAppShareOption001 begin.");
246 auto status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
247 EXPECT_EQ(status, E_OK);
248 LOG_INFO(UDMF_TEST, "RemoveAppShareOption001 end.");
249 }
250
251 /**
252 * @tc.name: DeleteData002
253 * @tc.desc: Delete data with valid params
254 * @tc.type: FUNC
255 */
256 HWTEST_F(UdmfClientSystemHapTest, systemAppSetData001, TestSize.Level1)
257 {
258 LOG_INFO(UDMF_TEST, "systemAppSetData001 begin.");
259 AllocHapToken1();
260 auto status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
261 status = UdmfClient::GetInstance().SetAppShareOption("drag", ShareOptions::IN_APP);
262 EXPECT_EQ(status, E_OK);
263 LOG_INFO(UDMF_TEST, "systemAppSetData001 SetAppShareOption success.");
264
265 CustomOption customOption = { .intention = UD_INTENTION_DRAG };
266 UnifiedData data;
267 PlainText plainText;
268 plainText.SetContent("systemApptestcontent1");
269 std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(plainText);
270 data.AddRecord(record);
271 std::string key;
272 status = UdmfClient::GetInstance().SetData(customOption, data, key);
273 ASSERT_EQ(status, E_OK);
274
275 QueryOption queryOption = { .key = key };
276 LOG_INFO(UDMF_TEST, "systemAppSetData001 SetData success, key:%{public}s.", key.c_str());
277
278 UnifiedData outputData;
279 status = UdmfClient::GetInstance().GetData(queryOption, outputData);
280 ASSERT_EQ(status, E_OK);
281 std::shared_ptr<UnifiedRecord> record2 = outputData.GetRecordAt(0);
282 ASSERT_NE(record2, nullptr);
283 auto type = record2->GetType();
284 ASSERT_EQ(type, UDType::PLAIN_TEXT);
285 auto text2 = static_cast<Text *>(record2.get());
286 ASSERT_NE(text2, nullptr);
287 auto plainText2 = static_cast<PlainText *>(record2.get());
288 ASSERT_EQ(plainText2->GetContent(), "systemApptestcontent1");
289
290 LOG_INFO(UDMF_TEST, "systemAppSetData001 end.");
291 }
292
293 /**
294 * @tc.name: systemAppSetData002
295 * @tc.desc: systemAppSetData CROSS_APP
296 * @tc.type: FUNC
297 */
298 HWTEST_F(UdmfClientSystemHapTest, systemAppSetData002, TestSize.Level1)
299 {
300 LOG_INFO(UDMF_TEST, "systemAppSetData002 begin.");
301 AllocHapToken1();
302 auto status = UdmfClient::GetInstance().RemoveAppShareOption("drag");
303 status = UdmfClient::GetInstance().SetAppShareOption("drag", ShareOptions::IN_APP);
304 EXPECT_EQ(status, E_OK);
305 LOG_INFO(UDMF_TEST, "systemAppSetData002 SetAppShareOption success.");
306 CustomOption customOption = { .intention = UD_INTENTION_DRAG };
307 UnifiedData data;
308 PlainText plainText;
309 plainText.SetContent("systemApptestcontent1");
310 std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(plainText);
311 data.AddRecord(record);
312 std::string key;
313 status = UdmfClient::GetInstance().SetData(customOption, data, key);
314 ASSERT_EQ(status, E_OK);
315
316 SetHapToken2();
317
318 QueryOption queryOption = { .key = key };
319 UnifiedData outputData;
320 status = UdmfClient::GetInstance().GetData(queryOption, outputData);
321 ASSERT_EQ(status, E_OK);
322
323 LOG_INFO(UDMF_TEST, "systemAppSetData002 end.");
324 }
325 } // OHOS::Test