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