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