blob: 170ead4fd84b5a3607fff26ec00c86023288b6f8 [file] [log] [blame]
Lawrence Tangd34f2b12022-07-19 15:36:31 +01001/**
2 * Defines tests for validating CPER-JSON IR output from the cper-parse library.
Ed Tanousfedd4572024-07-12 13:56:00 -07003 *
Lawrence Tangd34f2b12022-07-19 15:36:31 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
Ed Tanousedee0a32025-03-16 17:40:04 -07007#include "test-utils.h"
Ed Tanous54640292025-03-17 20:55:20 -07008#include "string.h"
9#include "assert.h"
10#include <ctype.h>
Ed Tanousa3663052025-03-16 12:54:36 -070011#include <json.h>
12#include <libcper/cper-parse.h>
13#include <libcper/generator/cper-generate.h>
14#include <libcper/generator/sections/gen-section.h>
15#include <libcper/json-schema.h>
16#include <libcper/sections/cper-section.h>
Lawrence Tangd34f2b12022-07-19 15:36:31 +010017
Ed Tanous54640292025-03-17 20:55:20 -070018#include "base64_test.h"
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080019
Lawrence Tangd34f2b12022-07-19 15:36:31 +010020/*
21* Test templates.
22*/
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080023static const GEN_VALID_BITS_TEST_TYPE allValidbitsSet = ALL_VALID;
24static const GEN_VALID_BITS_TEST_TYPE fixedValidbitsSet = SOME_VALID;
25static const int GEN_EXAMPLES = 0;
26
Ed Tanousedee0a32025-03-16 17:40:04 -070027static const char *cper_ext = "cperhex";
28static const char *json_ext = "json";
29
30struct file_info {
31 char *cper_out;
32 char *json_out;
33};
34
Ed Tanous54640292025-03-17 20:55:20 -070035void free_file_info(struct file_info *info)
Ed Tanousedee0a32025-03-16 17:40:04 -070036{
37 if (info == NULL) {
38 return;
39 }
40 free(info->cper_out);
41 free(info->json_out);
42 free(info);
43}
44
Ed Tanous54640292025-03-17 20:55:20 -070045struct file_info *file_info_init(const char *section_name)
Ed Tanousedee0a32025-03-16 17:40:04 -070046{
Ed Tanous54640292025-03-17 20:55:20 -070047 struct file_info *info = NULL;
Ed Tanousedee0a32025-03-16 17:40:04 -070048 char *buf = NULL;
49 size_t size;
50 int ret;
51
Ed Tanous54640292025-03-17 20:55:20 -070052 info = (struct file_info *)calloc(1, sizeof(struct file_info));
Ed Tanousedee0a32025-03-16 17:40:04 -070053 if (info == NULL) {
54 goto fail;
55 }
56
57 size = strlen(LIBCPER_EXAMPLES) + 1 + strlen(section_name) + 1 +
58 strlen(cper_ext) + 1;
59 info->cper_out = (char *)malloc(size);
60 ret = snprintf(info->cper_out, size, "%s/%s.%s", LIBCPER_EXAMPLES,
61 section_name, cper_ext);
62 if (ret != (int)size - 1) {
63 printf("snprintf0 failed\n");
64 goto fail;
65 }
66 size = strlen(LIBCPER_EXAMPLES) + 1 + strlen(section_name) + 1 +
67 strlen(json_ext) + 1;
68 info->json_out = (char *)malloc(size);
69 ret = snprintf(info->json_out, size, "%s/%s.%s", LIBCPER_EXAMPLES,
70 section_name, json_ext);
71 if (ret != (int)size - 1) {
72 printf("snprintf3 failed\n");
73 goto fail;
74 }
75 free(buf);
76 return info;
77
78fail:
79 free(buf);
80 free_file_info(info);
81 return NULL;
82}
83
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080084void cper_create_examples(const char *section_name)
85{
86 //Generate full CPER record for the given type.
Ed Tanousedee0a32025-03-16 17:40:04 -070087 json_object *ir = NULL;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080088 size_t size;
Ed Tanousedee0a32025-03-16 17:40:04 -070089 size_t file_size;
90 FILE *outFile = NULL;
Ed Tanous54640292025-03-17 20:55:20 -070091 unsigned char *file_data;
Ed Tanousedee0a32025-03-16 17:40:04 -070092 FILE *record = NULL;
93 char *buf = NULL;
Ed Tanous54640292025-03-17 20:55:20 -070094 struct file_info *info = file_info_init(section_name);
Ed Tanousedee0a32025-03-16 17:40:04 -070095 if (info == NULL) {
96 goto done;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080097 }
98
Ed Tanousedee0a32025-03-16 17:40:04 -070099 record = generate_record_memstream(&section_name, 1, &buf, &size, 0,
100 fixedValidbitsSet);
101
102 // Write example CPER to disk
103 outFile = fopen(info->cper_out, "wb");
104 if (outFile == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700105 printf("Failed to create/open CPER output file: %s\n",
106 info->cper_out);
Ed Tanousedee0a32025-03-16 17:40:04 -0700107 goto done;
108 }
109
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800110 fseek(record, 0, SEEK_END);
Ed Tanousedee0a32025-03-16 17:40:04 -0700111 file_size = ftell(record);
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800112 rewind(record);
Ed Tanous54640292025-03-17 20:55:20 -0700113 file_data = malloc(file_size);
114 if (fread(file_data, 1, file_size, record) != file_size) {
115 printf("Failed to read CPER data from memstream.");
Ed Tanousedee0a32025-03-16 17:40:04 -0700116 fclose(outFile);
Ed Tanous54640292025-03-17 20:55:20 -0700117 assert(0);
118
Ed Tanousedee0a32025-03-16 17:40:04 -0700119 goto done;
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800120 }
Ed Tanous54640292025-03-17 20:55:20 -0700121 for (size_t index = 0; index < file_size; index++) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700122 char hex_str[3];
123 int out = snprintf(hex_str, sizeof(hex_str), "%02x",
124 file_data[index]);
125 if (out != 2) {
126 printf("snprintf1 failed\n");
127 goto done;
128 }
129 fwrite(hex_str, sizeof(char), 2, outFile);
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800130 if (index % 30 == 29) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700131 fwrite("\n", sizeof(char), 1, outFile);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800132 }
133 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700134 fclose(outFile);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800135
136 //Convert to IR, free resources.
137 rewind(record);
Ed Tanousedee0a32025-03-16 17:40:04 -0700138 ir = cper_to_ir(record);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800139 if (ir == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700140 printf("Empty JSON from CPER bin2\n");
141 assert(0);
Ed Tanousedee0a32025-03-16 17:40:04 -0700142 goto done;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800143 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800144
145 //Write json output to disk
Ed Tanousedee0a32025-03-16 17:40:04 -0700146 json_object_to_file_ext(info->json_out, ir, JSON_C_TO_STRING_PRETTY);
Ed Tanousa3663052025-03-16 12:54:36 -0700147 json_object_put(ir);
148
Ed Tanousedee0a32025-03-16 17:40:04 -0700149done:
150 free_file_info(info);
151 if (record != NULL) {
152 fclose(record);
153 }
154 if (outFile != NULL) {
155 fclose(outFile);
156 }
Ed Tanousa3663052025-03-16 12:54:36 -0700157 free(buf);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800158}
159
Ed Tanousedee0a32025-03-16 17:40:04 -0700160int hex2int(char ch)
161{
162 if ((ch >= '0') && (ch <= '9')) {
163 return ch - '0';
164 }
165 if ((ch >= 'A') && (ch <= 'F')) {
166 return ch - 'A' + 10;
167 }
168 if ((ch >= 'a') && (ch <= 'f')) {
169 return ch - 'a' + 10;
170 }
171 return -1;
172}
173
Ed Tanous54640292025-03-17 20:55:20 -0700174int string_to_binary(const char *source, size_t length, unsigned char **retval)
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800175{
Ed Tanous54640292025-03-17 20:55:20 -0700176 size_t retval_size = length * 2;
177 *retval = malloc(retval_size);
178 int uppernibble = 1;
179
180 size_t ret_index = 0;
181
Ed Tanousedee0a32025-03-16 17:40:04 -0700182 for (size_t i = 0; i < length; i++) {
183 char c = source[i];
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800184 if (c == '\n') {
185 continue;
186 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700187 int val = hex2int(c);
188 if (val < 0) {
189 printf("Invalid hex character in test file: %c\n", c);
Ed Tanous54640292025-03-17 20:55:20 -0700190 return -1;
Ed Tanousedee0a32025-03-16 17:40:04 -0700191 }
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800192
193 if (uppernibble) {
Ed Tanous54640292025-03-17 20:55:20 -0700194 (*retval)[ret_index] = (unsigned char)(val << 4);
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800195 } else {
Ed Tanous54640292025-03-17 20:55:20 -0700196 (*retval)[ret_index] += (unsigned char)val;
197 ret_index++;
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800198 }
199 uppernibble = !uppernibble;
200 }
Ed Tanous54640292025-03-17 20:55:20 -0700201 return ret_index;
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800202}
203
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800204//Tests fixed CPER sections for IR validity with an example set.
205void cper_example_section_ir_test(const char *section_name)
206{
207 //Open CPER record for the given type.
Ed Tanous54640292025-03-17 20:55:20 -0700208 struct file_info *info = file_info_init(section_name);
Ed Tanousedee0a32025-03-16 17:40:04 -0700209 if (info == NULL) {
210 return;
211 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800212
Ed Tanousedee0a32025-03-16 17:40:04 -0700213 FILE *cper_file = fopen(info->cper_out, "rb");
214 if (cper_file == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700215 printf("Failed to open CPER file: %s\n", info->cper_out);
Ed Tanousedee0a32025-03-16 17:40:04 -0700216 free_file_info(info);
Ed Tanous54640292025-03-17 20:55:20 -0700217 assert(0);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800218 return;
219 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700220 fseek(cper_file, 0, SEEK_END);
221 size_t length = ftell(cper_file);
222 fseek(cper_file, 0, SEEK_SET);
223 char *buffer = (char *)malloc(length);
224 if (!buffer) {
225 free_file_info(info);
226 return;
227 }
228 if (fread(buffer, 1, length, cper_file) != length) {
Ed Tanous54640292025-03-17 20:55:20 -0700229 printf("Failed to read CPER file: %s\n", info->cper_out);
Ed Tanousedee0a32025-03-16 17:40:04 -0700230 free(buffer);
231 free_file_info(info);
232 return;
233 }
234 fclose(cper_file);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800235
Ed Tanous54640292025-03-17 20:55:20 -0700236 unsigned char *cper_bin;
237 int cper_bin_len = string_to_binary(buffer, length, &cper_bin);
238 if (cper_bin_len <= 0) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700239 free(buffer);
240 free_file_info(info);
Ed Tanous54640292025-03-17 20:55:20 -0700241 assert(0);
242 return;
243 }
244 printf("cper_bin: %s\n", cper_bin);
245 printf("cper_bin_len: %d\n", cper_bin_len);
246
247 //Convert to IR, free resources.
248 json_object *ir = cper_buf_to_ir(cper_bin, cper_bin_len);
249 if (ir == NULL) {
250 printf("Empty JSON from CPER bin3\n");
251 free(cper_bin);
252 free(buffer);
253 free_file_info(info);
254 assert(0);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800255 return;
256 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800257
Ed Tanousedee0a32025-03-16 17:40:04 -0700258 json_object *expected = json_object_from_file(info->json_out);
Ed Tanous54640292025-03-17 20:55:20 -0700259 assert(expected != NULL);
260 if (expected == NULL) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700261 free(buffer);
Ed Tanous54640292025-03-17 20:55:20 -0700262 free(cper_bin);
Ed Tanousedee0a32025-03-16 17:40:04 -0700263 free_file_info(info);
Ed Tanousa3663052025-03-16 12:54:36 -0700264 const char *str = json_object_to_json_string(ir);
265
266 const char *expected_str = json_object_to_json_string(expected);
Ed Tanous54640292025-03-17 20:55:20 -0700267 assert(strcmp(str, expected_str) == 0);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800268 return;
269 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800270
Ed Tanous54640292025-03-17 20:55:20 -0700271 assert(json_object_equal(ir, expected));
Ed Tanousedee0a32025-03-16 17:40:04 -0700272 free(buffer);
Ed Tanous54640292025-03-17 20:55:20 -0700273 free(cper_bin);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800274 json_object_put(ir);
Ed Tanousa3663052025-03-16 12:54:36 -0700275 json_object_put(expected);
Ed Tanousedee0a32025-03-16 17:40:04 -0700276 free_file_info(info);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800277}
Lawrence Tangcd505202022-07-19 16:55:11 +0100278
279//Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800280void cper_log_section_ir_test(const char *section_name, int single_section,
281 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100282{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100283 //Generate full CPER record for the given type.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100284 char *buf;
285 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100286 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800287 single_section, validBitsType);
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100288
Lawrence Tange407b4c2022-07-21 13:54:01 +0100289 //Convert to IR, free resources.
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100290 json_object *ir;
John Chungf8fc7052024-05-03 20:05:29 +0800291 if (single_section) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100292 ir = cper_single_section_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800293 } else {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100294 ir = cper_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800295 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800296
Lawrence Tange407b4c2022-07-21 13:54:01 +0100297 fclose(record);
298 free(buf);
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100299
Lawrence Tange407b4c2022-07-21 13:54:01 +0100300 //Validate against schema.
Ed Tanousa3663052025-03-16 12:54:36 -0700301 int valid = schema_validate_from_file(ir, single_section,
302 /*all_valid_bits*/ 1);
John Chungf8fc7052024-05-03 20:05:29 +0800303 json_object_put(ir);
Ed Tanous54640292025-03-17 20:55:20 -0700304
305 if (valid < 0) {
306 printf("IR validation test failed (single section mode = %d)\n",
307 single_section);
308 assert(0);
309 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100310}
311
Ed Tanousedee0a32025-03-16 17:40:04 -0700312int to_hex(const unsigned char *input, size_t size, char **out)
Ed Tanous50b966f2025-03-11 09:06:19 -0700313{
Ed Tanousedee0a32025-03-16 17:40:04 -0700314 *out = (char *)malloc(size * 2);
315 if (out == NULL) {
316 return -1;
Ed Tanous50b966f2025-03-11 09:06:19 -0700317 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700318 int out_index = 0;
319 for (size_t i = 0; i < size; i++) {
320 unsigned char c = input[i];
321 char hex_str[3];
322 int n = snprintf(hex_str, sizeof(hex_str), "%02x", c);
323 if (n != 2) {
324 printf("snprintf2 failed with code %d\n", n);
325 return -1;
326 }
327 (*out)[out_index] = hex_str[0];
328 out_index++;
329 (*out)[out_index] = hex_str[1];
330 out_index++;
331 }
332 return out_index;
Ed Tanous50b966f2025-03-11 09:06:19 -0700333}
334
Lawrence Tangcd505202022-07-19 16:55:11 +0100335//Checks for binary round-trip equality for a given randomly generated CPER record.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800336void cper_log_section_binary_test(const char *section_name, int single_section,
337 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangcd505202022-07-19 16:55:11 +0100338{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100339 //Generate CPER record for the given type.
340 char *buf;
341 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100342 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800343 single_section, validBitsType);
344 if (record == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700345 printf("Could not generate memstream for binary test");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800346 return;
347 }
Lawrence Tangcd505202022-07-19 16:55:11 +0100348
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100349 //Convert to IR.
350 json_object *ir;
John Chungf8fc7052024-05-03 20:05:29 +0800351 if (single_section) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100352 ir = cper_single_section_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800353 } else {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100354 ir = cper_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800355 }
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100356
357 //Now convert back to binary, and get a stream out.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100358 char *cper_buf;
359 size_t cper_buf_size;
360 FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
John Chungf8fc7052024-05-03 20:05:29 +0800361 if (single_section) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100362 ir_single_section_to_cper(ir, stream);
John Chungf8fc7052024-05-03 20:05:29 +0800363 } else {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100364 ir_to_cper(ir, stream);
John Chungf8fc7052024-05-03 20:05:29 +0800365 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100366 fclose(stream);
Lawrence Tangcd505202022-07-19 16:55:11 +0100367
Ed Tanousedee0a32025-03-16 17:40:04 -0700368 printf("size: %zu, cper_buf_size: %zu\n", size, cper_buf_size);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100369
Ed Tanousedee0a32025-03-16 17:40:04 -0700370 char *buf_hex;
371 int buf_hex_len = to_hex((unsigned char *)buf, size, &buf_hex);
372 char *cper_buf_hex;
373 int cper_buf_hex_len =
374 to_hex((unsigned char *)cper_buf, cper_buf_size, &cper_buf_hex);
375
Ed Tanous9f260e52025-04-24 09:37:59 -0700376 printf("%.*s\n", cper_buf_hex_len, cper_buf_hex);
377 printf("%.*s\n", buf_hex_len, buf_hex);
Ed Tanous54640292025-03-17 20:55:20 -0700378 assert(buf_hex_len == cper_buf_hex_len);
379 assert(memcmp(buf_hex, cper_buf_hex, buf_hex_len) == 0);
Ed Tanousedee0a32025-03-16 17:40:04 -0700380
Ed Tanousedee0a32025-03-16 17:40:04 -0700381 free(buf_hex);
Ed Tanous54640292025-03-17 20:55:20 -0700382 free(cper_buf_hex);
383
Lawrence Tange407b4c2022-07-21 13:54:01 +0100384 //Free everything up.
385 fclose(record);
386 free(buf);
387 free(cper_buf);
John Chungf8fc7052024-05-03 20:05:29 +0800388 json_object_put(ir);
Lawrence Tangcd505202022-07-19 16:55:11 +0100389}
390
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100391//Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
392void cper_log_section_dual_ir_test(const char *section_name)
393{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800394 cper_log_section_ir_test(section_name, 0, allValidbitsSet);
395 cper_log_section_ir_test(section_name, 1, allValidbitsSet);
396 //Validate against examples
397 cper_example_section_ir_test(section_name);
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100398}
399
400//Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
401void cper_log_section_dual_binary_test(const char *section_name)
402{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800403 cper_log_section_binary_test(section_name, 0, allValidbitsSet);
404 cper_log_section_binary_test(section_name, 1, allValidbitsSet);
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100405}
406
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100407/*
Lawrence Tang580423f2022-08-24 09:37:53 +0100408* Non-single section assertions.
409*/
Ed Tanous54640292025-03-17 20:55:20 -0700410void CompileTimeAssertions_TwoWayConversion()
Lawrence Tang580423f2022-08-24 09:37:53 +0100411{
John Chungf8fc7052024-05-03 20:05:29 +0800412 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tang580423f2022-08-24 09:37:53 +0100413 //If a conversion one way exists, a conversion the other way must exist.
John Chungf8fc7052024-05-03 20:05:29 +0800414 if (section_definitions[i].ToCPER != NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700415 assert(section_definitions[i].ToIR != NULL);
John Chungf8fc7052024-05-03 20:05:29 +0800416 }
417 if (section_definitions[i].ToIR != NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700418 assert(section_definitions[i].ToCPER != NULL);
John Chungf8fc7052024-05-03 20:05:29 +0800419 }
Lawrence Tang580423f2022-08-24 09:37:53 +0100420 }
421}
422
Ed Tanous54640292025-03-17 20:55:20 -0700423void CompileTimeAssertions_ShortcodeNoSpaces()
Lawrence Tang40519cb2022-08-24 15:50:08 +0100424{
John Chungf8fc7052024-05-03 20:05:29 +0800425 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang40519cb2022-08-24 15:50:08 +0100426 for (int j = 0;
427 generator_definitions[i].ShortName[j + 1] != '\0'; j++) {
Ed Tanous54640292025-03-17 20:55:20 -0700428 assert(isspace(generator_definitions[i].ShortName[j]) ==
429 0);
Lawrence Tang40519cb2022-08-24 15:50:08 +0100430 }
431 }
432}
433
Lawrence Tang580423f2022-08-24 09:37:53 +0100434/*
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100435* Single section tests.
436*/
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100437
Lawrence Tangcd505202022-07-19 16:55:11 +0100438//Generic processor tests.
Ed Tanous54640292025-03-17 20:55:20 -0700439void GenericProcessorTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100440{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100441 cper_log_section_dual_ir_test("generic");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100442}
Ed Tanous54640292025-03-17 20:55:20 -0700443void GenericProcessorTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100444{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100445 cper_log_section_dual_binary_test("generic");
Lawrence Tangcd505202022-07-19 16:55:11 +0100446}
447
448//IA32/x64 tests.
Ed Tanous54640292025-03-17 20:55:20 -0700449void IA32x64Tests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100450{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100451 cper_log_section_dual_ir_test("ia32x64");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100452}
Ed Tanous54640292025-03-17 20:55:20 -0700453void IA32x64Tests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100454{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100455 cper_log_section_dual_binary_test("ia32x64");
Lawrence Tangcd505202022-07-19 16:55:11 +0100456}
457
Ed Tanous54640292025-03-17 20:55:20 -0700458// void IPFTests_IRValid() {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100459// cper_log_section_dual_ir_test("ipf");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100460// }
Lawrence Tangcd505202022-07-19 16:55:11 +0100461
462//ARM tests.
Ed Tanous54640292025-03-17 20:55:20 -0700463void ArmTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100464{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100465 cper_log_section_dual_ir_test("arm");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100466}
Ed Tanous54640292025-03-17 20:55:20 -0700467void ArmTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100468{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100469 cper_log_section_dual_binary_test("arm");
Lawrence Tangcd505202022-07-19 16:55:11 +0100470}
471
472//Memory tests.
Ed Tanous54640292025-03-17 20:55:20 -0700473void MemoryTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100474{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100475 cper_log_section_dual_ir_test("memory");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100476}
Ed Tanous54640292025-03-17 20:55:20 -0700477void MemoryTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100478{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100479 cper_log_section_dual_binary_test("memory");
Lawrence Tangcd505202022-07-19 16:55:11 +0100480}
481
482//Memory 2 tests.
Ed Tanous54640292025-03-17 20:55:20 -0700483void Memory2Tests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100484{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100485 cper_log_section_dual_ir_test("memory2");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100486}
Ed Tanous54640292025-03-17 20:55:20 -0700487void Memory2Tests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100488{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100489 cper_log_section_dual_binary_test("memory2");
Lawrence Tangcd505202022-07-19 16:55:11 +0100490}
491
492//PCIe tests.
Ed Tanous54640292025-03-17 20:55:20 -0700493void PCIeTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100494{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100495 cper_log_section_dual_ir_test("pcie");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100496}
Ed Tanous54640292025-03-17 20:55:20 -0700497void PCIeTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100498{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100499 cper_log_section_dual_binary_test("pcie");
Lawrence Tangcd505202022-07-19 16:55:11 +0100500}
501
502//Firmware tests.
Ed Tanous54640292025-03-17 20:55:20 -0700503void FirmwareTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100504{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100505 cper_log_section_dual_ir_test("firmware");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100506}
Ed Tanous54640292025-03-17 20:55:20 -0700507void FirmwareTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100508{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100509 cper_log_section_dual_binary_test("firmware");
Lawrence Tangcd505202022-07-19 16:55:11 +0100510}
511
512//PCI Bus tests.
Ed Tanous54640292025-03-17 20:55:20 -0700513void PCIBusTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100514{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100515 cper_log_section_dual_ir_test("pcibus");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100516}
Ed Tanous54640292025-03-17 20:55:20 -0700517void PCIBusTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100518{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100519 cper_log_section_dual_binary_test("pcibus");
Lawrence Tangcd505202022-07-19 16:55:11 +0100520}
521
522//PCI Device tests.
Ed Tanous54640292025-03-17 20:55:20 -0700523void PCIDevTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100524{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100525 cper_log_section_dual_ir_test("pcidev");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100526}
Ed Tanous54640292025-03-17 20:55:20 -0700527void PCIDevTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100528{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100529 cper_log_section_dual_binary_test("pcidev");
Lawrence Tangcd505202022-07-19 16:55:11 +0100530}
531
532//Generic DMAr tests.
Ed Tanous54640292025-03-17 20:55:20 -0700533void DMArGenericTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100534{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100535 cper_log_section_dual_ir_test("dmargeneric");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100536}
Ed Tanous54640292025-03-17 20:55:20 -0700537void DMArGenericTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100538{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100539 cper_log_section_dual_binary_test("dmargeneric");
Lawrence Tangcd505202022-07-19 16:55:11 +0100540}
541
542//VT-d DMAr tests.
Ed Tanous54640292025-03-17 20:55:20 -0700543void DMArVtdTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100544{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100545 cper_log_section_dual_ir_test("dmarvtd");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100546}
Ed Tanous54640292025-03-17 20:55:20 -0700547void DMArVtdTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100548{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100549 cper_log_section_dual_binary_test("dmarvtd");
Lawrence Tangcd505202022-07-19 16:55:11 +0100550}
551
552//IOMMU DMAr tests.
Ed Tanous54640292025-03-17 20:55:20 -0700553void DMArIOMMUTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100554{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100555 cper_log_section_dual_ir_test("dmariommu");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100556}
Ed Tanous54640292025-03-17 20:55:20 -0700557void DMArIOMMUTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100558{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100559 cper_log_section_dual_binary_test("dmariommu");
Lawrence Tangcd505202022-07-19 16:55:11 +0100560}
561
562//CCIX PER tests.
Ed Tanous54640292025-03-17 20:55:20 -0700563void CCIXPERTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100564{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100565 cper_log_section_dual_ir_test("ccixper");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100566}
Ed Tanous54640292025-03-17 20:55:20 -0700567void CCIXPERTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100568{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100569 cper_log_section_dual_binary_test("ccixper");
Lawrence Tangcd505202022-07-19 16:55:11 +0100570}
571
572//CXL Protocol tests.
Ed Tanous54640292025-03-17 20:55:20 -0700573void CXLProtocolTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100574{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100575 cper_log_section_dual_ir_test("cxlprotocol");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100576}
Ed Tanous54640292025-03-17 20:55:20 -0700577void CXLProtocolTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100578{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100579 cper_log_section_dual_binary_test("cxlprotocol");
Lawrence Tangcd505202022-07-19 16:55:11 +0100580}
581
582//CXL Component tests.
Ed Tanous54640292025-03-17 20:55:20 -0700583void CXLComponentTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100584{
Lawrence Tang8f977452022-08-24 14:55:07 +0100585 cper_log_section_dual_ir_test("cxlcomponent-media");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100586}
Ed Tanous54640292025-03-17 20:55:20 -0700587void CXLComponentTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100588{
Lawrence Tang8f977452022-08-24 14:55:07 +0100589 cper_log_section_dual_binary_test("cxlcomponent-media");
Lawrence Tangcd505202022-07-19 16:55:11 +0100590}
591
Ed Tanous2d17ace2024-08-27 14:45:38 -0700592//NVIDIA section tests.
Ed Tanous54640292025-03-17 20:55:20 -0700593void NVIDIASectionTests_IRValid()
Ed Tanous2d17ace2024-08-27 14:45:38 -0700594{
595 cper_log_section_dual_ir_test("nvidia");
596}
Ed Tanous54640292025-03-17 20:55:20 -0700597void NVIDIASectionTests_BinaryEqual()
Ed Tanous2d17ace2024-08-27 14:45:38 -0700598{
599 cper_log_section_dual_binary_test("nvidia");
600}
601
Ed Tanous55968b12025-05-06 21:04:52 -0700602void NVIDIACMETSectionTests_IRValid()
603{
604 cper_example_section_ir_test("nvidia_cmet_info");
605}
606
Lawrence Tangcd505202022-07-19 16:55:11 +0100607//Unknown section tests.
Ed Tanous54640292025-03-17 20:55:20 -0700608void UnknownSectionTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100609{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100610 cper_log_section_dual_ir_test("unknown");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100611}
Ed Tanous54640292025-03-17 20:55:20 -0700612void UnknownSectionTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100613{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100614 cper_log_section_dual_binary_test("unknown");
Lawrence Tangcd505202022-07-19 16:55:11 +0100615}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100616
617//Entrypoint for the testing program.
Ed Tanous54640292025-03-17 20:55:20 -0700618int main()
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100619{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800620 if (GEN_EXAMPLES) {
621 cper_create_examples("arm");
622 cper_create_examples("ia32x64");
623 cper_create_examples("memory");
624 cper_create_examples("memory2");
625 cper_create_examples("pcie");
626 cper_create_examples("firmware");
627 cper_create_examples("pcibus");
628 cper_create_examples("pcidev");
629 cper_create_examples("dmargeneric");
630 cper_create_examples("dmarvtd");
631 cper_create_examples("dmariommu");
632 cper_create_examples("ccixper");
633 cper_create_examples("cxlprotocol");
634 cper_create_examples("cxlcomponent-media");
635 cper_create_examples("nvidia");
636 cper_create_examples("unknown");
637 }
Ed Tanous54640292025-03-17 20:55:20 -0700638 test_base64_encode_good();
639 test_base64_decode_good();
640 GenericProcessorTests_IRValid();
641 GenericProcessorTests_BinaryEqual();
642 IA32x64Tests_IRValid();
643 IA32x64Tests_BinaryEqual();
644 ArmTests_IRValid();
645 ArmTests_BinaryEqual();
646 MemoryTests_IRValid();
647 MemoryTests_BinaryEqual();
648 Memory2Tests_IRValid();
649 Memory2Tests_BinaryEqual();
650 PCIeTests_IRValid();
651 PCIeTests_BinaryEqual();
652 FirmwareTests_IRValid();
653 FirmwareTests_BinaryEqual();
654 PCIBusTests_IRValid();
655 PCIBusTests_BinaryEqual();
656 PCIDevTests_IRValid();
657 PCIDevTests_BinaryEqual();
658 DMArGenericTests_IRValid();
659 DMArGenericTests_BinaryEqual();
660 DMArVtdTests_IRValid();
661 DMArVtdTests_BinaryEqual();
662 DMArIOMMUTests_IRValid();
663 DMArIOMMUTests_BinaryEqual();
664 CCIXPERTests_IRValid();
665 CCIXPERTests_BinaryEqual();
666 CXLProtocolTests_IRValid();
667 CXLProtocolTests_BinaryEqual();
668 CXLComponentTests_IRValid();
669 CXLComponentTests_BinaryEqual();
670 NVIDIASectionTests_IRValid();
671 NVIDIASectionTests_BinaryEqual();
Ed Tanous55968b12025-05-06 21:04:52 -0700672 NVIDIACMETSectionTests_IRValid();
Ed Tanous54640292025-03-17 20:55:20 -0700673 UnknownSectionTests_IRValid();
674 UnknownSectionTests_BinaryEqual();
675 CompileTimeAssertions_TwoWayConversion();
676 CompileTimeAssertions_ShortcodeNoSpaces();
677
678 printf("\n\nTest completed successfully.\n");
679
680 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800681}