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 #include <gtest/gtest.h>
16 #include <unistd.h>
17
18 #include "accesstoken_kit.h"
19 #include "datashare_helper.h"
20 #include "datashare_log.h"
21 #include "hap_token_info.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "token_setproc.h"
25
26 namespace OHOS {
27 namespace DataShare {
28 using namespace testing::ext;
29 using namespace OHOS::Security::AccessToken;
30 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
31 std::string DATA_SHARE_URI = "datashare:///com.acts.datasharetest";
32 std::string SLIENT_ACCESS_URI = "datashare:///com.acts.datasharetest/entry/DB00/TBL00?Proxy=true";
33 std::string DATA_SHARE_PROXY_URI = "datashare:///com.acts.ohos.data.datasharetest/test";
34 constexpr int SUBSCRIBER_ID = 1000;
35 std::string TBL_STU_NAME = "name";
36 std::string TBL_STU_AGE = "age";
37 int32_t ERROR = -1;
38 std::shared_ptr<DataShare::DataShareHelper> g_slientAccessHelper;
39
40 class SlientSwitchTest : public testing::Test {
41 public:
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp();
45 void TearDown();
46 };
47
CreateDataShareHelper(int32_t systemAbilityId,const std::string & silentProxyUri,const std::string & providerUri="")48 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t systemAbilityId,
49 const std::string &silentProxyUri, const std::string &providerUri = "")
50 {
51 LOG_INFO("CreateDataShareHelper start");
52 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53 if (saManager == nullptr) {
54 LOG_ERROR("GetSystemAbilityManager get samgr failed.");
55 return nullptr;
56 }
57 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
58 if (remoteObj == nullptr) {
59 LOG_ERROR("GetSystemAbility service failed.");
60 return nullptr;
61 }
62 return DataShare::DataShareHelper::Creator(remoteObj, silentProxyUri, providerUri);
63 }
64
SetUpTestCase(void)65 void SlientSwitchTest::SetUpTestCase(void)
66 {
67 LOG_INFO("SetUpTestCase invoked");
68 auto dataShareHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, DATA_SHARE_URI);
69 ASSERT_TRUE(dataShareHelper != nullptr);
70 int sleepTime = 3;
71 sleep(sleepTime);
72
73 HapInfoParams info = {
74 .userID = 100,
75 .bundleName = "com.acts.datasharetest",
76 .instIndex = 0,
77 .appIDDesc = "com.acts.datasharetest"
78 };
79 HapPolicyParams policy = {
80 .apl = APL_NORMAL,
81 .domain = "test.domain",
82 .permList = {
83 {
84 .permissionName = "ohos.permission.test",
85 .bundleName = "com.acts.datasharetest",
86 .grantMode = 1,
87 .availableLevel = APL_NORMAL,
88 .label = "label",
89 .labelId = 1,
90 .description = "com.acts.datasharetest",
91 .descriptionId = 1
92 }
93 },
94 .permStateList = {
95 {
96 .permissionName = "ohos.permission.test",
97 .isGeneral = true,
98 .resDeviceID = { "local" },
99 .grantStatus = { PermissionState::PERMISSION_GRANTED },
100 .grantFlags = { 1 }
101 }
102 }
103 };
104 AccessTokenKit::AllocHapToken(info, policy);
105 auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID(
106 info.userID, info.bundleName, info.instIndex);
107 SetSelfTokenID(testTokenId);
108
109 g_slientAccessHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
110 ASSERT_TRUE(g_slientAccessHelper != nullptr);
111 LOG_INFO("SetUpTestCase end");
112 }
113
TearDownTestCase(void)114 void SlientSwitchTest::TearDownTestCase(void)
115 {
116 auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.acts.datasharetest", 0);
117 AccessTokenKit::DeleteToken(tokenId);
118 g_slientAccessHelper = nullptr;
119 }
120
SetUp(void)121 void SlientSwitchTest::SetUp(void) {}
TearDown(void)122 void SlientSwitchTest::TearDown(void) {}
123
124 /**
125 * @tc.name: SlientSwitch_SetSilentSwitch_Test_001
126 * @tc.desc: Test enabling silent switch with valid access URI
127 * @tc.type: FUNC
128 * @tc.require: NA
129 * @tc.precon: None
130 * @tc.step:
131 * 1. Create Uri object with SLIENT_ACCESS_URI
132 * 2. Call DataShareHelper::SetSilentSwitch() with URI and true (enable)
133 * 3. Verify the return value
134 * @tc.expect: The operation succeeds with return value E_OK
135 */
136 HWTEST_F(SlientSwitchTest, SlientSwitch_SetSilentSwitch_Test_001, TestSize.Level0)
137 {
138 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_001::Start");
139 Uri uri(SLIENT_ACCESS_URI);
140 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
141 EXPECT_EQ(retVal, E_OK);
142 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_001::End");
143 }
144
145 /**
146 * @tc.name: SlientSwitch_SetSilentSwitch_Test_002
147 * @tc.desc: Test disabling silent switch with valid access URI
148 * @tc.type: FUNC
149 * @tc.require: NA
150 * @tc.precon: None
151 * @tc.step:
152 * 1. Create Uri object with SLIENT_ACCESS_URI
153 * 2. Call DataShareHelper::SetSilentSwitch() with URI and false (disable)
154 * 3. Verify the return value
155 * @tc.expect: The operation succeeds with return value E_OK
156 */
157 HWTEST_F(SlientSwitchTest, SlientSwitch_SetSilentSwitch_Test_002, TestSize.Level0)
158 {
159 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_002::Start");
160 Uri uri(SLIENT_ACCESS_URI);
161 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
162 EXPECT_EQ(retVal, E_OK);
163 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_002::End");
164 }
165
166 /**
167 * @tc.name: SlientSwitch_SetSilentSwitch_Test_003
168 * @tc.desc: Test disabling silent switch with empty URI
169 * @tc.type: FUNC
170 * @tc.require: NA
171 * @tc.precon: None
172 * @tc.step:
173 * 1. Create empty Uri object
174 * 2. Call DataShareHelper::SetSilentSwitch() with empty URI and false (disable)
175 * 3. Verify the return value
176 * @tc.expect: The operation succeeds with return value E_OK
177 */
178 HWTEST_F(SlientSwitchTest, SlientSwitch_SetSilentSwitch_Test_003, TestSize.Level0)
179 {
180 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_003::Start");
181 Uri uri("");
182 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
183 EXPECT_EQ(retVal, E_OK);
184 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_003::End");
185 }
186
187 /**
188 * @tc.name: SlientSwitch_SetSilentSwitch_Test_004
189 * @tc.desc: Test enabling silent switch with empty URI
190 * @tc.type: FUNC
191 * @tc.require: NA
192 * @tc.precon: None
193 * @tc.step:
194 * 1. Create empty Uri object
195 * 2. Call DataShareHelper::SetSilentSwitch() with empty URI and true (enable)
196 * 3. Verify the return value
197 * @tc.expect: The operation succeeds with return value E_OK
198 */
199 HWTEST_F(SlientSwitchTest, SlientSwitch_SetSilentSwitch_Test_004, TestSize.Level0)
200 {
201 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_004::Start");
202 Uri uri("");
203 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
204 EXPECT_EQ(retVal, E_OK);
205 LOG_INFO("SlientSwitch_SetSilentSwitch_Test_004::End");
206 }
207
208 /**
209 * @tc.name: SlientSwitch_SwitchDisable_Insert_Test_001
210 * @tc.desc: Test data insertion when silent switch is disabled with valid URI
211 * @tc.type: FUNC
212 * @tc.require: NA
213 * @tc.precon: None
214 * @tc.step:
215 * 1. Create Uri object with SLIENT_ACCESS_URI
216 * 2. Disable silent switch using SetSilentSwitch() with URI
217 * 3. Prepare DataShareValuesBucket with test data (name="lisi", age=25)
218 * 4. Call Insert() method with URI and values bucket
219 * 5. Verify the return value
220 * @tc.expect: Insert operation fails (return value <= 0)
221 */
222 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Insert_Test_001, TestSize.Level0)
223 {
224 LOG_INFO("SlientSwitch_SwitchDisable_Insert_Test_001::Start");
225 Uri uri(SLIENT_ACCESS_URI);
226 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
227 EXPECT_EQ(retVal, E_OK);
228
229 auto helper = g_slientAccessHelper;
230 DataShare::DataShareValuesBucket valuesBucket;
231 std::string value = "lisi";
232 valuesBucket.Put(TBL_STU_NAME, value);
233 int age = 25;
234 valuesBucket.Put(TBL_STU_AGE, age);
235
236 retVal = helper->Insert(uri, valuesBucket);
237 EXPECT_EQ((retVal > 0), false);
238 LOG_INFO("SlientSwitch_SwitchDisable_Insert_Test_001::End");
239 }
240
241 /**
242 * @tc.name: SlientSwitch_SwitchDisable_Insert_Test_002
243 * @tc.desc: Test data insertion when silent switch is disabled with empty URI
244 * @tc.type: FUNC
245 * @tc.require: NA
246 * @tc.precon: None
247 * @tc.step:
248 * 1. Disable silent switch using SetSilentSwitch() with empty URI
249 * 2. Prepare DataShareValuesBucket with test data (name="wangwu", age=25)
250 * 3. Create Uri object with SLIENT_ACCESS_URI
251 * 4. Call Insert() method with URI and values bucket
252 * 5. Verify the return value
253 * @tc.expect: Insert operation fails (return value <= 0)
254 */
255 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Insert_Test_002, TestSize.Level0)
256 {
257 LOG_INFO("SlientSwitch_SwitchDisable_Insert_Test_002::Start");
258 Uri uri("");
259 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
260 EXPECT_EQ(retVal, E_OK);
261
262 auto helper = g_slientAccessHelper;
263 DataShare::DataShareValuesBucket valuesBucket;
264 std::string value = "wangwu";
265 valuesBucket.Put(TBL_STU_NAME, value);
266 int age = 25;
267 valuesBucket.Put(TBL_STU_AGE, age);
268 uri = Uri(SLIENT_ACCESS_URI);
269 retVal = helper->Insert(uri, valuesBucket);
270 EXPECT_EQ((retVal > 0), false);
271 LOG_INFO("SlientSwitch_SwitchDisable_Insert_Test_002::End");
272 }
273
274 /**
275 * @tc.name: SlientSwitch_SwitchEnable_Insert_Test_001
276 * @tc.desc: Test data insertion when silent switch is enabled with valid URI
277 * @tc.type: FUNC
278 * @tc.require: NA
279 * @tc.precon: None
280 * @tc.step:
281 * 1. Create Uri object with SLIENT_ACCESS_URI
282 * 2. Enable silent switch using SetSilentSwitch() with URI
283 * 3. Prepare DataShareValuesBucket with test data (name="lisi", age=25)
284 * 4. Call Insert() method with URI and values bucket
285 * 5. Verify the return value
286 * @tc.expect: Insert operation succeeds (return value > 0)
287 */
288 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Insert_Test_001, TestSize.Level0)
289 {
290 LOG_INFO("SlientSwitch_SwitchEnable_Insert_Test_001::Start");
291 Uri uri(SLIENT_ACCESS_URI);
292 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
293 EXPECT_EQ(retVal, E_OK);
294
295 auto helper = g_slientAccessHelper;
296 DataShare::DataShareValuesBucket valuesBucket;
297 std::string value = "lisi";
298 valuesBucket.Put(TBL_STU_NAME, value);
299 int age = 25;
300 valuesBucket.Put(TBL_STU_AGE, age);
301
302 retVal = helper->Insert(uri, valuesBucket);
303 EXPECT_EQ((retVal > 0), true);
304 LOG_INFO("SlientSwitch_SwitchEnable_Insert_Test_001::End");
305 }
306
307 /**
308 * @tc.name: SlientSwitch_SwitchEnable_Insert_Test_002
309 * @tc.desc: Test data insertion when silent switch is enabled with empty URI
310 * @tc.type: FUNC
311 * @tc.require: NA
312 * @tc.precon: None
313 * @tc.step:
314 * 1. Enable silent switch using SetSilentSwitch() with empty URI
315 * 2. Prepare DataShareValuesBucket with test data (name="wangwu", age=25)
316 * 3. Create Uri object with SLIENT_ACCESS_URI
317 * 4. Call Insert() method with URI and values bucket
318 * 5. Verify the return value
319 * @tc.expect: Insert operation succeeds (return value > 0)
320 */
321 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Insert_Test_002, TestSize.Level0)
322 {
323 LOG_INFO("SlientSwitch_SwitchEnable_Insert_Test_002::Start");
324 Uri uri("");
325 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
326 EXPECT_EQ(retVal, E_OK);
327
328 auto helper = g_slientAccessHelper;
329 DataShare::DataShareValuesBucket valuesBucket;
330 std::string value = "wangwu";
331 valuesBucket.Put(TBL_STU_NAME, value);
332 int age = 25;
333 valuesBucket.Put(TBL_STU_AGE, age);
334 uri = Uri(SLIENT_ACCESS_URI);
335 retVal = helper->Insert(uri, valuesBucket);
336 EXPECT_EQ((retVal > 0), true);
337 LOG_INFO("SlientSwitch_SwitchEnable_Insert_Test_002::End");
338 }
339
340 /**
341 * @tc.name: SlientSwitch_SwitchDisable_Update_Test_001
342 * @tc.desc: Test data update when silent switch is disabled with valid URI
343 * @tc.type: FUNC
344 * @tc.require: NA
345 * @tc.precon: None
346 * @tc.step:
347 * 1. Create Uri object with SLIENT_ACCESS_URI
348 * 2. Disable silent switch using SetSilentSwitch() with URI
349 * 3. Prepare DataShareValuesBucket with update data (age=50)
350 * 4. Create DataSharePredicates to select record with name="lisi"
351 * 5. Call Update() method with URI, predicates and values bucket
352 * 6. Verify the return value
353 * @tc.expect: Update operation fails (return value <= 0)
354 */
355 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Update_Test_001, TestSize.Level0)
356 {
357 LOG_INFO("SlientSwitch_SwitchDisable_Update_Test_001::Start");
358 Uri uri(SLIENT_ACCESS_URI);
359 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
360 EXPECT_EQ(retVal, E_OK);
361
362 auto helper = g_slientAccessHelper;
363 DataShare::DataShareValuesBucket valuesBucket;
364 int value = 50;
365 valuesBucket.Put(TBL_STU_AGE, value);
366 DataShare::DataSharePredicates predicates;
367 std::string selections = TBL_STU_NAME + " = 'lisi'";
368 predicates.SetWhereClause(selections);
369 retVal = helper->Update(uri, predicates, valuesBucket);
370 EXPECT_EQ((retVal > 0), false);
371 LOG_INFO("SlientSwitch_SwitchDisable_Update_Test_001::End");
372 }
373
374 /**
375 * @tc.name: SlientSwitch_SwitchDisable_Update_Test_002
376 * @tc.desc: Test data update when silent switch is disabled with empty URI
377 * @tc.type: FUNC
378 * @tc.require: NA
379 * @tc.precon: None
380 * @tc.step:
381 * 1. Disable silent switch using SetSilentSwitch() with empty URI
382 * 2. Prepare DataShareValuesBucket with update data (age=50)
383 * 3. Create DataSharePredicates to select record with name="wangwu"
384 * 4. Create Uri object with SLIENT_ACCESS_URI
385 * 5. Call Update() method with URI, predicates and values bucket
386 * 6. Verify the return value
387 * @tc.expect: Update operation fails (return value <= 0)
388 */
389 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Update_Test_002, TestSize.Level0)
390 {
391 LOG_INFO("SlientSwitch_SwitchDisable_Update_Test_002::Start");
392 Uri uri("");
393 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
394 EXPECT_EQ(retVal, E_OK);
395
396 auto helper = g_slientAccessHelper;
397 DataShare::DataShareValuesBucket valuesBucket;
398 int value = 50;
399 valuesBucket.Put(TBL_STU_AGE, value);
400 DataShare::DataSharePredicates predicates;
401 std::string selections = TBL_STU_NAME + " = 'wangwu'";
402 predicates.SetWhereClause(selections);
403 uri = Uri(SLIENT_ACCESS_URI);
404 retVal = helper->Update(uri, predicates, valuesBucket);
405 EXPECT_EQ((retVal > 0), false);
406 LOG_INFO("SlientSwitch_SwitchDisable_Update_Test_002::End");
407 }
408
409 /**
410 * @tc.name: SlientSwitch_SwitchEnable_Update_Test_001
411 * @tc.desc: Test data update when silent switch is enabled with valid URI
412 * @tc.type: FUNC
413 * @tc.require: NA
414 * @tc.precon: None
415 * @tc.step:
416 * 1. Create Uri object with SLIENT_ACCESS_URI
417 * 2. Enable silent switch using SetSilentSwitch() with URI
418 * 3. Prepare DataShareValuesBucket with update data (age=50)
419 * 4. Create DataSharePredicates to select record with name="lisi"
420 * 5. Call Update() method with URI, predicates and values bucket
421 * 6. Verify the return value
422 * @tc.expect: Update operation succeeds (return value > 0)
423 */
424 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Update_Test_001, TestSize.Level0)
425 {
426 LOG_INFO("SlientSwitch_SwitchEnable_Update_Test_001::Start");
427 Uri uri(SLIENT_ACCESS_URI);
428 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
429 EXPECT_EQ(retVal, E_OK);
430
431 auto helper = g_slientAccessHelper;
432 DataShare::DataShareValuesBucket valuesBucket;
433 int value = 50;
434 valuesBucket.Put(TBL_STU_AGE, value);
435 DataShare::DataSharePredicates predicates;
436 std::string selections = TBL_STU_NAME + " = 'lisi'";
437 predicates.SetWhereClause(selections);
438 retVal = helper->Update(uri, predicates, valuesBucket);
439 EXPECT_EQ((retVal > 0), true);
440 LOG_INFO("SlientSwitch_SwitchEnable_Update_Test_001::End");
441 }
442
443 /**
444 * @tc.name: SlientSwitch_SwitchEnable_Update_Test_002
445 * @tc.desc: Test data update when silent switch is enabled with empty URI
446 * @tc.type: FUNC
447 * @tc.require: NA
448 * @tc.precon: None
449 * @tc.step:
450 * 1. Enable silent switch using SetSilentSwitch() with empty URI
451 * 2. Prepare DataShareValuesBucket with update data (age=50)
452 * 3. Create DataSharePredicates to select record with name="wangwu"
453 * 4. Create Uri object with SLIENT_ACCESS_URI
454 * 5. Call Update() method with URI, predicates and values bucket
455 * 6. Verify the return value
456 * @tc.expect: Update operation succeeds (return value > 0)
457 */
458 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Update_Test_002, TestSize.Level0)
459 {
460 LOG_INFO("SlientSwitch_SwitchEnable_Update_Test_002::Start");
461 Uri uri("");
462 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
463 EXPECT_EQ(retVal, E_OK);
464
465 auto helper = g_slientAccessHelper;
466 DataShare::DataShareValuesBucket valuesBucket;
467 int value = 50;
468 valuesBucket.Put(TBL_STU_AGE, value);
469 DataShare::DataSharePredicates predicates;
470 std::string selections = TBL_STU_NAME + " = 'wangwu'";
471 predicates.SetWhereClause(selections);
472 uri = Uri(SLIENT_ACCESS_URI);
473 retVal = helper->Update(uri, predicates, valuesBucket);
474 EXPECT_EQ((retVal > 0), true);
475 LOG_INFO("SlientSwitch_SwitchEnable_Update_Test_002::End");
476 }
477
478 /**
479 * @tc.name: SlientSwitch_SwitchDisable_Query_Test_001
480 * @tc.desc: Test data query when silent switch is disabled with valid URI
481 * @tc.type: FUNC
482 * @tc.require: NA
483 * @tc.precon: None
484 * @tc.step:
485 * 1. Create Uri object with SLIENT_ACCESS_URI
486 * 2. Disable silent switch using SetSilentSwitch() with URI
487 * 3. Create DataSharePredicates to select record with name="lisi"
488 * 4. Call Query() method with URI, predicates and empty columns list
489 * 5. Verify the result set
490 * @tc.expect: Query operation fails (resultSet is nullptr)
491 */
492 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Query_Test_001, TestSize.Level0)
493 {
494 LOG_INFO("SlientSwitch_SwitchDisable_Query_Test_001::Start");
495 Uri uri(SLIENT_ACCESS_URI);
496 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
497 EXPECT_EQ(retVal, E_OK);
498
499 auto helper = g_slientAccessHelper;
500 DataShare::DataSharePredicates predicates;
501 predicates.EqualTo(TBL_STU_NAME, "lisi");
502 vector<string> columns;
503 auto resultSet = helper->Query(uri, predicates, columns);
504 ASSERT_TRUE(resultSet == nullptr);
505 LOG_INFO("SlientSwitch_SwitchDisable_Query_Test_001::End");
506 }
507
508 /**
509 * @tc.name: SlientSwitch_SwitchDisable_Query_Test_002
510 * @tc.desc: Test data query when silent switch is disabled with empty URI
511 * @tc.type: FUNC
512 * @tc.require: NA
513 * @tc.precon: None
514 * @tc.step:
515 * 1. Disable silent switch using SetSilentSwitch() with empty URI
516 * 2. Create DataSharePredicates to select record with name="wangwu"
517 * 3. Create Uri object with SLIENT_ACCESS_URI
518 * 4. Call Query() method with URI, predicates and empty columns list
519 * 5. Verify the result set
520 * @tc.expect: Query operation fails (resultSet is nullptr)
521 */
522 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Query_Test_002, TestSize.Level0)
523 {
524 LOG_INFO("SlientSwitch_SwitchDisable_Query_Test_002::Start");
525 Uri uri("");
526 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
527 EXPECT_EQ(retVal, E_OK);
528
529 auto helper = g_slientAccessHelper;
530 DataShare::DataSharePredicates predicates;
531 predicates.EqualTo(TBL_STU_NAME, "wangwu");
532 vector<string> columns;
533 uri = Uri(SLIENT_ACCESS_URI);
534 auto resultSet = helper->Query(uri, predicates, columns);
535 ASSERT_TRUE(resultSet == nullptr);
536 LOG_INFO("SlientSwitch_SwitchDisable_Query_Test_002::End");
537 }
538
539 /**
540 * @tc.name: SlientSwitch_SwitchEnable_Query_Test_001
541 * @tc.desc: Test data query when silent switch is enabled with valid URI
542 * @tc.type: FUNC
543 * @tc.require: NA
544 * @tc.precon: None
545 * @tc.step:
546 * 1. Create Uri object with SLIENT_ACCESS_URI
547 * 2. Enable silent switch using SetSilentSwitch() with URI
548 * 3. Create DataSharePredicates to select record with name="lisi"
549 * 4. Call Query() method with URI, predicates and empty columns list
550 * 5. Check row count of the result set
551 * @tc.expect: Query operation succeeds and returns 1 record
552 */
553 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Query_Test_001, TestSize.Level0)
554 {
555 LOG_INFO("SlientSwitch_SwitchEnable_Query_Test_001::Start");
556 Uri uri(SLIENT_ACCESS_URI);
557 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
558 EXPECT_EQ(retVal, E_OK);
559
560 auto helper = g_slientAccessHelper;
561 DataShare::DataSharePredicates predicates;
562 predicates.EqualTo(TBL_STU_NAME, "lisi");
563 vector<string> columns;
564 auto resultSet = helper->Query(uri, predicates, columns);
565 int result = 0;
566 if (resultSet != nullptr) {
567 resultSet->GetRowCount(result);
568 }
569 EXPECT_EQ(result, 1);
570 LOG_INFO("SlientSwitch_SwitchEnable_Query_Test_001::End");
571 }
572
573 /**
574 * @tc.name: SlientSwitch_SwitchEnable_Query_Test_002
575 * @tc.desc: Test data query when silent switch is enabled with empty URI
576 * @tc.type: FUNC
577 * @tc.require: NA
578 * @tc.precon: None
579 * @tc.step:
580 * 1. Enable silent switch using SetSilentSwitch() with empty URI
581 * 2. Create DataSharePredicates to select record with name="wangwu"
582 * 3. Create Uri object with SLIENT_ACCESS_URI
583 * 4. Call Query() method with URI, predicates and empty columns list
584 * 5. Check row count of the result set
585 * @tc.expect: Query operation succeeds and returns 1 record
586 */
587 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Query_Test_002, TestSize.Level0)
588 {
589 LOG_INFO("SlientSwitch_SwitchEnable_Query_Test_002::Start");
590 Uri uri("");
591 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
592 EXPECT_EQ(retVal, E_OK);
593
594 auto helper = g_slientAccessHelper;
595 DataShare::DataSharePredicates predicates;
596 predicates.EqualTo(TBL_STU_NAME, "wangwu");
597 vector<string> columns;
598 uri = Uri(SLIENT_ACCESS_URI);
599 auto resultSet = helper->Query(uri, predicates, columns);
600 int result = 0;
601 if (resultSet != nullptr) {
602 resultSet->GetRowCount(result);
603 }
604 EXPECT_EQ(result, 1);
605 LOG_INFO("SlientSwitch_SwitchEnable_Query_Test_002::End");
606 }
607
608 /**
609 * @tc.name: SlientSwitch_SwitchDisable_Delete_Test_001
610 * @tc.desc: Test data deletion when silent switch is disabled with valid URI
611 * @tc.type: FUNC
612 * @tc.require: NA
613 * @tc.precon: None
614 * @tc.step:
615 * 1. Create Uri object with SLIENT_ACCESS_URI
616 * 2. Disable silent switch using SetSilentSwitch() with URI
617 * 3. Create DataSharePredicates to select record with name="lisi"
618 * 4. Call Delete() method with URI and predicates
619 * 5. Verify the return value
620 * @tc.expect: Delete operation fails (return value <= 0)
621 */
622 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Delete_Test_001, TestSize.Level0)
623 {
624 LOG_INFO("SlientSwitch_SwitchDisable_Delete_Test_001::Start");
625 Uri uri(SLIENT_ACCESS_URI);
626 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
627 EXPECT_EQ(retVal, E_OK);
628
629 auto helper = g_slientAccessHelper;
630 DataShare::DataSharePredicates deletePredicates;
631 std::string selections = TBL_STU_NAME + " = 'lisi'";
632 deletePredicates.SetWhereClause(selections);
633 retVal = helper->Delete(uri, deletePredicates);
634 EXPECT_EQ((retVal > 0), false);
635 LOG_INFO("SlientSwitch_SwitchDisable_Delete_Test_001::End");
636 }
637
638 /**
639 * @tc.name: SlientSwitch_SwitchDisable_Delete_Test_002
640 * @tc.desc: Test data deletion when silent switch is disabled with empty URI
641 * @tc.type: FUNC
642 * @tc.require: NA
643 * @tc.precon: None
644 * @tc.step:
645 * 1. Disable silent switch using SetSilentSwitch() with empty URI
646 * 2. Create DataSharePredicates to select record with name="wangwu"
647 * 3. Create Uri object with SLIENT_ACCESS_URI
648 * 4. Call Delete() method with URI and predicates
649 * 5. Verify the return value
650 * @tc.expect: Delete operation fails (return value <= 0)
651 */
652 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_Delete_Test_002, TestSize.Level0)
653 {
654 LOG_INFO("SlientSwitch_SwitchDisable_Delete_Test_002::Start");
655 Uri uri("");
656 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
657 EXPECT_EQ(retVal, E_OK);
658
659 auto helper = g_slientAccessHelper;
660 DataShare::DataSharePredicates deletePredicates;
661 std::string selections = TBL_STU_NAME + " = 'wangwu'";
662 deletePredicates.SetWhereClause(selections);
663 uri = Uri(SLIENT_ACCESS_URI);
664 retVal = helper->Delete(uri, deletePredicates);
665 EXPECT_EQ((retVal > 0), false);
666 LOG_INFO("SlientSwitch_SwitchDisable_Delete_Test_002::End");
667 }
668
669 /**
670 * @tc.name: SlientSwitch_SwitchEnable_Delete_Test_001
671 * @tc.desc: Test data deletion when silent switch is enabled with valid URI
672 * @tc.type: FUNC
673 * @tc.require: NA
674 * @tc.precon: None
675 * @tc.step:
676 * 1. Create Uri object with SLIENT_ACCESS_URI
677 * 2. Enable silent switch using SetSilentSwitch() with URI
678 * 3. Create DataSharePredicates to select record with name="lisi"
679 * 4. Call Delete() method with URI and predicates
680 * 5. Verify the return value
681 * @tc.expect: Delete operation succeeds (return value > 0)
682 */
683 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Delete_Test_001, TestSize.Level0)
684 {
685 LOG_INFO("SlientSwitch_SwitchEnable_Delete_Test_001::Start");
686 Uri uri(SLIENT_ACCESS_URI);
687 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
688 EXPECT_EQ(retVal, E_OK);
689
690 auto helper = g_slientAccessHelper;
691 DataShare::DataSharePredicates deletePredicates;
692 std::string selections = TBL_STU_NAME + " = 'lisi'";
693 deletePredicates.SetWhereClause(selections);
694 retVal = helper->Delete(uri, deletePredicates);
695 EXPECT_EQ((retVal > 0), true);
696 LOG_INFO("SlientSwitch_SwitchEnable_Delete_Test_001::End");
697 }
698
699 /**
700 * @tc.name: SlientSwitch_SwitchEnable_Delete_Test_002
701 * @tc.desc: Test data deletion when silent switch is enabled with empty URI
702 * @tc.type: FUNC
703 * @tc.require: NA
704 * @tc.precon: None
705 * @tc.step:
706 * 1. Enable silent switch using SetSilentSwitch() with empty URI
707 * 2. Create DataSharePredicates to select record with name="wangwu"
708 * 3. Create Uri object with SLIENT_ACCESS_URI
709 * 4. Call Delete() method with URI and predicates
710 * 5. Verify the return value
711 * @tc.expect: Delete operation succeeds (return value > 0)
712 */
713 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_Delete_Test_002, TestSize.Level0)
714 {
715 LOG_INFO("SlientSwitch_SwitchEnable_Delete_Test_002::Start");
716 Uri uri("");
717 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
718 EXPECT_EQ(retVal, E_OK);
719
720 auto helper = g_slientAccessHelper;
721 DataShare::DataSharePredicates deletePredicates;
722 std::string selections = TBL_STU_NAME + " = 'wangwu'";
723 deletePredicates.SetWhereClause(selections);
724 uri = Uri(SLIENT_ACCESS_URI);
725 retVal = helper->Delete(uri, deletePredicates);
726 EXPECT_EQ((retVal > 0), true);
727 LOG_INFO("SlientSwitch_SwitchEnable_Delete_Test_002::End");
728 }
729
730 /**
731 * @tc.name: SlientSwitch_SwitchDisable_CreateHelper_Test_001
732 * @tc.desc: Test DataShareHelper creation when silent switch is disabled
733 * @tc.type: FUNC
734 * @tc.require: NA
735 * @tc.precon: None
736 * @tc.step:
737 1. Create initial DataShareHelper with valid parameters (should succeed)
738 2. Disable silent switch using SetSilentSwitch() with empty URI
739 3. Try to create another DataShareHelper with same parameters
740 4. Re-enable silent switch
741 * @tc.expect:
742 1. First helper creation succeeds (helper != nullptr)
743 2. Second helper creation fails (helper == nullptr)
744 3. Re-enabling switch returns E_OK
745 */
746 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_CreateHelper_Test_001, TestSize.Level0)
747 {
748 LOG_INFO("SlientSwitch_SwitchDisable_CreateHelper_Test_001::Start");
749 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
750 ASSERT_TRUE(helper != nullptr);
751
752 Uri uri("");
753 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
754 EXPECT_EQ(retVal, E_OK);
755
756 helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
757 ASSERT_TRUE(helper == nullptr);
758 retVal = DataShareHelper::SetSilentSwitch(uri, true);
759 EXPECT_EQ(retVal, E_OK);
760 LOG_INFO("SlientSwitch_SwitchDisable_CreateHelper_Test_001::End");
761 }
762
GetTemplate()763 Template GetTemplate()
764 {
765 PredicateTemplateNode node1("p1", "select name0 as name from TBL00");
766 PredicateTemplateNode node2("p2", "select name1 as name from TBL00");
767 std::vector<PredicateTemplateNode> nodes;
768 nodes.emplace_back(node1);
769 nodes.emplace_back(node2);
770 Template tpl(nodes, "select name1 as name from TBL00");
771 return tpl;
772 }
773
774 /**
775 * @tc.name: SlientSwitch_SwitchDisable_CreateHelper_Test_002
776 * @tc.desc: Test AddQueryTemplate when silent switch is disabled
777 * @tc.type: FUNC
778 * @tc.require: NA
779 * @tc.precon: None
780 * @tc.step:
781 * 1. Disable silent switch using SetSilentSwitch() with empty URI
782 * 2. Create DataShareHelper with valid parameters
783 * 3. Get query template using GetTemplate()
784 * 4. Call AddQueryTemplate with proxy URI, subscriber ID and template
785 * 5. Re-enable silent switch
786 * @tc.expect:
787 * 1. Helper creation succeeds (helper != nullptr)
788 * 2. AddQueryTemplate returns ERROR (-1)
789 * 3. Re-enabling switch returns E_OK
790 */
791 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchDisable_CreateHelper_Test_002, TestSize.Level0)
792 {
793 LOG_INFO("SlientSwitch_SwitchDisable_CreateHelper_Test_002::Start");
794 Uri uri("");
795 int retVal = DataShareHelper::SetSilentSwitch(uri, false);
796 EXPECT_EQ(retVal, E_OK);
797
798 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI, DATA_SHARE_URI);
799 ASSERT_TRUE(helper != nullptr);
800
801 auto tpl = GetTemplate();
802 auto result = helper->AddQueryTemplate(DATA_SHARE_PROXY_URI, SUBSCRIBER_ID, tpl);
803 EXPECT_EQ(result, ERROR);
804 retVal = DataShareHelper::SetSilentSwitch(uri, true);
805 EXPECT_EQ(retVal, E_OK);
806 LOG_INFO("SlientSwitch_SwitchDisable_CreateHelper_Test_002::End");
807 }
808
809 /**
810 * @tc.name: SlientSwitch_SwitchEnable_CreateHelper_Test_001
811 * @tc.desc: Test AddQueryTemplate when silent switch is enabled (without provider URI)
812 * @tc.type: FUNC
813 * @tc.require: NA
814 * @tc.precon: None
815 * @tc.step:
816 * 1. Enable silent switch using SetSilentSwitch() with empty URI
817 * 2. Create DataShareHelper with valid parameters (without provider URI)
818 * 3. Get query template using GetTemplate()
819 * 4. Call AddQueryTemplate with proxy URI, subscriber ID and template
820 * @tc.expect:
821 * 1. Helper creation succeeds (helper != nullptr)
822 * 2. AddQueryTemplate returns E_BUNDLE_NAME_NOT_EXIST
823 */
824 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_CreateHelper_Test_001, TestSize.Level0)
825 {
826 LOG_INFO("SlientSwitch_SwitchEnable_CreateHelper_Test_001::Start");
827 Uri uri("");
828 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
829 EXPECT_EQ(retVal, E_OK);
830
831 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
832 ASSERT_TRUE(helper != nullptr);
833
834 auto tpl = GetTemplate();
835 auto result = helper->AddQueryTemplate(DATA_SHARE_PROXY_URI, SUBSCRIBER_ID, tpl);
836 EXPECT_EQ(result, E_BUNDLE_NAME_NOT_EXIST);
837 LOG_INFO("SlientSwitch_SwitchEnable_CreateHelper_Test_001::End");
838 }
839
840 /**
841 * @tc.name: SlientSwitch_SwitchEnable_CreateHelper_Test_002
842 * @tc.desc: Test AddQueryTemplate when silent switch is enabled (with provider URI)
843 * @tc.type: FUNC
844 * @tc.require: NA
845 * @tc.precon: None
846 * @tc.step:
847 * 1. Enable silent switch using SetSilentSwitch() with empty URI
848 * 2. Create DataShareHelper with valid parameters (including provider URI)
849 * 3. Get query template using GetTemplate()
850 * 4. Call AddQueryTemplate with proxy URI, subscriber ID and template
851 * @tc.expect:
852 * 1. Helper creation succeeds (helper != nullptr)
853 * 2. AddQueryTemplate returns E_BUNDLE_NAME_NOT_EXIST
854 */
855 HWTEST_F(SlientSwitchTest, SlientSwitch_SwitchEnable_CreateHelper_Test_002, TestSize.Level0)
856 {
857 LOG_INFO("SlientSwitch_SwitchEnable_CreateHelper_Test_002::Start");
858 Uri uri("");
859 int retVal = DataShareHelper::SetSilentSwitch(uri, true);
860 EXPECT_EQ(retVal, E_OK);
861
862 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI, DATA_SHARE_URI);
863 ASSERT_TRUE(helper != nullptr);
864
865 auto tpl = GetTemplate();
866 auto result = helper->AddQueryTemplate(DATA_SHARE_PROXY_URI, SUBSCRIBER_ID, tpl);
867 EXPECT_EQ(result, E_BUNDLE_NAME_NOT_EXIST);
868 LOG_INFO("SlientSwitch_SwitchEnable_CreateHelper_Test_002::End");
869 }
870 } // namespace DataShare
871 } // namespace OHOS