2017-03-29 23:33:09 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-04-03 19:19:37 +02:00
|
|
|
|
|
|
|
import inspect
|
2017-03-29 23:33:09 +02:00
|
|
|
import optparse
|
|
|
|
import os
|
2017-04-03 19:19:37 +02:00
|
|
|
import sys
|
2017-03-29 23:33:09 +02:00
|
|
|
import unittest
|
|
|
|
import zipfile
|
2017-05-02 17:05:48 +02:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import requests
|
2017-03-29 23:33:09 +02:00
|
|
|
|
2017-04-03 19:19:37 +02:00
|
|
|
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)
|
|
|
|
|
2017-03-29 23:33:09 +02:00
|
|
|
import fdroidserver.common
|
|
|
|
import fdroidserver.index
|
|
|
|
import fdroidserver.signindex
|
|
|
|
|
|
|
|
|
2017-05-02 17:05:48 +02:00
|
|
|
GP_FINGERPRINT = 'B7C2EEFD8DAC7806AF67DFCD92EB18126BC08312A7F2D6F3862E46013C7A6135'
|
|
|
|
|
|
|
|
|
2017-03-29 23:33:09 +02:00
|
|
|
class IndexTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
fdroidserver.common.config = None
|
|
|
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
|
|
|
config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner')
|
|
|
|
fdroidserver.common.config = config
|
|
|
|
fdroidserver.signindex.config = config
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def test_verify_jar_signature_succeeds():
|
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
source_dir = os.path.join(basedir, 'signindex')
|
|
|
|
for f in ('testy.jar', 'guardianproject.jar'):
|
|
|
|
testfile = os.path.join(source_dir, f)
|
|
|
|
fdroidserver.index.verify_jar_signature(testfile)
|
|
|
|
|
|
|
|
def test_verify_jar_signature_fails(self):
|
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
source_dir = os.path.join(basedir, 'signindex')
|
|
|
|
testfile = os.path.join(source_dir, 'unsigned.jar')
|
|
|
|
with self.assertRaises(fdroidserver.index.VerificationException):
|
|
|
|
fdroidserver.index.verify_jar_signature(testfile)
|
|
|
|
|
|
|
|
def test_get_public_key_from_jar_succeeds(self):
|
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
source_dir = os.path.join(basedir, 'signindex')
|
|
|
|
for f in ('testy.jar', 'guardianproject.jar'):
|
|
|
|
testfile = os.path.join(source_dir, f)
|
|
|
|
jar = zipfile.ZipFile(testfile)
|
|
|
|
_, fingerprint = fdroidserver.index.get_public_key_from_jar(jar)
|
|
|
|
# comparing fingerprints should be sufficient
|
|
|
|
if f == 'testy.jar':
|
|
|
|
self.assertTrue(fingerprint ==
|
|
|
|
'818E469465F96B704E27BE2FEE4C63AB' +
|
|
|
|
'9F83DDF30E7A34C7371A4728D83B0BC1')
|
|
|
|
if f == 'guardianproject.jar':
|
2017-05-02 17:05:48 +02:00
|
|
|
self.assertTrue(fingerprint == GP_FINGERPRINT)
|
2017-03-29 23:33:09 +02:00
|
|
|
|
|
|
|
def test_get_public_key_from_jar_fails(self):
|
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
source_dir = os.path.join(basedir, 'signindex')
|
|
|
|
testfile = os.path.join(source_dir, 'unsigned.jar')
|
|
|
|
jar = zipfile.ZipFile(testfile)
|
|
|
|
with self.assertRaises(fdroidserver.index.VerificationException):
|
|
|
|
fdroidserver.index.get_public_key_from_jar(jar)
|
|
|
|
|
|
|
|
def test_download_repo_index_no_fingerprint(self):
|
|
|
|
with self.assertRaises(fdroidserver.index.VerificationException):
|
|
|
|
fdroidserver.index.download_repo_index("http://example.org")
|
|
|
|
|
|
|
|
def test_download_repo_index_no_jar(self):
|
2017-05-02 17:05:48 +02:00
|
|
|
with self.assertRaises(requests.exceptions.HTTPError):
|
2017-03-29 23:33:09 +02:00
|
|
|
fdroidserver.index.download_repo_index("http://example.org?fingerprint=nope")
|
|
|
|
|
2017-05-02 17:05:48 +02:00
|
|
|
@patch('requests.head')
|
|
|
|
def test_download_repo_index_same_etag(self, head):
|
|
|
|
url = 'http://example.org?fingerprint=test'
|
|
|
|
etag = '"4de5-54d840ce95cb9"'
|
|
|
|
|
|
|
|
head.return_value.headers = {'ETag': etag}
|
|
|
|
index, new_etag = fdroidserver.index.download_repo_index(url, etag=etag)
|
|
|
|
|
|
|
|
self.assertIsNone(index)
|
|
|
|
self.assertEqual(etag, new_etag)
|
|
|
|
|
|
|
|
@patch('requests.get')
|
|
|
|
@patch('requests.head')
|
|
|
|
def test_download_repo_index_new_etag(self, head, get):
|
|
|
|
url = 'http://example.org?fingerprint=' + GP_FINGERPRINT
|
|
|
|
etag = '"4de5-54d840ce95cb9"'
|
|
|
|
|
|
|
|
# fake HTTP answers
|
|
|
|
head.return_value.headers = {'ETag': 'new_etag'}
|
|
|
|
get.return_value.headers = {'ETag': 'new_etag'}
|
|
|
|
get.return_value.status_code = 200
|
|
|
|
testfile = os.path.join(os.path.dirname(__file__), 'signindex', 'guardianproject-v1.jar')
|
|
|
|
with open(testfile, 'rb') as file:
|
|
|
|
get.return_value.content = file.read()
|
|
|
|
|
|
|
|
index, new_etag = fdroidserver.index.download_repo_index(url, etag=etag)
|
|
|
|
|
|
|
|
# assert that the index was retrieved properly
|
|
|
|
self.assertEqual('Guardian Project Official Releases', index['repo']['name'])
|
|
|
|
self.assertEqual(GP_FINGERPRINT, index['repo']['fingerprint'])
|
|
|
|
self.assertTrue(len(index['repo']['pubkey']) > 500)
|
|
|
|
self.assertEqual(10, len(index['apps']))
|
|
|
|
self.assertEqual(10, len(index['packages']))
|
|
|
|
self.assertEqual('new_etag', new_etag)
|
2017-03-29 23:33:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
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()
|
|
|
|
newSuite.addTest(unittest.makeSuite(IndexTest))
|
|
|
|
unittest.main()
|