Convert files to hex
It was pointed out in code review these files would be easier to review
diffs on if they were in hex format on disk. This commit converts all
the existing files to "cperhex" which is cper in hex hexadecimal format
using the command 'xxd -p -l 64'
Change-Id: I5e762ec27a02b3d918b926a966074da8178d73b8
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/tests/ir-tests.cpp b/tests/ir-tests.cpp
index fa1f9f3..00bbf6a 100644
--- a/tests/ir-tests.cpp
+++ b/tests/ir-tests.cpp
@@ -8,6 +8,7 @@
#include "gtest/gtest.h"
#include "test-utils.hpp"
#include <json.h>
+#include <charconv>
#include <nlohmann/json.hpp>
#include <filesystem>
#include <fstream>
@@ -16,6 +17,7 @@
#include <libcper/generator/cper-generate.h>
#include <libcper/sections/cper-section.h>
#include <libcper/generator/sections/gen-section.h>
+#include <format>
namespace fs = std::filesystem;
@@ -31,7 +33,7 @@
//Generate full CPER record for the given type.
fs::path file_path = LIBCPER_EXAMPLES;
file_path /= section_name;
- fs::path cper_out = file_path.replace_extension("cper");
+ fs::path cper_out = file_path.replace_extension("cperhex");
fs::path json_out = file_path.replace_extension("json");
char *buf;
@@ -47,15 +49,21 @@
return;
}
- char buffer[1024];
- size_t bytesRead;
- while ((bytesRead = fread(buffer, 1, sizeof(buffer), record)) > 0) {
- outFile.write(buffer, bytesRead);
- if (!outFile) {
- std::cerr << "Failed to write to output file."
- << std::endl;
- outFile.close();
- return;
+ std::vector<unsigned char> file_data;
+ fseek(record, 0, SEEK_END);
+ size_t file_size = ftell(record);
+ rewind(record);
+ file_data.resize(file_size);
+ if (fread(file_data.data(), 1, file_data.size(), record) != file_size) {
+ std::cerr << "Failed to read CPER data from memstream."
+ << std::endl;
+ FAIL();
+ return;
+ }
+ for (size_t index = 0; index < file_data.size(); index++) {
+ outFile << std::format("{:02x}", file_data[index]);
+ if (index % 30 == 29) {
+ outFile << "\n";
}
}
outFile.close();
@@ -84,29 +92,51 @@
jsonOutFile.close();
}
+std::vector<unsigned char> string_to_binary(const std::string &source)
+{
+ std::vector<unsigned char> retval;
+ bool uppernibble = true;
+ for (const char c : source) {
+ unsigned char val = 0;
+ if (c == '\n') {
+ continue;
+ }
+ std::from_chars_result r = std::from_chars(&c, &c + 1, val, 16);
+ EXPECT_TRUE(r.ec == std::error_code())
+ << "Invalid hex character in test file: " << c;
+
+ if (uppernibble) {
+ retval.push_back(val << 4);
+ } else {
+ retval.back() += val;
+ }
+ uppernibble = !uppernibble;
+ }
+ return retval;
+}
+
//Tests fixed CPER sections for IR validity with an example set.
void cper_example_section_ir_test(const char *section_name)
{
//Open CPER record for the given type.
fs::path fpath = LIBCPER_EXAMPLES;
fpath /= section_name;
- fs::path cper = fpath.replace_extension("cper");
+ fs::path cper = fpath.replace_extension("cperhex");
fs::path json = fpath.replace_extension("json");
- // Do a C style read to obtain FILE*
- FILE *record = fopen(cper.string().c_str(), "rb");
- if (record == NULL) {
- std::cerr
- << "cper_example_section_ir_test: File cannot be opened/does not exist "
- << cper << std::endl;
- FAIL() << "cper_example_section_ir_test: File cannot be opened/does not exist";
+ std::ifstream cper_file(cper, std::ios::binary);
+ if (!cper_file.is_open()) {
+ std::cerr << "Failed to open CPER file: " << cper << std::endl;
+ FAIL() << "Failed to open CPER file";
return;
}
+ std::string cper_str((std::istreambuf_iterator<char>(cper_file)),
+ std::istreambuf_iterator<char>());
+ std::vector<unsigned char> cper_bin = string_to_binary(cper_str);
//Convert to IR, free resources.
- json_object *ir = cper_to_ir(record);
+ json_object *ir = cper_buf_to_ir(cper_bin.data(), cper_bin.size());
if (ir == NULL) {
- fclose(record);
std::cerr << "Empty JSON from CPER bin" << std::endl;
FAIL();
return;
@@ -117,12 +147,10 @@
std::cerr << "cper_example_section_ir_test: JSON parse error:"
<< std::endl;
FAIL() << "cper_example_section_ir_test: JSON parse error:";
- fclose(record);
json_object_put(ir);
return;
}
- fclose(record);
//Open json example file
nlohmann::json jGolden = loadJson(json.string().c_str());