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 #include "datashare_helper.h"
16
17 #include <gtest/gtest.h>
18
19 #include "accesstoken_kit.h"
20 #include "data_ability_observer_stub.h"
21 #include "datashare_log.h"
22 #include "hap_token_info.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26
27 namespace OHOS {
28 namespace DataShare {
29 using namespace testing::ext;
30 using namespace OHOS::Security::AccessToken;
31 using ChangeInfo = DataShareObserver::ChangeInfo;
32 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
33 std::string DATA_SHARE_URI = "datashare:///com.acts.datasharetest";
34 std::string SLIENT_ACCESS_URI = "datashareproxy://com.acts.datasharetest/test?Proxy=true";
35 std::string TBL_STU_NAME = "name";
36 std::string TBL_STU_AGE = "age";
37 std::shared_ptr<DataShare::DataShareHelper> g_exHelper;
38
39 class DataShareThresholdTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 };
46
47 class IDataShareAbilityObserverTest : public AAFwk::DataAbilityObserverStub {
48 public:
IDataShareAbilityObserverTest(int code)49 explicit IDataShareAbilityObserverTest(int code)
50 {
51 code_ = code;
52 }
53
~IDataShareAbilityObserverTest()54 ~IDataShareAbilityObserverTest()
55 {}
56
OnChange()57 void OnChange() {}
58
59 int code_;
60 };
61
CreateDataShareHelper(int32_t systemAbilityId,std::string uri)62 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t systemAbilityId, std::string uri)
63 {
64 LOG_INFO("CreateDataShareHelper start");
65 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
66 if (saManager == nullptr) {
67 LOG_ERROR("GetSystemAbilityManager get samgr failed.");
68 return nullptr;
69 }
70 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
71 if (remoteObj == nullptr) {
72 LOG_ERROR("GetSystemAbility service failed.");
73 return nullptr;
74 }
75 return DataShare::DataShareHelper::Creator(remoteObj, uri);
76 }
77
78
GetPermissionStateFulls()79 std::vector<PermissionStateFull> GetPermissionStateFulls()
80 {
81 std::vector<PermissionStateFull> permissionStateFulls = {
82 {
83 .permissionName = "ohos.permission.WRITE_CONTACTS",
84 .isGeneral = true,
85 .resDeviceID = { "local" },
86 .grantStatus = { PermissionState::PERMISSION_GRANTED },
87 .grantFlags = { 1 }
88 },
89 {
90 .permissionName = "ohos.permission.WRITE_CALL_LOG",
91 .isGeneral = true,
92 .resDeviceID = { "local" },
93 .grantStatus = { PermissionState::PERMISSION_GRANTED },
94 .grantFlags = { 1 }
95 },
96 {
97 .permissionName = "ohos.permission.GET_BUNDLE_INFO",
98 .isGeneral = true,
99 .resDeviceID = { "local" },
100 .grantStatus = { PermissionState::PERMISSION_GRANTED },
101 .grantFlags = { 1 }
102 }
103 };
104 return permissionStateFulls;
105 }
106
SetUpTestCase(void)107 void DataShareThresholdTest::SetUpTestCase(void)
108 {
109 LOG_INFO("SetUpTestCase invoked");
110 auto dataShareHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, DATA_SHARE_URI);
111 ASSERT_TRUE(dataShareHelper != nullptr);
112 int sleepTime = 3;
113 sleep(sleepTime);
114
115 HapInfoParams info = {
116 .userID = 100,
117 .bundleName = "ohos.datashareclienttest.demo",
118 .instIndex = 0,
119 .appIDDesc = "ohos.datashareclienttest.demo"
120 };
121 auto permStateList = GetPermissionStateFulls();
122 HapPolicyParams policy = {
123 .apl = APL_NORMAL,
124 .domain = "test.domain",
125 .permList = {
126 {
127 .permissionName = "ohos.permission.test",
128 .bundleName = "ohos.datashareclienttest.demo",
129 .grantMode = 1,
130 .availableLevel = APL_NORMAL,
131 .label = "label",
132 .labelId = 1,
133 .description = "ohos.datashareclienttest.demo",
134 .descriptionId = 1
135 }
136 },
137 .permStateList = permStateList
138 };
139 AccessTokenKit::AllocHapToken(info, policy);
140 auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenIDEx(
141 info.userID, info.bundleName, info.instIndex);
142 SetSelfTokenID(testTokenId.tokenIDEx);
143
144 g_exHelper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, DATA_SHARE_URI);
145 ASSERT_TRUE(g_exHelper != nullptr);
146 LOG_INFO("SetUpTestCase end");
147 }
148
TearDownTestCase(void)149 void DataShareThresholdTest::TearDownTestCase(void)
150 {
151 auto tokenId = AccessTokenKit::GetHapTokenIDEx(100, "ohos.datashareclienttest.demo", 0);
152 AccessTokenKit::DeleteToken(tokenId.tokenIDEx);
153 g_exHelper = nullptr;
154 }
155
SetUp(void)156 void DataShareThresholdTest::SetUp(void) {}
TearDown(void)157 void DataShareThresholdTest::TearDown(void) {}
158
159 /**
160 * @tc.name: Insert_Threshold_Test001
161 * @tc.desc: Verify silent access Insert operation behavior when exceeding and not exceeding the threshold
162 * @tc.type: FUNC
163 * @tc.require: issueIC8OCN
164 * @tc.precon: None
165 * @tc.step:
166 1. Create a DataShareHelper instance with silent access configuration
167 2. Prepare test data (name and age) in a DataShareValuesBucket
168 3. Perform initial Insert operation and verify success
169 4. Perform 2998 additional Insert operations (not exceeding threshold) and verify success
170 5. Perform 10 more Insert operations (exceeding threshold) and check results
171 * @tc.expect:
172 1. DataShareHelper is created successfully (not nullptr)
173 2. All Insert operations before threshold return positive values (success)
174 3. All Insert operations after threshold return DATA_SHARE_ERROR(-1)
175 */
176 HWTEST_F(DataShareThresholdTest, Insert_Threshold_Test001, TestSize.Level1)
177 {
178 LOG_INFO("Insert_Threshold_Test001::Start");
179 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
180 ASSERT_TRUE(helper != nullptr);
181
182 Uri uri(SLIENT_ACCESS_URI);
183
184 DataShare::DataShareValuesBucket valuesBucket;
185 std::string value = "zhangsan";
186 valuesBucket.Put(TBL_STU_NAME, value);
187 int age = 18;
188 valuesBucket.Put(TBL_STU_AGE, age);
189 int retVal = helper->Insert(uri, valuesBucket);
190 EXPECT_EQ((retVal > 0), true);
191
192 DataShare::DataShareValuesBucket valuesBucket1;
193 std::string value1 = "lisi";
194 valuesBucket1.Put(TBL_STU_NAME, value1);
195 age = 25;
196 valuesBucket1.Put(TBL_STU_AGE, age);
197 // not over threshold, insert success
198 for (int i = 0; i < 2998; i++) {
199 retVal = helper->Insert(uri, valuesBucket1);
200 EXPECT_EQ((retVal > 0), true);
201 }
202
203 // over threshold, insert ret DATA_SHARE_ERROR(-1)
204 for (int i = 0; i < 10; i++) {
205 retVal = helper->Insert(uri, valuesBucket1);
206 EXPECT_EQ(retVal, -1);
207 }
208 LOG_INFO("Insert_Threshold_Test001::End");
209 }
210
211 /**
212 * @tc.name: Update_Threshold_Test001
213 * @tc.desc: Verify silent access Update operation behavior when exceeding and not exceeding the threshold
214 * @tc.type: FUNC
215 * @tc.require: issueIC8OCN
216 * @tc.precon: None
217 * @tc.step:
218 1. Create a DataShareHelper instance with silent access configuration
219 2. Define update predicates to target specific data
220 3. Prepare update data in a DataShareValuesBucket
221 4. Perform 2999 Update operations (not exceeding threshold) and verify success
222 5. Perform 10 more Update operations (exceeding threshold) and check results
223 * @tc.expect:
224 1. DataShareHelper is created successfully (not nullptr)
225 2. All Update operations before threshold return 1 (success)
226 3. All Update operations after threshold return DATA_SHARE_ERROR(-1)
227 */
228 HWTEST_F(DataShareThresholdTest, Update_Threshold_Test001, TestSize.Level1)
229 {
230 LOG_INFO("Update_Threshold_Test001::Start");
231 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
232 ASSERT_TRUE(helper != nullptr);
233
234 Uri uri(SLIENT_ACCESS_URI);
235
236 DataShare::DataSharePredicates predicates;
237 std::string selections = TBL_STU_NAME + " = 'zhangsan'";
238 predicates.SetWhereClause(selections);
239
240 DataShare::DataShareValuesBucket valuesBucket;
241 valuesBucket.Put(TBL_STU_AGE, 10);
242 // not over threshold, update success
243 for (int i = 0; i < 2999; i++) {
244 int retVal = helper->Update(uri, predicates, valuesBucket);
245 EXPECT_EQ(retVal, 1);
246 }
247
248 // over threshold, update ret DATA_SHARE_ERROR(-1)
249 for (int i = 0; i < 10; i++) {
250 int retVal = helper->Update(uri, predicates, valuesBucket);
251 EXPECT_EQ(retVal, -1);
252 }
253 LOG_INFO("Update_Threshold_Test001::End");
254 }
255
256 /**
257 * @tc.name: Query_Threshold_Test001
258 * @tc.desc: Verify silent access Query operation behavior when exceeding and not exceeding the threshold
259 * @tc.type: FUNC
260 * @tc.require: issueIC8OCN
261 * @tc.precon: None
262 * @tc.step:
263 1. Create a DataShareHelper instance with silent access configuration
264 2. Define query predicates to target specific data
265 3. Perform 2999 Query operations (not exceeding threshold) and verify results
266 4. Perform 10 more Query operations (exceeding threshold) and check results
267 * @tc.expect:
268 1. DataShareHelper is created successfully (not nullptr)
269 2. All Query operations before threshold return valid result sets with 1 row
270 3. All Query operations after threshold return nullptr (DATA_SHARE_ERROR)
271 */
272 HWTEST_F(DataShareThresholdTest, Query_Threshold_Test001, TestSize.Level1)
273 {
274 LOG_INFO("Query_Threshold_Test001::Start");
275 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
276 ASSERT_TRUE(helper != nullptr);
277
278 Uri uri(SLIENT_ACCESS_URI);
279
280 DataShare::DataSharePredicates predicates;
281 predicates.EqualTo(TBL_STU_NAME, "zhangsan");
282 vector<string> columns;
283 int result = 0;
284
285 // not over threshold, query success
286 for (int i = 0; i < 2999; i++) {
287 auto resultSet = helper->Query(uri, predicates, columns);
288 ASSERT_NE(resultSet, nullptr);
289 resultSet->GetRowCount(result);
290 EXPECT_EQ(result, 1);
291 resultSet->Close();
292 }
293
294 // over threshold, query ret DATA_SHARE_ERROR(-1)
295 for (int i = 0; i < 10; i++) {
296 auto resultSet = helper->Query(uri, predicates, columns);
297 ASSERT_EQ(resultSet, nullptr);
298 }
299 LOG_INFO("Query_Threshold_Test001::End");
300 }
301
302 /**
303 * @tc.name: Delete_Threshold_Test001
304 * @tc.desc: Verify silent access Delete operation behavior when exceeding and not exceeding the threshold
305 * @tc.type: FUNC
306 * @tc.require: issueIC8OCN
307 * @tc.precon: None
308 * @tc.step:
309 1. Create a DataShareHelper instance with silent access configuration
310 2. Define delete predicates targeting non-existent data
311 3. Perform 2999 Delete operations (not exceeding threshold) and verify results
312 4. Perform 10 more Delete operations (exceeding threshold) and check results
313 * @tc.expect:
314 1. DataShareHelper is created successfully (not nullptr)
315 2. All Delete operations before threshold return 0 (no data deleted)
316 3. All Delete operations after threshold return DATA_SHARE_ERROR(-1)
317 */
318 HWTEST_F(DataShareThresholdTest, Delete_Threshold_Test001, TestSize.Level1)
319 {
320 LOG_INFO("Delete_Threshold_Test001::Start");
321 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
322 ASSERT_TRUE(helper != nullptr);
323
324 Uri uri(SLIENT_ACCESS_URI);
325
326 DataShare::DataSharePredicates deletePredicates;
327 std::string selections = TBL_STU_NAME + " = 'lisan'";
328 deletePredicates.SetWhereClause(selections);
329
330 // not over threshold, delete data not exists return 0
331 for (int i = 0; i < 2999; i++) {
332 int retVal = helper->Delete(uri, deletePredicates);
333 EXPECT_EQ(retVal, 0);
334 }
335
336 // over threshold, delete ret -1
337 for (int i = 0; i < 10; i++) {
338 int retVal = helper->Delete(uri, deletePredicates);
339 EXPECT_EQ(retVal, -1);
340 }
341 LOG_INFO("Delete_Threshold_Test001::End");
342 }
343
344 /**
345 * @tc.name: InsertEx_Threshold_Test001
346 * @tc.desc: Verify silent access InsertEx operation behavior when exceeding and not exceeding the threshold
347 * @tc.type: FUNC
348 * @tc.require: issueIC8OCN
349 * @tc.precon: None
350 * @tc.step:
351 1. Create a DataShareHelper instance with silent access configuration
352 2. Prepare test data (name and age) in a DataShareValuesBucket
353 3. Perform 2999 InsertEx operations (not exceeding threshold) and verify success
354 4. Perform 10 more InsertEx operations (exceeding threshold) and check results
355 * @tc.expect:
356 1. DataShareHelper is created successfully (not nullptr)
357 2. All InsertEx operations before threshold return (0, positive value) indicating success
358 3. All InsertEx operations after threshold return (DATA_SHARE_ERROR(-1), 0)
359 */
360 HWTEST_F(DataShareThresholdTest, InsertEx_Threshold_Test001, TestSize.Level1)
361 {
362 LOG_INFO("InsertEx_Threshold_Test001::Start");
363
364 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
365 ASSERT_TRUE(helper != nullptr);
366
367 Uri uri(SLIENT_ACCESS_URI);
368 DataShare::DataShareValuesBucket valuesBucket;
369 std::string value = "lisi";
370 valuesBucket.Put(TBL_STU_NAME, value);
371 int age = 25;
372 valuesBucket.Put(TBL_STU_AGE, age);
373 // not over threshold, insertEx success
374 for (int i = 0; i < 2999; i++) {
375 auto [errCode, retVal] = helper->InsertEx(uri, valuesBucket);
376 EXPECT_EQ(errCode, 0);
377 EXPECT_EQ((retVal > 0), true);
378 }
379
380 // over threshold, insertEx ret pair(DATA_SHARE_ERROR, 0)
381 for (int i = 0; i < 10; i++) {
382 auto [errCode, retVal] = helper->InsertEx(uri, valuesBucket);
383 EXPECT_EQ(errCode, -1);
384 EXPECT_EQ(retVal, 0);
385 }
386 LOG_INFO("InsertEx_Threshold_Test001::End");
387 }
388
389 /**
390 * @tc.name: UpdateEx_Threshold_Test001
391 * @tc.desc: Verify silent access UpdateEx operation behavior when exceeding and not exceeding the threshold
392 * @tc.type: FUNC
393 * @tc.require: issueIC8OCN
394 * @tc.precon: None
395 * @tc.step:
396 1. Create a DataShareHelper instance with silent access configuration
397 2. Define update predicates to target specific data
398 3. Prepare update data in a DataShareValuesBucket
399 4. Perform 2999 UpdateEx operations (not exceeding threshold) and verify success
400 5. Perform 10 more UpdateEx operations (exceeding threshold) and check results
401 * @tc.expect:
402 1. DataShareHelper is created successfully (not nullptr)
403 2. All UpdateEx operations before threshold return (0, 1) indicating success
404 3. All UpdateEx operations after threshold return (DATA_SHARE_ERROR(-1), 0)
405 */
406 HWTEST_F(DataShareThresholdTest, UpdateEx_Threshold_Test001, TestSize.Level1)
407 {
408 LOG_INFO("UpdateEx_Threshold_Test001::Start");
409
410 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
411 ASSERT_TRUE(helper != nullptr);
412
413 Uri uri(SLIENT_ACCESS_URI);
414
415 DataShare::DataSharePredicates predicates;
416 std::string selections = TBL_STU_NAME + " = 'zhangsan'";
417 predicates.SetWhereClause(selections);
418 DataShare::DataShareValuesBucket valuesBucket;
419 valuesBucket.Put(TBL_STU_AGE, 10);
420
421 // not over threshold, updateEx success
422 for (int i = 0; i < 2999; i++) {
423 auto [errCode, retVal] = helper->UpdateEx(uri, predicates, valuesBucket);
424 EXPECT_EQ(errCode, 0);
425 EXPECT_EQ(retVal, 1);
426 }
427
428 // over threshold, updateEx ret pair(DATA_SHARE_ERROR, 0)
429 for (int i = 0; i < 10; i++) {
430 auto [errCode, retVal] = helper->UpdateEx(uri, predicates, valuesBucket);
431 EXPECT_EQ(errCode, -1);
432 EXPECT_EQ(retVal, 0);
433 }
434 LOG_INFO("UpdateEx_Threshold_Test001::End");
435 }
436
437 /**
438 * @tc.name: DeleteEx_Threshold_Test001
439 * @tc.desc: Verify silent access DeleteEx operation behavior when exceeding and not exceeding the threshold
440 * @tc.type: FUNC
441 * @tc.require: issueIC8OCN
442 * @tc.precon: None
443 * @tc.step:
444 1. Create a DataShareHelper instance with silent access configuration
445 2. Define delete predicates targeting non-existent data
446 3. Perform 2999 DeleteEx operations (not exceeding threshold) and verify results
447 4. Perform 10 more DeleteEx operations (exceeding threshold) and check results
448 * @tc.expect:
449 1. DataShareHelper is created successfully (not nullptr)
450 2. All DeleteEx operations before threshold return (0, 0) indicating no data deleted
451 3. All DeleteEx operations after threshold return (DATA_SHARE_ERROR(-1), 0)
452 */
453 HWTEST_F(DataShareThresholdTest, DeleteEx_Threshold_Test001, TestSize.Level1)
454 {
455 LOG_INFO("DeleteEx_Threshold_Test001::Start");
456
457 auto helper = CreateDataShareHelper(STORAGE_MANAGER_MANAGER_ID, SLIENT_ACCESS_URI);
458 ASSERT_TRUE(helper != nullptr);
459
460 Uri uri(SLIENT_ACCESS_URI);
461
462 DataShare::DataSharePredicates deletePredicates;
463 std::string selections = TBL_STU_NAME + " = 'lisan'";
464 deletePredicates.SetWhereClause(selections);
465 // not over threshold, deleteEx data not exists return pair(0, 0)
466 for (int i = 0; i < 2999; i++) {
467 auto [errCode, retVal] = helper->DeleteEx(uri, deletePredicates);
468 EXPECT_EQ(errCode, 0);
469 EXPECT_EQ(retVal, 0);
470 }
471
472 // over threshold, deleteEx ret pair(DATA_SHARE_ERROR, 0)
473 for (int i = 0; i < 10; i++) {
474 auto [errCode, retVal] = helper->DeleteEx(uri, deletePredicates);
475 EXPECT_EQ(errCode, -1);
476 EXPECT_EQ(retVal, 0);
477 }
478 LOG_INFO("DeleteEx_Threshold_Test001::End");
479 }
480
481 /**
482 * @tc.name: Insert_Threshold_Test002
483 * @tc.desc: Verify non-silent access Insert operation behavior when exceeding the threshold
484 * @tc.type: FUNC
485 * @tc.require: issueIC8OCN
486 * @tc.precon: None
487 * @tc.step:
488 1. Prepare test data (name and age) in a DataShareValuesBucket
489 2. Perform 2999 Insert operations (not exceeding threshold) and verify success
490 3. Perform 10 more Insert operations (exceeding threshold) and check results
491 * @tc.expect:
492 1. All Insert operations before and after threshold return positive values (success)
493 2. No failure occurs when exceeding threshold for non-silent access
494 */
495 HWTEST_F(DataShareThresholdTest, Insert_Threshold_Test002, TestSize.Level1)
496 {
497 LOG_INFO("Insert_Threshold_Test002::Start");
498
499 Uri uri(DATA_SHARE_URI);
500 DataShare::DataShareValuesBucket valuesBucket;
501 std::string value = "lisi";
502 valuesBucket.Put(TBL_STU_NAME, value);
503 int age = 25;
504 valuesBucket.Put(TBL_STU_AGE, age);
505 // not over threshold, insert success
506 for (int i = 0; i < 2999; i++) {
507 int retVal = g_exHelper->Insert(uri, valuesBucket);
508 EXPECT_EQ((retVal > 0), true);
509 }
510
511 // over threshold, insert ret success
512 for (int i = 0; i < 10; i++) {
513 int retVal = g_exHelper->Insert(uri, valuesBucket);
514 EXPECT_EQ((retVal > 0), true);
515 }
516 LOG_INFO("Insert_Threshold_Test002::End");
517 }
518
519 /**
520 * @tc.name: Update_Threshold_Test002
521 * @tc.desc: Verify non-silent access Update operation behavior when exceeding the threshold
522 * @tc.type: FUNC
523 * @tc.require: issueIC8OCN
524 * @tc.precon: None
525 * @tc.step:
526 1. Define update predicates to target specific data
527 2. Prepare update data in a DataShareValuesBucket
528 3. Perform 2999 Update operations (not exceeding threshold) and verify success
529 4. Perform 10 more Update operations (exceeding threshold) and check results
530 * @tc.expect:
531 1. All Update operations before and after threshold return 1 (success)
532 2. No failure occurs when exceeding threshold for non-silent access
533 */
534 HWTEST_F(DataShareThresholdTest, Update_Threshold_Test002, TestSize.Level1)
535 {
536 LOG_INFO("Update_Threshold_Test002::Start");
537
538 Uri uri(DATA_SHARE_URI);
539
540 DataShare::DataSharePredicates predicates;
541 std::string selections = TBL_STU_NAME + " = 'zhangsan'";
542 predicates.SetWhereClause(selections);
543 DataShare::DataShareValuesBucket valuesBucket;
544 valuesBucket.Put(TBL_STU_AGE, 10);
545 // not over threshold, update success
546 for (int i = 0; i < 2999; i++) {
547 int retVal = g_exHelper->Update(uri, predicates, valuesBucket);
548 EXPECT_EQ(retVal, 1);
549 }
550
551 // over threshold, update success
552 for (int i = 0; i < 10; i++) {
553 int retVal = g_exHelper->Update(uri, predicates, valuesBucket);
554 EXPECT_EQ(retVal, 1);
555 }
556 LOG_INFO("Update_Threshold_Test002::End");
557 }
558
559 /**
560 * @tc.name: Query_Threshold_Test002
561 * @tc.desc: Verify non-silent access Query operation behavior when exceeding the threshold
562 * @tc.type: FUNC
563 * @tc.require: issueIC8OCN
564 * @tc.precon: None
565 * @tc.step:
566 1. Define query predicates to target specific data
567 2. Perform 2999 Query operations (not exceeding threshold) and verify results
568 3. Perform 10 more Query operations (exceeding threshold) and check results
569 * @tc.expect:
570 1. All Query operations before and after threshold return valid result sets with 1 row
571 2. No failure occurs when exceeding threshold for non-silent access
572 */
573 HWTEST_F(DataShareThresholdTest, Query_Threshold_Test002, TestSize.Level1)
574 {
575 LOG_INFO("Query_Threshold_Test002::Start");
576
577 Uri uri(DATA_SHARE_URI);
578 DataShare::DataSharePredicates predicates;
579 predicates.EqualTo(TBL_STU_NAME, "zhangsan");
580 vector<string> columns;
581 int result = 0;
582
583 // not over threshold, query success
584 for (int i = 0; i < 2999; i++) {
585 auto resultSet = g_exHelper->Query(uri, predicates, columns);
586 ASSERT_NE(resultSet, nullptr);
587 resultSet->GetRowCount(result);
588 EXPECT_EQ(result, 1);
589 resultSet->Close();
590 }
591
592 // over threshold, query success
593 for (int i = 0; i < 10; i++) {
594 auto resultSet = g_exHelper->Query(uri, predicates, columns);
595 ASSERT_NE(resultSet, nullptr);
596 resultSet->GetRowCount(result);
597 EXPECT_EQ(result, 1);
598 resultSet->Close();
599 }
600 LOG_INFO("Query_Threshold_Test002::End");
601 }
602
603 /**
604 * @tc.name: Delete_Threshold_Test002
605 * @tc.desc: Verify non-silent access Delete operation behavior when exceeding the threshold
606 * @tc.type: FUNC
607 * @tc.require: issueIC8OCN
608 * @tc.precon: None
609 * @tc.step:
610 1. Define delete predicates targeting non-existent data
611 2. Perform 2999 Delete operations (not exceeding threshold) and verify results
612 3. Perform 10 more Delete operations (exceeding threshold) and check results
613 * @tc.expect:
614 1. All Delete operations before and after threshold return 0 (no data deleted)
615 2. No failure occurs when exceeding threshold for non-silent access
616 */
617 HWTEST_F(DataShareThresholdTest, Delete_Threshold_Test002, TestSize.Level1)
618 {
619 LOG_INFO("Delete_Threshold_Test002::Start");
620
621 Uri uri(DATA_SHARE_URI);
622
623 DataShare::DataSharePredicates deletePredicates;
624 std::string selections = TBL_STU_NAME + " = 'lisan'";
625 deletePredicates.SetWhereClause(selections);
626
627 // not over threshold, delete data not exists return 0
628 for (int i = 0; i < 2999; i++) {
629 int retVal = g_exHelper->Delete(uri, deletePredicates);
630 EXPECT_EQ(retVal, 0);
631 }
632
633 // over threshold, delete data not exists return 0
634 for (int i = 0; i < 10; i++) {
635 int retVal = g_exHelper->Delete(uri, deletePredicates);
636 EXPECT_EQ(retVal, 0);
637 }
638 LOG_INFO("Delete_Threshold_Test002::End");
639 }
640
641 /**
642 * @tc.name: InsertEx_Threshold_Test002
643 * @tc.desc: Verify non-silent access InsertEx operation behavior when exceeding the threshold
644 * @tc.type: FUNC
645 * @tc.require: issueIC8OCN
646 * @tc.precon: None
647 * @tc.step:
648 1. Prepare test data (name and age) in a DataShareValuesBucket
649 2. Perform 2999 InsertEx operations (not exceeding threshold) and verify success
650 3. Perform 10 more InsertEx operations (exceeding threshold) and check results
651 * @tc.expect:
652 1. All InsertEx operations before and after threshold return (0, positive value) indicating success
653 2. No failure occurs when exceeding threshold for non-silent access
654 */
655 HWTEST_F(DataShareThresholdTest, InsertEx_Threshold_Test002, TestSize.Level1)
656 {
657 LOG_INFO("InsertEx_Threshold_Test002::Start");
658
659 Uri uri(DATA_SHARE_URI);
660 DataShare::DataShareValuesBucket valuesBucket;
661 std::string value = "lisi";
662 valuesBucket.Put(TBL_STU_NAME, value);
663 int age = 25;
664 valuesBucket.Put(TBL_STU_AGE, age);
665 // not over threshold, insertEx success
666 for (int i = 0; i < 2999; i++) {
667 auto [errCode, retVal] = g_exHelper->InsertEx(uri, valuesBucket);
668 EXPECT_EQ(errCode, 0);
669 EXPECT_EQ((retVal > 0), true);
670 }
671
672 // over threshold, insertEx success
673 for (int i = 0; i < 10; i++) {
674 auto [errCode, retVal] = g_exHelper->InsertEx(uri, valuesBucket);
675 EXPECT_EQ(errCode, 0);
676 EXPECT_EQ((retVal > 0), true);
677 }
678 LOG_INFO("InsertEx_Threshold_Test002::End");
679 }
680
681 /**
682 * @tc.name: UpdateEx_Threshold_Test002
683 * @tc.desc: Verify non-silent access UpdateEx operation behavior when exceeding the threshold
684 * @tc.type: FUNC
685 * @tc.require: issueIC8OCN
686 * @tc.precon: None
687 * @tc.step:
688 1. Define update predicates to target specific data
689 2. Prepare update data in a DataShareValuesBucket
690 3. Perform 2999 UpdateEx operations (not exceeding threshold) and verify success
691 4. Perform 10 more UpdateEx operations (exceeding threshold) and check results
692 * @tc.expect:
693 1. All UpdateEx operations before and after threshold return (0, 1) indicating success
694 2. No failure occurs when exceeding threshold for non-silent access
695 */
696 HWTEST_F(DataShareThresholdTest, UpdateEx_Threshold_Test002, TestSize.Level1)
697 {
698 LOG_INFO("UpdateEx_Threshold_Test002::Start");
699
700 Uri uri(DATA_SHARE_URI);
701
702 DataShare::DataSharePredicates predicates;
703 std::string selections = TBL_STU_NAME + " = 'zhangsan'";
704 predicates.SetWhereClause(selections);
705 DataShare::DataShareValuesBucket valuesBucket;
706 valuesBucket.Put(TBL_STU_AGE, 10);
707 // not over threshold, updateEx sccuess
708 for (int i = 0; i < 2999; i++) {
709 auto [errCode, retVal] = g_exHelper->UpdateEx(uri, predicates, valuesBucket);
710 EXPECT_EQ(errCode, 0);
711 EXPECT_EQ(retVal, 1);
712 }
713
714 // over threshold, updateEx ret success
715 for (int i = 0; i < 10; i++) {
716 auto [errCode, retVal] = g_exHelper->UpdateEx(uri, predicates, valuesBucket);
717 EXPECT_EQ(errCode, 0);
718 EXPECT_EQ(retVal, 1);
719 }
720 LOG_INFO("UpdateEx_Threshold_Test002::End");
721 }
722
723 /**
724 * @tc.name: DeleteEx_Threshold_Test002
725 * @tc.desc: Verify non-silent access DeleteEx operation behavior when exceeding the threshold
726 * @tc.type: FUNC
727 * @tc.require: issueIC8OCN
728 * @tc.precon: None
729 * @tc.step:
730 1. Define delete predicates targeting non-existent data
731 2. Perform 2999 DeleteEx operations (not exceeding threshold) and verify results
732 3. Perform 10 more DeleteEx operations (exceeding threshold) and check results
733 * @tc.expect:
734 1. All DeleteEx operations before and after threshold return (0, 0) indicating no data deleted
735 2. No failure occurs when exceeding threshold for non-silent access
736 */
737 HWTEST_F(DataShareThresholdTest, DeleteEx_Threshold_Test002, TestSize.Level1)
738 {
739 LOG_INFO("DeleteEx_Threshold_Test002::Start");
740
741 Uri uri(DATA_SHARE_URI);
742
743 DataShare::DataSharePredicates deletePredicates;
744 std::string selections = TBL_STU_NAME + " = 'lisan'";
745 deletePredicates.SetWhereClause(selections);
746 // not over threshold, deleteEx data not exists return pair(0, 0)
747 for (int i = 0; i < 2999; i++) {
748 auto [errCode, retVal] = g_exHelper->DeleteEx(uri, deletePredicates);
749 EXPECT_EQ(errCode, 0);
750 EXPECT_EQ(retVal, 0);
751 }
752
753 // over threshold, deleteEx data not exists return pair(0, 0)
754 for (int i = 0; i < 10; i++) {
755 auto [errCode, retVal] = g_exHelper->DeleteEx(uri, deletePredicates);
756 EXPECT_EQ(errCode, 0);
757 EXPECT_EQ(retVal, 0);
758 }
759 LOG_INFO("DeleteEx_Threshold_Test002::End");
760 }
761 }
762 }