File: test_specification.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 (440 lines) | stat: -rw-r--r-- 14,490 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#
# 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 dasbus.specification import DBusSpecificationParser, DBusSpecification, \
    DBusSpecificationError


class SpecificationTestCase(unittest.TestCase):

    def test_from_members(self):
        """Test a specification created from members."""
        method = DBusSpecification.Method(
            name="Method",
            interface_name="A",
            in_type="()",
            out_type="(s)"
        )

        signal = DBusSpecification.Signal(
            name="Signal",
            interface_name="B",
            type="(ii)"
        )

        prop = DBusSpecification.Property(
            name="Property",
            interface_name="B",
            readable=True,
            writable=False,
            type="i"
        )

        specification = DBusSpecification()
        specification.add_member(method)
        specification.add_member(signal)
        specification.add_member(prop)

        with self.assertRaises(DBusSpecificationError) as cm:
            specification.get_member("A", "Invalid")

        self.assertEqual(
            "DBus specification has no member 'A.Invalid'.",
            str(cm.exception)
        )

        with self.assertRaises(DBusSpecificationError) as cm:
            specification.get_member("Invalid", "Method")

        self.assertEqual(
            "DBus specification has no member 'Invalid.Method'.",
            str(cm.exception)
        )

        self.assertEqual(specification.interfaces, ["A", "B"])
        self.assertEqual(specification.members, [method, signal, prop])
        self.assertEqual(specification.get_member("A", "Method"), method)
        self.assertEqual(specification.get_member("B", "Signal"), signal)
        self.assertEqual(specification.get_member("B", "Property"), prop)

    def test_from_xml(self):
        """Test a specification created from XML."""
        xml = '''
        <node>
           <interface name="ComplexA">
                <method name="MethodA">
                    <arg direction="out" name="return" type="i"/>
                </method>
                <property access="read" name="PropertyA" type="d"/>
                <signal name="SignalA">
                   <arg direction="out" name="x" type="i"/>
                </signal>
            </interface>
            <interface name="ComplexB">
                <method name="MethodB">
                    <arg direction="in" name="a" type="s"/>
                    <arg direction="in" name="b" type="ad"/>
                    <arg direction="in" name="c" type="i"/>
                    <arg direction="out" name="return" type="i"/>
                </method>
                <property access="read" name="PropertyB" type="d"/>
                <signal name="SignalB">
                    <arg direction="out" name="x" type="b"/>
                    <arg direction="out" name="y" type="d"/>
                    <arg direction="out" name="z" type="(ii)"/>
                </signal>
            </interface>
        </node>
        '''
        specification = DBusSpecification.from_xml(xml)
        self.assertEqual(specification.interfaces, [
            'org.freedesktop.DBus.Introspectable',
            'org.freedesktop.DBus.Peer',
            'org.freedesktop.DBus.Properties',
            'ComplexA',
            'ComplexB',
        ])

        self.assertEqual(specification.members, [
            DBusSpecification.Method(
                name='Introspect',
                interface_name='org.freedesktop.DBus.Introspectable',
                in_type=None,
                out_type='(s)'
            ),
            DBusSpecification.Method(
                name='Ping',
                interface_name='org.freedesktop.DBus.Peer',
                in_type=None,
                out_type=None
            ),
            DBusSpecification.Method(
                name='GetMachineId',
                interface_name='org.freedesktop.DBus.Peer',
                in_type=None,
                out_type='(s)'
            ),
            DBusSpecification.Method(
                name='Get',
                interface_name='org.freedesktop.DBus.Properties',
                in_type='(ss)',
                out_type='(v)'
            ),
            DBusSpecification.Method(
                name='GetAll',
                interface_name='org.freedesktop.DBus.Properties',
                in_type='(s)',
                out_type='(a{sv})'
            ),
            DBusSpecification.Method(
                name='Set',
                interface_name='org.freedesktop.DBus.Properties',
                in_type='(ssv)',
                out_type=None
            ),
            DBusSpecification.Signal(
                name='PropertiesChanged',
                interface_name='org.freedesktop.DBus.Properties',
                type='(sa{sv}as)'
            ),
            DBusSpecification.Method(
                name='MethodA',
                interface_name='ComplexA',
                in_type=None,
                out_type='(i)'
            ),
            DBusSpecification.Property(
                name='PropertyA',
                interface_name='ComplexA',
                readable=True,
                writable=False,
                type='d'
            ),
            DBusSpecification.Signal(
                name='SignalA',
                interface_name='ComplexA',
                type='(i)'
            ),
            DBusSpecification.Method(
                name='MethodB',
                interface_name='ComplexB',
                in_type='(sadi)',
                out_type='(i)'
            ),
            DBusSpecification.Property(
                name='PropertyB',
                interface_name='ComplexB',
                readable=True,
                writable=False,
                type='d'
            ),
            DBusSpecification.Signal(
                name='SignalB',
                interface_name='ComplexB',
                type='(bd(ii))'
            )
        ])


class SpecificationParserTestCase(unittest.TestCase):

    def setUp(self):
        self.maxDiff = None

    def _compare(self, xml, expected_members):
        """Compare members of the specification."""
        specification = DBusSpecification()
        DBusSpecificationParser._parse_xml(specification, xml)
        self.assertEqual(specification.members, expected_members)

    def test_method(self):
        """Test XML with methods."""
        xml = '''
        <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="h"/>
                    <arg direction="out" name="return" type="(ib)"/>
                </method>
            </interface>
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Method(
                name='Method1',
                interface_name='Interface',
                in_type=None,
                out_type=None
            ),
            DBusSpecification.Method(
                name='Method2',
                interface_name='Interface',
                in_type='(i)',
                out_type=None
            ),
            DBusSpecification.Method(
                name='Method3',
                interface_name='Interface',
                in_type=None,
                out_type='(i)'
            ),
            DBusSpecification.Method(
                name='Method4',
                interface_name='Interface',
                in_type='(adh)',
                out_type='((ib))'
            )
        ])

    def test_property(self):
        """Test XML with a property."""
        xml = '''
        <node>
            <interface name="Interface">
                <property name="Property" type="i" access="readwrite" />
            </interface>
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Property(
                name='Property',
                interface_name='Interface',
                readable=True,
                writable=True,
                type='i'
            )
        ])

    def test_property_readonly(self):
        """Test readonly property."""
        xml = '''
        <node>
            <interface name="Interface">
                <property name="Property" type="i" access="read" />
            </interface>
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Property(
                name='Property',
                interface_name='Interface',
                readable=True,
                writable=False,
                type='i'
            )
        ])

    def test_property_writeonly(self):
        """Test writeonly property."""
        xml = '''
        <node>
            <interface name="Interface">
                <property name="Property" type="i" access="write" />
            </interface>
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Property(
                name='Property',
                interface_name='Interface',
                readable=False,
                writable=True,
                type='i'
            )
        ])

    def test_simple_signal(self):
        """Test interface with a simple signal."""
        xml = '''
        <node>
            <interface name="Interface">
                <signal name="SimpleSignal"/>
            </interface>
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Signal(
                name='SimpleSignal',
                interface_name='Interface',
                type=None
            )
        ])

    def test_signal(self):
        """Test interface with signals."""
        xml = '''
        <node>
            <interface name="Interface">
                <signal name="SignalSomething">
                    <arg direction="out" name="x" type="i"/>
                    <arg direction="out" name="y" type="s"/>
                </signal>
                <signal name="SomethingHappened"/>
            </interface>
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Signal(
                name='SignalSomething',
                interface_name='Interface',
                type='(is)'
            ),
            DBusSpecification.Signal(
                name='SomethingHappened',
                interface_name='Interface',
                type=None
            )
        ])

    def test_ignore(self):
        """Ignore invalid XML elements.."""
        xml = '''
        <node>
           <interface name="InterfaceA">
                <method name="MethodA">
                    <arg direction="out" name="return" type="i"/>
                    <ignored />
                </method>
                <property access="read" name="PropertyA" type="d"/>
                <signal name="SignalA">
                   <ignored />
                </signal>
                <ignored />
            </interface>
            <ignored />
        </node>
        '''
        self._compare(xml, [
            DBusSpecification.Method(
                name='MethodA',
                interface_name='InterfaceA',
                in_type=None,
                out_type='(i)'
            ),
            DBusSpecification.Property(
                name='PropertyA',
                interface_name='InterfaceA',
                readable=True,
                writable=False,
                type='d'
            ),
            DBusSpecification.Signal(
                name='SignalA',
                interface_name='InterfaceA',
                type=None
            )
        ])

        self._compare("<ignored />", [])

    def test_standard_interfaces(self):
        """Test with the standard interfaces."""
        specification = DBusSpecificationParser.parse_specification('<node />')
        self.assertEqual(specification.members, [
            DBusSpecification.Method(
                name='Introspect',
                interface_name='org.freedesktop.DBus.Introspectable',
                in_type=None,
                out_type='(s)'
            ),
            DBusSpecification.Method(
                name='Ping',
                interface_name='org.freedesktop.DBus.Peer',
                in_type=None,
                out_type=None
            ),
            DBusSpecification.Method(
                name='GetMachineId',
                interface_name='org.freedesktop.DBus.Peer',
                in_type=None,
                out_type='(s)'
            ),
            DBusSpecification.Method(
                name='Get',
                interface_name='org.freedesktop.DBus.Properties',
                in_type='(ss)',
                out_type='(v)'
            ),
            DBusSpecification.Method(
                name='GetAll',
                interface_name='org.freedesktop.DBus.Properties',
                in_type='(s)',
                out_type='(a{sv})'
            ),
            DBusSpecification.Method(
                name='Set',
                interface_name='org.freedesktop.DBus.Properties',
                in_type='(ssv)',
                out_type=None
            ),
            DBusSpecification.Signal(
                name='PropertiesChanged',
                interface_name='org.freedesktop.DBus.Properties',
                type='(sa{sv}as)'
            )
        ])