• 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 #include "iot_watchdog.h"
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 #define TEST_TASK_STACK_SIZE      0x2000
30 #define WAIT_TO_TEST_DONE         4
31 
32 static osPriority_t g_setPriority;
33 
34 /*
35  * @tc.register: register a test suit named "CalcMultiTest"
36  * @param: test subsystem name
37  * @param: c_example module name
38  * @param: CalcMultiTest test suit name
39  */
40 LITE_TEST_SUIT(security, securityData, HksGenerateRandomTest);
41 
ExecHksInitialize(void const * argument)42 static void ExecHksInitialize(void const *argument)
43 {
44     LiteTestPrint("HksInitialize Begin!\n");
45     TEST_ASSERT_TRUE(HksInitialize() == 0);
46     LiteTestPrint("HksInitialize End!\n");
47     osThreadExit();
48 }
49 
50 /**
51  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
52  * @return: true——setup success
53  */
HksGenerateRandomTestSetUp()54 static BOOL HksGenerateRandomTestSetUp()
55 {
56     LiteTestPrint("setup\n");
57     IoTWatchDogDisable();
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     IoTWatchDogEnable();
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