2
* Copyright Ā© 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
19
#include "mir_toolkit/mir_client_library.h"
20
#include "mir_toolkit/debug/surface.h"
22
#include "src/include/client/mir/client_platform_factory.h"
23
#include "src/include/client/mir/client_platform.h"
24
#include "src/include/client/mir/client_buffer_factory.h"
26
#include "mir/test/validity_matchers.h"
28
#include "mir_test_framework/headless_in_process_server.h"
29
#include "mir_test_framework/using_client_platform.h"
30
#include "mir_test_framework/using_stub_client_platform.h"
31
#include "mir_test_framework/stub_client_connection_configuration.h"
32
#include "mir_test_framework/any_surface.h"
33
#include "mir/test/doubles/stub_client_buffer_factory.h"
35
#include <gtest/gtest.h>
36
#include <gmock/gmock.h>
39
#include <boost/throw_exception.hpp>
42
namespace mcl = mir::client;
43
namespace mtf = mir_test_framework;
44
namespace mtd = mir::test::doubles;
48
enum Method : uint64_t
51
the_client_platform_factory = 1<<0,
52
create_client_platform = 1<<1,
53
create_egl_native_window = 1<<2,
54
create_buffer_factory = 1<<3
57
std::string const exception_text{"Ducks!"};
59
template<Method name, Method failure_set>
62
return (name & failure_set);
65
template<Method failure_set>
66
class ConfigurableFailurePlatform : public mir::client::ClientPlatform
68
std::shared_ptr<EGLNativeWindowType> create_egl_native_window(mir::client::EGLNativeSurface *)
70
if (should_fail<Method::create_egl_native_window, failure_set>())
72
BOOST_THROW_EXCEPTION(std::runtime_error{exception_text});
74
return std::shared_ptr<EGLNativeWindowType>{};
77
void populate(MirPlatformPackage&) const override
81
MirPlatformMessage* platform_operation(MirPlatformMessage const*) override
86
MirPlatformType platform_type() const
88
BOOST_THROW_EXCEPTION(std::runtime_error{exception_text});
89
return MirPlatformType::mir_platform_type_gbm;
91
std::shared_ptr<mir::client::ClientBufferFactory> create_buffer_factory()
93
if (should_fail<Method::create_buffer_factory, failure_set>())
95
BOOST_THROW_EXCEPTION(std::runtime_error{exception_text});
97
return std::make_shared<mtd::StubClientBufferFactory>();
99
std::shared_ptr<EGLNativeDisplayType> create_egl_native_display()
101
return std::shared_ptr<EGLNativeDisplayType>{};
103
MirNativeBuffer *convert_native_buffer(mir::graphics::NativeBuffer*) const
105
BOOST_THROW_EXCEPTION(std::runtime_error{exception_text});
108
MirPixelFormat get_egl_pixel_format(EGLDisplay, EGLConfig) const override
110
return mir_pixel_format_invalid;
114
template<Method failure_set>
115
class ConfigurableFailureFactory: public mir::client::ClientPlatformFactory
117
std::shared_ptr<mir::client::ClientPlatform>
118
create_client_platform(mir::client::ClientContext* /*context*/) override
120
if (should_fail<Method::create_client_platform, failure_set>())
122
BOOST_THROW_EXCEPTION(std::runtime_error{exception_text});
124
return std::make_shared<ConfigurableFailurePlatform<failure_set>>();
128
template<Method failure_set>
129
class ConfigurableFailureConfiguration : public mtf::StubConnectionConfiguration
131
using mtf::StubConnectionConfiguration::StubConnectionConfiguration;
133
std::shared_ptr<mir::client::ClientPlatformFactory> the_client_platform_factory() override
135
if (should_fail<Method::the_client_platform_factory, failure_set>())
137
BOOST_THROW_EXCEPTION(std::runtime_error{exception_text});
139
return std::make_shared<ConfigurableFailureFactory<failure_set>>();
143
struct ClientLibraryErrors : mtf::HeadlessInProcessServer
145
ClientLibraryErrors() { add_to_environment("MIR_SERVER_ENABLE_INPUT","off"); }
150
TEST_F(ClientLibraryErrors, exception_in_client_configuration_constructor_generates_error)
152
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::the_client_platform_factory>> stubby;
154
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
156
EXPECT_FALSE(mir_connection_is_valid(connection));
157
EXPECT_THAT(mir_connection_get_error_message(connection), testing::HasSubstr(exception_text));
158
mir_connection_release(connection);
161
TEST_F(ClientLibraryErrors, exception_in_platform_construction_generates_error)
163
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::create_client_platform>> stubby;
165
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
167
EXPECT_FALSE(mir_connection_is_valid(connection));
168
EXPECT_THAT(mir_connection_get_error_message(connection), testing::HasSubstr(exception_text));
169
mir_connection_release(connection);
172
TEST_F(ClientLibraryErrors, connecting_to_garbage_socket_returns_appropriate_error)
174
using namespace testing;
175
mtf::UsingStubClientPlatform stubby;
177
auto connection = mir_connect_sync("garbage", __PRETTY_FUNCTION__);
178
ASSERT_THAT(connection, NotNull());
180
char const* error = mir_connection_get_error_message(connection);
182
if (std::strcmp("connect: No such file or directory", error) &&
183
std::strcmp("Can't find MIR server", error) &&
184
!std::strstr(error, "Failed to connect to server socket"))
188
mir_connection_release(connection);
191
TEST_F(ClientLibraryErrors, create_surface_returns_error_object_on_failure)
193
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::create_buffer_factory>> stubby;
195
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
197
ASSERT_THAT(connection, IsValid());
199
auto surface = mtf::make_any_surface(connection);
200
ASSERT_NE(surface, nullptr);
201
EXPECT_FALSE(mir_surface_is_valid(surface));
202
EXPECT_THAT(mir_surface_get_error_message(surface), testing::HasSubstr(exception_text));
204
mir_surface_release_sync(surface);
205
mir_connection_release(connection);
210
void recording_surface_callback(MirSurface*, void* ctx)
212
auto called = static_cast<bool*>(ctx);
217
TEST_F(ClientLibraryErrors, surface_release_on_error_object_still_calls_callback)
219
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::create_buffer_factory>> stubby;
221
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
223
ASSERT_THAT(connection, IsValid());
225
auto surface = mtf::make_any_surface(connection);
227
ASSERT_NE(surface, nullptr);
228
EXPECT_FALSE(mir_surface_is_valid(surface));
229
EXPECT_THAT(mir_surface_get_error_message(surface), testing::HasSubstr(exception_text));
231
bool callback_called{false};
232
mir_surface_release(surface, &recording_surface_callback, &callback_called);
233
EXPECT_TRUE(callback_called);
234
mir_connection_release(connection);
238
TEST_F(ClientLibraryErrors, create_surface_returns_error_object_on_failure_in_reply_processing)
240
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::create_egl_native_window>> stubby;
242
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
244
ASSERT_THAT(connection, IsValid());
246
auto surface = mtf::make_any_surface(connection);
247
ASSERT_NE(surface, nullptr);
248
EXPECT_FALSE(mir_surface_is_valid(surface));
249
EXPECT_THAT(mir_surface_get_error_message(surface), testing::HasSubstr(exception_text));
251
mir_surface_release_sync(surface);
252
mir_connection_release(connection);
255
TEST_F(ClientLibraryErrors, passing_invalid_parent_id_to_surface_create)
257
using namespace testing;
259
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
261
ASSERT_THAT(connection, IsValid());
263
// An ID that parses as valid, but doesn't correspond to any
264
auto invalid_id = mir_persistent_id_from_string("05f223a2-39e5-48b9-9416-b0ce837351b6");
266
auto spec = mir_connection_create_spec_for_input_method(connection,
268
mir_pixel_format_argb_8888);
275
mir_surface_spec_attach_to_foreign_parent(spec, invalid_id, &rect, mir_edge_attachment_any);
277
auto surface = mir_surface_create_sync(spec);
278
EXPECT_THAT(surface, Not(IsValid()));
279
EXPECT_THAT(mir_surface_get_error_message(surface), MatchesRegex(".*Lookup.*failed.*"));
281
mir_persistent_id_release(invalid_id);
282
mir_surface_spec_release(spec);
283
mir_surface_release_sync(surface);
284
mir_connection_release(connection);
287
using ClientLibraryErrorsDeathTest = ClientLibraryErrors;
290
TEST_F(ClientLibraryErrorsDeathTest, creating_surface_on_garbage_connection_is_fatal)
292
mtf::UsingStubClientPlatform stubby;
294
auto connection = mir_connect_sync("garbage", __PRETTY_FUNCTION__);
296
ASSERT_FALSE(mir_connection_is_valid(connection));
298
mtf::make_any_surface(connection), "");
300
mir_connection_release(connection);
304
TEST_F(ClientLibraryErrorsDeathTest, creating_surface_synchronosly_on_malconstructed_connection_is_fatal)
306
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::the_client_platform_factory>> stubby;
308
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
310
ASSERT_FALSE(mir_connection_is_valid(connection));
311
EXPECT_DEATH(mtf::make_any_surface(connection), "");
313
mir_connection_release(connection);
316
TEST_F(ClientLibraryErrorsDeathTest, creating_surface_synchronosly_on_invalid_connection_is_fatal)
318
mtf::UsingClientPlatform<ConfigurableFailureConfiguration<Method::create_client_platform>> stubby;
320
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
322
ASSERT_FALSE(mir_connection_is_valid(connection));
323
EXPECT_DEATH(mtf::make_any_surface(connection), "");
325
mir_connection_release(connection);
328
TEST_F(ClientLibraryErrorsDeathTest, surface_spec_attaching_invalid_parent_id)
330
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
332
auto spec = mir_connection_create_spec_for_input_method(connection, 100, 100, mir_pixel_format_argb_8888);
340
EXPECT_DEATH(mir_surface_spec_attach_to_foreign_parent(spec, nullptr, &rect, mir_edge_attachment_any), "");
342
mir_connection_release(connection);
345
TEST_F(ClientLibraryErrorsDeathTest, surface_spec_attaching_invalid_rectangle)
347
auto connection = mir_connect_sync(new_connection().c_str(), __PRETTY_FUNCTION__);
349
auto spec = mir_connection_create_spec_for_input_method(connection, 100, 100, mir_pixel_format_argb_8888);
351
auto id = mir_persistent_id_from_string("fa69b2e9-d507-4005-be61-5068f40a5aec");
353
EXPECT_DEATH(mir_surface_spec_attach_to_foreign_parent(spec, id, nullptr, mir_edge_attachment_any), "");
355
mir_persistent_id_release(id);
356
mir_connection_release(connection);