xref: /openbmc/libcper/tests/test-utils.cpp (revision e42fb487839b242371b0150ab5b0b89c2d232976)
1d34f2b11SLawrence Tang /**
2d34f2b11SLawrence Tang  * Defines utility functions for testing CPER-JSON IR output from the cper-parse library.
3d34f2b11SLawrence Tang  *
4d34f2b11SLawrence Tang  * Author: Lawrence.Tang@arm.com
5d34f2b11SLawrence Tang  **/
6d34f2b11SLawrence Tang 
7f8fc7052SJohn Chung #include <cstdio>
8f8fc7052SJohn Chung #include <cstdlib>
9d34f2b11SLawrence Tang #include "test-utils.hpp"
10255bd81aSKarthik Rajagopalan 
11*e42fb487SThu Nguyen #include <libcper/BaseTypes.h>
12*e42fb487SThu Nguyen #include <libcper/generator/cper-generate.h>
13d34f2b11SLawrence Tang 
14d34f2b11SLawrence Tang //Returns a ready-for-use memory stream containing a CPER record with the given sections inside.
generate_record_memstream(const char ** types,UINT16 num_types,char ** buf,size_t * buf_size,int single_section)15e407b4c8SLawrence Tang FILE *generate_record_memstream(const char **types, UINT16 num_types,
16f8fc7052SJohn Chung 				char **buf, size_t *buf_size,
17f8fc7052SJohn Chung 				int single_section)
18d34f2b11SLawrence Tang {
19d34f2b11SLawrence Tang 	//Open a memory stream.
20d34f2b11SLawrence Tang 	FILE *stream = open_memstream(buf, buf_size);
21d34f2b11SLawrence Tang 
22d34f2b11SLawrence Tang 	//Generate a section to the stream, close & return.
23f8fc7052SJohn Chung 	if (!single_section) {
24f8fc7052SJohn Chung 		generate_cper_record(const_cast<char **>(types), num_types,
25f8fc7052SJohn Chung 				     stream);
26f8fc7052SJohn Chung 	} else {
27f8fc7052SJohn Chung 		generate_single_section_record(const_cast<char *>(types[0]),
28f8fc7052SJohn Chung 					       stream);
29f8fc7052SJohn Chung 	}
30d34f2b11SLawrence Tang 	fclose(stream);
31d34f2b11SLawrence Tang 
32d34f2b11SLawrence Tang 	//Return fmemopen() buffer for reading.
33d34f2b11SLawrence Tang 	return fmemopen(*buf, *buf_size, "r");
34d34f2b11SLawrence Tang }
35