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