diff options
author | Jack Lloyd <[email protected]> | 2017-12-29 11:52:18 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-12-29 11:52:18 -0500 |
commit | 33eead87d274cc35842389bbe591585a2cb675a3 (patch) | |
tree | 91bf2a13841a5a324832d180ebd97173bf4953ba /src | |
parent | 994db72e812421cd1c81f93ce43625d3dcd551e2 (diff) |
Move utils for reading test data files up to Test:: from OCSP
Diffstat (limited to 'src')
-rw-r--r-- | src/tests/test_ocsp.cpp | 36 | ||||
-rw-r--r-- | src/tests/tests.cpp | 41 | ||||
-rw-r--r-- | src/tests/tests.h | 3 |
3 files changed, 48 insertions, 32 deletions
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<uint8_t> 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<uint8_t> contents; - - while(file.good()) - { - std::vector<uint8_t> buf(4096); - file.read(reinterpret_cast<char*>(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<const Botan::X509_Certificate> load_test_X509_cert(const std::string& path) { return std::make_shared<const Botan::X509_Certificate>(Test::data_file(path)); @@ -56,7 +28,7 @@ class OCSP_Tests final : public Test std::shared_ptr<const Botan::OCSP::Response> load_test_OCSP_resp(const std::string& path) { - return std::make_shared<const Botan::OCSP::Response>(slurp_data_file(path)); + return std::make_shared<const Botan::OCSP::Response>(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<char>(file)), + std::istreambuf_iterator<char>()); + } + +std::vector<uint8_t> 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<uint8_t> contents; + + while(file.good()) + { + std::vector<uint8_t> buf(4096); + file.read(reinterpret_cast<char*>(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<Botan::RandomNumberGenerator> 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<uint8_t> read_binary_data_file(const std::string& path); + static Botan::RandomNumberGenerator& rng(); static std::string random_password(); static uint64_t timestamp(); // nanoseconds arbitrary epoch |