xref: /openbmc/libcper/tests/ir-tests.c (revision 55968b12e1e0aaaf0e70215e47d8a93706dab21b)
154640298SEd Tanous /**
254640298SEd Tanous  * Defines tests for validating CPER-JSON IR output from the cper-parse library.
354640298SEd Tanous  *
454640298SEd Tanous  * Author: Lawrence.Tang@arm.com
554640298SEd Tanous  **/
654640298SEd Tanous 
754640298SEd Tanous #include "test-utils.h"
854640298SEd Tanous #include "string.h"
954640298SEd Tanous #include "assert.h"
1054640298SEd Tanous #include <ctype.h>
1154640298SEd Tanous #include <json.h>
1254640298SEd Tanous #include <libcper/cper-parse.h>
1354640298SEd Tanous #include <libcper/generator/cper-generate.h>
1454640298SEd Tanous #include <libcper/generator/sections/gen-section.h>
1554640298SEd Tanous #include <libcper/json-schema.h>
1654640298SEd Tanous #include <libcper/sections/cper-section.h>
1754640298SEd Tanous 
1854640298SEd Tanous #include "base64_test.h"
1954640298SEd Tanous 
2054640298SEd Tanous /*
2154640298SEd Tanous * Test templates.
2254640298SEd Tanous */
2354640298SEd Tanous static const GEN_VALID_BITS_TEST_TYPE allValidbitsSet = ALL_VALID;
2454640298SEd Tanous static const GEN_VALID_BITS_TEST_TYPE fixedValidbitsSet = SOME_VALID;
2554640298SEd Tanous static const int GEN_EXAMPLES = 0;
2654640298SEd Tanous 
2754640298SEd Tanous static const char *cper_ext = "cperhex";
2854640298SEd Tanous static const char *json_ext = "json";
2954640298SEd Tanous 
3054640298SEd Tanous struct file_info {
3154640298SEd Tanous 	char *cper_out;
3254640298SEd Tanous 	char *json_out;
3354640298SEd Tanous };
3454640298SEd Tanous 
free_file_info(struct file_info * info)3554640298SEd Tanous void free_file_info(struct file_info *info)
3654640298SEd Tanous {
3754640298SEd Tanous 	if (info == NULL) {
3854640298SEd Tanous 		return;
3954640298SEd Tanous 	}
4054640298SEd Tanous 	free(info->cper_out);
4154640298SEd Tanous 	free(info->json_out);
4254640298SEd Tanous 	free(info);
4354640298SEd Tanous }
4454640298SEd Tanous 
file_info_init(const char * section_name)4554640298SEd Tanous struct file_info *file_info_init(const char *section_name)
4654640298SEd Tanous {
4754640298SEd Tanous 	struct file_info *info = NULL;
4854640298SEd Tanous 	char *buf = NULL;
4954640298SEd Tanous 	size_t size;
5054640298SEd Tanous 	int ret;
5154640298SEd Tanous 
5254640298SEd Tanous 	info = (struct file_info *)calloc(1, sizeof(struct file_info));
5354640298SEd Tanous 	if (info == NULL) {
5454640298SEd Tanous 		goto fail;
5554640298SEd Tanous 	}
5654640298SEd Tanous 
5754640298SEd Tanous 	size = strlen(LIBCPER_EXAMPLES) + 1 + strlen(section_name) + 1 +
5854640298SEd Tanous 	       strlen(cper_ext) + 1;
5954640298SEd Tanous 	info->cper_out = (char *)malloc(size);
6054640298SEd Tanous 	ret = snprintf(info->cper_out, size, "%s/%s.%s", LIBCPER_EXAMPLES,
6154640298SEd Tanous 		       section_name, cper_ext);
6254640298SEd Tanous 	if (ret != (int)size - 1) {
6354640298SEd Tanous 		printf("snprintf0 failed\n");
6454640298SEd Tanous 		goto fail;
6554640298SEd Tanous 	}
6654640298SEd Tanous 	size = strlen(LIBCPER_EXAMPLES) + 1 + strlen(section_name) + 1 +
6754640298SEd Tanous 	       strlen(json_ext) + 1;
6854640298SEd Tanous 	info->json_out = (char *)malloc(size);
6954640298SEd Tanous 	ret = snprintf(info->json_out, size, "%s/%s.%s", LIBCPER_EXAMPLES,
7054640298SEd Tanous 		       section_name, json_ext);
7154640298SEd Tanous 	if (ret != (int)size - 1) {
7254640298SEd Tanous 		printf("snprintf3 failed\n");
7354640298SEd Tanous 		goto fail;
7454640298SEd Tanous 	}
7554640298SEd Tanous 	free(buf);
7654640298SEd Tanous 	return info;
7754640298SEd Tanous 
7854640298SEd Tanous fail:
7954640298SEd Tanous 	free(buf);
8054640298SEd Tanous 	free_file_info(info);
8154640298SEd Tanous 	return NULL;
8254640298SEd Tanous }
8354640298SEd Tanous 
cper_create_examples(const char * section_name)8454640298SEd Tanous void cper_create_examples(const char *section_name)
8554640298SEd Tanous {
8654640298SEd Tanous 	//Generate full CPER record for the given type.
8754640298SEd Tanous 	json_object *ir = NULL;
8854640298SEd Tanous 	size_t size;
8954640298SEd Tanous 	size_t file_size;
9054640298SEd Tanous 	FILE *outFile = NULL;
9154640298SEd Tanous 	unsigned char *file_data;
9254640298SEd Tanous 	FILE *record = NULL;
9354640298SEd Tanous 	char *buf = NULL;
9454640298SEd Tanous 	struct file_info *info = file_info_init(section_name);
9554640298SEd Tanous 	if (info == NULL) {
9654640298SEd Tanous 		goto done;
9754640298SEd Tanous 	}
9854640298SEd Tanous 
9954640298SEd Tanous 	record = generate_record_memstream(&section_name, 1, &buf, &size, 0,
10054640298SEd Tanous 					   fixedValidbitsSet);
10154640298SEd Tanous 
10254640298SEd Tanous 	// Write example CPER to disk
10354640298SEd Tanous 	outFile = fopen(info->cper_out, "wb");
10454640298SEd Tanous 	if (outFile == NULL) {
10554640298SEd Tanous 		printf("Failed to create/open CPER output file: %s\n",
10654640298SEd Tanous 		       info->cper_out);
10754640298SEd Tanous 		goto done;
10854640298SEd Tanous 	}
10954640298SEd Tanous 
11054640298SEd Tanous 	fseek(record, 0, SEEK_END);
11154640298SEd Tanous 	file_size = ftell(record);
11254640298SEd Tanous 	rewind(record);
11354640298SEd Tanous 	file_data = malloc(file_size);
11454640298SEd Tanous 	if (fread(file_data, 1, file_size, record) != file_size) {
11554640298SEd Tanous 		printf("Failed to read CPER data from memstream.");
11654640298SEd Tanous 		fclose(outFile);
11754640298SEd Tanous 		assert(0);
11854640298SEd Tanous 
11954640298SEd Tanous 		goto done;
12054640298SEd Tanous 	}
12154640298SEd Tanous 	for (size_t index = 0; index < file_size; index++) {
12254640298SEd Tanous 		char hex_str[3];
12354640298SEd Tanous 		int out = snprintf(hex_str, sizeof(hex_str), "%02x",
12454640298SEd Tanous 				   file_data[index]);
12554640298SEd Tanous 		if (out != 2) {
12654640298SEd Tanous 			printf("snprintf1 failed\n");
12754640298SEd Tanous 			goto done;
12854640298SEd Tanous 		}
12954640298SEd Tanous 		fwrite(hex_str, sizeof(char), 2, outFile);
13054640298SEd Tanous 		if (index % 30 == 29) {
13154640298SEd Tanous 			fwrite("\n", sizeof(char), 1, outFile);
13254640298SEd Tanous 		}
13354640298SEd Tanous 	}
13454640298SEd Tanous 	fclose(outFile);
13554640298SEd Tanous 
13654640298SEd Tanous 	//Convert to IR, free resources.
13754640298SEd Tanous 	rewind(record);
13854640298SEd Tanous 	ir = cper_to_ir(record);
13954640298SEd Tanous 	if (ir == NULL) {
14054640298SEd Tanous 		printf("Empty JSON from CPER bin2\n");
14154640298SEd Tanous 		assert(0);
14254640298SEd Tanous 		goto done;
14354640298SEd Tanous 	}
14454640298SEd Tanous 
14554640298SEd Tanous 	//Write json output to disk
14654640298SEd Tanous 	json_object_to_file_ext(info->json_out, ir, JSON_C_TO_STRING_PRETTY);
14754640298SEd Tanous 	json_object_put(ir);
14854640298SEd Tanous 
14954640298SEd Tanous done:
15054640298SEd Tanous 	free_file_info(info);
15154640298SEd Tanous 	if (record != NULL) {
15254640298SEd Tanous 		fclose(record);
15354640298SEd Tanous 	}
15454640298SEd Tanous 	if (outFile != NULL) {
15554640298SEd Tanous 		fclose(outFile);
15654640298SEd Tanous 	}
15754640298SEd Tanous 	free(buf);
15854640298SEd Tanous }
15954640298SEd Tanous 
hex2int(char ch)16054640298SEd Tanous int hex2int(char ch)
16154640298SEd Tanous {
16254640298SEd Tanous 	if ((ch >= '0') && (ch <= '9')) {
16354640298SEd Tanous 		return ch - '0';
16454640298SEd Tanous 	}
16554640298SEd Tanous 	if ((ch >= 'A') && (ch <= 'F')) {
16654640298SEd Tanous 		return ch - 'A' + 10;
16754640298SEd Tanous 	}
16854640298SEd Tanous 	if ((ch >= 'a') && (ch <= 'f')) {
16954640298SEd Tanous 		return ch - 'a' + 10;
17054640298SEd Tanous 	}
17154640298SEd Tanous 	return -1;
17254640298SEd Tanous }
17354640298SEd Tanous 
string_to_binary(const char * source,size_t length,unsigned char ** retval)17454640298SEd Tanous int string_to_binary(const char *source, size_t length, unsigned char **retval)
17554640298SEd Tanous {
17654640298SEd Tanous 	size_t retval_size = length * 2;
17754640298SEd Tanous 	*retval = malloc(retval_size);
17854640298SEd Tanous 	int uppernibble = 1;
17954640298SEd Tanous 
18054640298SEd Tanous 	size_t ret_index = 0;
18154640298SEd Tanous 
18254640298SEd Tanous 	for (size_t i = 0; i < length; i++) {
18354640298SEd Tanous 		char c = source[i];
18454640298SEd Tanous 		if (c == '\n') {
18554640298SEd Tanous 			continue;
18654640298SEd Tanous 		}
18754640298SEd Tanous 		int val = hex2int(c);
18854640298SEd Tanous 		if (val < 0) {
18954640298SEd Tanous 			printf("Invalid hex character in test file: %c\n", c);
19054640298SEd Tanous 			return -1;
19154640298SEd Tanous 		}
19254640298SEd Tanous 
19354640298SEd Tanous 		if (uppernibble) {
19454640298SEd Tanous 			(*retval)[ret_index] = (unsigned char)(val << 4);
19554640298SEd Tanous 		} else {
19654640298SEd Tanous 			(*retval)[ret_index] += (unsigned char)val;
19754640298SEd Tanous 			ret_index++;
19854640298SEd Tanous 		}
19954640298SEd Tanous 		uppernibble = !uppernibble;
20054640298SEd Tanous 	}
20154640298SEd Tanous 	return ret_index;
20254640298SEd Tanous }
20354640298SEd Tanous 
20454640298SEd Tanous //Tests fixed CPER sections for IR validity with an example set.
cper_example_section_ir_test(const char * section_name)20554640298SEd Tanous void cper_example_section_ir_test(const char *section_name)
20654640298SEd Tanous {
20754640298SEd Tanous 	//Open CPER record for the given type.
20854640298SEd Tanous 	struct file_info *info = file_info_init(section_name);
20954640298SEd Tanous 	if (info == NULL) {
21054640298SEd Tanous 		return;
21154640298SEd Tanous 	}
21254640298SEd Tanous 
21354640298SEd Tanous 	FILE *cper_file = fopen(info->cper_out, "rb");
21454640298SEd Tanous 	if (cper_file == NULL) {
21554640298SEd Tanous 		printf("Failed to open CPER file: %s\n", info->cper_out);
21654640298SEd Tanous 		free_file_info(info);
21754640298SEd Tanous 		assert(0);
21854640298SEd Tanous 		return;
21954640298SEd Tanous 	}
22054640298SEd Tanous 	fseek(cper_file, 0, SEEK_END);
22154640298SEd Tanous 	size_t length = ftell(cper_file);
22254640298SEd Tanous 	fseek(cper_file, 0, SEEK_SET);
22354640298SEd Tanous 	char *buffer = (char *)malloc(length);
22454640298SEd Tanous 	if (!buffer) {
22554640298SEd Tanous 		free_file_info(info);
22654640298SEd Tanous 		return;
22754640298SEd Tanous 	}
22854640298SEd Tanous 	if (fread(buffer, 1, length, cper_file) != length) {
22954640298SEd Tanous 		printf("Failed to read CPER file: %s\n", info->cper_out);
23054640298SEd Tanous 		free(buffer);
23154640298SEd Tanous 		free_file_info(info);
23254640298SEd Tanous 		return;
23354640298SEd Tanous 	}
23454640298SEd Tanous 	fclose(cper_file);
23554640298SEd Tanous 
23654640298SEd Tanous 	unsigned char *cper_bin;
23754640298SEd Tanous 	int cper_bin_len = string_to_binary(buffer, length, &cper_bin);
23854640298SEd Tanous 	if (cper_bin_len <= 0) {
23954640298SEd Tanous 		free(buffer);
24054640298SEd Tanous 		free_file_info(info);
24154640298SEd Tanous 		assert(0);
24254640298SEd Tanous 		return;
24354640298SEd Tanous 	}
24454640298SEd Tanous 	printf("cper_bin: %s\n", cper_bin);
24554640298SEd Tanous 	printf("cper_bin_len: %d\n", cper_bin_len);
24654640298SEd Tanous 
24754640298SEd Tanous 	//Convert to IR, free resources.
24854640298SEd Tanous 	json_object *ir = cper_buf_to_ir(cper_bin, cper_bin_len);
24954640298SEd Tanous 	if (ir == NULL) {
25054640298SEd Tanous 		printf("Empty JSON from CPER bin3\n");
25154640298SEd Tanous 		free(cper_bin);
25254640298SEd Tanous 		free(buffer);
25354640298SEd Tanous 		free_file_info(info);
25454640298SEd Tanous 		assert(0);
25554640298SEd Tanous 		return;
25654640298SEd Tanous 	}
25754640298SEd Tanous 
25854640298SEd Tanous 	json_object *expected = json_object_from_file(info->json_out);
25954640298SEd Tanous 	assert(expected != NULL);
26054640298SEd Tanous 	if (expected == NULL) {
26154640298SEd Tanous 		free(buffer);
26254640298SEd Tanous 		free(cper_bin);
26354640298SEd Tanous 		free_file_info(info);
26454640298SEd Tanous 		const char *str = json_object_to_json_string(ir);
26554640298SEd Tanous 
26654640298SEd Tanous 		const char *expected_str = json_object_to_json_string(expected);
26754640298SEd Tanous 		assert(strcmp(str, expected_str) == 0);
26854640298SEd Tanous 		return;
26954640298SEd Tanous 	}
27054640298SEd Tanous 
27154640298SEd Tanous 	assert(json_object_equal(ir, expected));
27254640298SEd Tanous 	free(buffer);
27354640298SEd Tanous 	free(cper_bin);
27454640298SEd Tanous 	json_object_put(ir);
27554640298SEd Tanous 	json_object_put(expected);
27654640298SEd Tanous 	free_file_info(info);
27754640298SEd Tanous }
27854640298SEd Tanous 
27954640298SEd Tanous //Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
cper_log_section_ir_test(const char * section_name,int single_section,GEN_VALID_BITS_TEST_TYPE validBitsType)28054640298SEd Tanous void cper_log_section_ir_test(const char *section_name, int single_section,
28154640298SEd Tanous 			      GEN_VALID_BITS_TEST_TYPE validBitsType)
28254640298SEd Tanous {
28354640298SEd Tanous 	//Generate full CPER record for the given type.
28454640298SEd Tanous 	char *buf;
28554640298SEd Tanous 	size_t size;
28654640298SEd Tanous 	FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
28754640298SEd Tanous 						 single_section, validBitsType);
28854640298SEd Tanous 
28954640298SEd Tanous 	//Convert to IR, free resources.
29054640298SEd Tanous 	json_object *ir;
29154640298SEd Tanous 	if (single_section) {
29254640298SEd Tanous 		ir = cper_single_section_to_ir(record);
29354640298SEd Tanous 	} else {
29454640298SEd Tanous 		ir = cper_to_ir(record);
29554640298SEd Tanous 	}
29654640298SEd Tanous 
29754640298SEd Tanous 	fclose(record);
29854640298SEd Tanous 	free(buf);
29954640298SEd Tanous 
30054640298SEd Tanous 	//Validate against schema.
30154640298SEd Tanous 	int valid = schema_validate_from_file(ir, single_section,
30254640298SEd Tanous 					      /*all_valid_bits*/ 1);
30354640298SEd Tanous 	json_object_put(ir);
30454640298SEd Tanous 
30554640298SEd Tanous 	if (valid < 0) {
30654640298SEd Tanous 		printf("IR validation test failed (single section mode = %d)\n",
30754640298SEd Tanous 		       single_section);
30854640298SEd Tanous 		assert(0);
30954640298SEd Tanous 	}
31054640298SEd Tanous }
31154640298SEd Tanous 
to_hex(const unsigned char * input,size_t size,char ** out)31254640298SEd Tanous int to_hex(const unsigned char *input, size_t size, char **out)
31354640298SEd Tanous {
31454640298SEd Tanous 	*out = (char *)malloc(size * 2);
31554640298SEd Tanous 	if (out == NULL) {
31654640298SEd Tanous 		return -1;
31754640298SEd Tanous 	}
31854640298SEd Tanous 	int out_index = 0;
31954640298SEd Tanous 	for (size_t i = 0; i < size; i++) {
32054640298SEd Tanous 		unsigned char c = input[i];
32154640298SEd Tanous 		char hex_str[3];
32254640298SEd Tanous 		int n = snprintf(hex_str, sizeof(hex_str), "%02x", c);
32354640298SEd Tanous 		if (n != 2) {
32454640298SEd Tanous 			printf("snprintf2 failed with code %d\n", n);
32554640298SEd Tanous 			return -1;
32654640298SEd Tanous 		}
32754640298SEd Tanous 		(*out)[out_index] = hex_str[0];
32854640298SEd Tanous 		out_index++;
32954640298SEd Tanous 		(*out)[out_index] = hex_str[1];
33054640298SEd Tanous 		out_index++;
33154640298SEd Tanous 	}
33254640298SEd Tanous 	return out_index;
33354640298SEd Tanous }
33454640298SEd Tanous 
33554640298SEd Tanous //Checks for binary round-trip equality for a given randomly generated CPER record.
cper_log_section_binary_test(const char * section_name,int single_section,GEN_VALID_BITS_TEST_TYPE validBitsType)33654640298SEd Tanous void cper_log_section_binary_test(const char *section_name, int single_section,
33754640298SEd Tanous 				  GEN_VALID_BITS_TEST_TYPE validBitsType)
33854640298SEd Tanous {
33954640298SEd Tanous 	//Generate CPER record for the given type.
34054640298SEd Tanous 	char *buf;
34154640298SEd Tanous 	size_t size;
34254640298SEd Tanous 	FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
34354640298SEd Tanous 						 single_section, validBitsType);
34454640298SEd Tanous 	if (record == NULL) {
34554640298SEd Tanous 		printf("Could not generate memstream for binary test");
34654640298SEd Tanous 		return;
34754640298SEd Tanous 	}
34854640298SEd Tanous 
34954640298SEd Tanous 	//Convert to IR.
35054640298SEd Tanous 	json_object *ir;
35154640298SEd Tanous 	if (single_section) {
35254640298SEd Tanous 		ir = cper_single_section_to_ir(record);
35354640298SEd Tanous 	} else {
35454640298SEd Tanous 		ir = cper_to_ir(record);
35554640298SEd Tanous 	}
35654640298SEd Tanous 
35754640298SEd Tanous 	//Now convert back to binary, and get a stream out.
35854640298SEd Tanous 	char *cper_buf;
35954640298SEd Tanous 	size_t cper_buf_size;
36054640298SEd Tanous 	FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
36154640298SEd Tanous 	if (single_section) {
36254640298SEd Tanous 		ir_single_section_to_cper(ir, stream);
36354640298SEd Tanous 	} else {
36454640298SEd Tanous 		ir_to_cper(ir, stream);
36554640298SEd Tanous 	}
36654640298SEd Tanous 	fclose(stream);
36754640298SEd Tanous 
36854640298SEd Tanous 	printf("size: %zu, cper_buf_size: %zu\n", size, cper_buf_size);
36954640298SEd Tanous 
37054640298SEd Tanous 	char *buf_hex;
37154640298SEd Tanous 	int buf_hex_len = to_hex((unsigned char *)buf, size, &buf_hex);
37254640298SEd Tanous 	char *cper_buf_hex;
37354640298SEd Tanous 	int cper_buf_hex_len =
37454640298SEd Tanous 		to_hex((unsigned char *)cper_buf, cper_buf_size, &cper_buf_hex);
37554640298SEd Tanous 
3769f260e5eSEd Tanous 	printf("%.*s\n", cper_buf_hex_len, cper_buf_hex);
3779f260e5eSEd Tanous 	printf("%.*s\n", buf_hex_len, buf_hex);
37854640298SEd Tanous 	assert(buf_hex_len == cper_buf_hex_len);
37954640298SEd Tanous 	assert(memcmp(buf_hex, cper_buf_hex, buf_hex_len) == 0);
38054640298SEd Tanous 
38154640298SEd Tanous 	free(buf_hex);
38254640298SEd Tanous 	free(cper_buf_hex);
38354640298SEd Tanous 
38454640298SEd Tanous 	//Free everything up.
38554640298SEd Tanous 	fclose(record);
38654640298SEd Tanous 	free(buf);
38754640298SEd Tanous 	free(cper_buf);
38854640298SEd Tanous 	json_object_put(ir);
38954640298SEd Tanous }
39054640298SEd Tanous 
39154640298SEd Tanous //Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
cper_log_section_dual_ir_test(const char * section_name)39254640298SEd Tanous void cper_log_section_dual_ir_test(const char *section_name)
39354640298SEd Tanous {
39454640298SEd Tanous 	cper_log_section_ir_test(section_name, 0, allValidbitsSet);
39554640298SEd Tanous 	cper_log_section_ir_test(section_name, 1, allValidbitsSet);
39654640298SEd Tanous 	//Validate against examples
39754640298SEd Tanous 	cper_example_section_ir_test(section_name);
39854640298SEd Tanous }
39954640298SEd Tanous 
40054640298SEd Tanous //Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
cper_log_section_dual_binary_test(const char * section_name)40154640298SEd Tanous void cper_log_section_dual_binary_test(const char *section_name)
40254640298SEd Tanous {
40354640298SEd Tanous 	cper_log_section_binary_test(section_name, 0, allValidbitsSet);
40454640298SEd Tanous 	cper_log_section_binary_test(section_name, 1, allValidbitsSet);
40554640298SEd Tanous }
40654640298SEd Tanous 
40754640298SEd Tanous /*
40854640298SEd Tanous * Non-single section assertions.
40954640298SEd Tanous */
CompileTimeAssertions_TwoWayConversion()41054640298SEd Tanous void CompileTimeAssertions_TwoWayConversion()
41154640298SEd Tanous {
41254640298SEd Tanous 	for (size_t i = 0; i < section_definitions_len; i++) {
41354640298SEd Tanous 		//If a conversion one way exists, a conversion the other way must exist.
41454640298SEd Tanous 		if (section_definitions[i].ToCPER != NULL) {
41554640298SEd Tanous 			assert(section_definitions[i].ToIR != NULL);
41654640298SEd Tanous 		}
41754640298SEd Tanous 		if (section_definitions[i].ToIR != NULL) {
41854640298SEd Tanous 			assert(section_definitions[i].ToCPER != NULL);
41954640298SEd Tanous 		}
42054640298SEd Tanous 	}
42154640298SEd Tanous }
42254640298SEd Tanous 
CompileTimeAssertions_ShortcodeNoSpaces()42354640298SEd Tanous void CompileTimeAssertions_ShortcodeNoSpaces()
42454640298SEd Tanous {
42554640298SEd Tanous 	for (size_t i = 0; i < generator_definitions_len; i++) {
42654640298SEd Tanous 		for (int j = 0;
42754640298SEd Tanous 		     generator_definitions[i].ShortName[j + 1] != '\0'; j++) {
42854640298SEd Tanous 			assert(isspace(generator_definitions[i].ShortName[j]) ==
42954640298SEd Tanous 			       0);
43054640298SEd Tanous 		}
43154640298SEd Tanous 	}
43254640298SEd Tanous }
43354640298SEd Tanous 
43454640298SEd Tanous /*
43554640298SEd Tanous * Single section tests.
43654640298SEd Tanous */
43754640298SEd Tanous 
43854640298SEd Tanous //Generic processor tests.
GenericProcessorTests_IRValid()43954640298SEd Tanous void GenericProcessorTests_IRValid()
44054640298SEd Tanous {
44154640298SEd Tanous 	cper_log_section_dual_ir_test("generic");
44254640298SEd Tanous }
GenericProcessorTests_BinaryEqual()44354640298SEd Tanous void GenericProcessorTests_BinaryEqual()
44454640298SEd Tanous {
44554640298SEd Tanous 	cper_log_section_dual_binary_test("generic");
44654640298SEd Tanous }
44754640298SEd Tanous 
44854640298SEd Tanous //IA32/x64 tests.
IA32x64Tests_IRValid()44954640298SEd Tanous void IA32x64Tests_IRValid()
45054640298SEd Tanous {
45154640298SEd Tanous 	cper_log_section_dual_ir_test("ia32x64");
45254640298SEd Tanous }
IA32x64Tests_BinaryEqual()45354640298SEd Tanous void IA32x64Tests_BinaryEqual()
45454640298SEd Tanous {
45554640298SEd Tanous 	cper_log_section_dual_binary_test("ia32x64");
45654640298SEd Tanous }
45754640298SEd Tanous 
45854640298SEd Tanous // void IPFTests_IRValid() {
45954640298SEd Tanous //     cper_log_section_dual_ir_test("ipf");
46054640298SEd Tanous // }
46154640298SEd Tanous 
46254640298SEd Tanous //ARM tests.
ArmTests_IRValid()46354640298SEd Tanous void ArmTests_IRValid()
46454640298SEd Tanous {
46554640298SEd Tanous 	cper_log_section_dual_ir_test("arm");
46654640298SEd Tanous }
ArmTests_BinaryEqual()46754640298SEd Tanous void ArmTests_BinaryEqual()
46854640298SEd Tanous {
46954640298SEd Tanous 	cper_log_section_dual_binary_test("arm");
47054640298SEd Tanous }
47154640298SEd Tanous 
47254640298SEd Tanous //Memory tests.
MemoryTests_IRValid()47354640298SEd Tanous void MemoryTests_IRValid()
47454640298SEd Tanous {
47554640298SEd Tanous 	cper_log_section_dual_ir_test("memory");
47654640298SEd Tanous }
MemoryTests_BinaryEqual()47754640298SEd Tanous void MemoryTests_BinaryEqual()
47854640298SEd Tanous {
47954640298SEd Tanous 	cper_log_section_dual_binary_test("memory");
48054640298SEd Tanous }
48154640298SEd Tanous 
48254640298SEd Tanous //Memory 2 tests.
Memory2Tests_IRValid()48354640298SEd Tanous void Memory2Tests_IRValid()
48454640298SEd Tanous {
48554640298SEd Tanous 	cper_log_section_dual_ir_test("memory2");
48654640298SEd Tanous }
Memory2Tests_BinaryEqual()48754640298SEd Tanous void Memory2Tests_BinaryEqual()
48854640298SEd Tanous {
48954640298SEd Tanous 	cper_log_section_dual_binary_test("memory2");
49054640298SEd Tanous }
49154640298SEd Tanous 
49254640298SEd Tanous //PCIe tests.
PCIeTests_IRValid()49354640298SEd Tanous void PCIeTests_IRValid()
49454640298SEd Tanous {
49554640298SEd Tanous 	cper_log_section_dual_ir_test("pcie");
49654640298SEd Tanous }
PCIeTests_BinaryEqual()49754640298SEd Tanous void PCIeTests_BinaryEqual()
49854640298SEd Tanous {
49954640298SEd Tanous 	cper_log_section_dual_binary_test("pcie");
50054640298SEd Tanous }
50154640298SEd Tanous 
50254640298SEd Tanous //Firmware tests.
FirmwareTests_IRValid()50354640298SEd Tanous void FirmwareTests_IRValid()
50454640298SEd Tanous {
50554640298SEd Tanous 	cper_log_section_dual_ir_test("firmware");
50654640298SEd Tanous }
FirmwareTests_BinaryEqual()50754640298SEd Tanous void FirmwareTests_BinaryEqual()
50854640298SEd Tanous {
50954640298SEd Tanous 	cper_log_section_dual_binary_test("firmware");
51054640298SEd Tanous }
51154640298SEd Tanous 
51254640298SEd Tanous //PCI Bus tests.
PCIBusTests_IRValid()51354640298SEd Tanous void PCIBusTests_IRValid()
51454640298SEd Tanous {
51554640298SEd Tanous 	cper_log_section_dual_ir_test("pcibus");
51654640298SEd Tanous }
PCIBusTests_BinaryEqual()51754640298SEd Tanous void PCIBusTests_BinaryEqual()
51854640298SEd Tanous {
51954640298SEd Tanous 	cper_log_section_dual_binary_test("pcibus");
52054640298SEd Tanous }
52154640298SEd Tanous 
52254640298SEd Tanous //PCI Device tests.
PCIDevTests_IRValid()52354640298SEd Tanous void PCIDevTests_IRValid()
52454640298SEd Tanous {
52554640298SEd Tanous 	cper_log_section_dual_ir_test("pcidev");
52654640298SEd Tanous }
PCIDevTests_BinaryEqual()52754640298SEd Tanous void PCIDevTests_BinaryEqual()
52854640298SEd Tanous {
52954640298SEd Tanous 	cper_log_section_dual_binary_test("pcidev");
53054640298SEd Tanous }
53154640298SEd Tanous 
53254640298SEd Tanous //Generic DMAr tests.
DMArGenericTests_IRValid()53354640298SEd Tanous void DMArGenericTests_IRValid()
53454640298SEd Tanous {
53554640298SEd Tanous 	cper_log_section_dual_ir_test("dmargeneric");
53654640298SEd Tanous }
DMArGenericTests_BinaryEqual()53754640298SEd Tanous void DMArGenericTests_BinaryEqual()
53854640298SEd Tanous {
53954640298SEd Tanous 	cper_log_section_dual_binary_test("dmargeneric");
54054640298SEd Tanous }
54154640298SEd Tanous 
54254640298SEd Tanous //VT-d DMAr tests.
DMArVtdTests_IRValid()54354640298SEd Tanous void DMArVtdTests_IRValid()
54454640298SEd Tanous {
54554640298SEd Tanous 	cper_log_section_dual_ir_test("dmarvtd");
54654640298SEd Tanous }
DMArVtdTests_BinaryEqual()54754640298SEd Tanous void DMArVtdTests_BinaryEqual()
54854640298SEd Tanous {
54954640298SEd Tanous 	cper_log_section_dual_binary_test("dmarvtd");
55054640298SEd Tanous }
55154640298SEd Tanous 
55254640298SEd Tanous //IOMMU DMAr tests.
DMArIOMMUTests_IRValid()55354640298SEd Tanous void DMArIOMMUTests_IRValid()
55454640298SEd Tanous {
55554640298SEd Tanous 	cper_log_section_dual_ir_test("dmariommu");
55654640298SEd Tanous }
DMArIOMMUTests_BinaryEqual()55754640298SEd Tanous void DMArIOMMUTests_BinaryEqual()
55854640298SEd Tanous {
55954640298SEd Tanous 	cper_log_section_dual_binary_test("dmariommu");
56054640298SEd Tanous }
56154640298SEd Tanous 
56254640298SEd Tanous //CCIX PER tests.
CCIXPERTests_IRValid()56354640298SEd Tanous void CCIXPERTests_IRValid()
56454640298SEd Tanous {
56554640298SEd Tanous 	cper_log_section_dual_ir_test("ccixper");
56654640298SEd Tanous }
CCIXPERTests_BinaryEqual()56754640298SEd Tanous void CCIXPERTests_BinaryEqual()
56854640298SEd Tanous {
56954640298SEd Tanous 	cper_log_section_dual_binary_test("ccixper");
57054640298SEd Tanous }
57154640298SEd Tanous 
57254640298SEd Tanous //CXL Protocol tests.
CXLProtocolTests_IRValid()57354640298SEd Tanous void CXLProtocolTests_IRValid()
57454640298SEd Tanous {
57554640298SEd Tanous 	cper_log_section_dual_ir_test("cxlprotocol");
57654640298SEd Tanous }
CXLProtocolTests_BinaryEqual()57754640298SEd Tanous void CXLProtocolTests_BinaryEqual()
57854640298SEd Tanous {
57954640298SEd Tanous 	cper_log_section_dual_binary_test("cxlprotocol");
58054640298SEd Tanous }
58154640298SEd Tanous 
58254640298SEd Tanous //CXL Component tests.
CXLComponentTests_IRValid()58354640298SEd Tanous void CXLComponentTests_IRValid()
58454640298SEd Tanous {
58554640298SEd Tanous 	cper_log_section_dual_ir_test("cxlcomponent-media");
58654640298SEd Tanous }
CXLComponentTests_BinaryEqual()58754640298SEd Tanous void CXLComponentTests_BinaryEqual()
58854640298SEd Tanous {
58954640298SEd Tanous 	cper_log_section_dual_binary_test("cxlcomponent-media");
59054640298SEd Tanous }
59154640298SEd Tanous 
59254640298SEd Tanous //NVIDIA section tests.
NVIDIASectionTests_IRValid()59354640298SEd Tanous void NVIDIASectionTests_IRValid()
59454640298SEd Tanous {
59554640298SEd Tanous 	cper_log_section_dual_ir_test("nvidia");
59654640298SEd Tanous }
NVIDIASectionTests_BinaryEqual()59754640298SEd Tanous void NVIDIASectionTests_BinaryEqual()
59854640298SEd Tanous {
59954640298SEd Tanous 	cper_log_section_dual_binary_test("nvidia");
60054640298SEd Tanous }
60154640298SEd Tanous 
NVIDIACMETSectionTests_IRValid()602*55968b12SEd Tanous void NVIDIACMETSectionTests_IRValid()
603*55968b12SEd Tanous {
604*55968b12SEd Tanous 	cper_example_section_ir_test("nvidia_cmet_info");
605*55968b12SEd Tanous }
606*55968b12SEd Tanous 
60754640298SEd Tanous //Unknown section tests.
UnknownSectionTests_IRValid()60854640298SEd Tanous void UnknownSectionTests_IRValid()
60954640298SEd Tanous {
61054640298SEd Tanous 	cper_log_section_dual_ir_test("unknown");
61154640298SEd Tanous }
UnknownSectionTests_BinaryEqual()61254640298SEd Tanous void UnknownSectionTests_BinaryEqual()
61354640298SEd Tanous {
61454640298SEd Tanous 	cper_log_section_dual_binary_test("unknown");
61554640298SEd Tanous }
61654640298SEd Tanous 
61754640298SEd Tanous //Entrypoint for the testing program.
main()61854640298SEd Tanous int main()
61954640298SEd Tanous {
62054640298SEd Tanous 	if (GEN_EXAMPLES) {
62154640298SEd Tanous 		cper_create_examples("arm");
62254640298SEd Tanous 		cper_create_examples("ia32x64");
62354640298SEd Tanous 		cper_create_examples("memory");
62454640298SEd Tanous 		cper_create_examples("memory2");
62554640298SEd Tanous 		cper_create_examples("pcie");
62654640298SEd Tanous 		cper_create_examples("firmware");
62754640298SEd Tanous 		cper_create_examples("pcibus");
62854640298SEd Tanous 		cper_create_examples("pcidev");
62954640298SEd Tanous 		cper_create_examples("dmargeneric");
63054640298SEd Tanous 		cper_create_examples("dmarvtd");
63154640298SEd Tanous 		cper_create_examples("dmariommu");
63254640298SEd Tanous 		cper_create_examples("ccixper");
63354640298SEd Tanous 		cper_create_examples("cxlprotocol");
63454640298SEd Tanous 		cper_create_examples("cxlcomponent-media");
63554640298SEd Tanous 		cper_create_examples("nvidia");
63654640298SEd Tanous 		cper_create_examples("unknown");
63754640298SEd Tanous 	}
63854640298SEd Tanous 	test_base64_encode_good();
63954640298SEd Tanous 	test_base64_decode_good();
64054640298SEd Tanous 	GenericProcessorTests_IRValid();
64154640298SEd Tanous 	GenericProcessorTests_BinaryEqual();
64254640298SEd Tanous 	IA32x64Tests_IRValid();
64354640298SEd Tanous 	IA32x64Tests_BinaryEqual();
64454640298SEd Tanous 	ArmTests_IRValid();
64554640298SEd Tanous 	ArmTests_BinaryEqual();
64654640298SEd Tanous 	MemoryTests_IRValid();
64754640298SEd Tanous 	MemoryTests_BinaryEqual();
64854640298SEd Tanous 	Memory2Tests_IRValid();
64954640298SEd Tanous 	Memory2Tests_BinaryEqual();
65054640298SEd Tanous 	PCIeTests_IRValid();
65154640298SEd Tanous 	PCIeTests_BinaryEqual();
65254640298SEd Tanous 	FirmwareTests_IRValid();
65354640298SEd Tanous 	FirmwareTests_BinaryEqual();
65454640298SEd Tanous 	PCIBusTests_IRValid();
65554640298SEd Tanous 	PCIBusTests_BinaryEqual();
65654640298SEd Tanous 	PCIDevTests_IRValid();
65754640298SEd Tanous 	PCIDevTests_BinaryEqual();
65854640298SEd Tanous 	DMArGenericTests_IRValid();
65954640298SEd Tanous 	DMArGenericTests_BinaryEqual();
66054640298SEd Tanous 	DMArVtdTests_IRValid();
66154640298SEd Tanous 	DMArVtdTests_BinaryEqual();
66254640298SEd Tanous 	DMArIOMMUTests_IRValid();
66354640298SEd Tanous 	DMArIOMMUTests_BinaryEqual();
66454640298SEd Tanous 	CCIXPERTests_IRValid();
66554640298SEd Tanous 	CCIXPERTests_BinaryEqual();
66654640298SEd Tanous 	CXLProtocolTests_IRValid();
66754640298SEd Tanous 	CXLProtocolTests_BinaryEqual();
66854640298SEd Tanous 	CXLComponentTests_IRValid();
66954640298SEd Tanous 	CXLComponentTests_BinaryEqual();
67054640298SEd Tanous 	NVIDIASectionTests_IRValid();
67154640298SEd Tanous 	NVIDIASectionTests_BinaryEqual();
672*55968b12SEd Tanous 	NVIDIACMETSectionTests_IRValid();
67354640298SEd Tanous 	UnknownSectionTests_IRValid();
67454640298SEd Tanous 	UnknownSectionTests_BinaryEqual();
67554640298SEd Tanous 	CompileTimeAssertions_TwoWayConversion();
67654640298SEd Tanous 	CompileTimeAssertions_ShortcodeNoSpaces();
67754640298SEd Tanous 
67854640298SEd Tanous 	printf("\n\nTest completed successfully.\n");
67954640298SEd Tanous 
68054640298SEd Tanous 	return 0;
68154640298SEd Tanous }
682