blob: 86ec021a61b70f658baa9225265c00b25353540c [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 Tange407b4c2022-07-21 13:54:01 +010021void single_section_ir_test(const char *section_name)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010022{
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 //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);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010027
Lawrence Tange407b4c2022-07-21 13:54:01 +010028 //Convert to IR, free resources.
29 json_object *ir = cper_to_ir(record);
30 fclose(record);
31 free(buf);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010032
Lawrence Tange407b4c2022-07-21 13:54:01 +010033 //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;
Lawrence Tangd34f2b12022-07-19 15:36:31 +010038}
39
Lawrence Tangcd505202022-07-19 16:55:11 +010040//Checks for binary round-trip equality for a given randomly generated CPER record.
Lawrence Tange407b4c2022-07-21 13:54:01 +010041void single_section_binary_test(const char *section_name)
Lawrence Tangcd505202022-07-19 16:55:11 +010042{
Lawrence Tange407b4c2022-07-21 13:54:01 +010043 //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);
Lawrence Tangcd505202022-07-19 16:55:11 +010047
Lawrence Tange407b4c2022-07-21 13:54:01 +010048 //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);
Lawrence Tangcd505202022-07-19 16:55:11 +010056
Lawrence Tange407b4c2022-07-21 13:54:01 +010057 //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);
Lawrence Tangcd505202022-07-19 16:55:11 +010066}
67
Lawrence Tangd34f2b12022-07-19 15:36:31 +010068/*
69* Single section tests.
70*/
Lawrence Tangcd505202022-07-19 16:55:11 +010071//Generic processor tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +010072TEST(GenericProcessorTests, IRValid)
73{
74 single_section_ir_test("generic");
Lawrence Tangd34f2b12022-07-19 15:36:31 +010075}
Lawrence Tange407b4c2022-07-21 13:54:01 +010076TEST(GenericProcessorTests, BinaryEqual)
77{
78 single_section_binary_test("generic");
Lawrence Tangcd505202022-07-19 16:55:11 +010079}
80
81//IA32/x64 tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +010082TEST(IA32x64Tests, IRValid)
83{
84 single_section_ir_test("ia32x64");
Lawrence Tangd34f2b12022-07-19 15:36:31 +010085}
Lawrence Tange407b4c2022-07-21 13:54:01 +010086TEST(IA32x64Tests, BinaryEqual)
87{
88 single_section_binary_test("ia32x64");
Lawrence Tangcd505202022-07-19 16:55:11 +010089}
90
Lawrence Tangd34f2b12022-07-19 15:36:31 +010091// TEST(IPFTests, IRValid) {
92// single_section_ir_test("ipf");
93// }
Lawrence Tangcd505202022-07-19 16:55:11 +010094
95//ARM tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +010096TEST(ArmTests, IRValid)
97{
98 single_section_ir_test("arm");
Lawrence Tangd34f2b12022-07-19 15:36:31 +010099}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100100TEST(ArmTests, BinaryEqual)
101{
102 single_section_binary_test("arm");
Lawrence Tangcd505202022-07-19 16:55:11 +0100103}
104
105//Memory tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100106TEST(MemoryTests, IRValid)
107{
108 single_section_ir_test("memory");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100109}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100110TEST(MemoryTests, BinaryEqual)
111{
112 single_section_binary_test("memory");
Lawrence Tangcd505202022-07-19 16:55:11 +0100113}
114
115//Memory 2 tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116TEST(Memory2Tests, IRValid)
117{
118 single_section_ir_test("memory2");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100119}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100120TEST(Memory2Tests, BinaryEqual)
121{
122 single_section_binary_test("memory2");
Lawrence Tangcd505202022-07-19 16:55:11 +0100123}
124
125//PCIe tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126TEST(PCIeTests, IRValid)
127{
128 single_section_ir_test("pcie");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100129}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130TEST(PCIeTests, BinaryEqual)
131{
132 single_section_binary_test("pcie");
Lawrence Tangcd505202022-07-19 16:55:11 +0100133}
134
135//Firmware tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136TEST(FirmwareTests, IRValid)
137{
138 single_section_ir_test("firmware");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100139}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140TEST(FirmwareTests, BinaryEqual)
141{
142 single_section_binary_test("firmware");
Lawrence Tangcd505202022-07-19 16:55:11 +0100143}
144
145//PCI Bus tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100146TEST(PCIBusTests, IRValid)
147{
148 single_section_ir_test("pcibus");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100149}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100150TEST(PCIBusTests, BinaryEqual)
151{
152 single_section_binary_test("pcibus");
Lawrence Tangcd505202022-07-19 16:55:11 +0100153}
154
155//PCI Device tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156TEST(PCIDevTests, IRValid)
157{
158 single_section_ir_test("pcidev");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100159}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160TEST(PCIDevTests, BinaryEqual)
161{
162 single_section_binary_test("pcidev");
Lawrence Tangcd505202022-07-19 16:55:11 +0100163}
164
165//Generic DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100166TEST(DMArGenericTests, IRValid)
167{
168 single_section_ir_test("dmargeneric");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100169}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100170TEST(DMArGenericTests, BinaryEqual)
171{
172 single_section_binary_test("dmargeneric");
Lawrence Tangcd505202022-07-19 16:55:11 +0100173}
174
175//VT-d DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100176TEST(DMArVtdTests, IRValid)
177{
178 single_section_ir_test("dmarvtd");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100179}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100180TEST(DMArVtdTests, BinaryEqual)
181{
182 single_section_binary_test("dmarvtd");
Lawrence Tangcd505202022-07-19 16:55:11 +0100183}
184
185//IOMMU DMAr tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100186TEST(DMArIOMMUTests, IRValid)
187{
188 single_section_ir_test("dmariommu");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100189}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100190TEST(DMArIOMMUTests, BinaryEqual)
191{
192 single_section_binary_test("dmariommu");
Lawrence Tangcd505202022-07-19 16:55:11 +0100193}
194
195//CCIX PER tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196TEST(CCIXPERTests, IRValid)
197{
198 single_section_ir_test("ccixper");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100199}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100200TEST(CCIXPERTests, BinaryEqual)
201{
202 single_section_binary_test("ccixper");
Lawrence Tangcd505202022-07-19 16:55:11 +0100203}
204
205//CXL Protocol tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100206TEST(CXLProtocolTests, IRValid)
207{
208 single_section_ir_test("cxlprotocol");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100209}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100210TEST(CXLProtocolTests, BinaryEqual)
211{
212 single_section_binary_test("cxlprotocol");
Lawrence Tangcd505202022-07-19 16:55:11 +0100213}
214
215//CXL Component tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100216TEST(CXLComponentTests, IRValid)
217{
218 single_section_ir_test("cxlcomponent");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100219}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100220TEST(CXLComponentTests, BinaryEqual)
221{
222 single_section_binary_test("cxlcomponent");
Lawrence Tangcd505202022-07-19 16:55:11 +0100223}
224
225//Unknown section tests.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100226TEST(UnknownSectionTests, IRValid)
227{
228 single_section_ir_test("unknown");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100229}
Lawrence Tange407b4c2022-07-21 13:54:01 +0100230TEST(UnknownSectionTests, BinaryEqual)
231{
232 single_section_binary_test("unknown");
Lawrence Tangcd505202022-07-19 16:55:11 +0100233}
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100234
235//Entrypoint for the testing program.
236int main()
237{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100238 testing::InitGoogleTest();
239 return RUN_ALL_TESTS();
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100240}