2019-07-23 17:28:19 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
2021-06-07 15:02:03 +02:00
|
|
|
from pathlib import Path
|
2019-07-23 17:28:19 +02:00
|
|
|
|
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
localmodule = Path(__file__).resolve().parent.parent
|
|
|
|
print('localmodule: ' + str(localmodule))
|
2019-07-23 17:28:19 +02:00
|
|
|
if localmodule not in sys.path:
|
2021-06-07 17:27:30 +02:00
|
|
|
sys.path.insert(0, str(localmodule))
|
2019-07-23 17:28:19 +02:00
|
|
|
|
|
|
|
import fdroidserver.checkupdates
|
|
|
|
import fdroidserver.metadata
|
2020-01-31 15:20:24 +01:00
|
|
|
from fdroidserver.exception import FDroidException
|
2019-07-23 17:28:19 +02:00
|
|
|
|
|
|
|
|
2020-01-31 15:23:16 +01:00
|
|
|
class CheckupdatesTest(unittest.TestCase):
|
|
|
|
'''fdroidserver/checkupdates.py'''
|
2019-07-23 17:28:19 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2021-06-07 15:02:03 +02:00
|
|
|
self.basedir = localmodule / 'tests'
|
|
|
|
self.tmpdir = localmodule / '.testfiles'
|
|
|
|
self.tmpdir.mkdir(exist_ok=True)
|
|
|
|
# TODO: Python3.6: Accepts a path-like object.
|
|
|
|
os.chdir(str(self.basedir))
|
2019-07-23 17:28:19 +02:00
|
|
|
|
2020-01-02 14:27:12 +01:00
|
|
|
def test_autoupdatemode_no_suffix(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
fdroidserver.checkupdates.options.auto = 'bleh'
|
|
|
|
fdroidserver.checkupdates.config = {}
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.CurrentVersion = '1.1.8-fdroid'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'HTTP'
|
|
|
|
app.AutoUpdateMode = 'Version %v'
|
|
|
|
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
build.versionCode = app.CurrentVersionCode
|
|
|
|
build.versionName = app.CurrentVersion
|
eliminate app.builds everywhere, it should be app['Builds']
The .txt format was the last place where the lowercase "builds" was used,
this converts references everywhere to be "Builds". This makes it possible
to load metadata YAML files with any YAML parser, then have it possible to
use fdroidserver methods on that data, like metadata.write_metadata().
The test files in tests/metadata/dump/*.yaml were manually edited by cutting
the builds: block and putting it the sort order for Builds: so the contents
should be unchanged.
```
sed -i \
-e 's/app\.builds/app.get('Builds', \[\])/g' \
-e "s/app\.get(Builds, \[\]) =/app\['Builds'] =/g" \
-e "s/app\.get(Builds, \[\]) =/app\['Builds'] =/g" \
-e "s/app\.get(Builds, \[\])/app.get('Builds', \[\])/g" \
-e "s/app\.get('Builds', \[\])\.append/app\['Builds'\].append/g" \
-e "s/app\['builds'\]/app.get('Builds', [])/g" \
*/*.*
```
2020-12-09 16:01:21 +01:00
|
|
|
app['Builds'].append(build)
|
2020-01-02 14:27:12 +01:00
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)
|
|
|
|
):
|
2020-01-02 14:27:12 +01:00
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
|
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
2022-10-15 10:09:19 +02:00
|
|
|
|
|
|
|
build = app['Builds'][-1]
|
|
|
|
self.assertEqual(build.versionName, '1.1.9')
|
|
|
|
self.assertEqual(build.commit, '1.1.9')
|
2020-01-02 14:27:12 +01:00
|
|
|
|
2021-06-15 20:53:05 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_http', lambda app: ('1.7.9', 10107)
|
|
|
|
):
|
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
|
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
2021-07-27 21:06:34 +02:00
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
2022-10-15 10:09:19 +02:00
|
|
|
|
|
|
|
build = app['Builds'][-1]
|
|
|
|
self.assertEqual(build.versionName, '1.1.9')
|
|
|
|
self.assertEqual(build.commit, '1.1.9')
|
2021-06-15 20:53:05 +02:00
|
|
|
|
2020-01-02 14:27:12 +01:00
|
|
|
def test_autoupdatemode_suffix(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
fdroidserver.checkupdates.options.auto = 'bleh'
|
|
|
|
fdroidserver.checkupdates.config = {}
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.CurrentVersion = '1.1.8-fdroid'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'HTTP'
|
2021-06-07 15:02:03 +02:00
|
|
|
app.AutoUpdateMode = r'Version +.%c-fdroid v%v_%c'
|
2020-01-02 14:27:12 +01:00
|
|
|
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
build.versionCode = app.CurrentVersionCode
|
|
|
|
build.versionName = app.CurrentVersion
|
eliminate app.builds everywhere, it should be app['Builds']
The .txt format was the last place where the lowercase "builds" was used,
this converts references everywhere to be "Builds". This makes it possible
to load metadata YAML files with any YAML parser, then have it possible to
use fdroidserver methods on that data, like metadata.write_metadata().
The test files in tests/metadata/dump/*.yaml were manually edited by cutting
the builds: block and putting it the sort order for Builds: so the contents
should be unchanged.
```
sed -i \
-e 's/app\.builds/app.get('Builds', \[\])/g' \
-e "s/app\.get(Builds, \[\]) =/app\['Builds'] =/g" \
-e "s/app\.get(Builds, \[\]) =/app\['Builds'] =/g" \
-e "s/app\.get(Builds, \[\])/app.get('Builds', \[\])/g" \
-e "s/app\.get('Builds', \[\])\.append/app\['Builds'\].append/g" \
-e "s/app\['builds'\]/app.get('Builds', [])/g" \
*/*.*
```
2020-12-09 16:01:21 +01:00
|
|
|
app['Builds'].append(build)
|
2020-01-02 14:27:12 +01:00
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)
|
|
|
|
):
|
2020-01-02 14:27:12 +01:00
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
|
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
2022-10-15 10:09:19 +02:00
|
|
|
|
|
|
|
build = app['Builds'][-1]
|
|
|
|
self.assertEqual(build.versionName, '1.1.9.10109-fdroid')
|
|
|
|
self.assertEqual(build.commit, 'v1.1.9_10109')
|
2020-01-02 14:27:12 +01:00
|
|
|
|
2019-07-23 17:28:19 +02:00
|
|
|
def test_checkupdates_app_http(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
fdroidserver.checkupdates.options.auto = 'bleh'
|
|
|
|
fdroidserver.checkupdates.config = {}
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'HTTP'
|
|
|
|
app.UpdateCheckData = 'mock'
|
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_http', lambda app: (None, 'bla')
|
|
|
|
):
|
2021-07-29 11:52:22 +02:00
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
2019-07-23 17:28:19 +02:00
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)
|
|
|
|
):
|
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.metadata.write_metadata', mock.Mock()
|
|
|
|
) as wrmock:
|
2019-07-23 17:28:19 +02:00
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
|
|
|
wrmock.assert_called_with(app.metadatapath, app)
|
|
|
|
|
2021-05-30 11:12:42 +02:00
|
|
|
def test_checkupdates_app_tags(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
fdroidserver.checkupdates.options.auto = 'bleh'
|
|
|
|
fdroidserver.checkupdates.config = {}
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.CurrentVersion = '1.1.8'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'Tags'
|
|
|
|
app.AutoUpdateMode = 'Version'
|
|
|
|
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
build.versionCode = app.CurrentVersionCode
|
|
|
|
build.versionName = app.CurrentVersion
|
|
|
|
app['Builds'].append(build)
|
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_tags',
|
|
|
|
lambda app, pattern: (None, 'bla', None),
|
|
|
|
):
|
2021-07-29 11:52:22 +02:00
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
2021-05-30 11:12:42 +02:00
|
|
|
|
2021-06-07 15:02:03 +02:00
|
|
|
with mock.patch(
|
|
|
|
'fdroidserver.checkupdates.check_tags',
|
|
|
|
lambda app, pattern: ('1.1.9', 10109, 'v1.1.9'),
|
|
|
|
):
|
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
|
2021-05-30 11:12:42 +02:00
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
2022-10-15 10:09:19 +02:00
|
|
|
|
|
|
|
build = app['Builds'][-1]
|
|
|
|
self.assertEqual(build.versionName, '1.1.9')
|
|
|
|
self.assertEqual(build.commit, 'v1.1.9')
|
2021-05-30 11:12:42 +02:00
|
|
|
|
2019-07-23 20:45:04 +02:00
|
|
|
def test_check_http(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'HTTP'
|
2021-06-09 10:08:33 +02:00
|
|
|
app.UpdateCheckData = r'https://a.net/b.txt|c(.*)|https://d.net/e.txt|v(.*)'
|
2019-07-23 20:51:51 +02:00
|
|
|
app.UpdateCheckIgnore = 'beta'
|
2019-07-23 20:45:04 +02:00
|
|
|
|
|
|
|
respmock = mock.Mock()
|
|
|
|
respmock.read = lambda: 'v1.1.9\nc10109'.encode('utf-8')
|
|
|
|
with mock.patch('urllib.request.urlopen', lambda a, b, c: respmock):
|
|
|
|
vername, vercode = fdroidserver.checkupdates.check_http(app)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '1.1.9')
|
|
|
|
self.assertEqual(vercode, 10109)
|
2019-07-23 20:45:04 +02:00
|
|
|
|
2020-01-31 15:20:24 +01:00
|
|
|
def test_check_http_blocks_unknown_schemes(self):
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
for scheme in ('file', 'ssh', 'http', ';pwn'):
|
|
|
|
app.id = scheme
|
|
|
|
faked = scheme + '://fake.url/for/testing/scheme'
|
|
|
|
app.UpdateCheckData = faked + '|ignored|' + faked + '|ignored'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
2021-07-27 21:54:52 +02:00
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
fdroidserver.checkupdates.check_http(app)
|
2020-01-31 15:20:24 +01:00
|
|
|
|
2019-07-23 20:51:51 +02:00
|
|
|
def test_check_http_ignore(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'HTTP'
|
2021-06-09 10:08:33 +02:00
|
|
|
app.UpdateCheckData = r'https://a.net/b.txt|c(.*)|https://d.net/e.txt|v(.*)'
|
2019-07-23 20:51:51 +02:00
|
|
|
app.UpdateCheckIgnore = 'beta'
|
|
|
|
|
|
|
|
respmock = mock.Mock()
|
|
|
|
respmock.read = lambda: 'v1.1.9-beta\nc10109'.encode('utf-8')
|
|
|
|
with mock.patch('urllib.request.urlopen', lambda a, b, c: respmock):
|
|
|
|
vername, vercode = fdroidserver.checkupdates.check_http(app)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, None)
|
2019-07-23 20:51:51 +02:00
|
|
|
|
2021-05-18 10:21:05 +02:00
|
|
|
def test_check_tags_data(self):
|
|
|
|
fdroidserver.checkupdates.options = mock.Mock()
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'loop.starts.shooting'
|
|
|
|
app.metadatapath = 'metadata/' + app.id + '.yml'
|
|
|
|
app.RepoType = 'git'
|
|
|
|
app.CurrentVersionCode = 10108
|
|
|
|
app.UpdateCheckMode = 'Tags'
|
2021-06-09 10:08:33 +02:00
|
|
|
app.UpdateCheckData = r'b.txt|c(.*)|e.txt|v(.*)'
|
2021-05-18 10:21:05 +02:00
|
|
|
|
|
|
|
vcs = mock.Mock()
|
2021-06-09 10:08:33 +02:00
|
|
|
vcs.latesttags.return_value = ['1.1.9', '1.1.8']
|
2021-05-18 10:21:05 +02:00
|
|
|
with mock.patch(
|
2021-06-07 15:02:03 +02:00
|
|
|
'pathlib.Path.read_text', lambda a: 'v1.1.9\nc10109'
|
2021-06-09 08:19:53 +02:00
|
|
|
) as _ignored, mock.patch.object(
|
|
|
|
Path, 'is_file'
|
|
|
|
) as mock_path, mock.patch('fdroidserver.common.getvcs', return_value=vcs):
|
2021-05-18 10:21:05 +02:00
|
|
|
_ignored # silence the linters
|
2021-06-09 08:19:53 +02:00
|
|
|
mock_path.is_file.return_falue = True
|
2021-06-07 15:02:03 +02:00
|
|
|
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '1.1.9')
|
|
|
|
self.assertEqual(vercode, 10109)
|
2021-05-18 10:21:05 +02:00
|
|
|
|
2021-06-09 10:08:33 +02:00
|
|
|
app.UpdateCheckData = r'b.txt|c(.*)|.|v(.*)'
|
|
|
|
with mock.patch(
|
|
|
|
'pathlib.Path.read_text', lambda a: 'v1.1.0\nc10109'
|
|
|
|
) as _ignored, mock.patch.object(
|
|
|
|
Path, 'is_file'
|
|
|
|
) as mock_path, mock.patch('fdroidserver.common.getvcs', return_value=vcs):
|
|
|
|
_ignored # silence the linters
|
|
|
|
mock_path.is_file.return_falue = True
|
|
|
|
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '1.1.0')
|
|
|
|
self.assertEqual(vercode, 10109)
|
2021-06-09 10:08:33 +02:00
|
|
|
|
|
|
|
app.UpdateCheckData = r'b.txt|c(.*)||'
|
|
|
|
with mock.patch(
|
|
|
|
'pathlib.Path.read_text', lambda a: 'v1.1.9\nc10109'
|
|
|
|
) as _ignored, mock.patch.object(
|
|
|
|
Path, 'is_file'
|
|
|
|
) as mock_path, mock.patch('fdroidserver.common.getvcs', return_value=vcs):
|
|
|
|
_ignored # silence the linters
|
|
|
|
mock_path.is_file.return_falue = True
|
|
|
|
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '1.1.9')
|
|
|
|
self.assertEqual(vercode, 10109)
|
2021-06-09 10:08:33 +02:00
|
|
|
|
|
|
|
vcs.latesttags.return_value = ['Android-1.1.0', '1.1.8']
|
|
|
|
app.UpdateCheckData = r'b.txt|c(.*)||Android-([\d.]+)'
|
|
|
|
with mock.patch(
|
|
|
|
'pathlib.Path.read_text', lambda a: 'v1.1.9\nc10109'
|
|
|
|
) as _ignored, mock.patch.object(
|
|
|
|
Path, 'is_file'
|
|
|
|
) as mock_path, mock.patch('fdroidserver.common.getvcs', return_value=vcs):
|
|
|
|
_ignored # silence the linters
|
|
|
|
mock_path.is_file.return_falue = True
|
|
|
|
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '1.1.0')
|
|
|
|
self.assertEqual(vercode, 10109)
|
2021-06-09 10:08:33 +02:00
|
|
|
|
|
|
|
app.UpdateCheckData = r'|\+(\d+)||Android-([\d.]+)'
|
|
|
|
vcs.latesttags.return_value = ['Android-1.1.0+1']
|
|
|
|
with mock.patch('fdroidserver.common.getvcs', return_value=vcs):
|
|
|
|
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '1.1.0')
|
|
|
|
self.assertEqual(vercode, 1)
|
2021-06-09 10:08:33 +02:00
|
|
|
|
2021-06-15 19:18:19 +02:00
|
|
|
app.UpdateCheckData = '|||'
|
|
|
|
vcs.latesttags.return_value = ['2']
|
|
|
|
with mock.patch('fdroidserver.common.getvcs', return_value=vcs):
|
|
|
|
vername, vercode, _tag = fdroidserver.checkupdates.check_tags(app, None)
|
2022-10-15 10:09:19 +02:00
|
|
|
self.assertEqual(vername, '2')
|
|
|
|
self.assertEqual(vercode, 2)
|
2021-06-15 19:18:19 +02:00
|
|
|
|
2019-07-23 17:28:19 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = optparse.OptionParser()
|
2021-06-07 11:49:21 +02:00
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
2019-07-23 17:28:19 +02:00
|
|
|
(fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
2020-01-31 15:23:16 +01:00
|
|
|
newSuite.addTest(unittest.makeSuite(CheckupdatesTest))
|
2019-07-23 17:28:19 +02:00
|
|
|
unittest.main(failfast=False)
|