aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/fixtures.hpp
blob: 60047df816050d6c682392e92c9daa29f95ed429 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "PDB repository" project.
// For details, see https://github.com/egor-tensin/pdb-repo.
// Distributed under the MIT License.

#pragma once

#include "paths.hpp"

#include <pdb/all.hpp>
#include <test_lib.hpp>

#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>

#include <iterator>
#include <string>
#include <unordered_set>
#include <utility>

template <typename T>
using Set = std::unordered_set<T>;

template <typename T>
Set<T> join(Set<T>&& xs, Set<T>&& ys) {
    xs.insert(std::make_move_iterator(ys.begin()), std::make_move_iterator(ys.end()));
    return std::move(xs);
}

class DbgHelp {
public:
    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"};
        return name;
    }

    typedef Set<std::string> SymbolList;
    typedef Set<pdb::Address> AddressList;

    static AddressList expected_function_addresses() {
        return cast({reinterpret_cast<void*>(&test_ns::foo),
                     reinterpret_cast<void*>(&test_ns::bar),
                     reinterpret_cast<void*>(&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) {
            qualified.emplace(get_namespace() + "::" + std::move(name));
        }
        return qualified;
    }

    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_module_pdb_path() {
        // That's pretty ad-hoc, but seems to work; based on
        // https://www.boost.org/doc/libs/1_70_0/libs/test/doc/html/boost_test/runtime_config/custom_command_line_arguments.html
        BOOST_TEST_REQUIRE(boost::unit_test::framework::master_test_suite().argc == 3);
        BOOST_TEST_REQUIRE(boost::unit_test::framework::master_test_suite().argv[1] ==
                           "--test_lib_pdb");
        return boost::unit_test::framework::master_test_suite().argv[2];
    }
};

class CurrentProcess : public DbgHelp {
public:
    CurrentProcess() : DbgHelp{init_dbghelp(true)} {}
};