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 | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | void single_section_ir_test(const char *section_name) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 22 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 23 | //Generate CPER record for the given type. |
| 24 | char *buf; |
| 25 | size_t size; |
| 26 | FILE *record = generate_record_memstream(§ion_name, 1, &buf, &size); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 27 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 28 | //Convert to IR, free resources. |
| 29 | json_object *ir = cper_to_ir(record); |
| 30 | fclose(record); |
| 31 | free(buf); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 32 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 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", |
| 36 | ir, error_message); |
| 37 | ASSERT_TRUE(valid) << error_message; |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 40 | //Checks for binary round-trip equality for a given randomly generated CPER record. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 41 | void single_section_binary_test(const char *section_name) |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 42 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 43 | //Generate CPER record for the given type. |
| 44 | char *buf; |
| 45 | size_t size; |
| 46 | FILE *record = generate_record_memstream(§ion_name, 1, &buf, &size); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 47 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 48 | //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 Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 56 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 57 | //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 Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 68 | /* |
| 69 | * Single section tests. |
| 70 | */ |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 71 | //Generic processor tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 72 | TEST(GenericProcessorTests, IRValid) |
| 73 | { |
| 74 | single_section_ir_test("generic"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 75 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 76 | TEST(GenericProcessorTests, BinaryEqual) |
| 77 | { |
| 78 | single_section_binary_test("generic"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | //IA32/x64 tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 82 | TEST(IA32x64Tests, IRValid) |
| 83 | { |
| 84 | single_section_ir_test("ia32x64"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 85 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 86 | TEST(IA32x64Tests, BinaryEqual) |
| 87 | { |
| 88 | single_section_binary_test("ia32x64"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 91 | // TEST(IPFTests, IRValid) { |
| 92 | // single_section_ir_test("ipf"); |
| 93 | // } |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 94 | |
| 95 | //ARM tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 96 | TEST(ArmTests, IRValid) |
| 97 | { |
| 98 | single_section_ir_test("arm"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 99 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 100 | TEST(ArmTests, BinaryEqual) |
| 101 | { |
| 102 | single_section_binary_test("arm"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | //Memory tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 106 | TEST(MemoryTests, IRValid) |
| 107 | { |
| 108 | single_section_ir_test("memory"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 109 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 110 | TEST(MemoryTests, BinaryEqual) |
| 111 | { |
| 112 | single_section_binary_test("memory"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | //Memory 2 tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 116 | TEST(Memory2Tests, IRValid) |
| 117 | { |
| 118 | single_section_ir_test("memory2"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 119 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 120 | TEST(Memory2Tests, BinaryEqual) |
| 121 | { |
| 122 | single_section_binary_test("memory2"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | //PCIe tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 126 | TEST(PCIeTests, IRValid) |
| 127 | { |
| 128 | single_section_ir_test("pcie"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 129 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 130 | TEST(PCIeTests, BinaryEqual) |
| 131 | { |
| 132 | single_section_binary_test("pcie"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | //Firmware tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 136 | TEST(FirmwareTests, IRValid) |
| 137 | { |
| 138 | single_section_ir_test("firmware"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 139 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 140 | TEST(FirmwareTests, BinaryEqual) |
| 141 | { |
| 142 | single_section_binary_test("firmware"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | //PCI Bus tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 146 | TEST(PCIBusTests, IRValid) |
| 147 | { |
| 148 | single_section_ir_test("pcibus"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 149 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 150 | TEST(PCIBusTests, BinaryEqual) |
| 151 | { |
| 152 | single_section_binary_test("pcibus"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | //PCI Device tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 156 | TEST(PCIDevTests, IRValid) |
| 157 | { |
| 158 | single_section_ir_test("pcidev"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 159 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 160 | TEST(PCIDevTests, BinaryEqual) |
| 161 | { |
| 162 | single_section_binary_test("pcidev"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | //Generic DMAr tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 166 | TEST(DMArGenericTests, IRValid) |
| 167 | { |
| 168 | single_section_ir_test("dmargeneric"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 169 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 170 | TEST(DMArGenericTests, BinaryEqual) |
| 171 | { |
| 172 | single_section_binary_test("dmargeneric"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | //VT-d DMAr tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 176 | TEST(DMArVtdTests, IRValid) |
| 177 | { |
| 178 | single_section_ir_test("dmarvtd"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 179 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 180 | TEST(DMArVtdTests, BinaryEqual) |
| 181 | { |
| 182 | single_section_binary_test("dmarvtd"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | //IOMMU DMAr tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 186 | TEST(DMArIOMMUTests, IRValid) |
| 187 | { |
| 188 | single_section_ir_test("dmariommu"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 189 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 190 | TEST(DMArIOMMUTests, BinaryEqual) |
| 191 | { |
| 192 | single_section_binary_test("dmariommu"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | //CCIX PER tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 196 | TEST(CCIXPERTests, IRValid) |
| 197 | { |
| 198 | single_section_ir_test("ccixper"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 199 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 200 | TEST(CCIXPERTests, BinaryEqual) |
| 201 | { |
| 202 | single_section_binary_test("ccixper"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | //CXL Protocol tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 206 | TEST(CXLProtocolTests, IRValid) |
| 207 | { |
| 208 | single_section_ir_test("cxlprotocol"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 209 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 210 | TEST(CXLProtocolTests, BinaryEqual) |
| 211 | { |
| 212 | single_section_binary_test("cxlprotocol"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | //CXL Component tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 216 | TEST(CXLComponentTests, IRValid) |
| 217 | { |
| 218 | single_section_ir_test("cxlcomponent"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 219 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 220 | TEST(CXLComponentTests, BinaryEqual) |
| 221 | { |
| 222 | single_section_binary_test("cxlcomponent"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | //Unknown section tests. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 226 | TEST(UnknownSectionTests, IRValid) |
| 227 | { |
| 228 | single_section_ir_test("unknown"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 229 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 230 | TEST(UnknownSectionTests, BinaryEqual) |
| 231 | { |
| 232 | single_section_binary_test("unknown"); |
Lawrence Tang | cd50520 | 2022-07-19 16:55:11 +0100 | [diff] [blame] | 233 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 234 | |
| 235 | //Entrypoint for the testing program. |
| 236 | int main() |
| 237 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 238 | testing::InitGoogleTest(); |
| 239 | return RUN_ALL_TESTS(); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 240 | } |