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 
20 #include "hks_api.h"
21 #include "hks_exist_test.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 "cmsis_os2.h"
28 #include "ohos_types.h"
29 
30 #include <unistd.h>
31 
32 #define TEST_TASK_STACK_SIZE      0x2000
33 #define WAIT_TO_TEST_DONE         4
34 
35 static osPriority_t g_setPriority;
36 static const struct HksTestKeyExistParams g_testKeyExistParams[] = {
37     /* normal case */
38     { 0, HKS_SUCCESS, true, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE } },
39 };
40 
41 
42 /*
43  * @tc.register: register a test suit named "CalcMultiTest"
44  * @param: test subsystem name
45  * @param: c_example module name
46  * @param: CalcMultiTest test suit name
47  */
48 LITE_TEST_SUIT(security, securityData, HksExistTest);
49 
ExecHksInitialize(void const *argument)50 static void ExecHksInitialize(void const *argument)
51 {
52     (void)argument;
53     LiteTestPrint("HksInitialize Begin!\n");
54     TEST_ASSERT_TRUE(HksInitialize() == 0);
55     LiteTestPrint("HksInitialize End!\n");
56     osThreadExit();
57 }
58 
ExecHksExistTest001(void const *argument)59 static void ExecHksExistTest001(void const *argument)
60 {
61     (void)argument;
62     int32_t ret;
63     LiteTestPrint("HksExistTest001 Begin!\n");
64     struct HksBlob *keyAlias = NULL;
65     if (g_testKeyExistParams[0].isGenKey) {
66         TEST_ASSERT_TRUE(TestGenDefaultKeyAndGetAlias(&keyAlias) == 0);
67         ret = HksKeyExistRun(keyAlias, 1);
68         TEST_ASSERT_TRUE(ret == g_testKeyExistParams[0].expectResult);
69         TEST_ASSERT_TRUE(HksDeleteKey(keyAlias, NULL) == HKS_SUCCESS);
70     } else {
71         ret = TestConstuctBlob(&keyAlias,
72                                g_testKeyExistParams[0].keyAliasParams.blobExist,
73                                g_testKeyExistParams[0].keyAliasParams.blobSize,
74                                g_testKeyExistParams[0].keyAliasParams.blobDataExist,
75                                g_testKeyExistParams[0].keyAliasParams.blobDataSize);
76         TEST_ASSERT_TRUE(ret == 0);
77         ret = HksKeyExistRun(keyAlias, 1);
78         if (ret != g_testKeyExistParams[0].expectResult) {
79             HKS_TEST_LOG_I("HksKeyExistRun 2 failed, ret[%u] = %d", g_testKeyExistParams[0].testId, ret);
80         }
81         TEST_ASSERT_TRUE(ret == g_testKeyExistParams[0].expectResult);
82     }
83     TestFreeBlob(&keyAlias);
84     TEST_ASSERT_TRUE(ret == 0);
85 
86     LiteTestPrint("HksExistTest001 End!\n");
87     osThreadExit();
88 }
89 /**
90  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
91  * @return: true——setup success
92  */
HksExistTestSetUp(void)93 static BOOL HksExistTestSetUp(void)
94 {
95     LiteTestPrint("setup\n");
96     osThreadId_t id;
97     osThreadAttr_t attr;
98     g_setPriority = osPriorityAboveNormal6;
99     attr.name = "test";
100     attr.attr_bits = 0U;
101     attr.cb_mem = NULL;
102     attr.cb_size = 0U;
103     attr.stack_mem = NULL;
104     attr.stack_size = TEST_TASK_STACK_SIZE;
105     attr.priority = g_setPriority;
106     id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
107     if (id == NULL) {
108         printf("Failed to create thread ExecHksInitialize!\n");
109     }
110     osDelay(WAIT_TO_TEST_DONE);
111     LiteTestPrint("HksGenerateKeyTestSetUp End2!\n");
112     return TRUE;
113 }
114 
115 /**
116  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
117  * @return: true——teardown success
118  */
HksExistTestTearDown(void)119 static BOOL HksExistTestTearDown(void)
120 {
121     LiteTestPrint("tearDown\n");
122     return TRUE;
123 }
124 
125 /**
126  * @tc.name: HksExistTest.HksExistTest001
127  * @tc.desc: The static function will return true;
128  * @tc.type: FUNC
129  */
LITE_TEST_CASEnull130 LITE_TEST_CASE(HksExistTest, HksExistTest001, Level1)
131 {
132     osThreadId_t id;
133     osThreadAttr_t attr;
134     g_setPriority = osPriorityAboveNormal6;
135     attr.name = "test";
136     attr.attr_bits = 0U;
137     attr.cb_mem = NULL;
138     attr.cb_size = 0U;
139     attr.stack_mem = NULL;
140     attr.stack_size = TEST_TASK_STACK_SIZE;
141     attr.priority = g_setPriority;
142     id = osThreadNew((osThreadFunc_t)ExecHksExistTest001, NULL, &attr);
143     if (id == NULL) {
144         printf("Failed to create thread ExecHksExistTest001!\n");
145     }
146     osDelay(WAIT_TO_TEST_DONE);
147     LiteTestPrint("HksExistTest001 End2!\n");
148 }
149 RUN_TEST_SUITE(HksExistTest);
150 #endif /* _CUT_AUTHENTICATE_ */
151 
152