• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
20 
21 #include "hks_api.h"
22 #include "hks_param.h"
23 #include "hks_test_api_performance.h"
24 #include "hks_test_common.h"
25 #include "hks_test_log.h"
26 #include "hks_type.h"
27 #include "hks_hash_test.h"
28 
29 #include "cmsis_os2.h"
30 #include "ohos_types.h"
31 
32 #define DEFAULT_SRC_DATA_SIZE 200
33 #define DIGEST_SHA1_HASH_SIZE 20
34 #define DIGEST_SHA224_HASH_SIZE 28
35 #define DIGEST_SHA256_HASH_SIZE 32
36 #define DIGEST_SHA384_HASH_SIZE 48
37 #define DIGEST_SHA512_HASH_SIZE 64
38 #define TEST_TASK_STACK_SIZE      0x2000
39 #define WAIT_TO_TEST_DONE         4
40 
41 static osPriority_t g_setPriority;
42 
43 /*
44  * @tc.register: register a test suit named "CalcMultiTest"
45  * @param: test subsystem name
46  * @param: c_example module name
47  * @param: CalcMultiTest test suit name
48  */
49 LITE_TEST_SUIT(security, securityData, HksHashTest);
50 
ExecHksInitialize(void const * argument)51 static void ExecHksInitialize(void const *argument)
52 {
53     LiteTestPrint("HksInitialize Begin!\n");
54     TEST_ASSERT_TRUE(HksInitialize() == 0);
55     LiteTestPrint("HksInitialize End!\n");
56     osThreadExit();
57 }
58 
59 /**
60  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
61  * @return: true——setup success
62  */
HksHashTestSetUp()63 static BOOL HksHashTestSetUp()
64 {
65     LiteTestPrint("setup\n");
66     osThreadId_t id;
67     osThreadAttr_t attr;
68     g_setPriority = osPriorityAboveNormal6;
69     attr.name = "test";
70     attr.attr_bits = 0U;
71     attr.cb_mem = NULL;
72     attr.cb_size = 0U;
73     attr.stack_mem = NULL;
74     attr.stack_size = TEST_TASK_STACK_SIZE;
75     attr.priority = g_setPriority;
76     id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
77     sleep(WAIT_TO_TEST_DONE);
78     LiteTestPrint("HksMacTestSetUp End2!\n");
79     return TRUE;
80 }
81 
82 /**
83  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
84  * @return: true——teardown success
85  */
HksHashTestTearDown()86 static BOOL HksHashTestTearDown()
87 {
88     LiteTestPrint("tearDown\n");
89     return TRUE;
90 }
91 
92 static const struct HksTestHashParams g_testHashParams[] = {
93     /* normal case */
94     { 0, HKS_SUCCESS,
95         { true, true, HKS_DIGEST_SHA256 },
96         { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE },
97         { true, DIGEST_SHA256_HASH_SIZE, true, DIGEST_SHA256_HASH_SIZE }
98     },
99     { 1, HKS_SUCCESS,
100         { true, true, HKS_DIGEST_SHA384 },
101         { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE },
102         { true, DIGEST_SHA384_HASH_SIZE, true, DIGEST_SHA384_HASH_SIZE }
103     },
104     { 2, HKS_SUCCESS,
105         { true, true, HKS_DIGEST_SHA512 },
106         { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE },
107         { true, DIGEST_SHA512_HASH_SIZE, true, DIGEST_SHA512_HASH_SIZE }
108     },
109 };
110 
ExecHksHashTestCommon(int index)111 static void ExecHksHashTestCommon(int index)
112 {
113     struct HksParamSet *paramSet = NULL;
114     struct HksBlob *srcData = NULL;
115     struct HksBlob *hash = NULL;
116 
117     int32_t ret = TestConstructHashParamSet(&paramSet,
118         g_testHashParams[index].paramSetParams.paramSetExist,
119         g_testHashParams[index].paramSetParams.setDigest, g_testHashParams[index].paramSetParams.digest);
120     TEST_ASSERT_TRUE(ret == 0);
121 
122     ret = TestConstuctBlob(&srcData,
123         g_testHashParams[index].srcDataParams.blobExist,
124         g_testHashParams[index].srcDataParams.blobSize,
125         g_testHashParams[index].srcDataParams.blobDataExist,
126         g_testHashParams[index].srcDataParams.blobDataSize);
127     TEST_ASSERT_TRUE(ret == 0);
128 
129     ret = TestConstructBlobOut(&hash,
130         g_testHashParams[index].hashParams.blobExist,
131         g_testHashParams[index].hashParams.blobSize,
132         g_testHashParams[index].hashParams.blobDataExist,
133         g_testHashParams[index].hashParams.blobDataSize);
134     TEST_ASSERT_TRUE(ret == 0);
135 
136     ret = HksHashRun(paramSet, srcData, hash, 1);
137     if (ret != g_testHashParams[index].expectResult) {
138         HKS_TEST_LOG_I("HksHashRun failed, ret[%u] = %d", g_testHashParams[index].testId, ret);
139     }
140     TEST_ASSERT_TRUE(ret == g_testHashParams[index].expectResult);
141 
142     HksFreeParamSet(&paramSet);
143     TestFreeBlob(&srcData);
144     TestFreeBlob(&hash);
145     HKS_TEST_LOG_I("[%u]TestHash, Testcase_Hash_[%03u] pass!", 1, g_testHashParams[index].testId);
146     TEST_ASSERT_TRUE(ret == 0);
147 }
148 
ExecHksHashTest001(void const * argument)149 static void ExecHksHashTest001(void const *argument)
150 {
151     LiteTestPrint("HksMacTest001 Begin!\n");
152     ExecHksHashTestCommon(0);
153     LiteTestPrint("HksMacTest001 End!\n");
154     osThreadExit();
155 }
156 
ExecHksHashTest002(void const * argument)157 static void ExecHksHashTest002(void const *argument)
158 {
159     LiteTestPrint("HksMacTest002 Begin!\n");
160     ExecHksHashTestCommon(1);
161     LiteTestPrint("HksMacTest002 End!\n");
162     osThreadExit();
163 }
164 
ExecHksHashTest003(void const * argument)165 static void ExecHksHashTest003(void const *argument)
166 {
167     LiteTestPrint("HksMacTest003 Begin!\n");
168     ExecHksHashTestCommon(2);
169     LiteTestPrint("HksMacTest003 End!\n");
170     osThreadExit();
171 }
172 
173 /**
174  * @tc.name: HksHashTest.HksHashTest001
175  * @tc.desc: The static function will return true;
176  * @tc.type: FUNC
177  */
LITE_TEST_CASE(HksHashTest,HksHashTest001,Level1)178 LITE_TEST_CASE(HksHashTest, HksHashTest001, Level1)
179 {
180     osThreadId_t id;
181     osThreadAttr_t attr;
182     g_setPriority = osPriorityAboveNormal6;
183     attr.name = "test";
184     attr.attr_bits = 0U;
185     attr.cb_mem = NULL;
186     attr.cb_size = 0U;
187     attr.stack_mem = NULL;
188     attr.stack_size = TEST_TASK_STACK_SIZE;
189     attr.priority = g_setPriority;
190     id = osThreadNew((osThreadFunc_t)ExecHksHashTest001, NULL, &attr);
191     sleep(WAIT_TO_TEST_DONE);
192     LiteTestPrint("HksMacTest001 End2!\n");
193 }
194 /**
195  * @tc.name: HksHashTest.HksHashTest002
196  * @tc.desc: The static function will return true;
197  * @tc.type: FUNC
198  */
LITE_TEST_CASE(HksHashTest,HksHashTest002,Level1)199 LITE_TEST_CASE(HksHashTest, HksHashTest002, Level1)
200 {
201     osThreadId_t id;
202     osThreadAttr_t attr;
203     g_setPriority = osPriorityAboveNormal6;
204     attr.name = "test";
205     attr.attr_bits = 0U;
206     attr.cb_mem = NULL;
207     attr.cb_size = 0U;
208     attr.stack_mem = NULL;
209     attr.stack_size = TEST_TASK_STACK_SIZE;
210     attr.priority = g_setPriority;
211     id = osThreadNew((osThreadFunc_t)ExecHksHashTest002, NULL, &attr);
212     sleep(WAIT_TO_TEST_DONE);
213     LiteTestPrint("HksMacTest001 End2!\n");
214 }
215 /**
216  * @tc.name: HksHashTest.HksHashTest003
217  * @tc.desc: The static function will return true;
218  * @tc.type: FUNC
219  */
LITE_TEST_CASE(HksHashTest,HksHashTest003,Level1)220 LITE_TEST_CASE(HksHashTest, HksHashTest003, Level1)
221 {
222     osThreadId_t id;
223     osThreadAttr_t attr;
224     g_setPriority = osPriorityAboveNormal6;
225     attr.name = "test";
226     attr.attr_bits = 0U;
227     attr.cb_mem = NULL;
228     attr.cb_size = 0U;
229     attr.stack_mem = NULL;
230     attr.stack_size = TEST_TASK_STACK_SIZE;
231     attr.priority = g_setPriority;
232     id = osThreadNew((osThreadFunc_t)ExecHksHashTest003, NULL, &attr);
233     sleep(WAIT_TO_TEST_DONE);
234     LiteTestPrint("HksMacTest001 End2!\n");
235 }
236 RUN_TEST_SUITE(HksHashTest);
237 
238 #endif /* _CUT_AUTHENTICATE_ */
239 
240