File: shared_library_test.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (135 lines) | stat: -rw-r--r-- 4,222 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mir/shared_library.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <boost/exception/diagnostic_information.hpp>
#include <stdexcept>

#include "mir_test_framework/executable_path.h"

namespace mtf = mir_test_framework;
using namespace testing;

namespace
{
class SharedLibrary : public testing::Test
{
public:
    SharedLibrary()
        : nonexistent_library{"imma_totally_not_a_library"},
          existing_library{mtf::library_path() + "/libmircore.so"},
          nonexistent_function{"yo_dawg"},
          existing_function{"_ZN3mir17fatal_error_abortEPKcz"},
          existent_version{"MIR_CORE_2.9"},
          nonexistent_version{"GOATS_ON_THE_GREEN"}
    {
    }

    std::string const nonexistent_library;
    std::string const existing_library;
    std::string const nonexistent_function;
    std::string const existing_function;
    std::string const existent_version;
    std::string const nonexistent_version;
};
}

TEST_F(SharedLibrary, load_nonexistent_library_fails)
{
    EXPECT_THROW({ mir::SharedLibrary nonexistent(nonexistent_library); }, std::runtime_error);
}

TEST_F(SharedLibrary, load_nonexistent_library_fails_with_useful_info)
{
    try
    {
        mir::SharedLibrary nonexistent(nonexistent_library);
    }
    catch (std::exception const& error)
    {
        auto info = boost::diagnostic_information(error);

        EXPECT_THAT(info, AnyOf(HasSubstr("cannot open shared object"),
                                HasSubstr("Error loading shared library"))) << "What went wrong";
        EXPECT_THAT(info, HasSubstr(nonexistent_library)) << "Name of library";
    }
}

TEST_F(SharedLibrary, load_valid_library_works)
{
    mir::SharedLibrary existing(existing_library);
}

TEST_F(SharedLibrary, load_nonexistent_function_fails)
{
    mir::SharedLibrary existing(existing_library);

    EXPECT_THROW({ existing.load_function<void(*)()>(nonexistent_function); }, std::runtime_error);
}

TEST_F(SharedLibrary, load_nonexistent_function_fails_with_useful_info)
{
    mir::SharedLibrary existing(existing_library);

    try
    {
        existing.load_function<void(*)()>(nonexistent_function);
    }
    catch (std::exception const& error)
    {
        auto info = boost::diagnostic_information(error);

        EXPECT_THAT(info, AnyOf(HasSubstr("undefined symbol"),
                                HasSubstr("Symbol not found"))) << "What went wrong";
        EXPECT_THAT(info, HasSubstr(nonexistent_function)) << "Name of function";
    }
}

TEST_F(SharedLibrary, load_valid_function_works)
{
    mir::SharedLibrary existing(existing_library);
    existing.load_function<void(*)()>(existing_function);
}

TEST_F(SharedLibrary, load_valid_versioned_function_works)
{
    mir::SharedLibrary existing{existing_library};
    existing.load_function<void(*)()>(existing_function, existent_version);
}

TEST_F(SharedLibrary, load_invalid_versioned_function_fails_with_appropriate_error)
{
    mir::SharedLibrary existing{existing_library};

    try
    {
        existing.load_function<void(*)()>(existing_function, nonexistent_version);
    }
    catch (std::exception const& error)
    {
        auto info = boost::diagnostic_information(error);

        EXPECT_THAT(info, AnyOf(HasSubstr("undefined symbol"),
                                HasSubstr("Symbol not found"))) << "What went wrong";
        EXPECT_THAT(info, HasSubstr(nonexistent_version)) << "Version info";
        EXPECT_THAT(info, HasSubstr(existing_library)) << "Name of library";
        EXPECT_THAT(info, HasSubstr(existing_function)) << "Name of function";
    }
}