• 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 "hks_generate_key_test.h"
19 
20 #include <hctest.h>
21 #include "iot_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 "cmsis_os2.h"
28 #include "ohos_types.h"
29 
30 #define DEFAULT_X25519_PARAM_SET_OUT 104
31 #define TEST_TASK_STACK_SIZE      0x2000
32 #define WAIT_TO_TEST_DONE         4
33 
34 static osPriority_t g_setPriority;
35 
36 /*
37  * @tc.register: register a test suit named "CalcMultiTest"
38  * @param: test subsystem name
39  * @param: c_example module name
40  * @param: CalcMultiTest test suit name
41  */
42 LITE_TEST_SUIT(security, securityData, HksGenerateKeyTest);
43 
ExecHksInitialize(void const * argument)44 static void ExecHksInitialize(void const *argument)
45 {
46     LiteTestPrint("HksInitialize Begin!\n");
47     TEST_ASSERT_TRUE(HksInitialize() == 0);
48     LiteTestPrint("HksInitialize End!\n");
49     osThreadExit();
50 }
51 
52 /**
53  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
54  * @return: true——setup success
55  */
HksGenerateKeyTestSetUp()56 static BOOL HksGenerateKeyTestSetUp()
57 {
58     LiteTestPrint("setup\n");
59     IoTWatchDogDisable();
60     osThreadId_t id;
61     osThreadAttr_t attr;
62     g_setPriority = osPriorityAboveNormal6;
63     attr.name = "test";
64     attr.attr_bits = 0U;
65     attr.cb_mem = NULL;
66     attr.cb_size = 0U;
67     attr.stack_mem = NULL;
68     attr.stack_size = TEST_TASK_STACK_SIZE;
69     attr.priority = g_setPriority;
70     id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
71     sleep(WAIT_TO_TEST_DONE);
72     LiteTestPrint("HksGenerateKeyTestSetUp End2!\n");
73     return TRUE;
74 }
75 
76 /**
77  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
78  * @return: true——teardown success
79  */
HksGenerateKeyTestTearDown()80 static BOOL HksGenerateKeyTestTearDown()
81 {
82     LiteTestPrint("tearDown\n");
83     IoTWatchDogEnable();
84     return TRUE;
85 }
86 
87 
88 static const struct HksTestGenKeyParams g_testGenKeyParams[] = {
89     /* x25519: ree sign/verify */
90     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, {
91         true,
92         true, HKS_ALG_X25519,
93         true, HKS_CURVE25519_KEY_SIZE_256,
94         true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
95         false, 0,
96         false, 0,
97         false, 0,
98         true, HKS_STORAGE_TEMP },
99         { true, DEFAULT_X25519_PARAM_SET_OUT },
100     },
101 };
102 
ExecHksGenerateKeyTest001(void const * argument)103 static void ExecHksGenerateKeyTest001(void const *argument)
104 {
105     LiteTestPrint("HksGenerateKeyTest001 Begin!\n");
106 
107     uint32_t times = 1;
108     uint32_t index = 0;
109     uint32_t performTimes = 1;
110     struct HksBlob *keyAlias = NULL;
111     int32_t ret = TestConstuctBlob(&keyAlias,
112         g_testGenKeyParams[index].keyAliasParams.blobExist,
113         g_testGenKeyParams[index].keyAliasParams.blobSize,
114         g_testGenKeyParams[index].keyAliasParams.blobDataExist,
115         g_testGenKeyParams[index].keyAliasParams.blobDataSize);
116     TEST_ASSERT_TRUE(ret == 0);
117 
118     struct HksParamSet *paramSet = NULL;
119     struct GenerateKeyParamSetStructure paramStruct = { &paramSet,
120         g_testGenKeyParams[index].paramSetParams.paramSetExist,
121         g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
122         g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
123         g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
124         g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
125         g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
126         g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
127         g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
128         g_testGenKeyParams[index].paramSetParams.keyStorageFlag };
129     ret = TestConstructGenerateKeyParamSet(&paramStruct);
130     TEST_ASSERT_TRUE(ret == 0);
131 
132     struct HksParamSet *paramSetOut = NULL;
133     ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
134         g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
135         g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
136     TEST_ASSERT_TRUE(ret == 0);
137 
138     ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
139     if (ret != g_testGenKeyParams[index].expectResult) {
140         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
141     }
142     TEST_ASSERT_TRUE(ret == g_testGenKeyParams[index].expectResult);
143 
144     if ((ret == HKS_SUCCESS) &&
145         !(g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag == true) &&
146         (g_testGenKeyParams[index].paramSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
147         HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0);
148     }
149     TestFreeBlob(&keyAlias);
150     HksFreeParamSet(&paramSet);
151     HksFreeParamSet(&paramSetOut);
152     HKS_TEST_LOG_I("[%u]TestGenerateKey, Testcase_GenerateKey_[%03u] pass!", times, g_testGenKeyParams[index].testId);
153     TEST_ASSERT_TRUE(ret == 0);
154 
155     LiteTestPrint("HksGenerateKeyTest001 End!\n");
156     osThreadExit();
157 }
158 
159 /**
160  * @tc.name: HksGenerateKeyTest.HksGenerateKeyTest001
161  * @tc.desc: The static function will return true;
162  * @tc.type: FUNC
163  */
LITE_TEST_CASE(HksGenerateKeyTest,HksGenerateKeyTest001,Level1)164 LITE_TEST_CASE(HksGenerateKeyTest, HksGenerateKeyTest001, Level1)
165 {
166     osThreadId_t id;
167     osThreadAttr_t attr;
168     g_setPriority = osPriorityAboveNormal6;
169     attr.name = "test";
170     attr.attr_bits = 0U;
171     attr.cb_mem = NULL;
172     attr.cb_size = 0U;
173     attr.stack_mem = NULL;
174     attr.stack_size = TEST_TASK_STACK_SIZE;
175     attr.priority = g_setPriority;
176     id = osThreadNew((osThreadFunc_t)ExecHksGenerateKeyTest001, NULL, &attr);
177     sleep(WAIT_TO_TEST_DONE);
178     LiteTestPrint("HksGenerateKeyTest001 End2!\n");
179 }
180 
181 RUN_TEST_SUITE(HksGenerateKeyTest);
182 #endif /* _CUT_AUTHENTICATE_ */
183 
184