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 #include <memory>
18 #include "accesstoken_kit.h"
19 #include "ams_mgr_proxy.h"
20 #include "data_share_manager_impl.h"
21 #include "data_share_service_proxy.h"
22 #include "datashare_helper.h"
23 #include "datashare_log.h"
24 #include "datashare_template.h"
25 #include "hap_token_info.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "token_setproc.h"
29 #include "datashare_errno.h"
30 #include "published_data_subscriber_manager.h"
31 #include "rdb_subscriber_manager.h"
32 #include "ishared_result_set_stub.h"
33 #include "message_parcel.h"
34 #include "ikvstore_data_service.h"
35 #include "shared_block.h"
36 #include "datashare_block_writer_impl.h"
37 #include "datashare_connection.h"
38 #include "ikvstore_data_service_mock.h"
39 #include "general_controller_service_impl.h"
40 #include "persistent_data_controller.h"
41 #include "published_data_controller.h"
42
43 namespace OHOS {
44 namespace DataShare {
45 using namespace testing::ext;
46 using namespace OHOS::Security::AccessToken;
47 constexpr int INVALID_VALUE = -1;
48
49 class AbnormalBranchTest : public testing::Test {
50 public:
51 static void SetUpTestCase(void);
52 static void TearDownTestCase(void);
53 void SetUp();
54 void TearDown();
55 };
56
SetUpTestCase(void)57 void AbnormalBranchTest::SetUpTestCase(void)
58 {
59 }
TearDownTestCase(void)60 void AbnormalBranchTest::TearDownTestCase(void)
61 {
62 }
SetUp(void)63 void AbnormalBranchTest::SetUp(void)
64 {
65 }
TearDown(void)66 void AbnormalBranchTest::TearDown(void)
67 {
68 }
69
70 // Used for mock DataShareKvServiceProxy in order to
71 // cover the situation that DataShareManagerImpl::GetServiceProxy() == nullptr;
DataShareManagerImplHelper()72 void DataShareManagerImplHelper()
73 {
74 auto helper = DataShareManagerImpl::GetInstance();
75 helper->dataShareService_ = nullptr;
76 auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77 auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
78 sptr<MockDataShareKvServiceProxy> mockProxy = sptr<MockDataShareKvServiceProxy>
79 (new MockDataShareKvServiceProxy(remoteObject));
80 EXPECT_CALL(*mockProxy, GetFeatureInterface(testing::_))
81 .WillOnce(testing::Return(nullptr));
82
83 helper->dataMgrService_ = (sptr<DataShareKvServiceProxy>)mockProxy;
84 }
85
86 /**
87 * @tc.name: AbnormalBranchTest_shareBlock_Null_Test_001
88 * @tc.desc: Verify operations on DataShareBlockWriterImpl when share block is null
89 * @tc.type: FUNC
90 * @tc.precon: None
91 * @tc.step:
92 1. Create DataShareBlockWriterImpl instance
93 2. Call various write operations (AllocRow, Write with different types)
94 3. Check return values of all operations
95 * @tc.expect:
96 1. All operations return E_ERROR
97 */
98 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_shareBlock_Null_Test_001, TestSize.Level0)
99 {
100 LOG_INFO("AbnormalBranchTest_shareBlock_Null_Test_001::Start");
101 DataShareBlockWriterImpl impl;
102 int result = impl.AllocRow();
103 EXPECT_EQ(result, E_ERROR);
104 result = impl.Write(1);
105 EXPECT_EQ(result, E_ERROR);
106 int64_t intValue = 0;
107 result = impl.Write(1, intValue);
108 EXPECT_EQ(result, E_ERROR);
109 double doubleValue = 0.0;
110 result = impl.Write(1, doubleValue);
111 EXPECT_EQ(result, E_ERROR);
112 uint8_t *unitValue = nullptr;
113 result = impl.Write(1, unitValue, 0);
114 EXPECT_EQ(result, E_ERROR);
115 char *charValue = nullptr;
116 result = impl.Write(1, charValue, 0);
117 EXPECT_EQ(result, E_ERROR);
118 LOG_INFO("AbnormalBranchTest_shareBlock_Null_Test_001::End");
119 }
120
121 /**
122 * @tc.name: AbnormalBranchTest_ResultSetStubNull_Test_001
123 * @tc.desc: Verify ISharedResultSetStub behavior when input parameters are null
124 * @tc.type: FUNC
125 * @tc.precon: None
126 * @tc.step:
127 1. Create ISharedResultSetStub instance with null parameter
128 2. Call CreateStub method with null result and valid parcel
129 3. Check returned ISharedResultSet pointer
130 * @tc.expect:
131 1. CreateStub returns nullptr
132 */
133 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_ResultSetStubNull_Test_001, TestSize.Level0)
134 {
135 LOG_INFO("AbnormalBranchTest_ResultSetStubNull_Test_001::Start");
136 ISharedResultSetStub stub(nullptr);
137 std::shared_ptr<DataShareResultSet> result = nullptr;
138 OHOS::MessageParcel parcel;
139 sptr<ISharedResultSet> resultSet = stub.CreateStub(result, parcel);
140 EXPECT_EQ(resultSet, nullptr);
141 LOG_INFO("AbnormalBranchTest_ResultSetStubNull_Test_001::End");
142 }
143
144 /**
145 * @tc.name: AbnormalBranchTest_RegisterClientDeathObserverNull_Test_001
146 * @tc.desc: Verify RegisterClientDeathObserver with null observer
147 * @tc.type: FUNC
148 * @tc.precon: None
149 * @tc.step:
150 1. Create DataShareKvServiceProxy with null parameter
151 2. Call RegisterClientDeathObserver with empty appId and null observer
152 3. Check return value
153 * @tc.expect:
154 1. Method returns -1 (failure)
155 */
156 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_RegisterClientDeathObserverNull_Test_001, TestSize.Level0)
157 {
158 LOG_INFO("AbnormalBranchTest_RegisterClientDeathObserverNull_Test_001::Start");
159 DataShareKvServiceProxy proxy(nullptr);
160 std::string appId;
161 uint32_t result = proxy.RegisterClientDeathObserver(appId, nullptr);
162 EXPECT_EQ(result, -1);
163 LOG_INFO("AbnormalBranchTest_RegisterClientDeathObserverNull_Test_001::End");
164 }
165
166 /**
167 * @tc.name: AbnormalBranchTest_mReadOnlyInvalid_Test_001
168 * @tc.desc: Verify invalid operations on read-only SharedBlock
169 * @tc.type: FUNC
170 * @tc.precon: None
171 * @tc.step:
172 1. Create read-only SharedBlock instance
173 2. Attempt modification operations (Clear, SetColumnNum, AllocRow, etc.)
174 3. Check return values of all operations
175 * @tc.expect:
176 1. All modification operations return SHARED_BLOCK_INVALID_OPERATION
177 2. Init operation succeeds
178 */
179 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_mReadOnlyInvalid_Test_001, TestSize.Level0)
180 {
181 LOG_INFO("AbnormalBranchTest_mReadOnlyInvalid_Test_001::Start");
182 std::string name = "Test Shared\0";
183 bool readOnly = true;
184 int32_t size = 1024;
185 sptr<Ashmem> ashmem = Ashmem::CreateAshmem(name.c_str(), size);
186 ashmem->MapReadAndWriteAshmem();
187 AppDataFwk::SharedBlock temp(name, ashmem, size, readOnly);
188 int result = temp.Init();
189 EXPECT_TRUE(result);
190 result = temp.Clear();
191 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
192 result = temp.SetColumnNum(1);
193 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
194 result = temp.AllocRow();
195 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
196 result = temp.FreeLastRow();
197 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
198 int64_t intValue = 0;
199 result = temp.PutLong(1, 1, intValue);
200 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
201 double doubleValue = 0.0;
202 result = temp.PutDouble(1, 1, doubleValue);
203 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
204 result = temp.PutNull(1, 1);
205 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
206 result = temp.SetRawData(nullptr, 0);
207 EXPECT_EQ(result, AppDataFwk::SharedBlock::SHARED_BLOCK_INVALID_OPERATION);
208 LOG_INFO("AbnormalBranchTest_mReadOnlyInvalid_Test_001::End");
209 }
210
211 /**
212 * @tc.name: AbnormalBranchTest_CreatorPossibleNull_Test_002
213 * @tc.desc: Verify DataShareHelper::Creator with invalid parameters
214 * @tc.type: FUNC
215 * @tc.precon: None
216 * @tc.step:
217 1. Prepare CreateOptions with null token
218 2. Call DataShareHelper::Creator with empty URI and prepared options
219 3. Check returned DataShareHelper pointer
220 * @tc.expect:
221 1. Creator returns nullptr (failure to create helper)
222 */
223 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_CreatorPossibleNull_Test_002, TestSize.Level0)
224 {
225 LOG_INFO("AbnormalBranchTest_CreatorPossibleNull_Test_002::Start");
226 std::string strUri;
227 CreateOptions options;
228 options.token_ = nullptr;
229 options.isProxy_ = false;
230 std::string bundleName;
231 std::shared_ptr<DataShareHelper> dataHelper = DataShare::DataShareHelper::Creator(strUri, options, bundleName);
232 EXPECT_EQ(dataHelper, nullptr);
233 LOG_INFO("AbnormalBranchTest_CreatorPossibleNull_Test_002::End");
234 }
235
236 /**
237 * @tc.name: AbnormalBranchTest_AddObserversProxyNull_Test_001
238 * @tc.desc: Verify PublishedDataSubscriberManager::AddObservers with null proxy
239 * @tc.type: FUNC
240 * @tc.precon: None
241 * @tc.step:
242 1. Prepare null proxy and empty URIs list
243 2. Call AddObservers with null subscriber, proxy and empty URIs
244 3. Check size of returned results
245 * @tc.expect:
246 1. Results size equals URIs size (0 in this case)
247 */
248 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_AddObserversProxyNull_Test_001, TestSize.Level0)
249 {
250 LOG_INFO("AbnormalBranchTest_AddObserversProxyNull_Test_001::Start");
251 void *subscriber = nullptr;
252 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
253 const std::vector<std::string> uris = {};
254 int64_t subscriberId = 0;
__anon5fd91aad0102(const PublishedDataChangeNode &changeNode)255 const PublishedDataCallback callback = [](const PublishedDataChangeNode &changeNode){};
256 std::vector<OperationResult> results = PublishedDataSubscriberManager::GetInstance().AddObservers(subscriber,
257 proxy, uris, subscriberId, callback);
258 EXPECT_EQ(results.size(), uris.size());
259 LOG_INFO("AbnormalBranchTest_AddObserversProxyNull_Test_001::End");
260 }
261
262 /**
263 * @tc.name: AbnormalBranchTest_AddObserversProxyNull_Test_002
264 * @tc.desc: Verify RdbSubscriberManager::AddObservers with null proxy
265 * @tc.type: FUNC
266 * @tc.precon: None
267 * @tc.step:
268 1. Prepare null proxy and empty URIs list
269 2. Call AddObservers with null subscriber, proxy and empty URIs
270 3. Check size of returned results
271 * @tc.expect:
272 1. Results size equals URIs size (0 in this case)
273 */
274 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_AddObserversProxyNull_Test_002, TestSize.Level0)
275 {
276 LOG_INFO("AbnormalBranchTest_AddObserversProxyNull_Test_002::Start");
277 void *subscriber = nullptr;
278 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
279 const std::vector<std::string> uris = {};
280 TemplateId templateId;
__anon5fd91aad0202(const RdbChangeNode &changeNode)281 const RdbCallback callback = [](const RdbChangeNode &changeNode){};
282 std::vector<OperationResult> results = RdbSubscriberManager::GetInstance().AddObservers(subscriber, proxy, uris,
283 templateId, callback);
284 EXPECT_EQ(results.size(), uris.size());
285 LOG_INFO("AbnormalBranchTest_AddObserversProxyNull_Test_002::End");
286 }
287
288 /**
289 * @tc.name: AbnormalBranchTest_DelObserversProxyNull_Test_001
290 * @tc.desc: Verify PublishedDataSubscriberManager::DelObservers with null proxy
291 * @tc.type: FUNC
292 * @tc.precon: None
293 * @tc.step:
294 1. Prepare null proxy and empty URIs list
295 2. Call DelObservers with null subscriber and proxy (both overloads)
296 3. Check size of returned results
297 * @tc.expect:
298 1. Results size equals URIs size (0 in this case) for both overloads
299 */
300 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_DelObserversProxyNull_Test_001, TestSize.Level0)
301 {
302 LOG_INFO("AbnormalBranchTest_DelObserversProxyNull_Test_001::Start");
303 void *subscriber = nullptr;
304 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
305 const std::vector<std::string> uris = {};
306 int64_t subscriberId = 0;
307 std::vector<OperationResult> results = PublishedDataSubscriberManager::GetInstance().DelObservers(subscriber,
308 proxy);
309 EXPECT_EQ(results.size(), uris.size());
310 results = PublishedDataSubscriberManager::GetInstance().DelObservers(subscriber, proxy, uris, subscriberId);
311 EXPECT_EQ(results.size(), uris.size());
312 LOG_INFO("AbnormalBranchTest_DelObserversProxyNull_Test_001::End");
313 }
314
315 /**
316 * @tc.name: AbnormalBranchTest_DelObserversProxyNull_Test_002
317 * @tc.desc: Verify RdbSubscriberManager::DelObservers with null proxy
318 * @tc.type: FUNC
319 * @tc.precon: None
320 * @tc.step:
321 1. Prepare null proxy and empty URIs list
322 2. Call DelObservers with null subscriber and proxy (both overloads)
323 3. Check size of returned results
324 * @tc.expect:
325 1. Results size equals URIs size (0 in this case) for both overloads
326 */
327 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_DelObserversProxyNull_Test_002, TestSize.Level0)
328 {
329 LOG_INFO("AbnormalBranchTest_DelObserversProxyNull_Test_002::Start");
330 void *subscriber = nullptr;
331 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
332 const std::vector<std::string> uris = {};
333 TemplateId templateId;
334 std::vector<OperationResult> results = RdbSubscriberManager::GetInstance().DelObservers(subscriber, proxy, uris,
335 templateId);
336 EXPECT_EQ(results.size(), uris.size());
337 results = RdbSubscriberManager::GetInstance().DelObservers(subscriber, proxy);
338 EXPECT_EQ(results.size(), uris.size());
339 LOG_INFO("AbnormalBranchTest_DelObserversProxyNull_Test_002::End");
340 }
341
342 /**
343 * @tc.name: AbnormalBranchTest_EnableObserversProxyNull_Test_001
344 * @tc.desc: Verify PublishedDataSubscriberManager::EnableObservers with null proxy
345 * @tc.type: FUNC
346 * @tc.precon: None
347 * @tc.step:
348 1. Prepare null proxy and empty URIs list
349 2. Call EnableObservers with null subscriber, proxy and empty URIs
350 3. Check size of returned results
351 * @tc.expect:
352 1. Results size equals URIs size (0 in this case)
353 */
354 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_EnableObserversProxyNull_Test_001, TestSize.Level0)
355 {
356 LOG_INFO("AbnormalBranchTest_EnableObserversProxyNull_Test_001::Start");
357 void *subscriber = nullptr;
358 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
359 const std::vector<std::string> uris = {};
360 int64_t subscriberId = 0;
361 std::vector<OperationResult> results = PublishedDataSubscriberManager::GetInstance().EnableObservers(subscriber,
362 proxy, uris, subscriberId);
363 EXPECT_EQ(results.size(), uris.size());
364 LOG_INFO("AbnormalBranchTest_EnableObserversProxyNull_Test_001::End");
365 }
366
367 /**
368 * @tc.name: AbnormalBranchTest_EnableObserversProxyNull_Test_002
369 * @tc.desc: Verify RdbSubscriberManager::EnableObservers with null proxy
370 * @tc.type: FUNC
371 * @tc.precon: None
372 * @tc.step:
373 1. Prepare null proxy and empty URIs list
374 2. Call EnableObservers with null subscriber, proxy and empty URIs
375 3. Check size of returned results
376 * @tc.expect:
377 1. Results size equals URIs size (0 in this case)
378 */
379 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_EnableObserversProxyNull_Test_002, TestSize.Level0)
380 {
381 LOG_INFO("AbnormalBranchTest_EnableObserversProxyNull_Test_002::Start");
382 void *subscriber = nullptr;
383 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
384 const std::vector<std::string> uris = {};
385 TemplateId templateId;
386 std::vector<OperationResult> results = RdbSubscriberManager::GetInstance().EnableObservers(subscriber, proxy,
387 uris, templateId);
388 EXPECT_EQ(results.size(), uris.size());
389 LOG_INFO("AbnormalBranchTest_EnableObserversProxyNull_Test_002::End");
390 }
391
392 /**
393 * @tc.name: AbnormalBranchTest_DisableObserversProxyNull_Test_001
394 * @tc.desc: Verify PublishedDataSubscriberManager::DisableObservers with null proxy
395 * @tc.type: FUNC
396 * @tc.precon: None
397 * @tc.step:
398 1. Prepare null proxy and empty URIs list
399 2. Call DisableObservers with null subscriber, proxy and empty URIs
400 3. Check size of returned results
401 * @tc.expect:
402 1. Results size equals URIs size (0 in this case)
403 */
404 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_DisableObserversProxyNull_Test_001, TestSize.Level0)
405 {
406 LOG_INFO("AbnormalBranchTest_DisableObserversProxyNull_Test_001::Start");
407 void *subscriber = nullptr;
408 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
409 const std::vector<std::string> uris = {};
410 int64_t subscriberId = 0;
411 std::vector<OperationResult> results = PublishedDataSubscriberManager::GetInstance().DisableObservers(subscriber,
412 proxy, uris, subscriberId);
413 EXPECT_EQ(results.size(), uris.size());
414 LOG_INFO("AbnormalBranchTest_DisableObserversProxyNull_Test_001::End");
415 }
416
417 /**
418 * @tc.name: AbnormalBranchTest_DisableObserversProxyNull_Test_002
419 * @tc.desc: Verify RdbSubscriberManager::DisableObservers with null proxy
420 * @tc.type: FUNC
421 * @tc.precon: None
422 * @tc.step:
423 1. Prepare null proxy and empty URIs list
424 2. Call DisableObservers with null subscriber, proxy and empty URIs
425 3. Check size of returned results
426 * @tc.expect:
427 1. Results size equals URIs size (0 in this case)
428 */
429 HWTEST_F(AbnormalBranchTest, AbnormalBranchTest_DisableObserversProxyNull_Test_002, TestSize.Level0)
430 {
431 LOG_INFO("AbnormalBranchTest_DisableObserversProxyNull_Test_002::Start");
432 void *subscriber = nullptr;
433 std::shared_ptr<DataShareServiceProxy> proxy = nullptr;
434 const std::vector<std::string> uris = {};
435 TemplateId templateId;
436 std::vector<OperationResult> results = RdbSubscriberManager::GetInstance().DisableObservers(subscriber, proxy,
437 uris, templateId);
438 EXPECT_EQ(results.size(), uris.size());
439 LOG_INFO("AbnormalBranchTest_DisableObserversProxyNull_Test_002::End");
440 }
441
442 /**
443 * @tc.name: PublishDelObserversTest001
444 * @tc.desc: Verify PublishedDataSubscriberManager observer operations with null proxy
445 * @tc.type: FUNC
446 * @tc.precon: None
447 * @tc.step:
448 1. Prepare null subscriber and valid proxy
449 2. Call DisableObservers with null proxy and valid URIs
450 3. Call DelObservers with valid proxy and URIs
451 4. Call RecoverObservers with null proxy
452 5. Check results sizes
453 * @tc.expect:
454 1. DisableObservers with null proxy returns 0 results
455 2. DelObservers with valid proxy returns results matching URIs size
456 */
457 HWTEST_F(AbnormalBranchTest, PublishDelObserversTest001, TestSize.Level0)
458 {
459 LOG_INFO("PublishDelObserversTest001::Start");
460 void *subscriber = nullptr;
461 auto proxy = DataShareManagerImpl::GetServiceProxy();
462 std::vector<std::string> uris = {};
463 std::string uri = "datashare:///com.test/db0/tbl0";
464 uris.push_back(uri);
465 int64_t subscriberId = 0;
466 std::vector<OperationResult> results = PublishedDataSubscriberManager::GetInstance().DisableObservers(subscriber,
467 nullptr, uris, subscriberId);
468 EXPECT_EQ(results.size(), 0);
469 results = PublishedDataSubscriberManager::GetInstance().DelObservers(subscriber,
470 proxy, uris, subscriberId);
471 EXPECT_EQ(results.size(), uris.size());
472 proxy = nullptr;
473 PublishedDataSubscriberManager::GetInstance().RecoverObservers(proxy);
474 LOG_INFO("PublishDelObserversTest001::End");
475 }
476
477 /**
478 * @tc.name: PublishedDataSubscriberManagerOperatorTest001
479 * @tc.desc: Verify PublishedDataObserver comparison operators
480 * @tc.type: FUNC
481 * @tc.precon: None
482 * @tc.step:
483 1. Create two PublishedDataObserver instances with identical callbacks
484 2. Compare them using != and == operators
485 3. Check comparison results
486 * @tc.expect:
487 1. != operator returns true (observers are distinct)
488 2. == operator returns false (observers are distinct)
489 */
490 HWTEST_F(AbnormalBranchTest, PublishedDataSubscriberManagerOperatorTest001, TestSize.Level0)
491 {
492 LOG_INFO("PublishedDataSubscriberManagerOperatorTest001::Start");
__anon5fd91aad0302(const PublishedDataChangeNode &changeNode)493 const PublishedDataCallback callback = [](const PublishedDataChangeNode &changeNode){};
494 PublishedDataObserver ob(callback);
495 PublishedDataObserver obCompare(callback);
496 EXPECT_TRUE(ob != obCompare);
497 EXPECT_FALSE(ob == obCompare);
498 LOG_INFO("PublishedDataSubscriberManagerOperatorTest001::End");
499 }
500
501 /**
502 * @tc.name: RdbDelObserversTest001
503 * @tc.desc: Verify RdbSubscriberManager::DelObservers with valid and null proxy
504 * @tc.type: FUNC
505 * @tc.precon: None
506 * @tc.step:
507 1. Prepare null subscriber and valid proxy
508 2. Call DelObservers with valid proxy and URIs
509 3. Call RecoverObservers with null proxy
510 4. Check results size
511 * @tc.expect:
512 1. DelObservers returns results matching URIs size
513 */
514 HWTEST_F(AbnormalBranchTest, RdbDelObserversTest001, TestSize.Level0)
515 {
516 LOG_INFO("RdbDelObserversTest001::Start");
517 void *subscriber = nullptr;
518 auto proxy = DataShareManagerImpl::GetServiceProxy();
519 std::vector<std::string> uris = {};
520 std::string uri = "datashare:///com.test/db0/tbl0";
521 uris.push_back(uri);
522 TemplateId templateId;
523 std::vector<OperationResult> results = RdbSubscriberManager::GetInstance().DelObservers(subscriber,
524 proxy, uris, templateId);
525 EXPECT_EQ(results.size(), uris.size());
526 proxy = nullptr;
527 PublishedDataSubscriberManager::GetInstance().RecoverObservers(proxy);
528 LOG_INFO("RdbDelObserversTest001::End");
529 }
530
531 /**
532 * @tc.name: RdbDisableObserversTest001
533 * @tc.desc: Verify RdbSubscriberManager::DisableObservers with valid and null proxy
534 * @tc.type: FUNC
535 * @tc.precon: None
536 * @tc.step:
537 1. Prepare null subscriber and valid proxy
538 2. Call DisableObservers with null proxy and valid URIs
539 3. Call DisableObservers with valid proxy and URIs
540 4. Call RecoverObservers with null proxy
541 5. Check results sizes
542 * @tc.expect:
543 1. DisableObservers with null proxy returns 0 results
544 2. DisableObservers with valid proxy returns results matching URIs size
545 */
546 HWTEST_F(AbnormalBranchTest, RdbDisableObserversTest001, TestSize.Level0)
547 {
548 LOG_INFO("RdbDisableObserversTest001::Start");
549 void *subscriber = nullptr;
550 auto proxy = DataShareManagerImpl::GetServiceProxy();
551 std::vector<std::string> uris = {};
552 std::string uri = "datashare:///com.test/db0/tbl0";
553 uris.push_back(uri);
554 TemplateId templateId;
555 std::vector<OperationResult> results = RdbSubscriberManager::GetInstance().DisableObservers(subscriber,
556 nullptr, uris, templateId);
557 EXPECT_EQ(results.size(), 0);
558 results = RdbSubscriberManager::GetInstance().DisableObservers(subscriber,
559 proxy, uris, templateId);
560 EXPECT_EQ(results.size(), uris.size());
561 proxy = nullptr;
562 RdbSubscriberManager::GetInstance().RecoverObservers(proxy);
563 LOG_INFO("RdbDisableObserversTest001::End");
564 }
565
566 /**
567 * @tc.name: RdbSubscriberManagerOperatorTest001
568 * @tc.desc: Verify RdbObserver comparison operators
569 * @tc.type: FUNC
570 * @tc.precon: None
571 * @tc.step:
572 1. Create two RdbObserver instances with identical callbacks
573 2. Compare them using != and == operators
574 3. Check comparison results
575 * @tc.expect:
576 1. != operator returns true (observers are distinct)
577 2. == operator returns false (observers are distinct)
578 */
579 HWTEST_F(AbnormalBranchTest, RdbSubscriberManagerOperatorTest001, TestSize.Level0)
580 {
581 LOG_INFO("RdbSubscriberManagerOperatorTest001::Start");
__anon5fd91aad0402(const RdbChangeNode &changeNode)582 const RdbCallback callback = [](const RdbChangeNode &changeNode){};
583 RdbObserver ob(callback);
584 RdbObserver obCompare(callback);
585 EXPECT_TRUE(ob != obCompare);
586 EXPECT_FALSE(ob == obCompare);
587 LOG_INFO("RdbSubscriberManagerOperatorTest001::End");
588 }
589
590 /**
591 * @tc.name: RegisterClientDeathObserverTest001
592 * @tc.desc: Verify DataShareManagerImpl::RegisterClientDeathObserver under various conditions
593 * @tc.type: FUNC
594 * @tc.precon: None
595 * @tc.step:
596 1. Create DataShareManagerImpl instance with null service and empty bundle name
597 2. Call RegisterClientDeathObserver
598 3. Set valid service proxy and call again
599 4. Set valid bundle name and call again
600 5. Check clientDeathObserverPtr_ state
601 * @tc.expect:
602 1. Observer remains null with empty bundle name
603 2. Observer is created when valid bundle name is provided
604 */
605 HWTEST_F(AbnormalBranchTest, RegisterClientDeathObserverTest001, TestSize.Level0)
606 {
607 LOG_INFO("RegisterClientDeathObserverTest001::Start");
608 auto datashareManager = new DataShareManagerImpl();
609 datashareManager->dataMgrService_ = nullptr;
610 datashareManager->bundleName_ = "";
611 datashareManager->clientDeathObserverPtr_ = nullptr;
612 datashareManager->RegisterClientDeathObserver();
613 datashareManager->dataMgrService_ = datashareManager->GetDistributedDataManager();
614 EXPECT_NE(datashareManager->dataMgrService_, nullptr);
615 datashareManager->RegisterClientDeathObserver();
616 EXPECT_EQ(datashareManager->clientDeathObserverPtr_, nullptr);
617 datashareManager->bundleName_ = "com.testbundlename";
618 datashareManager->RegisterClientDeathObserver();
619 EXPECT_NE(datashareManager->clientDeathObserverPtr_, nullptr);
620 datashareManager->RegisterClientDeathObserver();
621 LOG_INFO("RegisterClientDeathObserverTest001::End");
622 }
623
624 /**
625 * @tc.name: RegisterClientDeathObserverTest002
626 * @tc.desc: Check the main process of RegisterClientDeathObserver
627 * @tc.type: FUNC
628 * @tc.precon: None
629 * @tc.expect: Successfully register client death observer
630 * @tc.step: 1. Create a DataShareManagerImpl instance;
631 2. Try to use the instance to get datashare service proxy;
632 3. Check whether all member functions of the object are successfully constructed.
633 * @tc.require: issueIBX9HL
634 */
635 HWTEST_F(AbnormalBranchTest, RegisterClientDeathObserverTest002, TestSize.Level0)
636 {
637 LOG_INFO("RegisterClientDeathObserverTest002::Start");
638 auto datashareManager = DataShareManagerImpl::GetInstance();
639 datashareManager->SetBundleName("com.testbundlename");
640 datashareManager->GetDataShareServiceProxy();
641 EXPECT_NE(datashareManager->dataMgrService_, nullptr);
642 EXPECT_NE(datashareManager->bundleName_, "");
643 EXPECT_NE(datashareManager->clientDeathObserverPtr_, nullptr);
644 LOG_INFO("RegisterClientDeathObserverTest002::End");
645 }
646
647 /**
648 * @tc.name: OnRemoteDiedTest001
649 * @tc.desc: Check the main process of OnRemoteDied and the reset process ResetServiceHandle
650 * @tc.type: FUNC
651 * @tc.precon: None
652 * @tc.expect: Successfully process OnRemoteDied
653 * @tc.step: 1. Create a DataShareManagerImpl instance;
654 2. Call the OnRemoteDied() function;
655 3. Check whether datashareManager is successfully deconstructed.
656 * @tc.require: issueIBX9HL
657 */
658 HWTEST_F(AbnormalBranchTest, OnRemoteDiedTest001, TestSize.Level0)
659 {
660 LOG_INFO("OnRemoteDiedTest001::Start");
661 auto datashareManager = DataShareManagerImpl::GetInstance();
662 datashareManager->SetBundleName("com.testbundlename");
663 datashareManager->GetDataShareServiceProxy();
664 EXPECT_NE(datashareManager->dataMgrService_, nullptr);
665 EXPECT_NE(datashareManager->bundleName_, "");
666 EXPECT_NE(datashareManager->clientDeathObserverPtr_, nullptr);
667
668 datashareManager->OnRemoteDied();
669 EXPECT_EQ(datashareManager->dataMgrService_, nullptr);
670 EXPECT_EQ(datashareManager->dataShareService_, nullptr);
671 LOG_INFO("OnRemoteDiedTest001::End");
672 }
673
674 /**
675 * @tc.name: SetRegisterCallbackTest001
676 * @tc.desc: Check the main process of SetRegisterCallback
677 * @tc.type: FUNC
678 * @tc.precon: None
679 * @tc.expect: Successfully process SetRegisterCallback
680 * @tc.step: 1. Create a DataShareManagerImpl instance;
681 2. Call the SetRegisterCallback() function;
682 3. Check whether datashareManager is successfully deconstructed.
683 * @tc.require: issueIBX9HL
684 */
685 HWTEST_F(AbnormalBranchTest, SetRegisterCallbackTest001, TestSize.Level0)
686 {
687 LOG_INFO("SetRegisterCallbackTest001::Start");
688 auto datashareManager = DataShareManagerImpl::GetInstance();
__anon5fd91aad0502()689 std::function<void()> callback = [](){};
690 datashareManager->SetRegisterCallback(nullptr, callback);
691 LOG_INFO("SetRegisterCallbackTest001::End");
692 }
693
694 /**
695 * @tc.name: AmsMgrProxyOnProxyDiedTest001
696 * @tc.desc: Verify AmsMgrProxy behavior when proxy dies
697 * @tc.type: FUNC
698 * @tc.precon: None
699 * @tc.step:
700 1. Create AmsMgrProxy with null service and proxy
701 2. Call OnProxyDied and verify state
702 3. Create new proxy, connect to SA, verify initialization
703 4. Call OnProxyDied again and verify cleanup
704 * @tc.expect:
705 1. OnProxyDied handles null pointers gracefully
706 2. Proxy initializes properly after ConnectSA
707 3. OnProxyDied cleans up resources
708 */
709 HWTEST_F(AbnormalBranchTest, AmsMgrProxyOnProxyDiedTest001, TestSize.Level0)
710 {
711 LOG_INFO("AmsMgrProxyOnProxyDiedTest001::Start");
712 AmsMgrProxy* proxy = new AmsMgrProxy();
713 proxy->sa_ = nullptr;
714 proxy->proxy_ = nullptr;
715 proxy->OnProxyDied();
716 delete proxy;
717 proxy = new AmsMgrProxy();
718 proxy->sa_ = nullptr;
719 proxy->proxy_ = nullptr;
720 proxy->ConnectSA();
721 EXPECT_NE(proxy->sa_, nullptr);
722 EXPECT_NE(proxy->proxy_, nullptr);
723 EXPECT_NE(proxy->deathRecipient_, nullptr);
724 proxy->OnProxyDied();
725 delete proxy;
726 LOG_INFO("AmsMgrProxyOnProxyDiedTest001::End");
727 }
728
729 /**
730 * @tc.name: AmsMgrProxyOnProxyDiedTest002
731 * @tc.desc: Test sa_ with nullptr and destructor of AmsMgrProxy
732 * @tc.type: FUNC
733 * @tc.precon: None
734 * @tc.expect: Successfully process SetRegisterCallback
735 * @tc.step: 1. Create a AmsMgrProxy instance;
736 2. Clear the proxy;
737 3. After clear, try to connect SA;
738 4. Check if this proxy can be connected successfully.
739 * @tc.require: issueIBX9HL
740 */
741 HWTEST_F(AbnormalBranchTest, AmsMgrProxyOnProxyDiedTest002, TestSize.Level0)
742 {
743 LOG_INFO("AmsMgrProxyOnProxyDiedTest002::Start");
744 AmsMgrProxy* proxy = AmsMgrProxy::GetInstance();
745 proxy->OnProxyDied();
746 proxy->ConnectSA();
747 EXPECT_NE(proxy->sa_, nullptr);
748 EXPECT_NE(proxy->proxy_, nullptr);
749 EXPECT_NE(proxy->deathRecipient_, nullptr);
750 LOG_INFO("AmsMgrProxyOnProxyDiedTest002::End");
751 }
752
753 /**
754 * @tc.name: DataShareServiceProxySubscribeRdbDataTest001
755 * @tc.desc: Verify DataShareServiceProxy subscription behavior after proxy death
756 * @tc.type: FUNC
757 * @tc.precon: None
758 * @tc.step:
759 1. Create AmsMgrProxy with null service and proxy
760 2. Call OnProxyDied and verify cleanup
761 3. Create new proxy, connect to SA, verify initialization
762 4. Call OnProxyDied again and verify cleanup
763 * @tc.expect:
764 1. Proxy handles null states gracefully
765 2. Proxy initializes properly after ConnectSA
766 3. Resources are cleaned up after OnProxyDied
767 */
768 HWTEST_F(AbnormalBranchTest, DataShareServiceProxySubscribeRdbDataTest001, TestSize.Level0)
769 {
770 LOG_INFO("DataShareServiceProxySubscribeRdbDataTest001::Start");
771 AmsMgrProxy* proxy = new AmsMgrProxy();
772 proxy->sa_ = nullptr;
773 proxy->proxy_ = nullptr;
774 proxy->OnProxyDied();
775 delete proxy;
776 proxy = new AmsMgrProxy();
777 proxy->sa_ = nullptr;
778 proxy->proxy_ = nullptr;
779 proxy->ConnectSA();
780 EXPECT_NE(proxy->sa_, nullptr);
781 EXPECT_NE(proxy->proxy_, nullptr);
782 EXPECT_NE(proxy->deathRecipient_, nullptr);
783 proxy->OnProxyDied();
784 delete proxy;
785 LOG_INFO("DataShareServiceProxySubscribeRdbDataTest001::End");
786 }
787
788 /**
789 * @tc.name: SubscribeRdbDataTest001
790 * @tc.desc: Verify SubscribeRdbData with empty URIs and null observer
791 * @tc.type: FUNC
792 * @tc.precon: None
793 * @tc.step:
794 1. Prepare empty URIs list and null observer
795 2. Call SubscribeRdbData with valid proxy
796 3. Check size of returned result set
797 * @tc.expect:
798 1. Result set size is 0 (matches empty URIs list)
799 */
800 HWTEST_F(AbnormalBranchTest, SubscribeRdbDataTest001, TestSize.Level0)
801 {
802 LOG_INFO("EnableSubscribePublishedDataTest001::Start");
803 std::vector<std::string> uris = {};
804 TemplateId templateId;
805 sptr<IDataProxyRdbObserver> observer = OHOS::sptr<IDataProxyRdbObserver>(nullptr);
806 auto proxy = DataShareManagerImpl::GetServiceProxy();
807 auto resultset = proxy->SubscribeRdbData(uris, templateId, observer);
808 EXPECT_EQ(resultset.size(), 0);
809 LOG_INFO("EnableSubscribePublishedDataTest001::End");
810 }
811
812 /**
813 * @tc.name: SubscribePublishedDataTest001
814 * @tc.desc: Verify SubscribePublishedData with empty URIs and null observer
815 * @tc.type: FUNC
816 * @tc.precon: None
817 * @tc.step:
818 1. Prepare empty URIs list and null observer
819 2. Call SubscribePublishedData with valid proxy
820 3. Check size of returned result set
821 * @tc.expect:
822 1. Result set size is 0 (matches empty URIs list)
823 */
824 HWTEST_F(AbnormalBranchTest, SubscribePublishedDataTest001, TestSize.Level0)
825 {
826 LOG_INFO("SubscribePublishedDataTest001::Start");
827 std::vector<std::string> uris = {};
828 int64_t subscriberId = 1;
829 sptr<IDataProxyPublishedDataObserver> observer = OHOS::sptr<IDataProxyPublishedDataObserver>(nullptr);
830 auto proxy = DataShareManagerImpl::GetServiceProxy();
831 auto resultset = proxy->SubscribePublishedData(uris, subscriberId, observer);
832 EXPECT_EQ(resultset.size(), 0);
833 LOG_INFO("SubscribePublishedDataTest001::End");
834 }
835
836 /**
837 * @tc.name: GeneralControllerServiceImplInsertTest001
838 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
839 * @tc.type: FUNC
840 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
841 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return an DATA_SHARE_ERROR.
842 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
843 2. Call Insert() in GeneralControllerServiceImpl;
844 3. Check if the function return DATA_SHARE_ERROR.
845 * @tc.require: issueIBX9HL
846 */
847 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplInsertTest001, TestSize.Level1)
848 {
849 LOG_INFO("GeneralControllerServiceImplInsertTest001::Start");
850 DataShareManagerImplHelper();
851 DataShare::DataShareValuesBucket valuesBucket;
852 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
853 Uri uri(proxyUri);
854 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
855 int ret = generalCtl->Insert(uri, valuesBucket);
856 EXPECT_EQ(ret, DATA_SHARE_ERROR);
857 LOG_INFO("GeneralControllerServiceImplInsertTest001::End");
858 }
859
860 /**
861 * @tc.name: GeneralControllerServiceImplUpdateTest001
862 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
863 * @tc.type: FUNC
864 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
865 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return an DATA_SHARE_ERROR.
866 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
867 2. Call Update() in GeneralControllerServiceImpl;
868 3. Check if the function return DATA_SHARE_ERROR.
869 * @tc.require: issueIBX9HL
870 */
871 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplUpdateTest001, TestSize.Level1)
872 {
873 LOG_INFO("GeneralControllerServiceImplUpdateTest001::Start");
874 DataShareManagerImplHelper();
875 DataShare::DataSharePredicates predicates;
876 DataShare::DataShareValuesBucket valuesBucket;
877 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
878 Uri uri(proxyUri);
879 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
880 int ret = generalCtl->Update(uri, predicates, valuesBucket);
881 EXPECT_EQ(ret, DATA_SHARE_ERROR);
882 LOG_INFO("GeneralControllerServiceImplUpdateTest001::End");
883 }
884
885 /**
886 * @tc.name: GeneralControllerServiceImplDeleteTest001
887 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
888 * @tc.type: FUNC
889 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
890 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return an DATA_SHARE_ERROR.
891 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
892 2. Call Update() in GeneralControllerServiceImpl;
893 3. Check if the function return DATA_SHARE_ERROR.
894 * @tc.require: issueIBX9HL
895 */
896 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplDeleteTest001, TestSize.Level1)
897 {
898 LOG_INFO("GeneralControllerServiceImplDeleteTest001::Start");
899 DataShareManagerImplHelper();
900 DataShare::DataSharePredicates predicates;
901 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
902 Uri uri(proxyUri);
903 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
904 int ret = generalCtl->Delete(uri, predicates);
905 EXPECT_EQ(ret, DATA_SHARE_ERROR);
906 LOG_INFO("GeneralControllerServiceImplDeleteTest001::End");
907 }
908
909 /**
910 * @tc.name: GeneralControllerServiceImplInsertExTest001
911 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
912 * @tc.type: FUNC
913 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
914 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code
915 and return pair of DATA_SHARE_ERROR and 0.
916 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
917 2. Call InsertEx() in GeneralControllerServiceImpl;
918 3. Check if the function return pair of DATA_SHARE_ERROR and 0.
919 * @tc.require: issueIBX9HL
920 */
921 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplInsertExTest001, TestSize.Level1)
922 {
923 LOG_INFO("GeneralControllerServiceImplInsertExTest001::Start");
924 DataShareManagerImplHelper();
925 DataShare::DataShareValuesBucket valuesBucket;
926 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
927 Uri uri(proxyUri);
928 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
929 auto ret = generalCtl->InsertEx(uri, valuesBucket);
930 EXPECT_EQ(ret, std::make_pair(DATA_SHARE_ERROR, 0));
931 LOG_INFO("GeneralControllerServiceImplInsertExTest001::End");
932 }
933
934 /**
935 * @tc.name: GeneralControllerServiceImplUpdateExTest001
936 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
937 * @tc.type: FUNC
938 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
939 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code
940 and return pair of DATA_SHARE_ERROR and 0.
941 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
942 2. Call UpdateEx() in GeneralControllerServiceImpl;
943 3. Check if the function return pair of DATA_SHARE_ERROR and 0.
944 * @tc.require: issueIBX9HL
945 */
946 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplUpdateExTest001, TestSize.Level1)
947 {
948 LOG_INFO("GeneralControllerServiceImplUpdateExTest001::Start");
949 DataShareManagerImplHelper();
950 DataShare::DataSharePredicates predicates;
951 DataShare::DataShareValuesBucket valuesBucket;
952 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
953 Uri uri(proxyUri);
954 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
955 auto ret = generalCtl->UpdateEx(uri, predicates, valuesBucket);
956 EXPECT_EQ(ret, std::make_pair(DATA_SHARE_ERROR, 0));
957 LOG_INFO("GeneralControllerServiceImplUpdateExTest001::End");
958 }
959
960 /**
961 * @tc.name: GeneralControllerServiceImplDeleteExTest001
962 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
963 * @tc.type: FUNC
964 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
965 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code
966 and return pair of DATA_SHARE_ERROR and 0.
967 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
968 2. Call DeleteEx() in GeneralControllerServiceImpl;
969 3. Check if the function return pair of DATA_SHARE_ERROR and 0.
970 * @tc.require: issueIBX9HL
971 */
972 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplDeleteExTest001, TestSize.Level1)
973 {
974 LOG_INFO("GeneralControllerServiceImplDeleteExTest001::Start");
975 DataShareManagerImplHelper();
976 DataShare::DataSharePredicates predicates;
977 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
978 Uri uri(proxyUri);
979 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
980 auto ret = generalCtl->DeleteEx(uri, predicates);
981 EXPECT_EQ(ret, std::make_pair(DATA_SHARE_ERROR, 0));
982 LOG_INFO("GeneralControllerServiceImplDeleteExTest001::End");
983 }
984
985 /**
986 * @tc.name: GeneralControllerServiceImplQueryTest001
987 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
988 * @tc.type: FUNC
989 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
990 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return a nullptr.
991 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
992 2. Call Query() in GeneralControllerServiceImpl;
993 3. Check if the function return nullptr.
994 * @tc.require: issueIBX9HL
995 */
996 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplQueryTest001, TestSize.Level1)
997 {
998 LOG_INFO("GeneralControllerServiceImplQueryTest001::Start");
999 DataShareManagerImplHelper();
1000 DataShare::DataSharePredicates predicates;
1001 std::vector<string> columns;
1002 DatashareBusinessError error;
1003 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1004 Uri uri(proxyUri);
1005 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
1006 DataShareOption option;
1007 auto ret = generalCtl->Query(uri, predicates, columns, error, option);
1008 EXPECT_EQ(ret, nullptr);
1009 LOG_INFO("GeneralControllerServiceImplQueryTest001::End");
1010 }
1011
1012 /**
1013 * @tc.name: GeneralControllerServiceImplNotifyChangeTest001
1014 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1015 * @tc.type: FUNC
1016 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1017 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code.
1018 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1019 2. Call NotifyChange() in GeneralControllerServiceImpl;
1020 3. Check if the function return.
1021 * @tc.require: issueIBX9HL
1022 */
1023 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplNotifyChangeTest001, TestSize.Level1)
1024 {
1025 LOG_INFO("GeneralControllerServiceImplNotifyChangeTest001::Start");
1026 DataShareManagerImplHelper();
1027 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1028 Uri uri(proxyUri);
1029 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
1030 generalCtl->NotifyChange(uri);
1031 LOG_INFO("GeneralControllerServiceImplNotifyChangeTest001::End");
1032 }
1033
1034 /**
1035 * @tc.name: GeneralControllerServiceImplSetRegisterCallbackTest001
1036 * @tc.desc: Check the main process
1037 * @tc.type: FUNC
1038 * @tc.precon: None
1039 * @tc.expect: Enter the DataShareManagerImpl::GetInstance() branch in the code.
1040 * @tc.step: 1. Call SetRegisterCallback() in GeneralControllerServiceImpl;
1041 2. Check if the function return.
1042 * @tc.require: issueIBX9HL
1043 */
1044 HWTEST_F(AbnormalBranchTest, GeneralControllerServiceImplSetRegisterCallbackTest001, TestSize.Level1)
1045 {
1046 LOG_INFO("GeneralControllerServiceImplSetRegisterCallbackTest001::Start");
1047 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1048 auto generalCtl = std::make_shared<GeneralControllerServiceImpl>(proxyUri);
1049 generalCtl->SetRegisterCallback();
1050 LOG_INFO("GeneralControllerServiceImplSetRegisterCallbackTest001::End");
1051 }
1052
1053 /**
1054 * @tc.name: PersistentDataControllerAddQueryTemplateTest001
1055 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1056 * @tc.type: FUNC
1057 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1058 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return INVALID_VALUE
1059 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1060 2. Call AddQueryTemplate() in PersistentDataController;
1061 3. Check if the function return INVALID_VALUE.
1062 * @tc.require: issueIBX9HL
1063 */
1064 HWTEST_F(AbnormalBranchTest, PersistentDataControllerAddQueryTemplateTest001, TestSize.Level1)
1065 {
1066 LOG_INFO("PersistentDataControllerAddQueryTemplateTest001::Start");
1067 DataShareManagerImplHelper();
1068 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1069 std::vector<PredicateTemplateNode> nodes;
1070 Template tpl(nodes, "select name1 as name from TBL00");
1071 auto controller = PersistentDataController();
1072 auto ret = controller.AddQueryTemplate(proxyUri, 0, tpl);
1073 EXPECT_EQ(ret, INVALID_VALUE);
1074 LOG_INFO("PersistentDataControllerAddQueryTemplateTest001::End");
1075 }
1076
1077 /**
1078 * @tc.name: PersistentDataControllerDelQueryTemplateTest001
1079 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1080 * @tc.type: FUNC
1081 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1082 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return INVALID_VALUE
1083 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1084 2. Call DelQueryTemplate() in PersistentDataController;
1085 3. Check if the function return INVALID_VALUE.
1086 * @tc.require: issueIBX9HL
1087 */
1088 HWTEST_F(AbnormalBranchTest, PersistentDataControllerDelQueryTemplateTest001, TestSize.Level1)
1089 {
1090 LOG_INFO("PersistentDataControllerDelQueryTemplateTest001::Start");
1091 DataShareManagerImplHelper();
1092 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1093 auto controller = PersistentDataController();
1094 auto ret = controller.DelQueryTemplate(proxyUri, 0);
1095 EXPECT_EQ(ret, INVALID_VALUE);
1096 LOG_INFO("PersistentDataControllerDelQueryTemplateTest001::End");
1097 }
1098
1099 /**
1100 * @tc.name: PersistentDataControllerSubscribeRdbDataTest001
1101 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1102 * @tc.type: FUNC
1103 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1104 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1105 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1106 2. Call SubscribeRdbData() in PersistentDataController;
1107 3. Check if the function return empty vector.
1108 * @tc.require: issueIBX9HL
1109 */
1110 HWTEST_F(AbnormalBranchTest, PersistentDataControllerSubscribeRdbDataTest001, TestSize.Level1)
1111 {
1112 LOG_INFO("PersistentDataControllerSubscribeRdbDataTest001::Start");
1113 DataShareManagerImplHelper();
1114 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1115 std::vector<std::string> uris = {proxyUri};
1116 TemplateId tplId;
1117 auto callback = std::function<void(const RdbChangeNode &)>();
1118 auto controller = PersistentDataController();
1119 auto ret = controller.SubscribeRdbData(nullptr, uris, tplId, callback);
1120 EXPECT_EQ(ret.size(), 0);
1121 LOG_INFO("PersistentDataControllerSubscribeRdbDataTest001::End");
1122 }
1123
1124 /**
1125 * @tc.name: PersistentDataControllerUnSubscribeRdbDataTest001
1126 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1127 * @tc.type: FUNC
1128 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1129 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1130 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1131 2. Call UnSubscribeRdbData() in PersistentDataController;
1132 3. Check if the function return empty vector.
1133 * @tc.require: issueIBX9HL
1134 */
1135 HWTEST_F(AbnormalBranchTest, PersistentDataControllerUnSubscribeRdbDataTest001, TestSize.Level1)
1136 {
1137 LOG_INFO("PersistentDataControllerUnSubscribeRdbDataTest001::Start");
1138 DataShareManagerImplHelper();
1139 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1140 std::vector<std::string> uris = {proxyUri};
1141 TemplateId tplId;
1142 auto controller = PersistentDataController();
1143 auto ret = controller.UnSubscribeRdbData(nullptr, uris, tplId);
1144 EXPECT_EQ(ret.size(), 0);
1145 LOG_INFO("PersistentDataControllerUnSubscribeRdbDataTest001::End");
1146 }
1147
1148 /**
1149 * @tc.name: PersistentDataControllerEnableSubscribeRdbDataTest001
1150 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1151 * @tc.type: FUNC
1152 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1153 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1154 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1155 2. Call EnableSubscribeRdbData() in PersistentDataController;
1156 3. Check if the function return empty vector.
1157 * @tc.require: issueIBX9HL
1158 */
1159 HWTEST_F(AbnormalBranchTest, PersistentDataControllerEnableSubscribeRdbDataTest001, TestSize.Level1)
1160 {
1161 LOG_INFO("PersistentDataControllerEnableSubscribeRdbDataTest001::Start");
1162 DataShareManagerImplHelper();
1163 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1164 std::vector<std::string> uris = {proxyUri};
1165 TemplateId tplId;
1166 auto controller = PersistentDataController();
1167 auto ret = controller.EnableSubscribeRdbData(nullptr, uris, tplId);
1168 EXPECT_EQ(ret.size(), 0);
1169 LOG_INFO("PersistentDataControllerEnableSubscribeRdbDataTest001::End");
1170 }
1171
1172 /**
1173 * @tc.name: PersistentDataControllerDisableSubscribeRdbDataTest001
1174 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1175 * @tc.type: FUNC
1176 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1177 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1178 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1179 2. Call DisableSubscribeRdbData() in PersistentDataController;
1180 3. Check if the function return empty vector.
1181 * @tc.require: issueIBX9HL
1182 */
1183 HWTEST_F(AbnormalBranchTest, PersistentDataControllerDisableSubscribeRdbDataTest001, TestSize.Level1)
1184 {
1185 LOG_INFO("PersistentDataControllerDisableSubscribeRdbDataTest001::Start");
1186 DataShareManagerImplHelper();
1187 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1188 std::vector<std::string> uris = {proxyUri};
1189 TemplateId tplId;
1190 auto controller = PersistentDataController();
1191 auto ret = controller.DisableSubscribeRdbData(nullptr, uris, tplId);
1192 EXPECT_EQ(ret.size(), 0);
1193 LOG_INFO("PersistentDataControllerDisableSubscribeRdbDataTest001::End");
1194 }
1195
1196 /**
1197 * @tc.name: PublishedDataControllerPublishTest001
1198 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1199 * @tc.type: FUNC
1200 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1201 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1202 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1203 2. Call Publish() in PublishedDataController;
1204 3. Check if the function return empty vector.
1205 * @tc.require: issueIBX9HL
1206 */
1207 HWTEST_F(AbnormalBranchTest, PublishedDataControllerPublishTest001, TestSize.Level1)
1208 {
1209 LOG_INFO("PublishedDataControllerPublishTest001::Start");
1210 DataShareManagerImplHelper();
1211 Data data;
1212 std::string bundleName = "com.acts.ohos.data.datasharetest";
1213 auto controller = PublishedDataController();
1214 auto ret = controller.Publish(data, bundleName);
1215 EXPECT_EQ(ret.size(), 0);
1216 LOG_INFO("PublishedDataControllerPublishTest001::End");
1217 }
1218
1219 /**
1220 * @tc.name: PublishedDataControllerGetPublishedDataTest001
1221 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1222 * @tc.type: FUNC
1223 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1224 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code
1225 and return empty datas_ vector in Data.
1226 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1227 2. Call GetPublishedData() in PublishedDataController;
1228 3. Check if the function return empty datas_ vector in Data.
1229 * @tc.require: issueIBX9HL
1230 */
1231 HWTEST_F(AbnormalBranchTest, PublishedDataControllerGetPublishedDataTest001, TestSize.Level1)
1232 {
1233 LOG_INFO("PublishedDataControllerGetPublishedDataTest001::Start");
1234 DataShareManagerImplHelper();
1235 std::string bundleName = "com.acts.ohos.data.datasharetest";
1236 int errCode = 0;
1237 auto controller = PublishedDataController();
1238 auto ret = controller.GetPublishedData(bundleName, errCode);
1239 EXPECT_EQ(ret.datas_.size(), 0);
1240 LOG_INFO("PublishedDataControllerGetPublishedDataTest001::End");
1241 }
1242
1243 /**
1244 * @tc.name: PublishedDataControllerSubscribePublishedDataTest001
1245 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1246 * @tc.type: FUNC
1247 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1248 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1249 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1250 2. Call SubscribePublishedData() in PublishedDataController;
1251 3. Check if the function return empty vector.
1252 * @tc.require: issueIBX9HL
1253 */
1254 HWTEST_F(AbnormalBranchTest, PublishedDataControllerSubscribePublishedDataTest001, TestSize.Level1)
1255 {
1256 LOG_INFO("PublishedDataControllerSubscribePublishedDataTest001::Start");
1257 DataShareManagerImplHelper();
1258 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1259 std::vector<std::string> uris = {proxyUri};
1260 auto callback = std::function<void(const PublishedDataChangeNode &changeNode)>();
1261 auto controller = PublishedDataController();
1262 auto ret = controller.SubscribePublishedData(nullptr, uris, 0, callback);
1263 EXPECT_EQ(ret.size(), 0);
1264 LOG_INFO("PublishedDataControllerSubscribePublishedDataTest001::End");
1265 }
1266
1267 /**
1268 * @tc.name: PublishedDataControllerSubscribeUnSubscribePublishedDataTest001
1269 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1270 * @tc.type: FUNC
1271 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1272 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1273 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1274 2. Call UnSubscribePublishedData() in PublishedDataController;
1275 3. Check if the function return empty vector.
1276 * @tc.require: issueIBX9HL
1277 */
1278 HWTEST_F(AbnormalBranchTest, PublishedDataControllerSubscribeUnSubscribePublishedDataTest001, TestSize.Level1)
1279 {
1280 LOG_INFO("PublishedDataControllerSubscribeUnSubscribePublishedDataTest001::Start");
1281 DataShareManagerImplHelper();
1282 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1283 std::vector<std::string> uris = {proxyUri};
1284 auto controller = PublishedDataController();
1285 auto ret = controller.UnSubscribePublishedData(nullptr, uris, 0);
1286 EXPECT_EQ(ret.size(), 0);
1287 LOG_INFO("PublishedDataControllerSubscribeUnSubscribePublishedDataTest001::End");
1288 }
1289
1290 /**
1291 * @tc.name: PublishedDataControllerSubscribeEnableSubscribePublishedDataTest001
1292 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1293 * @tc.type: FUNC
1294 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1295 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1296 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1297 2. Call EnableSubscribePublishedData() in PublishedDataController;
1298 3. Check if the function return empty vector.
1299 * @tc.require: issueIBX9HL
1300 */
1301 HWTEST_F(AbnormalBranchTest, PublishedDataControllerSubscribeEnableSubscribePublishedDataTest001, TestSize.Level1)
1302 {
1303 LOG_INFO("PublishedDataControllerSubscribeEnableSubscribePublishedDataTest001::Start");
1304 DataShareManagerImplHelper();
1305 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1306 std::vector<std::string> uris = {proxyUri};
1307 auto controller = PublishedDataController();
1308 auto ret = controller.EnableSubscribePublishedData(nullptr, uris, 0);
1309 EXPECT_EQ(ret.size(), 0);
1310 LOG_INFO("PublishedDataControllerSubscribeEnableSubscribePublishedDataTest001::End");
1311 }
1312
1313 /**
1314 * @tc.name: PublishedDataControllerSubscribeDisableSubscribePublishedDataTest001
1315 * @tc.desc: Fill the branch DataShareManagerImpl::GetServiceProxy() == nullptr
1316 * @tc.type: FUNC
1317 * @tc.precon: Mock the DataShareManagerImpl::GetServiceProxy() return nullptr
1318 * @tc.expect: Enter the DataShareManagerImpl::GetServiceProxy() branch in the code and return empty vector
1319 * @tc.step: 1. Mock GetServiceProxy() to return nullptr;
1320 2. Call DisableSubscribePublishedData() in PublishedDataController;
1321 3. Check if the function return empty vector.
1322 * @tc.require: issueIBX9HL
1323 */
1324 HWTEST_F(AbnormalBranchTest, PublishedDataControllerSubscribeDisableSubscribePublishedDataTest001, TestSize.Level1)
1325 {
1326 LOG_INFO("PublishedDataControllerSubscribeDisableSubscribePublishedDataTest001::Start");
1327 DataShareManagerImplHelper();
1328 std::string proxyUri = "datashareproxy://com.acts.ohos.data.datasharetest/test";
1329 std::vector<std::string> uris = {proxyUri};
1330 auto controller = PublishedDataController();
1331 auto ret = controller.DisableSubscribePublishedData(nullptr, uris, 0);
1332 EXPECT_EQ(ret.size(), 0);
1333 LOG_INFO("PublishedDataControllerSubscribeDisableSubscribePublishedDataTest001::End");
1334 }
1335 } // namespace DataShare
1336 } // namespace OHOS