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