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> |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 10 | #include <map> |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 11 | #include <filesystem> |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 12 | #include <vector> |
| 13 | #include <algorithm> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 14 | #include "test-utils.hpp" |
Karthik Rajagopalan | 255bd81 | 2024-09-06 14:36:34 -0700 | [diff] [blame] | 15 | |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 16 | #include <libcper/BaseTypes.h> |
| 17 | #include <libcper/generator/cper-generate.h> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 18 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 19 | extern "C" { |
| 20 | #include <jsoncdaccord.h> |
| 21 | #include <json.h> |
| 22 | #include <libcper/log.h> |
| 23 | } |
| 24 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 25 | namespace fs = std::filesystem; |
| 26 | |
Ed Tanous | 2c4d7b6 | 2025-03-16 12:22:02 -0700 | [diff] [blame] | 27 | // Objects that have mutually exclusive fields (and thereforce can't have both |
| 28 | // required at the same time) can be added to this list. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 29 | // Truly optional properties that shouldn't be added to "required" field for |
| 30 | // validating the entire schema with validationbits=1 |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 31 | // In most cases making sure examples set all valid bits is preferable to adding to this list |
| 32 | const static std::vector<std::string> optional_props = { |
| 33 | { // Some sections don't parse header correctly? |
| 34 | "header", |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 35 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 36 | // Each section is optional |
| 37 | "GenericProcessor", "Ia32x64Processor", "ArmProcessor", "Memory", |
| 38 | "Memory2", "Pcie", "PciBus", "PciComponent", "Firmware", |
| 39 | "GenericDmar", "VtdDmar", "IommuDmar", "CcixPer", "CxlProtocol", |
| 40 | "CxlComponent", "Nvidia", "Ampere", "Unknown", |
| 41 | |
| 42 | // CXL? might have a bug? |
| 43 | "partitionID", |
| 44 | |
| 45 | // CXL protocol |
| 46 | "capabilityStructure", "deviceSerial", |
| 47 | |
| 48 | // CXL component |
| 49 | "cxlComponentEventLog", "addressSpace", "errorType", |
| 50 | "participationType", "timedOut", "level", "operation", "preciseIP", |
| 51 | "restartableIP", "overflow", "uncorrected", "transactionType", |
| 52 | |
| 53 | // PCIe AER |
| 54 | "addressSpace", "errorType", "participationType", "timedOut", "level", |
| 55 | "operation", "preciseIP", "restartableIP", "overflow", "uncorrected", |
| 56 | "transactionType" } |
| 57 | }; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 58 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 59 | //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] | 60 | FILE *generate_record_memstream(const char **types, UINT16 num_types, |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 61 | char **buf, size_t *buf_size, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 62 | int single_section, |
| 63 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 64 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | //Open a memory stream. |
| 66 | FILE *stream = open_memstream(buf, buf_size); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 67 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 68 | //Generate a section to the stream, close & return. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 69 | if (!single_section) { |
| 70 | generate_cper_record(const_cast<char **>(types), num_types, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 71 | stream, validBitsType); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 72 | } else { |
| 73 | generate_single_section_record(const_cast<char *>(types[0]), |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 74 | stream, validBitsType); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 75 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 76 | fclose(stream); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 77 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 78 | //Return fmemopen() buffer for reading. |
| 79 | return fmemopen(*buf, *buf_size, "r"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 80 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 81 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 82 | int iterate_make_required_props(json_object *jsonSchema, bool all_valid_bits) |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 83 | { |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 84 | //properties |
| 85 | json_object *properties = |
| 86 | json_object_object_get(jsonSchema, "properties"); |
| 87 | |
| 88 | if (properties != nullptr) { |
| 89 | json_object *requrired_arr = json_object_new_array(); |
| 90 | |
| 91 | json_object_object_foreach(properties, property_name, |
| 92 | property_value) |
| 93 | { |
| 94 | bool add_to_required = true; |
| 95 | const auto it_find_opt_prop = std::ranges::find( |
| 96 | optional_props, property_name); |
| 97 | if (it_find_opt_prop != optional_props.end()) { |
| 98 | add_to_required = false; |
| 99 | } |
| 100 | |
| 101 | if (add_to_required) { |
| 102 | //Add to list if property is not optional |
| 103 | json_object_array_add( |
| 104 | requrired_arr, |
| 105 | json_object_new_string(property_name)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | json_object_object_foreach(properties, property_name2, |
| 110 | property_value2) |
| 111 | { |
| 112 | (void)property_name2; |
| 113 | if (iterate_make_required_props(property_value2, |
| 114 | all_valid_bits) < 0) { |
| 115 | return -1; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (all_valid_bits) { |
| 120 | json_object_object_add(jsonSchema, "required", |
| 121 | requrired_arr); |
| 122 | } |
| 123 | //json_object_put(requrired_arr); |
| 124 | } |
| 125 | |
| 126 | // ref |
| 127 | json_object *ref = json_object_object_get(jsonSchema, "$ref"); |
| 128 | if (ref != nullptr) { |
| 129 | const char *ref_str = json_object_get_string(ref); |
| 130 | if (ref_str != nullptr) { |
| 131 | std::string ref_path = LIBCPER_JSON_SPEC; |
| 132 | // remove the leading . |
| 133 | ref_path += std::string(ref_str).substr(1); |
| 134 | json_object *ref_obj = |
| 135 | json_object_from_file(ref_path.c_str()); |
| 136 | if (ref_obj == nullptr) { |
| 137 | printf("Failed to parse file: %s\n", |
| 138 | ref_path.c_str()); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | if (iterate_make_required_props(ref_obj, |
| 143 | all_valid_bits) < 0) { |
| 144 | json_object_put(ref_obj); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | json_object_object_foreach(ref_obj, key, val) |
| 149 | { |
| 150 | json_object_object_add(jsonSchema, key, |
| 151 | json_object_get(val)); |
| 152 | } |
| 153 | json_object_object_del(jsonSchema, "$ref"); |
| 154 | |
| 155 | json_object_put(ref_obj); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 158 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 159 | //oneOf |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 160 | const json_object *oneOf = json_object_object_get(jsonSchema, "oneOf"); |
| 161 | if (oneOf != nullptr) { |
| 162 | size_t num_elements = json_object_array_length(oneOf); |
| 163 | |
| 164 | for (size_t i = 0; i < num_elements; i++) { |
| 165 | json_object *obj = json_object_array_get_idx(oneOf, i); |
| 166 | if (iterate_make_required_props(obj, all_valid_bits) < |
| 167 | 0) { |
| 168 | return -1; |
| 169 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | //items |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 174 | const json_object *items = json_object_object_get(jsonSchema, "items"); |
| 175 | if (items != nullptr) { |
| 176 | json_object_object_foreach(items, key, val) |
| 177 | { |
| 178 | (void)key; |
| 179 | if (iterate_make_required_props(val, all_valid_bits) < |
| 180 | 0) { |
| 181 | return -1; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 182 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 186 | return 1; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 189 | int schema_validate_from_file(json_object *to_test, int single_section, |
| 190 | int all_valid_bits) |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 191 | { |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 192 | const char *schema_file; |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 193 | if (single_section) { |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 194 | schema_file = "cper-json-section-log.json"; |
Ed Tanous | d6b6263 | 2025-03-14 15:30:07 -0700 | [diff] [blame] | 195 | } else { |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 196 | schema_file = "cper-json-full-log.json"; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 199 | std::string schema_path = LIBCPER_JSON_SPEC; |
| 200 | schema_path += "/"; |
| 201 | schema_path += schema_file; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 202 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 203 | json_object *schema = json_object_from_file(schema_path.c_str()); |
| 204 | if (schema == nullptr) { |
| 205 | cper_print_log("Could not parse schema file: %s", schema_file); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Ed Tanous | a366305 | 2025-03-16 12:54:36 -0700 | [diff] [blame^] | 209 | if (iterate_make_required_props(schema, all_valid_bits) < 0) { |
| 210 | printf("Failed to make required props\n"); |
| 211 | json_object_put(schema); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | int err = jdac_validate(to_test, schema); |
| 216 | if (err == JDAC_ERR_VALID) { |
| 217 | printf("validation ok\n"); |
| 218 | json_object_put(schema); |
| 219 | return 1; |
| 220 | } |
| 221 | printf("validate failed %d: %s\n", err, jdac_errorstr(err)); |
| 222 | |
| 223 | printf("schema: \n%s\n", |
| 224 | json_object_to_json_string_ext(schema, JSON_C_TO_STRING_PRETTY)); |
| 225 | printf("to_test: \n%s\n", json_object_to_json_string_ext( |
| 226 | to_test, JSON_C_TO_STRING_PRETTY)); |
| 227 | json_object_put(schema); |
| 228 | return 0; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 229 | } |