xref: /openbmc/libcper/tests/ir-tests.cpp (revision cd505205)
1d34f2b11SLawrence Tang /**
2d34f2b11SLawrence Tang  * Defines tests for validating CPER-JSON IR output from the cper-parse library.
3d34f2b11SLawrence Tang  *
4d34f2b11SLawrence Tang  * Author: Lawrence.Tang@arm.com
5d34f2b11SLawrence Tang  **/
6d34f2b11SLawrence Tang 
7d34f2b11SLawrence Tang #include "gtest/gtest.h"
8d34f2b11SLawrence Tang #include "test-utils.hpp"
9d34f2b11SLawrence Tang extern "C" {
10d34f2b11SLawrence Tang #include "json.h"
11d34f2b11SLawrence Tang #include "../cper-parse.h"
12d34f2b11SLawrence Tang #include "../json-schema.h"
13d34f2b11SLawrence Tang #include "../generator/cper-generate.h"
14d34f2b11SLawrence Tang }
15d34f2b11SLawrence Tang 
16d34f2b11SLawrence Tang /*
17d34f2b11SLawrence Tang * Test templates.
18d34f2b11SLawrence Tang */
19*cd505205SLawrence Tang 
20*cd505205SLawrence Tang //Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
21d34f2b11SLawrence Tang void single_section_ir_test(const char* section_name)
22d34f2b11SLawrence Tang {
23*cd505205SLawrence Tang     //Generate CPER record for the given type.
24d34f2b11SLawrence Tang     char* buf;
25d34f2b11SLawrence Tang     size_t size;
26d34f2b11SLawrence Tang     FILE* record = generate_record_memstream(&section_name, 1, &buf, &size);
27d34f2b11SLawrence Tang 
28d34f2b11SLawrence Tang     //Convert to IR, free resources.
29d34f2b11SLawrence Tang     json_object* ir = cper_to_ir(record);
30d34f2b11SLawrence Tang     fclose(record);
31d34f2b11SLawrence Tang     free(buf);
32d34f2b11SLawrence Tang 
33d34f2b11SLawrence Tang     //Validate against schema.
34d34f2b11SLawrence Tang     char error_message[JSON_ERROR_MSG_MAX_LEN] = {0};
35d34f2b11SLawrence Tang     int valid = validate_schema_from_file("./specification/cper-json.json", ir, error_message);
36d34f2b11SLawrence Tang     ASSERT_TRUE(valid) << error_message;
37d34f2b11SLawrence Tang }
38d34f2b11SLawrence Tang 
39*cd505205SLawrence Tang //Checks for binary round-trip equality for a given randomly generated CPER record.
40*cd505205SLawrence Tang void single_section_binary_test(const char* section_name)
41*cd505205SLawrence Tang {
42*cd505205SLawrence Tang     //Generate CPER record for the given type.
43*cd505205SLawrence Tang     char* buf;
44*cd505205SLawrence Tang     size_t size;
45*cd505205SLawrence Tang     FILE* record = generate_record_memstream(&section_name, 1, &buf, &size);
46*cd505205SLawrence Tang 
47*cd505205SLawrence Tang     //Convert to IR, then back to binary, getting a stream out.
48*cd505205SLawrence Tang     json_object* ir = cper_to_ir(record);
49*cd505205SLawrence Tang     char* cper_buf;
50*cd505205SLawrence Tang     size_t cper_buf_size;
51*cd505205SLawrence Tang     FILE* stream = open_memstream(&cper_buf, &cper_buf_size);
52*cd505205SLawrence Tang     ir_to_cper(ir, stream);
53*cd505205SLawrence Tang     fclose(stream);
54*cd505205SLawrence Tang 
55*cd505205SLawrence Tang     //Validate the two are identical.
56*cd505205SLawrence Tang     ASSERT_EQ(size, cper_buf_size);
57*cd505205SLawrence Tang     ASSERT_EQ(memcmp(buf, cper_buf, size), 0) << "Binary output was not identical to input.";
58*cd505205SLawrence Tang 
59*cd505205SLawrence Tang     //Free everything up.
60*cd505205SLawrence Tang     fclose(record);
61*cd505205SLawrence Tang     free(buf);
62*cd505205SLawrence Tang     free(cper_buf);
63*cd505205SLawrence Tang }
64*cd505205SLawrence Tang 
65d34f2b11SLawrence Tang /*
66d34f2b11SLawrence Tang * Single section tests.
67d34f2b11SLawrence Tang */
68*cd505205SLawrence Tang //Generic processor tests.
69d34f2b11SLawrence Tang TEST(GenericProcessorTests, IRValid) {
70d34f2b11SLawrence Tang     single_section_ir_test("generic");
71d34f2b11SLawrence Tang }
72*cd505205SLawrence Tang TEST(GenericProcessorTests, BinaryEqual) {
73*cd505205SLawrence Tang     single_section_binary_test("generic");
74*cd505205SLawrence Tang }
75*cd505205SLawrence Tang 
76*cd505205SLawrence Tang //IA32/x64 tests.
77d34f2b11SLawrence Tang TEST(IA32x64Tests, IRValid) {
78d34f2b11SLawrence Tang     single_section_ir_test("ia32x64");
79d34f2b11SLawrence Tang }
80*cd505205SLawrence Tang TEST(IA32x64Tests, BinaryEqual) {
81*cd505205SLawrence Tang     single_section_binary_test("ia32x64");
82*cd505205SLawrence Tang }
83*cd505205SLawrence Tang 
84d34f2b11SLawrence Tang // TEST(IPFTests, IRValid) {
85d34f2b11SLawrence Tang //     single_section_ir_test("ipf");
86d34f2b11SLawrence Tang // }
87*cd505205SLawrence Tang 
88*cd505205SLawrence Tang //ARM tests.
89d34f2b11SLawrence Tang TEST(ArmTests, IRValid) {
90d34f2b11SLawrence Tang     single_section_ir_test("arm");
91d34f2b11SLawrence Tang }
92*cd505205SLawrence Tang TEST(ArmTests, BinaryEqual) {
93*cd505205SLawrence Tang     single_section_binary_test("arm");
94*cd505205SLawrence Tang }
95*cd505205SLawrence Tang 
96*cd505205SLawrence Tang //Memory tests.
97d34f2b11SLawrence Tang TEST(MemoryTests, IRValid) {
98d34f2b11SLawrence Tang     single_section_ir_test("memory");
99d34f2b11SLawrence Tang }
100*cd505205SLawrence Tang TEST(MemoryTests, BinaryEqual) {
101*cd505205SLawrence Tang     single_section_binary_test("memory");
102*cd505205SLawrence Tang }
103*cd505205SLawrence Tang 
104*cd505205SLawrence Tang //Memory 2 tests.
105d34f2b11SLawrence Tang TEST(Memory2Tests, IRValid) {
106d34f2b11SLawrence Tang     single_section_ir_test("memory2");
107d34f2b11SLawrence Tang }
108*cd505205SLawrence Tang TEST(Memory2Tests, BinaryEqual) {
109*cd505205SLawrence Tang     single_section_binary_test("memory2");
110*cd505205SLawrence Tang }
111*cd505205SLawrence Tang 
112*cd505205SLawrence Tang //PCIe tests.
113d34f2b11SLawrence Tang TEST(PCIeTests, IRValid) {
114d34f2b11SLawrence Tang     single_section_ir_test("pcie");
115d34f2b11SLawrence Tang }
116*cd505205SLawrence Tang TEST(PCIeTests, BinaryEqual) {
117*cd505205SLawrence Tang     single_section_binary_test("pcie");
118*cd505205SLawrence Tang }
119*cd505205SLawrence Tang 
120*cd505205SLawrence Tang //Firmware tests.
121d34f2b11SLawrence Tang TEST(FirmwareTests, IRValid) {
122d34f2b11SLawrence Tang     single_section_ir_test("firmware");
123d34f2b11SLawrence Tang }
124*cd505205SLawrence Tang TEST(FirmwareTests, BinaryEqual) {
125*cd505205SLawrence Tang     single_section_binary_test("firmware");
126*cd505205SLawrence Tang }
127*cd505205SLawrence Tang 
128*cd505205SLawrence Tang //PCI Bus tests.
129d34f2b11SLawrence Tang TEST(PCIBusTests, IRValid) {
130d34f2b11SLawrence Tang     single_section_ir_test("pcibus");
131d34f2b11SLawrence Tang }
132*cd505205SLawrence Tang TEST(PCIBusTests, BinaryEqual) {
133*cd505205SLawrence Tang     single_section_binary_test("pcibus");
134*cd505205SLawrence Tang }
135*cd505205SLawrence Tang 
136*cd505205SLawrence Tang //PCI Device tests.
137d34f2b11SLawrence Tang TEST(PCIDevTests, IRValid) {
138d34f2b11SLawrence Tang     single_section_ir_test("pcidev");
139d34f2b11SLawrence Tang }
140*cd505205SLawrence Tang TEST(PCIDevTests, BinaryEqual) {
141*cd505205SLawrence Tang     single_section_binary_test("pcidev");
142*cd505205SLawrence Tang }
143*cd505205SLawrence Tang 
144*cd505205SLawrence Tang //Generic DMAr tests.
145d34f2b11SLawrence Tang TEST(DMArGenericTests, IRValid) {
146d34f2b11SLawrence Tang     single_section_ir_test("dmargeneric");
147d34f2b11SLawrence Tang }
148*cd505205SLawrence Tang TEST(DMArGenericTests, BinaryEqual) {
149*cd505205SLawrence Tang     single_section_binary_test("dmargeneric");
150*cd505205SLawrence Tang }
151*cd505205SLawrence Tang 
152*cd505205SLawrence Tang //VT-d DMAr tests.
153d34f2b11SLawrence Tang TEST(DMArVtdTests, IRValid) {
154d34f2b11SLawrence Tang     single_section_ir_test("dmarvtd");
155d34f2b11SLawrence Tang }
156*cd505205SLawrence Tang TEST(DMArVtdTests, BinaryEqual) {
157*cd505205SLawrence Tang     single_section_binary_test("dmarvtd");
158*cd505205SLawrence Tang }
159*cd505205SLawrence Tang 
160*cd505205SLawrence Tang //IOMMU DMAr tests.
161d34f2b11SLawrence Tang TEST(DMArIOMMUTests, IRValid) {
162d34f2b11SLawrence Tang     single_section_ir_test("dmariommu");
163d34f2b11SLawrence Tang }
164*cd505205SLawrence Tang TEST(DMArIOMMUTests, BinaryEqual) {
165*cd505205SLawrence Tang     single_section_binary_test("dmariommu");
166*cd505205SLawrence Tang }
167*cd505205SLawrence Tang 
168*cd505205SLawrence Tang //CCIX PER tests.
169d34f2b11SLawrence Tang TEST(CCIXPERTests, IRValid) {
170d34f2b11SLawrence Tang     single_section_ir_test("ccixper");
171d34f2b11SLawrence Tang }
172*cd505205SLawrence Tang TEST(CCIXPERTests, BinaryEqual) {
173*cd505205SLawrence Tang     single_section_binary_test("ccixper");
174*cd505205SLawrence Tang }
175*cd505205SLawrence Tang 
176*cd505205SLawrence Tang //CXL Protocol tests.
177d34f2b11SLawrence Tang TEST(CXLProtocolTests, IRValid) {
178d34f2b11SLawrence Tang     single_section_ir_test("cxlprotocol");
179d34f2b11SLawrence Tang }
180*cd505205SLawrence Tang TEST(CXLProtocolTests, BinaryEqual) {
181*cd505205SLawrence Tang     single_section_binary_test("cxlprotocol");
182*cd505205SLawrence Tang }
183*cd505205SLawrence Tang 
184*cd505205SLawrence Tang //CXL Component tests.
185d34f2b11SLawrence Tang TEST(CXLComponentTests, IRValid) {
186d34f2b11SLawrence Tang     single_section_ir_test("cxlcomponent");
187d34f2b11SLawrence Tang }
188*cd505205SLawrence Tang TEST(CXLComponentTests, BinaryEqual) {
189*cd505205SLawrence Tang     single_section_binary_test("cxlcomponent");
190*cd505205SLawrence Tang }
191*cd505205SLawrence Tang 
192*cd505205SLawrence Tang //Unknown section tests.
193d34f2b11SLawrence Tang TEST(UnknownSectionTests, IRValid) {
194d34f2b11SLawrence Tang     single_section_ir_test("unknown");
195d34f2b11SLawrence Tang }
196*cd505205SLawrence Tang TEST(UnknownSectionTests, BinaryEqual) {
197*cd505205SLawrence Tang     single_section_binary_test("unknown");
198*cd505205SLawrence Tang }
199d34f2b11SLawrence Tang 
200d34f2b11SLawrence Tang //Entrypoint for the testing program.
201d34f2b11SLawrence Tang int main()
202d34f2b11SLawrence Tang {
203d34f2b11SLawrence Tang     testing::InitGoogleTest();
204d34f2b11SLawrence Tang     return RUN_ALL_TESTS();
205d34f2b11SLawrence Tang }