From 33eead87d274cc35842389bbe591585a2cb675a3 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 29 Dec 2017 11:52:18 -0500 Subject: Move utils for reading test data files up to Test:: from OCSP --- src/tests/test_ocsp.cpp | 36 ++++-------------------------------- src/tests/tests.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/tests/tests.h | 3 +++ 3 files changed, 48 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/tests/test_ocsp.cpp b/src/tests/test_ocsp.cpp index 2839d9ef5..6a872ae62 100644 --- a/src/tests/test_ocsp.cpp +++ b/src/tests/test_ocsp.cpp @@ -21,34 +21,6 @@ namespace Botan_Tests { class OCSP_Tests final : public Test { private: - std::vector slurp_data_file(const std::string& path) - { - const std::string fsname = Test::data_file(path); - std::ifstream file(fsname.c_str(), std::ios::binary); - if(!file.good()) - { - throw Test_Error("Error reading from " + fsname); - } - - std::vector contents; - - while(file.good()) - { - std::vector buf(4096); - file.read(reinterpret_cast(buf.data()), buf.size()); - size_t got = file.gcount(); - - if(got == 0 && file.eof()) - { - break; - } - - contents.insert(contents.end(), buf.data(), buf.data() + got); - } - - return contents; - } - std::shared_ptr load_test_X509_cert(const std::string& path) { return std::make_shared(Test::data_file(path)); @@ -56,7 +28,7 @@ class OCSP_Tests final : public Test std::shared_ptr load_test_OCSP_resp(const std::string& path) { - return std::make_shared(slurp_data_file(path)); + return std::make_shared(Test::read_binary_data_file(path)); } Test::Result test_response_parsing() @@ -75,7 +47,7 @@ class OCSP_Tests final : public Test { try { - Botan::OCSP::Response resp(slurp_data_file(ocsp_input_path)); + Botan::OCSP::Response resp(Test::read_binary_data_file(ocsp_input_path)); result.test_success("Parsed input " + ocsp_input_path); } catch(Botan::Exception& e) @@ -93,7 +65,7 @@ class OCSP_Tests final : public Test try { - Botan::OCSP::Response resp1(slurp_data_file("x509/ocsp/resp1.der")); + Botan::OCSP::Response resp1(Test::read_binary_data_file("x509/ocsp/resp1.der")); const auto &certs1 = resp1.certificates(); if(result.test_eq("Expected count of certificates", certs1.size(), 1)) { @@ -105,7 +77,7 @@ class OCSP_Tests final : public Test result.test_eq("CN matches expected", matches, true); } - Botan::OCSP::Response resp2(slurp_data_file("x509/ocsp/resp2.der")); + Botan::OCSP::Response resp2(Test::read_binary_data_file("x509/ocsp/resp2.der")); const auto &certs2 = resp2.certificates(); result.test_eq("Expect no certificates", certs2.size(), 0); } diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index bda3cd648..e4ed5e896 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -506,6 +506,47 @@ Test* Test::get_test(const std::string& test_name) return nullptr; } +std::string Test::read_data_file(const std::string& path) + { + const std::string fsname = Test::data_file(path); + std::ifstream file(fsname.c_str()); + if(!file.good()) + { + throw Test_Error("Error reading from " + fsname); + } + + return std::string((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + } + +std::vector Test::read_binary_data_file(const std::string& path) + { + const std::string fsname = Test::data_file(path); + std::ifstream file(fsname.c_str(), std::ios::binary); + if(!file.good()) + { + throw Test_Error("Error reading from " + fsname); + } + + std::vector contents; + + while(file.good()) + { + std::vector buf(4096); + file.read(reinterpret_cast(buf.data()), buf.size()); + size_t got = file.gcount(); + + if(got == 0 && file.eof()) + { + break; + } + + contents.insert(contents.end(), buf.data(), buf.data() + got); + } + + return contents; + } + // static member variables of Test std::unique_ptr Test::m_test_rng; std::string Test::m_data_dir; diff --git a/src/tests/tests.h b/src/tests/tests.h index 9ab8e43b8..193f7e06d 100644 --- a/src/tests/tests.h +++ b/src/tests/tests.h @@ -413,6 +413,9 @@ class Test static const std::string& data_dir(); + static std::string read_data_file(const std::string& path); + static std::vector read_binary_data_file(const std::string& path); + static Botan::RandomNumberGenerator& rng(); static std::string random_password(); static uint64_t timestamp(); // nanoseconds arbitrary epoch -- cgit v1.2.3