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