Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 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 | */ |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 19 | |
| 20 | //Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 21 | void single_section_ir_test(const char* section_name) |
| 22 | { |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 23 | //Generate CPER record for the given type. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 24 | char* buf; |
| 25 | size_t size; |
| 26 | FILE* record = generate_record_memstream(§ion_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 Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 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(§ion_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 Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 65 | /* |
| 66 | * Single section tests. |
| 67 | */ |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 68 | //Generic processor tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 69 | TEST(GenericProcessorTests, IRValid) { |
| 70 | single_section_ir_test("generic"); |
| 71 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 72 | TEST(GenericProcessorTests, BinaryEqual) { |
| 73 | single_section_binary_test("generic"); |
| 74 | } |
| 75 | |
| 76 | //IA32/x64 tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 77 | TEST(IA32x64Tests, IRValid) { |
| 78 | single_section_ir_test("ia32x64"); |
| 79 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 80 | TEST(IA32x64Tests, BinaryEqual) { |
| 81 | single_section_binary_test("ia32x64"); |
| 82 | } |
| 83 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 84 | // TEST(IPFTests, IRValid) { |
| 85 | // single_section_ir_test("ipf"); |
| 86 | // } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 87 | |
| 88 | //ARM tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 89 | TEST(ArmTests, IRValid) { |
| 90 | single_section_ir_test("arm"); |
| 91 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 92 | TEST(ArmTests, BinaryEqual) { |
| 93 | single_section_binary_test("arm"); |
| 94 | } |
| 95 | |
| 96 | //Memory tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 97 | TEST(MemoryTests, IRValid) { |
| 98 | single_section_ir_test("memory"); |
| 99 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 100 | TEST(MemoryTests, BinaryEqual) { |
| 101 | single_section_binary_test("memory"); |
| 102 | } |
| 103 | |
| 104 | //Memory 2 tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 105 | TEST(Memory2Tests, IRValid) { |
| 106 | single_section_ir_test("memory2"); |
| 107 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 108 | TEST(Memory2Tests, BinaryEqual) { |
| 109 | single_section_binary_test("memory2"); |
| 110 | } |
| 111 | |
| 112 | //PCIe tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 113 | TEST(PCIeTests, IRValid) { |
| 114 | single_section_ir_test("pcie"); |
| 115 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 116 | TEST(PCIeTests, BinaryEqual) { |
| 117 | single_section_binary_test("pcie"); |
| 118 | } |
| 119 | |
| 120 | //Firmware tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 121 | TEST(FirmwareTests, IRValid) { |
| 122 | single_section_ir_test("firmware"); |
| 123 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 124 | TEST(FirmwareTests, BinaryEqual) { |
| 125 | single_section_binary_test("firmware"); |
| 126 | } |
| 127 | |
| 128 | //PCI Bus tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 129 | TEST(PCIBusTests, IRValid) { |
| 130 | single_section_ir_test("pcibus"); |
| 131 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 132 | TEST(PCIBusTests, BinaryEqual) { |
| 133 | single_section_binary_test("pcibus"); |
| 134 | } |
| 135 | |
| 136 | //PCI Device tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 137 | TEST(PCIDevTests, IRValid) { |
| 138 | single_section_ir_test("pcidev"); |
| 139 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 140 | TEST(PCIDevTests, BinaryEqual) { |
| 141 | single_section_binary_test("pcidev"); |
| 142 | } |
| 143 | |
| 144 | //Generic DMAr tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 145 | TEST(DMArGenericTests, IRValid) { |
| 146 | single_section_ir_test("dmargeneric"); |
| 147 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 148 | TEST(DMArGenericTests, BinaryEqual) { |
| 149 | single_section_binary_test("dmargeneric"); |
| 150 | } |
| 151 | |
| 152 | //VT-d DMAr tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 153 | TEST(DMArVtdTests, IRValid) { |
| 154 | single_section_ir_test("dmarvtd"); |
| 155 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 156 | TEST(DMArVtdTests, BinaryEqual) { |
| 157 | single_section_binary_test("dmarvtd"); |
| 158 | } |
| 159 | |
| 160 | //IOMMU DMAr tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 161 | TEST(DMArIOMMUTests, IRValid) { |
| 162 | single_section_ir_test("dmariommu"); |
| 163 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 164 | TEST(DMArIOMMUTests, BinaryEqual) { |
| 165 | single_section_binary_test("dmariommu"); |
| 166 | } |
| 167 | |
| 168 | //CCIX PER tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 169 | TEST(CCIXPERTests, IRValid) { |
| 170 | single_section_ir_test("ccixper"); |
| 171 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 172 | TEST(CCIXPERTests, BinaryEqual) { |
| 173 | single_section_binary_test("ccixper"); |
| 174 | } |
| 175 | |
| 176 | //CXL Protocol tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 177 | TEST(CXLProtocolTests, IRValid) { |
| 178 | single_section_ir_test("cxlprotocol"); |
| 179 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 180 | TEST(CXLProtocolTests, BinaryEqual) { |
| 181 | single_section_binary_test("cxlprotocol"); |
| 182 | } |
| 183 | |
| 184 | //CXL Component tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 185 | TEST(CXLComponentTests, IRValid) { |
| 186 | single_section_ir_test("cxlcomponent"); |
| 187 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 188 | TEST(CXLComponentTests, BinaryEqual) { |
| 189 | single_section_binary_test("cxlcomponent"); |
| 190 | } |
| 191 | |
| 192 | //Unknown section tests. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 193 | TEST(UnknownSectionTests, IRValid) { |
| 194 | single_section_ir_test("unknown"); |
| 195 | } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame^] | 196 | TEST(UnknownSectionTests, BinaryEqual) { |
| 197 | single_section_binary_test("unknown"); |
| 198 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 199 | |
| 200 | //Entrypoint for the testing program. |
| 201 | int main() |
| 202 | { |
| 203 | testing::InitGoogleTest(); |
| 204 | return RUN_ALL_TESTS(); |
| 205 | } |