~thomas-voss/biometryd/remove-obsolete-configuration-header

« back to all changes in this revision

Viewing changes to tests/test_dbus_codec.cpp

  • Committer: Thomas Voß
  • Date: 2016-05-02 06:16:01 UTC
  • Revision ID: thomas.voss@canonical.com-20160502061601-vqzwfw4c0rxfna04
Initial commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 *
 
18
 */
 
19
 
 
20
#include <biometry/runtime.h>
 
21
#include <biometry/dbus/codec.h>
 
22
 
 
23
#include <core/dbus/fixture.h>
 
24
#include <core/dbus/macros.h>
 
25
#include <core/dbus/message.h>
 
26
#include <core/dbus/object.h>
 
27
 
 
28
#include <core/posix/fork.h>
 
29
#include <core/posix/signal.h>
 
30
 
 
31
#include <core/dbus/asio/executor.h>
 
32
 
 
33
#include <gmock/gmock.h>
 
34
 
 
35
#include "did_finish_successfully.h"
 
36
#include "echo_service.h"
 
37
 
 
38
namespace
 
39
{
 
40
struct VerifyingEchoService : public testing::EchoService
 
41
{
 
42
    struct Reference
 
43
    {
 
44
        static biometry::Application application()
 
45
        {
 
46
            return biometry::Application{"JustSomeDummyApplication"};
 
47
        }
 
48
 
 
49
        static biometry::Progress progress()
 
50
        {
 
51
            biometry::Progress result;
 
52
            result.percent = biometry::Percent::from_raw_value(0.42);
 
53
            return result;
 
54
        }
 
55
 
 
56
        static biometry::Point point()
 
57
        {
 
58
            return biometry::Point{42, 42};
 
59
        }
 
60
 
 
61
        static biometry::Rectangle rectangle()
 
62
        {
 
63
            return {point(), point()};
 
64
        }
 
65
 
 
66
        static biometry::Reason reason()
 
67
        {
 
68
            return biometry::Reason{"because we are testing"};
 
69
        }
 
70
 
 
71
        static biometry::User user()
 
72
        {
 
73
            return biometry::User::current();
 
74
        }
 
75
 
 
76
        static biometry::Variant::None none()
 
77
        {
 
78
            return biometry::Variant::None{};
 
79
        }
 
80
 
 
81
        static biometry::Variant variant()
 
82
        {
 
83
            return biometry::Variant::v(
 
84
            {
 
85
                biometry::Variant::b(true),
 
86
                biometry::Variant::i(42),
 
87
                biometry::Variant::d(42.),
 
88
                biometry::Variant::r(Reference::rectangle()),
 
89
                biometry::Variant::s("42"),
 
90
                biometry::Variant::v(
 
91
                {
 
92
                    biometry::Variant::b(true),
 
93
                    biometry::Variant::i(42),
 
94
                    biometry::Variant::d(42.),
 
95
                    biometry::Variant::r(Reference::rectangle()),
 
96
                    biometry::Variant::s("42")
 
97
                })
 
98
            });
 
99
        }
 
100
 
 
101
        static biometry::Void void_()
 
102
        {
 
103
            return biometry::Void{};
 
104
        }
 
105
    };
 
106
 
 
107
    void application(const biometry::Application& value)
 
108
    {
 
109
        EXPECT_EQ(Reference::application(), value);
 
110
    }
 
111
 
 
112
    void progress(const biometry::Progress& value)
 
113
    {
 
114
        EXPECT_EQ(Reference::progress(), value);
 
115
    }
 
116
 
 
117
    void point(const biometry::Point& value)
 
118
    {
 
119
        EXPECT_EQ(Reference::point(), value);
 
120
    }
 
121
 
 
122
    void rectangle(const biometry::Rectangle& value)
 
123
    {
 
124
        EXPECT_EQ(Reference::rectangle(), value);
 
125
    }
 
126
 
 
127
    void reason(const biometry::Reason& value)
 
128
    {
 
129
        EXPECT_EQ(Reference::reason(), value);
 
130
    }
 
131
 
 
132
    void user(const biometry::User& value)
 
133
    {
 
134
        EXPECT_EQ(Reference::user(), value);
 
135
    }
 
136
 
 
137
    void none(const biometry::Variant::None& value)
 
138
    {
 
139
        EXPECT_EQ(Reference::none(), value);
 
140
    }
 
141
 
 
142
    void variant(const biometry::Variant& value)
 
143
    {
 
144
        EXPECT_EQ(Reference::variant(), value);
 
145
    }
 
146
 
 
147
    void void_(const biometry::Void& value)
 
148
    {
 
149
        EXPECT_EQ(Reference::void_(), value);
 
150
    }
 
151
};
 
152
}
 
153
 
 
154
struct DbusCodec : public core::dbus::testing::Fixture
 
155
{
 
156
};
 
157
 
 
158
TEST_F(DbusCodec, encoding_and_decoding_works_for_relevant_types)
 
159
{
 
160
    auto sk = [this]()
 
161
    {
 
162
        auto trap = core::posix::trap_signals_for_all_subsequent_threads({core::posix::Signal::sig_term});
 
163
        trap->signal_raised().connect([trap](core::posix::Signal)
 
164
        {
 
165
            trap->stop();
 
166
        });
 
167
 
 
168
        auto bus = session_bus();
 
169
        bus->install_executor(core::dbus::asio::make_executor(bus));
 
170
 
 
171
        std::thread t{[bus]() { bus->run(); }};
 
172
 
 
173
        auto impl = std::make_shared<VerifyingEchoService>();
 
174
        auto sk = std::make_shared<testing::SkeletonEchoService>(bus,
 
175
                    core::dbus::Service::add_service(bus, testing::EchoService::name())
 
176
                        ->add_object_for_path(testing::EchoService::path()),
 
177
                    impl);
 
178
 
 
179
        trap->run();
 
180
 
 
181
        bus->stop();
 
182
 
 
183
        if (t.joinable())
 
184
            t.join();
 
185
 
 
186
        return Test::HasFailure() ? core::posix::exit::Status::failure : core::posix::exit::Status::success;
 
187
    };
 
188
 
 
189
    auto st = [this]()
 
190
    {
 
191
        auto bus = session_bus();
 
192
        bus->install_executor(core::dbus::asio::make_executor(bus));
 
193
 
 
194
        std::thread t{[bus]() { bus->run(); }};
 
195
 
 
196
        auto st = std::make_shared<testing::StubEchoService>(core::dbus::Service::use_service(bus, testing::EchoService::name())->object_for_path(testing::EchoService::path()));
 
197
 
 
198
        EXPECT_NO_THROW(st->application(VerifyingEchoService::Reference::application()));
 
199
        EXPECT_NO_THROW(st->progress(VerifyingEchoService::Reference::progress()));
 
200
        EXPECT_NO_THROW(st->point(VerifyingEchoService::Reference::point()));
 
201
        EXPECT_NO_THROW(st->rectangle(VerifyingEchoService::Reference::rectangle()));
 
202
        EXPECT_NO_THROW(st->reason(VerifyingEchoService::Reference::reason()));
 
203
        EXPECT_NO_THROW(st->user(VerifyingEchoService::Reference::user()));
 
204
        EXPECT_NO_THROW(st->none(VerifyingEchoService::Reference::none()));
 
205
        EXPECT_NO_THROW(st->variant(VerifyingEchoService::Reference::variant()));
 
206
        EXPECT_NO_THROW(st->void_(VerifyingEchoService::Reference::void_()));
 
207
 
 
208
        bus->stop(); if (t.joinable()) t.join();
 
209
 
 
210
        return Test::HasFailure() ? core::posix::exit::Status::failure : core::posix::exit::Status::success;
 
211
    };
 
212
 
 
213
    auto skp = core::posix::fork(sk, core::posix::StandardStream::empty);
 
214
    std::this_thread::sleep_for(std::chrono::milliseconds{500});
 
215
    auto stp = core::posix::fork(st, core::posix::StandardStream::empty);
 
216
 
 
217
    EXPECT_TRUE(testing::did_finish_successfully(stp.wait_for(core::posix::wait::Flags::untraced)));
 
218
    EXPECT_NO_THROW(skp.send_signal_or_throw(core::posix::Signal::sig_term));
 
219
    EXPECT_TRUE(testing::did_finish_successfully(skp.wait_for(core::posix::wait::Flags::untraced)));
 
220
}