• 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_ = true and options.token_ != nullptr
111  * @tc.type: FUNC
112  * @tc.precon: None
113  * @tc.step:
114     1.Create a DataShareHelper object what options.isProxy_ = true and options.token_ != nullptr
115     2.call Creator function and check the result
116  * @tc.experct: Creator failed and not reutrn nullptr
117  */
118 HWTEST_F(DataShareHelperTest, CreatorTest004, TestSize.Level0)
119 {
120     LOG_INFO("DataShareHelperTest CreatorTest004::Start");
121     CreateOptions options;
122     options.isProxy_ = true;
123 
124     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
125     EXPECT_NE(saManager, nullptr);
126     auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
127     EXPECT_NE(remoteObj, nullptr);
128     options.token_ = remoteObj;
129     auto result = DataShareHelper::Creator(NON_SILENT_ACCESS_ERROR_URI, options);
130     EXPECT_EQ(result, nullptr);
131     LOG_INFO("DataShareHelperTest CreatorTest004::End");
132 }
133 
134 /**
135  * @tc.name:
136  * @tc.desc: test CreateExtHelper function when Uri contains "appIndex="
137  * @tc.type: FUNC
138  * @tc.require:issueIC413F
139  * @tc.precon: None
140  * @tc.step:
141     1.Create a DataShareHelper object what Uri contains "appIndex="
142     2.call CreateExtHelper function and check the result
143  * @tc.experct: CreateExtHelper failed and reutrn nullptr
144  */
145 HWTEST_F(DataShareHelperTest, CreateExtHelper001, TestSize.Level0)
146 {
147     LOG_INFO("DataShareHelperTest CreateExtHelper001::Start");
148     OHOS::Uri uri("datashareproxy://com.acts.ohos.data.datasharetest/test?appIndex=abcd");
149     uri.query_ = ("appIndex=abcd");
150     sptr<IRemoteObject> token = nullptr;
151     int waitTime = 1000;
152     bool isSystem = false;
153     auto result = DataShareHelper::CreateExtHelper(uri, token, waitTime, isSystem);
154     EXPECT_EQ(result, nullptr);
155     LOG_INFO("DataShareHelperTest CreateExtHelper001::End");
156 }
157 
158 /**
159  * @tc.name: SetSilentSwitch001
160  * @tc.desc: test SetSilentSwitch function when DataShareManagerImpl::GetServiceProxy() == nullptr
161  * @tc.type: FUNC
162  * @tc.require:issueIC413F
163  * @tc.precon: None
164  * @tc.step:
165     1.Create a DataShareHelper object
166     2.call SetSilentSwitch function when DataShareManagerImpl::GetServiceProxy() == nullptr
167  * @tc.experct: SetSilentSwitch failed and reutrn DATA_SHARE_ERROR
168  */
169 HWTEST_F(DataShareHelperTest, SetSilentSwitch001, TestSize.Level0)
170 {
171     LOG_INFO("DataShareHelperTest SetSilentSwitch001::Start");
172     DataShareManagerImplHelper();
173     OHOS::Uri uri("datashareproxy://com.acts.ohos.data.datasharetest/test?appIndex=abcd");
174     uri.query_ = ("appIndex=abcd");
175     bool enable = false;
176     bool isSystem = false;
177     auto result = DataShareHelper::SetSilentSwitch(uri, enable, isSystem);
178     EXPECT_EQ(result, DATA_SHARE_ERROR);
179     LOG_INFO("DataShareHelperTest SetSilentSwitch001::End");
180 }
181 
182 /**
183  * @tc.name: GetSilentProxyStatus001
184  * @tc.desc: test GetSilentProxyStatus function when DataShareManagerImpl::GetServiceProxy() == nullptr
185  * @tc.type: FUNC
186  * @tc.require:issueIC413F
187  * @tc.precon: None
188  * @tc.step:
189     1.Create a DataShareHelper object
190     2.call GetSilentProxyStatus function when DataShareManagerImpl::GetServiceProxy() == nullptr
191  * @tc.experct: GetSilentProxyStatus failed and reutrn E_ERROR
192  */
193 HWTEST_F(DataShareHelperTest, GetSilentProxyStatus001, TestSize.Level0)
194 {
195     LOG_INFO("DataShareHelperTest GetSilentProxyStatus001::Start");
196     DataShareManagerImplHelper();
197     std::string uri = "datashareproxy://com.acts.ohos.data.datasharetest/test?appIndex=abcd";
198     bool isSystem = false;
199     auto result = DataShareHelper::GetSilentProxyStatus(uri, isSystem);
200     EXPECT_EQ(result, E_ERROR);
201     LOG_INFO("DataShareHelperTest GetSilentProxyStatus001::End");
202 }
203 
204 /**
205  * @tc.name: CreateServiceHelper001
206  * @tc.desc: test CreateExtHelper function when DataShareManagerImpl::GetServiceProxy() == nullptr
207  * @tc.type: FUNC
208  * @tc.require:issueIC413F
209  * @tc.precon: None
210  * @tc.step:
211     1.Create a DataShareHelper object
212     2.call CreateServiceHelper function when DataShareManagerImpl::GetServiceProxy() == nullptr
213  * @tc.experct: CreateServiceHelper failed and reutrn nullptr
214  */
215 HWTEST_F(DataShareHelperTest, CreateServiceHelper001, TestSize.Level0)
216 {
217     LOG_INFO("DataShareHelperTest CreateServiceHelper001::Start");
218     DataShareManagerImplHelper();
219     std::string exuri = "testExuri";
220     std::string bundleName = "bundleName";
221     bool isSystem = false;
222     auto result = DataShareHelper::CreateServiceHelper(exuri, bundleName, isSystem);
223     EXPECT_EQ(result, nullptr);
224     LOG_INFO("DataShareHelperTest CreateServiceHelper001::End");
225 }
226 } // namespace DataShare
227 } // namespace OHOS