~thomas-voss/dbus-cpp/fix-destruction-of-bus-instances

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
/*
 * Copyright © 2013 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 <core/dbus/dbus.h>
#include <core/dbus/message.h>

#include <gtest/gtest.h>

#include <chrono>
#include <memory>

namespace dbus = core::dbus;

TEST(Message, BuildingAMethodCallMessageSucceedsForValidArguments)
{
    const std::string destination = core::dbus::DBus::name();
    const std::string path = core::dbus::DBus::path().as_string();
    const std::string interface = core::dbus::DBus::name();
    const std::string member = "ListNames";

    std::shared_ptr<core::dbus::Message> msg;
    EXPECT_NO_THROW(msg = core::dbus::Message::make_method_call(destination, path, interface, member););
    EXPECT_NE(nullptr, msg.get());
}

TEST(Message, BuildingAMethodCallMessageThrowsForInvalidArguments)
{
    const std::string destination = core::dbus::DBus::name();
    const std::string path = "an:invalid:path";
    const std::string interface = core::dbus::DBus::name();
    const std::string member = "ListNames";

    std::shared_ptr<core::dbus::Message> msg;
    EXPECT_ANY_THROW(msg = core::dbus::Message::make_method_call(destination, path, interface, member););
    EXPECT_EQ(nullptr, msg.get());
}

TEST(Message, AccessingAReaderOnAnEmptyMessageThrows)
{
    const std::string destination = core::dbus::DBus::name();
    const std::string path = core::dbus::DBus::path().as_string();
    const std::string interface = core::dbus::DBus::name();
    const std::string member = "ListNames";

    auto msg = core::dbus::Message::make_method_call(destination, path, interface, member);

    EXPECT_ANY_THROW(msg->reader());
}

TEST(Message, AccessingAWriterOnAnyMessageSucceeds)
{
    const std::string destination = core::dbus::DBus::name();
    const std::string path = core::dbus::DBus::path().as_string();
    const std::string interface = core::dbus::DBus::name();
    const std::string member = "ListNames";

    auto msg = core::dbus::Message::make_method_call(
                destination,
                path,
                interface,
                member);

    EXPECT_NO_THROW(auto writer = msg->writer());

    {
        msg->writer().push_int16(43);
        msg->writer().push_int16(42);
    }

    EXPECT_NO_THROW(auto writer = msg->writer(););
}

TEST(Message, WriteAndSuccessiveReadAreIdempotent)
{
    const std::string destination = core::dbus::DBus::name();
    const std::string path = core::dbus::DBus::path().as_string();
    const std::string interface = core::dbus::DBus::name();
    const std::string member = "ListNames";

    auto msg = core::dbus::Message::make_method_call(
                destination,
                path,
                interface,
                member);

    const int32_t expected_integer_value
    {
        43
    };
    const double expected_floating_point_value
    {
        42.
    };

    {
        auto writer = msg->writer();
        writer.push_int32(expected_integer_value);
        writer.push_floating_point(expected_floating_point_value);
    }

    auto reader = msg->reader();
    auto i = reader.pop_int32();
    auto d = reader.pop_floating_point();

    EXPECT_EQ(expected_integer_value, i);
    EXPECT_EQ(expected_floating_point_value, d);
}

TEST(Message, WriteAndSuccessiveIterationAreIdempotent)
{
    const std::string destination = core::dbus::DBus::name();
    const std::string path = core::dbus::DBus::path().as_string();
    const std::string interface = core::dbus::DBus::name();
    const std::string member = "ListNames";

    auto msg = core::dbus::Message::make_method_call(
                destination,
                path,
                interface,
                member);

    const std::int32_t expected_integer_value
    {
        43
    };
    const double expected_floating_point_value
    {
        42.
    };

    {
        auto writer = msg->writer();
        writer.push_int32(expected_integer_value);
        writer.push_floating_point(expected_floating_point_value);
        auto vw = writer.open_variant(dbus::types::Signature("(id)"));
        {
            auto sw = vw.open_structure();
            {
                sw.push_int32(expected_integer_value);
                sw.push_floating_point(expected_floating_point_value);
            }
            vw.close_structure(std::move(sw));
        }
        writer.close_variant(std::move(vw));
    }

    auto reader = msg->reader();
    EXPECT_EQ(dbus::ArgumentType::int32, reader.type());
    EXPECT_NO_THROW(reader.pop());
    EXPECT_EQ(dbus::ArgumentType::floating_point, reader.type());
    EXPECT_NO_THROW(reader.pop());
    auto vr = reader.pop_variant();
    {
        auto sr = vr.pop_structure();
        {
            EXPECT_EQ(dbus::ArgumentType::int32, sr.type());
            EXPECT_EQ(expected_integer_value, sr.pop_int32());
            EXPECT_EQ(dbus::ArgumentType::floating_point, sr.type());
            EXPECT_EQ(expected_floating_point_value, sr.pop_floating_point());
        }
    }
}