diff options
author | Jack Lloyd <[email protected]> | 2018-11-17 16:23:51 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-11-23 11:15:25 -0500 |
commit | b909778857b3e0b7eb86ac26c818e5f25baaddbd (patch) | |
tree | f8a5c9cbec26310bbfc9077563892b04db158a48 /src/tests | |
parent | c20a428ca2f7c1ef96e642f55bb898010444c499 (diff) |
Make exceptions easier to translate to error codes
Avoid throwing base Botan::Exception type, as it is difficult to
determine what the error is in that case.
Add Exception::error_code and Exception::error_type which allows
(for error code) more information about the error and (for error type)
allows knowing the error type without requiring a sequence of catches.
See GH #1742
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/main.cpp | 2 | ||||
-rw-r--r-- | src/tests/test_ffi.cpp | 4 | ||||
-rw-r--r-- | src/tests/test_pkcs11_low_level.cpp | 12 | ||||
-rw-r--r-- | src/tests/test_rng.cpp | 2 | ||||
-rw-r--r-- | src/tests/test_rng.h | 4 | ||||
-rw-r--r-- | src/tests/test_tests.cpp | 2 | ||||
-rw-r--r-- | src/tests/test_tss.cpp | 8 | ||||
-rw-r--r-- | src/tests/test_x509_path.cpp | 22 | ||||
-rw-r--r-- | src/tests/tests.h | 1 |
9 files changed, 28 insertions, 29 deletions
diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 6777f85f4..180daf1d4 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -102,7 +102,7 @@ int main(int argc, char* argv[]) #if defined(BOTAN_HAS_OPENSSL) if(opts.provider().empty() || opts.provider() == "openssl") { - ::ERR_free_strings(); + ERR_free_strings(); ::ERR_remove_thread_state(nullptr); } #endif diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index aca2dbfb9..50c85eff6 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -214,12 +214,12 @@ class FFI_Unit_Tests final : public Test TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size())); TEST_FFI_OK(botan_rng_reseed, (rng, 256)); - TEST_FFI_RC(-20, botan_rng_reseed_from_rng, (rng, null_rng, 256)); + TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_reseed_from_rng, (rng, null_rng, 256)); if(rdrand_rng) { TEST_FFI_OK(botan_rng_reseed_from_rng, (rng, rdrand_rng, 256)); } - TEST_FFI_RC(-20, botan_rng_get, (null_rng, outbuf.data(), outbuf.size())); + TEST_FFI_RC(BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, botan_rng_get, (null_rng, outbuf.data(), outbuf.size())); TEST_FFI_OK(botan_rng_destroy, (rng)); } diff --git a/src/tests/test_pkcs11_low_level.cpp b/src/tests/test_pkcs11_low_level.cpp index ffe9dfc20..6dbd6ac0c 100644 --- a/src/tests/test_pkcs11_low_level.cpp +++ b/src/tests/test_pkcs11_low_level.cpp @@ -74,7 +74,7 @@ class RAII_LowLevel if(slots.empty()) { - throw Exception("No slot with attached token found"); + throw Test_Error("No slot with attached token found"); } return slots; @@ -100,7 +100,7 @@ class RAII_LowLevel { if(!m_is_session_open) { - throw Exception("no open session"); + throw Test_Error("no open session"); } return m_session_handle; } @@ -109,7 +109,7 @@ class RAII_LowLevel { if(!m_is_session_open) { - throw Exception("no open session"); + throw Test_Error("no open session"); } m_low_level.get()->C_CloseSession(m_session_handle); @@ -120,12 +120,12 @@ class RAII_LowLevel { if(!m_is_session_open) { - throw Exception("no open session"); + throw Test_Error("no open session"); } if(m_is_logged_in) { - throw Exception("Already logged in"); + throw Test_Error("Already logged in"); } m_low_level.get()->C_Login(m_session_handle, user_type, pin); @@ -136,7 +136,7 @@ class RAII_LowLevel { if(!m_is_logged_in) { - throw Exception("Not logged in"); + throw Test_Error("Not logged in"); } m_low_level.get()->C_Logout(m_session_handle); diff --git a/src/tests/test_rng.cpp b/src/tests/test_rng.cpp index 0b316335e..a7510b650 100644 --- a/src/tests/test_rng.cpp +++ b/src/tests/test_rng.cpp @@ -150,7 +150,7 @@ class Stateful_RNG_Tests : public Test size_t poll(Botan::RandomNumberGenerator&) override { - throw Botan::Exception("polling not available"); + throw Botan::Not_Implemented("polling not available"); } }; diff --git a/src/tests/test_rng.h b/src/tests/test_rng.h index 822536971..01fe89c92 100644 --- a/src/tests/test_rng.h +++ b/src/tests/test_rng.h @@ -122,7 +122,7 @@ class Fixed_Output_Position_RNG final : public Fixed_Output_RNG void add_entropy(const uint8_t*, size_t) override { - throw Botan::Exception("add_entropy() not supported by this RNG, test bug?"); + throw Test_Error("add_entropy() not supported by this RNG, test bug?"); } std::string name() const override @@ -148,7 +148,7 @@ class SeedCapturing_RNG final : public Botan::RandomNumberGenerator public: void randomize(uint8_t[], size_t) override { - throw Botan::Exception("SeedCapturing_RNG has no output"); + throw Test_Error("SeedCapturing_RNG has no output"); } bool accepts_input() const override { return true; } diff --git a/src/tests/test_tests.cpp b/src/tests/test_tests.cpp index 49a05d6ab..a7324eef8 100644 --- a/src/tests/test_tests.cpp +++ b/src/tests/test_tests.cpp @@ -143,7 +143,7 @@ class Test_Tests final : public Test { Test::Result test_result(testcase_name); test_result.test_throws("test_throws", "expected msg", - []() { throw Botan::Exception("not the message"); }); + []() { throw std::runtime_error("not the message"); }); verify_failure("test_throws", result, test_result); } diff --git a/src/tests/test_tss.cpp b/src/tests/test_tss.cpp index 714f08df6..139e2087d 100644 --- a/src/tests/test_tss.cpp +++ b/src/tests/test_tss.cpp @@ -40,8 +40,8 @@ class TSS_Recovery_Tests final : public Text_Based_Test shares.push_back(Botan::RTSS_Share(v.data(), v.size())); } - auto reconstructed_secret = Botan::RTSS_Share::reconstruct(shares); - result.test_eq("Reconstructed secret correctly from all shares", reconstructed_secret, input); + auto reconstructed_secret_all = Botan::RTSS_Share::reconstruct(shares); + result.test_eq("Reconstructed secret correctly from all shares", reconstructed_secret_all, input); if(header == "Invalid") result.test_failure("Invalid shares should not result in recovery"); @@ -127,8 +127,8 @@ class TSS_Generation_Tests final : public Text_Based_Test result.test_eq("Expected share", shares[i].data(), expected_shares[i]); } - auto reconstructed_secret = Botan::RTSS_Share::reconstruct(shares); - result.test_eq("Reconstructed secret correctly from all shares", reconstructed_secret, input); + auto reconstructed_secret_all = Botan::RTSS_Share::reconstruct(shares); + result.test_eq("Reconstructed secret correctly from all shares", reconstructed_secret_all, input); if(N != M) { diff --git a/src/tests/test_x509_path.cpp b/src/tests/test_x509_path.cpp index 57f2d8602..e86c7b70f 100644 --- a/src/tests/test_x509_path.cpp +++ b/src/tests/test_x509_path.cpp @@ -634,19 +634,17 @@ std::vector<Test::Result> BSI_Path_Validation_Tests::run() /* Some certificates are rejected when executing the X509_Certificate constructor * by throwing a Decoding_Error exception. */ - catch(const Botan::Decoding_Error& d) + catch(const Botan::Exception& e) { - result.test_eq(test_name + " path validation result", d.what(), - expected_result); - } - catch(const Botan::X509_CRL::X509_CRL_Error& e) - { - result.test_eq(test_name + " path validation result", e.what(), - expected_result); - } - catch(const std::exception& e) - { - result.test_failure(test_name, e.what()); + if(e.error_type() == Botan::ErrorType::DecodingFailure) + { + result.test_eq(test_name + " path validation result", e.what(), + expected_result); + } + else + { + result.test_failure(test_name, e.what()); + } } result.end_timer(); diff --git a/src/tests/tests.h b/src/tests/tests.h index 7a0e4e54e..d21acf6f9 100644 --- a/src/tests/tests.h +++ b/src/tests/tests.h @@ -45,6 +45,7 @@ class Test_Error final : public Botan::Exception { public: explicit Test_Error(const std::string& what) : Exception("Test error", what) {} + Botan::ErrorType error_type() const noexcept override { return Botan::ErrorType::Unknown; } }; class Test_Options |