File: test_server.py

package info (click to toggle)
dasbus 1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 880 kB
  • sloc: python: 7,550; makefile: 101; sh: 4
file content (395 lines) | stat: -rw-r--r-- 13,028 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#
# Copyright (C) 2019  Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA
#
import unittest
from textwrap import dedent
from unittest.mock import Mock

from dasbus.error import ErrorMapper, ErrorRule
from dasbus.server.handler import ServerObjectHandler, GLibServer
from dasbus.server.interface import accepts_additional_arguments
from dasbus.signal import Signal
from dasbus.specification import DBusSpecificationError
from dasbus.typing import get_variant


class MethodFailedException(Exception):
    """The method has failed."""
    pass


class DBusServerTestCase(unittest.TestCase):
    """Test DBus server support."""

    NO_PARAMETERS = get_variant("()", tuple())

    def setUp(self):
        self.message_bus = Mock()
        self.connection = self.message_bus.connection
        self.error_mapper = ErrorMapper()
        self.object = None
        self.object_path = "/my/path"
        self.handler = None

    def _publish_object(self, xml="<node />"):
        """Publish a mocked object."""
        self.object = Mock(__dbus_xml__=dedent(xml))

        # Raise AttributeError for default methods.
        del self.object.Get
        del self.object.Set
        del self.object.GetAll

        # Create object signals.
        self.object.Signal1 = Signal()
        self.object.Signal2 = Signal()

        # Create default object signals.
        self.object.PropertiesChanged = Signal()

        self.handler = ServerObjectHandler(
            self.message_bus,
            self.object_path,
            self.object,
            error_mapper=self.error_mapper
        )
        self.handler.connect_object()

    def _call_method(self, interface, method, parameters=NO_PARAMETERS,
                     reply=None):
        invocation = Mock()
        invocation.get_sender.return_value = ":1.0"

        GLibServer._object_callback(
            self.connection, Mock(), self.object_path, interface, method,
            parameters, invocation, (self.handler._method_callback, ())
        )

        invocation.return_dbus_error.assert_not_called()
        invocation.return_value.assert_called_once_with(reply)

    def _call_method_with_error(self, interface, method,
                                parameters=NO_PARAMETERS,
                                error_name=None,
                                error_message=None):
        invocation = Mock()

        with self.assertLogs(level='WARN'):
            self.handler._method_callback(
                invocation,
                interface,
                method,
                parameters
            )

        invocation.return_dbus_error.assert_called_once()
        invocation.return_value.assert_not_called()

        (name, msg), kwargs = invocation.return_dbus_error.call_args

        self.assertEqual(kwargs, {})
        self.assertEqual(name, error_name, "Unexpected error name.")

        if error_message is not None:
            self.assertEqual(msg, error_message, "Unexpected error message.")

    def test_register(self):
        """Test the object registration."""
        with self.assertRaises(DBusSpecificationError) as cm:
            self._publish_object("<node />")

        self.assertEqual(
            "No DBus interfaces for registration.",
            str(cm.exception)
        )

        self._publish_object("""
        <node>
            <interface name="Interface" />
        </node>
        """)
        self.message_bus.connection.register_object.assert_called()

        self.handler.disconnect_object()
        self.message_bus.connection.unregister_object.assert_called()

    def test_method(self):
        """Test the method publishing."""
        self._publish_object("""
        <node>
            <interface name="Interface">
                <method name="Method1"/>
                <method name="Method2">
                    <arg direction="in" name="x" type="i"/>
                </method>
                <method name="Method3">
                    <arg direction="out" name="return" type="i"/>
                </method>
                <method name="Method4">
                    <arg direction="in" name="x" type="ad"/>
                    <arg direction="in" name="y" type="o"/>
                    <arg direction="out" name="return" type="(ib)"/>
                </method>
                <method name="Method5">
                    <arg direction="out" name="return" type="i"/>
                    <arg direction="out" name="return" type="b"/>
                </method>
            </interface>
        </node>
        """)

        self.object.Method2.return_value = None
        self._call_method(
            "Interface",
            "Method2",
            parameters=get_variant("(i)", (1, ))
        )
        self.object.Method2.assert_called_once_with(1)

        self.object.Method1.return_value = None
        self._call_method("Interface", "Method1")
        self.object.Method1.assert_called_once_with()

        self.object.Method3.return_value = 0
        self._call_method(
            "Interface",
            "Method3",
            reply=get_variant("(i)", (0, ))
        )
        self.object.Method3.assert_called_once_with()

        self.object.Method4.return_value = (1, True)
        self._call_method(
            "Interface",
            "Method4",
            parameters=get_variant("(ado)", ([1.2, 2.3], "/my/path")),
            reply=get_variant("((ib))", ((1, True), ))
        )
        self.object.Method4.assert_called_once_with([1.2, 2.3], "/my/path")

        self.object.Method5.return_value = (1, True)
        self._call_method(
            "Interface",
            "Method5",
            reply=get_variant("(ib)", (1, True))
        )
        self.object.Method5.assert_called_once_with()

        self._call_method_with_error(
            "Interface",
            "MethodInvalid",
            error_name="not.known.Error.DBusSpecificationError",
            error_message="DBus specification has no member "
                          "'Interface.MethodInvalid'."
        )

        self.error_mapper.add_rule(ErrorRule(
            exception_type=MethodFailedException,
            error_name="MethodFailed"
        ))

        self.object.Method1.side_effect = MethodFailedException(
            "The method has failed."
        )
        self._call_method_with_error(
            "Interface",
            "Method1",
            error_name="MethodFailed",
            error_message="The method has failed."
        )

    def test_invalid_method_result(self):
        """Test a method with an invalid result."""
        self._publish_object("""
        <node>
            <interface name="Interface">
                <method name="Method">
                    <arg direction="out" name="return" type="t"/>
                </method>
            </interface>
        </node>
        """)

        self.object.Method.return_value = -1
        self._call_method_with_error(
            "Interface",
            "Method",
            error_name="not.known.Error.OverflowError"
        )

    def test_property(self):
        """Test the property publishing."""
        self._publish_object("""
        <node>
            <interface name="Interface">
                <property name="Property1" type="i" access="readwrite" />
                <property name="Property2" type="s" access="read" />
                <property name="Property3" type="b" access="write" />
            </interface>
        </node>
        """)

        self.object.Property1 = 0
        self._call_method(
            "org.freedesktop.DBus.Properties", "Get",
            parameters=get_variant("(ss)", (
                "Interface",
                "Property1"
            )),
            reply=get_variant("(v)", (get_variant("i", 0), ))
        )

        self._call_method(
            "org.freedesktop.DBus.Properties", "Set",
            parameters=get_variant("(ssv)", (
                "Interface",
                "Property1",
                get_variant("i", 1)
            )),
        )
        self.assertEqual(self.object.Property1, 1)

        self.object.Property2 = "Hello"
        self._call_method(
            "org.freedesktop.DBus.Properties", "Get",
            parameters=get_variant("(ss)", (
                "Interface",
                "Property2"
            )),
            reply=get_variant("(v)", (get_variant("s", "Hello"), ))
        )
        self._call_method_with_error(
            "org.freedesktop.DBus.Properties", "Set",
            parameters=get_variant("(ssv)", (
                "Interface",
                "Property2",
                get_variant("s", "World")
            )),
            error_name="not.known.Error.AttributeError",
            error_message="The property Interface.Property2 "
                          "is not writable."
        )
        self.assertEqual(self.object.Property2, "Hello")

        self.object.Property3 = True
        self._call_method_with_error(
            "org.freedesktop.DBus.Properties", "Get",
            parameters=get_variant("(ss)", (
                "Interface",
                "Property3"
            )),
            error_name="not.known.Error.AttributeError",
            error_message="The property Interface.Property3 "
                          "is not readable."
        )
        self._call_method(
            "org.freedesktop.DBus.Properties", "Set",
            parameters=get_variant("(ssv)", (
                "Interface",
                "Property3",
                get_variant("b", False)
            )),
        )
        self.assertEqual(self.object.Property3, False)

        self._call_method(
            "org.freedesktop.DBus.Properties", "GetAll",
            parameters=get_variant("(s)", ("Interface", )),
            reply=get_variant("(a{sv})", ({
                "Property1": get_variant("i", 1),
                "Property2": get_variant("s", "Hello")
            }, ))
        )

        self.object.PropertiesChanged(
            "Interface",
            {"Property1": get_variant("i", 1)},
            ["Property2"]
        )
        self.message_bus.connection.emit_signal.assert_called_once_with(
            None,
            self.object_path,
            "org.freedesktop.DBus.Properties",
            "PropertiesChanged",
            get_variant("(sa{sv}as)", (
                "Interface",
                {"Property1": get_variant("i", 1)},
                ["Property2"]
            ))
        )

    def test_signal(self):
        """Test the signal publishing."""
        self._publish_object("""
        <node>
            <interface name="Interface">
                <signal name="Signal1" />
                <signal name="Signal2">
                    <arg direction="out" name="x" type="i"/>
                    <arg direction="out" name="y" type="s"/>
                </signal>
            </interface>
        </node>
        """)
        self.object.Signal1()
        self.message_bus.connection.emit_signal.assert_called_once_with(
            None,
            self.object_path,
            "Interface",
            "Signal1",
            None
        )

        self.message_bus.connection.emit_signal.reset_mock()

        self.object.Signal2(1, "Test")
        self.message_bus.connection.emit_signal.assert_called_once_with(
            None,
            self.object_path,
            "Interface",
            "Signal2",
            get_variant("(is)", (1, "Test"))
        )

    def test_call_info(self):
        self._publish_object("""
        <node>
            <interface name="Interface">
                <method name="Method1"/>
                <method name="Method2">
                    <arg direction="in" name="x" type="i"/>
                </method>
            </interface>
        </node>
        """)

        accepts_additional_arguments(self.object.Method1)
        self._call_method("Interface", "Method1")
        self.object.Method1.assert_called_once_with(
            call_info={"sender": ":1.0"}
        )

        accepts_additional_arguments(self.object.Method2)
        self._call_method(
            "Interface",
            "Method2",
            parameters=get_variant("(i)", (1, ))
        )
        self.object.Method2.assert_called_once_with(
            1, call_info={"sender": ":1.0"}
        )