blob: a961e405456f0f8e121b742f4fd0de7ffe835b00 [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.
3 *
4 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include "gtest/gtest.h"
8#include "test-utils.hpp"
9extern "C" {
Lawrence Tang5202bbb2022-08-12 14:54:36 +010010#include <json.h>
Lawrence Tangd34f2b12022-07-19 15:36:31 +010011#include "../cper-parse.h"
12#include "../json-schema.h"
13#include "../generator/cper-generate.h"
14}
15
16/*
17* Test templates.
18*/
Lawrence Tangcd505202022-07-19 16:55:11 +010019
20//Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
Lawrence Tanga4f662f2022-08-08 14:37:36 +010021void cper_log_section_ir_test(const char *section_name, int single_section)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010022{
Lawrence Tanga4f662f2022-08-08 14:37:36 +010023 //Generate full CPER record for the given type.
Lawrence Tange407b4c2022-07-21 13:54:01 +010024 char *buf;
25 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +010026 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
27 single_section);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010028
Lawrence Tange407b4c2022-07-21 13:54:01 +010029 //Convert to IR, free resources.
Lawrence Tanga4f662f2022-08-08 14:37:36 +010030 json_object *ir;
31 if (single_section)
32 ir = cper_single_section_to_ir(record);
33 else
34 ir = cper_to_ir(record);
Lawrence Tange407b4c2022-07-21 13:54:01 +010035 fclose(record);
36 free(buf);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010037
Lawrence Tange407b4c2022-07-21 13:54:01 +010038 //Validate against schema.
39 char error_message[JSON_ERROR_MSG_MAX_LEN] = { 0 };
40 int valid = validate_schema_from_file("./specification/cper-json.json",
41 ir, error_message);
Lawrence Tanga4f662f2022-08-08 14:37:36 +010042 ASSERT_TRUE(valid)
43 << "IR validation test failed (single section mode = "
44 << single_section << ") with message: " << error_message;
Lawrence Tangd34f2b12022-07-19 15:36:31 +010045}
46
Lawrence Tangcd505202022-07-19 16:55:11 +010047//Checks for binary round-trip equality for a given randomly generated CPER record.
Lawrence Tanga4f662f2022-08-08 14:37:36 +010048void cper_log_section_binary_test(const char *section_name, int single_section)
Lawrence Tangcd505202022-07-19 16:55:11 +010049{
Lawrence Tange407b4c2022-07-21 13:54:01 +010050 //Generate CPER record for the given type.
51 char *buf;
52 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +010053 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
54 single_section);
Lawrence Tangcd505202022-07-19 16:55:11 +010055
Lawrence Tanga4f662f2022-08-08 14:37:36 +010056 //Convert to IR.
57 json_object *ir;
58 if (single_section)
59 ir = cper_single_section_to_ir(record);
60 else
61 ir = cper_to_ir(record);
62
63 //Now convert back to binary, and get a stream out.
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 char *cper_buf;
65 size_t cper_buf_size;
66 FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
Lawrence Tanga4f662f2022-08-08 14:37:36 +010067 if (single_section)
68 ir_single_section_to_cper(ir, stream);
69 else
70 ir_to_cper(ir, stream);
Lawrence Tange407b4c2022-07-21 13:54:01 +010071 size_t cper_len = ftell(stream);
72 fclose(stream);
Lawrence Tangcd505202022-07-19 16:55:11 +010073
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 //Validate the two are identical.
75 ASSERT_GE(size, cper_len);
76 ASSERT_EQ(memcmp(buf, cper_buf, cper_len), 0)
Lawrence Tanga4f662f2022-08-08 14:37:36 +010077 << "Binary output was not identical to input (single section mode = "
78 << single_section << ").";
Lawrence Tange407b4c2022-07-21 13:54:01 +010079
80 //Free everything up.
81 fclose(record);
82 free(buf);
83 free(cper_buf);
Lawrence Tangcd505202022-07-19 16:55:11 +010084}
85
Lawrence Tanga4f662f2022-08-08 14:37:36 +010086//Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
87void cper_log_section_dual_ir_test(const char *section_name)
88{
89 cper_log_section_ir_test(section_name, 0);
90 cper_log_section_ir_test(section_name, 1);
91}
92
93//Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
94void cper_log_section_dual_binary_test(const char *section_name)
95{
96 cper_log_section_binary_test(section_name, 0);
97 cper_log_section_binary_test(section_name, 1);
98}
99
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100100/*
101* Single section tests.
102*/
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100103
Lawrence Tangcd505202022-07-19 16:55:11 +0100104//Generic processor tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100105TEST(GenericProcessorTests, IRValid)
106{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100107 cper_log_section_dual_ir_test("generic");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100108}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100109TEST(GenericProcessorTests, BinaryEqual)
110{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100111 cper_log_section_dual_binary_test("generic");
Lawrence Tangcd505202022-07-19 16:55:11 +0100112}
113
114//IA32/x64 tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115TEST(IA32x64Tests, IRValid)
116{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100117 cper_log_section_dual_ir_test("ia32x64");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100118}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100119TEST(IA32x64Tests, BinaryEqual)
120{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100121 cper_log_section_dual_binary_test("ia32x64");
Lawrence Tangcd505202022-07-19 16:55:11 +0100122}
123
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100124// TEST(IPFTests, IRValid) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100125// cper_log_section_dual_ir_test("ipf");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100126// }
Lawrence Tangcd505202022-07-19 16:55:11 +0100127
128//ARM tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100129TEST(ArmTests, IRValid)
130{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100131 cper_log_section_dual_ir_test("arm");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100132}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100133TEST(ArmTests, BinaryEqual)
134{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100135 cper_log_section_dual_binary_test("arm");
Lawrence Tangcd505202022-07-19 16:55:11 +0100136}
137
138//Memory tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100139TEST(MemoryTests, IRValid)
140{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100141 cper_log_section_dual_ir_test("memory");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100142}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143TEST(MemoryTests, BinaryEqual)
144{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100145 cper_log_section_dual_binary_test("memory");
Lawrence Tangcd505202022-07-19 16:55:11 +0100146}
147
148//Memory 2 tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100149TEST(Memory2Tests, IRValid)
150{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100151 cper_log_section_dual_ir_test("memory2");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100152}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100153TEST(Memory2Tests, BinaryEqual)
154{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100155 cper_log_section_dual_binary_test("memory2");
Lawrence Tangcd505202022-07-19 16:55:11 +0100156}
157
158//PCIe tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100159TEST(PCIeTests, IRValid)
160{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100161 cper_log_section_dual_ir_test("pcie");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100162}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100163TEST(PCIeTests, BinaryEqual)
164{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100165 cper_log_section_dual_binary_test("pcie");
Lawrence Tangcd505202022-07-19 16:55:11 +0100166}
167
168//Firmware tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100169TEST(FirmwareTests, IRValid)
170{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100171 cper_log_section_dual_ir_test("firmware");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100172}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100173TEST(FirmwareTests, BinaryEqual)
174{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100175 cper_log_section_dual_binary_test("firmware");
Lawrence Tangcd505202022-07-19 16:55:11 +0100176}
177
178//PCI Bus tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100179TEST(PCIBusTests, IRValid)
180{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100181 cper_log_section_dual_ir_test("pcibus");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100182}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100183TEST(PCIBusTests, BinaryEqual)
184{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100185 cper_log_section_dual_binary_test("pcibus");
Lawrence Tangcd505202022-07-19 16:55:11 +0100186}
187
188//PCI Device tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100189TEST(PCIDevTests, IRValid)
190{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100191 cper_log_section_dual_ir_test("pcidev");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100192}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100193TEST(PCIDevTests, BinaryEqual)
194{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100195 cper_log_section_dual_binary_test("pcidev");
Lawrence Tangcd505202022-07-19 16:55:11 +0100196}
197
198//Generic DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100199TEST(DMArGenericTests, IRValid)
200{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100201 cper_log_section_dual_ir_test("dmargeneric");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100202}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100203TEST(DMArGenericTests, BinaryEqual)
204{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100205 cper_log_section_dual_binary_test("dmargeneric");
Lawrence Tangcd505202022-07-19 16:55:11 +0100206}
207
208//VT-d DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100209TEST(DMArVtdTests, IRValid)
210{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100211 cper_log_section_dual_ir_test("dmarvtd");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100212}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100213TEST(DMArVtdTests, BinaryEqual)
214{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100215 cper_log_section_dual_binary_test("dmarvtd");
Lawrence Tangcd505202022-07-19 16:55:11 +0100216}
217
218//IOMMU DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100219TEST(DMArIOMMUTests, IRValid)
220{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100221 cper_log_section_dual_ir_test("dmariommu");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100222}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223TEST(DMArIOMMUTests, BinaryEqual)
224{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100225 cper_log_section_dual_binary_test("dmariommu");
Lawrence Tangcd505202022-07-19 16:55:11 +0100226}
227
228//CCIX PER tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100229TEST(CCIXPERTests, IRValid)
230{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100231 cper_log_section_dual_ir_test("ccixper");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100232}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100233TEST(CCIXPERTests, BinaryEqual)
234{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100235 cper_log_section_dual_binary_test("ccixper");
Lawrence Tangcd505202022-07-19 16:55:11 +0100236}
237
238//CXL Protocol tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100239TEST(CXLProtocolTests, IRValid)
240{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100241 cper_log_section_dual_ir_test("cxlprotocol");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100242}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100243TEST(CXLProtocolTests, BinaryEqual)
244{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100245 cper_log_section_dual_binary_test("cxlprotocol");
Lawrence Tangcd505202022-07-19 16:55:11 +0100246}
247
248//CXL Component tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100249TEST(CXLComponentTests, IRValid)
250{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100251 cper_log_section_dual_ir_test("cxlcomponent");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100252}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100253TEST(CXLComponentTests, BinaryEqual)
254{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100255 cper_log_section_dual_binary_test("cxlcomponent");
Lawrence Tangcd505202022-07-19 16:55:11 +0100256}
257
258//Unknown section tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100259TEST(UnknownSectionTests, IRValid)
260{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100261 cper_log_section_dual_ir_test("unknown");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100262}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100263TEST(UnknownSectionTests, BinaryEqual)
264{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100265 cper_log_section_dual_binary_test("unknown");
Lawrence Tangcd505202022-07-19 16:55:11 +0100266}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100267
268//Entrypoint for the testing program.
269int main()
270{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100271 testing::InitGoogleTest();
272 return RUN_ALL_TESTS();
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100273}