File: test_low_level_errors.py

package info (click to toggle)
python-sdbus 0.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 996 kB
  • sloc: python: 7,911; ansic: 2,507; makefile: 9; sh: 4
file content (210 lines) | stat: -rw-r--r-- 6,554 bytes parent folder | download | duplicates (4)
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
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2020-2022 igo95862

# This file is part of python-sdbus

# 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
from __future__ import annotations

from asyncio import get_running_loop, wait_for
from typing import Any

from sdbus.dbus_common_elements import DbusLocalObjectMeta
from sdbus.exceptions import DbusFailedError
from sdbus.unittest import IsolatedDbusTestCase

from sdbus import (
    DbusInterfaceCommonAsync,
    dbus_method_async,
    dbus_property_async,
    request_default_bus_name_async,
)

HELLO_WORLD = 'Hello, world!'


class DbusDerivePropertydError(DbusFailedError):
    dbus_error_name = 'org.example.PropertyError'


class IndependentError(Exception):
    ...


GOOD_STR = 'Good'


class InterfaceWithErrors(
    DbusInterfaceCommonAsync,
    interface_name='org.example.errors',
):
    @dbus_property_async('s')
    def indep_err_getter(self) -> str:
        raise IndependentError

    @dbus_property_async('s')
    def derrive_err_getter(self) -> str:
        raise DbusDerivePropertydError

    @dbus_method_async(result_signature='s')
    async def hello_error(self) -> str:
        raise AttributeError

    @dbus_method_async(result_signature='s')
    async def hello_world(self) -> str:
        return HELLO_WORLD

    @dbus_property_async('s')
    def indep_err_setable(self) -> str:
        return GOOD_STR

    @indep_err_setable.setter
    def indep_err_setter(self, new_value: str) -> None:
        raise IndependentError

    @dbus_property_async('s')
    def derrive_err_settable(self) -> str:
        return GOOD_STR

    @derrive_err_settable.setter
    def derrive_err_setter(self, new_value: str) -> None:
        raise DbusDerivePropertydError


class TestLowLevelErrors(IsolatedDbusTestCase):
    async def asyncSetUp(self) -> None:
        await super().asyncSetUp()

        await request_default_bus_name_async('org.test')
        self.test_object = InterfaceWithErrors()
        self.test_object.export_to_dbus('/')

        self.test_object_connection = InterfaceWithErrors.new_proxy(
            'org.test', '/')

        loop = get_running_loop()

        def silence_exceptions(*args: Any, **kwrags: Any) -> None:
            ...

        loop.set_exception_handler(silence_exceptions)

    async def test_property_getter_independent_error(self) -> None:
        with self.assertRaises(DbusFailedError) as cm:
            await wait_for(
                self.test_object_connection.indep_err_getter.get_async(),
                timeout=1,
            )

        should_be_dbus_failed = cm.exception
        self.assertIs(should_be_dbus_failed.__class__, DbusFailedError)

        await self.test_object_connection.hello_world()

    async def test_property_getter_derived_error(self) -> None:
        with self.assertRaises(DbusDerivePropertydError):
            await wait_for(
                self.test_object_connection.derrive_err_getter.get_async(),
                timeout=1,
            )

        await self.test_object_connection.hello_world()

    async def test_property_setter_independent_error(self) -> None:

        self.assertEqual(
            await wait_for(
                self.test_object_connection.indep_err_setable.get_async(),
                timeout=1,
            ),
            GOOD_STR,
        )

        with self.assertRaises(DbusFailedError) as cm:
            await wait_for(
                self.test_object_connection.
                indep_err_setable.set_async('Test'),
                timeout=1,
            )

        should_be_dbus_failed = cm.exception
        self.assertIs(should_be_dbus_failed.__class__, DbusFailedError)

        await self.test_object_connection.hello_world()

    async def test_property_setter_derived_error(self) -> None:

        self.assertEqual(
            await wait_for(
                self.test_object_connection.derrive_err_settable.get_async(),
                timeout=1,
            ),
            GOOD_STR,
        )

        with self.assertRaises(DbusDerivePropertydError):
            await wait_for(
                self.test_object_connection.
                derrive_err_settable.set_async('Test'),
                timeout=1,
            )

        await self.test_object_connection.hello_world()

    async def test_property_callback_error(self) -> None:
        dbus_local_meta = self.test_object._dbus
        if not isinstance(dbus_local_meta, DbusLocalObjectMeta):
            raise TypeError
        interface = dbus_local_meta.activated_interfaces[0]
        interface.property_get_dict.pop(b'DerriveErrSettable')

        with self.assertRaises(DbusFailedError):
            await wait_for(
                self.test_object_connection.derrive_err_settable,
                timeout=1,
            )

    async def test_method_callback_error(self) -> None:
        TEST_KEY = b'HelloWorld'
        dbus_local_meta = self.test_object._dbus
        if not isinstance(dbus_local_meta, DbusLocalObjectMeta):
            raise TypeError
        interface = dbus_local_meta.activated_interfaces[0]
        interface.method_dict.pop(TEST_KEY)

        with self.assertRaises(DbusFailedError):
            await wait_for(
                self.test_object_connection.hello_world(),
                timeout=1,
            )

        interface.method_dict[TEST_KEY] = None

        with self.assertRaises(TypeError):
            await wait_for(
                self.test_object_connection.hello_world(),
                timeout=1,
            )

        def test_raise(*args: Any, **kwargs: Any) -> None:
            raise DbusDerivePropertydError

        interface.method_dict[TEST_KEY] = test_raise
        with self.assertRaises(DbusDerivePropertydError):
            await wait_for(
                self.test_object_connection.hello_world(),
                timeout=1,
            )