• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "datashare_connection.h"
17 
18 #include <gtest/gtest.h>
19 
20 #include "accesstoken_kit.h"
21 #include "data_ability_observer_interface.h"
22 #include "datashare_errno.h"
23 #include "datashare_helper.h"
24 #include "datashare_log.h"
25 #include "datashare_proxy.h"
26 #include "extension_manager_proxy.h"
27 #include "general_controller.h"
28 #include "general_controller_provider_impl.h"
29 #include "general_controller_service_impl.h"
30 
31 namespace OHOS {
32 namespace DataShare {
33 using namespace testing::ext;
34 using namespace OHOS::AAFwk;
35 class DataShareConnectionTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
UrisEqual(std::list<Uri> uri1,std::list<Uri> uri2)41     bool UrisEqual(std::list<Uri> uri1, std::list<Uri> uri2)
42     {
43         if (uri1.size() != uri2.size()) {
44             return false;
45         }
46         auto cmp = [](const Uri &first, const Uri &second) {
47             return first.ToString() < second.ToString();
48         };
49         uri1.sort(cmp);
50         uri2.sort(cmp);
51         auto it1 = uri1.begin();
52         auto it2 = uri2.begin();
53         for (; it1 != uri1.end() && it2 != uri2.end(); it1++, it2++) {
54             if (!it1->Equals(*it2)) {
55                 return false;
56             }
57         }
58         return true;
59     }
60 };
61 
62 class RemoteObjectTest : public IRemoteObject {
63 public:
RemoteObjectTest(std::u16string descriptor)64     explicit RemoteObjectTest(std::u16string descriptor) : IRemoteObject(descriptor) {}
~RemoteObjectTest()65     ~RemoteObjectTest() {}
66 
GetObjectRefCount()67     int32_t GetObjectRefCount()
68     {
69         return 0;
70     }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)71     int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
72     {
73         return 0;
74     }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)75     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
76     {
77         return true;
78     }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)79     bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
80     {
81         return true;
82     }
Dump(int fd,const std::vector<std::u16string> & args)83     int Dump(int fd, const std::vector<std::u16string> &args)
84     {
85         return 0;
86     }
87 };
88 
89 class IDataAbilityObserverTest : public DataAbilityObserverStub {
90 public:
IDataAbilityObserverTest(std::string uri)91     explicit IDataAbilityObserverTest(std::string uri) {uri_ = uri;}
~IDataAbilityObserverTest()92     ~IDataAbilityObserverTest()
93     {}
94 
OnChange()95     void OnChange()
96     {
97         GTEST_LOG_(INFO) << "OnChange enter";
98     }
99     std::string uri_;
100 };
101 
102 std::string DATA_SHARE_URI = "datashare:///com.acts.datasharetest";
103 std::string DATA_SHARE_URI1 = "datashare:///com.acts.datasharetest1";
104 
SetUpTestCase(void)105 void DataShareConnectionTest::SetUpTestCase(void) {}
TearDownTestCase(void)106 void DataShareConnectionTest::TearDownTestCase(void) {}
SetUp(void)107 void DataShareConnectionTest::SetUp(void) {}
TearDown(void)108 void DataShareConnectionTest::TearDown(void) {}
109 
110 /**
111 * @tc.name: DataShareConnection_UpdateObserverExtsProviderMap_Test_001
112 * @tc.desc: test UpdateObserverExtsProviderMap normal func
113 * @tc.type: FUNC
114 */
115 HWTEST_F(DataShareConnectionTest, DataShareConnection_UpdateObserverExtsProviderMap_Test_001, TestSize.Level0)
116 {
117     LOG_INFO("DataShareConnection_UpdateObserverExtsProviderMap_Test_001::Start");
118 
119     Uri uri(DATA_SHARE_URI);
120     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
121     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
122     ASSERT_NE(token, nullptr);
123     sptr<DataShare::DataShareConnection> connection =
124         new (std::nothrow) DataShare::DataShareConnection(uri, token);
125     ASSERT_NE(connection, nullptr);
126 
127     // insert data
128     EXPECT_TRUE(connection->observerExtsProvider_.Empty());
129     sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI);
130     ASSERT_NE(dataObserver, nullptr);
131     connection->UpdateObserverExtsProviderMap(uri, dataObserver, true);
132 
133     Uri uri1(DATA_SHARE_URI1);
134     sptr<IDataAbilityObserverTest> dataObserver1 = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI1);
135     ASSERT_NE(dataObserver1, nullptr);
136     connection->UpdateObserverExtsProviderMap(uri1, dataObserver1, true);
137 
138     EXPECT_FALSE(connection->observerExtsProvider_.Empty());
139     EXPECT_EQ(connection->observerExtsProvider_.Size(), 2);
140     connection = nullptr;
141 
142     LOG_INFO("DataShareConnection_UpdateObserverExtsProviderMap_Test_001::End");
143 }
144 
145 /**
146 * @tc.name: DataShareConnection_DeleteObserverExtsProviderMap_001
147 * @tc.desc: test DeleteObserverExtsProviderMap default func
148 * @tc.type: FUNC
149 */
150 HWTEST_F(DataShareConnectionTest, DataShareConnection_DeleteObserverExtsProviderMap_001, TestSize.Level0)
151 {
152     LOG_INFO("DataShareConnection_DeleteObserverExtsProviderMap_001::Start");
153 
154     Uri uri(DATA_SHARE_URI);
155     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
156     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
157     ASSERT_NE(token, nullptr);
158     sptr<DataShare::DataShareConnection> connection =
159         new (std::nothrow) DataShare::DataShareConnection(uri, token);
160     ASSERT_NE(connection, nullptr);
161 
162     // insert data
163     EXPECT_TRUE(connection->observerExtsProvider_.Empty());
164     sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI);
165     connection->UpdateObserverExtsProviderMap(uri, dataObserver, true);
166 
167     Uri uri1(DATA_SHARE_URI1);
168     sptr<IDataAbilityObserverTest> dataObserver1 = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI1);
169     connection->UpdateObserverExtsProviderMap(uri1, dataObserver1, true);
170 
171     EXPECT_FALSE(connection->observerExtsProvider_.Empty());
172     EXPECT_EQ(connection->observerExtsProvider_.Size(), 2);
173 
174     // delete data that uri can match observer
175     connection->DeleteObserverExtsProviderMap(uri1, dataObserver1);
176     EXPECT_EQ(connection->observerExtsProvider_.Size(), 1);
177 
178     // delete data that uri can not match observer
179     connection->DeleteObserverExtsProviderMap(uri1, dataObserver);
180     EXPECT_EQ(connection->observerExtsProvider_.Size(), 1);
181     connection = nullptr;
182 
183     LOG_INFO("DataShareConnection_DeleteObserverExtsProviderMap_001::End");
184 }
185 
186 /**
187 * @tc.name: DataShareConnection_ReRegisterObserverExtProvider_Test_001
188 * @tc.desc: test ReRegisterObserverExtProvider default func
189 * @tc.type: FUNC
190 */
191 HWTEST_F(DataShareConnectionTest, DataShareConnection_ReRegisterObserverExtProvider_Test_001, TestSize.Level0)
192 {
193     LOG_INFO("DataShareConnection_ReRegisterObserverExtProvider_Test_001::Start");
194 
195     Uri uri(DATA_SHARE_URI);
196     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
197     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
198     ASSERT_NE(token, nullptr);
199     sptr<DataShare::DataShareConnection> connection =
200         new (std::nothrow) DataShare::DataShareConnection(uri, token);
201     ASSERT_NE(connection, nullptr);
202 
203     // get proxy not null
204     std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
205     ASSERT_NE(tokenProxy, nullptr);
206     connection->dataShareProxy_ = tokenProxy;
207 
208     // insert data
209     EXPECT_TRUE(connection->observerExtsProvider_.Empty());
210     sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI);
211     connection->UpdateObserverExtsProviderMap(uri, dataObserver, true);
212 
213     Uri uri1(DATA_SHARE_URI1);
214     sptr<IDataAbilityObserverTest> dataObserver1 = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI1);
215     connection->UpdateObserverExtsProviderMap(uri1, dataObserver1, true);
216 
217     EXPECT_FALSE(connection->observerExtsProvider_.Empty());
218     EXPECT_EQ(connection->observerExtsProvider_.Size(), 2);
219 
220     // test ReRegister func
221     connection->ReRegisterObserverExtProvider();
222     // reRegister success, update observer map
223     EXPECT_FALSE(connection->observerExtsProvider_.Empty());
224     connection = nullptr;
225 
226     LOG_INFO("DataShareConnection_ReRegisterObserverExtProvider_Test_001::End");
227 }
228 
229 /**
230 * @tc.name: DataShareConnection_OnAbilityConnectDone_Test_001
231 * @tc.desc: test ReRegisterObserverExtProvider default func
232 * @tc.type: FUNC
233 */
234 HWTEST_F(DataShareConnectionTest, DataShareConnection_OnAbilityConnectDone_Test_001, TestSize.Level0)
235 {
236     LOG_INFO("DataShareConnection_OnAbilityConnectDone_Test_001::Start");
237 
238     Uri uri(DATA_SHARE_URI);
239     std::u16string tokenString = u"OHOS.DataShare.IDataShare";
240     sptr<IRemoteObject> token = new (std::nothrow) RemoteObjectTest(tokenString);
241     ASSERT_NE(token, nullptr);
242     sptr<DataShare::DataShareConnection> connection =
243         new (std::nothrow) DataShare::DataShareConnection(uri, token);
244     ASSERT_NE(connection, nullptr);
245 
246     // get proxy not null
247     std::shared_ptr<DataShareProxy> tokenProxy = std::make_shared<DataShareProxy>(token);
248     ASSERT_NE(tokenProxy, nullptr);
249     connection->dataShareProxy_ = tokenProxy;
250 
251     // insert data
252     EXPECT_TRUE(connection->observerExtsProvider_.Empty());
253     sptr<IDataAbilityObserverTest> dataObserver = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI);
254     connection->UpdateObserverExtsProviderMap(uri, dataObserver, true);
255 
256     Uri uri1(DATA_SHARE_URI1);
257     sptr<IDataAbilityObserverTest> dataObserver1 = new (std::nothrow) IDataAbilityObserverTest(DATA_SHARE_URI1);
258     connection->UpdateObserverExtsProviderMap(uri1, dataObserver1, true);
259 
260     EXPECT_FALSE(connection->observerExtsProvider_.Empty());
261     EXPECT_EQ(connection->observerExtsProvider_.Size(), 2);
262 
263     // test ReRegister func
264     connection->isReconnect_.store(true);
265     std::string deviceId = "deviceId";
266     std::string bundleName = "bundleName";
267     std::string abilityName = "abilityName";
268     AppExecFwk::ElementName element(deviceId, bundleName, abilityName);
269     int resultCode = 0;
270     connection->OnAbilityConnectDone(element, token, resultCode);
271 
272     // reRegister success, update observer map
273     EXPECT_FALSE(connection->observerExtsProvider_.Empty());
274     connection = nullptr;
275 
276     LOG_INFO("DataShareConnection_OnAbilityConnectDone_Test_001::End");
277 }
278 }
279 }