aboutsummaryrefslogtreecommitdiff
path: root/cc/util
diff options
context:
space:
mode:
authorambrosin <ambrosin@google.com>2023-01-13 02:25:34 -0800
committerCopybara-Service <copybara-worker@google.com>2023-01-13 02:27:06 -0800
commit72208aa4b53d1347af42ea91884d8d3d81d85baf (patch)
treecd2101499f81c1d6df4e8edb254603eacbdb6012 /cc/util
parenta4bf1755ff663bb07c5a025af2ec15c130c43a6d (diff)
downloadtink-72208aa4b53d1347af42ea91884d8d3d81d85baf.tar.gz
Simplify ReadTestFile in cc/util/test_util.(h|cc)
Use std libraries to read the file content. This should be a portable way to read test files. PiperOrigin-RevId: 501792823
Diffstat (limited to 'cc/util')
-rw-r--r--cc/util/test_util.cc35
-rw-r--r--cc/util/test_util.h4
2 files changed, 13 insertions, 26 deletions
diff --git a/cc/util/test_util.cc b/cc/util/test_util.cc
index 3e449c100..9802c9df8 100644
--- a/cc/util/test_util.cc
+++ b/cc/util/test_util.cc
@@ -24,9 +24,12 @@
#include <cmath>
#include <cstdint>
#include <cstdlib>
+#include <fstream>
+#include <ios>
#include <iostream>
#include <memory>
#include <ostream>
+#include <sstream>
#include <string>
#include <vector>
@@ -120,32 +123,16 @@ int GetTestFileDescriptor(absl::string_view filename) {
return fd;
}
-std::string ReadTestFile(std::string filename) {
- std::string full_filename =
- absl::StrCat(crypto::tink::test::TmpDir(), "/", filename);
- int fd = open(full_filename.c_str(), O_RDONLY);
- if (fd == -1) {
- std::clog << "Cannot open file " << full_filename
- << " error: " << errno << std::endl;
+std::string ReadTestFile(absl::string_view filename) {
+ std::string full_filename = absl::StrCat(test::TmpDir(), "/", filename);
+ std::ifstream input_stream(full_filename, std::ios::binary);
+ if (!input_stream) {
+ std::clog << "Cannot open file " << full_filename << std::endl;
exit(1);
}
- std::string contents;
- int buffer_size = 128 * 1024;
- auto buffer = absl::make_unique<uint8_t[]>(buffer_size);
- int read_result = read(fd, buffer.get(), buffer_size);
- while (read_result > 0) {
- std::clog << "Read " << read_result << " bytes" << std::endl;
- contents.append(reinterpret_cast<const char*>(buffer.get()), read_result);
- read_result = read(fd, buffer.get(), buffer_size);
- }
- if (read_result < 0) {
- std::clog << "Error reading file " << full_filename
- << " error: " << errno << std::endl;
- exit(1);
- }
- close(fd);
- std::clog << "Read in total " << contents.length() << " bytes" << std::endl;
- return contents;
+ std::stringstream buffer;
+ buffer << input_stream.rdbuf();
+ return buffer.str();
}
util::StatusOr<std::string> HexDecode(absl::string_view hex) {
diff --git a/cc/util/test_util.h b/cc/util/test_util.h
index b2ed2b088..5f97f5509 100644
--- a/cc/util/test_util.h
+++ b/cc/util/test_util.h
@@ -78,8 +78,8 @@ int GetTestFileDescriptor(absl::string_view filename,
// Creates a new test file with the specified 'filename', ready for writing.
int GetTestFileDescriptor(absl::string_view filename);
-// Reads the test file specified by 'filename', and returns its contents.
-std::string ReadTestFile(std::string filename);
+// Reads the test file specified by `filename`, and returns its contents.
+std::string ReadTestFile(absl::string_view filename);
// Converts a hexadecimal string into a string of bytes.
// Returns a status if the size of the input is odd or if the input contains