• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dataproxy_handle.h"
20 #include "dataproxy_handle_common.h"
21 #include "datashare_log.h"
22 #include "hap_token_info.h"
23 #include "token_setproc.h"
24 #include "datashare_errno.h"
25 
26 namespace OHOS {
27 namespace DataShare {
28 using namespace testing::ext;
29 using namespace OHOS::Security::AccessToken;
30 std::string TEST_URI1 = "datashareproxy://com.acts.datasharetest/test1";
31 std::string TEST_URI2 = "datashareproxy://com.acts.datasharetest.other/test1";
32 std::string TEST_UNUSED_URI = "datashareproxy://com.acts.datasharetest/unused";
33 std::atomic_int g_callbackTimes = 0;
34 
35 class ProxyHandleTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void ProxyHandleTest::SetUpTestCase(void)
44 {
45     LOG_INFO("SetUpTestCase invoked");
46     int sleepTime = 1;
47     sleep(sleepTime);
48 
49     HapInfoParams info = { .userID = 100,
50         .bundleName = "com.acts.datasharetest",
51         .instIndex = 0,
52         .appIDDesc = "com.acts.datasharetest",
53         .isSystemApp = true };
54     HapPolicyParams policy = { .apl = APL_SYSTEM_BASIC,
55         .domain = "test.domain",
56         .permList = { { .permissionName = "ohos.permission.GET_BUNDLE_INFO",
57             .bundleName = "com.acts.datasharetest",
58             .grantMode = 1,
59             .availableLevel = APL_SYSTEM_BASIC,
60             .label = "label",
61             .labelId = 1,
62             .description = "com.acts.datasharetest",
63             .descriptionId = 1 } },
64         .permStateList = { { .permissionName = "ohos.permission.GET_BUNDLE_INFO",
65             .isGeneral = true,
66             .resDeviceID = { "local" },
67             .grantStatus = { PermissionState::PERMISSION_GRANTED },
68             .grantFlags = { 1 } } } };
69     AccessTokenKit::AllocHapToken(info, policy);
70     auto testTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenIDEx(
71         info.userID, info.bundleName, info.instIndex);
72     SetSelfTokenID(testTokenId.tokenIDEx);
73 
74     LOG_INFO("SetUpTestCase end");
75 }
76 
TearDownTestCase(void)77 void ProxyHandleTest::TearDownTestCase(void)
78 {
79     auto tokenId = AccessTokenKit::GetHapTokenID(100, "com.acts.datasharetest", 0);
80     AccessTokenKit::DeleteToken(tokenId);
81 }
82 
SetUp(void)83 void ProxyHandleTest::SetUp(void)
84 {
85 }
86 
TearDown(void)87 void ProxyHandleTest::TearDown(void)
88 {
89 }
90 
91 /**
92  * @tc.name: ProxyHandleTest_Publish_Test_001
93  * @tc.desc: Verify DataProxyHandle successfully publishing string data
94  * @tc.type: FUNC
95  * @tc.require: issueIC8OCN
96  * @tc.precon: None
97  * @tc.step:
98     1. Create a DataProxyHandle instance
99     2. Configure the publish parameters to SHARED_CONFIG type
100     3. Prepare string-type test data and publish it
101     4. Retrieve the data via the same URI
102  * @tc.expect:
103     1. The DataProxyHandle is created successfully
104     2. The data publish operation returns a SUCCESS status code
105     3. The retrieved data matches the originally published data
106  */
107 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Publish_Test_001, TestSize.Level0)
108 {
109     LOG_INFO("ProxyHandleTest_Publish_Test_001::Start");
110     auto [ret, handle] = DataShare::DataProxyHandle::Create();
111     EXPECT_EQ(ret, E_OK);
112     EXPECT_NE(handle, nullptr);
113 
114     DataProxyConfig proxyConfig;
115     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
116 
117     // publish and get string
118     DataProxyValue value = "hello";
119     DataShareProxyData data(TEST_URI1, value);
120     std::vector<DataShareProxyData> proxyData;
121     proxyData.push_back(data);
122 
123     LOG_INFO("PublishProxyData::Start");
124     auto ret2 = handle->PublishProxyData(proxyData, proxyConfig);
125     EXPECT_EQ(ret2[0].result_, DataProxyErrorCode::SUCCESS);
126 
127     std::vector<std::string> uris;
128     uris.push_back(TEST_URI1);
129     LOG_INFO("GetProxyData::Start");
130     auto ret3 = handle->GetProxyData(uris, proxyConfig);
131     EXPECT_EQ(ret3[0].value_, value);
132 
133     LOG_INFO("ProxyHandleTest_Publish_Test_001::End");
134 }
135 
136 /**
137  * @tc.name: ProxyHandleTest_Publish_Test_002
138  * @tc.desc: Verify DataProxyHandle successfully publishing integer data
139  * @tc.type: FUNC
140  * @tc.require: issueIC8OCN
141  * @tc.precon: None
142  * @tc.step:
143     1. Create a DataProxyHandle instance
144     2. Configure the publish parameters to SHARED_CONFIG type
145     3. Prepare integer-type test data and publish it
146     4. Retrieve the data via the same URI
147  * @tc.expect:
148     1. The DataProxyHandle is created successfully
149     2. The data publish operation returns a SUCCESS status code
150     3. The retrieved data matches the originally published data
151  */
152 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Publish_Test_002, TestSize.Level0)
153 {
154     LOG_INFO("ProxyHandleTest_Publish_Test_002::Start");
155     auto [ret, handle] = DataShare::DataProxyHandle::Create();
156     EXPECT_EQ(ret, E_OK);
157 
158     DataProxyConfig proxyConfig;
159     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
160 
161     // publish and get int
162     DataProxyValue value = 123456;
163     DataShareProxyData data(TEST_URI1, value);
164     std::vector<DataShareProxyData> proxyData;
165     proxyData.push_back(data);
166 
167     auto ret2 = handle->PublishProxyData(proxyData, proxyConfig);
168     EXPECT_EQ(ret2[0].result_, DataProxyErrorCode::SUCCESS);
169 
170     std::vector<std::string> uris;
171     uris.push_back(TEST_URI1);
172     auto ret3 = handle->GetProxyData(uris, proxyConfig);
173     EXPECT_EQ(ret3[0].value_, value);
174 
175     LOG_INFO("ProxyHandleTest_Publish_Test_002::End");
176 }
177 
178 /**
179  * @tc.name: ProxyHandleTest_Publish_Test_003
180  * @tc.desc: Verify DataProxyHandle successfully
181  * @tc.type: FUNC
182  * @tc.require: issueIC8OCN
183  * @tc.precon: None
184  * @tc.step:
185     1. Create a DataProxyHandle instance
186     2. Configure the publish parameters to SHARED_CONFIG type
187     3. Prepare double-precision floating-point test data and publish it
188     4. Retrieve the data via the same URI
189  * @tc.expect:
190     1. The DataProxyHandle is created successfully
191     2. The data publish operation returns a SUCCESS status code
192     3. The retrieved data matches the originally published data
193  */
194 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Publish_Test_003, TestSize.Level0)
195 {
196     LOG_INFO("ProxyHandleTest_Publish_Test_003::Start");
197     auto [ret, handle] = DataShare::DataProxyHandle::Create();
198     EXPECT_EQ(ret, E_OK);
199 
200     DataProxyConfig proxyConfig;
201     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
202 
203     // publish and get double
204     DataProxyValue value = 123456.123456;
205     DataShareProxyData data(TEST_URI1, value);
206     std::vector<DataShareProxyData> proxyData;
207     proxyData.push_back(data);
208 
209     auto ret2 = handle->PublishProxyData(proxyData, proxyConfig);
210     EXPECT_EQ(ret2[0].result_, DataProxyErrorCode::SUCCESS);
211 
212     std::vector<std::string> uris;
213     uris.push_back(TEST_URI1);
214     auto ret3 = handle->GetProxyData(uris, proxyConfig);
215     EXPECT_EQ(ret3[0].value_, value);
216 
217     LOG_INFO("ProxyHandleTest_Publish_Test_003::End");
218 }
219 
220 /**
221  * @tc.name: ProxyHandleTest_Publish_Test_004
222  * @tc.desc: Verify DataProxyHandle successfully publishing boolean data
223  * @tc.type: FUNC
224  * @tc.require: issueIC8OCN
225  * @tc.precon: None
226  * @tc.step:
227     1. Create a DataProxyHandle instance
228     2. Configure the publish parameters to SHARED_CONFIG type
229     3. Prepare boolean-type test data and publish it
230     4. Retrieve the data via the same URI
231  * @tc.expect:
232     1. The DataProxyHandle is created successfully
233     2. The data publish operation returns a SUCCESS status code
234     3. The retrieved data matches the originally published data
235  */
236 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Publish_Test_004, TestSize.Level0)
237 {
238     LOG_INFO("ProxyHandleTest_Publish_Test_004::Start");
239     auto [ret, handle] = DataShare::DataProxyHandle::Create();
240     EXPECT_EQ(ret, E_OK);
241 
242     DataProxyConfig proxyConfig;
243     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
244 
245     // publish and get bool
246     DataProxyValue value = true;
247     DataShareProxyData data(TEST_URI1, value);
248     std::vector<DataShareProxyData> proxyData;
249     proxyData.push_back(data);
250 
251     auto ret2 = handle->PublishProxyData(proxyData, proxyConfig);
252     EXPECT_EQ(ret2[0].result_, DataProxyErrorCode::SUCCESS);
253 
254     std::vector<std::string> uris;
255     uris.push_back(TEST_URI1);
256     auto ret3 = handle->GetProxyData(uris, proxyConfig);
257     EXPECT_EQ(ret3[0].value_, value);
258 
259     LOG_INFO("ProxyHandleTest_Publish_Test_004::End");
260 }
261 
262 /**
263  * @tc.name: ProxyHandleTest_Publish_Test_005
264  * @tc.desc: Verify that DataProxyHandle fails to publish data from another bundlename
265  * @tc.type: FUNC
266  * @tc.require: issueIC8OCN
267  * @tc.precon: None
268  * @tc.step:
269     1. Create a DataProxyHandle instance
270     2. Configure the publish parameters to SHARED_CONFIG type
271     3. Prepare test data pointing to another application's bundlename and attempt to publish it
272  * @tc.expect:
273     1. The DataProxyHandle is created successfully
274     2. The data publish operation returns a NO_PERMISSION status code
275  */
276 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Publish_Test_005, TestSize.Level0)
277 {
278     LOG_INFO("ProxyHandleTest_Publish_Test_005::Start");
279     auto [ret, handle] = DataShare::DataProxyHandle::Create();
280     EXPECT_EQ(ret, E_OK);
281 
282     DataProxyConfig proxyConfig;
283     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
284 
285     // publish and get bool
286     DataProxyValue value = true;
287     DataShareProxyData data(TEST_URI2, value);
288     std::vector<DataShareProxyData> proxyData;
289     proxyData.push_back(data);
290 
291     // Publish other bundle name, failed because of no permission.
292     auto ret2 = handle->PublishProxyData(proxyData, proxyConfig);
293     EXPECT_EQ(ret2[0].result_, DataProxyErrorCode::NO_PERMISSION);
294 
295     LOG_INFO("ProxyHandleTest_Publish_Test_005::End");
296 }
297 
298 /**
299  * @tc.name: ProxyHandleTest_Delete_Test_001
300  * @tc.desc: Verify the functionality of DataProxyHandle successfully deleting published data
301  * @tc.type: FUNC
302  * @tc.require: issueIC8OCN
303  * @tc.precon: None
304  * @tc.step:
305     1. Create a DataProxyHandle instance
306     2. Configure the operation parameters to SHARED_CONFIG type
307     3. Publish test data
308     4. Verify that the data can be retrieved normally
309     5. Delete the published data
310     6. Attempt to retrieve the deleted data again
311  * @tc.expect:
312     1. The data publish operation returns a SUCCESS status code
313     2. The data retrieval operation returns data that matches the published data
314     3. The data deletion operation returns a SUCCESS status code
315     4. Attempting to retrieve the data after deletion returns a URI_NOT_EXIST status code
316  */
317 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Delete_Test_001, TestSize.Level0)
318 {
319     LOG_INFO("ProxyHandleTest_Delete_Test_001::Start");
320     auto [ret, handle] = DataShare::DataProxyHandle::Create();
321     EXPECT_EQ(ret, E_OK);
322 
323     DataProxyConfig proxyConfig;
324     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
325 
326     // publish and get bool
327     DataProxyValue value = true;
328     DataShareProxyData data(TEST_URI1, value);
329     std::vector<DataShareProxyData> proxyData;
330     proxyData.push_back(data);
331 
332     auto ret2 = handle->PublishProxyData(proxyData, proxyConfig);
333     EXPECT_EQ(ret2[0].result_, DataProxyErrorCode::SUCCESS);
334 
335     std::vector<std::string> uris;
336     uris.push_back(TEST_URI1);
337     auto ret3 = handle->GetProxyData(uris, proxyConfig);
338     EXPECT_EQ(ret3[0].value_, value);
339 
340     // delete the data success
341     auto ret4 = handle->DeleteProxyData(uris, proxyConfig);
342     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::SUCCESS);
343 
344     // get data failed
345     auto ret5 = handle->GetProxyData(uris, proxyConfig);
346     EXPECT_EQ(ret5[0].result_, DataProxyErrorCode::URI_NOT_EXIST);
347 
348     LOG_INFO("ProxyHandleTest_Delete_Test_001::End");
349 }
350 
351 /**
352  * @tc.name: ProxyHandleTest_Delete_Test_002
353  * @tc.desc: Verify that DataProxyHandle fails to delete data from another bundlename
354  * @tc.type: FUNC
355  * @tc.require: issueIC8OCN
356  * @tc.precon: None
357  * @tc.step:
358     1. Create a DataProxyHandle instance
359     2. Configure the operation parameters to SHARED_CONFIG type
360     3. Attempt to delete test data pointing to another application's bundle name
361  * @tc.expect:
362     1. The DataProxyHandle is created successfully
363     2. The data deletion operation returns a NO_PERMISSION status code
364  */
365 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Delete_Test_002, TestSize.Level0)
366 {
367     LOG_INFO("ProxyHandleTest_Delete_Test_002::Start");
368     auto [ret, handle] = DataShare::DataProxyHandle::Create();
369     EXPECT_EQ(ret, E_OK);
370 
371     DataProxyConfig proxyConfig;
372     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
373 
374     std::vector<std::string> uris;
375     uris.push_back(TEST_URI2);
376 
377     // delete data of other bundle name, failed because of no permission.
378     auto ret4 = handle->DeleteProxyData(uris, proxyConfig);
379     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::NO_PERMISSION);
380 
381     LOG_INFO("ProxyHandleTest_Delete_Test_002::End");
382 }
383 
384 /**
385  * @tc.name: ProxyHandleTest_Delete_Test_003
386  * @tc.desc: Verify that DataProxyHandle fails to delete unpublished data
387  * @tc.type: FUNC
388  * @tc.require: issueIC8OCN
389  * @tc.precon: None
390  * @tc.step:
391     1. Create a DataProxyHandle instance
392     2. Configure the operation parameters to SHARED_CONFIG type
393     3. Attempt to delete unpublished test data
394  * @tc.expect:
395     1. The DataProxyHandle is created successfully
396     2. The data deletion operation returns a URI_NOT_EXIST status code
397  */
398 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Delete_Test_003, TestSize.Level0)
399 {
400     LOG_INFO("ProxyHandleTest_Delete_Test_003::Start");
401     auto [ret, handle] = DataShare::DataProxyHandle::Create();
402     EXPECT_EQ(ret, E_OK);
403 
404     DataProxyConfig proxyConfig;
405     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
406 
407     std::vector<std::string> uris;
408     uris.push_back(TEST_UNUSED_URI);
409 
410     // delete unpublished data failed
411     auto ret4 = handle->DeleteProxyData(uris, proxyConfig);
412     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::URI_NOT_EXIST);
413 
414     LOG_INFO("ProxyHandleTest_Delete_Test_003::End");
415 }
416 
417 /**
418  * @tc.name: ProxyHandleTest_Get_Test_001
419  * @tc.desc: Verify that DataProxyHandle fails to retrieve unpublished data
420  * @tc.type: FUNC
421  * @tc.require: issueIC8OCN
422  * @tc.precon: None
423  * @tc.step:
424     1. Create a DataProxyHandle instance
425     2. Configure the operation parameters to SHARED_CONFIG type
426     3. Attempt to retrieve unpublished test data
427  * @tc.expect:
428     1. The DataProxyHandle is created successfully
429     2. The data retrieval operation returns a URI_NOT_EXIST status code
430  */
431 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Get_Test_001, TestSize.Level0)
432 {
433     LOG_INFO("ProxyHandleTest_Get_Test_001::Start");
434     auto [ret, handle] = DataShare::DataProxyHandle::Create();
435     EXPECT_EQ(ret, E_OK);
436 
437     DataProxyConfig proxyConfig;
438     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
439 
440     std::vector<std::string> uris;
441     uris.push_back(TEST_UNUSED_URI);
442 
443     // delete unpublished data failed
444     auto ret4 = handle->GetProxyData(uris, proxyConfig);
445     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::URI_NOT_EXIST);
446 
447     LOG_INFO("ProxyHandleTest_Get_Test_001::End");
448 }
449 
450 /**
451  * @tc.name: ProxyHandleTest_Subscribe_Test_001
452  * @tc.desc: Verify the functionality of DataProxyHandle successfully subscribing
453  * @tc.type: FUNC
454  * @tc.require: issueIC8OCN
455  * @tc.precon: None
456  * @tc.step:
457     1. Create a DataProxyHandle instance
458     2. Configure the operation parameters to SHARED_CONFIG type
459     3. Publish initial test data
460     4. Subscribe to data change events
461     5. Update the subscribed data
462  * @tc.expect:
463     1. The data publish operation returns a SUCCESS status code
464     2. The data subscription operation returns a SUCCESS status code
465     3. The callback function is triggered once after the data is updated
466     4. The number of change notifications received by the callback function is correct
467  */
468 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Subscribe_Test_001, TestSize.Level0)
469 {
470     LOG_INFO("ProxyHandleTest_Subscribe_Test_001::Start");
471     auto [ret, handle] = DataShare::DataProxyHandle::Create();
472     EXPECT_EQ(ret, E_OK);
473 
474     DataProxyConfig proxyConfig;
475     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
476 
477     // publish data first
478     DataProxyValue value1 = "hello";
479     DataShareProxyData data1(TEST_URI1, value1);
480     std::vector<DataShareProxyData> proxyData1;
481     proxyData1.push_back(data1);
482     handle->PublishProxyData(proxyData1, proxyConfig);
483 
484     std::vector<std::string> uris;
485     uris.push_back(TEST_URI1);
486     // subscribe data
487     g_callbackTimes = 0;
__anonb13cb0de0102(const std::vector<DataProxyChangeInfo> &changeNode) 488     auto ret4 = handle->SubscribeProxyData(uris, [](const std::vector<DataProxyChangeInfo> &changeNode) {
489         LOG_INFO("ProxyHandleTest_Subscribe_Test_001::CallBack success");
490         g_callbackTimes++;
491         EXPECT_EQ(changeNode.size(), 1);
492     });
493     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::SUCCESS);
494 
495     // publish data and do callback
496     DataProxyValue value2 = "world";
497     DataShareProxyData data2(TEST_URI1, value2);
498     std::vector<DataShareProxyData> proxyData2;
499     proxyData2.push_back(data2);
500     handle->PublishProxyData(proxyData2, proxyConfig);
501     EXPECT_EQ(g_callbackTimes, 1);
502     LOG_INFO("ProxyHandleTest_Subscribe_Test_001::End");
503 }
504 
505 /**
506  * @tc.name: ProxyHandleTest_Subscribe_Test_002
507  * @tc.desc: Verify that DataProxyHandle fails to subscribe to unpublished data
508  * @tc.type: FUNC
509  * @tc.require: issueIC8OCN
510  * @tc.precon: None
511  * @tc.step:
512     1. Create a DataProxyHandle instance
513     2. Configure the operation parameters to SHARED_CONFIG type
514     3. Attempt to subscribe to unpublished test data
515  * @tc.expect:
516     1. The DataProxyHandle is created successfully
517     2. The data subscription operation returns a URI_NOT_EXIST status code
518  */
519 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Subscribe_Test_002, TestSize.Level0)
520 {
521     LOG_INFO("ProxyHandleTest_Subscribe_Test_002::Start");
522     auto [ret, handle] = DataShare::DataProxyHandle::Create();
523     EXPECT_EQ(ret, E_OK);
524 
525     DataProxyConfig proxyConfig;
526     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
527 
528     std::vector<std::string> uris;
529     uris.push_back(TEST_UNUSED_URI);
530     // subscribe data
531     g_callbackTimes = 0;
__anonb13cb0de0202(const std::vector<DataProxyChangeInfo> &changeNode) 532     auto ret4 = handle->SubscribeProxyData(uris, [](const std::vector<DataProxyChangeInfo> &changeNode) {
533         LOG_INFO("ProxyHandleTest_Subscribe_Test_002::CallBack success");
534         g_callbackTimes++;
535         EXPECT_EQ(changeNode.size(), 1);
536     });
537     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::URI_NOT_EXIST);
538 
539     LOG_INFO("ProxyHandleTest_Subscribe_Test_002::End");
540 }
541 
542 /**
543  * @tc.name: ProxyHandleTest_Unsubscribe_Test_001
544  * @tc.desc: Verify the functionality of DataProxyHandle successfully unsubscribing from data changes
545  * @tc.type: FUNC
546  * @tc.require: issueIC8OCN
547  * @tc.precon: None
548  * @tc.step:
549     1. Create a DataProxyHandle instance
550     2. Configure the operation parameters to SHARED_CONFIG type
551     3. Publish initial test data
552     4. Subscribe to data change events
553     5. Update the data to verify that the callback function is triggered
554     6. Unsubscribe from the data
555     7. Update the data again
556  * @tc.expect:
557     1. The data publish operation returns a SUCCESS status code
558     2. The data subscription operation returns a SUCCESS status code
559     3. The callback function is triggered after the first data update
560     4. The unsubscribe operation is successful
561     5. The callback function is not triggered after the second data update
562  */
563 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Unsubscribe_Test_001, TestSize.Level0)
564 {
565     LOG_INFO("ProxyHandleTest_Unsubscribe_Test_001::Start");
566     auto [ret, handle] = DataShare::DataProxyHandle::Create();
567     EXPECT_EQ(ret, E_OK);
568 
569     DataProxyConfig proxyConfig;
570     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
571 
572     // publish data first
573     DataProxyValue value1 = "hello";
574     DataShareProxyData data1(TEST_URI1, value1);
575     std::vector<DataShareProxyData> proxyData1;
576     proxyData1.push_back(data1);
577     handle->PublishProxyData(proxyData1, proxyConfig);
578 
579     std::vector<std::string> uris;
580     uris.push_back(TEST_URI1);
581     // subscribe data
582     g_callbackTimes = 0;
__anonb13cb0de0302(const std::vector<DataProxyChangeInfo> &changeNode) 583     auto ret4 = handle->SubscribeProxyData(uris, [](const std::vector<DataProxyChangeInfo> &changeNode) {
584         LOG_INFO("ProxyHandleTest_Unsubscribe_Test_001::CallBack success");
585         g_callbackTimes++;
586         EXPECT_EQ(changeNode.size(), 1);
587     });
588     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::SUCCESS);
589 
590     // publish data and do callback
591     DataProxyValue value2 = "world";
592     DataShareProxyData data2(TEST_URI1, value2);
593     std::vector<DataShareProxyData> proxyData2;
594     proxyData2.push_back(data2);
595     handle->PublishProxyData(proxyData2, proxyConfig);
596 
597     // unsubscribe data success
598     ret4 = handle->UnsubscribeProxyData(uris);
599 
600 
601     // publish data and not do callback
602     handle->PublishProxyData(proxyData2, proxyConfig);
603     LOG_INFO("ProxyHandleTest_Unsubscribe_Test_001::End");
604 }
605 
606 /**
607  * @tc.name: ProxyHandleTest_Unsubscribe_Test_002
608  * @tc.desc: Verify the functionality of DataProxyHandle successfully unsubscribing
609  * @tc.type: FUNC
610  * @tc.require: issueIC8OCN
611  * @tc.precon: None
612  * @tc.step:
613     1. Create a DataProxyHandle instance
614     2. Configure the operation parameters to SHARED_CONFIG type
615     3. Publish initial test data
616     4. Subscribe to data change events
617     5. Update the data to verify that the callback function is triggered
618     6. Call the unsubscribe interface with no parameters to unsubscribe from all subscriptions
619     7. Update the data again
620  * @tc.expect:
621     1. The data publish operation returns a SUCCESS status code
622     2. The data subscription operation returns a SUCCESS status code
623     3. The callback function is triggered after the first data update
624     4. The operation to unsubscribe from all subscriptions is successful
625     5. The callback function is not triggered after the second data update
626  */
627 HWTEST_F(ProxyHandleTest, ProxyHandleTest_Unsubscribe_Test_002, TestSize.Level0)
628 {
629     LOG_INFO("ProxyHandleTest_Unsubscribe_Test_002::Start");
630     auto [ret, handle] = DataShare::DataProxyHandle::Create();
631     EXPECT_EQ(ret, E_OK);
632 
633     DataProxyConfig proxyConfig;
634     proxyConfig.type_ = DataProxyType::SHARED_CONFIG;
635 
636     // publish data first
637     DataProxyValue value1 = "hello";
638     DataShareProxyData data1(TEST_URI1, value1);
639     std::vector<DataShareProxyData> proxyData1;
640     proxyData1.push_back(data1);
641     handle->PublishProxyData(proxyData1, proxyConfig);
642 
643     std::vector<std::string> uris;
644     uris.push_back(TEST_URI1);
645     // subscribe data
646     g_callbackTimes = 0;
__anonb13cb0de0402(const std::vector<DataProxyChangeInfo> &changeNode) 647     auto ret4 = handle->SubscribeProxyData(uris, [](const std::vector<DataProxyChangeInfo> &changeNode) {
648         LOG_INFO("ProxyHandleTest_Unsubscribe_Test_002::CallBack success");
649         g_callbackTimes++;
650         EXPECT_EQ(changeNode.size(), 1);
651     });
652     EXPECT_EQ(ret4[0].result_, DataProxyErrorCode::SUCCESS);
653 
654     // publish data and do callback
655     DataProxyValue value2 = "world";
656     DataShareProxyData data2(TEST_URI1, value2);
657     std::vector<DataShareProxyData> proxyData2;
658     proxyData2.push_back(data2);
659     handle->PublishProxyData(proxyData2, proxyConfig);
660 
661     // unsubscribe all uris success
662     std::vector<std::string> emptyUris;
663     auto ret2 = handle->UnsubscribeProxyData(emptyUris);
664 
665     // publish data and not do callback
666     handle->PublishProxyData(proxyData2, proxyConfig);
667     LOG_INFO("ProxyHandleTest_Unsubscribe_Test_002::End");
668 }
669 } // namespace DataShare
670 } // namespace OHOS