/** * Defines tests for validating CPER-JSON IR output from the cper-parse library. * * Author: Lawrence.Tang@arm.com **/ #include "gtest/gtest.h" #include "test-utils.hpp" extern "C" { #include #include "../cper-parse.h" #include "../json-schema.h" #include "../generator/cper-generate.h" #include "../sections/cper-section.h" } /* * Test templates. */ //Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity. void cper_log_section_ir_test(const char *section_name, int single_section) { //Generate full CPER record for the given type. char *buf; size_t size; FILE *record = generate_record_memstream(§ion_name, 1, &buf, &size, single_section); //Convert to IR, free resources. json_object *ir; if (single_section) ir = cper_single_section_to_ir(record); else ir = cper_to_ir(record); fclose(record); free(buf); //Validate against schema. char error_message[JSON_ERROR_MSG_MAX_LEN] = { 0 }; int valid = validate_schema_from_file("./specification/cper-json.json", ir, error_message); ASSERT_TRUE(valid) << "IR validation test failed (single section mode = " << single_section << ") with message: " << error_message; } //Checks for binary round-trip equality for a given randomly generated CPER record. void cper_log_section_binary_test(const char *section_name, int single_section) { //Generate CPER record for the given type. char *buf; size_t size; FILE *record = generate_record_memstream(§ion_name, 1, &buf, &size, single_section); //Convert to IR. json_object *ir; if (single_section) ir = cper_single_section_to_ir(record); else ir = cper_to_ir(record); //Now convert back to binary, and get a stream out. char *cper_buf; size_t cper_buf_size; FILE *stream = open_memstream(&cper_buf, &cper_buf_size); if (single_section) ir_single_section_to_cper(ir, stream); else ir_to_cper(ir, stream); size_t cper_len = ftell(stream); fclose(stream); //Validate the two are identical. ASSERT_GE(size, cper_len); ASSERT_EQ(memcmp(buf, cper_buf, cper_len), 0) << "Binary output was not identical to input (single section mode = " << single_section << ")."; //Free everything up. fclose(record); free(buf); free(cper_buf); } //Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode. void cper_log_section_dual_ir_test(const char *section_name) { cper_log_section_ir_test(section_name, 0); cper_log_section_ir_test(section_name, 1); } //Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode. void cper_log_section_dual_binary_test(const char *section_name) { cper_log_section_binary_test(section_name, 0); cper_log_section_binary_test(section_name, 1); } /* * Non-single section assertions. */ TEST(CompileTimeAssertions, TwoWayConversion) { for (int i=0; i