1 /*
2 * Copyright (C) 2021 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 #ifndef _CUT_AUTHENTICATE_
17
18 #include "hks_hash_test.h"
19
20 #include <hctest.h>
21 #include "hi_watchdog.h"
22 #include "hks_api.h"
23 #include "hks_param.h"
24 #include "hks_test_api_performance.h"
25 #include "hks_test_common.h"
26 #include "hks_test_log.h"
27 #include "hks_type.h"
28
29
30 #define DEFAULT_SRC_DATA_SIZE 200
31
32 #define DIGEST_SHA256_HASH_SIZE 32
33
34 /*
35 * @tc.register: register a test suit named "CalcMultiTest"
36 * @param: test subsystem name
37 * @param: c_example module name
38 * @param: CalcMultiTest test suit name
39 */
40 LITE_TEST_SUIT(husk, huks_lite, HksHashTest);
41
42 /**
43 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
44 * @return: true——setup success
45 */
HksHashTestSetUp()46 static BOOL HksHashTestSetUp()
47 {
48 LiteTestPrint("setup\n");
49 hi_watchdog_disable();
50 TEST_ASSERT_TRUE(HksInitialize() == 0);
51 return TRUE;
52 }
53
54 /**
55 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
56 * @return: true——teardown success
57 */
HksHashTestTearDown()58 static BOOL HksHashTestTearDown()
59 {
60 LiteTestPrint("tearDown\n");
61 hi_watchdog_enable();
62 return TRUE;
63 }
64
65
66 static const struct HksTestHashParams g_testHashParams[] = {
67 /* normal case */
68 { 0, HKS_SUCCESS,
69 { true, true, HKS_DIGEST_SHA256},
70 { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE },
71 { true, DIGEST_SHA256_HASH_SIZE, true, DIGEST_SHA256_HASH_SIZE }
72 },
73 };
74
75 /**
76 * @tc.name: HksHashTest.HksHashTest001
77 * @tc.desc: The static function will return true;
78 * @tc.type: FUNC
79 */
LITE_TEST_CASE(HksHashTest,HksHashTest001,Level1)80 LITE_TEST_CASE(HksHashTest, HksHashTest001, Level1)
81 {
82 struct HksParamSet *paramSet = NULL;
83 struct HksBlob *srcData = NULL;
84 struct HksBlob *hash = NULL;
85
86 int32_t ret = TestConstructHashParamSet(¶mSet,
87 g_testHashParams[0].paramSetParams.paramSetExist,
88 g_testHashParams[0].paramSetParams.setDigest, g_testHashParams[0].paramSetParams.digest);
89 TEST_ASSERT_TRUE(ret == 0);
90
91 ret = TestConstuctBlob(&srcData,
92 g_testHashParams[0].srcDataParams.blobExist,
93 g_testHashParams[0].srcDataParams.blobSize,
94 g_testHashParams[0].srcDataParams.blobDataExist,
95 g_testHashParams[0].srcDataParams.blobDataSize);
96 TEST_ASSERT_TRUE(ret == 0);
97
98 ret = TestConstructBlobOut(&hash,
99 g_testHashParams[0].hashParams.blobExist,
100 g_testHashParams[0].hashParams.blobSize,
101 g_testHashParams[0].hashParams.blobDataExist,
102 g_testHashParams[0].hashParams.blobDataSize);
103 TEST_ASSERT_TRUE(ret == 0);
104
105 ret = HksHashRun(paramSet, srcData, hash, 1);
106 if (ret != g_testHashParams[0].expectResult) {
107 HKS_TEST_LOG_I("HksHashRun failed, ret[%u] = %d", g_testHashParams[0].testId, ret);
108 }
109 TEST_ASSERT_TRUE(ret == g_testHashParams[0].expectResult);
110
111 HksFreeParamSet(¶mSet);
112 TestFreeBlob(&srcData);
113 TestFreeBlob(&hash);
114 HKS_TEST_LOG_I("[%u]TestHash, Testcase_Hash_[%03u] pass!", 1, g_testHashParams[0].testId);
115 TEST_ASSERT_TRUE(ret == 0);
116 }
117 RUN_TEST_SUITE(HksHashTest);
118
119 #endif /* _CUT_AUTHENTICATE_ */
120
121