2019-07-23 17:28:19 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
|
|
|
|
localmodule = os.path.realpath(
|
|
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
|
|
|
|
print('localmodule: ' + localmodule)
|
|
|
|
if localmodule not in sys.path:
|
|
|
|
sys.path.insert(0, localmodule)
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.basedir = os.path.join(localmodule, 'tests')
|
|
|
|
self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles'))
|
|
|
|
if not os.path.exists(self.tmpdir):
|
|
|
|
os.makedirs(self.tmpdir)
|
|
|
|
os.chdir(self.basedir)
|
|
|
|
|
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
|
|
|
|
|
|
|
with mock.patch('fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)):
|
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
|
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
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
|
|
|
build = app['Builds'][-1]
|
2020-01-02 14:27:12 +01:00
|
|
|
self.assertEqual(build.versionName, '1.1.9')
|
|
|
|
self.assertEqual(build.commit, '1.1.9')
|
|
|
|
|
|
|
|
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'
|
|
|
|
app.AutoUpdateMode = 'Version +.%c-fdroid v%v_%c'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
with mock.patch('fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)):
|
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()):
|
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
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
|
|
|
build = app['Builds'][-1]
|
2020-01-02 14:27:12 +01:00
|
|
|
self.assertEqual(build.versionName, '1.1.9.10109-fdroid')
|
|
|
|
self.assertEqual(build.commit, 'v1.1.9_10109')
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
with mock.patch('fdroidserver.checkupdates.check_http', lambda app: (None, 'bla')):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
|
|
|
|
|
|
|
with mock.patch('fdroidserver.checkupdates.check_http', lambda app: ('1.1.9', 10109)):
|
|
|
|
with mock.patch('fdroidserver.metadata.write_metadata', mock.Mock()) as wrmock:
|
|
|
|
with mock.patch('subprocess.call', lambda cmd: 0):
|
|
|
|
fdroidserver.checkupdates.checkupdates_app(app)
|
|
|
|
wrmock.assert_called_with(app.metadatapath, app)
|
|
|
|
|
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'
|
|
|
|
app.UpdateCheckData = '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)
|
|
|
|
self.assertEqual(vername, '1.1.9')
|
|
|
|
self.assertEqual(vercode, '10109')
|
|
|
|
|
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'
|
|
|
|
vername, vercode = fdroidserver.checkupdates.check_http(app)
|
|
|
|
self.assertIsNone(vername)
|
|
|
|
self.assertTrue(FDroidException.__name__ in vercode)
|
|
|
|
|
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'
|
|
|
|
app.UpdateCheckData = 'https://a.net/b.txt|c(.*)|https://d.net/e.txt|v(.*)'
|
|
|
|
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)
|
|
|
|
self.assertEqual(vername, None)
|
|
|
|
self.assertEqual(vercode, 'Version 1.1.9-beta is ignored')
|
|
|
|
|
2019-07-23 17:28:19 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("-v", "--verbose", action="store_true", default=False,
|
|
|
|
help="Spew out even more information than normal")
|
|
|
|
(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)
|