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 <hctest.h>
19 #include "iot_watchdog.h"
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_api_performance.h"
23 #include "hks_test_common.h"
24 #include "hks_test_log.h"
25 #include "hks_type.h"
26 #include "hks_hash_test.h"
27
28 #include "cmsis_os2.h"
29 #include "ohos_types.h"
30
31 #define DEFAULT_SRC_DATA_SIZE 200
32 #define DIGEST_SHA256_HASH_SIZE 32
33 #define TEST_TASK_STACK_SIZE 0x2000
34 #define WAIT_TO_TEST_DONE 4
35
36 static osPriority_t g_setPriority;
37
38 /*
39 * @tc.register: register a test suit named "CalcMultiTest"
40 * @param: test subsystem name
41 * @param: c_example module name
42 * @param: CalcMultiTest test suit name
43 */
44 LITE_TEST_SUIT(security, securityData, HksHashTest);
45
ExecHksInitialize(void const * argument)46 static void ExecHksInitialize(void const *argument)
47 {
48 LiteTestPrint("HksInitialize Begin!\n");
49 TEST_ASSERT_TRUE(HksInitialize() == 0);
50 LiteTestPrint("HksInitialize End!\n");
51 osThreadExit();
52 }
53
54 /**
55 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
56 * @return: true——setup success
57 */
HksHashTestSetUp()58 static BOOL HksHashTestSetUp()
59 {
60 LiteTestPrint("setup\n");
61 IoTWatchDogDisable();
62 osThreadId_t id;
63 osThreadAttr_t attr;
64 g_setPriority = osPriorityAboveNormal6;
65 attr.name = "test";
66 attr.attr_bits = 0U;
67 attr.cb_mem = NULL;
68 attr.cb_size = 0U;
69 attr.stack_mem = NULL;
70 attr.stack_size = TEST_TASK_STACK_SIZE;
71 attr.priority = g_setPriority;
72 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
73 sleep(WAIT_TO_TEST_DONE);
74 LiteTestPrint("HksMacTestSetUp End2!\n");
75 return TRUE;
76 }
77
78 /**
79 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
80 * @return: true——teardown success
81 */
HksHashTestTearDown()82 static BOOL HksHashTestTearDown()
83 {
84 LiteTestPrint("tearDown\n");
85 IoTWatchDogEnable();
86 return TRUE;
87 }
88
89 static const struct HksTestHashParams g_testHashParams[] = {
90 /* normal case */
91 { 0, HKS_SUCCESS,
92 { true, true, HKS_DIGEST_SHA256 },
93 { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE },
94 { true, DIGEST_SHA256_HASH_SIZE, true, DIGEST_SHA256_HASH_SIZE }
95 },
96 };
97
ExecHksHashTest001(void const * argument)98 static void ExecHksHashTest001(void const *argument)
99 {
100 LiteTestPrint("HksMacTest001 Begin!\n");
101 struct HksParamSet *paramSet = NULL;
102 struct HksBlob *srcData = NULL;
103 struct HksBlob *hash = NULL;
104
105 int32_t ret = TestConstructHashParamSet(¶mSet,
106 g_testHashParams[0].paramSetParams.paramSetExist,
107 g_testHashParams[0].paramSetParams.setDigest, g_testHashParams[0].paramSetParams.digest);
108 TEST_ASSERT_TRUE(ret == 0);
109
110 ret = TestConstuctBlob(&srcData,
111 g_testHashParams[0].srcDataParams.blobExist,
112 g_testHashParams[0].srcDataParams.blobSize,
113 g_testHashParams[0].srcDataParams.blobDataExist,
114 g_testHashParams[0].srcDataParams.blobDataSize);
115 TEST_ASSERT_TRUE(ret == 0);
116
117 ret = TestConstructBlobOut(&hash,
118 g_testHashParams[0].hashParams.blobExist,
119 g_testHashParams[0].hashParams.blobSize,
120 g_testHashParams[0].hashParams.blobDataExist,
121 g_testHashParams[0].hashParams.blobDataSize);
122 TEST_ASSERT_TRUE(ret == 0);
123
124 ret = HksHashRun(paramSet, srcData, hash, 1);
125 if (ret != g_testHashParams[0].expectResult) {
126 HKS_TEST_LOG_I("HksHashRun failed, ret[%u] = %d", g_testHashParams[0].testId, ret);
127 }
128 TEST_ASSERT_TRUE(ret == g_testHashParams[0].expectResult);
129
130 HksFreeParamSet(¶mSet);
131 TestFreeBlob(&srcData);
132 TestFreeBlob(&hash);
133 HKS_TEST_LOG_I("[%u]TestHash, Testcase_Hash_[%03u] pass!", 1, g_testHashParams[0].testId);
134 TEST_ASSERT_TRUE(ret == 0);
135
136 LiteTestPrint("HksMacTest001 End!\n");
137 osThreadExit();
138 }
139
140 /**
141 * @tc.name: HksHashTest.HksHashTest001
142 * @tc.desc: The static function will return true;
143 * @tc.type: FUNC
144 */
LITE_TEST_CASE(HksHashTest,HksHashTest001,Level1)145 LITE_TEST_CASE(HksHashTest, HksHashTest001, Level1)
146 {
147 osThreadId_t id;
148 osThreadAttr_t attr;
149 g_setPriority = osPriorityAboveNormal6;
150 attr.name = "test";
151 attr.attr_bits = 0U;
152 attr.cb_mem = NULL;
153 attr.cb_size = 0U;
154 attr.stack_mem = NULL;
155 attr.stack_size = TEST_TASK_STACK_SIZE;
156 attr.priority = g_setPriority;
157 id = osThreadNew((osThreadFunc_t)ExecHksHashTest001, NULL, &attr);
158 sleep(WAIT_TO_TEST_DONE);
159 LiteTestPrint("HksMacTest001 End2!\n");
160 }
161 RUN_TEST_SUITE(HksHashTest);
162
163 #endif /* _CUT_AUTHENTICATE_ */
164
165