Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Defines utility functions for testing CPER-JSON IR output from the cper-parse library. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstdlib> |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 9 | #include <fstream> |
| 10 | #include <filesystem> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 11 | #include "test-utils.hpp" |
Karthik Rajagopalan | 255bd81 | 2024-09-06 14:36:34 -0700 | [diff] [blame] | 12 | |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 13 | #include <libcper/BaseTypes.h> |
| 14 | #include <libcper/generator/cper-generate.h> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 15 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 16 | namespace fs = std::filesystem; |
| 17 | |
Ed Tanous | 2c4d7b6 | 2025-03-16 12:22:02 -0700 | [diff] [blame] | 18 | // Objects that have mutually exclusive fields (and thereforce can't have both |
| 19 | // required at the same time) can be added to this list. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 20 | // Truly optional properties that shouldn't be added to "required" field for |
| 21 | // validating the entire schema with validationbits=1 |
| 22 | const static std::map<std::string, std::vector<std::string> > |
| 23 | optional_properties_map = { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 24 | { "./sections/cper-cxl-protocol.json", |
| 25 | { "capabilityStructure", "deviceSerial" } }, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 26 | { "./sections/cper-cxl-component.json", |
| 27 | { "cxlComponentEventLog" } }, |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 28 | { "./sections/cper-ia32x64-processor.json", |
| 29 | { "addressSpace", "errorType", "participationType", |
| 30 | "timedOut", "level", "operation", "preciseIP", |
| 31 | "restartableIP", "overflow", "uncorrected", |
| 32 | "transactionType" } }, |
Erwin Tsaur | 8870c07 | 2025-02-28 12:57:12 -0800 | [diff] [blame^] | 33 | // Several PCIe properties are dependent on the device capability |
| 34 | // version and whether the device is running in Flit-Mode. |
| 35 | // All of these properties are optional. |
| 36 | { "./sections/cper-pcie.json", |
| 37 | { "device_capabilities2", "device_status2", "device_control2", |
| 38 | "link_capabilities2", "link_status2", "link_control2", |
| 39 | "slot_capabilities2", "slot_status2", "slot_control2" } }, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | nlohmann::json loadJson(const char *filePath) |
| 43 | { |
| 44 | std::ifstream file(filePath); |
| 45 | if (!file.is_open()) { |
| 46 | std::cerr << "Failed to open file: " << filePath << std::endl; |
| 47 | } |
| 48 | nlohmann::json out = nlohmann::json::parse(file, nullptr, false); |
| 49 | return out; |
| 50 | } |
| 51 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 52 | //Returns a ready-for-use memory stream containing a CPER record with the given sections inside. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 53 | FILE *generate_record_memstream(const char **types, UINT16 num_types, |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 54 | char **buf, size_t *buf_size, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 55 | int single_section, |
| 56 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 57 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 58 | //Open a memory stream. |
| 59 | FILE *stream = open_memstream(buf, buf_size); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 60 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 61 | //Generate a section to the stream, close & return. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 62 | if (!single_section) { |
| 63 | generate_cper_record(const_cast<char **>(types), num_types, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 64 | stream, validBitsType); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 65 | } else { |
| 66 | generate_single_section_record(const_cast<char *>(types[0]), |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 67 | stream, validBitsType); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 68 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 69 | fclose(stream); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 70 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 71 | //Return fmemopen() buffer for reading. |
| 72 | return fmemopen(*buf, *buf_size, "r"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 73 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 74 | |
| 75 | void iterate_make_required_props(nlohmann::json &jsonSchema, |
| 76 | std::vector<std::string> &optional_props) |
| 77 | { |
| 78 | //id |
| 79 | const auto it_id = jsonSchema.find("$id"); |
| 80 | if (it_id != jsonSchema.end()) { |
| 81 | auto id_strptr = it_id->get_ptr<const std::string *>(); |
| 82 | std::string id_str = *id_strptr; |
| 83 | if (id_str.find("header") != std::string::npos || |
| 84 | id_str.find("section-descriptor") != std::string::npos) { |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | //oneOf |
| 89 | const auto it_oneof = jsonSchema.find("oneOf"); |
| 90 | if (it_oneof != jsonSchema.end()) { |
| 91 | //Iterate over oneOf properties |
| 92 | for (auto &oneOfProp : *it_oneof) { |
| 93 | iterate_make_required_props(oneOfProp, optional_props); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | //items |
| 98 | const auto it_items = jsonSchema.find("items"); |
| 99 | if (it_items != jsonSchema.end()) { |
| 100 | iterate_make_required_props(*it_items, optional_props); |
| 101 | } |
| 102 | //required |
| 103 | const auto it_req = jsonSchema.find("required"); |
| 104 | if (it_req == jsonSchema.end()) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | //properties |
| 109 | const auto it_prop = jsonSchema.find("properties"); |
| 110 | if (it_prop == jsonSchema.end()) { |
| 111 | return; |
| 112 | } |
| 113 | nlohmann::json &propertyFields = *it_prop; |
| 114 | nlohmann::json::array_t property_list; |
| 115 | if (propertyFields.is_object()) { |
| 116 | for (auto &[key, value] : propertyFields.items()) { |
| 117 | const auto it_find_opt_prop = |
| 118 | std::find(optional_props.begin(), |
| 119 | optional_props.end(), key); |
| 120 | if (it_find_opt_prop == optional_props.end()) { |
| 121 | //Add to list if property is not optional |
| 122 | property_list.push_back(key); |
| 123 | } |
| 124 | |
| 125 | iterate_make_required_props(value, optional_props); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | *it_req = property_list; |
| 130 | } |
| 131 | |
| 132 | // Document loader callback function |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 133 | const nlohmann::json *documentLoader(const std::string &uri, |
| 134 | AddRequiredProps add_required_props) |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 135 | { |
| 136 | // Load the schema from a file |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 137 | std::unique_ptr<nlohmann::json> ref_schema = |
| 138 | std::make_unique<nlohmann::json>(); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 139 | *ref_schema = loadJson(uri.c_str()); |
| 140 | if (ref_schema->is_discarded()) { |
| 141 | std::cerr << "Could not open schema file: " << uri << std::endl; |
| 142 | } |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 143 | if (add_required_props == AddRequiredProps::YES) { |
| 144 | std::vector<std::string> opt = {}; |
| 145 | const auto it_optional_file = optional_properties_map.find(uri); |
| 146 | if (it_optional_file != optional_properties_map.end()) { |
| 147 | opt = it_optional_file->second; |
| 148 | } |
| 149 | iterate_make_required_props(*ref_schema, opt); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 150 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 151 | |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 152 | return ref_schema.release(); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Document release callback function |
| 156 | void documentRelease(const nlohmann::json *adapter) |
| 157 | { |
| 158 | delete adapter; // Free the adapter memory |
| 159 | } |
| 160 | |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 161 | std::unique_ptr<valijson::Schema> |
| 162 | load_schema(AddRequiredProps add_required_props, int single_section) |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 163 | { |
| 164 | // Load the schema |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 165 | fs::path pathObj(LIBCPER_JSON_SPEC); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 166 | |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 167 | if (single_section) { |
| 168 | pathObj /= "cper-json-section-log.json"; |
| 169 | } else { |
| 170 | pathObj /= "cper-json-full-log.json"; |
| 171 | } |
| 172 | nlohmann::json schema_root = loadJson(pathObj.c_str()); |
| 173 | fs::path base_path(LIBCPER_JSON_SPEC); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 174 | try { |
| 175 | fs::current_path(base_path); |
| 176 | // std::cout << "Changed directory to: " << fs::current_path() |
| 177 | // << std::endl; |
| 178 | } catch (const fs::filesystem_error &e) { |
| 179 | std::cerr << "Filesystem error: " << e.what() << std::endl; |
| 180 | } |
| 181 | |
| 182 | // Parse the json schema into an internal schema format |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 183 | std::unique_ptr<valijson::Schema> schema = |
| 184 | std::make_unique<valijson::Schema>(); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 185 | valijson::SchemaParser parser; |
| 186 | valijson::adapters::NlohmannJsonAdapter schemaDocumentAdapter( |
| 187 | schema_root); |
| 188 | |
| 189 | // Set up callbacks for resolving external references |
| 190 | try { |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 191 | parser.populateSchema( |
| 192 | schemaDocumentAdapter, *schema, |
| 193 | [add_required_props](const std::string &uri) { |
| 194 | return documentLoader(uri, add_required_props); |
| 195 | }, |
| 196 | documentRelease); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 197 | } catch (std::exception &e) { |
| 198 | std::cerr << "Failed to parse schema: " << e.what() |
| 199 | << std::endl; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 200 | } |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 201 | return schema; |
| 202 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 203 | |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 204 | int schema_validate_from_file(const valijson::Schema &schema, |
| 205 | nlohmann::json &jsonData, |
| 206 | std::string &error_message) |
| 207 | { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 208 | // Perform validation |
| 209 | valijson::Validator validator(valijson::Validator::kStrongTypes); |
| 210 | valijson::ValidationResults results; |
| 211 | valijson::adapters::NlohmannJsonAdapter targetDocumentAdapter(jsonData); |
| 212 | if (!validator.validate(schema, targetDocumentAdapter, &results)) { |
| 213 | std::cerr << "Validation failed." << std::endl; |
| 214 | valijson::ValidationResults::Error error; |
| 215 | unsigned int errorNum = 1; |
| 216 | while (results.popError(error)) { |
| 217 | std::string context; |
Ed Tanous | badae11 | 2025-03-10 13:38:51 -0700 | [diff] [blame] | 218 | for (const std::string &str : error.context) { |
| 219 | context += str; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Ed Tanous | badae11 | 2025-03-10 13:38:51 -0700 | [diff] [blame] | 222 | std::cout << "Error #" << errorNum << '\n' |
| 223 | << " context: " << context << '\n' |
| 224 | << " desc: " << error.description << '\n'; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 225 | ++errorNum; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | error_message = "Schema validation successful"; |
| 231 | return 1; |
| 232 | } |