blob: 2275663422271e3ed90affe86148659044e99035 [file] [log] [blame]
Lawrence Tangd34f2b12022-07-19 15:36:31 +01001/**
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"
9extern "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*/
Lawrence Tangcd505202022-07-19 16:55:11 +010019
20//Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010021void single_section_ir_test(const char* section_name)
22{
Lawrence Tangcd505202022-07-19 16:55:11 +010023 //Generate CPER record for the given type.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010024 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
Lawrence Tangcd505202022-07-19 16:55:11 +010039//Checks for binary round-trip equality for a given randomly generated CPER record.
40void 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
Lawrence Tangd34f2b12022-07-19 15:36:31 +010065/*
66* Single section tests.
67*/
Lawrence Tangcd505202022-07-19 16:55:11 +010068//Generic processor tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010069TEST(GenericProcessorTests, IRValid) {
70 single_section_ir_test("generic");
71}
Lawrence Tangcd505202022-07-19 16:55:11 +010072TEST(GenericProcessorTests, BinaryEqual) {
73 single_section_binary_test("generic");
74}
75
76//IA32/x64 tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010077TEST(IA32x64Tests, IRValid) {
78 single_section_ir_test("ia32x64");
79}
Lawrence Tangcd505202022-07-19 16:55:11 +010080TEST(IA32x64Tests, BinaryEqual) {
81 single_section_binary_test("ia32x64");
82}
83
Lawrence Tangd34f2b12022-07-19 15:36:31 +010084// TEST(IPFTests, IRValid) {
85// single_section_ir_test("ipf");
86// }
Lawrence Tangcd505202022-07-19 16:55:11 +010087
88//ARM tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010089TEST(ArmTests, IRValid) {
90 single_section_ir_test("arm");
91}
Lawrence Tangcd505202022-07-19 16:55:11 +010092TEST(ArmTests, BinaryEqual) {
93 single_section_binary_test("arm");
94}
95
96//Memory tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010097TEST(MemoryTests, IRValid) {
98 single_section_ir_test("memory");
99}
Lawrence Tangcd505202022-07-19 16:55:11 +0100100TEST(MemoryTests, BinaryEqual) {
101 single_section_binary_test("memory");
102}
103
104//Memory 2 tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100105TEST(Memory2Tests, IRValid) {
106 single_section_ir_test("memory2");
107}
Lawrence Tangcd505202022-07-19 16:55:11 +0100108TEST(Memory2Tests, BinaryEqual) {
109 single_section_binary_test("memory2");
110}
111
112//PCIe tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100113TEST(PCIeTests, IRValid) {
114 single_section_ir_test("pcie");
115}
Lawrence Tangcd505202022-07-19 16:55:11 +0100116TEST(PCIeTests, BinaryEqual) {
117 single_section_binary_test("pcie");
118}
119
120//Firmware tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100121TEST(FirmwareTests, IRValid) {
122 single_section_ir_test("firmware");
123}
Lawrence Tangcd505202022-07-19 16:55:11 +0100124TEST(FirmwareTests, BinaryEqual) {
125 single_section_binary_test("firmware");
126}
127
128//PCI Bus tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100129TEST(PCIBusTests, IRValid) {
130 single_section_ir_test("pcibus");
131}
Lawrence Tangcd505202022-07-19 16:55:11 +0100132TEST(PCIBusTests, BinaryEqual) {
133 single_section_binary_test("pcibus");
134}
135
136//PCI Device tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100137TEST(PCIDevTests, IRValid) {
138 single_section_ir_test("pcidev");
139}
Lawrence Tangcd505202022-07-19 16:55:11 +0100140TEST(PCIDevTests, BinaryEqual) {
141 single_section_binary_test("pcidev");
142}
143
144//Generic DMAr tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100145TEST(DMArGenericTests, IRValid) {
146 single_section_ir_test("dmargeneric");
147}
Lawrence Tangcd505202022-07-19 16:55:11 +0100148TEST(DMArGenericTests, BinaryEqual) {
149 single_section_binary_test("dmargeneric");
150}
151
152//VT-d DMAr tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100153TEST(DMArVtdTests, IRValid) {
154 single_section_ir_test("dmarvtd");
155}
Lawrence Tangcd505202022-07-19 16:55:11 +0100156TEST(DMArVtdTests, BinaryEqual) {
157 single_section_binary_test("dmarvtd");
158}
159
160//IOMMU DMAr tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100161TEST(DMArIOMMUTests, IRValid) {
162 single_section_ir_test("dmariommu");
163}
Lawrence Tangcd505202022-07-19 16:55:11 +0100164TEST(DMArIOMMUTests, BinaryEqual) {
165 single_section_binary_test("dmariommu");
166}
167
168//CCIX PER tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100169TEST(CCIXPERTests, IRValid) {
170 single_section_ir_test("ccixper");
171}
Lawrence Tangcd505202022-07-19 16:55:11 +0100172TEST(CCIXPERTests, BinaryEqual) {
173 single_section_binary_test("ccixper");
174}
175
176//CXL Protocol tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100177TEST(CXLProtocolTests, IRValid) {
178 single_section_ir_test("cxlprotocol");
179}
Lawrence Tangcd505202022-07-19 16:55:11 +0100180TEST(CXLProtocolTests, BinaryEqual) {
181 single_section_binary_test("cxlprotocol");
182}
183
184//CXL Component tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100185TEST(CXLComponentTests, IRValid) {
186 single_section_ir_test("cxlcomponent");
187}
Lawrence Tangcd505202022-07-19 16:55:11 +0100188TEST(CXLComponentTests, BinaryEqual) {
189 single_section_binary_test("cxlcomponent");
190}
191
192//Unknown section tests.
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100193TEST(UnknownSectionTests, IRValid) {
194 single_section_ir_test("unknown");
195}
Lawrence Tangcd505202022-07-19 16:55:11 +0100196TEST(UnknownSectionTests, BinaryEqual) {
197 single_section_binary_test("unknown");
198}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100199
200//Entrypoint for the testing program.
201int main()
202{
203 testing::InitGoogleTest();
204 return RUN_ALL_TESTS();
205}