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
16 #define LOG_TAG "CloudInfoTest"
17 #include <gtest/gtest.h>
18
19 #include "serializable/serializable.h"
20 #include "cloud/cloud_db.h"
21 #include "cloud/cloud_event.h"
22 #include "cloud/cloud_info.h"
23 #include "cloud/cloud_server.h"
24 #include "cloud/schema_meta.h"
25 #include "utils/crypto.h"
26 #include "screen/screen_manager.h"
27 #include "store/general_store.h"
28 #include "store/general_value.h"
29 #include "store/general_watcher.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::DistributedData;
33 namespace OHOS::Test {
34 class CloudInfoTest : public testing::Test {
35 public:
SetUpTestCase(void)36 static void SetUpTestCase(void){};
TearDownTestCase(void)37 static void TearDownTestCase(void){};
SetUp()38 void SetUp(){};
TearDown()39 void TearDown(){};
40 };
41
42 class ServicesCloudServerTest : public testing::Test {
43 public:
SetUpTestCase(void)44 static void SetUpTestCase(void){};
TearDownTestCase(void)45 static void TearDownTestCase(void){};
SetUp()46 void SetUp(){};
TearDown()47 void TearDown(){};
48 };
49
50 class ServicesCloudDBTest : public testing::Test {
51 public:
SetUpTestCase(void)52 static void SetUpTestCase(void){};
TearDownTestCase(void)53 static void TearDownTestCase(void){};
SetUp()54 void SetUp(){};
TearDown()55 void TearDown(){};
56 };
57
58 class CloudEventTest : public testing::Test {
59 public:
SetUpTestCase(void)60 static void SetUpTestCase(void){};
TearDownTestCase(void)61 static void TearDownTestCase(void){};
SetUp()62 void SetUp(){};
TearDown()63 void TearDown(){};
64 };
65
66 class ScreenManagerTest : public testing::Test {
67 public:
68 static void SetUpTestCase(void);
TearDownTestCase(void)69 static void TearDownTestCase(void){};
SetUp()70 void SetUp(){};
TearDown()71 void TearDown(){};
72 };
73
SetUpTestCase()74 void ScreenManagerTest::SetUpTestCase()
75 {
76 ScreenManager::GetInstance()->BindExecutor(nullptr);
77 }
78
79 class MockGeneralWatcher : public DistributedData::GeneralWatcher {
80 public:
OnChange(const Origin & origin,const PRIFields & primaryFields,ChangeInfo && values)81 int32_t OnChange(const Origin &origin, const PRIFields &primaryFields,
82 ChangeInfo &&values) override
83 {
84 return GeneralError::E_OK;
85 }
86
OnChange(const Origin & origin,const Fields & fields,ChangeData && datas)87 int32_t OnChange(const Origin &origin, const Fields &fields, ChangeData &&datas) override
88 {
89 return GeneralError::E_OK;
90 }
91 };
92
93 class MockQuery : public GenQuery {
94 public:
95 static const uint64_t TYPE_ID = 1;
96
IsEqual(uint64_t tid)97 bool IsEqual(uint64_t tid) override
98 {
99 return tid == TYPE_ID;
100 }
101
GetTables()102 std::vector<std::string> GetTables() override
103 {
104 return {"table1", "table2"};
105 }
106 };
107
108 /**
109 * @tc.name: GetSchemaPrefix
110 * @tc.desc: Get schema prefix.
111 * @tc.type: FUNC
112 * @tc.require:
113 * @tc.author: Anvette
114 */
115 HWTEST_F(CloudInfoTest, GetSchemaPrefix, TestSize.Level0)
116 {
117 CloudInfo cloudInfo;
118 auto result = cloudInfo.GetSchemaPrefix("ohos.test.demo");
119 ASSERT_EQ(result, "CLOUD_SCHEMA###0###ohos.test.demo");
120
121 result = cloudInfo.GetSchemaPrefix("");
122 ASSERT_EQ(result, "CLOUD_SCHEMA###0");
123 }
124
125 /**
126 * @tc.name: IsValid
127 * @tc.desc: Determine if it is limited.
128 * @tc.type: FUNC
129 * @tc.require:
130 * @tc.author: Anvette
131 */
132 HWTEST_F(CloudInfoTest, IsValid, TestSize.Level0)
133 {
134 CloudInfo cloudInfo;
135 auto result = cloudInfo.IsValid();
136 ASSERT_FALSE(result);
137
138 CloudInfo cloudInfo1;
139 cloudInfo1.user = 111;
140 cloudInfo1.id = "test1_id";
141 cloudInfo1.totalSpace = 0;
142 cloudInfo1.remainSpace = 0;
143 cloudInfo1.enableCloud = false;
144
145 Serializable::json node1;
146 cloudInfo1.Marshal(node1);
147 result = cloudInfo1.IsValid();
148 ASSERT_TRUE(result);
149 }
150
151 /**
152 * @tc.name: Exist
153 * @tc.desc: Determine if the package exists.
154 * @tc.type: FUNC
155 * @tc.require:
156 * @tc.author: Anvette
157 */
158 HWTEST_F(CloudInfoTest, Exist, TestSize.Level0)
159 {
160 CloudInfo cloudInfo;
161 auto result = cloudInfo.Exist("", 1);
162 ASSERT_FALSE(result);
163
164 CloudInfo::AppInfo appInfo;
165 appInfo.bundleName = "test_cloud_bundleName";
166 appInfo.appId = "test_cloud_id";
167 appInfo.version = 0;
168 appInfo.instanceId = 100;
169 appInfo.cloudSwitch = false;
170
171 cloudInfo.user = 111;
172 cloudInfo.id = "test_cloud_id";
173 cloudInfo.totalSpace = 0;
174 cloudInfo.remainSpace = 100;
175 cloudInfo.enableCloud = true;
176 cloudInfo.apps["test_cloud_bundleName"] = std::move(appInfo);
177
178 Serializable::json node1;
179 cloudInfo.Marshal(node1);
180 result = cloudInfo.Exist("test_cloud_bundleName", 100);
181 ASSERT_TRUE(result);
182 }
183
184 /**
185 * @tc.name: Exist
186 * @tc.desc: Is it on.
187 * @tc.type: FUNC
188 * @tc.require:
189 * @tc.author: Anvette
190 */
191 HWTEST_F(CloudInfoTest, IsOn, TestSize.Level0)
192 {
193 CloudInfo cloudInfo;
194 auto result = cloudInfo.IsOn("ohos.test.demo", 1);
195 ASSERT_FALSE(result);
196
197 CloudInfo::AppInfo appInfo;
198 appInfo.bundleName = "test_cloud_bundleName";
199 appInfo.appId = "test_cloud_id";
200 appInfo.version = 0;
201 appInfo.instanceId = 100;
202 appInfo.cloudSwitch = true;
203
204 cloudInfo.user = 111;
205 cloudInfo.id = "test_cloud_id";
206 cloudInfo.totalSpace = 0;
207 cloudInfo.remainSpace = 100;
208 cloudInfo.enableCloud = true;
209 cloudInfo.apps["test_cloud_bundleName"] = std::move(appInfo);
210
211 Serializable::json node1;
212 cloudInfo.Marshal(node1);
213 result = cloudInfo.IsOn("test_cloud_bundleName", 100);
214 ASSERT_TRUE(result);
215 }
216
217 /**
218 * @tc.name: GetPrefix
219 * @tc.desc: Get prefix.
220 * @tc.type: FUNC
221 * @tc.require:
222 * @tc.author: Anvette
223 */
224 HWTEST_F(CloudInfoTest, GetPrefix, TestSize.Level0)
225 {
226 const std::initializer_list<std::string> fields;
227 auto result = CloudInfo::GetPrefix(fields);
228 ASSERT_EQ(result, "CLOUD_INFO###");
229 }
230
231 /**
232 * @tc.name: CloudInfoTest001
233 * @tc.desc: Marshal and Unmarshal of CloudInfo.
234 * @tc.type: FUNC
235 * @tc.require:
236 * @tc.author: SQL
237 */
238 HWTEST_F(CloudInfoTest, CloudInfoTest001, TestSize.Level0)
239 {
240 CloudInfo cloudInfo1;
241 cloudInfo1.user = 111;
242 cloudInfo1.id = "test1_id";
243 cloudInfo1.totalSpace = 0;
244 cloudInfo1.remainSpace = 0;
245 cloudInfo1.enableCloud = false;
246 cloudInfo1.maxNumber = CloudInfo::DEFAULT_BATCH_NUMBER;
247 cloudInfo1.maxSize = CloudInfo::DEFAULT_BATCH_SIZE;
248
249 Serializable::json node1;
250 cloudInfo1.Marshal(node1);
251 EXPECT_EQ(Serializable::Marshall(cloudInfo1), to_string(node1));
252
253 CloudInfo cloudInfo2;
254 cloudInfo2.Unmarshal(node1);
255 EXPECT_EQ(Serializable::Marshall(cloudInfo1), Serializable::Marshall(cloudInfo2));
256 }
257
258 /**
259 * @tc.name: AppInfoTest
260 * @tc.desc: Marshal and Unmarshal of AppInfo.
261 * @tc.type: FUNC
262 * @tc.require:
263 * @tc.author: Anvette
264 */
265 HWTEST_F(CloudInfoTest, AppInfoTest, TestSize.Level0)
266 {
267 CloudInfo::AppInfo cloudInfoAppInfo1;
268 cloudInfoAppInfo1.bundleName = "ohos.test.demo";
269 cloudInfoAppInfo1.appId = "test1_id";
270 cloudInfoAppInfo1.version = 0;
271 cloudInfoAppInfo1.instanceId = 0;
272 cloudInfoAppInfo1.cloudSwitch = false;
273
274 Serializable::json node1;
275 cloudInfoAppInfo1.Marshal(node1);
276 EXPECT_EQ(Serializable::Marshall(cloudInfoAppInfo1), to_string(node1));
277
278 CloudInfo::AppInfo cloudInfoAppInfo2;
279 cloudInfoAppInfo2.Unmarshal(node1);
280 EXPECT_EQ(Serializable::Marshall(cloudInfoAppInfo1), Serializable::Marshall(cloudInfoAppInfo2));
281 }
282
283 /**
284 * @tc.name: TableTest
285 * @tc.desc: Marshal and Unmarshal of Table.
286 * @tc.type: FUNC
287 * @tc.require:
288 * @tc.author: Anvette
289 */
290 HWTEST_F(CloudInfoTest, TableTest, TestSize.Level0)
291 {
292 Field field1;
293 field1.colName = "test1_colName";
294 field1.alias = "test1_alias";
295 field1.type = 1;
296 field1.primary = true;
297 field1.nullable = false;
298
299 Table table1;
300 table1.name = "test1_name";
301 table1.sharedTableName = "test1_sharedTableName";
302 table1.alias = "test1_alias";
303 table1.fields.push_back(field1);
304 Serializable::json node1;
305 table1.Marshal(node1);
306 EXPECT_EQ(Serializable::Marshall(table1), to_string(node1));
307
308 Table table2;
309 table2.Unmarshal(node1);
310 EXPECT_EQ(Serializable::Marshall(table1), Serializable::Marshall(table2));
311 }
312
313 /**
314 * @tc.name: IsAllSwitchOffTest
315 * @tc.desc: Determine if all apps have their cloud synchronization disabled.
316 * @tc.type: FUNC
317 * @tc.require:
318 * @tc.author: Anvette
319 */
320 HWTEST_F(CloudInfoTest, IsAllSwitchOffTest, TestSize.Level0)
321 {
322 CloudInfo cloudInfo;
323 auto result = cloudInfo.IsAllSwitchOff();
324 ASSERT_TRUE(result);
325
326 CloudInfo cloudInfo1;
327 CloudInfo::AppInfo appInfo;
328 std::map<std::string, CloudInfo::AppInfo> apps;
329 appInfo.bundleName = "test_cloud_bundleName";
330 appInfo.appId = "test_cloud_id";
331 appInfo.version = 0;
332 appInfo.instanceId = 100;
333 appInfo.cloudSwitch = true;
334 apps = { { "test_cloud", appInfo } };
335 cloudInfo1.apps = apps;
336 result = cloudInfo1.IsAllSwitchOff();
337 ASSERT_FALSE(result);
338 }
339
340 /**
341 * @tc.name: GetSchemaKey
342 * @tc.desc: Get schema key.
343 * @tc.type: FUNC
344 * @tc.require:
345 * @tc.author: Anvette
346 */
347 HWTEST_F(CloudInfoTest, GetSchemaKeyTest1, TestSize.Level0)
348 {
349 std::string bundleName = "test_cloud_bundleName";
350 int32_t instanceId = 123;
351 CloudInfo cloudInfo;
352 auto result = cloudInfo.GetSchemaKey(bundleName, instanceId);
353 ASSERT_EQ(result, "CLOUD_SCHEMA###0###test_cloud_bundleName###123");
354 }
355
356 /**
357 * @tc.name: GetSchemaKey
358 * @tc.desc: Get schema key.
359 * @tc.type: FUNC
360 * @tc.require:
361 * @tc.author: Anvette
362 */
363 HWTEST_F(CloudInfoTest, GetSchemaKeyTest2, TestSize.Level0)
364 {
365 int32_t user = 123;
366 std::string bundleName = "test_cloud_bundleName";
367 int32_t instanceId = 456;
368 CloudInfo cloudInfo;
369 auto result = cloudInfo.GetSchemaKey(user, bundleName, instanceId);
370 ASSERT_EQ(result, "CLOUD_SCHEMA###123###test_cloud_bundleName###456");
371 }
372
373 /**
374 * @tc.name: GetSchemaKey
375 * @tc.desc: Get schema key.
376 * @tc.type: FUNC
377 * @tc.require:
378 * @tc.author: Anvette
379 */
380 HWTEST_F(CloudInfoTest, GetSchemaKeyTest3, TestSize.Level0)
381 {
382 StoreMetaData meta;
383 meta.bundleName = "test_cloud_bundleName";
384 meta.user = "123";
385 CloudInfo cloudInfo;
386 auto result = cloudInfo.GetSchemaKey(meta);
387 ASSERT_EQ(result, "CLOUD_SCHEMA###123###test_cloud_bundleName###0");
388 }
389
390 /**
391 * @tc.name: GetSchemaKey
392 * @tc.desc: Get schema key.
393 * @tc.type: FUNC
394 * @tc.require:
395 * @tc.author: Anvette
396 */
397 HWTEST_F(CloudInfoTest, GetSchemaKeyTest4, TestSize.Level0)
398 {
399 CloudInfo cloudInfo;
400 CloudInfo::AppInfo appInfo;
401 std::map<std::string, CloudInfo::AppInfo> apps;
402 appInfo.bundleName = "test_cloud_bundleName";
403 appInfo.appId = "test_cloud_id";
404 appInfo.version = 0;
405 appInfo.instanceId = 100;
406 appInfo.cloudSwitch = true;
407 apps = { { "test_cloud", appInfo } };
408 cloudInfo.apps = apps;
409 std::map<std::string, std::string> info;
410 info = { {"test_cloud_bundleName", "CLOUD_SCHEMA###0###test_cloud###100"} };
411 auto result = cloudInfo.GetSchemaKey();
412 ASSERT_EQ(info, result);
413 }
414
415 /**
416 * @tc.name: CloudServer
417 * @tc.desc: test CloudServer function.
418 * @tc.type: FUNC
419 * @tc.require:
420 * @tc.author: SQL
421 */
422 HWTEST_F(ServicesCloudServerTest, CloudServer, TestSize.Level0)
423 {
424 CloudServer cloudServer;
425 int32_t userId = 100;
426 cloudServer.GetServerInfo(userId);
427 std::string bundleName = "com.ohos.cloudtest";
428 cloudServer.GetAppSchema(userId, bundleName);
429 std::map<std::string, std::vector<CloudServer::Database>> dbs;
430 auto result1 = cloudServer.Subscribe(userId, dbs);
431 EXPECT_EQ(result1, GeneralError::E_OK);
432 result1 = cloudServer.Unsubscribe(userId, dbs);
433 EXPECT_EQ(result1, GeneralError::E_OK);
434
435 int user = 1;
436 uint32_t tokenId = 123;
437 CloudServer::Database dbMeta;
438 auto result2 = cloudServer.ConnectAssetLoader(tokenId, dbMeta);
439 EXPECT_EQ(result2, nullptr);
440 result2 = cloudServer.ConnectAssetLoader(bundleName, user, dbMeta);
441 EXPECT_EQ(result2, nullptr);
442
443 auto result3 = cloudServer.ConnectCloudDB(tokenId, dbMeta);
444 EXPECT_EQ(result3, nullptr);
445 result3 = cloudServer.ConnectCloudDB(bundleName, user, dbMeta);
446 EXPECT_EQ(result3, nullptr);
447
448 auto result4 = cloudServer.ConnectSharingCenter(userId, bundleName);
449 EXPECT_EQ(result4, nullptr);
450 cloudServer.Clean(userId);
451 cloudServer.ReleaseUserInfo(userId);
452 std::shared_ptr<ExecutorPool> executor = std::make_shared<ExecutorPool>(1, 0);
453 cloudServer.Bind(executor);
454 auto result5 = cloudServer.IsSupportCloud(userId);
455 EXPECT_FALSE(result5);
456 }
457
458 /**
459 * @tc.name: CloudDB
460 * @tc.desc: test CloudDB function.
461 * @tc.type: FUNC
462 * @tc.require:
463 * @tc.author: SQL
464 */
465 HWTEST_F(ServicesCloudDBTest, CloudDB, TestSize.Level0)
466 {
467 CloudDB cloudDB;
468 std::string table = "table";
469 VBucket extend;
470 VBuckets values;
471 auto result1 = cloudDB.Execute(table, "sql", extend);
472 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
473 result1 = cloudDB.BatchInsert(table, std::move(values), values);
474 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
475 result1 = cloudDB.BatchUpdate(table, std::move(values), values);
476 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
477 result1 = cloudDB.BatchDelete(table, values);
478 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
479 result1 = cloudDB.PreSharing(table, values);
480 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
481
482 CloudDB::Devices devices;
483 MockQuery query;
484 CloudDB::Async async;
485 result1 = cloudDB.Sync(devices, 0, query, async, 0);
486 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
487
488 MockGeneralWatcher watcher;
489 result1 = cloudDB.Watch(1, watcher);
490 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
491 result1 = cloudDB.Unwatch(1, watcher);
492 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
493
494 result1 = cloudDB.Lock();
495 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
496 result1 = cloudDB.Unlock();
497 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
498 result1 = cloudDB.Heartbeat();
499 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
500 result1 = cloudDB.Close();
501 EXPECT_EQ(result1, GeneralError::E_NOT_SUPPORT);
502
503 auto result2 = cloudDB.Query(table, extend);
504 EXPECT_EQ(result2.second, nullptr);
505 result2 = cloudDB.Query(query, extend);
506 EXPECT_EQ(result2.second, nullptr);
507
508 auto result3 = cloudDB.AliveTime();
509 EXPECT_EQ(result3, -1);
510 std::string bundleName = "com.ohos.cloudDBtest";
511 auto result4 = cloudDB.GetEmptyCursor(bundleName);
512 std::pair<int32_t, std::string> cursor = { GeneralError::E_NOT_SUPPORT, "" };
513 EXPECT_EQ(result4, cursor);
514 }
515
516 /**
517 * @tc.name: SchemaMeta
518 * @tc.desc: test SchemaMeta GetLowVersion and GetHighVersion function.
519 * @tc.type: FUNC
520 * @tc.require:
521 * @tc.author: SQL
522 */
523 HWTEST_F(CloudInfoTest, SchemaMeta, TestSize.Level0)
524 {
525 SchemaMeta schemaMeta;
526 auto metaVersion = SchemaMeta::CURRENT_VERSION & 0xFFFF;
527 auto result1 = schemaMeta.GetLowVersion();
528 EXPECT_EQ(result1, metaVersion);
529 metaVersion = SchemaMeta::CURRENT_VERSION & ~0xFFFF;
530 auto result2 = schemaMeta.GetHighVersion();
531 EXPECT_EQ(result2, metaVersion);
532 }
533
534 /**
535 * @tc.name: GetEventId
536 * @tc.desc: test GetEventId function
537 * @tc.type: FUNC
538 * @tc.require:
539 * @tc.author:
540 */
541 HWTEST_F(CloudEventTest, GetEventId, TestSize.Level0)
542 {
543 int32_t evtId = 1;
544 StoreInfo info;
545 CloudEvent event(evtId, info);
546 auto ret = event.GetEventId();
547 EXPECT_EQ(ret, evtId);
548 }
549 } // namespace OHOS::Test
550