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 #include <cstdio>
17 #include <tuple>
18 
19 #include <gtest/gtest.h>
20 #include <fcntl.h>
21 #include <file_ex.h>
22 
23 #include "b_filesystem/b_file_hash.h"
24 #include "test_manager.h"
25 
26 namespace OHOS::FileManagement::Backup {
27 using namespace std;
28 
29 class BFileHashTest : 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  * @brief 创建测试文件
39  *
40  * @return tuple<bool, string, string> 创建结果、文件路径、文件内容
41  */
GetTestFile(const TestManager & tm)42 static tuple<string, string> GetTestFile(const TestManager &tm)
43 {
44     string path = tm.GetRootDirCurTest();
45     string filePath = path + "temp.txt";
46     string content = "backup test";
47     if (bool contentCreate = SaveStringToFile(filePath, content, true); !contentCreate) {
48         throw system_error(errno, system_category());
49     }
50     return {filePath, content};
51 }
52 
53 /**
54  * @tc.number: SUB_backup_b_file_hash_HashWithSHA256_0100
55  * @tc.name: b_file_hash_HashWithSHA256_0100
56  * @tc.desc: Test function of HashWithSHA256 interface for SUCCESS.
57  * @tc.size: MEDIUM
58  * @tc.type: FUNC
59  * @tc.level Level 1
60  */
61 HWTEST_F(BFileHashTest, b_file_hash_HashWithSHA256_0100, testing::ext::TestSize.Level0)
62 {
63     GTEST_LOG_(INFO) << "BFileHashTest-begin b_file_hash_HashWithSHA256_0100";
64     try {
65         TestManager tm(__func__);
66         const auto [filePath, content] = GetTestFile(tm);
67 
68         auto [res, fileHash] = BackupFileHash::HashWithSHA256(filePath);
69 
70         EXPECT_EQ(res, 0);
71     } catch (const exception &e) {
72         GTEST_LOG_(INFO) << "BFileHashTest-an exception occurred by HashWithSHA256.";
73         e.what();
74     }
75     GTEST_LOG_(INFO) << "BFileHashTest-end b_file_hash_HashWithSHA256_0100";
76 }
77 
78 /**
79  * @tc.number: SUB_backup_b_file_hash_HashWithSHA256_0101
80  * @tc.name: b_file_hash_HashWithSHA256_0100
81  * @tc.desc: Test function of HashWithSHA256 interface for SUCCESS.
82  * @tc.size: MEDIUM
83  * @tc.type: FUNC
84  * @tc.level Level 1
85  */
86 HWTEST_F(BFileHashTest, b_file_hash_HashWithSHA256_0101, testing::ext::TestSize.Level0)
87 {
88     GTEST_LOG_(INFO) << "BFileHashTest-begin b_file_hash_HashWithSHA256_0101";
89     try {
90         std::string filePath = "/errPath";
91         auto [res, fileHash] = BackupFileHash::HashWithSHA256(filePath);
92         EXPECT_NE(res, 0);
93     } catch (const exception &e) {
94         GTEST_LOG_(INFO) << "BFileHashTest-an exception occurred by HashWithSHA256.";
95         e.what();
96     }
97     GTEST_LOG_(INFO) << "BFileHashTest-end b_file_hash_HashWithSHA256_0101";
98 }
99 } // namespace OHOS::FileManagement::Backup