1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 /* BEGIN_HEADER */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdint.h>
22 #include "bsl_sal.h"
23 #include "bsl_errno.h"
24 #include "bsl_binlog_id.h"
25 #include "bsl_log_internal.h"
26 #include "bsl_log.h"
27
28 static uintptr_t g_binlogFlag = 0;
29
30 // Binlog fixed-length callback
BinLogFix(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para1,void * para2,void * para3,void * para4)31 static void BinLogFix(uint32_t logId, uint32_t logLevel, uint32_t logType,
32 void *format, void *para1, void *para2, void *para3, void *para4)
33 {
34 (void)logId;
35 (void)logLevel;
36 (void)logType;
37 (void)format;
38 (void)para2;
39 (void)para3;
40 (void)para4;
41 g_binlogFlag = (uintptr_t)para1;
42 }
43
44 // Binlog variable-length callback
BinLogVar(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para)45 static void BinLogVar(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para)
46 {
47 (void)logId;
48 (void)logLevel;
49 (void)logType;
50 (void)format;
51 g_binlogFlag = (uintptr_t)para;
52 }
53
BinLogFixLenFunc(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para1,void * para2,void * para3,void * para4)54 void BinLogFixLenFunc(uint32_t logId, uint32_t logLevel, uint32_t logType,
55 void *format, void *para1, void *para2, void *para3, void *para4)
56 {
57 (void)logId;
58 (void)logLevel;
59 (void)logType;
60 (void)format;
61 (void)para1;
62 (void)para2;
63 (void)para3;
64 (void)para4;
65 }
66
BinLogVarLenFunc(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para)67 void BinLogVarLenFunc(uint32_t logId, uint32_t logLevel, uint32_t logType,
68 void *format, void *para)
69 {
70 (void)logId;
71 (void)logLevel;
72 (void)logType;
73 (void)format;
74 (void)para;
75 }
76
77 static FILE *g_LogOutput = NULL;
78
79 // Binlog fixed-length callback, output to a file
BinLogFixFunc(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para1,void * para2,void * para3,void * para4)80 static void BinLogFixFunc(uint32_t logId, uint32_t logLevel, uint32_t logType,
81 void *format, void *para1, void *para2, void *para3, void *para4)
82 {
83 uint8_t *pucFormat = (uint8_t *)format;
84 uint8_t ucTemp;
85 uint8_t ucNo = 0;
86
87 fprintf(g_LogOutput, "HITLS_BIN_LOG[FIXLEN]: logId=%u, logLevel=%u, logType=%u.\n", logId, logLevel, logType);
88
89 /* Search for all format specifiers */
90 while (*pucFormat != '\0') {
91 if (*pucFormat == '%') {
92 /* Get the second character */
93 ucTemp = *(pucFormat + 1);
94 if ((ucTemp == 'd') || (ucTemp == 'l') || (ucTemp == 'p') || (ucTemp == 'u') || (ucTemp == 'x')) {
95 ucNo++;
96 }
97 }
98 pucFormat++;
99 }
100
101 switch(ucNo) {
102 case 0: // Pure character strings are not output.
103 break;
104 case 1: // Output parameters only
105 fprintf(g_LogOutput, "0x%lX\n", (uint64_t)para1);
106 break;
107 case 2: // Output parameters only
108 fprintf(g_LogOutput, "0x%lX 0x%lX\n", (uint64_t)para1, (uint64_t)para2);
109 break;
110 case 3: // Output parameters only
111 fprintf(g_LogOutput, "0x%lX 0x%lX 0x%lX\n", (uint64_t)para1, (uint64_t)para2, (uint64_t)para3);
112 break;
113 case 4: // Output parameters only
114 fprintf(g_LogOutput, "0x%lX 0x%lX 0x%lX 0x%lX\n", (uint64_t)para1, (uint64_t)para2, (uint64_t)para3,
115 (uint64_t)para4);
116 break;
117 default: // The number of parameters is incorrect.
118 fprintf(g_LogOutput, "Invalid Number of Format Specifiers in LOG\n");
119 return;
120 }
121 }
122
123 // Binlog variable-length callback, output to a file
BinLogVarFunc(uint32_t logId,uint32_t logLevel,uint32_t logType,void * format,void * para)124 static void BinLogVarFunc(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para)
125 {
126 uint8_t *pucFormat = (uint8_t *)format;
127 uint8_t ucTemp;
128 uint8_t ucValidNo = 0;
129
130 fprintf(g_LogOutput, "HITLS_BIN_LOG[VARLEN]: logId=%u, logLevel=%u, logType=%u.\n", logId, logLevel, logType);
131
132 while (*pucFormat != '\0') {
133 if (*pucFormat == '%') {
134 /* Get the second character */
135 ucTemp = *(pucFormat + 1);
136 if (ucTemp == 's') { // Only the %s parameter can be contained.
137 ucValidNo++;
138 }
139 }
140 pucFormat++;
141 }
142
143 if (ucValidNo != 1) {
144 fprintf(g_LogOutput, "Invalid Number of %%s present in Var Len LOG\n");
145 }
146
147 fprintf(g_LogOutput, "%s\n", (const char *)para);
148 }
149
150 /* END_HEADER */
151
152 /**
153 * @test SDV_BSL_LOG_VERSION_API_TC001
154 * @title Obtaining the HiTLS version number of the log submodule of the BSL module
155 * @precon nan
156 * @brief
157 * 1. The buffer of the received version string is empty
158 * or the buffer length of the received version string is empty. Expected result 1 is obtained.
159 * 2. The buffer length of the received version string is less than the minimum length.
160 * Expected result 2 is obtained.
161 * 3. Received version and length of memory application. Expected result 3 is obtained.
162 * @expect
163 * 1. Return BSL_LOG_ERR_BAD_PARAM
164 * 2. Return BSL_LOG_ERR_BAD_PARAM
165 * 3. Succeeded. The version length is equal to the current version length,
166 * and the version string content is equal to the current version string content.
167 */
168 /* BEGIN_CASE */
SDV_BSL_LOG_VERSION_API_TC001(void)169 void SDV_BSL_LOG_VERSION_API_TC001(void)
170 {
171 char version[200];
172 uint32_t versionLen = 100;
173 ASSERT_TRUE(BSL_LOG_GetVersion(NULL, NULL) == BSL_LOG_ERR_BAD_PARAM);
174 ASSERT_TRUE(BSL_LOG_GetVersion((char *)version, &versionLen) == BSL_LOG_ERR_BAD_PARAM);
175 const char *versionStr = "openHiTLS 0.2.1 20 May 2025";
176 versionLen = 200;
177 ASSERT_TRUE(BSL_LOG_GetVersion((char *)version, &versionLen) == BSL_SUCCESS);
178 ASSERT_COMPARE("version", versionStr, strlen(versionStr), version, versionLen);
179 uint64_t versionNum = BSL_LOG_GetVersionNum();
180 ASSERT_EQ(versionNum, 0x0020001fULL);
181 EXIT:
182 return;
183 }
184 /* END_CASE */
185
186 /**
187 * @test SDV_BSL_REG_BIN_LOG_FUNC_TC001
188 * @title Setting the Log Callback Hook
189 * @precon nan
190 * @brief
191 * 1. Call the BSL_LOG_RegBinLogFunc interface and leave the input parameter empty. Expected result 1 is obtained.
192 * 2. Call BSL_LOG_RegBinLogFunc. If funcs is not empty, fixLenFunc is empty,
193 * and varLenFunc is empty, expected result 2 is obtained.
194 * 3. Call BSL_LOG_RegBinLogFunc. If funcs is not empty, fixLenFunc is empty,
195 * and varLenFunc is not empty, expected result 3 is obtained.
196 * 4. Call BSL_LOG_RegBinLogFunc. If funcs, fixLenFunc, and varLenFunc are not empty, expected result 4 is obtained.
197 * 5. Call BSL_LOG_RegBinLogFunc. Ensure that funcs and fixLenFunc are not empty,
198 * and varLenFunc are empty. Expected result 5 is obtained.
199 * 6. Call BSL_LOG_RegBinLogFunc repeatedly. If funcs, fixLenFunc, and varLenFunc are not empty.
200 * Expected result 6 is obtained.
201 * @expect
202 * 1.BSL_NULL_INPUT
203 * 2.BSL_NULL_INPUT
204 * 3.BSL_NULL_INPUT
205 * 4.BSL_SUCCESS
206 * 5.BSL_NULL_INPUT
207 * 6.BSL_SUCCESS
208 */
209 /* BEGIN_CASE */
SDV_BSL_REG_BIN_LOG_FUNC_TC001(void)210 void SDV_BSL_REG_BIN_LOG_FUNC_TC001(void)
211 {
212 TestMemInit();
213
214 BSL_LOG_BinLogFuncs func = {0};
215 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(NULL) == BSL_NULL_INPUT);
216
217 func.fixLenFunc = NULL;
218 func.varLenFunc = NULL;
219 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(&func) == BSL_NULL_INPUT);
220
221 func.fixLenFunc = NULL;
222 func.varLenFunc = BinLogVarLenFunc;
223 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(&func) == BSL_NULL_INPUT);
224
225 func.fixLenFunc = BinLogFixLenFunc;
226 func.varLenFunc = BinLogVarLenFunc;
227 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(&func) == BSL_SUCCESS);
228
229 func.fixLenFunc = BinLogFixLenFunc;
230 func.varLenFunc = NULL;
231 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(&func) == BSL_NULL_INPUT);
232
233 func.fixLenFunc = BinLogFixLenFunc;
234 func.varLenFunc = BinLogVarLenFunc;
235 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(&func) == BSL_SUCCESS);
236 ASSERT_TRUE(BSL_LOG_RegBinLogFunc(&func) == BSL_SUCCESS);
237 EXIT:
238 return;
239 }
240 /* END_CASE */
241
242 /**
243 * @test SDV_BSL_BIN_LOG_API_TC001
244 * @title Testing the Log Recording of the Log Submodule of the BSL Module
245 * @precon
246 * @brief
247 * 1. Setting an invalid dotting log level. Expected result 1 is obtained.
248 * 2. Set a valid dotting log level and obtain the dotting log level. Expected result 2 is obtained.
249 * 3. Set the parameter of the dotting log callback function to NULL. Expected result 3 is obtained.
250 * 4. The parameter and member of the callback function for setting dotting logs are not NULL.
251 * Expected result 4 is obtained.
252 * 5. The parameter of the callback function for setting dotting logs is not NULL.
253 * The callback function for four parameters is not NULL, and the callback function for one parameter is NULL.
254 * Expected result 5 is obtained.
255 * 6. The parameter of the callback function for setting dotting logs is not NULL.
256 * The value 1 of the callback function is not NULL. The value 4 of the callback function is NULL.
257 * Expected result 6 is obtained.
258 * 7. The parameter and member of the callback function for setting dotting logs are not NULL.
259 * Expected result 7 is obtained.
260 * 8. Overwrite the callback function for setting dotting logs. Expected result 8 is obtained.
261 * 9. Invoke the 4 parameter dotting to filter logs by log level. Expected result 9 is obtained.
262 * 10. Invoke the 4 parameter dotting, and the log level is not filtered. Expected result 10 is obtained.
263 * 11. Invoke variable parameter dotting and log level filtering. Expected result 11 is obtained.
264 * 12. Invoke variable parameter dotting, and log level filtering is not performed. Expected result 12 is obtained.
265 * @expect
266 * 1. BSL_LOG_ERR_BAD_PARAM
267 * 2. BSL_LOG_LEVEL_ERR
268 * 3. BSL_LOG_LEVEL_ERR
269 * 4. BSL_LOG_ERR_BAD_PARAM
270 * 5. BSL_LOG_LEVEL_ERR
271 * 6. BSL_NULL_INPUT
272 * 7. BSL_SUCCESS
273 * 8. BSL_SUCCESS
274 * 9. The global return value is not modified.
275 * 10. The global return value is changed to para1.
276 * 11. The global return value is not modified.
277 * 12. The global return value is changed to para.
278 */
279 /* BEGIN_CASE */
SDV_BSL_BIN_LOG_API_TC001(void)280 void SDV_BSL_BIN_LOG_API_TC001(void)
281 {
282 int32_t ret;
283 int32_t retlevel;
284
285 ret = BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_DEBUG + 1);
286 ASSERT_TRUE(ret != BSL_SUCCESS);
287 /*
288 * The get loglevel command is used to obtain the default value
289 * when the loglevel command fails to be set for the first time.
290 */
291 retlevel = BSL_LOG_GetBinLogLevel();
292 ASSERT_TRUE(retlevel == BSL_LOG_LEVEL_ERR);
293
294 ret = BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_ERR);
295 ASSERT_TRUE(ret == BSL_SUCCESS);
296 // The BSL_LOG_GetBinLogLevel function is replaced.
297 retlevel = BSL_LOG_GetBinLogLevel();
298 ASSERT_TRUE(retlevel == BSL_LOG_LEVEL_ERR);
299
300 // If the set loglevel operation fails, get loglevel to obtain the original value.
301 ret = BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_DEBUG + 1);
302 ASSERT_TRUE(ret != BSL_SUCCESS);
303 retlevel = BSL_LOG_GetBinLogLevel();
304 ASSERT_TRUE(retlevel == BSL_LOG_LEVEL_ERR);
305
306 ret = BSL_LOG_RegBinLogFunc(NULL);
307 ASSERT_TRUE(ret != BSL_SUCCESS);
308
309 BSL_LOG_BinLogFuncs funcs = {NULL, NULL};
310 ret = BSL_LOG_RegBinLogFunc(&funcs);
311 ASSERT_TRUE(ret != BSL_SUCCESS);
312
313 funcs.fixLenFunc = BinLogFix;
314 funcs.varLenFunc = NULL;
315 ret = BSL_LOG_RegBinLogFunc(&funcs);
316 ASSERT_TRUE(ret != BSL_SUCCESS);
317
318 funcs.fixLenFunc = NULL;
319 funcs.varLenFunc = BinLogVar;
320 ret = BSL_LOG_RegBinLogFunc(&funcs);
321 ASSERT_TRUE(ret != BSL_SUCCESS);
322
323 funcs.fixLenFunc = BinLogFix;
324 funcs.varLenFunc = BinLogVar;
325 ret = BSL_LOG_RegBinLogFunc(&funcs);
326 ASSERT_TRUE(ret == BSL_SUCCESS);
327
328 g_binlogFlag = 0;
329 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05001, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN, "this is test %d %d", 1, 2, 0, 0);
330 ASSERT_TRUE(g_binlogFlag == 0);
331 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05001, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "this is test %d %d", 1, 2, 0, 0);
332 ASSERT_TRUE(g_binlogFlag == 1);
333
334 const char *s = "abc";
335 g_binlogFlag = 0;
336 BSL_LOG_BINLOG_VARLEN(BINLOG_ID05001, BSL_LOG_LEVEL_INFO, BSL_LOG_BINLOG_TYPE_RUN, "this is test %s", s);
337 ASSERT_TRUE(g_binlogFlag == 0);
338 BSL_LOG_BINLOG_VARLEN(BINLOG_ID05001, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "this is test %s", s);
339 ASSERT_TRUE(g_binlogFlag == (uintptr_t)s);
340 EXIT:
341 return;
342 }
343 /* END_CASE */
344
345 /**
346 * @test SDV_BSL_SET_BIN_LOG_LEVEL_API_TC001
347 * @title Setting the log level
348 * @precon nan
349 * @brief
350 * 1. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_SEC - 1.
351 * Expected result 1 is obtained.
352 * 2. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_SEC.
353 * Expected result 2 is obtained.
354 * 3. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_FATAL.
355 * Expected result 3 is obtained.
356 * 4. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_ERR.
357 * Expected result 4 is obtained.
358 * 5. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_WARN.
359 * Expected result 5 is obtained.
360 * 6. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_INFO.
361 * Expected result 6 is obtained.
362 * 7. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_DEBUG.
363 * Expected result 7 is obtained.
364 * 8. Call the BSL_LOG_SetBinLogLevel interface with the input parameter BSL_LOG_LEVEL_DEBUG + 1.
365 * Expected result 8 is obtained.
366 * @expect
367 * 1.BSL_LOG_ERR_BAD_PARAM
368 * 2.BSL_SUCCESS
369 * 3.BSL_SUCCESS
370 * 4.BSL_SUCCESS
371 * 5.BSL_SUCCESS
372 * 6.BSL_SUCCESS
373 * 7.BSL_SUCCESS
374 * 8.BSL_LOG_ERR_BAD_PARAM
375 */
376 /* BEGIN_CASE */
SDV_BSL_SET_BIN_LOG_LEVEL_API_TC001(void)377 void SDV_BSL_SET_BIN_LOG_LEVEL_API_TC001(void)
378 {
379 TestMemInit();
380
381 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_SEC - 1) == BSL_LOG_ERR_BAD_PARAM);
382 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_SEC) == BSL_SUCCESS);
383 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_FATAL) == BSL_SUCCESS);
384 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_ERR) == BSL_SUCCESS);
385 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_WARN) == BSL_SUCCESS);
386 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_INFO) == BSL_SUCCESS);
387 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_DEBUG) == BSL_SUCCESS);
388 ASSERT_TRUE(BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_DEBUG + 1) == BSL_LOG_ERR_BAD_PARAM);
389 EXIT:
390 return;
391 }
392 /* END_CASE */
393
394 /**
395 * @test SDV_BSL_BIN_LOG_FUNC_TC001
396 * @title Demo of dotting logs of the BSL module. The log file is /tmp/HITLS_binlog_test.log.
397 * @precon nan
398 * @brief
399 * 1. Clean up and reopen the file. Expected result 1 is obtained.
400 * 2. Setting the log level. Expected result 2 is obtained.
401 * 3. Registering the Dotting Log Function. Expected result 3 is obtained.
402 * 4. Invoke the 4 parameter dotting to filter logs by log level. Expected result 4 is obtained.
403 * 5. Invoke the four-parameter dot. Expected result 5 is obtained.
404 * 6. Invoke the 1 parameter measurement point.
405 * @expect
406 * 1. Succeeded in clearing the remaining log files.
407 * 2. BSL_SUCCESS
408 * 3. BSL_SUCCESS
409 * 4. No output is generated in the log file.
410 * 5. The log file has output.
411 * 6. The log file has output.
412 */
413 /* BEGIN_CASE */
SDV_BSL_BIN_LOG_FUNC_TC001(void)414 void SDV_BSL_BIN_LOG_FUNC_TC001(void)
415 {
416 int32_t ret;
417 const char *filename = "./HITLS_binlog_test.log";
418 remove(filename);
419
420 g_LogOutput = fopen(filename, "w");
421 ASSERT_TRUE(g_LogOutput != NULL);
422
423 ret = BSL_LOG_SetBinLogLevel(BSL_LOG_LEVEL_ERR);
424 ASSERT_TRUE(ret == BSL_SUCCESS);
425
426 BSL_LOG_BinLogFuncs funcs = {BinLogFixFunc, BinLogVarFunc};
427 ret = BSL_LOG_RegBinLogFunc(&funcs);
428 ASSERT_TRUE(ret == BSL_SUCCESS);
429
430 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05001, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN,
431 "this is test %d %d", 1, 2, 0, 0);
432
433 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05002, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
434 "this is test %d %d", 1, 2, 2147483647, 4294967295);
435
436 BSL_LOG_BINLOG_VARLEN(BINLOG_ID05003, BSL_LOG_LEVEL_FATAL, BSL_LOG_BINLOG_TYPE_RUN,
437 "this is test msg: %s", "hello world");
438
439 EXIT:
440 fclose(g_LogOutput); // flush and close
441 }
442 /* END_CASE */