summaryrefslogtreecommitdiff
path: root/tests/twisted/account-manager/update-parameters.py
blob: a030a01f8a320a42cf55be9b5f33a70617aacadb (plain)
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
# vim: set fileencoding=utf-8 :
# Copyright © 2009 Nokia Corporation
# Copyright © 2009–2011 Collabora Ltd.
#
# 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 St, Fifth Floor, Boston, MA
# 02110-1301 USA

import os
import time

import dbus
import dbus.service

from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
        call_async, assertEquals, assertSameSets
from mctest import exec_test, SimulatedConnection, create_fakecm_account,\
        SimulatedChannel
import constants as cs

def test(q, bus, mc, **kwargs):
    # Create an account
    params = dbus.Dictionary(
        {"account": "someguy@example.com",
         "password": "secrecy",
         "nickname": "albinoblacksheep",
         }, signature='sv')
    (simulated_cm, account) = create_fakecm_account(q, bus, mc, params)

    # Enable the account
    account.Set(cs.ACCOUNT, 'Enabled', True,
            dbus_interface=cs.PROPERTIES_IFACE)
    q.expect('dbus-signal',
            path=account.object_path,
            signal='AccountPropertyChanged',
            interface=cs.ACCOUNT)

    # Go online
    requested_presence = dbus.Struct((dbus.UInt32(2L), dbus.String(u'brb'),
                dbus.String(u'Be back soon!')))
    account.Set(cs.ACCOUNT,
            'RequestedPresence', requested_presence,
            dbus_interface=cs.PROPERTIES_IFACE)

    e = q.expect('dbus-method-call', method='RequestConnection',
            args=['fakeprotocol', params],
            destination=tp_name_prefix + '.ConnectionManager.fakecm',
            path=tp_path_prefix + '/ConnectionManager/fakecm',
            interface=tp_name_prefix + '.ConnectionManager',
            handled=False)

    conn = SimulatedConnection(q, bus, 'fakecm', 'fakeprotocol', '_',
            'myself')

    q.dbus_return(e.message, conn.bus_name, conn.object_path, signature='so')

    # MC does some setup, including fetching the list of Channels

    q.expect_many(
            EventPattern('dbus-method-call',
                interface=cs.PROPERTIES_IFACE, method='GetAll',
                args=[cs.CONN_IFACE_REQUESTS],
                path=conn.object_path, handled=True),
            )

    # MC prepares the connection, does any pre-Connect setup, then
    # calls Connect
    q.expect('dbus-method-call', method='Connect',
            path=conn.object_path, handled=True)

    # Connect succeeds
    conn.StatusChanged(cs.CONN_STATUS_CONNECTED, cs.CSR_NONE_SPECIFIED)

    # Assert that the NormalizedName is harvested from the Connection at some
    # point
    while 1:
        e = q.expect('dbus-signal',
                interface=cs.ACCOUNT, signal='AccountPropertyChanged',
                path=account.object_path)
        if 'NormalizedName' in e.args[0]:
            assert e.args[0]['NormalizedName'] == 'myself', e.args
            break

    # Check the requested presence is online
    properties = account.GetAll(cs.ACCOUNT,
            dbus_interface=cs.PROPERTIES_IFACE)
    assert properties is not None
    assert properties.get('RequestedPresence') == requested_presence, \
        properties.get('RequestedPresence')

    # Set some parameters. They include setting account to \\, as a regression
    # test for part of fd.o #28557.
    call_async(q, account, 'UpdateParameters',
            {
                'account': r'\\',
                'secret-mushroom': '/Amanita muscaria/',
                'snakes': dbus.UInt32(42),
                'com.example.Badgerable.Badgered': True,
            },
            [],
            dbus_interface=cs.ACCOUNT)

    set_call, ret, _ = q.expect_many(
            EventPattern('dbus-method-call',
                path=conn.object_path,
                interface=cs.PROPERTIES_IFACE, method='Set',
                args=['com.example.Badgerable', 'Badgered', True],
                handled=False),
            EventPattern('dbus-return',
                method='UpdateParameters'),
            EventPattern('dbus-signal',
                path=account.object_path,
                interface=cs.ACCOUNT, signal='AccountPropertyChanged',
                args=[{'Parameters': {
                    'account': r'\\',
                    'com.example.Badgerable.Badgered': True,
                    'password': 'secrecy',
                    'nickname': 'albinoblacksheep',
                    'secret-mushroom': '/Amanita muscaria/',
                    'snakes': 42,
                    }}]),
            )

    # the D-Bus property should be set instantly; the others will take effect
    # on reconnection
    not_yet = ret.value[0]
    not_yet.sort()
    assert not_yet == ['account', 'secret-mushroom', 'snakes'], not_yet

    # Try to update 'account' to a value of the wrong type; MC should complain,
    # without having changed the value of 'snakes'.
    call_async(q, account, 'UpdateParameters',
            { 'account': dbus.UInt32(39),
              'snakes': dbus.UInt32(39),
            },
            [],
            dbus_interface=cs.ACCOUNT)
    q.expect('dbus-error', name=cs.INVALID_ARGUMENT)

    props = account.Get(cs.ACCOUNT, 'Parameters',
        dbus_interface=cs.PROPERTIES_IFACE)
    assertEquals(42, props['snakes'])

    # Try to update a parameter that doesn't exist; again, 'snakes' should not
    # be changed.
    call_async(q, account, 'UpdateParameters',
            { 'accccccount': dbus.UInt32(39),
              'snakes': dbus.UInt32(39),
            },
            [],
            dbus_interface=cs.ACCOUNT)
    q.expect('dbus-error', name=cs.INVALID_ARGUMENT)

    props = account.Get(cs.ACCOUNT, 'Parameters',
        dbus_interface=cs.PROPERTIES_IFACE)
    assertEquals(42, props['snakes'])

    # Unset some parameters, including a parameter which doesn't exist at all.
    # The spec says that “If the given parameters […] do not exist at all, the
    # account manager MUST accept this without error.”
    call_async(q, account, 'UpdateParameters',
            {},
            ['nickname', 'com.example.Badgerable.Badgered', 'froufrou'],
            dbus_interface=cs.ACCOUNT)

    ret, _, _ = q.expect_many(
            EventPattern('dbus-return',
                method='UpdateParameters'),
            EventPattern('dbus-signal',
                path=account.object_path,
                interface=cs.ACCOUNT, signal='AccountPropertyChanged',
                args=[{'Parameters': {
                    'account': r'\\',
                    'password': 'secrecy',
                    'secret-mushroom': '/Amanita muscaria/',
                    'snakes': 42,
                    }}]),
            EventPattern('dbus-method-call',
                path=conn.object_path,
                interface=cs.PROPERTIES_IFACE, method='Set',
                args=['com.example.Badgerable', 'Badgered', False],
                handled=False),
            )

    # Because com.example.Badgerable.Badgered has a default value (namely
    # False), unsetting that parameter should cause the default value to be set
    # on the CM.
    not_yet = ret.value[0]
    assertEquals(['nickname'], not_yet)

    # Set contrived-example to its default value; since there's been no
    # practical change, we shouldn't be told we need to reconnect to apply it.
    call_async(q, account, 'UpdateParameters',
        { 'contrived-example': dbus.UInt32(5) }, [])
    ret, _ = q.expect_many(
            EventPattern('dbus-return', method='UpdateParameters'),
            EventPattern('dbus-signal',
                path=account.object_path,
                interface=cs.ACCOUNT, signal='AccountPropertyChanged',
                args=[{'Parameters': {
                    'account': r'\\',
                    'password': 'secrecy',
                    'secret-mushroom': '/Amanita muscaria/',
                    'snakes': 42,
                    "contrived-example": 5,
                    }}]),
            )
    not_yet = ret.value[0]
    assertEquals([], not_yet)

    # Unset contrived-example; again, MC should be smart enough to know we
    # don't need to do anything.
    call_async(q, account, 'UpdateParameters', {}, ['contrived-example'])
    ret, _ = q.expect_many(
            EventPattern('dbus-return', method='UpdateParameters'),
            EventPattern('dbus-signal',
                path=account.object_path,
                interface=cs.ACCOUNT, signal='AccountPropertyChanged',
                args=[{'Parameters': {
                    'account': r'\\',
                    'password': 'secrecy',
                    'secret-mushroom': '/Amanita muscaria/',
                    'snakes': 42,
                    }}]),
            )
    not_yet = ret.value[0]
    assertEquals([], not_yet)

    # Unset contrived-example again; the spec decrees that “If the given
    # parameters were not, in fact, stored, […] the account manager MUST accept
    # this without error.”
    call_async(q, account, 'UpdateParameters', {}, ['contrived-example'])
    ret = q.expect('dbus-return', method='UpdateParameters')
    not_yet = ret.value[0]
    assertEquals([], not_yet)

    cache_dir = os.environ['XDG_CACHE_HOME']

    # Now that we're using GVariant-based storage, the backslashes aren't
    # escaped.
    assertEquals(r'\\',
            kwargs['fake_accounts_service'].accounts
            [account.object_path[len(cs.ACCOUNT_PATH_PREFIX):]]
            .params['account'])
    assertEquals(None,
            kwargs['fake_accounts_service'].accounts
            [account.object_path[len(cs.ACCOUNT_PATH_PREFIX):]]
            .untyped_params.get('account', None))

if __name__ == '__main__':
    exec_test(test, {}, pass_kwargs=True)