File: test_install.py

package info (click to toggle)
fdroidserver 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,948 kB
  • sloc: python: 34,139; xml: 2,186; sh: 1,362; java: 293; makefile: 54; javascript: 23
file content (257 lines) | stat: -rwxr-xr-x 10,281 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import os
import textwrap
import unittest

from pathlib import Path
from unittest.mock import Mock, patch

import fdroidserver
from fdroidserver import common, install
from fdroidserver.exception import BuildException, FDroidException


@unittest.skipIf(os.uname().machine == 's390x', 'adb is not ported to s390x')
class InstallTest(unittest.TestCase):
    '''fdroidserver/install.py'''

    def tearDown(self):
        common.config = None

    def test_devices(self):
        config = dict()
        fdroidserver.common.fill_config_defaults(config)
        fdroidserver.common.config = config
        try:
            config['adb'] = fdroidserver.common.find_sdk_tools_cmd('adb')
        except FDroidException as e:
            self.skipTest(f'Skipping test because: {e}')
        self.assertTrue(os.path.exists(config['adb']))
        self.assertTrue(os.path.isfile(config['adb']))
        devices = fdroidserver.install.devices()
        self.assertIsInstance(devices, list, 'install.devices() did not return a list!')
        for device in devices:
            self.assertIsInstance(device, str)

    def test_devices_fail(self):
        common.config = dict()
        common.fill_config_defaults(common.config)
        common.config['adb'] = '/bin/false'
        with self.assertRaises(FDroidException):
            fdroidserver.install.devices()

    def test_devices_fail_nonexistent(self):
        """This is mostly just to document this strange difference in behavior"""
        common.config = dict()
        common.fill_config_defaults(common.config)
        common.config['adb'] = '/nonexistent'
        with self.assertRaises(BuildException):
            fdroidserver.install.devices()

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_none(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = 'List of devices attached\n\n'
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual([], fdroidserver.install.devices())

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_one(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = 'List of devices attached\n05995813\tdevice\n\n'
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual(['05995813'], fdroidserver.install.devices())

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_many(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = textwrap.dedent(
            """* daemon not running; starting now at tcp:5037
            * daemon started successfully
            List of devices attached
            RZCT809FTQM	device
            05995813	device
            emulator-5556	device
            emulator-5554	unauthorized
            0a388e93	no permissions (missing udev rules? user is in the plugdev group); see [http://developer.android.com/tools/device.html]
            986AY133QL	device
            09301JEC215064	device
            015d165c3010200e	device
            4DCESKVGUC85VOTO	device

            """
        )
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual(
            [
                'RZCT809FTQM',
                '05995813',
                'emulator-5556',
                '986AY133QL',
                '09301JEC215064',
                '015d165c3010200e',
                '4DCESKVGUC85VOTO',
            ],
            fdroidserver.install.devices(),
        )

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_error(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = textwrap.dedent(
            """* daemon not running. starting it now on port 5037 *
            * daemon started successfully *
            ** daemon still not running
            error: cannot connect to daemon
            """
        )
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual([], fdroidserver.install.devices())

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_no_permissions(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = textwrap.dedent(
            """List of devices attached
            ????????????????	no permissions
            """
        )
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual([], fdroidserver.install.devices())

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_unauthorized(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = textwrap.dedent(
            """List of devices attached
            aeef5e4e	unauthorized
            """
        )
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual([], fdroidserver.install.devices())

    @patch('fdroidserver.common.SdkToolsPopen')
    def test_devices_with_mock_no_permissions_with_serial(self, mock_SdkToolsPopen):
        p = Mock()
        mock_SdkToolsPopen.return_value = p
        p.output = textwrap.dedent(
            """List of devices attached
             4DCESKVGUC85VOTO	no permissions (missing udev rules? user is in the plugdev group); see [http://developer.android.com/tools/device.html]

            """
        )
        p.returncode = 0
        common.config = dict()
        common.fill_config_defaults(common.config)
        self.assertEqual([], fdroidserver.install.devices())

    @staticmethod
    def _download_raise(privacy_mode):
        raise Exception('fake failed download')

    @patch('fdroidserver.install.download_apk')
    @patch('fdroidserver.install.download_fdroid_apk')
    @patch('fdroidserver.install.download_fdroid_apk_from_github')
    @patch('fdroidserver.install.download_fdroid_apk_from_ipns')
    @patch('fdroidserver.install.download_fdroid_apk_from_maven')
    def test_install_fdroid_apk_privacy_mode_true(
        self, maven, ipns, github, download_fdroid_apk, download_apk
    ):
        download_apk.side_effect = self._download_raise
        download_fdroid_apk.side_effect = self._download_raise
        github.side_effect = self._download_raise
        ipns.side_effect = self._download_raise
        maven.side_effect = self._download_raise
        fdroidserver.common.config = {'jarsigner': 'fakepath'}
        install.install_fdroid_apk(privacy_mode=True)
        download_apk.assert_not_called()
        download_fdroid_apk.assert_not_called()
        github.assert_called_once()
        ipns.assert_called_once()
        maven.assert_called_once()

    @patch('fdroidserver.install.download_apk')
    @patch('fdroidserver.install.download_fdroid_apk')
    @patch('fdroidserver.install.download_fdroid_apk_from_github')
    @patch('fdroidserver.install.download_fdroid_apk_from_ipns')
    @patch('fdroidserver.install.download_fdroid_apk_from_maven')
    def test_install_fdroid_apk_privacy_mode_false(
        self, maven, ipns, github, download_fdroid_apk, download_apk
    ):
        download_apk.side_effect = self._download_raise
        download_fdroid_apk.side_effect = self._download_raise
        github.side_effect = self._download_raise
        ipns.side_effect = self._download_raise
        maven.side_effect = self._download_raise
        fdroidserver.common.config = {'jarsigner': 'fakepath'}
        install.install_fdroid_apk(privacy_mode=False)
        download_apk.assert_called_once()
        download_fdroid_apk.assert_called_once()
        github.assert_called_once()
        ipns.assert_called_once()
        maven.assert_called_once()

    @patch('fdroidserver.install.download_apk')
    @patch('fdroidserver.install.download_fdroid_apk')
    @patch('fdroidserver.install.download_fdroid_apk_from_github')
    @patch('fdroidserver.install.download_fdroid_apk_from_ipns')
    @patch('fdroidserver.install.download_fdroid_apk_from_maven')
    @patch('locale.getlocale', lambda: ('zh_CN', 'UTF-8'))
    def test_install_fdroid_apk_privacy_mode_locale_auto(
        self, maven, ipns, github, download_fdroid_apk, download_apk
    ):
        download_apk.side_effect = self._download_raise
        download_fdroid_apk.side_effect = self._download_raise
        github.side_effect = self._download_raise
        ipns.side_effect = self._download_raise
        maven.side_effect = self._download_raise
        fdroidserver.common.config = {'jarsigner': 'fakepath'}
        install.install_fdroid_apk(privacy_mode=None)
        download_apk.assert_not_called()
        download_fdroid_apk.assert_not_called()
        github.assert_called_once()
        ipns.assert_called_once()
        maven.assert_called_once()

    @patch('fdroidserver.net.download_using_mirrors', lambda m: 'testvalue')
    def test_download_fdroid_apk_smokecheck(self):
        self.assertEqual('testvalue', install.download_fdroid_apk())

    @unittest.skipUnless(os.getenv('test_download_fdroid_apk'), 'requires net access')
    def test_download_fdroid_apk(self):
        f = install.download_fdroid_apk()
        self.assertTrue(Path(f).exists())

    @unittest.skipUnless(os.getenv('test_download_fdroid_apk'), 'requires net access')
    def test_download_fdroid_apk_from_maven(self):
        f = install.download_fdroid_apk_from_maven()
        self.assertTrue(Path(f).exists())

    @unittest.skipUnless(os.getenv('test_download_fdroid_apk'), 'requires net access')
    def test_download_fdroid_apk_from_ipns(self):
        f = install.download_fdroid_apk_from_ipns()
        self.assertTrue(Path(f).exists())

    @unittest.skipUnless(os.getenv('test_download_fdroid_apk'), 'requires net access')
    def test_download_fdroid_apk_from_github(self):
        f = install.download_fdroid_apk_from_github()
        self.assertTrue(Path(f).exists())