• 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 <gtest/gtest.h>
17 #include <unistd.h>
18 
19 #include "datashare_helper.h"
20 #include "datashare_helper_impl.h"
21 #include "datashare_log.h"
22 #include "datashare_uri_utils.h"
23 #include "iservice_registry.h"
24 #include "ikvstore_data_service_mock.h"
25 
26 namespace OHOS {
27 namespace DataShare {
28 using namespace testing::ext;
29 
30 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
31 std::string NON_SILENT_ACCESS_URI = "datashare:///com.acts.datasharetest";
32 std::string NON_SILENT_ACCESS_ERROR_URI = "datashare:///com.acts.test";
33 constexpr int DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID = 1301;
34 
35 class DataShareHelperTest : public testing::Test {
36 public:
SetUpTestCase(void)37     static void SetUpTestCase(void){};
TearDownTestCase(void)38     static void TearDownTestCase(void){};
SetUp()39     void SetUp(){};
TearDown()40     void TearDown(){};
41 };
42 
DataShareManagerImplHelper()43 void DataShareManagerImplHelper()
44 {
45     auto helper = DataShareManagerImpl::GetInstance();
46     helper->dataShareService_ = nullptr;
47     auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48     auto remoteObject = manager->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
49     sptr<MockDataShareKvServiceProxy> mockProxy = sptr<MockDataShareKvServiceProxy>
50         (new MockDataShareKvServiceProxy(remoteObject));
51     EXPECT_CALL(*mockProxy, GetFeatureInterface(testing::_))
52         .WillOnce(testing::Return(nullptr));
53     helper->dataMgrService_ = (sptr<DataShareKvServiceProxy>)mockProxy;
54 }
55 
56 /**
57  * @tc.name:
58  * @tc.desc: test Creator function when options.isProxy_ = false and options.token_ = nullptr
59  * @tc.type: FUNC
60  * @tc.require:issueIC413F
61  * @tc.precon: None
62  * @tc.step:
63     1.Create a DataShareHelper object what options.isProxy_ = false and options.token_ = nullptr
64     2.call Creator function and check the result
65  * @tc.experct: Creator failed and reutrn nullptr
66  */
67 HWTEST_F(DataShareHelperTest, CreatorTest001, TestSize.Level0)
68 {
69     LOG_INFO("DataShareHelperTest CreatorTest001::Start");
70     std::string strUri = "testUri";
71     CreateOptions options;
72     options.isProxy_ = false;
73     options.token_ = nullptr;
74     std::string bundleName = "testBundle";
75     int waitTime = 1;
76     bool isSystem = true;
77     auto result = DataShareHelper::Creator(strUri, options, bundleName, waitTime, isSystem);
78     EXPECT_EQ(result, nullptr);
79     LOG_INFO("DataShareHelperTest CreatorTest001::End");
80 }
81 
82 /**
83  * @tc.name:
84  * @tc.desc: test Creator function when options.isProxy_ = true and options.token_ = nullptr
85  * @tc.type: FUNC
86  * @tc.require:issueIC413F
87  * @tc.precon: None
88  * @tc.step:
89     1.Create a DataShareHelper object what options.isProxy_ = true and options.token_ = nullptr
90     2.call Creator function and check the result
91  * @tc.experct: Creator failed and reutrn nullptr
92  */
93 HWTEST_F(DataShareHelperTest, CreatorTest002, TestSize.Level0)
94 {
95     LOG_INFO("DataShareHelperTest CreatorTest002::Start");
96     std::string strUri = "testUri";
97     CreateOptions options;
98     options.isProxy_ = true;
99     options.token_ = nullptr;
100     std::string bundleName = "testBundle";
101     int waitTime = 1;
102     bool isSystem = true;
103     auto result = DataShareHelper::Creator(strUri, options, bundleName, waitTime, isSystem);
104     EXPECT_EQ(result, nullptr);
105     LOG_INFO("DataShareHelperTest CreatorTest002::End");
106 }
107 
108 /**
109  * @tc.name:
110  * @tc.desc: test Creator function when options.isProxy_ = false and options.token_ != nullptr
111  * @tc.type: FUNC
112  * @tc.require:issueICDSBD
113  * @tc.precon: None
114  * @tc.step:
115     1.Create a DataShareHelper object what options.isProxy_ = false and options.token_ != nullptr
116     2.call Creator function and check the result
117  * @tc.experct: Creator succeed and not reutrn nullptr
118  */
119 HWTEST_F(DataShareHelperTest, CreatorTest003, TestSize.Level0)
120 {
121     LOG_INFO("DataShareHelperTest CreatorTest003::Start");
122     CreateOptions options;
123     options.isProxy_ = false;
124 
125     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
126     EXPECT_NE(saManager, nullptr);
127     auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
128     EXPECT_NE(remoteObj, nullptr);
129     options.token_ = remoteObj;
130     auto result = DataShareHelper::Creator(NON_SILENT_ACCESS_URI, options);
131     EXPECT_NE(result, nullptr);
132     LOG_INFO("DataShareHelperTest CreatorTest003::End");
133 }
134 
135 /**
136  * @tc.name:
137  * @tc.desc: test Creator function when options.isProxy_ = true and options.token_ != nullptr
138  * @tc.type: FUNC
139  * @tc.precon: None
140  * @tc.step:
141     1.Create a DataShareHelper object what options.isProxy_ = true and options.token_ != nullptr
142     2.call Creator function and check the result
143  * @tc.experct: Creator failed and not reutrn nullptr
144  */
145 HWTEST_F(DataShareHelperTest, CreatorTest004, TestSize.Level0)
146 {
147     LOG_INFO("DataShareHelperTest CreatorTest004::Start");
148     CreateOptions options;
149     options.isProxy_ = true;
150     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151     EXPECT_NE(saManager, nullptr);
152     auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
153     EXPECT_NE(remoteObj, nullptr);
154     options.token_ = remoteObj;
155     auto result = DataShareHelper::Creator(NON_SILENT_ACCESS_ERROR_URI, options);
156     EXPECT_EQ(result, nullptr);
157     LOG_INFO("DataShareHelperTest CreatorTest004::End");
158 }
159 
160 /**
161  * @tc.name:
162  * @tc.desc: test CreateExtHelper function when Uri contains "appIndex="
163  * @tc.type: FUNC
164  * @tc.require:issueIC413F
165  * @tc.precon: None
166  * @tc.step:
167     1.Create a DataShareHelper object what Uri contains "appIndex="
168     2.call CreateExtHelper function and check the result
169  * @tc.experct: CreateExtHelper failed and reutrn nullptr
170  */
171 HWTEST_F(DataShareHelperTest, CreateExtHelper001, TestSize.Level0)
172 {
173     LOG_INFO("DataShareHelperTest CreateExtHelper001::Start");
174     OHOS::Uri uri("datashareproxy://com.acts.ohos.data.datasharetest/test?appIndex=abcd");
175     uri.query_ = ("appIndex=abcd");
176     sptr<IRemoteObject> token = nullptr;
177     int waitTime = 1000;
178     bool isSystem = false;
179     auto result = DataShareHelper::CreateExtHelper(uri, token, waitTime, isSystem);
180     EXPECT_EQ(result, nullptr);
181     LOG_INFO("DataShareHelperTest CreateExtHelper001::End");
182 }
183 
184 /**
185  * @tc.name: SetSilentSwitch001
186  * @tc.desc: test SetSilentSwitch function when DataShareManagerImpl::GetServiceProxy() == nullptr
187  * @tc.type: FUNC
188  * @tc.require:issueIC413F
189  * @tc.precon: None
190  * @tc.step:
191     1.Create a DataShareHelper object
192     2.call SetSilentSwitch function when DataShareManagerImpl::GetServiceProxy() == nullptr
193  * @tc.experct: SetSilentSwitch failed and reutrn DATA_SHARE_ERROR
194  */
195 HWTEST_F(DataShareHelperTest, SetSilentSwitch001, TestSize.Level0)
196 {
197     LOG_INFO("DataShareHelperTest SetSilentSwitch001::Start");
198     DataShareManagerImplHelper();
199     OHOS::Uri uri("datashareproxy://com.acts.ohos.data.datasharetest/test?appIndex=abcd");
200     uri.query_ = ("appIndex=abcd");
201     bool enable = false;
202     bool isSystem = false;
203     auto result = DataShareHelper::SetSilentSwitch(uri, enable, isSystem);
204     EXPECT_EQ(result, DATA_SHARE_ERROR);
205     LOG_INFO("DataShareHelperTest SetSilentSwitch001::End");
206 }
207 
208 /**
209  * @tc.name: GetSilentProxyStatus001
210  * @tc.desc: test GetSilentProxyStatus function when DataShareManagerImpl::GetServiceProxy() == nullptr
211  * @tc.type: FUNC
212  * @tc.require:issueIC413F
213  * @tc.precon: None
214  * @tc.step:
215     1.Create a DataShareHelper object
216     2.call GetSilentProxyStatus function when DataShareManagerImpl::GetServiceProxy() == nullptr
217  * @tc.experct: GetSilentProxyStatus failed and reutrn E_ERROR
218  */
219 HWTEST_F(DataShareHelperTest, GetSilentProxyStatus001, TestSize.Level0)
220 {
221     LOG_INFO("DataShareHelperTest GetSilentProxyStatus001::Start");
222     DataShareManagerImplHelper();
223     std::string uri = "datashareproxy://com.acts.ohos.data.datasharetest/test?appIndex=abcd";
224     bool isSystem = false;
225     auto result = DataShareHelper::GetSilentProxyStatus(uri, isSystem);
226     EXPECT_EQ(result, E_ERROR);
227     LOG_INFO("DataShareHelperTest GetSilentProxyStatus001::End");
228 }
229 
230 /**
231  * @tc.name: CreateServiceHelper001
232  * @tc.desc: test CreateExtHelper function when DataShareManagerImpl::GetServiceProxy() == nullptr
233  * @tc.type: FUNC
234  * @tc.require:issueIC413F
235  * @tc.precon: None
236  * @tc.step:
237     1.Create a DataShareHelper object
238     2.call CreateServiceHelper function when DataShareManagerImpl::GetServiceProxy() == nullptr
239  * @tc.experct: CreateServiceHelper failed and reutrn nullptr
240  */
241 HWTEST_F(DataShareHelperTest, CreateServiceHelper001, TestSize.Level0)
242 {
243     LOG_INFO("DataShareHelperTest CreateServiceHelper001::Start");
244     DataShareManagerImplHelper();
245     std::string exuri = "testExuri";
246     std::string bundleName = "bundleName";
247     bool isSystem = false;
248     auto result = DataShareHelper::CreateServiceHelper(exuri, bundleName, isSystem);
249     EXPECT_EQ(result, nullptr);
250     LOG_INFO("DataShareHelperTest CreateServiceHelper001::End");
251 }
252 } // namespace DataShare
253 } // namespace OHOS