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> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 9 | #include "test-utils.hpp" |
Karthik Rajagopalan | 255bd81 | 2024-09-06 14:36:34 -0700 | [diff] [blame] | 10 | |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 11 | #include <libcper/BaseTypes.h> |
| 12 | #include <libcper/generator/cper-generate.h> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 13 | |
| 14 | //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] | 15 | FILE *generate_record_memstream(const char **types, UINT16 num_types, |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 16 | char **buf, size_t *buf_size, |
| 17 | int single_section) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 18 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 19 | //Open a memory stream. |
| 20 | FILE *stream = open_memstream(buf, buf_size); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 21 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | //Generate a section to the stream, close & return. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 23 | if (!single_section) { |
| 24 | generate_cper_record(const_cast<char **>(types), num_types, |
| 25 | stream); |
| 26 | } else { |
| 27 | generate_single_section_record(const_cast<char *>(types[0]), |
| 28 | stream); |
| 29 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 30 | fclose(stream); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 31 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 32 | //Return fmemopen() buffer for reading. |
| 33 | return fmemopen(*buf, *buf_size, "r"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 34 | } |