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