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_info.h" 21 #include "cloud/schema_meta.h" 22 #include "nlohmann/json.hpp" 23 #include "utils/crypto.h" 24 25 using namespace testing::ext; 26 using namespace OHOS::DistributedData; 27 class CloudInfoTest : public testing::Test { 28 public: SetUpTestCase(void)29 static void SetUpTestCase(void){}; TearDownTestCase(void)30 static void TearDownTestCase(void){}; SetUp()31 void SetUp(){}; TearDown()32 void TearDown(){}; 33 }; 34 35 /** 36 * @tc.name: GetSchemaPrefix 37 * @tc.desc: Get schema prefix. 38 * @tc.type: FUNC 39 * @tc.require: 40 * @tc.author: Anvette 41 */ 42 HWTEST_F(CloudInfoTest, GetSchemaPrefix, TestSize.Level0) 43 { 44 CloudInfo cloudInfo; 45 auto result = cloudInfo.GetSchemaPrefix("ohos.test.demo"); 46 ASSERT_EQ(result, "CLOUD_SCHEMA###0###ohos.test.demo"); 47 48 result = cloudInfo.GetSchemaPrefix(""); 49 ASSERT_EQ(result, "CLOUD_SCHEMA###0"); 50 } 51 52 /** 53 * @tc.name: IsValid 54 * @tc.desc: Determine if it is limited. 55 * @tc.type: FUNC 56 * @tc.require: 57 * @tc.author: Anvette 58 */ 59 HWTEST_F(CloudInfoTest, IsValid, TestSize.Level0) 60 { 61 CloudInfo cloudInfo; 62 auto result = cloudInfo.IsValid(); 63 ASSERT_FALSE(result); 64 65 CloudInfo cloudInfo1; 66 cloudInfo1.user = 111; 67 cloudInfo1.id = "test1_id"; 68 cloudInfo1.totalSpace = 0; 69 cloudInfo1.remainSpace = 0; 70 cloudInfo1.enableCloud = false; 71 72 Serializable::json node1; 73 cloudInfo1.Marshal(node1); 74 result = cloudInfo1.IsValid(); 75 ASSERT_TRUE(result); 76 } 77 78 /** 79 * @tc.name: Exist 80 * @tc.desc: Determine if the package exists. 81 * @tc.type: FUNC 82 * @tc.require: 83 * @tc.author: Anvette 84 */ 85 HWTEST_F(CloudInfoTest, Exist, TestSize.Level0) 86 { 87 CloudInfo cloudInfo; 88 auto result = cloudInfo.Exist("", 1); 89 ASSERT_FALSE(result); 90 91 CloudInfo::AppInfo appInfo; 92 appInfo.bundleName = "test_cloud_bundleName"; 93 appInfo.appId = "test_cloud_id"; 94 appInfo.version = 0; 95 appInfo.instanceId = 100; 96 appInfo.cloudSwitch = false; 97 98 cloudInfo.user = 111; 99 cloudInfo.id = "test_cloud_id"; 100 cloudInfo.totalSpace = 0; 101 cloudInfo.remainSpace = 100; 102 cloudInfo.enableCloud = true; 103 cloudInfo.apps["test_cloud_bundleName"] = std::move(appInfo); 104 105 Serializable::json node1; 106 cloudInfo.Marshal(node1); 107 result = cloudInfo.Exist("test_cloud_bundleName", 100); 108 ASSERT_TRUE(result); 109 } 110 111 /** 112 * @tc.name: Exist 113 * @tc.desc: Is it on. 114 * @tc.type: FUNC 115 * @tc.require: 116 * @tc.author: Anvette 117 */ 118 HWTEST_F(CloudInfoTest, IsOn, TestSize.Level0) 119 { 120 CloudInfo cloudInfo; 121 auto result = cloudInfo.IsOn("ohos.test.demo", 1); 122 ASSERT_FALSE(result); 123 124 CloudInfo::AppInfo appInfo; 125 appInfo.bundleName = "test_cloud_bundleName"; 126 appInfo.appId = "test_cloud_id"; 127 appInfo.version = 0; 128 appInfo.instanceId = 100; 129 appInfo.cloudSwitch = true; 130 131 cloudInfo.user = 111; 132 cloudInfo.id = "test_cloud_id"; 133 cloudInfo.totalSpace = 0; 134 cloudInfo.remainSpace = 100; 135 cloudInfo.enableCloud = true; 136 cloudInfo.apps["test_cloud_bundleName"] = std::move(appInfo); 137 138 Serializable::json node1; 139 cloudInfo.Marshal(node1); 140 result = cloudInfo.IsOn("test_cloud_bundleName", 100); 141 ASSERT_TRUE(result); 142 } 143 144 /** 145 * @tc.name: GetPrefix 146 * @tc.desc: Get prefix. 147 * @tc.type: FUNC 148 * @tc.require: 149 * @tc.author: Anvette 150 */ 151 HWTEST_F(CloudInfoTest, GetPrefix, TestSize.Level0) 152 { 153 const std::initializer_list<std::string> fields; 154 auto result = CloudInfo::GetPrefix(fields); 155 ASSERT_EQ(result, "CLOUD_INFO###"); 156 } 157 158 /** 159 * @tc.name: CloudInfoTest 160 * @tc.desc: Marshal and Unmarshal of CloudInfo. 161 * @tc.type: FUNC 162 * @tc.require: 163 * @tc.author: Anvette 164 */ 165 HWTEST_F(CloudInfoTest, CloudInfoTest, TestSize.Level0) 166 { 167 CloudInfo cloudInfo1; 168 cloudInfo1.user = 111; 169 cloudInfo1.id = "test1_id"; 170 cloudInfo1.totalSpace = 0; 171 cloudInfo1.remainSpace = 0; 172 cloudInfo1.enableCloud = false; 173 174 Serializable::json node1; 175 cloudInfo1.Marshal(node1); 176 EXPECT_EQ(Serializable::Marshall(cloudInfo1), to_string(node1)); 177 178 CloudInfo cloudInfo2; 179 cloudInfo2.Unmarshal(node1); 180 EXPECT_EQ(Serializable::Marshall(cloudInfo1), Serializable::Marshall(cloudInfo1)); 181 } 182 183 /** 184 * @tc.name: AppInfoTest 185 * @tc.desc: Marshal and Unmarshal of AppInfo. 186 * @tc.type: FUNC 187 * @tc.require: 188 * @tc.author: Anvette 189 */ 190 HWTEST_F(CloudInfoTest, AppInfoTest, TestSize.Level0) 191 { 192 CloudInfo::AppInfo cloudInfoAppInfo1; 193 cloudInfoAppInfo1.bundleName = "ohos.test.demo"; 194 cloudInfoAppInfo1.appId = "test1_id"; 195 cloudInfoAppInfo1.version = 0; 196 cloudInfoAppInfo1.instanceId = 0; 197 cloudInfoAppInfo1.cloudSwitch = false; 198 199 Serializable::json node1; 200 cloudInfoAppInfo1.Marshal(node1); 201 EXPECT_EQ(Serializable::Marshall(cloudInfoAppInfo1), to_string(node1)); 202 203 CloudInfo::AppInfo cloudInfoAppInfo2; 204 cloudInfoAppInfo2.Unmarshal(node1); 205 EXPECT_EQ(Serializable::Marshall(cloudInfoAppInfo1), Serializable::Marshall(cloudInfoAppInfo2)); 206 } 207 208 /** 209 * @tc.name: TableTest 210 * @tc.desc: Marshal and Unmarshal of Table. 211 * @tc.type: FUNC 212 * @tc.require: 213 * @tc.author: Anvette 214 */ 215 HWTEST_F(CloudInfoTest, TableTest, TestSize.Level0) 216 { 217 Field field1; 218 field1.colName = "test1_colName"; 219 field1.alias = "test1_alias"; 220 field1.type = 1; 221 field1.primary = true; 222 field1.nullable = false; 223 224 Table table1; 225 table1.name = "test1_name"; 226 table1.sharedTableName = "test1_sharedTableName"; 227 table1.alias = "test1_alias"; 228 table1.fields.push_back(field1); 229 Serializable::json node1; 230 table1.Marshal(node1); 231 EXPECT_EQ(Serializable::Marshall(table1), to_string(node1)); 232 233 Table table2; 234 table2.Unmarshal(node1); 235 EXPECT_EQ(Serializable::Marshall(table1), Serializable::Marshall(table2)); 236 }