1 /* 2 * Copyright (c) 2022-2023 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 <cstdio> 17 #include <fcntl.h> 18 #include <gtest/gtest.h> 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 22 #include "b_json/b_json_cached_entity.h" 23 #include "b_json/b_json_entity_caps.h" 24 #include "directory_ex.h" 25 #include "file_ex.h" 26 #include "test_manager.h" 27 28 namespace OHOS::FileManagement::Backup { 29 class BJsonCachedEntityTest : public testing::Test { 30 public: SetUpTestCase(void)31 static void SetUpTestCase(void) {}; TearDownTestCase()32 static void TearDownTestCase() {}; SetUp()33 void SetUp() {}; TearDown()34 void TearDown() {}; 35 }; 36 37 /** 38 * @tc.number: SUB_backup_b_json_construction_0100 39 * @tc.name: b_json_construction_0100 40 * @tc.desc: Test function of construction interface for SUCCESS. 41 * @tc.size: MEDIUM 42 * @tc.type: FUNC 43 * @tc.level Level 0 44 * @tc.require: I6F3GV 45 */ 46 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0100, testing::ext::TestSize.Level0) 47 { 48 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0100"; 49 try { 50 TestManager tm("b_json_construction_0100"); 51 52 std::string path = tm.GetRootDirCurTest(); 53 std::string filePath = path + ".json"; 54 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 55 } catch (...) { 56 EXPECT_TRUE(false); 57 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 58 } 59 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0100"; 60 } 61 62 /** 63 * @tc.number: SUB_backup_b_json_construction_0101 64 * @tc.name: b_json_construction_0101 65 * @tc.desc: Test function of construction interface for SUCCESS. 66 * @tc.size: MEDIUM 67 * @tc.type: FUNC 68 * @tc.level Level 0 69 * @tc.require: I6F3GV 70 */ 71 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0101, testing::ext::TestSize.Level0) 72 { 73 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0101"; 74 try { 75 TestManager tm("b_json_construction_0101"); 76 77 std::string path = tm.GetRootDirCurTest(); 78 std::string filePath = path + ""; 79 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 80 } catch (...) { 81 EXPECT_TRUE(false); 82 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 83 } 84 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0101"; 85 } 86 87 /** 88 * @tc.number: SUB_backup_b_json_construction_0102 89 * @tc.name: b_json_construction_0102 90 * @tc.desc: Test function of construction interface for SUCCESS. 91 * @tc.size: MEDIUM 92 * @tc.type: FUNC 93 * @tc.level Level 0 94 * @tc.require: I6F3GV 95 */ 96 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0102, testing::ext::TestSize.Level0) 97 { 98 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0102"; 99 try { 100 TestManager tm("b_json_construction_0102"); 101 102 std::string path = tm.GetRootDirCurTest(); 103 std::string filePath = path + ".json"; 104 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, NULL))); 105 } catch (...) { 106 EXPECT_TRUE(false); 107 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 108 } 109 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0102"; 110 } 111 112 /** 113 * @tc.number: SUB_backup_b_json_construction_0104 114 * @tc.name: b_json_construction_0104 115 * @tc.desc: Test function of construction interface for SUCCESS. 116 * @tc.size: MEDIUM 117 * @tc.type: FUNC 118 * @tc.level Level 0 119 * @tc.require: I6F3GV 120 */ 121 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0104, testing::ext::TestSize.Level0) 122 { 123 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0104"; 124 try { 125 TestManager tm("b_json_construction_0104"); 126 127 std::string path = tm.GetRootDirCurTest(); 128 std::string filePath = path + ".json"; 129 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDONLY, NULL))); 130 } catch (...) { 131 EXPECT_TRUE(false); 132 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 133 } 134 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0104"; 135 } 136 137 /** 138 * @tc.number: SUB_backup_b_json_construction_0105 139 * @tc.name: b_json_construction_0105 140 * @tc.desc: Test function of construction interface for SUCCESS. 141 * @tc.size: MEDIUM 142 * @tc.type: FUNC 143 * @tc.level Level 0 144 * @tc.require: I6F3GV 145 */ 146 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0105, testing::ext::TestSize.Level0) 147 { 148 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0105"; 149 try { 150 TestManager tm("b_json_construction_0105"); 151 152 std::string path = tm.GetRootDirCurTest(); 153 std::string filePath = path + ""; 154 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, NULL))); 155 } catch (...) { 156 EXPECT_TRUE(false); 157 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 158 } 159 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0105"; 160 } 161 162 /** 163 * @tc.number: SUB_backup_b_json_construction_0106 164 * @tc.name: b_json_construction_0106 165 * @tc.desc: Test function of construction interface for SUCCESS. 166 * @tc.size: MEDIUM 167 * @tc.type: FUNC 168 * @tc.level Level 0 169 * @tc.require: I6F3GV 170 */ 171 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0106, testing::ext::TestSize.Level0) 172 { 173 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0106"; 174 try { 175 TestManager tm("b_json_construction_0106"); 176 177 std::string path = tm.GetRootDirCurTest(); 178 std::string filePath = path + ""; 179 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDONLY, 0600))); 180 } catch (...) { 181 EXPECT_TRUE(true); 182 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 183 } 184 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0106"; 185 } 186 187 /** 188 * @tc.number: SUB_backup_b_json_construction_0107 189 * @tc.name: b_json_construction_0107 190 * @tc.desc: Test function of construction interface for SUCCESS. 191 * @tc.size: MEDIUM 192 * @tc.type: FUNC 193 * @tc.level Level 0 194 * @tc.require: I6F3GV 195 */ 196 HWTEST_F(BJsonCachedEntityTest, b_json_construction_0107, testing::ext::TestSize.Level0) 197 { 198 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_construction_0107"; 199 try { 200 TestManager tm("b_json_construction_0107"); 201 202 std::string path = tm.GetRootDirCurTest(); 203 std::string filePath = path + ""; 204 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDONLY, NULL))); 205 } catch (...) { 206 EXPECT_TRUE(true); 207 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred by construction."; 208 } 209 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_construction_0107"; 210 } 211 212 /** 213 * @tc.number: SUB_backup_b_json_Structuralize_0100 214 * @tc.name: b_json_Structuralize_0100 215 * @tc.desc: Test function of Structuralize interface for SUCCESS. 216 * @tc.size: MEDIUM 217 * @tc.type: FUNC 218 * @tc.level Level 0 219 * @tc.require: I6F3GV 220 */ 221 HWTEST_F(BJsonCachedEntityTest, b_json_Structuralize_0100, testing::ext::TestSize.Level0) 222 { 223 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_Structuralize_0100"; 224 try { 225 TestManager tm("b_json_Structuralize_0100"); 226 227 std::string path = tm.GetRootDirCurTest(); 228 std::string filePath = path + ".json"; 229 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 230 jce.Structuralize(); 231 } catch (...) { 232 EXPECT_TRUE(false); 233 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 234 } 235 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_Structuralize_0100"; 236 } 237 238 /** 239 * @tc.number: SUB_backup_b_json_GetFd_0100 240 * @tc.name: b_json_GetFd_0100 241 * @tc.desc: Test function of GetFd interface for SUCCESS. 242 * @tc.size: MEDIUM 243 * @tc.type: FUNC 244 * @tc.level Level 0 245 * @tc.require: I6F3GV 246 */ 247 HWTEST_F(BJsonCachedEntityTest, b_json_GetFd_0100, testing::ext::TestSize.Level0) 248 { 249 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_GetFd_0100"; 250 try { 251 TestManager tm("b_json_GetFd_0100"); 252 253 std::string path = tm.GetRootDirCurTest(); 254 std::string filePath = path + ".json"; 255 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 256 jce.GetFd(); 257 } catch (...) { 258 EXPECT_TRUE(false); 259 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 260 } 261 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_GetFd_0100"; 262 } 263 264 /** 265 * @tc.number: SUB_backup_b_json_Persist_0100 266 * @tc.name: b_json_Persist_0100 267 * @tc.desc: Test function of Persist interface for SUCCESS. 268 * @tc.size: MEDIUM 269 * @tc.type: FUNC 270 * @tc.level Level 0 271 * @tc.require: I6F3GV 272 */ 273 HWTEST_F(BJsonCachedEntityTest, b_json_Persist_0100, testing::ext::TestSize.Level0) 274 { 275 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_Persist_0100"; 276 try { 277 TestManager tm("b_json_Persist_0100"); 278 279 std::string path = tm.GetRootDirCurTest(); 280 std::string filePath = path + ".json"; 281 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 282 jce.Persist(); 283 } catch (...) { 284 EXPECT_TRUE(false); 285 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 286 } 287 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_Persist_0100"; 288 } 289 290 /** 291 * @tc.number: SUB_backup_b_json_ReloadFromFile_0100 292 * @tc.name: b_json_ReloadFromFile_0100 293 * @tc.desc: Test function of ReloadFromFile interface for SUCCESS. 294 * @tc.size: MEDIUM 295 * @tc.type: FUNC 296 * @tc.level Level 0 297 * @tc.require: I6F3GV 298 */ 299 HWTEST_F(BJsonCachedEntityTest, b_json_ReloadFromFile_0100, testing::ext::TestSize.Level0) 300 { 301 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_ReloadFromFile_0100"; 302 try { 303 TestManager tm("b_json_ReloadFromFile_0100"); 304 305 std::string path = tm.GetRootDirCurTest(); 306 std::string filePath = path + ".json"; 307 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 308 int ret = jce.ReloadFromFile(); 309 EXPECT_EQ(ret, 0); 310 } catch (...) { 311 EXPECT_TRUE(false); 312 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 313 } 314 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_ReloadFromFile_0100"; 315 } 316 317 /** 318 * @tc.number: SUB_backup_b_json_ReloadFromString_0100 319 * @tc.name: b_json_ReloadFromString_0100 320 * @tc.desc: Test function of ReloadFromString interface for SUCCESS. 321 * @tc.size: MEDIUM 322 * @tc.type: FUNC 323 * @tc.level Level 0 324 * @tc.require: I6F3GV 325 */ 326 HWTEST_F(BJsonCachedEntityTest, b_json_ReloadFromString_0100, testing::ext::TestSize.Level0) 327 { 328 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_ReloadFromString_0100"; 329 try { 330 TestManager tm("b_json_ReloadFromString_0100"); 331 332 std::string path = tm.GetRootDirCurTest(); 333 std::string filePath = path + ".json"; 334 std::string_view sv = R"({"key":1})"; 335 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 336 jce.ReloadFromString(sv); 337 } catch (...) { 338 EXPECT_TRUE(true); 339 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 340 } 341 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_ReloadFromString_0100"; 342 } 343 344 } // namespace OHOS::FileManagement::Backup