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 */ 19 20 //Tests a single randomly generated CPER section of the given type to ensure CPER-JSON IR validity. 21 void cper_log_section_ir_test(const char *section_name, int single_section) 22 { 23 //Generate full CPER record for the given type. 24 char *buf; 25 size_t size; 26 FILE *record = generate_record_memstream(§ion_name, 1, &buf, &size, 27 single_section); 28 29 //Convert to IR, free resources. 30 json_object *ir; 31 if (single_section) 32 ir = cper_single_section_to_ir(record); 33 else 34 ir = cper_to_ir(record); 35 fclose(record); 36 free(buf); 37 38 //Validate against schema. 39 char error_message[JSON_ERROR_MSG_MAX_LEN] = { 0 }; 40 int valid = validate_schema_from_file("./specification/cper-json.json", 41 ir, error_message); 42 ASSERT_TRUE(valid) 43 << "IR validation test failed (single section mode = " 44 << single_section << ") with message: " << error_message; 45 } 46 47 //Checks for binary round-trip equality for a given randomly generated CPER record. 48 void cper_log_section_binary_test(const char *section_name, int single_section) 49 { 50 //Generate CPER record for the given type. 51 char *buf; 52 size_t size; 53 FILE *record = generate_record_memstream(§ion_name, 1, &buf, &size, 54 single_section); 55 56 //Convert to IR. 57 json_object *ir; 58 if (single_section) 59 ir = cper_single_section_to_ir(record); 60 else 61 ir = cper_to_ir(record); 62 63 //Now convert back to binary, and get a stream out. 64 char *cper_buf; 65 size_t cper_buf_size; 66 FILE *stream = open_memstream(&cper_buf, &cper_buf_size); 67 if (single_section) 68 ir_single_section_to_cper(ir, stream); 69 else 70 ir_to_cper(ir, stream); 71 size_t cper_len = ftell(stream); 72 fclose(stream); 73 74 //Validate the two are identical. 75 ASSERT_GE(size, cper_len); 76 ASSERT_EQ(memcmp(buf, cper_buf, cper_len), 0) 77 << "Binary output was not identical to input (single section mode = " 78 << single_section << ")."; 79 80 //Free everything up. 81 fclose(record); 82 free(buf); 83 free(cper_buf); 84 } 85 86 //Tests randomly generated CPER sections for IR validity of a given type, in both single section mode and full CPER log mode. 87 void cper_log_section_dual_ir_test(const char *section_name) 88 { 89 cper_log_section_ir_test(section_name, 0); 90 cper_log_section_ir_test(section_name, 1); 91 } 92 93 //Tests randomly generated CPER sections for binary compatibility of a given type, in both single section mode and full CPER log mode. 94 void cper_log_section_dual_binary_test(const char *section_name) 95 { 96 cper_log_section_binary_test(section_name, 0); 97 cper_log_section_binary_test(section_name, 1); 98 } 99 100 /* 101 * Single section tests. 102 */ 103 104 //Generic processor tests. 105 TEST(GenericProcessorTests, IRValid) 106 { 107 cper_log_section_dual_ir_test("generic"); 108 } 109 TEST(GenericProcessorTests, BinaryEqual) 110 { 111 cper_log_section_dual_binary_test("generic"); 112 } 113 114 //IA32/x64 tests. 115 TEST(IA32x64Tests, IRValid) 116 { 117 cper_log_section_dual_ir_test("ia32x64"); 118 } 119 TEST(IA32x64Tests, BinaryEqual) 120 { 121 cper_log_section_dual_binary_test("ia32x64"); 122 } 123 124 // TEST(IPFTests, IRValid) { 125 // cper_log_section_dual_ir_test("ipf"); 126 // } 127 128 //ARM tests. 129 TEST(ArmTests, IRValid) 130 { 131 cper_log_section_dual_ir_test("arm"); 132 } 133 TEST(ArmTests, BinaryEqual) 134 { 135 cper_log_section_dual_binary_test("arm"); 136 } 137 138 //Memory tests. 139 TEST(MemoryTests, IRValid) 140 { 141 cper_log_section_dual_ir_test("memory"); 142 } 143 TEST(MemoryTests, BinaryEqual) 144 { 145 cper_log_section_dual_binary_test("memory"); 146 } 147 148 //Memory 2 tests. 149 TEST(Memory2Tests, IRValid) 150 { 151 cper_log_section_dual_ir_test("memory2"); 152 } 153 TEST(Memory2Tests, BinaryEqual) 154 { 155 cper_log_section_dual_binary_test("memory2"); 156 } 157 158 //PCIe tests. 159 TEST(PCIeTests, IRValid) 160 { 161 cper_log_section_dual_ir_test("pcie"); 162 } 163 TEST(PCIeTests, BinaryEqual) 164 { 165 cper_log_section_dual_binary_test("pcie"); 166 } 167 168 //Firmware tests. 169 TEST(FirmwareTests, IRValid) 170 { 171 cper_log_section_dual_ir_test("firmware"); 172 } 173 TEST(FirmwareTests, BinaryEqual) 174 { 175 cper_log_section_dual_binary_test("firmware"); 176 } 177 178 //PCI Bus tests. 179 TEST(PCIBusTests, IRValid) 180 { 181 cper_log_section_dual_ir_test("pcibus"); 182 } 183 TEST(PCIBusTests, BinaryEqual) 184 { 185 cper_log_section_dual_binary_test("pcibus"); 186 } 187 188 //PCI Device tests. 189 TEST(PCIDevTests, IRValid) 190 { 191 cper_log_section_dual_ir_test("pcidev"); 192 } 193 TEST(PCIDevTests, BinaryEqual) 194 { 195 cper_log_section_dual_binary_test("pcidev"); 196 } 197 198 //Generic DMAr tests. 199 TEST(DMArGenericTests, IRValid) 200 { 201 cper_log_section_dual_ir_test("dmargeneric"); 202 } 203 TEST(DMArGenericTests, BinaryEqual) 204 { 205 cper_log_section_dual_binary_test("dmargeneric"); 206 } 207 208 //VT-d DMAr tests. 209 TEST(DMArVtdTests, IRValid) 210 { 211 cper_log_section_dual_ir_test("dmarvtd"); 212 } 213 TEST(DMArVtdTests, BinaryEqual) 214 { 215 cper_log_section_dual_binary_test("dmarvtd"); 216 } 217 218 //IOMMU DMAr tests. 219 TEST(DMArIOMMUTests, IRValid) 220 { 221 cper_log_section_dual_ir_test("dmariommu"); 222 } 223 TEST(DMArIOMMUTests, BinaryEqual) 224 { 225 cper_log_section_dual_binary_test("dmariommu"); 226 } 227 228 //CCIX PER tests. 229 TEST(CCIXPERTests, IRValid) 230 { 231 cper_log_section_dual_ir_test("ccixper"); 232 } 233 TEST(CCIXPERTests, BinaryEqual) 234 { 235 cper_log_section_dual_binary_test("ccixper"); 236 } 237 238 //CXL Protocol tests. 239 TEST(CXLProtocolTests, IRValid) 240 { 241 cper_log_section_dual_ir_test("cxlprotocol"); 242 } 243 TEST(CXLProtocolTests, BinaryEqual) 244 { 245 cper_log_section_dual_binary_test("cxlprotocol"); 246 } 247 248 //CXL Component tests. 249 TEST(CXLComponentTests, IRValid) 250 { 251 cper_log_section_dual_ir_test("cxlcomponent"); 252 } 253 TEST(CXLComponentTests, BinaryEqual) 254 { 255 cper_log_section_dual_binary_test("cxlcomponent"); 256 } 257 258 //Unknown section tests. 259 TEST(UnknownSectionTests, IRValid) 260 { 261 cper_log_section_dual_ir_test("unknown"); 262 } 263 TEST(UnknownSectionTests, BinaryEqual) 264 { 265 cper_log_section_dual_binary_test("unknown"); 266 } 267 268 //Entrypoint for the testing program. 269 int main() 270 { 271 testing::InitGoogleTest(); 272 return RUN_ALL_TESTS(); 273 }