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 #include "securec.h"
17 #include "helper.h"
18 #include "test.h"
19
20 TestInfo g_testResult;
21
ConvertInt(const char * intStr,int * outNum)22 int ConvertInt(const char *intStr, int *outNum)
23 {
24 int *num = outNum;
25 uint32_t i = 0;
26 if (intStr[0] == '-') {
27 i = 1;
28 }
29
30 for (; i < strlen(intStr); i++) {
31 if (intStr[i] > '9' || intStr[i] < '0') {
32 return 1;
33 }
34 }
35 // Decimal
36 *num = strtol(intStr, NULL, 10);
37
38 return 0;
39 }
40
ConvertString(char ** str)41 int ConvertString(char **str)
42 {
43 if ((*str)[0] != '"') {
44 return 1;
45 }
46 uint32_t back = strlen(*str) - 1;
47 if ((*str)[back] != '"') {
48 back--;
49 if ((*str)[back] != '"') {
50 return 1;
51 }
52 }
53 (*str)[back] = '\0';
54 (*str)++;
55
56 return 0;
57 }
58
IsValidHexChar(char c)59 int IsValidHexChar(char c)
60 {
61 if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
62 return 0;
63 }
64 return 1;
65 }
66
ConvertHex(const char * str,Hex * output)67 int ConvertHex(const char *str, Hex *output)
68 {
69 uint32_t len = strlen(str);
70 if (len == 0) {
71 output->x = NULL;
72 output->len = 0;
73 return 0;
74 }
75 // The length of a hex string must be a multiple of 2.
76 if (len % 2 != 0) {
77 return 1;
78 }
79 // Length of the hex string/2 = Length of the byte stream
80 len = len / 2;
81 output->x = (uint8_t *)malloc(len * sizeof(uint8_t));
82 if (output->x == NULL) {
83 return 1;
84 }
85 output->len = len;
86
87 // Every 2 bytes in a group
88 for (uint32_t i = 0; i < 2 * len; i += 2) {
89 if ((IsValidHexChar(str[i]) == 1) || (IsValidHexChar(str[i + 1]) == 1)) {
90 goto ERR;
91 }
92 // hex to int formulas: (Hex % 32 + 9) % 25 = int, hex
93 output->x[i / 2] = (str[i] % 32 + 9) % 25 * 16 + (str[i + 1] % 32 + 9) % 25;
94 }
95 return 0;
96
97 ERR:
98 free(output->x);
99 output->len = 0;
100 return 1;
101 }
102
RecordFailure(const char * test,const char * filename)103 void RecordFailure(const char *test, const char *filename)
104 {
105 g_testResult.result = TEST_RESULT_FAILED;
106 if (strcpy_s(g_testResult.test, sizeof(g_testResult.test), test) != 0) {
107 Print("failure log failed: message too long\n");
108 }
109 if (strcpy_s(g_testResult.filename, sizeof(g_testResult.filename), filename) != 0) {
110 Print("failure log failed: filename too long\n");
111 }
112 }
113
SkipTest(const char * filename)114 void SkipTest(const char *filename)
115 {
116 g_testResult.result = TEST_RESULT_SKIPPED;
117 if (strcpy_s(g_testResult.filename, sizeof(g_testResult.filename), filename) != 0) {
118 Print("failure log failed: filename too long\n");
119 }
120 }
PrintResult(bool showDetail,char * vectorName,uint64_t useTime)121 void PrintResult(bool showDetail, char *vectorName, uint64_t useTime)
122 {
123 if (showDetail) {
124 if (g_testResult.result == TEST_RESULT_SUCCEED) {
125 Print("pass. use ms: %ld\n", useTime);
126 } else if (g_testResult.result == TEST_RESULT_SKIPPED) {
127 Print("skip\n");
128 } else {
129 Print("failed\n");
130 Print("at: (%s) in %s\n", g_testResult.test, g_testResult.filename);
131 }
132 } else if (g_testResult.result == TEST_RESULT_FAILED) {
133 Print("\nfailed at vector: %s\n", vectorName);
134 Print("at: (%s) in %s\n", g_testResult.test, g_testResult.filename);
135 }
136 }
137
PrintLog(FILE * logFile)138 void PrintLog(FILE *logFile)
139 {
140 int ret;
141 if (g_testResult.result == TEST_RESULT_SUCCEED) {
142 ret = fprintf(logFile, "pass\n");
143 if (ret < 0) {
144 Print("write to log file failed\n");
145 }
146 } else if (g_testResult.result == TEST_RESULT_SKIPPED) {
147 ret = fprintf(logFile, "skip\n");
148 if (ret < 0) {
149 Print("write to log file failed\n");
150 }
151 } else {
152 ret = fprintf(logFile, "failed\n");
153 if (ret < 0) {
154 Print("write to log file failed\n");
155 }
156 ret = fprintf(logFile, "at: (%s) in in %s\n", g_testResult.test, g_testResult.filename);
157 if (ret < 0) {
158 Print("write to log file failed\n");
159 }
160 }
161 }
162
PrintDiff(const uint8_t * str1,uint32_t size1,const uint8_t * str2,uint32_t size2)163 void PrintDiff(const uint8_t *str1, uint32_t size1, const uint8_t *str2, uint32_t size2)
164 {
165 Print("\nCompare different:\nstr1: ");
166 uint32_t i;
167 for (i = 0; i < size1; i++) {
168 Print("%02X ", str1[i]);
169 }
170 Print("\nstr2: ");
171 for (i = 0; i < size2; i++) {
172 Print("%02X ", str2[i]);
173 }
174 Print("\n");
175 }