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_Structuralize_0100 64 * @tc.name: b_json_Structuralize_0100 65 * @tc.desc: Test function of Structuralize 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_Structuralize_0100, testing::ext::TestSize.Level0) 72 { 73 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_Structuralize_0100"; 74 try { 75 TestManager tm("b_json_Structuralize_0100"); 76 77 std::string path = tm.GetRootDirCurTest(); 78 std::string filePath = path + ".json"; 79 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 80 jce.Structuralize(); 81 } catch (...) { 82 EXPECT_TRUE(false); 83 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 84 } 85 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_Structuralize_0100"; 86 } 87 88 /** 89 * @tc.number: SUB_backup_b_json_GetFd_0100 90 * @tc.name: b_json_GetFd_0100 91 * @tc.desc: Test function of GetFd interface for SUCCESS. 92 * @tc.size: MEDIUM 93 * @tc.type: FUNC 94 * @tc.level Level 0 95 * @tc.require: I6F3GV 96 */ 97 HWTEST_F(BJsonCachedEntityTest, b_json_GetFd_0100, testing::ext::TestSize.Level0) 98 { 99 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-begin b_json_GetFd_0100"; 100 try { 101 TestManager tm("b_json_GetFd_0100"); 102 103 std::string path = tm.GetRootDirCurTest(); 104 std::string filePath = path + ".json"; 105 BJsonCachedEntity<BJsonEntityCaps> jce(UniqueFd(open(filePath.data(), O_RDWR | O_CREAT, 0600))); 106 jce.GetFd(); 107 } catch (...) { 108 EXPECT_TRUE(false); 109 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-an exception occurred."; 110 } 111 GTEST_LOG_(INFO) << "BJsonCachedEntityTest-end b_json_GetFd_0100"; 112 } 113 } // namespace OHOS::FileManagement::Backup