21
21
#include <gtest/gtest.h>
23
23
#include <boost/exception/diagnostic_information.hpp>
25
24
#include <stdexcept>
26
#include "mir_test_framework/executable_path.h"
28
namespace mtf = mir_test_framework;
32
35
HasSubstring(char const* substring) : substring(substring) {}
36
HasSubstring(std::string const& substring) : substring{substring.c_str()} {}
34
38
friend::testing::AssertionResult operator,(std::string const& target, HasSubstring const& match)
48
52
HasSubstring& operator=(HasSubstring const&) = delete;
51
#define EXPECT_THAT(target, condition) EXPECT_TRUE((target, condition))
53
char const* const nonexistent_library = "nonexistent_library";
54
char const* const existing_library = "libmirplatformgraphics.so";
55
char const* const nonexistent_function = "nonexistent_library";
56
char const* const existing_function = "create_platform";
55
#define MIR_EXPECT_THAT(target, condition) EXPECT_TRUE((target, condition))
57
class SharedLibrary : public testing::Test
61
: nonexistent_library{"imma_totally_not_a_library"},
62
existing_library{mtf::library_path() + "/" MIR_CLIENT_DRIVER_BINARY},
63
nonexistent_function{"yo_dawg"},
64
existing_function{"create_client_platform_factory"},
65
existent_version{"MIR_CLIENTPLATFORM_1"},
66
nonexistent_version{"GOATS_ON_THE_GREEN"}
70
std::string const nonexistent_library;
71
std::string const existing_library;
72
std::string const nonexistent_function;
73
std::string const existing_function;
74
std::string const existent_version;
75
std::string const nonexistent_version;
59
TEST(SharedLibrary, load_nonexistent_library_fails)
80
TEST_F(SharedLibrary, load_nonexistent_library_fails)
61
82
EXPECT_THROW({ mir::SharedLibrary nonexistent(nonexistent_library); }, std::runtime_error);
64
TEST(SharedLibrary, load_nonexistent_library_fails_with_useful_info)
85
TEST_F(SharedLibrary, load_nonexistent_library_fails_with_useful_info)
72
93
auto info = boost::diagnostic_information(error);
74
EXPECT_THAT(info, HasSubstring("cannot open shared object")) << "What went wrong";
75
EXPECT_THAT(info, HasSubstring(nonexistent_library)) << "Name of library";
95
MIR_EXPECT_THAT(info, HasSubstring("cannot open shared object")) << "What went wrong";
96
MIR_EXPECT_THAT(info, HasSubstring(nonexistent_library)) << "Name of library";
79
TEST(SharedLibrary, load_valid_library_works)
100
TEST_F(SharedLibrary, load_valid_library_works)
81
102
mir::SharedLibrary existing(existing_library);
84
TEST(SharedLibrary, load_nonexistent_function_fails)
105
TEST_F(SharedLibrary, load_nonexistent_function_fails)
86
107
mir::SharedLibrary existing(existing_library);
88
109
EXPECT_THROW({ existing.load_function<void(*)()>(nonexistent_function); }, std::runtime_error);
91
TEST(SharedLibrary, load_nonexistent_function_fails_with_useful_info)
112
TEST_F(SharedLibrary, load_nonexistent_function_fails_with_useful_info)
93
114
mir::SharedLibrary existing(existing_library);
101
122
auto info = boost::diagnostic_information(error);
103
EXPECT_THAT(info, HasSubstring("undefined symbol")) << "What went wrong";
104
EXPECT_THAT(info, HasSubstring(existing_library)) << "Name of library";
105
EXPECT_THAT(info, HasSubstring(nonexistent_function)) << "Name of function";
124
MIR_EXPECT_THAT(info, HasSubstring("undefined symbol")) << "What went wrong";
125
MIR_EXPECT_THAT(info, HasSubstring(existing_library)) << "Name of library";
126
MIR_EXPECT_THAT(info, HasSubstring(nonexistent_function)) << "Name of function";
109
TEST(SharedLibrary, load_valid_function_works)
130
TEST_F(SharedLibrary, load_valid_function_works)
111
132
mir::SharedLibrary existing(existing_library);
112
133
existing.load_function<void(*)()>(existing_function);
136
TEST_F(SharedLibrary, load_valid_versioned_function_works)
138
mir::SharedLibrary existing{existing_library};
139
existing.load_function<void(*)()>(existing_function, existent_version);
142
TEST_F(SharedLibrary, load_invalid_versioned_function_fails_with_appropriate_error)
144
mir::SharedLibrary existing{existing_library};
148
existing.load_function<void(*)()>(existing_function, nonexistent_version);
150
catch (std::exception const& error)
152
auto info = boost::diagnostic_information(error);
154
MIR_EXPECT_THAT(info, HasSubstring("undefined symbol")) << "What went wrong";
155
MIR_EXPECT_THAT(info, HasSubstring(nonexistent_version)) << "Version info";
156
MIR_EXPECT_THAT(info, HasSubstring(existing_library)) << "Name of library";
157
MIR_EXPECT_THAT(info, HasSubstring(existing_function)) << "Name of function";