diff options
Diffstat (limited to 'test/unit_tests')
-rw-r--r-- | test/unit_tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | test/unit_tests/call_stack.cpp | 79 | ||||
-rw-r--r-- | test/unit_tests/dbghelp.cpp | 22 | ||||
-rw-r--r-- | test/unit_tests/fixtures.hpp | 82 |
4 files changed, 144 insertions, 41 deletions
diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt index 6b5eca6..1241061 100644 --- a/test/unit_tests/CMakeLists.txt +++ b/test/unit_tests/CMakeLists.txt @@ -1,6 +1,6 @@ find_package(Boost REQUIRED COMPONENTS filesystem unit_test_framework) -add_executable(unit_tests main.cpp dbghelp.cpp error.cpp) +add_executable(unit_tests main.cpp call_stack.cpp dbghelp.cpp error.cpp) target_link_libraries(unit_tests PRIVATE pdb_repo test_lib) target_link_libraries(unit_tests PRIVATE Boost::disable_autolinking Boost::filesystem Boost::nowide Boost::unit_test_framework) diff --git a/test/unit_tests/call_stack.cpp b/test/unit_tests/call_stack.cpp new file mode 100644 index 0000000..481ab6e --- /dev/null +++ b/test/unit_tests/call_stack.cpp @@ -0,0 +1,79 @@ +#include "fixtures.hpp" + +#include <pdb/all.hpp> +#include <test_lib.hpp> + +#include <boost/test/unit_test.hpp> + +#include <algorithm> +#include <sstream> +#include <string> +#include <vector> + +BOOST_FIXTURE_TEST_SUITE(call_stack_tests, CurrentProcess) + +BOOST_AUTO_TEST_CASE(call_stack) { + try { + test_ns::throw_call_stack(); + } catch (const pdb::CallStack& call_stack) { + // First, check that the call stack have been caught. + BOOST_TEST(true, "Caught the call stack"); + + // Debug output: + std::vector<std::string> pretty; + pretty.reserve(call_stack.length); + + BOOST_TEST_MESSAGE("Call stack:"); + for (const auto& addr : call_stack) { + pretty.emplace_back(call_stack.pretty_print_address(dbghelp, addr)); + BOOST_TEST_MESSAGE('\t' << pdb::format_address(addr) << ' ' << pretty.back()); + } + + // Second, resolve the symbols: + std::vector<pdb::SymbolInfo> symbols; + symbols.reserve(call_stack.length); + + BOOST_TEST_MESSAGE("Resolved symbols:"); + for (const auto& addr : call_stack) { + symbols.emplace_back(dbghelp.resolve_symbol(addr)); + const auto& symbol = symbols.back(); + BOOST_TEST_MESSAGE('\t' << pdb::format_address(symbol.get_offline_address()) << ' ' + << symbol.get_name()); + } + + { + // Third, check that the expected function frames are in the call stack. + const auto expected = expected_function_addresses(); + + const auto check = [&](pdb::Address addr) { + for (const auto& symbol : symbols) + if (symbol.get_offline_address() == addr) + return true; + return false; + }; + for (const auto& addr : expected) { + BOOST_TEST(check(addr), "Function frame captured: " << pdb::format_address(addr)); + } + } + + { + // Fourth, check that the expected function names are in the call stack. + const auto expected = expected_functions_full(); + + const auto check = [&](const std::string& name) { + for (const auto& actual : pretty) + if (actual.find(name) != std::string::npos) + return true; + return false; + }; + for (const auto& name : expected) { + BOOST_TEST(check(name), "Function frame captured: " << name); + } + } + return; + } + // We must catch the call stack. + BOOST_TEST(false, "Didn't catch the call stack"); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/unit_tests/dbghelp.cpp b/test/unit_tests/dbghelp.cpp index 4346bf7..bd2d300 100644 --- a/test/unit_tests/dbghelp.cpp +++ b/test/unit_tests/dbghelp.cpp @@ -1,8 +1,6 @@ #include "fixtures.hpp" -#include "paths.hpp" #include <pdb/all.hpp> -#include <test_lib.hpp> #include <boost/test/unit_test.hpp> @@ -10,15 +8,7 @@ #include <string> #include <vector> -namespace { - -void throw_call_stack() { - throw pdb::CallStack::capture(); -} - -} // namespace - -BOOST_FIXTURE_TEST_SUITE(dbghelp_tests, DbgHelpWithSymbols) +BOOST_FIXTURE_TEST_SUITE(enum_symbols_tests, PostMortem) BOOST_AUTO_TEST_CASE(enum_symbols) { // Symbols can be enumerated, and all the expected symbols are there. @@ -44,14 +34,4 @@ BOOST_AUTO_TEST_CASE(enum_symbols) { } } -BOOST_AUTO_TEST_CASE(call_stack) { - try { - test_ns::foo(&throw_call_stack); - } catch (const pdb::CallStack& call_stack) { - BOOST_TEST(true, "Caught the call stack"); - return; - } - BOOST_TEST(false, "Didn't catch the call stack"); -} - BOOST_AUTO_TEST_SUITE_END() diff --git a/test/unit_tests/fixtures.hpp b/test/unit_tests/fixtures.hpp index eb7dcc0..fadd393 100644 --- a/test/unit_tests/fixtures.hpp +++ b/test/unit_tests/fixtures.hpp @@ -3,6 +3,7 @@ #include "paths.hpp" #include <pdb/all.hpp> +#include <test_lib.hpp> #include <boost/filesystem.hpp> #include <boost/test/unit_test.hpp> @@ -12,19 +13,6 @@ #include <unordered_set> #include <utility> -class DbgHelp { -public: - DbgHelp() : dbghelp{pdb::DbgHelp::post_mortem()} { BOOST_TEST_MESSAGE("Initializing DbgHelp"); } - - ~DbgHelp() { BOOST_TEST_MESSAGE("Cleaning up DbgHelp"); } - - const pdb::DbgHelp dbghelp; - -private: - DbgHelp(const DbgHelp&) = delete; - DbgHelp& operator=(const DbgHelp&) = delete; -}; - template <typename T> using Set = std::unordered_set<T>; @@ -34,9 +22,18 @@ Set<T> join(Set<T>&& xs, Set<T>&& ys) { return std::move(xs); } -class DbgHelpWithSymbols : public DbgHelp { +class DbgHelp { public: - DbgHelpWithSymbols() { load_test_lib_pdb(); } + DbgHelp(pdb::DbgHelp&& dbghelp) : dbghelp{std::move(dbghelp)} {} + + ~DbgHelp() { BOOST_TEST_MESSAGE("Cleaning up DbgHelp"); } + + const pdb::DbgHelp dbghelp; + + static const std::string& get_module_name() { + static const std::string name{"test_lib"}; + return name; + } static const std::string& get_namespace() { static const std::string name{"test_ns"}; @@ -44,16 +41,41 @@ public: } typedef Set<std::string> SymbolList; + typedef Set<pdb::Address> AddressList; + + static AddressList expected_function_addresses() { + return cast({&test_ns::foo, &test_ns::bar, &test_ns::baz}); + } static SymbolList expected_functions() { return make_qualified({"foo", "bar", "baz"}); } + static SymbolList expected_functions_full() { return add_module(expected_functions()); } + static SymbolList expected_variables() { return make_qualified({"var"}); } static SymbolList expected_symbols() { return join(expected_functions(), expected_variables()); } +protected: + static pdb::DbgHelp init_dbghelp(bool current_process) { + BOOST_TEST_MESSAGE("Initializing DbgHelp"); + if (current_process) { + return pdb::DbgHelp::current_process(); + } else { + return pdb::DbgHelp::post_mortem(); + } + } + private: + static AddressList cast(Set<void*>&& fs) { + AddressList addresses; + for (auto&& f : fs) { + addresses.emplace(reinterpret_cast<pdb::Address>(f)); + } + return addresses; + } + static SymbolList make_qualified(SymbolList&& plain) { SymbolList qualified; for (auto&& name : plain) { @@ -62,13 +84,35 @@ private: return qualified; } - void load_test_lib_pdb() { - const auto pdb_path = get_test_lib_pdb_path().string(); + static SymbolList add_module(SymbolList&& plain) { + SymbolList full; + for (auto&& name : plain) { + full.emplace(get_module_name() + "!" + std::move(name)); + } + return full; + } + + DbgHelp(const DbgHelp&) = delete; + DbgHelp& operator=(const DbgHelp&) = delete; +}; + +class PostMortem : public DbgHelp { +public: + PostMortem() : DbgHelp{init_dbghelp(false)} { load_module_pdb(); } + +private: + void load_module_pdb() { + const auto pdb_path = get_module_pdb_path().string(); BOOST_TEST_MESSAGE("Loading PDB: " << pdb_path); dbghelp.load_pdb(pdb_path); } - static boost::filesystem::path get_test_lib_pdb_path() { - return Paths::get().exe_dir / "test_lib.pdb"; + static boost::filesystem::path get_module_pdb_path() { + return Paths::get().exe_dir / (get_module_name() + ".pdb"); } }; + +class CurrentProcess : public DbgHelp { +public: + CurrentProcess() : DbgHelp{init_dbghelp(true)} {} +}; |