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