blob: a0a0db1ac919efe5f98406258115ee79b94889d7 [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 Tanous54640292025-03-17 20:55:20 -0700376 assert(buf_hex_len == cper_buf_hex_len);
377 assert(memcmp(buf_hex, cper_buf_hex, buf_hex_len) == 0);
Ed Tanousedee0a32025-03-16 17:40:04 -0700378
Ed Tanousedee0a32025-03-16 17:40:04 -0700379 free(buf_hex);
Ed Tanous54640292025-03-17 20:55:20 -0700380 free(cper_buf_hex);
381
Lawrence Tange407b4c2022-07-21 13:54:01 +0100382 //Free everything up.
383 fclose(record);
384 free(buf);
385 free(cper_buf);
John Chungf8fc7052024-05-03 20:05:29 +0800386 json_object_put(ir);
Lawrence Tangcd505202022-07-19 16:55:11 +0100387}
388
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100389//Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
390void cper_log_section_dual_ir_test(const char *section_name)
391{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800392 cper_log_section_ir_test(section_name, 0, allValidbitsSet);
393 cper_log_section_ir_test(section_name, 1, allValidbitsSet);
394 //Validate against examples
395 cper_example_section_ir_test(section_name);
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100396}
397
398//Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
399void cper_log_section_dual_binary_test(const char *section_name)
400{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800401 cper_log_section_binary_test(section_name, 0, allValidbitsSet);
402 cper_log_section_binary_test(section_name, 1, allValidbitsSet);
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100403}
404
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100405/*
Lawrence Tang580423f2022-08-24 09:37:53 +0100406* Non-single section assertions.
407*/
Ed Tanous54640292025-03-17 20:55:20 -0700408void CompileTimeAssertions_TwoWayConversion()
Lawrence Tang580423f2022-08-24 09:37:53 +0100409{
John Chungf8fc7052024-05-03 20:05:29 +0800410 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tang580423f2022-08-24 09:37:53 +0100411 //If a conversion one way exists, a conversion the other way must exist.
John Chungf8fc7052024-05-03 20:05:29 +0800412 if (section_definitions[i].ToCPER != NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700413 assert(section_definitions[i].ToIR != NULL);
John Chungf8fc7052024-05-03 20:05:29 +0800414 }
415 if (section_definitions[i].ToIR != NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700416 assert(section_definitions[i].ToCPER != NULL);
John Chungf8fc7052024-05-03 20:05:29 +0800417 }
Lawrence Tang580423f2022-08-24 09:37:53 +0100418 }
419}
420
Ed Tanous54640292025-03-17 20:55:20 -0700421void CompileTimeAssertions_ShortcodeNoSpaces()
Lawrence Tang40519cb2022-08-24 15:50:08 +0100422{
John Chungf8fc7052024-05-03 20:05:29 +0800423 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang40519cb2022-08-24 15:50:08 +0100424 for (int j = 0;
425 generator_definitions[i].ShortName[j + 1] != '\0'; j++) {
Ed Tanous54640292025-03-17 20:55:20 -0700426 assert(isspace(generator_definitions[i].ShortName[j]) ==
427 0);
Lawrence Tang40519cb2022-08-24 15:50:08 +0100428 }
429 }
430}
431
Lawrence Tang580423f2022-08-24 09:37:53 +0100432/*
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100433* Single section tests.
434*/
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100435
Lawrence Tangcd505202022-07-19 16:55:11 +0100436//Generic processor tests.
Ed Tanous54640292025-03-17 20:55:20 -0700437void GenericProcessorTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100438{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100439 cper_log_section_dual_ir_test("generic");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100440}
Ed Tanous54640292025-03-17 20:55:20 -0700441void GenericProcessorTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100442{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100443 cper_log_section_dual_binary_test("generic");
Lawrence Tangcd505202022-07-19 16:55:11 +0100444}
445
446//IA32/x64 tests.
Ed Tanous54640292025-03-17 20:55:20 -0700447void IA32x64Tests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100448{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100449 cper_log_section_dual_ir_test("ia32x64");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100450}
Ed Tanous54640292025-03-17 20:55:20 -0700451void IA32x64Tests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100452{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100453 cper_log_section_dual_binary_test("ia32x64");
Lawrence Tangcd505202022-07-19 16:55:11 +0100454}
455
Ed Tanous54640292025-03-17 20:55:20 -0700456// void IPFTests_IRValid() {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100457// cper_log_section_dual_ir_test("ipf");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100458// }
Lawrence Tangcd505202022-07-19 16:55:11 +0100459
460//ARM tests.
Ed Tanous54640292025-03-17 20:55:20 -0700461void ArmTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100462{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100463 cper_log_section_dual_ir_test("arm");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100464}
Ed Tanous54640292025-03-17 20:55:20 -0700465void ArmTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100466{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100467 cper_log_section_dual_binary_test("arm");
Lawrence Tangcd505202022-07-19 16:55:11 +0100468}
469
470//Memory tests.
Ed Tanous54640292025-03-17 20:55:20 -0700471void MemoryTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100472{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100473 cper_log_section_dual_ir_test("memory");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100474}
Ed Tanous54640292025-03-17 20:55:20 -0700475void MemoryTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100476{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100477 cper_log_section_dual_binary_test("memory");
Lawrence Tangcd505202022-07-19 16:55:11 +0100478}
479
480//Memory 2 tests.
Ed Tanous54640292025-03-17 20:55:20 -0700481void Memory2Tests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100482{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100483 cper_log_section_dual_ir_test("memory2");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100484}
Ed Tanous54640292025-03-17 20:55:20 -0700485void Memory2Tests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100486{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100487 cper_log_section_dual_binary_test("memory2");
Lawrence Tangcd505202022-07-19 16:55:11 +0100488}
489
490//PCIe tests.
Ed Tanous54640292025-03-17 20:55:20 -0700491void PCIeTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100492{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100493 cper_log_section_dual_ir_test("pcie");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100494}
Ed Tanous54640292025-03-17 20:55:20 -0700495void PCIeTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100496{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100497 cper_log_section_dual_binary_test("pcie");
Lawrence Tangcd505202022-07-19 16:55:11 +0100498}
499
500//Firmware tests.
Ed Tanous54640292025-03-17 20:55:20 -0700501void FirmwareTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100502{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100503 cper_log_section_dual_ir_test("firmware");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100504}
Ed Tanous54640292025-03-17 20:55:20 -0700505void FirmwareTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100506{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100507 cper_log_section_dual_binary_test("firmware");
Lawrence Tangcd505202022-07-19 16:55:11 +0100508}
509
510//PCI Bus tests.
Ed Tanous54640292025-03-17 20:55:20 -0700511void PCIBusTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100512{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100513 cper_log_section_dual_ir_test("pcibus");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100514}
Ed Tanous54640292025-03-17 20:55:20 -0700515void PCIBusTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100516{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100517 cper_log_section_dual_binary_test("pcibus");
Lawrence Tangcd505202022-07-19 16:55:11 +0100518}
519
520//PCI Device tests.
Ed Tanous54640292025-03-17 20:55:20 -0700521void PCIDevTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100522{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100523 cper_log_section_dual_ir_test("pcidev");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100524}
Ed Tanous54640292025-03-17 20:55:20 -0700525void PCIDevTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100526{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100527 cper_log_section_dual_binary_test("pcidev");
Lawrence Tangcd505202022-07-19 16:55:11 +0100528}
529
530//Generic DMAr tests.
Ed Tanous54640292025-03-17 20:55:20 -0700531void DMArGenericTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100532{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100533 cper_log_section_dual_ir_test("dmargeneric");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100534}
Ed Tanous54640292025-03-17 20:55:20 -0700535void DMArGenericTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100536{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100537 cper_log_section_dual_binary_test("dmargeneric");
Lawrence Tangcd505202022-07-19 16:55:11 +0100538}
539
540//VT-d DMAr tests.
Ed Tanous54640292025-03-17 20:55:20 -0700541void DMArVtdTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100542{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100543 cper_log_section_dual_ir_test("dmarvtd");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100544}
Ed Tanous54640292025-03-17 20:55:20 -0700545void DMArVtdTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100546{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100547 cper_log_section_dual_binary_test("dmarvtd");
Lawrence Tangcd505202022-07-19 16:55:11 +0100548}
549
550//IOMMU DMAr tests.
Ed Tanous54640292025-03-17 20:55:20 -0700551void DMArIOMMUTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100552{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100553 cper_log_section_dual_ir_test("dmariommu");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100554}
Ed Tanous54640292025-03-17 20:55:20 -0700555void DMArIOMMUTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100556{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100557 cper_log_section_dual_binary_test("dmariommu");
Lawrence Tangcd505202022-07-19 16:55:11 +0100558}
559
560//CCIX PER tests.
Ed Tanous54640292025-03-17 20:55:20 -0700561void CCIXPERTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100562{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100563 cper_log_section_dual_ir_test("ccixper");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100564}
Ed Tanous54640292025-03-17 20:55:20 -0700565void CCIXPERTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100566{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100567 cper_log_section_dual_binary_test("ccixper");
Lawrence Tangcd505202022-07-19 16:55:11 +0100568}
569
570//CXL Protocol tests.
Ed Tanous54640292025-03-17 20:55:20 -0700571void CXLProtocolTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100572{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100573 cper_log_section_dual_ir_test("cxlprotocol");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100574}
Ed Tanous54640292025-03-17 20:55:20 -0700575void CXLProtocolTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100576{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100577 cper_log_section_dual_binary_test("cxlprotocol");
Lawrence Tangcd505202022-07-19 16:55:11 +0100578}
579
580//CXL Component tests.
Ed Tanous54640292025-03-17 20:55:20 -0700581void CXLComponentTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100582{
Lawrence Tang8f977452022-08-24 14:55:07 +0100583 cper_log_section_dual_ir_test("cxlcomponent-media");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100584}
Ed Tanous54640292025-03-17 20:55:20 -0700585void CXLComponentTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100586{
Lawrence Tang8f977452022-08-24 14:55:07 +0100587 cper_log_section_dual_binary_test("cxlcomponent-media");
Lawrence Tangcd505202022-07-19 16:55:11 +0100588}
589
Ed Tanous2d17ace2024-08-27 14:45:38 -0700590//NVIDIA section tests.
Ed Tanous54640292025-03-17 20:55:20 -0700591void NVIDIASectionTests_IRValid()
Ed Tanous2d17ace2024-08-27 14:45:38 -0700592{
593 cper_log_section_dual_ir_test("nvidia");
594}
Ed Tanous54640292025-03-17 20:55:20 -0700595void NVIDIASectionTests_BinaryEqual()
Ed Tanous2d17ace2024-08-27 14:45:38 -0700596{
597 cper_log_section_dual_binary_test("nvidia");
598}
599
Lawrence Tangcd505202022-07-19 16:55:11 +0100600//Unknown section tests.
Ed Tanous54640292025-03-17 20:55:20 -0700601void UnknownSectionTests_IRValid()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100602{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100603 cper_log_section_dual_ir_test("unknown");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100604}
Ed Tanous54640292025-03-17 20:55:20 -0700605void UnknownSectionTests_BinaryEqual()
Lawrence Tange407b4c2022-07-21 13:54:01 +0100606{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100607 cper_log_section_dual_binary_test("unknown");
Lawrence Tangcd505202022-07-19 16:55:11 +0100608}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100609
610//Entrypoint for the testing program.
Ed Tanous54640292025-03-17 20:55:20 -0700611int main()
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100612{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800613 if (GEN_EXAMPLES) {
614 cper_create_examples("arm");
615 cper_create_examples("ia32x64");
616 cper_create_examples("memory");
617 cper_create_examples("memory2");
618 cper_create_examples("pcie");
619 cper_create_examples("firmware");
620 cper_create_examples("pcibus");
621 cper_create_examples("pcidev");
622 cper_create_examples("dmargeneric");
623 cper_create_examples("dmarvtd");
624 cper_create_examples("dmariommu");
625 cper_create_examples("ccixper");
626 cper_create_examples("cxlprotocol");
627 cper_create_examples("cxlcomponent-media");
628 cper_create_examples("nvidia");
629 cper_create_examples("unknown");
630 }
Ed Tanous54640292025-03-17 20:55:20 -0700631 test_base64_encode_good();
632 test_base64_decode_good();
633 GenericProcessorTests_IRValid();
634 GenericProcessorTests_BinaryEqual();
635 IA32x64Tests_IRValid();
636 IA32x64Tests_BinaryEqual();
637 ArmTests_IRValid();
638 ArmTests_BinaryEqual();
639 MemoryTests_IRValid();
640 MemoryTests_BinaryEqual();
641 Memory2Tests_IRValid();
642 Memory2Tests_BinaryEqual();
643 PCIeTests_IRValid();
644 PCIeTests_BinaryEqual();
645 FirmwareTests_IRValid();
646 FirmwareTests_BinaryEqual();
647 PCIBusTests_IRValid();
648 PCIBusTests_BinaryEqual();
649 PCIDevTests_IRValid();
650 PCIDevTests_BinaryEqual();
651 DMArGenericTests_IRValid();
652 DMArGenericTests_BinaryEqual();
653 DMArVtdTests_IRValid();
654 DMArVtdTests_BinaryEqual();
655 DMArIOMMUTests_IRValid();
656 DMArIOMMUTests_BinaryEqual();
657 CCIXPERTests_IRValid();
658 CCIXPERTests_BinaryEqual();
659 CXLProtocolTests_IRValid();
660 CXLProtocolTests_BinaryEqual();
661 CXLComponentTests_IRValid();
662 CXLComponentTests_BinaryEqual();
663 NVIDIASectionTests_IRValid();
664 NVIDIASectionTests_BinaryEqual();
665 UnknownSectionTests_IRValid();
666 UnknownSectionTests_BinaryEqual();
667 CompileTimeAssertions_TwoWayConversion();
668 CompileTimeAssertions_ShortcodeNoSpaces();
669
670 printf("\n\nTest completed successfully.\n");
671
672 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800673}