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 #include "hks_generate_random_test.h"
17 #include "hctest.h"
18
19 #include "hks_api.h"
20 #include "hks_param.h"
21 #include "hks_test_api_performance.h"
22 #include "hks_test_common.h"
23 #include "hks_test_log.h"
24 #include "hks_type.h"
25
26 #include "cmsis_os2.h"
27 #include "ohos_types.h"
28
29 #include <unistd.h>
30
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, HksGenerateRandomTest);
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 */
HksGenerateRandomTestSetUp()56 static BOOL HksGenerateRandomTestSetUp()
57 {
58 LiteTestPrint("setup\n");
59 osThreadId_t id;
60 osThreadAttr_t attr;
61 g_setPriority = osPriorityAboveNormal6;
62 attr.name = "test";
63 attr.attr_bits = 0U;
64 attr.cb_mem = NULL;
65 attr.cb_size = 0U;
66 attr.stack_mem = NULL;
67 attr.stack_size = TEST_TASK_STACK_SIZE;
68 attr.priority = g_setPriority;
69 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
70 sleep(WAIT_TO_TEST_DONE);
71 LiteTestPrint("HksGenerateRandomTestSetUp End2!\n");
72 return TRUE;
73 }
74
75 /**
76 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
77 * @return: true——teardown success
78 */
HksGenerateRandomTestTearDown()79 static BOOL HksGenerateRandomTestTearDown()
80 {
81 LiteTestPrint("tearDown\n");
82 return TRUE;
83 }
84
85 static const struct HksTestGenRandomParams g_testGenRandomParams[] = {
86 /* normal case */
87 { 0, HKS_SUCCESS, { true, HKS_MAX_RANDOM_LEN, true, HKS_MAX_RANDOM_LEN } },
88 };
89
ExecHksGenerateRandomTest001(void const * argument)90 static void ExecHksGenerateRandomTest001(void const *argument)
91 {
92 int32_t ret;
93 struct HksBlob *random = NULL;
94 LiteTestPrint("HksGenerateRandomTest001 Begin!\n");
95
96 ret = TestConstructBlobOut(&random,
97 g_testGenRandomParams[0].randomParams.blobExist,
98 g_testGenRandomParams[0].randomParams.blobSize,
99 g_testGenRandomParams[0].randomParams.blobDataExist,
100 g_testGenRandomParams[0].randomParams.blobDataSize);
101 TEST_ASSERT_TRUE(ret == 0);
102
103 ret = HksGenerateRandomRun(random, 1);
104 if (ret != g_testGenRandomParams[0].expectResult) {
105 HKS_TEST_LOG_I("HksGenerateRandomRun failed, ret[%u] = %d", g_testGenRandomParams[0].testId, ret);
106 }
107 TEST_ASSERT_TRUE(ret == g_testGenRandomParams[0].expectResult);
108
109 TestFreeBlob(&random);
110 TEST_ASSERT_TRUE(ret == 0);
111
112 LiteTestPrint("HksGenerateRandomTest001 End!\n");
113 osThreadExit();
114 }
115
116 /**
117 * @tc.name: HksGenerateRandomTest.HksGenerateRandomTest001
118 * @tc.desc: The static function will return true;
119 * @tc.type: FUNC
120 */
LITE_TEST_CASE(HksGenerateRandomTest,HksGenerateRandomTest001,Level1)121 LITE_TEST_CASE(HksGenerateRandomTest, HksGenerateRandomTest001, Level1)
122 {
123 osThreadId_t id;
124 osThreadAttr_t attr;
125 g_setPriority = osPriorityAboveNormal6;
126 attr.name = "test";
127 attr.attr_bits = 0U;
128 attr.cb_mem = NULL;
129 attr.cb_size = 0U;
130 attr.stack_mem = NULL;
131 attr.stack_size = TEST_TASK_STACK_SIZE;
132 attr.priority = g_setPriority;
133 id = osThreadNew((osThreadFunc_t)ExecHksGenerateRandomTest001, NULL, &attr);
134 sleep(WAIT_TO_TEST_DONE);
135 LiteTestPrint("HksGenerateRandomTest001 End2!\n");
136 }
137
138 RUN_TEST_SUITE(HksGenerateRandomTest);
139