2017-12-15 00:29:38 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-12-19 22:51:03 +01:00
|
|
|
import glob
|
|
|
|
import inspect
|
|
|
|
import logging
|
2017-12-15 00:29:38 +01:00
|
|
|
import optparse
|
2017-12-19 22:51:03 +01:00
|
|
|
import os
|
2020-06-03 16:55:43 +02:00
|
|
|
import shutil
|
2017-12-19 22:51:03 +01:00
|
|
|
import sys
|
2020-05-14 16:08:56 +02:00
|
|
|
import tempfile
|
|
|
|
import textwrap
|
2017-12-19 22:51:03 +01:00
|
|
|
import unittest
|
2020-06-03 16:55:43 +02:00
|
|
|
import uuid
|
2020-06-03 23:40:01 +02:00
|
|
|
import yaml
|
2020-05-14 16:08:56 +02:00
|
|
|
from unittest import mock
|
2017-12-19 22:51:03 +01:00
|
|
|
|
|
|
|
localmodule = os.path.realpath(
|
2021-06-07 11:49:21 +02:00
|
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
|
|
|
|
)
|
2017-12-19 22:51:03 +01:00
|
|
|
print('localmodule: ' + localmodule)
|
|
|
|
if localmodule not in sys.path:
|
|
|
|
sys.path.insert(0, localmodule)
|
2017-12-15 00:29:38 +01:00
|
|
|
|
2020-05-14 16:08:56 +02:00
|
|
|
import fdroidserver.build
|
2017-12-15 00:29:38 +01:00
|
|
|
import fdroidserver.common
|
2017-12-19 22:51:03 +01:00
|
|
|
import fdroidserver.metadata
|
|
|
|
import fdroidserver.scanner
|
2017-12-15 00:29:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ScannerTest(unittest.TestCase):
|
2017-12-19 22:51:03 +01:00
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
self.basedir = os.path.join(localmodule, 'tests')
|
2020-05-14 16:08:56 +02:00
|
|
|
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)
|
2017-12-19 22:51:03 +01:00
|
|
|
|
2017-12-15 00:29:38 +01:00
|
|
|
def test_scan_source_files(self):
|
2020-06-03 18:34:26 +02:00
|
|
|
fdroidserver.scanner.options = mock.Mock()
|
2020-05-14 16:08:56 +02:00
|
|
|
fdroidserver.scanner.options.json = False
|
2017-12-19 22:51:03 +01:00
|
|
|
source_files = os.path.join(self.basedir, 'source-files')
|
2017-12-15 00:29:38 +01:00
|
|
|
projects = {
|
2020-01-31 23:49:50 +01:00
|
|
|
'cn.wildfirechat.chat': 4,
|
2020-02-03 21:13:26 +01:00
|
|
|
'com.integreight.onesheeld': 11,
|
2017-12-15 00:29:38 +01:00
|
|
|
'Zillode': 1,
|
2019-10-23 12:23:37 +02:00
|
|
|
'firebase-suspect': 1,
|
2019-11-02 19:54:54 +01:00
|
|
|
'org.mozilla.rocket': 3,
|
2019-10-23 12:23:37 +02:00
|
|
|
'realm': 1,
|
2020-02-13 10:22:24 +01:00
|
|
|
'se.manyver': 2,
|
2017-12-15 00:29:38 +01:00
|
|
|
}
|
2017-12-19 22:51:03 +01:00
|
|
|
for d in glob.glob(os.path.join(source_files, '*')):
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
fatal_problems = fdroidserver.scanner.scan_source(d, build)
|
2019-10-23 12:23:37 +02:00
|
|
|
should = projects.get(os.path.basename(d), 0)
|
2021-06-07 11:49:21 +02:00
|
|
|
self.assertEqual(
|
|
|
|
should, fatal_problems, "%s should have %d errors!" % (d, should)
|
|
|
|
)
|
2017-12-15 00:29:38 +01:00
|
|
|
|
2020-05-27 22:03:23 +02:00
|
|
|
def test_get_gradle_compile_commands(self):
|
|
|
|
test_files = [
|
|
|
|
('source-files/fdroid/fdroidclient/build.gradle', 'yes', 17),
|
|
|
|
('source-files/com.nextcloud.client/build.gradle', 'generic', 24),
|
|
|
|
('source-files/com.kunzisoft.testcase/build.gradle', 'libre', 4),
|
|
|
|
('source-files/cn.wildfirechat.chat/chat/build.gradle', 'yes', 33),
|
|
|
|
('source-files/org.tasks/app/build.gradle.kts', 'generic', 39),
|
|
|
|
('source-files/at.bitfire.davdroid/build.gradle', 'standard', 16),
|
|
|
|
('source-files/se.manyver/android/app/build.gradle', 'indie', 29),
|
|
|
|
('source-files/osmandapp/osmand/build.gradle', 'free', 5),
|
|
|
|
('source-files/eu.siacs.conversations/build.gradle', 'free', 23),
|
|
|
|
('source-files/org.mozilla.rocket/app/build.gradle', 'focus', 42),
|
|
|
|
]
|
|
|
|
|
|
|
|
for f, flavor, count in test_files:
|
|
|
|
i = 0
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
build.gradle = [flavor]
|
|
|
|
regexs = fdroidserver.scanner.get_gradle_compile_commands(build)
|
2021-06-14 15:38:37 +02:00
|
|
|
with open(f, encoding='utf-8') as fp:
|
2020-05-27 22:03:23 +02:00
|
|
|
for line in fp.readlines():
|
|
|
|
for regex in regexs:
|
|
|
|
m = regex.match(line)
|
|
|
|
if m:
|
|
|
|
i += 1
|
|
|
|
self.assertEqual(count, i)
|
|
|
|
|
2020-06-03 23:40:01 +02:00
|
|
|
def test_scan_source_files_sneaky_maven(self):
|
|
|
|
"""Check for sneaking in banned maven repos"""
|
2021-06-07 11:49:21 +02:00
|
|
|
testdir = tempfile.mkdtemp(
|
|
|
|
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
|
|
|
|
)
|
2020-06-03 23:40:01 +02:00
|
|
|
os.chdir(testdir)
|
|
|
|
fdroidserver.scanner.config = None
|
|
|
|
fdroidserver.scanner.options = mock.Mock()
|
|
|
|
fdroidserver.scanner.options.json = True
|
2021-06-14 15:38:37 +02:00
|
|
|
with open('build.gradle', 'w', encoding='utf-8') as fp:
|
2021-06-07 11:49:21 +02:00
|
|
|
fp.write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2020-06-03 23:40:01 +02:00
|
|
|
maven {
|
|
|
|
"https://jitpack.io"
|
|
|
|
url 'https://maven.fabric.io/public'
|
|
|
|
}
|
|
|
|
maven {
|
|
|
|
"https://maven.google.com"
|
|
|
|
setUrl('https://evilcorp.com/maven')
|
|
|
|
}
|
2021-06-07 11:49:21 +02:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2020-06-03 23:40:01 +02:00
|
|
|
count = fdroidserver.scanner.scan_source(testdir)
|
|
|
|
self.assertEqual(2, count, 'there should be this many errors')
|
|
|
|
|
2020-06-03 16:55:43 +02:00
|
|
|
def test_scan_source_file_types(self):
|
2020-06-17 10:33:55 +02:00
|
|
|
"""Build product files are not allowed, test they are detected
|
|
|
|
|
|
|
|
This test runs as if `fdroid build` running to test the
|
|
|
|
difference between absolute and relative paths.
|
|
|
|
|
|
|
|
"""
|
2021-06-07 11:49:21 +02:00
|
|
|
testdir = tempfile.mkdtemp(
|
|
|
|
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
|
|
|
|
)
|
2020-06-17 10:33:55 +02:00
|
|
|
build_dir = os.path.join('build', 'fake.app')
|
|
|
|
abs_build_dir = os.path.join(testdir, build_dir)
|
|
|
|
os.makedirs(abs_build_dir, exist_ok=True)
|
|
|
|
os.chdir(abs_build_dir)
|
2020-06-03 16:55:43 +02:00
|
|
|
|
|
|
|
fdroidserver.scanner.config = None
|
|
|
|
fdroidserver.scanner.options = mock.Mock()
|
|
|
|
fdroidserver.scanner.options.json = True
|
|
|
|
|
|
|
|
keep = [
|
|
|
|
'arg.jar',
|
|
|
|
'ascii.out',
|
|
|
|
'baz.so',
|
2020-06-03 21:26:03 +02:00
|
|
|
'classes.dex',
|
2020-06-03 16:55:43 +02:00
|
|
|
'sqlcipher.aar',
|
|
|
|
'static.a',
|
2020-06-03 21:26:03 +02:00
|
|
|
'src/test/resources/classes.dex',
|
2020-06-03 16:55:43 +02:00
|
|
|
]
|
2021-06-07 11:49:21 +02:00
|
|
|
remove = ['gradle-wrapper.jar', 'gradlew', 'gradlew.bat']
|
2020-06-03 21:26:03 +02:00
|
|
|
os.makedirs('src/test/resources', exist_ok=True)
|
2020-06-03 16:55:43 +02:00
|
|
|
for f in keep + remove:
|
|
|
|
with open(f, 'w') as fp:
|
|
|
|
fp.write('placeholder')
|
|
|
|
self.assertTrue(os.path.exists(f))
|
2021-06-07 11:49:21 +02:00
|
|
|
binaries = ['binary.out', 'fake.png', 'snippet.png']
|
2020-06-03 16:55:43 +02:00
|
|
|
with open('binary.out', 'wb') as fp:
|
|
|
|
fp.write(b'\x00\x00')
|
|
|
|
fp.write(uuid.uuid4().bytes)
|
|
|
|
shutil.copyfile('binary.out', 'fake.png')
|
|
|
|
os.chmod('fake.png', 0o755)
|
|
|
|
os.system('ls -l binary.out')
|
|
|
|
with open('snippet.png', 'wb') as fp:
|
2021-06-07 11:49:21 +02:00
|
|
|
fp.write(
|
|
|
|
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00'
|
|
|
|
b'\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sB'
|
|
|
|
b'IT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\tpHYs\x00\x00\n'
|
|
|
|
b'a\x00\x00\na\x01\xfc\xccJ%\x00\x00\x00\x19tEXtSoftware'
|
|
|
|
)
|
2020-06-03 16:55:43 +02:00
|
|
|
os.chmod('snippet.png', 0o755)
|
|
|
|
os.system('ls -l fake.png')
|
|
|
|
|
2020-06-17 10:33:55 +02:00
|
|
|
# run scanner as if from `fdroid build`
|
|
|
|
os.chdir(testdir)
|
|
|
|
count = fdroidserver.scanner.scan_source(build_dir)
|
2020-06-03 21:26:03 +02:00
|
|
|
self.assertEqual(6, count, 'there should be this many errors')
|
2020-06-17 10:33:55 +02:00
|
|
|
os.chdir(build_dir)
|
2020-06-03 16:55:43 +02:00
|
|
|
|
|
|
|
for f in keep + binaries:
|
|
|
|
self.assertTrue(os.path.exists(f), f + ' should still be there')
|
|
|
|
for f in remove:
|
|
|
|
self.assertFalse(os.path.exists(f), f + ' should have been removed')
|
|
|
|
|
|
|
|
files = dict()
|
|
|
|
for section in ('errors', 'infos', 'warnings'):
|
|
|
|
files[section] = []
|
|
|
|
for msg, f in fdroidserver.scanner.json_per_build[section]:
|
|
|
|
files[section].append(f)
|
|
|
|
|
|
|
|
self.assertFalse('ascii.out' in files['errors'],
|
|
|
|
'an ASCII .out file is not an error')
|
|
|
|
self.assertFalse('snippet.png' in files['errors'],
|
|
|
|
'an executable valid image is not an error')
|
|
|
|
|
|
|
|
self.assertTrue('arg.jar' in files['errors'], 'all JAR files are errors')
|
|
|
|
self.assertTrue('baz.so' in files['errors'], 'all .so files are errors')
|
|
|
|
self.assertTrue('binary.out' in files['errors'], 'a binary .out file is an error')
|
2020-06-03 21:26:03 +02:00
|
|
|
self.assertTrue('classes.dex' in files['errors'], 'all classes.dex files are errors')
|
2020-06-03 16:55:43 +02:00
|
|
|
self.assertTrue('sqlcipher.aar' in files['errors'], 'all AAR files are errors')
|
|
|
|
self.assertTrue('static.a' in files['errors'], 'all .a files are errors')
|
|
|
|
|
|
|
|
self.assertTrue('fake.png' in files['warnings'],
|
|
|
|
'a random binary that is executable that is not an image is a warning')
|
2020-06-03 21:26:03 +02:00
|
|
|
self.assertTrue('src/test/resources/classes.dex' in files['warnings'],
|
|
|
|
'suspicious file but in a test dir is a warning')
|
2020-06-03 16:55:43 +02:00
|
|
|
|
|
|
|
for f in remove:
|
|
|
|
self.assertTrue(f in files['infos'],
|
|
|
|
f + ' should be removed with an info message')
|
|
|
|
|
2020-05-14 16:08:56 +02:00
|
|
|
def test_build_local_scanner(self):
|
|
|
|
"""`fdroid build` calls scanner functions, test them here"""
|
2021-06-07 11:49:21 +02:00
|
|
|
testdir = tempfile.mkdtemp(
|
|
|
|
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
|
|
|
|
)
|
2020-05-14 16:08:56 +02:00
|
|
|
os.chdir(testdir)
|
|
|
|
|
|
|
|
config = dict()
|
|
|
|
fdroidserver.common.fill_config_defaults(config)
|
|
|
|
fdroidserver.common.config = config
|
2020-06-15 20:03:19 +02:00
|
|
|
fdroidserver.build.config = config
|
2020-05-14 16:08:56 +02:00
|
|
|
fdroidserver.build.options = mock.Mock()
|
|
|
|
fdroidserver.build.options.json = False
|
2020-06-15 20:03:19 +02:00
|
|
|
fdroidserver.build.options.scan_binary = False
|
2020-05-14 16:08:56 +02:00
|
|
|
fdroidserver.build.options.notarball = True
|
|
|
|
fdroidserver.build.options.skipscan = False
|
|
|
|
fdroidserver.scanner.options = fdroidserver.build.options
|
|
|
|
|
|
|
|
app = fdroidserver.metadata.App()
|
|
|
|
app.id = 'mocked.app.id'
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
build.commit = '1.0'
|
|
|
|
build.output = app.id + '.apk'
|
2020-06-03 15:12:04 +02:00
|
|
|
build.scanignore = ['baz.so', 'foo.aar']
|
2020-05-14 16:08:56 +02:00
|
|
|
build.versionCode = '1'
|
|
|
|
build.versionName = '1.0'
|
|
|
|
vcs = mock.Mock()
|
|
|
|
|
|
|
|
for f in ('baz.so', 'foo.aar', 'gradle-wrapper.jar'):
|
|
|
|
with open(f, 'w') as fp:
|
|
|
|
fp.write('placeholder')
|
|
|
|
self.assertTrue(os.path.exists(f))
|
|
|
|
|
2021-06-14 15:38:37 +02:00
|
|
|
with open('build.xml', 'w', encoding='utf-8') as fp:
|
2021-06-07 11:49:21 +02:00
|
|
|
fp.write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2020-05-14 16:08:56 +02:00
|
|
|
<project basedir="." default="clean" name="mockapp">
|
|
|
|
<target name="release"/>
|
|
|
|
<target name="clean"/>
|
2021-06-07 11:49:21 +02:00
|
|
|
</project>"""
|
|
|
|
)
|
|
|
|
)
|
2020-05-14 16:08:56 +02:00
|
|
|
|
|
|
|
def make_fake_apk(output, build):
|
|
|
|
with open(build.output, 'w') as fp:
|
|
|
|
fp.write('APK PLACEHOLDER')
|
|
|
|
return output
|
|
|
|
|
|
|
|
with mock.patch('fdroidserver.common.replace_build_vars', wraps=make_fake_apk):
|
|
|
|
with mock.patch('fdroidserver.common.get_native_code', return_value='x86'):
|
|
|
|
with mock.patch('fdroidserver.common.get_apk_id',
|
|
|
|
return_value=(app.id, build.versionCode, build.versionName)):
|
|
|
|
with mock.patch('fdroidserver.common.is_apk_and_debuggable', return_value=False):
|
|
|
|
fdroidserver.build.build_local(
|
|
|
|
app, build, vcs,
|
|
|
|
build_dir=testdir, output_dir=testdir,
|
|
|
|
log_dir=None, srclib_dir=None, extlib_dir=None, tmp_dir=None,
|
|
|
|
force=False, onserver=False, refresh=False
|
|
|
|
)
|
|
|
|
self.assertTrue(os.path.exists('baz.so'))
|
|
|
|
self.assertTrue(os.path.exists('foo.aar'))
|
|
|
|
self.assertFalse(os.path.exists('gradle-wrapper.jar'))
|
|
|
|
|
2020-06-03 23:40:01 +02:00
|
|
|
def test_gradle_maven_url_regex(self):
|
|
|
|
"""Check the regex can find all the cases"""
|
|
|
|
with open(os.path.join(self.basedir, 'gradle-maven-blocks.yaml')) as fp:
|
|
|
|
data = yaml.safe_load(fp)
|
|
|
|
|
|
|
|
urls = []
|
|
|
|
for entry in data:
|
|
|
|
found = False
|
|
|
|
for m in fdroidserver.scanner.MAVEN_URL_REGEX.findall(entry):
|
|
|
|
urls.append(m)
|
|
|
|
found = True
|
|
|
|
self.assertTrue(found, 'this block should produce a URL:\n' + entry)
|
|
|
|
self.assertEqual(len(data), len(urls), 'each data example should produce a URL')
|
|
|
|
|
2020-06-30 00:02:04 +02:00
|
|
|
def test_scan_gradle_file_with_multiple_problems(self):
|
|
|
|
"""Check that the scanner can handle scandelete with gradle files with multiple problems"""
|
2021-06-07 11:49:21 +02:00
|
|
|
testdir = tempfile.mkdtemp(
|
|
|
|
prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir
|
|
|
|
)
|
2020-06-30 00:02:04 +02:00
|
|
|
os.chdir(testdir)
|
|
|
|
fdroidserver.scanner.config = None
|
|
|
|
fdroidserver.scanner.options = mock.Mock()
|
|
|
|
build = fdroidserver.metadata.Build()
|
|
|
|
build.scandelete = ['build.gradle']
|
2021-06-14 15:38:37 +02:00
|
|
|
with open('build.gradle', 'w', encoding='utf-8') as fp:
|
2021-06-07 11:49:21 +02:00
|
|
|
fp.write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2020-06-30 00:02:04 +02:00
|
|
|
maven {
|
|
|
|
url 'https://maven.fabric.io/public'
|
|
|
|
}
|
|
|
|
maven {
|
|
|
|
url 'https://evilcorp.com/maven'
|
|
|
|
}
|
2021-06-07 11:49:21 +02:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2020-06-30 00:02:04 +02:00
|
|
|
count = fdroidserver.scanner.scan_source(testdir, build)
|
|
|
|
self.assertFalse(os.path.exists("build.gradle"))
|
|
|
|
self.assertEqual(0, count, 'there should be this many errors')
|
2017-12-15 00:29:38 +01:00
|
|
|
|
2020-06-30 02:47:53 +02:00
|
|
|
|
2017-12-15 00:29:38 +01:00
|
|
|
if __name__ == "__main__":
|
2018-08-14 10:34:13 +02:00
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
2017-12-15 00:29:38 +01:00
|
|
|
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",
|
|
|
|
)
|
2017-12-15 00:29:38 +01:00
|
|
|
(fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
|
|
|
newSuite.addTest(unittest.makeSuite(ScannerTest))
|
|
|
|
unittest.main(failfast=False)
|