xref: /openbmc/libcper/tests/ir-tests.cpp (revision 8f977457)
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" {
105202bbb4SLawrence Tang #include <json.h>
11d34f2b11SLawrence Tang #include "../cper-parse.h"
12d34f2b11SLawrence Tang #include "../json-schema.h"
13d34f2b11SLawrence Tang #include "../generator/cper-generate.h"
14580423feSLawrence Tang #include "../sections/cper-section.h"
15d34f2b11SLawrence Tang }
16d34f2b11SLawrence Tang 
17d34f2b11SLawrence Tang /*
18d34f2b11SLawrence Tang * Test templates.
19d34f2b11SLawrence Tang */
20cd505205SLawrence Tang 
21cd505205SLawrence Tang //Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
22a4f662f1SLawrence Tang void cper_log_section_ir_test(const char *section_name, int single_section)
23d34f2b11SLawrence Tang {
24a4f662f1SLawrence Tang 	//Generate full CPER record for the given type.
25d34f2b11SLawrence Tang 	char *buf;
26d34f2b11SLawrence Tang 	size_t size;
27a4f662f1SLawrence Tang 	FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
28a4f662f1SLawrence Tang 						 single_section);
29d34f2b11SLawrence Tang 
30d34f2b11SLawrence Tang 	//Convert to IR, free resources.
31a4f662f1SLawrence Tang 	json_object *ir;
32a4f662f1SLawrence Tang 	if (single_section)
33a4f662f1SLawrence Tang 		ir = cper_single_section_to_ir(record);
34a4f662f1SLawrence Tang 	else
35a4f662f1SLawrence Tang 		ir = cper_to_ir(record);
36d34f2b11SLawrence Tang 	fclose(record);
37d34f2b11SLawrence Tang 	free(buf);
38d34f2b11SLawrence Tang 
39d34f2b11SLawrence Tang 	//Validate against schema.
40d34f2b11SLawrence Tang 	char error_message[JSON_ERROR_MSG_MAX_LEN] = { 0 };
41e407b4c8SLawrence Tang 	int valid = validate_schema_from_file("./specification/cper-json.json",
42e407b4c8SLawrence Tang 					      ir, error_message);
43a4f662f1SLawrence Tang 	ASSERT_TRUE(valid)
44a4f662f1SLawrence Tang 		<< "IR validation test failed (single section mode = "
45a4f662f1SLawrence Tang 		<< single_section << ") with message: " << error_message;
46d34f2b11SLawrence Tang }
47d34f2b11SLawrence Tang 
48cd505205SLawrence Tang //Checks for binary round-trip equality for a given randomly generated CPER record.
49a4f662f1SLawrence Tang void cper_log_section_binary_test(const char *section_name, int single_section)
50cd505205SLawrence Tang {
51cd505205SLawrence Tang 	//Generate CPER record for the given type.
52cd505205SLawrence Tang 	char *buf;
53cd505205SLawrence Tang 	size_t size;
54a4f662f1SLawrence Tang 	FILE *record = generate_record_memstream(&section_name, 1, &buf, &size,
55a4f662f1SLawrence Tang 						 single_section);
56cd505205SLawrence Tang 
57a4f662f1SLawrence Tang 	//Convert to IR.
58a4f662f1SLawrence Tang 	json_object *ir;
59a4f662f1SLawrence Tang 	if (single_section)
60a4f662f1SLawrence Tang 		ir = cper_single_section_to_ir(record);
61a4f662f1SLawrence Tang 	else
62a4f662f1SLawrence Tang 		ir = cper_to_ir(record);
63a4f662f1SLawrence Tang 
64a4f662f1SLawrence Tang 	//Now convert back to binary, and get a stream out.
65cd505205SLawrence Tang 	char *cper_buf;
66cd505205SLawrence Tang 	size_t cper_buf_size;
67cd505205SLawrence Tang 	FILE *stream = open_memstream(&cper_buf, &cper_buf_size);
68a4f662f1SLawrence Tang 	if (single_section)
69a4f662f1SLawrence Tang 		ir_single_section_to_cper(ir, stream);
70a4f662f1SLawrence Tang 	else
71cd505205SLawrence Tang 		ir_to_cper(ir, stream);
72aacf0e26SLawrence Tang 	size_t cper_len = ftell(stream);
73cd505205SLawrence Tang 	fclose(stream);
74cd505205SLawrence Tang 
75cd505205SLawrence Tang 	//Validate the two are identical.
76aacf0e26SLawrence Tang 	ASSERT_GE(size, cper_len);
77e407b4c8SLawrence Tang 	ASSERT_EQ(memcmp(buf, cper_buf, cper_len), 0)
78a4f662f1SLawrence Tang 		<< "Binary output was not identical to input (single section mode = "
79a4f662f1SLawrence Tang 		<< single_section << ").";
80cd505205SLawrence Tang 
81cd505205SLawrence Tang 	//Free everything up.
82cd505205SLawrence Tang 	fclose(record);
83cd505205SLawrence Tang 	free(buf);
84cd505205SLawrence Tang 	free(cper_buf);
85cd505205SLawrence Tang }
86cd505205SLawrence Tang 
87a4f662f1SLawrence Tang //Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode.
88a4f662f1SLawrence Tang void cper_log_section_dual_ir_test(const char *section_name)
89a4f662f1SLawrence Tang {
90a4f662f1SLawrence Tang 	cper_log_section_ir_test(section_name, 0);
91a4f662f1SLawrence Tang 	cper_log_section_ir_test(section_name, 1);
92a4f662f1SLawrence Tang }
93a4f662f1SLawrence Tang 
94a4f662f1SLawrence Tang //Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode.
95a4f662f1SLawrence Tang void cper_log_section_dual_binary_test(const char *section_name)
96a4f662f1SLawrence Tang {
97a4f662f1SLawrence Tang 	cper_log_section_binary_test(section_name, 0);
98a4f662f1SLawrence Tang 	cper_log_section_binary_test(section_name, 1);
99a4f662f1SLawrence Tang }
100a4f662f1SLawrence Tang 
101d34f2b11SLawrence Tang /*
102580423feSLawrence Tang * Non-single section assertions.
103580423feSLawrence Tang */
104580423feSLawrence Tang TEST(CompileTimeAssertions, TwoWayConversion)
105580423feSLawrence Tang {
106580423feSLawrence Tang 	for (int i=0; i<section_definitions_len; i++)
107580423feSLawrence Tang 	{
108580423feSLawrence Tang 		//If a conversion one way exists, a conversion the other way must exist.
109580423feSLawrence Tang 		std::string err = "If a CPER conversion exists one way, there must be an equivalent method in reverse.";
110580423feSLawrence Tang 		if (section_definitions[i].ToCPER != NULL)
111580423feSLawrence Tang 			ASSERT_NE(section_definitions[i].ToIR, NULL) << err;
112580423feSLawrence Tang 		if (section_definitions[i].ToIR != NULL)
113580423feSLawrence Tang 			ASSERT_NE(section_definitions[i].ToCPER, NULL) << err;
114580423feSLawrence Tang 	}
115580423feSLawrence Tang }
116580423feSLawrence Tang 
117580423feSLawrence Tang /*
118d34f2b11SLawrence Tang * Single section tests.
119d34f2b11SLawrence Tang */
120a4f662f1SLawrence Tang 
121cd505205SLawrence Tang //Generic processor tests.
122e407b4c8SLawrence Tang TEST(GenericProcessorTests, IRValid)
123e407b4c8SLawrence Tang {
124a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("generic");
125d34f2b11SLawrence Tang }
126e407b4c8SLawrence Tang TEST(GenericProcessorTests, BinaryEqual)
127e407b4c8SLawrence Tang {
128a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("generic");
129cd505205SLawrence Tang }
130cd505205SLawrence Tang 
131cd505205SLawrence Tang //IA32/x64 tests.
132e407b4c8SLawrence Tang TEST(IA32x64Tests, IRValid)
133e407b4c8SLawrence Tang {
134a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("ia32x64");
135d34f2b11SLawrence Tang }
136e407b4c8SLawrence Tang TEST(IA32x64Tests, BinaryEqual)
137e407b4c8SLawrence Tang {
138a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("ia32x64");
139cd505205SLawrence Tang }
140cd505205SLawrence Tang 
141d34f2b11SLawrence Tang // TEST(IPFTests, IRValid) {
142a4f662f1SLawrence Tang //     cper_log_section_dual_ir_test("ipf");
143d34f2b11SLawrence Tang // }
144cd505205SLawrence Tang 
145cd505205SLawrence Tang //ARM tests.
146e407b4c8SLawrence Tang TEST(ArmTests, IRValid)
147e407b4c8SLawrence Tang {
148a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("arm");
149d34f2b11SLawrence Tang }
150e407b4c8SLawrence Tang TEST(ArmTests, BinaryEqual)
151e407b4c8SLawrence Tang {
152a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("arm");
153cd505205SLawrence Tang }
154cd505205SLawrence Tang 
155cd505205SLawrence Tang //Memory tests.
156e407b4c8SLawrence Tang TEST(MemoryTests, IRValid)
157e407b4c8SLawrence Tang {
158a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("memory");
159d34f2b11SLawrence Tang }
160e407b4c8SLawrence Tang TEST(MemoryTests, BinaryEqual)
161e407b4c8SLawrence Tang {
162a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("memory");
163cd505205SLawrence Tang }
164cd505205SLawrence Tang 
165cd505205SLawrence Tang //Memory 2 tests.
166e407b4c8SLawrence Tang TEST(Memory2Tests, IRValid)
167e407b4c8SLawrence Tang {
168a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("memory2");
169d34f2b11SLawrence Tang }
170e407b4c8SLawrence Tang TEST(Memory2Tests, BinaryEqual)
171e407b4c8SLawrence Tang {
172a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("memory2");
173cd505205SLawrence Tang }
174cd505205SLawrence Tang 
175cd505205SLawrence Tang //PCIe tests.
176e407b4c8SLawrence Tang TEST(PCIeTests, IRValid)
177e407b4c8SLawrence Tang {
178a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("pcie");
179d34f2b11SLawrence Tang }
180e407b4c8SLawrence Tang TEST(PCIeTests, BinaryEqual)
181e407b4c8SLawrence Tang {
182a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("pcie");
183cd505205SLawrence Tang }
184cd505205SLawrence Tang 
185cd505205SLawrence Tang //Firmware tests.
186e407b4c8SLawrence Tang TEST(FirmwareTests, IRValid)
187e407b4c8SLawrence Tang {
188a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("firmware");
189d34f2b11SLawrence Tang }
190e407b4c8SLawrence Tang TEST(FirmwareTests, BinaryEqual)
191e407b4c8SLawrence Tang {
192a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("firmware");
193cd505205SLawrence Tang }
194cd505205SLawrence Tang 
195cd505205SLawrence Tang //PCI Bus tests.
196e407b4c8SLawrence Tang TEST(PCIBusTests, IRValid)
197e407b4c8SLawrence Tang {
198a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("pcibus");
199d34f2b11SLawrence Tang }
200e407b4c8SLawrence Tang TEST(PCIBusTests, BinaryEqual)
201e407b4c8SLawrence Tang {
202a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("pcibus");
203cd505205SLawrence Tang }
204cd505205SLawrence Tang 
205cd505205SLawrence Tang //PCI Device tests.
206e407b4c8SLawrence Tang TEST(PCIDevTests, IRValid)
207e407b4c8SLawrence Tang {
208a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("pcidev");
209d34f2b11SLawrence Tang }
210e407b4c8SLawrence Tang TEST(PCIDevTests, BinaryEqual)
211e407b4c8SLawrence Tang {
212a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("pcidev");
213cd505205SLawrence Tang }
214cd505205SLawrence Tang 
215cd505205SLawrence Tang //Generic DMAr tests.
216e407b4c8SLawrence Tang TEST(DMArGenericTests, IRValid)
217e407b4c8SLawrence Tang {
218a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("dmargeneric");
219d34f2b11SLawrence Tang }
220e407b4c8SLawrence Tang TEST(DMArGenericTests, BinaryEqual)
221e407b4c8SLawrence Tang {
222a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("dmargeneric");
223cd505205SLawrence Tang }
224cd505205SLawrence Tang 
225cd505205SLawrence Tang //VT-d DMAr tests.
226e407b4c8SLawrence Tang TEST(DMArVtdTests, IRValid)
227e407b4c8SLawrence Tang {
228a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("dmarvtd");
229d34f2b11SLawrence Tang }
230e407b4c8SLawrence Tang TEST(DMArVtdTests, BinaryEqual)
231e407b4c8SLawrence Tang {
232a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("dmarvtd");
233cd505205SLawrence Tang }
234cd505205SLawrence Tang 
235cd505205SLawrence Tang //IOMMU DMAr tests.
236e407b4c8SLawrence Tang TEST(DMArIOMMUTests, IRValid)
237e407b4c8SLawrence Tang {
238a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("dmariommu");
239d34f2b11SLawrence Tang }
240e407b4c8SLawrence Tang TEST(DMArIOMMUTests, BinaryEqual)
241e407b4c8SLawrence Tang {
242a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("dmariommu");
243cd505205SLawrence Tang }
244cd505205SLawrence Tang 
245cd505205SLawrence Tang //CCIX PER tests.
246e407b4c8SLawrence Tang TEST(CCIXPERTests, IRValid)
247e407b4c8SLawrence Tang {
248a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("ccixper");
249d34f2b11SLawrence Tang }
250e407b4c8SLawrence Tang TEST(CCIXPERTests, BinaryEqual)
251e407b4c8SLawrence Tang {
252a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("ccixper");
253cd505205SLawrence Tang }
254cd505205SLawrence Tang 
255cd505205SLawrence Tang //CXL Protocol tests.
256e407b4c8SLawrence Tang TEST(CXLProtocolTests, IRValid)
257e407b4c8SLawrence Tang {
258a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("cxlprotocol");
259d34f2b11SLawrence Tang }
260e407b4c8SLawrence Tang TEST(CXLProtocolTests, BinaryEqual)
261e407b4c8SLawrence Tang {
262a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("cxlprotocol");
263cd505205SLawrence Tang }
264cd505205SLawrence Tang 
265cd505205SLawrence Tang //CXL Component tests.
266e407b4c8SLawrence Tang TEST(CXLComponentTests, IRValid)
267e407b4c8SLawrence Tang {
268*8f977457SLawrence Tang 	cper_log_section_dual_ir_test("cxlcomponent-media");
269d34f2b11SLawrence Tang }
270e407b4c8SLawrence Tang TEST(CXLComponentTests, BinaryEqual)
271e407b4c8SLawrence Tang {
272*8f977457SLawrence Tang 	cper_log_section_dual_binary_test("cxlcomponent-media");
273cd505205SLawrence Tang }
274cd505205SLawrence Tang 
275cd505205SLawrence Tang //Unknown section tests.
276e407b4c8SLawrence Tang TEST(UnknownSectionTests, IRValid)
277e407b4c8SLawrence Tang {
278a4f662f1SLawrence Tang 	cper_log_section_dual_ir_test("unknown");
279d34f2b11SLawrence Tang }
280e407b4c8SLawrence Tang TEST(UnknownSectionTests, BinaryEqual)
281e407b4c8SLawrence Tang {
282a4f662f1SLawrence Tang 	cper_log_section_dual_binary_test("unknown");
283cd505205SLawrence Tang }
284d34f2b11SLawrence Tang 
285d34f2b11SLawrence Tang //Entrypoint for the testing program.
286d34f2b11SLawrence Tang int main()
287d34f2b11SLawrence Tang {
288d34f2b11SLawrence Tang 	testing::InitGoogleTest();
289d34f2b11SLawrence Tang 	return RUN_ALL_TESTS();
290d34f2b11SLawrence Tang }