~thomas-voss/location-service/fix-1408984

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
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <com/ubuntu/location/service/daemon.h>
#include <com/ubuntu/location/service/provider_daemon.h>

#include <com/ubuntu/location/service/stub.h>

#include <com/ubuntu/location/position.h>
#include <com/ubuntu/location/update.h>

#include <core/dbus/fixture.h>

#include <core/dbus/asio/executor.h>

#include <core/posix/fork.h>

#include "did_finish_successfully.h"

#include <gmock/gmock.h>

namespace location = com::ubuntu::location;

namespace
{
struct MockEventReceiver
{
    MOCK_METHOD1(position_update_received, void(const location::Update<location::Position>&));
    MOCK_METHOD1(heading_update_received, void(const location::Update<location::Heading>&));
    MOCK_METHOD1(velocity_update_received, void(const location::Update<location::Velocity>&));
};

struct RemoteProviderdTest : public core::dbus::testing::Fixture
{

};
}

TEST_F(RemoteProviderdTest, AClientReceivesUpdatesFromAnOutOfProcessProvider)
{
    auto oopp = core::posix::fork([this]()
    {
        auto bus = session_bus();

        const char* argv[] =
        {
            "--bus", "session",                                         // 2
            "--service-name", "com.ubuntu.location.providers.Dummy",    // 4
            "--service-path", "/com/ubuntu/location/providers/Dummy",   // 6
            "--provider", "dummy::Provider"                             // 8
        };

        auto dbus_connection_factory = [bus](core::dbus::WellKnownBus)
        {
            return bus;
        };

        return static_cast<core::posix::exit::Status>(
                    location::service::ProviderDaemon::main(
                        location::service::ProviderDaemon::Configuration::from_command_line_args(
                            8, argv, dbus_connection_factory)));
    }, core::posix::StandardStream::empty);

    std::this_thread::sleep_for(std::chrono::milliseconds{250});

    auto service = core::posix::fork([this]()
    {
        core::posix::this_process::env::set_or_throw(
                    "TRUST_STORE_PERMISSION_MANAGER_IS_RUNNING_UNDER_TESTING",
                    "1");

        const char* argv[] =
        {
            "--bus", "session",                                                 // 2
            "--provider", "remote::Provider",                                   // 4
            "--remote::Provider::bus=session_with_address_from_env",            // 5
            "--remote::Provider::name=com.ubuntu.location.providers.Dummy",     // 6
            "--remote::Provider::path=/com/ubuntu/location/providers/Dummy"     // 7
        };

        // The daemon instance requires two bus instances.
        auto dbus_connection_factory = [this](core::dbus::WellKnownBus)
        {
            return session_bus();
        };

        return static_cast<core::posix::exit::Status>(
                    location::service::Daemon::main(
                        location::service::Daemon::Configuration::from_command_line_args(
                            7, argv, dbus_connection_factory)));
    }, core::posix::StandardStream::empty);

    std::this_thread::sleep_for(std::chrono::milliseconds{250});

    auto client = core::posix::fork([this]()
    {
        using namespace ::testing;

        auto trap = core::posix::trap_signals_for_all_subsequent_threads(
        {
            core::posix::Signal::sig_term,
            core::posix::Signal::sig_int
        });

        trap->signal_raised().connect([trap](const core::posix::Signal&)
        {
            trap->stop();
        });

        auto bus = session_bus();
        bus->install_executor(core::dbus::asio::make_executor(bus));

        std::thread worker
        {
            [bus]() { bus->run(); }
        };

        MockEventReceiver receiver;
        EXPECT_CALL(receiver, position_update_received(_)).Times(AtLeast(1));
        EXPECT_CALL(receiver, heading_update_received(_)).Times(AtLeast(1));
        EXPECT_CALL(receiver, velocity_update_received(_)).Times(AtLeast(1));

        com::ubuntu::location::service::Stub service{bus};

        auto session = service.create_session_for_criteria(location::Criteria{});

        session->updates().position.changed().connect([&receiver](location::Update<location::Position> pos)
        {
             receiver.position_update_received(pos);
        });

        session->updates().heading.changed().connect([&receiver](location::Update<location::Heading> heading)
        {
             receiver.heading_update_received(heading);
        });

        session->updates().velocity.changed().connect([&receiver](location::Update<location::Velocity> velocity)
        {
             receiver.velocity_update_received(velocity);
        });

        session->updates().position_status = location::service::session::Interface::Updates::Status::enabled;
        session->updates().velocity_status = location::service::session::Interface::Updates::Status::enabled;
        session->updates().heading_status = location::service::session::Interface::Updates::Status::enabled;

        trap->run();

        bus->stop();

        if (worker.joinable())
            worker.join();

        return ::testing::Test::HasFailure() ? core::posix::exit::Status::failure :
                                               core::posix::exit::Status::success;
    }, core::posix::StandardStream::empty);

    std::this_thread::sleep_for(std::chrono::milliseconds{4000});

    std::cout << "Shutting down client" << std::endl;
    client.send_signal_or_throw(core::posix::Signal::sig_term);
    EXPECT_TRUE(did_finish_successfully(client.wait_for(core::posix::wait::Flags::untraced)));

    std::cout << "Shutting down service" << std::endl;
    service.send_signal_or_throw(core::posix::Signal::sig_term);
    EXPECT_TRUE(did_finish_successfully(service.wait_for(core::posix::wait::Flags::untraced)));

    std::cout << "Shutting down oopp" << std::endl;
    oopp.send_signal_or_throw(core::posix::Signal::sig_term);
    EXPECT_TRUE(did_finish_successfully(oopp.wait_for(core::posix::wait::Flags::untraced)));
}