1 /*
2 * Copyright (c) 2024-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 <memory>
17 #include <gtest/gtest.h>
18 #include "fs_digest_utils.h"
19
20 namespace OHOS {
21 namespace SignatureTools {
22
23 class DigestUtilsTest : public testing::Test {
24 public:
SetUpTestCase(void)25 static void SetUpTestCase(void)
26 {
27 };
TearDownTestCase()28 static void TearDownTestCase()
29 {
30 };
SetUp()31 void SetUp()
32 {
33 };
TearDown()34 void TearDown()
35 {
36 };
37 };
38
39 /**
40 * @tc.name: addData001
41 * @tc.desc: add digest data without length
42 * @tc.size: MEDIUM
43 * @tc.type: FUNC
44 * @tc.level Level 1
45 * @tc.require: SR000H63TL
46 */
HWTEST_F(DigestUtilsTest, addData001, testing::ext::TestSize.Level1)47 HWTEST_F(DigestUtilsTest, addData001, testing::ext::TestSize.Level1)
48 {
49 std::shared_ptr<DigestUtils> api = std::make_shared<DigestUtils>(HASH_SHA256);
50
51 char charData[32] = { -66, -72, 8, -21, 1, 28, 23, 2, -1, -1, -1, -1, 15, 16, 40, -57, -34,
52 -119, 1, 6, -76, -72, 8, -66, -72, 8, -66, -72, 8, -86, -50, 8 };
53 std::string data(charData);
54 api->AddData(data);
55
56 DigestUtils::Type type = DigestUtils::Type::HEX;
57 std::string str = api->Result(type);
58
59 EXPECT_NE(str.size(), 0);
60 }
61
62 /**
63 * @tc.name: addData002
64 * @tc.desc: add digest data with length
65 * @tc.size: MEDIUM
66 * @tc.type: FUNC
67 * @tc.level Level 1
68 * @tc.require: SR000H63TL
69 */
HWTEST_F(DigestUtilsTest, addData002, testing::ext::TestSize.Level1)70 HWTEST_F(DigestUtilsTest, addData002, testing::ext::TestSize.Level1)
71 {
72 std::shared_ptr<DigestUtils> api = std::make_shared<DigestUtils>(HASH_SHA256);
73
74 char pData[32] = { -66, -72, 8, -21, 1, 28, 23, 2, -1, -1, -1, -1, 15, 16, 40, -57, -34,
75 -119, 1, 6, -76, -72, 8, -66, -72, 8, -66, -72, 8, -86, -50, 8 };
76 int length = 32;
77 api->AddData(pData, length);
78
79 DigestUtils::Type type = DigestUtils::Type::HEX;
80 std::string str = api->Result(type);
81
82 EXPECT_NE(str.size(), 0);
83 }
84
85 /**
86 * @tc.name: result
87 * @tc.desc: get digest result and size
88 * @tc.size: MEDIUM
89 * @tc.type: FUNC
90 * @tc.level Level 1
91 * @tc.require: SR000H63TL
92 */
HWTEST_F(DigestUtilsTest, result, testing::ext::TestSize.Level1)93 HWTEST_F(DigestUtilsTest, result, testing::ext::TestSize.Level1)
94 {
95 std::shared_ptr<DigestUtils> api = std::make_shared<DigestUtils>(HASH_SHA256);
96
97 DigestUtils::Type type = DigestUtils::Type::HEX;
98 std::string str = api->Result(type);
99
100 EXPECT_EQ(str.size(), 64);
101 }
102 } // namespace SignatureTools
103 } // namespace OHOS