File: test_shared_library_prober.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 (246 lines) | stat: -rw-r--r-- 7,679 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * 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_prober.h"

#include "mir_test_framework/executable_path.h"

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <cstring>
#include <unordered_map>

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

namespace mtf = mir_test_framework;

namespace
{
class MockSharedLibraryProberReport : public mir::SharedLibraryProberReport
{
public:
    MOCK_METHOD1(probing_path, void(boost::filesystem::path const&));
    MOCK_METHOD2(probing_failed, void(boost::filesystem::path const&, std::exception const&));
    MOCK_METHOD1(loading_library, void(boost::filesystem::path const&));
    MOCK_METHOD2(loading_failed, void(boost::filesystem::path const&, std::exception const&));
};

class SharedLibraryProber : public testing::Test
{
public:
    SharedLibraryProber()
        : library_path{mtf::test_data_path() + "/shared-libraries"}
    {
        // Can't use std::string, as mkdtemp mutates its argument.
        auto tmp_name = std::unique_ptr<char[], std::function<void(char*)>>{strdup("/tmp/mir_empty_directory_XXXXXX"),
                                                                            [](char* data) {free(data);}};
        if (mkdtemp(tmp_name.get()) == NULL)
        {
            throw std::system_error{errno, std::system_category(), "Failed to create temporary directory"};
        }
        temporary_directory = std::string{tmp_name.get()};
    }

    ~SharedLibraryProber()
    {
        // Can't do anything useful in case of failure...
        rmdir(temporary_directory.c_str());
    }

    std::string const library_path;
    std::string temporary_directory;
    testing::NiceMock<MockSharedLibraryProberReport> null_report;
};

}

TEST_F(SharedLibraryProber, returns_non_empty_list_for_path_containing_libraries)
{
    auto libraries = mir::libraries_for_path(library_path, null_report);
    EXPECT_GE(libraries.size(), 1u);
}

TEST_F(SharedLibraryProber, raises_exception_for_nonexistent_path)
{
    EXPECT_THROW(mir::libraries_for_path("/a/path/that/certainly/doesnt/exist", null_report),
                 std::system_error);
}

TEST_F(SharedLibraryProber, exception_mentions_missing_path_name)
{
    bool thrown = false;

    try
    {
        mir::libraries_for_path("/panacea", null_report);
    }
    catch (std::system_error& err)
    {
        thrown = true;
        EXPECT_THAT(err.what(), testing::HasSubstr("/panacea"));
    }

    EXPECT_TRUE(thrown);
}

TEST_F(SharedLibraryProber, non_existent_path_raises_ENOENT_error)
{
    try
    {
        mir::libraries_for_path("/a/path/that/certainly/doesnt/exist", null_report);
    }
    catch (std::system_error &err)
    {
        EXPECT_EQ(err.code(), std::error_code(ENOENT, std::system_category()));
    }
}

TEST_F(SharedLibraryProber, path_with_no_shared_libraries_returns_empty_list)
{
    auto libraries = mir::libraries_for_path(temporary_directory.c_str(), null_report);
    EXPECT_EQ(0u, libraries.size());
}

TEST_F(SharedLibraryProber, logs_start_of_probe)
{
    using namespace testing;
    testing::NiceMock<MockSharedLibraryProberReport> report;

    EXPECT_CALL(report, probing_path(testing::Eq(library_path)));

    mir::libraries_for_path(library_path, report);
}

TEST_F(SharedLibraryProber, logs_for_nonexistent_path)
{
    using namespace testing;
    NiceMock<MockSharedLibraryProberReport> report;

    EXPECT_CALL(report, probing_path(testing::Eq("/yo/dawg/I/heard/you/liked/slashes")));

    EXPECT_THROW(mir::libraries_for_path("/yo/dawg/I/heard/you/liked/slashes", report),
                 std::runtime_error);
}

TEST_F(SharedLibraryProber, logs_failure_for_nonexistent_path)
{
    using namespace testing;
    NiceMock<MockSharedLibraryProberReport> report;

    EXPECT_CALL(report, probing_failed(Eq("/yo/dawg/I/heard/you/liked/slashes"), _));

    EXPECT_THROW(mir::libraries_for_path("/yo/dawg/I/heard/you/liked/slashes", report),
                 std::runtime_error);

}

TEST_F(SharedLibraryProber, logs_no_libraries_for_path_without_libraries)
{
    using namespace testing;
    NiceMock<MockSharedLibraryProberReport> report;

    EXPECT_CALL(report, loading_library(_)).Times(0);

    mir::libraries_for_path(temporary_directory.c_str(), report);
}

namespace
{
MATCHER_P(FilenameMatches, matcher, "")
{
    using namespace testing;
    *result_listener << "where the path is " << arg;
    return Matches(matcher)(arg.filename().native());
}
}

TEST_F(SharedLibraryProber, logs_each_library_probed)
{
    using namespace testing;
    NiceMock<MockSharedLibraryProberReport> report;

    auto const dso_filename_regex = ".*\\.so(\\..*)?";

    // We have at least 8 DSOs to probe:
    // i386, amd64, armhf, arm64, powerpc, ppc64el, this-arch, libinvalid.so.3
    EXPECT_CALL(report,
        loading_library(FilenameMatches(MatchesRegex(dso_filename_regex)))).Times(AtLeast(8));
    // We shouldn't probe anything that doesn't look like a DSO.
    EXPECT_CALL(report,
        loading_library(FilenameMatches(Not(MatchesRegex(dso_filename_regex))))).Times(0);

    mir::libraries_for_path(library_path, report);
}

TEST_F(SharedLibraryProber, logs_failure_for_load_failure)
{
    using namespace testing;
    NiceMock<MockSharedLibraryProberReport> report;

    std::unordered_map<std::string, bool> probing_map;

    ON_CALL(report, loading_library(_))
        .WillByDefault(Invoke([&probing_map](auto const& filename)
        {
            probing_map[filename.filename().native()] = true;
        }));
    ON_CALL(report, loading_failed(_,_))
        .WillByDefault(Invoke([&probing_map](auto const& filename, auto const&)
        {
            probing_map[filename.filename().native()] = false;
        }));

    mir::libraries_for_path(library_path, report);

    EXPECT_FALSE(probing_map.at("libinvalid.so.3"));

    // As we add extra test DSOs you can add them to this list, but it's not mandatory.
    // At least one of these should fail on any conceivable architecture.
    EXPECT_THAT(probing_map, Contains(AnyOf(
        std::make_pair("lib386.so", false),
        std::make_pair("libamd64.so", false),
        std::make_pair("libarmhf.so", false),
        std::make_pair("libarm64.so", false),
        std::make_pair("libpowerpc.so", false),
        std::make_pair("libppc64el.so", false))));
}

TEST_F(SharedLibraryProber, does_not_log_failure_on_success)
{
    using namespace testing;
    NiceMock<MockSharedLibraryProberReport> report;

    std::unordered_map<std::string, bool> probing_map;

    ON_CALL(report, loading_library(_))
        .WillByDefault(Invoke([&probing_map](auto const& filename)
        {
            probing_map[filename.filename().native()] = true;
        }));
    ON_CALL(report, loading_failed(_,_))
        .WillByDefault(Invoke([&probing_map](auto const& filename, auto const&)
        {
            probing_map[filename.filename().native()] = false;
        }));

    mir::libraries_for_path(library_path, report);

    // libthis-arch should always be loadable...
    EXPECT_TRUE(probing_map.at("libthis-arch.so"));
}