diff options
author | Simon Warta <[email protected]> | 2015-07-16 17:19:32 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-07-16 19:01:37 +0200 |
commit | 2ea885ffe9f44fada457b9cc8e169418c57f1bdb (patch) | |
tree | 73897ab4cca1050e9c4bb7f57a3ef3508514c6f6 | |
parent | acac09fc411eeb8d52f4565ba50c057298552679 (diff) |
Refactor internal/filesystem.h
Closes #198
-rw-r--r-- | src/lib/cert/x509/certstor.cpp | 4 | ||||
-rw-r--r-- | src/lib/utils/exceptn.h | 8 | ||||
-rw-r--r-- | src/lib/utils/filesystem.cpp (renamed from src/lib/utils/fs.cpp) | 60 | ||||
-rw-r--r-- | src/lib/utils/filesystem.h (renamed from src/lib/utils/fs.h) | 7 | ||||
-rw-r--r-- | src/lib/utils/info.txt | 24 | ||||
-rw-r--r-- | src/tests/nist_x509.cpp | 24 | ||||
-rw-r--r-- | src/tests/tests.cpp | 21 |
7 files changed, 92 insertions, 56 deletions
diff --git a/src/lib/cert/x509/certstor.cpp b/src/lib/cert/x509/certstor.cpp index d075fe706..e3498f602 100644 --- a/src/lib/cert/x509/certstor.cpp +++ b/src/lib/cert/x509/certstor.cpp @@ -6,7 +6,7 @@ */ #include <botan/certstor.h> -#include <botan/fs.h> +#include <botan/internal/filesystem.h> namespace Botan { @@ -118,7 +118,7 @@ Certificate_Store_In_Memory::Certificate_Store_In_Memory(const std::string& dir) if(dir == "") return; - std::vector<std::string> maybe_certs = list_all_readable_files_in_or_under(dir); + std::vector<std::string> maybe_certs = get_files_recursive(dir); for(auto&& cert_file : maybe_certs) { try diff --git a/src/lib/utils/exceptn.h b/src/lib/utils/exceptn.h index 7a10319c5..06db697bf 100644 --- a/src/lib/utils/exceptn.h +++ b/src/lib/utils/exceptn.h @@ -158,6 +158,14 @@ struct BOTAN_DLL Stream_IO_Error : public Exception }; /** +* No_Filesystem_Access Exception +*/ +struct BOTAN_DLL No_Filesystem_Access : public Exception + { + No_Filesystem_Access() : Exception("No filesystem access enabled.") {} + }; + +/** * Self Test Failure Exception */ struct BOTAN_DLL Self_Test_Failure : public Internal_Error diff --git a/src/lib/utils/fs.cpp b/src/lib/utils/filesystem.cpp index b1ada17db..0a31dec7a 100644 --- a/src/lib/utils/fs.cpp +++ b/src/lib/utils/filesystem.cpp @@ -4,40 +4,46 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/fs.h> +#include <botan/exceptn.h> +#include <botan/internal/filesystem.h> #include <algorithm> -#include <deque> -#include <functional> -#include <memory> #if defined(BOTAN_HAS_BOOST_FILESYSTEM) #include <boost/filesystem.hpp> - #elif defined(BOTAN_TARGET_OS_HAS_READDIR) #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> + #include <deque> + #include <memory> + #include <functional> #endif namespace Botan { -std::vector<std::string> -list_all_readable_files_in_or_under(const std::string& dir_path) - { - std::vector<std::string> paths; +namespace { #if defined(BOTAN_HAS_BOOST_FILESYSTEM) +std::vector<std::string> impl_boost_filesystem(const std::string& dir_path) +{ namespace fs = boost::filesystem; - fs::recursive_directory_iterator end; - for(fs::recursive_directory_iterator dir(dir_path); dir != end; ++dir) + std::vector<std::string> out; + + for(fs::recursive_directory_iterator dir(dir_path), end; dir != end; ++dir) { if(fs::is_regular_file(dir->path())) - paths.push_back(dir->path().string()); + { + out.push_back(dir->path().string()); + } } + return out; +} #elif defined(BOTAN_TARGET_OS_HAS_READDIR) - +std::vector<std::string> impl_readdir(const std::string& dir_path) + { + std::vector<std::string> out; std::deque<std::string> dir_list; dir_list.push_back(dir_path); @@ -65,22 +71,32 @@ list_all_readable_files_in_or_under(const std::string& dir_path) if(S_ISDIR(stat_buf.st_mode)) dir_list.push_back(full_path); else if(S_ISREG(stat_buf.st_mode)) - paths.push_back(full_path); + out.push_back(full_path); } } } -#else -#if defined(_MSC_VER) - #pragma message ( "No filesystem access enabled" ) -#else - #warning "No filesystem access enabled" + + return out; + } #endif + +} + +std::vector<std::string> get_files_recursive(const std::string& dir) + { + std::vector<std::string> files; + +#if defined(BOTAN_HAS_BOOST_FILESYSTEM) + files = impl_boost_filesystem(dir); +#elif defined(BOTAN_TARGET_OS_HAS_READDIR) + files = impl_readdir(dir); +#else + throw No_Filesystem_Access(); #endif - std::sort(paths.begin(), paths.end()); + std::sort(files.begin(), files.end()); - return paths; + return files; } } - diff --git a/src/lib/utils/fs.h b/src/lib/utils/filesystem.h index 25ec5ecd1..6f9b5196e 100644 --- a/src/lib/utils/fs.h +++ b/src/lib/utils/filesystem.h @@ -4,8 +4,8 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#ifndef BOTAN_UTIL_FS_H__ -#define BOTAN_UTIL_FS_H__ +#ifndef BOTAN_UTIL_FILESYSTEM_H__ +#define BOTAN_UTIL_FILESYSTEM_H__ #include <botan/types.h> #include <vector> @@ -13,8 +13,7 @@ namespace Botan { -BOTAN_DLL std::vector<std::string> -list_all_readable_files_in_or_under(const std::string& dir); +BOTAN_DLL std::vector<std::string> get_files_recursive(const std::string& dir); } diff --git a/src/lib/utils/info.txt b/src/lib/utils/info.txt index 90f45aa3f..b8c7b85d2 100644 --- a/src/lib/utils/info.txt +++ b/src/lib/utils/info.txt @@ -2,17 +2,6 @@ define UTIL_FUNCTIONS 20140123 load_on always -<header:internal> -bit_ops.h -donna128.h -prefetch.h -rounding.h -semaphore.h -stl_util.h -ta_utils.h -xor_buf.h -</header:internal> - <header:public> assert.h bswap.h @@ -21,7 +10,6 @@ charset.h cpuid.h database.h exceptn.h -fs.h get_byte.h loadstor.h mem_ops.h @@ -31,3 +19,15 @@ rotate.h types.h version.h </header:public> + +<header:internal> +bit_ops.h +donna128.h +filesystem.h +prefetch.h +rounding.h +semaphore.h +stl_util.h +ta_utils.h +xor_buf.h +</header:internal> diff --git a/src/tests/nist_x509.cpp b/src/tests/nist_x509.cpp index 734f2b355..25578d239 100644 --- a/src/tests/nist_x509.cpp +++ b/src/tests/nist_x509.cpp @@ -18,7 +18,7 @@ #if defined(BOTAN_HAS_X509_CERTIFICATES) #include <botan/x509path.h> -#include <botan/fs.h> +#include <botan/internal/filesystem.h> #include <algorithm> #include <iostream> @@ -37,10 +37,14 @@ size_t test_nist_x509() const std::string root_test_dir = "src/tests/data/nist_x509/"; const size_t total_tests = 76; - if(list_all_readable_files_in_or_under(root_test_dir).empty()) + try { - std::cout << "No FS access, skipping NIST X.509 validation tests" << std::endl; - test_report("NIST X.509 path validation", 0, 0); + // Do nothing, just test filesystem access + get_files_recursive(root_test_dir); + } + catch(No_Filesystem_Access) + { + std::cout << "Warning: No filesystem access, skipping NIST X.509 validation tests" << std::endl; return 0; } @@ -57,17 +61,17 @@ size_t test_nist_x509() for(size_t test_no = 1; test_no <= total_tests; ++test_no) { const std::string test_dir = root_test_dir + "/test" + (test_no <= 9 ? "0" : "") + std::to_string(test_no); - const std::vector<std::string> all_files = list_all_readable_files_in_or_under(test_dir); + + const std::vector<std::string> all_files = get_files_recursive(test_dir); + if (all_files.empty()) + std::cout << "Warning: No test files found in '" << test_dir << "'" << std::endl; std::vector<std::string> certs, crls; std::string root_cert, to_verify; - for(size_t k = 0; k != all_files.size(); k++) + for(const auto ¤t : all_files) { - const std::string current = all_files[k]; - - if(current.find("int") != std::string::npos && - current.find(".crt") != std::string::npos) + if(current.find("int") != std::string::npos && current.find(".crt") != std::string::npos) certs.push_back(current); else if(current.find("root.crt") != std::string::npos) root_cert = current; diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index de83965f1..a0fd17879 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -8,7 +8,7 @@ #include <iostream> #include <fstream> #include <botan/auto_rng.h> -#include <botan/fs.h> +#include <botan/internal/filesystem.h> #define CATCH_CONFIG_RUNNER #define CATCH_CONFIG_CONSOLE_WIDTH 60 @@ -19,6 +19,8 @@ #include <botan/system_rng.h> #endif +using namespace Botan; + Botan::RandomNumberGenerator& test_rng() { #if defined(BOTAN_HAS_SYSTEM_RNG) @@ -33,14 +35,21 @@ size_t run_tests_in_dir(const std::string& dir, std::function<size_t (const std: { size_t fails = 0; - auto files = Botan::list_all_readable_files_in_or_under(dir); - if (files.empty()) + try + { + auto files = get_files_recursive(dir); + + if (files.empty()) + std::cout << "Warning: No test files found in '" << dir << "'" << std::endl; + + for(const auto file: files) + fails += fn(file); + } + catch(No_Filesystem_Access) { - std::cout << "Warning: No test files found in '" << dir << "'" << std::endl; + std::cout << "Warning: No filesystem access available to read test files in '" << dir << "'" << std::endl; } - for(const auto file: files) - fails += fn(file); return fails; } |