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