• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <unistd.h>
19 #include "hash_core.h"
20 
21 namespace OHOS {
22 namespace FileManagement {
23 namespace ModuleFileIO {
24 using namespace std;
25 
26 static const string g_filePath = "/data/test/HashCoreTest.txt";
27 class HashCoreTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void)
30     {
31         int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644);
32         close(fd);
33     };
TearDownTestCase()34     static void TearDownTestCase()
35     {
36         rmdir(g_filePath.c_str());
37     };
SetUp()38     void SetUp() {};
TearDown()39     void TearDown() {};
40 };
41 
42 /**
43  * @tc.name: DoHashTest_0001
44  * @tc.desc: Test function of DoHash() interface for invalid alg.
45  * @tc.size: MEDIUM
46  * @tc.type: FUNC
47  * @tc.level Level 1
48  */
49 HWTEST_F(HashCoreTest, DoHashTest_0001, testing::ext::TestSize.Level1)
50 {
51     GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0001";
52     string alg = "sha128";
53     auto ret = HashCore::DoHash(g_filePath, alg);
54     EXPECT_FALSE(ret.IsSuccess());
55 
56     auto err = ret.GetError();
57     EXPECT_EQ(err.GetErrNo(), 13900020);
58 
59     GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0001";
60 }
61 
62 /**
63  * @tc.name: DoHashTest_0002
64  * @tc.desc: Test function of DoHash() interface for md5 success.
65  * @tc.size: MEDIUM
66  * @tc.type: FUNC
67  * @tc.level Level 1
68  */
69 HWTEST_F(HashCoreTest, DoHashTest_0002, testing::ext::TestSize.Level1)
70 {
71     GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0002";
72     auto ret = HashCore::DoHash(g_filePath, "md5");
73     ASSERT_TRUE(ret.IsSuccess());
74 
75     GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0002";
76 }
77 
78 /**
79  * @tc.name: DoHashTest_0003
80  * @tc.desc: Test function of DoHash() interface for sha1 success.
81  * @tc.size: MEDIUM
82  * @tc.type: FUNC
83  * @tc.level Level 1
84  */
85 HWTEST_F(HashCoreTest, DoHashTest_0003, testing::ext::TestSize.Level1)
86 {
87     GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0003";
88     auto ret = HashCore::DoHash(g_filePath, "sha1");
89     ASSERT_TRUE(ret.IsSuccess());
90 
91     GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0003";
92 }
93 
94 /**
95  * @tc.name: DoHashTest_0004
96  * @tc.desc: Test function of DoHash() interface for sha256 success.
97  * @tc.size: MEDIUM
98  * @tc.type: FUNC
99  * @tc.level Level 1
100  */
101 HWTEST_F(HashCoreTest, DoHashTest_0004, testing::ext::TestSize.Level1)
102 {
103     GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0004";
104     auto ret = HashCore::DoHash(g_filePath, "sha256");
105     ASSERT_TRUE(ret.IsSuccess());
106 
107     GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0004";
108 }
109 
110 /**
111  * @tc.name: DoHashTest_0005
112  * @tc.desc: Test function of DoHash() interface for no such file or directory.
113  * @tc.size: MEDIUM
114  * @tc.type: FUNC
115  * @tc.level Level 1
116  */
117 HWTEST_F(HashCoreTest, DoHashTest_0005, testing::ext::TestSize.Level1)
118 {
119     GTEST_LOG_(INFO) << "HashCoreTest-begin DoHashTest_0005";
120     auto ret = HashCore::DoHash("/data/local/tmp/azuxyicayhyskjeh", "sha256");
121     EXPECT_FALSE(ret.IsSuccess());
122 
123     auto err = ret.GetError();
124     EXPECT_EQ(err.GetErrNo(), 13900002);
125 
126     GTEST_LOG_(INFO) << "HashCoreTest-end DoHashTest_0005";
127 }
128 
129 } // namespace ModuleFileIO
130 } // namespace FileManagement
131 } // namespace OHOS