blob: 1017e75684dedfd377f516d066c94cd0218f8711 [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"
Lawrence Tang580423f2022-08-24 09:37:53 +010014#include "../sections/cper-section.h"
Lawrence Tangd34f2b12022-07-19 15:36:31 +010015}
16
17/*
18* Test templates.
19*/
Lawrence Tangcd505202022-07-19 16:55:11 +010020
21//Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
Lawrence Tanga4f662f2022-08-08 14:37:36 +010022void cper_log_section_ir_test(const char *section_name, int single_section)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010023{
Lawrence Tanga4f662f2022-08-08 14:37:36 +010024 //Generate full CPER record for the given type.
Lawrence Tange407b4c2022-07-21 13:54:01 +010025 char *buf;
26 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +010027 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
28 single_section);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010029
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 //Convert to IR, free resources.
Lawrence Tanga4f662f2022-08-08 14:37:36 +010031 json_object *ir;
32 if (single_section)
33 ir = cper_single_section_to_ir(record);
34 else
35 ir = cper_to_ir(record);
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 fclose(record);
37 free(buf);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010038
Lawrence Tange407b4c2022-07-21 13:54:01 +010039 //Validate against schema.
40 char error_message[JSON_ERROR_MSG_MAX_LEN] = { 0 };
41 int valid = validate_schema_from_file("./specification/cper-json.json",
42 ir, error_message);
Lawrence Tanga4f662f2022-08-08 14:37:36 +010043 ASSERT_TRUE(valid)
44 << "IR validation test failed (single section mode = "
45 << single_section << ") with message: " << error_message;
Lawrence Tangd34f2b12022-07-19 15:36:31 +010046}
47
Lawrence Tangcd505202022-07-19 16:55:11 +010048//Checks for binary round-trip equality for a given randomly generated CPER record.
Lawrence Tanga4f662f2022-08-08 14:37:36 +010049void cper_log_section_binary_test(const char *section_name, int single_section)
Lawrence Tangcd505202022-07-19 16:55:11 +010050{
Lawrence Tange407b4c2022-07-21 13:54:01 +010051 //Generate CPER record for the given type.
52 char *buf;
53 size_t size;
Lawrence Tanga4f662f2022-08-08 14:37:36 +010054 FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
55 single_section);
Lawrence Tangcd505202022-07-19 16:55:11 +010056
Lawrence Tanga4f662f2022-08-08 14:37:36 +010057 //Convert to IR.
58 json_object *ir;
59 if (single_section)
60 ir = cper_single_section_to_ir(record);
61 else
62 ir = cper_to_ir(record);
63
64 //Now convert back to binary, and get a stream out.
Lawrence Tange407b4c2022-07-21 13:54:01 +010065 char *cper_buf;
66 size_t cper_buf_size;
67 FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
Lawrence Tanga4f662f2022-08-08 14:37:36 +010068 if (single_section)
69 ir_single_section_to_cper(ir, stream);
70 else
71 ir_to_cper(ir, stream);
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 size_t cper_len = ftell(stream);
73 fclose(stream);
Lawrence Tangcd505202022-07-19 16:55:11 +010074
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 //Validate the two are identical.
76 ASSERT_GE(size, cper_len);
77 ASSERT_EQ(memcmp(buf, cper_buf, cper_len), 0)
Lawrence Tanga4f662f2022-08-08 14:37:36 +010078 << "Binary output was not identical to input (single section mode = "
79 << single_section << ").";
Lawrence Tange407b4c2022-07-21 13:54:01 +010080
81 //Free everything up.
82 fclose(record);
83 free(buf);
84 free(cper_buf);
Lawrence Tangcd505202022-07-19 16:55:11 +010085}
86
Lawrence Tanga4f662f2022-08-08 14:37:36 +010087//Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
88void cper_log_section_dual_ir_test(const char *section_name)
89{
90 cper_log_section_ir_test(section_name, 0);
91 cper_log_section_ir_test(section_name, 1);
92}
93
94//Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
95void cper_log_section_dual_binary_test(const char *section_name)
96{
97 cper_log_section_binary_test(section_name, 0);
98 cper_log_section_binary_test(section_name, 1);
99}
100
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100101/*
Lawrence Tang580423f2022-08-24 09:37:53 +0100102* Non-single section assertions.
103*/
104TEST(CompileTimeAssertions, TwoWayConversion)
105{
106 for (int i=0; i<section_definitions_len; i++)
107 {
108 //If a conversion one way exists, a conversion the other way must exist.
109 std::string err = "If a CPER conversion exists one way, there must be an equivalent method in reverse.";
110 if (section_definitions[i].ToCPER != NULL)
111 ASSERT_NE(section_definitions[i].ToIR, NULL) << err;
112 if (section_definitions[i].ToIR != NULL)
113 ASSERT_NE(section_definitions[i].ToCPER, NULL) << err;
114 }
115}
116
117/*
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100118* Single section tests.
119*/
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100120
Lawrence Tangcd505202022-07-19 16:55:11 +0100121//Generic processor tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122TEST(GenericProcessorTests, IRValid)
123{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100124 cper_log_section_dual_ir_test("generic");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100125}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126TEST(GenericProcessorTests, BinaryEqual)
127{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100128 cper_log_section_dual_binary_test("generic");
Lawrence Tangcd505202022-07-19 16:55:11 +0100129}
130
131//IA32/x64 tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132TEST(IA32x64Tests, IRValid)
133{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100134 cper_log_section_dual_ir_test("ia32x64");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100135}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136TEST(IA32x64Tests, BinaryEqual)
137{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100138 cper_log_section_dual_binary_test("ia32x64");
Lawrence Tangcd505202022-07-19 16:55:11 +0100139}
140
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100141// TEST(IPFTests, IRValid) {
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100142// cper_log_section_dual_ir_test("ipf");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100143// }
Lawrence Tangcd505202022-07-19 16:55:11 +0100144
145//ARM tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100146TEST(ArmTests, IRValid)
147{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100148 cper_log_section_dual_ir_test("arm");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100149}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100150TEST(ArmTests, BinaryEqual)
151{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100152 cper_log_section_dual_binary_test("arm");
Lawrence Tangcd505202022-07-19 16:55:11 +0100153}
154
155//Memory tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156TEST(MemoryTests, IRValid)
157{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100158 cper_log_section_dual_ir_test("memory");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100159}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160TEST(MemoryTests, BinaryEqual)
161{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100162 cper_log_section_dual_binary_test("memory");
Lawrence Tangcd505202022-07-19 16:55:11 +0100163}
164
165//Memory 2 tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100166TEST(Memory2Tests, IRValid)
167{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100168 cper_log_section_dual_ir_test("memory2");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100169}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100170TEST(Memory2Tests, BinaryEqual)
171{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100172 cper_log_section_dual_binary_test("memory2");
Lawrence Tangcd505202022-07-19 16:55:11 +0100173}
174
175//PCIe tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100176TEST(PCIeTests, IRValid)
177{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100178 cper_log_section_dual_ir_test("pcie");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100179}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100180TEST(PCIeTests, BinaryEqual)
181{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100182 cper_log_section_dual_binary_test("pcie");
Lawrence Tangcd505202022-07-19 16:55:11 +0100183}
184
185//Firmware tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100186TEST(FirmwareTests, IRValid)
187{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100188 cper_log_section_dual_ir_test("firmware");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100189}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100190TEST(FirmwareTests, BinaryEqual)
191{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100192 cper_log_section_dual_binary_test("firmware");
Lawrence Tangcd505202022-07-19 16:55:11 +0100193}
194
195//PCI Bus tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196TEST(PCIBusTests, IRValid)
197{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100198 cper_log_section_dual_ir_test("pcibus");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100199}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100200TEST(PCIBusTests, BinaryEqual)
201{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100202 cper_log_section_dual_binary_test("pcibus");
Lawrence Tangcd505202022-07-19 16:55:11 +0100203}
204
205//PCI Device tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100206TEST(PCIDevTests, IRValid)
207{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100208 cper_log_section_dual_ir_test("pcidev");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100209}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100210TEST(PCIDevTests, BinaryEqual)
211{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100212 cper_log_section_dual_binary_test("pcidev");
Lawrence Tangcd505202022-07-19 16:55:11 +0100213}
214
215//Generic DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100216TEST(DMArGenericTests, IRValid)
217{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100218 cper_log_section_dual_ir_test("dmargeneric");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100219}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100220TEST(DMArGenericTests, BinaryEqual)
221{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100222 cper_log_section_dual_binary_test("dmargeneric");
Lawrence Tangcd505202022-07-19 16:55:11 +0100223}
224
225//VT-d DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100226TEST(DMArVtdTests, IRValid)
227{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100228 cper_log_section_dual_ir_test("dmarvtd");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100229}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100230TEST(DMArVtdTests, BinaryEqual)
231{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100232 cper_log_section_dual_binary_test("dmarvtd");
Lawrence Tangcd505202022-07-19 16:55:11 +0100233}
234
235//IOMMU DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100236TEST(DMArIOMMUTests, IRValid)
237{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100238 cper_log_section_dual_ir_test("dmariommu");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100239}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100240TEST(DMArIOMMUTests, BinaryEqual)
241{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100242 cper_log_section_dual_binary_test("dmariommu");
Lawrence Tangcd505202022-07-19 16:55:11 +0100243}
244
245//CCIX PER tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100246TEST(CCIXPERTests, IRValid)
247{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100248 cper_log_section_dual_ir_test("ccixper");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100249}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100250TEST(CCIXPERTests, BinaryEqual)
251{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100252 cper_log_section_dual_binary_test("ccixper");
Lawrence Tangcd505202022-07-19 16:55:11 +0100253}
254
255//CXL Protocol tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100256TEST(CXLProtocolTests, IRValid)
257{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100258 cper_log_section_dual_ir_test("cxlprotocol");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100259}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100260TEST(CXLProtocolTests, BinaryEqual)
261{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100262 cper_log_section_dual_binary_test("cxlprotocol");
Lawrence Tangcd505202022-07-19 16:55:11 +0100263}
264
265//CXL Component tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100266TEST(CXLComponentTests, IRValid)
267{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100268 cper_log_section_dual_ir_test("cxlcomponent");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100269}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100270TEST(CXLComponentTests, BinaryEqual)
271{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100272 cper_log_section_dual_binary_test("cxlcomponent");
Lawrence Tangcd505202022-07-19 16:55:11 +0100273}
274
275//Unknown section tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100276TEST(UnknownSectionTests, IRValid)
277{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100278 cper_log_section_dual_ir_test("unknown");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100279}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100280TEST(UnknownSectionTests, BinaryEqual)
281{
Lawrence Tanga4f662f2022-08-08 14:37:36 +0100282 cper_log_section_dual_binary_test("unknown");
Lawrence Tangcd505202022-07-19 16:55:11 +0100283}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100284
285//Entrypoint for the testing program.
286int main()
287{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100288 testing::InitGoogleTest();
289 return RUN_ALL_TESTS();
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100290}