1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "DataShareProfileConfigTest"
16 
17 #include <gtest/gtest.h>
18 #include <unistd.h>
19 
20 #include "datashare_errno.h"
21 #include "data_share_db_config.h"
22 #include "data_share_profile_config.h"
23 #include "data_share_service_impl.h"
24 #include "log_print.h"
25 
26 namespace OHOS::Test {
27 using namespace testing::ext;
28 using namespace OHOS::DataShare;
29 using namespace OHOS::DistributedData;
30 std::string DATA_SHARE_URI = "datashare:///com.acts.datasharetest";
31 constexpr uint32_t CALLERTOKENID = 100;
32 class DataShareProfileConfigTest : public testing::Test {
33 public:
34     static constexpr int64_t USER_TEST = 100;
SetUpTestCase(void)35     static void SetUpTestCase(void){};
TearDownTestCase(void)36     static void TearDownTestCase(void){};
SetUp()37     void SetUp(){};
TearDown()38     void TearDown(){};
39 };
40 
41 /**
42 * @tc.name: GetDbConfig
43 * @tc.desc: test data_share_db_config is GetDbConfig abnormal scene
44 * @tc.type: FUNC
45 * @tc.require:SQL
46 */
47 HWTEST_F(DataShareProfileConfigTest, GetDbConfig, TestSize.Level1)
48 {
49     DataShareDbConfig dbConfig;
50     DataShareDbConfig::DbConfig config {"", "", "", "", "", 0, false};
51     auto result = dbConfig.GetDbConfig(config);
52     EXPECT_EQ(std::get<0>(result), NativeRdb::E_DB_NOT_EXIST);
53 
54     config.hasExtension = true;
55     result = dbConfig.GetDbConfig(config);
56     EXPECT_EQ(std::get<0>(result), NativeRdb::E_DB_NOT_EXIST);
57 
58     config.uri = DATA_SHARE_URI;
59     config.bundleName = "bundleName";
60     config.storeName = "storeName";
61     config.userId = USER_TEST;
62     result = dbConfig.GetDbConfig(config);
63     EXPECT_NE(std::get<0>(result), DataShare::E_OK);
64 }
65 
66 /**
67 * @tc.name: Config
68 * @tc.desc: test config Marshal and Unmarshal function
69 * @tc.type: FUNC
70 * @tc.require:SQL
71 */
72 HWTEST_F(DataShareProfileConfigTest, Config, TestSize.Level1)
73 {
74     Config config;
75     config.uri = "uri";
76     config.crossUserMode = 1;
77     config.writePermission = "writePermission";
78     config.readPermission = "readPermission";
79     Serializable::json node;
80     config.Marshal(node);
81     EXPECT_EQ(node["uri"], "uri");
82     EXPECT_EQ(node["crossUserMode"], 1);
83     EXPECT_EQ(node["writePermission"], "writePermission");
84     EXPECT_EQ(node["readPermission"], "readPermission");
85 
86     Config configs;
87     configs.Unmarshal(node);
88     EXPECT_EQ(configs.uri, "uri");
89     EXPECT_EQ(configs.crossUserMode, 1);
90     EXPECT_EQ(configs.writePermission, "writePermission");
91     EXPECT_EQ(configs.readPermission, "readPermission");
92 }
93 
94 /**
95 * @tc.name: ProfileInfo001
96 * @tc.desc: test ProfileInfo Marshal and Unmarshal function
97 * @tc.type: FUNC
98 * @tc.require:SQL
99 */
100 HWTEST_F(DataShareProfileConfigTest, ProfileInfo001, TestSize.Level1)
101 {
102     ProfileInfo info;
103     Config config;
104     config.uri = "uri";
105     config.crossUserMode = 1;
106     config.writePermission = "writePermission";
107     config.readPermission = "readPermission";
108     info.tableConfig = { config };
109     info.isSilentProxyEnable = 1;
110     info.storeName = "storeName";
111     info.tableName = "tableName";
112 
113     Serializable::json node;
114     info.Marshal(node);
115     EXPECT_EQ(node["isSilentProxyEnable"], true);
116     EXPECT_EQ(node["path"], "storeName/tableName");
117     EXPECT_EQ(node["scope"], "module");
118     EXPECT_EQ(node["type"], "rdb");
119 
120     ProfileInfo profileInfo;
121     profileInfo.Unmarshal(node);
122     EXPECT_EQ(profileInfo.isSilentProxyEnable, true);
123     EXPECT_EQ(profileInfo.storeName, "storeName");
124     EXPECT_EQ(profileInfo.tableName, "tableName");
125     EXPECT_EQ(profileInfo.scope, "module");
126     EXPECT_EQ(profileInfo.type, "rdb");
127 }
128 
129 /**
130 * @tc.name: ProfileInfo002
131 * @tc.desc: test ProfileInfo Unmarshal abnormal scenario
132 * @tc.type: FUNC
133 * @tc.require:SQL
134 */
135 HWTEST_F(DataShareProfileConfigTest, ProfileInfo002, TestSize.Level1)
136 {
137     ProfileInfo info;
138     Config config;
139     config.uri = "uri";
140     config.crossUserMode = 1;
141     config.writePermission = "writePermission";
142     config.readPermission = "readPermission";
143     info.tableConfig = { config };
144     info.isSilentProxyEnable = 1;
145     info.storeName = "";
146     info.tableName = "";
147     Serializable::json node1;
148     info.Marshal(node1);
149     EXPECT_EQ(node1["path"], "/");
150     ProfileInfo profileInfo;
151     profileInfo.Unmarshal(node1);
152     EXPECT_EQ(profileInfo.storeName, "");
153     EXPECT_EQ(profileInfo.tableName, "");
154 
155     Serializable::json node2;
156     info.storeName = "storeName";
157     info.tableName = "";
158     info.Marshal(node2);
159     EXPECT_EQ(node2["path"], "storeName/");
160     profileInfo.Unmarshal(node2);
161     EXPECT_EQ(profileInfo.storeName, "");
162     EXPECT_EQ(profileInfo.tableName, "");
163 
164     Serializable::json node3;
165     info.storeName = "";
166     info.tableName = "tableName";
167     info.Marshal(node3);
168     EXPECT_EQ(node3["path"], "/tableName");
169     profileInfo.Unmarshal(node3);
170     EXPECT_EQ(profileInfo.storeName, "");
171     EXPECT_EQ(profileInfo.tableName, "");
172 }
173 
174 /**
175 * @tc.name: ReadProfile
176 * @tc.desc: test ReadProfile error scenario
177 * @tc.type: FUNC
178 * @tc.require:SQL
179 */
180 HWTEST_F(DataShareProfileConfigTest, ReadProfile, TestSize.Level1)
181 {
182     DataShareProfileConfig profileConfig;
183     auto result = profileConfig.ReadProfile("");
184     EXPECT_EQ(result, "");
185     result = profileConfig.ReadProfile("resPath");
186     EXPECT_EQ(result, "");
187 }
188 
189 /**
190 * @tc.name: GetAccessCrossMode
191 * @tc.desc: test GetAccessCrossMode function
192 * @tc.type: FUNC
193 * @tc.require:SQL
194 */
195 HWTEST_F(DataShareProfileConfigTest, GetAccessCrossMode, TestSize.Level1)
196 {
197     DataShareProfileConfig profileConfig;
198     ProfileInfo info;
199     Config config;
200     config.uri = "storeUri";
201     config.crossUserMode = 1;
202     config.writePermission = "writePermission";
203     config.readPermission = "readPermission";
204     info.tableConfig = { config };
205     info.isSilentProxyEnable = 1;
206     info.storeName = "storeName";
207     info.tableName = "tableName";
208 
209     auto result = profileConfig.GetAccessCrossMode(info, "tableUri", "storeUri");
210     EXPECT_EQ(result, AccessCrossMode::USER_SHARED);
211 }
212 
213 /**
214 * @tc.name: GetFromDataProperties
215 * @tc.desc: test GetFromDataProperties scenario
216 * @tc.type: FUNC
217 * @tc.require:SQL
218 */
219 HWTEST_F(DataShareProfileConfigTest, GetFromDataProperties, TestSize.Level1)
220 {
221     DataProviderConfig providerConfig(DATA_SHARE_URI, CALLERTOKENID);
222     providerConfig.GetProviderInfo();
223 
224     ProfileInfo info;
225     info.isSilentProxyEnable = 1;
226     info.storeName = "storeName";
227     info.tableName = "tableName";
228     info.scope = "moduletest";
229     auto result = providerConfig.GetFromDataProperties(info, "moduleName");
230     EXPECT_EQ(result, DataShare::E_OK);
231 }
232 
233 /**
234 * @tc.name: GetFromUriPath
235 * @tc.desc: test GetFromUriPath error scenario
236 * @tc.type: FUNC
237 * @tc.require:SQL
238 */
239 HWTEST_F(DataShareProfileConfigTest, GetFromUriPath, TestSize.Level1)
240 {
241     DataProviderConfig providerConfig(DATA_SHARE_URI, CALLERTOKENID);
242     providerConfig.GetProviderInfo();
243 
244     ProfileInfo info;
245     info.isSilentProxyEnable = 1;
246     info.storeName = "";
247     info.tableName = "";
248     info.scope = "moduletest";
249     auto result = providerConfig.GetFromDataProperties(info, "moduleName");
250     EXPECT_EQ(result, DataShare::E_OK);
251 
252     result = providerConfig.GetFromUriPath();
253     EXPECT_EQ(result, false);
254 }
255 
256 /**
257 * @tc.name: GetFromExtension
258 * @tc.desc: test GetFromExtension error scenario
259 * @tc.type: FUNC
260 * @tc.require:SQL
261 */
262 HWTEST_F(DataShareProfileConfigTest, GetFromExtension, TestSize.Level1)
263 {
264     DataProviderConfig providerConfig(DATA_SHARE_URI, CALLERTOKENID);
265     providerConfig.GetProviderInfo();
266 
267     ProfileInfo info;
268     Config config;
269     config.uri = DATA_SHARE_URI;
270     config.crossUserMode = 1;
271     config.writePermission = "writePermission";
272     config.readPermission = "readPermission";
273     info.tableConfig = { config };
274     info.isSilentProxyEnable = 1;
275     info.storeName = "storeName";
276     info.tableName = "tableName";
277     auto result = providerConfig.GetFromDataProperties(info, "moduleName");
278     EXPECT_EQ(result, DataShare::E_OK);
279 
280     result = providerConfig.GetFromExtension();
281     EXPECT_EQ(result, DataShare::E_URI_NOT_EXIST);
282 }
283 } // namespace OHOS::Test