summaryrefslogtreecommitdiff
path: root/tests/twisted/account-storage/storage_helper.py
blob: ebbe78e6dc198d5323d1598fb2941d475190efda (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
# Helper code for former default account storage backends
#
# Copyright (C) 2009-2010 Nokia Corporation
# Copyright (C) 2009-2014 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 errno
import os
import os.path

import dbus

from servicetest import (
    assertEquals, assertContains, assertLength,
    )
from mctest import (
    exec_test, get_fakecm_account, connect_to_mc,
    MC, SimulatedConnectionManager,
    )
import constants as cs

# This doesn't escape its parameters before passing them to the shell,
# so be careful.
def account_store(op, backend, key=None, value=None,
        account='fakecm/fakeprotocol/dontdivert_40example_2ecom0'):
    cmd = [ '../account-store', op, backend, account ]
    if key:
        cmd.append(key)
        if value:
            cmd.append(value)

    lines = os.popen(' '.join(cmd)).read()
    ret = []
    for line in lines.split('\n'):
        if line.startswith('** '):
            continue

        if line:
            ret.append(line)

    if len(ret) > 0:
        return ret[0]
    else:
        return None

def test_keyfile(q, bus, mc, how_old='5.12'):
    simulated_cm = SimulatedConnectionManager(q, bus)

    if how_old == '5.12':
        # This is not actually ~/.mission-control, but it uses the same
        # code paths.
        dot_mission_control = os.environ['MC_ACCOUNT_DIR']
        old_key_file_name = os.path.join(dot_mission_control, 'accounts.cfg')

        os.makedirs(dot_mission_control +
            '/fakecm/fakeprotocol/dontdivert1_40example_2ecom0')
        avatar_bin = open(dot_mission_control +
            '/fakecm/fakeprotocol/dontdivert1_40example_2ecom0/avatar.bin',
            'w')
        avatar_bin.write('hello, world')
        avatar_bin.close()
    elif how_old == '5.14':
        # Same format, different location.
        old_key_file_name = os.path.join(os.environ['XDG_DATA_HOME'],
                'telepathy', 'mission-control', 'accounts.cfg')

        # exercise override of an avatar in XDG_DATA_DIRS
        avatar_dir = (os.environ['XDG_DATA_DIRS'].split(':')[0] +
            '/telepathy/mission-control')
        os.makedirs(avatar_dir)
        avatar_bin = open(avatar_dir +
            '/fakecm-fakeprotocol-dontdivert1_40example_2ecom0.avatar',
            'w')
        avatar_bin.write('hello, world')
        avatar_bin.close()
    else:
        raise AssertionError('Unsupported value for how_old')

    a1_new_variant_file_name = os.path.join(os.environ['XDG_DATA_HOME'],
            'telepathy', 'mission-control',
            'fakecm-fakeprotocol-dontdivert1_40example_2ecom0.account')
    a1_tail = 'fakecm/fakeprotocol/dontdivert1_40example_2ecom0'

    a2_new_variant_file_name = os.path.join(os.environ['XDG_DATA_HOME'],
            'telepathy', 'mission-control',
            'fakecm-fakeprotocol-dontdivert2_40example_2ecom0.account')
    a2_tail = 'fakecm/fakeprotocol/dontdivert2_40example_2ecom0'

    try:
        os.makedirs(os.path.dirname(old_key_file_name), 0700)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    open(old_key_file_name, 'w').write(
r"""# Telepathy accounts
[%s]
manager=fakecm
protocol=fakeprotocol
param-account=dontdivert1@example.com
param-password=1
DisplayName=First among equals
AutomaticPresence=2;available;;
AvatarMime=text/plain
avatar_token=hello, world

[%s]
manager=fakecm
protocol=fakeprotocol
param-account=dontdivert2@example.com
param-password=2
DisplayName=Second to none
AutomaticPresence=2;available;;
""" % (a1_tail, a2_tail))

    mc = MC(q, bus)
    account_manager, properties, interfaces = connect_to_mc(q, bus, mc)

    # During MC startup, it moved the old keyfile's contents into
    # variant-based files, and deleted the old keyfile.
    assert not os.path.exists(old_key_file_name)
    assert os.path.exists(a1_new_variant_file_name)
    assert os.path.exists(a2_new_variant_file_name)
    assertEquals("'First among equals'",
            account_store('get', 'variant-file', 'DisplayName',
                account=a1_tail))
    assertEquals("'Second to none'",
            account_store('get', 'variant-file', 'DisplayName',
                account=a2_tail))
    # MC doesn't currently ensure that parameters are stored with their
    # proper types.
    assertEquals("keyfile-escaped 'dontdivert1@example.com'",
            account_store('get', 'variant-file', 'param-account',
                account=a1_tail))
    assertEquals("keyfile-escaped 'dontdivert2@example.com'",
            account_store('get', 'variant-file', 'param-account',
                account=a2_tail))

    # Also, MC has both accounts in memory...
    assertContains(cs.ACCOUNT_PATH_PREFIX + a1_tail,
            properties['ValidAccounts'])
    account = get_fakecm_account(bus, mc, cs.ACCOUNT_PATH_PREFIX + a1_tail)
    assertEquals('dontdivert1@example.com',
            account.Properties.Get(cs.ACCOUNT, 'Parameters')['account'])
    assertEquals('First among equals',
            account.Properties.Get(cs.ACCOUNT, 'DisplayName'))
    assertEquals((dbus.ByteArray('hello, world'), 'text/plain'),
            account.Properties.Get(cs.ACCOUNT_IFACE_AVATAR, 'Avatar',
                byte_arrays=True))

    assertContains(cs.ACCOUNT_PATH_PREFIX + a2_tail,
            properties['ValidAccounts'])
    account = get_fakecm_account(bus, mc, cs.ACCOUNT_PATH_PREFIX + a2_tail)
    assertEquals('dontdivert2@example.com',
            account.Properties.Get(cs.ACCOUNT, 'Parameters')['account'])
    assertEquals('Second to none',
            account.Properties.Get(cs.ACCOUNT, 'DisplayName'))

    # ... and no other accounts.
    assertLength(2, properties['ValidAccounts'])