Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
Lawrence Tang | 368e0b4 | 2022-07-07 14:31:06 +0100 | [diff] [blame] | 2 | #include "../cper-parse.h" |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 3 | #include "json.h" |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 4 | #include "../json-schema.h" |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 5 | |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame^] | 6 | void test_ir_to_cper(int argc, char* argv[]); |
| 7 | void test_cper_to_ir(int argc, char* argv[]); |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 8 | |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame^] | 9 | int main(int argc, char* argv[]) |
| 10 | { |
| 11 | test_ir_to_cper(argc, argv); |
| 12 | return 0; |
| 13 | } |
| 14 | |
| 15 | void test_ir_to_cper(int argc, char* argv[]) |
| 16 | { |
| 17 | //Read JSON IR from file. |
| 18 | json_object* ir = json_object_from_file(argv[1]); |
| 19 | if (ir == NULL) |
| 20 | { |
| 21 | printf("Could not read IR JSON, import returned null."); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | //Open a read for the output file. |
| 26 | FILE* cper_file = fopen(argv[2], "w"); |
| 27 | if (cper_file == NULL) { |
| 28 | printf("Could not open output file, file handle returned null."); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | //Run the converter. |
| 33 | ir_to_cper(ir, cper_file); |
| 34 | fclose(cper_file); |
| 35 | printf("Conversion finished!\n"); |
| 36 | } |
| 37 | |
| 38 | void test_cper_to_ir(int argc, char* argv[]) |
| 39 | { |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 40 | //Get a handle for the log file. |
| 41 | FILE* cper_file = fopen(argv[1], "r"); |
| 42 | if (cper_file == NULL) { |
| 43 | printf("Could not open CPER record, file handle returned null."); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame^] | 44 | return; |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | json_object* ir = cper_to_ir(cper_file); |
| 48 | fclose(cper_file); |
| 49 | |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 50 | const char* json_output = json_object_to_json_string(ir); |
| 51 | printf("\n%s\n", json_output); |
| 52 | |
| 53 | //Test JSON validator. |
| 54 | if (argc >= 3) |
| 55 | { |
| 56 | printf("Validating output with specification %s...\n", argv[2]); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 57 | validate_schema_debug_enable(); |
| 58 | char* error_message = malloc(JSON_ERROR_MSG_MAX_LEN); |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 59 | if (!validate_schema_from_file(argv[2], ir, error_message)) |
| 60 | { |
| 61 | printf("Validation failed: %s\n", error_message); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | printf("Validation passed!\n"); |
| 66 | } |
| 67 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 68 | } |