blob: ad4ae3b0a0e3aaa2736cc0d39eb60060c72fea84 [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);
Aushim Nagarkattic1e38a12025-06-26 11:59:41 -0700117 outFile = NULL;
Ed Tanous54640292025-03-17 20:55:20 -0700118 assert(0);
119
Ed Tanousedee0a32025-03-16 17:40:04 -0700120 goto done;
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800121 }
Ed Tanous54640292025-03-17 20:55:20 -0700122 for (size_t index = 0; index < file_size; index++) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700123 char hex_str[3];
124 int out = snprintf(hex_str, sizeof(hex_str), "%02x",
125 file_data[index]);
126 if (out != 2) {
127 printf("snprintf1 failed\n");
128 goto done;
129 }
130 fwrite(hex_str, sizeof(char), 2, outFile);
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800131 if (index % 30 == 29) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700132 fwrite("\n", sizeof(char), 1, outFile);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800133 }
134 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700135 fclose(outFile);
Aushim Nagarkattic1e38a12025-06-26 11:59:41 -0700136 outFile = NULL;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800137
138 //Convert to IR, free resources.
139 rewind(record);
Ed Tanousedee0a32025-03-16 17:40:04 -0700140 ir = cper_to_ir(record);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800141 if (ir == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700142 printf("Empty JSON from CPER bin2\n");
143 assert(0);
Ed Tanousedee0a32025-03-16 17:40:04 -0700144 goto done;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800145 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800146
147 //Write json output to disk
Ed Tanousedee0a32025-03-16 17:40:04 -0700148 json_object_to_file_ext(info->json_out, ir, JSON_C_TO_STRING_PRETTY);
Ed Tanousa3663052025-03-16 12:54:36 -0700149 json_object_put(ir);
150
Ed Tanousedee0a32025-03-16 17:40:04 -0700151done:
152 free_file_info(info);
153 if (record != NULL) {
154 fclose(record);
155 }
156 if (outFile != NULL) {
157 fclose(outFile);
158 }
Ed Tanousa3663052025-03-16 12:54:36 -0700159 free(buf);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800160}
161
Ed Tanousedee0a32025-03-16 17:40:04 -0700162int hex2int(char ch)
163{
164 if ((ch >= '0') && (ch <= '9')) {
165 return ch - '0';
166 }
167 if ((ch >= 'A') && (ch <= 'F')) {
168 return ch - 'A' + 10;
169 }
170 if ((ch >= 'a') && (ch <= 'f')) {
171 return ch - 'a' + 10;
172 }
173 return -1;
174}
175
Ed Tanous54640292025-03-17 20:55:20 -0700176int string_to_binary(const char *source, size_t length, unsigned char **retval)
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800177{
Ed Tanous54640292025-03-17 20:55:20 -0700178 size_t retval_size = length * 2;
179 *retval = malloc(retval_size);
180 int uppernibble = 1;
181
182 size_t ret_index = 0;
183
Ed Tanousedee0a32025-03-16 17:40:04 -0700184 for (size_t i = 0; i < length; i++) {
185 char c = source[i];
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800186 if (c == '\n') {
187 continue;
188 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700189 int val = hex2int(c);
190 if (val < 0) {
191 printf("Invalid hex character in test file: %c\n", c);
Ed Tanous54640292025-03-17 20:55:20 -0700192 return -1;
Ed Tanousedee0a32025-03-16 17:40:04 -0700193 }
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800194
195 if (uppernibble) {
Ed Tanous54640292025-03-17 20:55:20 -0700196 (*retval)[ret_index] = (unsigned char)(val << 4);
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800197 } else {
Ed Tanous54640292025-03-17 20:55:20 -0700198 (*retval)[ret_index] += (unsigned char)val;
199 ret_index++;
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800200 }
201 uppernibble = !uppernibble;
202 }
Ed Tanous54640292025-03-17 20:55:20 -0700203 return ret_index;
Ed Tanousa2dce4b2025-03-05 15:33:06 -0800204}
205
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800206//Tests fixed CPER sections for IR validity with an example set.
207void cper_example_section_ir_test(const char *section_name)
208{
209 //Open CPER record for the given type.
Ed Tanous54640292025-03-17 20:55:20 -0700210 struct file_info *info = file_info_init(section_name);
Ed Tanousedee0a32025-03-16 17:40:04 -0700211 if (info == NULL) {
212 return;
213 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800214
Ed Tanousedee0a32025-03-16 17:40:04 -0700215 FILE *cper_file = fopen(info->cper_out, "rb");
216 if (cper_file == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700217 printf("Failed to open CPER file: %s\n", info->cper_out);
Ed Tanousedee0a32025-03-16 17:40:04 -0700218 free_file_info(info);
Ed Tanous54640292025-03-17 20:55:20 -0700219 assert(0);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800220 return;
221 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700222 fseek(cper_file, 0, SEEK_END);
223 size_t length = ftell(cper_file);
224 fseek(cper_file, 0, SEEK_SET);
225 char *buffer = (char *)malloc(length);
226 if (!buffer) {
227 free_file_info(info);
228 return;
229 }
230 if (fread(buffer, 1, length, cper_file) != length) {
Ed Tanous54640292025-03-17 20:55:20 -0700231 printf("Failed to read CPER file: %s\n", info->cper_out);
Ed Tanousedee0a32025-03-16 17:40:04 -0700232 free(buffer);
233 free_file_info(info);
234 return;
235 }
236 fclose(cper_file);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800237
Ed Tanous54640292025-03-17 20:55:20 -0700238 unsigned char *cper_bin;
239 int cper_bin_len = string_to_binary(buffer, length, &cper_bin);
240 if (cper_bin_len <= 0) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700241 free(buffer);
242 free_file_info(info);
Ed Tanous54640292025-03-17 20:55:20 -0700243 assert(0);
244 return;
245 }
246 printf("cper_bin: %s\n", cper_bin);
247 printf("cper_bin_len: %d\n", cper_bin_len);
248
249 //Convert to IR, free resources.
250 json_object *ir = cper_buf_to_ir(cper_bin, cper_bin_len);
251 if (ir == NULL) {
252 printf("Empty JSON from CPER bin3\n");
253 free(cper_bin);
254 free(buffer);
255 free_file_info(info);
256 assert(0);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800257 return;
258 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800259
Ed Tanousedee0a32025-03-16 17:40:04 -0700260 json_object *expected = json_object_from_file(info->json_out);
Ed Tanous54640292025-03-17 20:55:20 -0700261 assert(expected != NULL);
262 if (expected == NULL) {
Ed Tanousedee0a32025-03-16 17:40:04 -0700263 free(buffer);
Ed Tanous54640292025-03-17 20:55:20 -0700264 free(cper_bin);
Ed Tanousedee0a32025-03-16 17:40:04 -0700265 free_file_info(info);
Ed Tanousa3663052025-03-16 12:54:36 -0700266 const char *str = json_object_to_json_string(ir);
267
268 const char *expected_str = json_object_to_json_string(expected);
Ed Tanous54640292025-03-17 20:55:20 -0700269 assert(strcmp(str, expected_str) == 0);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800270 return;
271 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800272
Ed Tanous54640292025-03-17 20:55:20 -0700273 assert(json_object_equal(ir, expected));
Ed Tanousedee0a32025-03-16 17:40:04 -0700274 free(buffer);
Ed Tanous54640292025-03-17 20:55:20 -0700275 free(cper_bin);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800276 json_object_put(ir);
Ed Tanousa3663052025-03-16 12:54:36 -0700277 json_object_put(expected);
Ed Tanousedee0a32025-03-16 17:40:04 -0700278 free_file_info(info);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800279}
Lawrence Tangcd505202022-07-19 16:55:11 +0100280
281//Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800282void cper_log_section_ir_test(const char *section_name, int single_section,
283 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100284{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100285 //Generate full CPER record for the given type.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100286 char *buf;
287 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100288 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800289 single_section, validBitsType);
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100290
Lawrence Tange407b4c2022-07-21 13:54:01 +0100291 //Convert to IR, free resources.
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100292 json_object *ir;
John Chungf8fc7052024-05-03 20:05:29 +0800293 if (single_section) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100294 ir = cper_single_section_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800295 } else {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100296 ir = cper_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800297 }
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800298
Lawrence Tange407b4c2022-07-21 13:54:01 +0100299 fclose(record);
300 free(buf);
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100301
Lawrence Tange407b4c2022-07-21 13:54:01 +0100302 //Validate against schema.
Ed Tanousa3663052025-03-16 12:54:36 -0700303 int valid = schema_validate_from_file(ir, single_section,
304 /*all_valid_bits*/ 1);
John Chungf8fc7052024-05-03 20:05:29 +0800305 json_object_put(ir);
Ed Tanous54640292025-03-17 20:55:20 -0700306
307 if (valid < 0) {
308 printf("IR validation test failed (single section mode = %d)\n",
309 single_section);
310 assert(0);
311 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100312}
313
John Chungfa6c53a2025-07-28 16:45:23 -0500314//Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
315void cper_buf_log_section_ir_test(const char *section_name, int single_section,
316 GEN_VALID_BITS_TEST_TYPE validBitsType)
317{
318 //Generate full CPER record for the given type.
319 char *buf;
320 size_t size;
321 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
322 single_section, validBitsType);
323
324 //Convert.
325 json_object *ir;
326 if (single_section) {
327 ir = cper_buf_single_section_to_ir((UINT8 *)buf, size);
328 } else {
329 ir = cper_buf_to_ir((UINT8 *)buf, size);
330 }
331 fclose(record);
332 free(buf);
333
334 if (!ir) {
335 printf("IR validation test failed (%d) : json object empty \n",
336 single_section);
337 assert(0);
338 }
339
340 //Validate against schema.
341 int valid = schema_validate_from_file(ir, single_section,
342 /*all_valid_bits*/ 1);
343 json_object_put(ir);
344
345 if (valid < 0) {
346 printf("IR validation test failed (single section mode = %d)\n",
347 single_section);
348 assert(0);
349 }
350}
351
Ed Tanousedee0a32025-03-16 17:40:04 -0700352int to_hex(const unsigned char *input, size_t size, char **out)
Ed Tanous50b966f2025-03-11 09:06:19 -0700353{
Ed Tanousedee0a32025-03-16 17:40:04 -0700354 *out = (char *)malloc(size * 2);
355 if (out == NULL) {
356 return -1;
Ed Tanous50b966f2025-03-11 09:06:19 -0700357 }
Ed Tanousedee0a32025-03-16 17:40:04 -0700358 int out_index = 0;
359 for (size_t i = 0; i < size; i++) {
360 unsigned char c = input[i];
361 char hex_str[3];
362 int n = snprintf(hex_str, sizeof(hex_str), "%02x", c);
363 if (n != 2) {
364 printf("snprintf2 failed with code %d\n", n);
365 return -1;
366 }
367 (*out)[out_index] = hex_str[0];
368 out_index++;
369 (*out)[out_index] = hex_str[1];
370 out_index++;
371 }
372 return out_index;
Ed Tanous50b966f2025-03-11 09:06:19 -0700373}
374
Lawrence Tangcd505202022-07-19 16:55:11 +0100375//Checks for binary round-trip equality for a given randomly generated CPER record.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800376void cper_log_section_binary_test(const char *section_name, int single_section,
377 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangcd505202022-07-19 16:55:11 +0100378{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100379 //Generate CPER record for the given type.
380 char *buf;
381 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100382 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800383 single_section, validBitsType);
384 if (record == NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700385 printf("Could not generate memstream for binary test");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800386 return;
387 }
Lawrence Tangcd505202022-07-19 16:55:11 +0100388
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100389 //Convert to IR.
390 json_object *ir;
John Chungf8fc7052024-05-03 20:05:29 +0800391 if (single_section) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100392 ir = cper_single_section_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800393 } else {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100394 ir = cper_to_ir(record);
John Chungf8fc7052024-05-03 20:05:29 +0800395 }
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100396
397 //Now convert back to binary, and get a stream out.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100398 char *cper_buf;
399 size_t cper_buf_size;
400 FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
John Chungf8fc7052024-05-03 20:05:29 +0800401 if (single_section) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100402 ir_single_section_to_cper(ir, stream);
John Chungf8fc7052024-05-03 20:05:29 +0800403 } else {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100404 ir_to_cper(ir, stream);
John Chungf8fc7052024-05-03 20:05:29 +0800405 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100406 fclose(stream);
Lawrence Tangcd505202022-07-19 16:55:11 +0100407
Ed Tanousedee0a32025-03-16 17:40:04 -0700408 printf("size: %zu, cper_buf_size: %zu\n", size, cper_buf_size);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100409
Ed Tanousedee0a32025-03-16 17:40:04 -0700410 char *buf_hex;
411 int buf_hex_len = to_hex((unsigned char *)buf, size, &buf_hex);
412 char *cper_buf_hex;
413 int cper_buf_hex_len =
414 to_hex((unsigned char *)cper_buf, cper_buf_size, &cper_buf_hex);
415
Ed Tanous9f260e52025-04-24 09:37:59 -0700416 printf("%.*s\n", cper_buf_hex_len, cper_buf_hex);
417 printf("%.*s\n", buf_hex_len, buf_hex);
Ed Tanous54640292025-03-17 20:55:20 -0700418 assert(buf_hex_len == cper_buf_hex_len);
419 assert(memcmp(buf_hex, cper_buf_hex, buf_hex_len) == 0);
Ed Tanousedee0a32025-03-16 17:40:04 -0700420
Ed Tanousedee0a32025-03-16 17:40:04 -0700421 free(buf_hex);
Ed Tanous54640292025-03-17 20:55:20 -0700422 free(cper_buf_hex);
423
Lawrence Tange407b4c2022-07-21 13:54:01 +0100424 //Free everything up.
425 fclose(record);
426 free(buf);
427 free(cper_buf);
John Chungf8fc7052024-05-03 20:05:29 +0800428 json_object_put(ir);
Lawrence Tangcd505202022-07-19 16:55:11 +0100429}
430
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100431//Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
432void cper_log_section_dual_ir_test(const char *section_name)
433{
John Chungfa6c53a2025-07-28 16:45:23 -0500434 // Test with file based APIs
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800435 cper_log_section_ir_test(section_name, 0, allValidbitsSet);
436 cper_log_section_ir_test(section_name, 1, allValidbitsSet);
John Chungfa6c53a2025-07-28 16:45:23 -0500437
438 // Test with buffer based APIs
439 cper_buf_log_section_ir_test(section_name, 0, allValidbitsSet);
440 cper_buf_log_section_ir_test(section_name, 1, allValidbitsSet);
441
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800442 //Validate against examples
443 cper_example_section_ir_test(section_name);
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100444}
445
446//Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
447void cper_log_section_dual_binary_test(const char *section_name)
448{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800449 cper_log_section_binary_test(section_name, 0, allValidbitsSet);
450 cper_log_section_binary_test(section_name, 1, allValidbitsSet);
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100451}
452
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100453/*
Lawrence Tang580423f2022-08-24 09:37:53 +0100454* Non-single section assertions.
455*/
Ed Tanous398899a2025-07-25 11:07:32 -0700456void CompileTimeAssertions_TwoWayConversion(void)
Lawrence Tang580423f2022-08-24 09:37:53 +0100457{
John Chungf8fc7052024-05-03 20:05:29 +0800458 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tang580423f2022-08-24 09:37:53 +0100459 //If a conversion one way exists, a conversion the other way must exist.
John Chungf8fc7052024-05-03 20:05:29 +0800460 if (section_definitions[i].ToCPER != NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700461 assert(section_definitions[i].ToIR != NULL);
John Chungf8fc7052024-05-03 20:05:29 +0800462 }
463 if (section_definitions[i].ToIR != NULL) {
Ed Tanous54640292025-03-17 20:55:20 -0700464 assert(section_definitions[i].ToCPER != NULL);
John Chungf8fc7052024-05-03 20:05:29 +0800465 }
Lawrence Tang580423f2022-08-24 09:37:53 +0100466 }
467}
468
Ed Tanous398899a2025-07-25 11:07:32 -0700469void CompileTimeAssertions_ShortcodeNoSpaces(void)
Lawrence Tang40519cb2022-08-24 15:50:08 +0100470{
John Chungf8fc7052024-05-03 20:05:29 +0800471 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang40519cb2022-08-24 15:50:08 +0100472 for (int j = 0;
473 generator_definitions[i].ShortName[j + 1] != '\0'; j++) {
Ed Tanous54640292025-03-17 20:55:20 -0700474 assert(isspace(generator_definitions[i].ShortName[j]) ==
475 0);
Lawrence Tang40519cb2022-08-24 15:50:08 +0100476 }
477 }
478}
479
Lawrence Tang580423f2022-08-24 09:37:53 +0100480/*
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100481* Single section tests.
482*/
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100483
Lawrence Tangcd505202022-07-19 16:55:11 +0100484//Generic processor tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700485void GenericProcessorTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100486{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100487 cper_log_section_dual_ir_test("generic");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100488}
Ed Tanous398899a2025-07-25 11:07:32 -0700489void GenericProcessorTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100490{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100491 cper_log_section_dual_binary_test("generic");
Lawrence Tangcd505202022-07-19 16:55:11 +0100492}
493
494//IA32/x64 tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700495void IA32x64Tests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100496{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100497 cper_log_section_dual_ir_test("ia32x64");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100498}
Ed Tanous398899a2025-07-25 11:07:32 -0700499void IA32x64Tests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100500{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100501 cper_log_section_dual_binary_test("ia32x64");
Lawrence Tangcd505202022-07-19 16:55:11 +0100502}
503
Ed Tanous54640292025-03-17 20:55:20 -0700504// void IPFTests_IRValid() {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100505// cper_log_section_dual_ir_test("ipf");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100506// }
Lawrence Tangcd505202022-07-19 16:55:11 +0100507
508//ARM tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700509void ArmTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100510{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100511 cper_log_section_dual_ir_test("arm");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100512}
Ed Tanous398899a2025-07-25 11:07:32 -0700513void ArmTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100514{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100515 cper_log_section_dual_binary_test("arm");
Lawrence Tangcd505202022-07-19 16:55:11 +0100516}
517
518//Memory tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700519void MemoryTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100520{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100521 cper_log_section_dual_ir_test("memory");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100522}
Ed Tanous398899a2025-07-25 11:07:32 -0700523void MemoryTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100524{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100525 cper_log_section_dual_binary_test("memory");
Lawrence Tangcd505202022-07-19 16:55:11 +0100526}
527
528//Memory 2 tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700529void Memory2Tests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100530{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100531 cper_log_section_dual_ir_test("memory2");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100532}
Ed Tanous398899a2025-07-25 11:07:32 -0700533void Memory2Tests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100534{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100535 cper_log_section_dual_binary_test("memory2");
Lawrence Tangcd505202022-07-19 16:55:11 +0100536}
537
538//PCIe tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700539void PCIeTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100540{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100541 cper_log_section_dual_ir_test("pcie");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100542}
Ed Tanous398899a2025-07-25 11:07:32 -0700543void PCIeTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100544{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100545 cper_log_section_dual_binary_test("pcie");
Lawrence Tangcd505202022-07-19 16:55:11 +0100546}
547
548//Firmware tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700549void FirmwareTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100550{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100551 cper_log_section_dual_ir_test("firmware");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100552}
Ed Tanous398899a2025-07-25 11:07:32 -0700553void FirmwareTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100554{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100555 cper_log_section_dual_binary_test("firmware");
Lawrence Tangcd505202022-07-19 16:55:11 +0100556}
557
558//PCI Bus tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700559void PCIBusTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100560{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100561 cper_log_section_dual_ir_test("pcibus");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100562}
Ed Tanous398899a2025-07-25 11:07:32 -0700563void PCIBusTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100564{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100565 cper_log_section_dual_binary_test("pcibus");
Lawrence Tangcd505202022-07-19 16:55:11 +0100566}
567
568//PCI Device tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700569void PCIDevTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100570{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100571 cper_log_section_dual_ir_test("pcidev");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100572}
Ed Tanous398899a2025-07-25 11:07:32 -0700573void PCIDevTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100574{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100575 cper_log_section_dual_binary_test("pcidev");
Lawrence Tangcd505202022-07-19 16:55:11 +0100576}
577
578//Generic DMAr tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700579void DMArGenericTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100580{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100581 cper_log_section_dual_ir_test("dmargeneric");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100582}
Ed Tanous398899a2025-07-25 11:07:32 -0700583void DMArGenericTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100584{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100585 cper_log_section_dual_binary_test("dmargeneric");
Lawrence Tangcd505202022-07-19 16:55:11 +0100586}
587
588//VT-d DMAr tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700589void DMArVtdTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100590{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100591 cper_log_section_dual_ir_test("dmarvtd");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100592}
Ed Tanous398899a2025-07-25 11:07:32 -0700593void DMArVtdTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100594{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100595 cper_log_section_dual_binary_test("dmarvtd");
Lawrence Tangcd505202022-07-19 16:55:11 +0100596}
597
598//IOMMU DMAr tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700599void DMArIOMMUTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100600{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100601 cper_log_section_dual_ir_test("dmariommu");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100602}
Ed Tanous398899a2025-07-25 11:07:32 -0700603void DMArIOMMUTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100604{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100605 cper_log_section_dual_binary_test("dmariommu");
Lawrence Tangcd505202022-07-19 16:55:11 +0100606}
607
608//CCIX PER tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700609void CCIXPERTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100610{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100611 cper_log_section_dual_ir_test("ccixper");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100612}
Ed Tanous398899a2025-07-25 11:07:32 -0700613void CCIXPERTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100614{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100615 cper_log_section_dual_binary_test("ccixper");
Lawrence Tangcd505202022-07-19 16:55:11 +0100616}
617
618//CXL Protocol tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700619void CXLProtocolTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100620{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100621 cper_log_section_dual_ir_test("cxlprotocol");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100622}
Ed Tanous398899a2025-07-25 11:07:32 -0700623void CXLProtocolTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100624{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100625 cper_log_section_dual_binary_test("cxlprotocol");
Lawrence Tangcd505202022-07-19 16:55:11 +0100626}
627
628//CXL Component tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700629void CXLComponentTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100630{
Lawrence Tang8f977452022-08-24 14:55:07 +0100631 cper_log_section_dual_ir_test("cxlcomponent-media");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100632}
Ed Tanous398899a2025-07-25 11:07:32 -0700633void CXLComponentTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100634{
Lawrence Tang8f977452022-08-24 14:55:07 +0100635 cper_log_section_dual_binary_test("cxlcomponent-media");
Lawrence Tangcd505202022-07-19 16:55:11 +0100636}
637
Ed Tanous2d17ace2024-08-27 14:45:38 -0700638//NVIDIA section tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700639void NVIDIASectionTests_IRValid(void)
Ed Tanous2d17ace2024-08-27 14:45:38 -0700640{
641 cper_log_section_dual_ir_test("nvidia");
642}
Ed Tanous398899a2025-07-25 11:07:32 -0700643void NVIDIASectionTests_BinaryEqual(void)
Ed Tanous2d17ace2024-08-27 14:45:38 -0700644{
645 cper_log_section_dual_binary_test("nvidia");
646}
647
Ed Tanous398899a2025-07-25 11:07:32 -0700648void NVIDIACMETSectionTests_IRValid(void)
Ed Tanous55968b12025-05-06 21:04:52 -0700649{
650 cper_example_section_ir_test("nvidia_cmet_info");
651}
652
Lawrence Tangcd505202022-07-19 16:55:11 +0100653//Unknown section tests.
Ed Tanous398899a2025-07-25 11:07:32 -0700654void UnknownSectionTests_IRValid(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100655{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100656 cper_log_section_dual_ir_test("unknown");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100657}
Ed Tanous398899a2025-07-25 11:07:32 -0700658void UnknownSectionTests_BinaryEqual(void)
Lawrence Tange407b4c2022-07-21 13:54:01 +0100659{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100660 cper_log_section_dual_binary_test("unknown");
Lawrence Tangcd505202022-07-19 16:55:11 +0100661}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100662
663//Entrypoint for the testing program.
Ed Tanous398899a2025-07-25 11:07:32 -0700664int main(void)
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100665{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800666 if (GEN_EXAMPLES) {
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -0700667 cper_create_examples("generic");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800668 cper_create_examples("arm");
669 cper_create_examples("ia32x64");
670 cper_create_examples("memory");
671 cper_create_examples("memory2");
672 cper_create_examples("pcie");
673 cper_create_examples("firmware");
674 cper_create_examples("pcibus");
675 cper_create_examples("pcidev");
676 cper_create_examples("dmargeneric");
677 cper_create_examples("dmarvtd");
678 cper_create_examples("dmariommu");
679 cper_create_examples("ccixper");
680 cper_create_examples("cxlprotocol");
681 cper_create_examples("cxlcomponent-media");
682 cper_create_examples("nvidia");
683 cper_create_examples("unknown");
684 }
Ed Tanous54640292025-03-17 20:55:20 -0700685 test_base64_encode_good();
686 test_base64_decode_good();
687 GenericProcessorTests_IRValid();
688 GenericProcessorTests_BinaryEqual();
689 IA32x64Tests_IRValid();
690 IA32x64Tests_BinaryEqual();
691 ArmTests_IRValid();
692 ArmTests_BinaryEqual();
693 MemoryTests_IRValid();
694 MemoryTests_BinaryEqual();
695 Memory2Tests_IRValid();
696 Memory2Tests_BinaryEqual();
697 PCIeTests_IRValid();
698 PCIeTests_BinaryEqual();
699 FirmwareTests_IRValid();
700 FirmwareTests_BinaryEqual();
701 PCIBusTests_IRValid();
702 PCIBusTests_BinaryEqual();
703 PCIDevTests_IRValid();
704 PCIDevTests_BinaryEqual();
705 DMArGenericTests_IRValid();
706 DMArGenericTests_BinaryEqual();
707 DMArVtdTests_IRValid();
708 DMArVtdTests_BinaryEqual();
709 DMArIOMMUTests_IRValid();
710 DMArIOMMUTests_BinaryEqual();
711 CCIXPERTests_IRValid();
712 CCIXPERTests_BinaryEqual();
713 CXLProtocolTests_IRValid();
714 CXLProtocolTests_BinaryEqual();
715 CXLComponentTests_IRValid();
716 CXLComponentTests_BinaryEqual();
717 NVIDIASectionTests_IRValid();
718 NVIDIASectionTests_BinaryEqual();
Ed Tanous55968b12025-05-06 21:04:52 -0700719 NVIDIACMETSectionTests_IRValid();
Ed Tanous54640292025-03-17 20:55:20 -0700720 UnknownSectionTests_IRValid();
721 UnknownSectionTests_BinaryEqual();
722 CompileTimeAssertions_TwoWayConversion();
723 CompileTimeAssertions_ShortcodeNoSpaces();
724
725 printf("\n\nTest completed successfully.\n");
726
727 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800728}