Update test_adblock for new config
This required some changes on how URLs are handled during those tests. Before, we simply could return a path and (since we had a patched QNAM), nobody complained. Now this actually needs to be a valid URL, so we use https://www.example.com/path everywhere instead.
This commit is contained in:
parent
78d7ac311f
commit
ff05560047
@ -34,24 +34,24 @@ pytestmark = pytest.mark.usefixtures('qapp', 'config_tmpdir')
|
|||||||
|
|
||||||
# TODO See ../utils/test_standarddirutils for OSError and caplog assertion
|
# TODO See ../utils/test_standarddirutils for OSError and caplog assertion
|
||||||
|
|
||||||
WHITELISTED_HOSTS = ('qutebrowser.org', 'mediumhost.io')
|
WHITELISTED_HOSTS = ['qutebrowser.org', 'mediumhost.io']
|
||||||
|
|
||||||
BLOCKLIST_HOSTS = ('localhost',
|
BLOCKLIST_HOSTS = ['localhost',
|
||||||
'mediumhost.io',
|
'mediumhost.io',
|
||||||
'malware.badhost.org',
|
'malware.badhost.org',
|
||||||
'4-verybadhost.com',
|
'4-verybadhost.com',
|
||||||
'ads.worsthostever.net')
|
'ads.worsthostever.net']
|
||||||
|
|
||||||
CLEAN_HOSTS = ('goodhost.gov', 'verygoodhost.com')
|
CLEAN_HOSTS = ['goodhost.gov', 'verygoodhost.com']
|
||||||
|
|
||||||
URLS_TO_CHECK = ('http://localhost',
|
URLS_TO_CHECK = ['http://localhost',
|
||||||
'http://mediumhost.io',
|
'http://mediumhost.io',
|
||||||
'ftp://malware.badhost.org',
|
'ftp://malware.badhost.org',
|
||||||
'http://4-verybadhost.com',
|
'http://4-verybadhost.com',
|
||||||
'http://ads.worsthostever.net',
|
'http://ads.worsthostever.net',
|
||||||
'http://goodhost.gov',
|
'http://goodhost.gov',
|
||||||
'ftp://verygoodhost.com',
|
'ftp://verygoodhost.com',
|
||||||
'http://qutebrowser.org')
|
'http://qutebrowser.org']
|
||||||
|
|
||||||
|
|
||||||
class BaseDirStub:
|
class BaseDirStub:
|
||||||
@ -190,7 +190,8 @@ def generic_blocklists(directory):
|
|||||||
file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
|
file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
|
||||||
name='false_positive', line_format='one_per_line')
|
name='false_positive', line_format='one_per_line')
|
||||||
files_to_zip = [file1, file2, file3]
|
files_to_zip = [file1, file2, file3]
|
||||||
blocklist1 = QUrl(create_zipfile(directory, files_to_zip, 'block1'))
|
blocklist1 = QUrl('http://example.com/')
|
||||||
|
blocklist1.setPath(create_zipfile(directory, files_to_zip, 'block1'))
|
||||||
|
|
||||||
# remote zip file without file named hosts
|
# remote zip file without file named hosts
|
||||||
# (Should raise a FileNotFoundError)
|
# (Should raise a FileNotFoundError)
|
||||||
@ -201,38 +202,37 @@ def generic_blocklists(directory):
|
|||||||
file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
|
file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
|
||||||
name='false_positive', line_format='one_per_line')
|
name='false_positive', line_format='one_per_line')
|
||||||
files_to_zip = [file1, file2, file3]
|
files_to_zip = [file1, file2, file3]
|
||||||
blocklist2 = QUrl(create_zipfile(directory, files_to_zip, 'block2'))
|
blocklist2 = QUrl('http://example.com/')
|
||||||
|
blocklist2.setPath(create_zipfile(directory, files_to_zip, 'block2'))
|
||||||
|
|
||||||
# remote zip file with only one valid hosts file inside
|
# remote zip file with only one valid hosts file inside
|
||||||
blocklist3 = create_blocklist(directory,
|
file1 = create_blocklist(directory, blocked_hosts=[BLOCKLIST_HOSTS[3]],
|
||||||
blocked_hosts=[BLOCKLIST_HOSTS[3]],
|
name='malwarelist', line_format='etc_hosts')
|
||||||
name='malwarelist', line_format='etc_hosts')
|
blocklist3 = QUrl('http://example.com/')
|
||||||
blocklist3 = QUrl(create_zipfile(directory, [blocklist3], 'block3'))
|
blocklist3.setPath(create_zipfile(directory, [file1], 'block3'))
|
||||||
|
|
||||||
# local text file with valid hosts
|
# local text file with valid hosts
|
||||||
blocklist4 = QUrl(create_blocklist(directory,
|
blocklist4 = QUrl.fromLocalFile(create_blocklist(
|
||||||
blocked_hosts=[BLOCKLIST_HOSTS[4]],
|
directory, blocked_hosts=[BLOCKLIST_HOSTS[4]],
|
||||||
name='mycustomblocklist',
|
name='mycustomblocklist', line_format='one_per_line'))
|
||||||
line_format='one_per_line'))
|
|
||||||
blocklist4.setScheme('file')
|
|
||||||
|
|
||||||
# remote text file without valid hosts format
|
# remote text file without valid hosts format
|
||||||
blocklist5 = QUrl(create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
|
blocklist5 = QUrl('http://example.com/')
|
||||||
name='notcorrectlist',
|
blocklist5.setPath(create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
|
||||||
line_format='not_correct'))
|
name='notcorrectlist',
|
||||||
|
line_format='not_correct'))
|
||||||
|
|
||||||
return [blocklist1, blocklist2, blocklist3, blocklist4, blocklist5]
|
return [blocklist1.toString(), blocklist2.toString(),
|
||||||
|
blocklist3.toString(), blocklist4.toString(),
|
||||||
|
blocklist5.toString()]
|
||||||
|
|
||||||
|
|
||||||
def test_disabled_blocking_update(basedir, config_stub, download_stub,
|
def test_disabled_blocking_update(basedir, config_stub, download_stub,
|
||||||
data_tmpdir, tmpdir, win_registry, caplog):
|
data_tmpdir, tmpdir, win_registry, caplog):
|
||||||
"""Ensure no URL is blocked when host blocking is disabled."""
|
"""Ensure no URL is blocked when host blocking is disabled."""
|
||||||
config_stub.data = {
|
config_stub.val.content.host_blocking.lists = generic_blocklists(tmpdir)
|
||||||
'content': {
|
config_stub.val.content.host_blocking.enabled = False
|
||||||
'host-block-lists': generic_blocklists(tmpdir),
|
|
||||||
'host-blocking-enabled': False,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.adblock_update()
|
host_blocker.adblock_update()
|
||||||
while host_blocker._in_progress:
|
while host_blocker._in_progress:
|
||||||
@ -247,12 +247,9 @@ def test_disabled_blocking_update(basedir, config_stub, download_stub,
|
|||||||
def test_no_blocklist_update(config_stub, download_stub,
|
def test_no_blocklist_update(config_stub, download_stub,
|
||||||
data_tmpdir, basedir, tmpdir, win_registry):
|
data_tmpdir, basedir, tmpdir, win_registry):
|
||||||
"""Ensure no URL is blocked when no block list exists."""
|
"""Ensure no URL is blocked when no block list exists."""
|
||||||
config_stub.data = {
|
config_stub.val.content.host_blocking.lists = None
|
||||||
'content': {
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-block-lists': None,
|
|
||||||
'host-blocking-enabled': True,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.adblock_update()
|
host_blocker.adblock_update()
|
||||||
host_blocker.read_hosts()
|
host_blocker.read_hosts()
|
||||||
@ -263,13 +260,10 @@ def test_no_blocklist_update(config_stub, download_stub,
|
|||||||
def test_successful_update(config_stub, basedir, download_stub,
|
def test_successful_update(config_stub, basedir, download_stub,
|
||||||
data_tmpdir, tmpdir, win_registry, caplog):
|
data_tmpdir, tmpdir, win_registry, caplog):
|
||||||
"""Ensure hosts from host-block-lists are blocked after an update."""
|
"""Ensure hosts from host-block-lists are blocked after an update."""
|
||||||
config_stub.data = {
|
config_stub.val.content.host_blocking.lists = generic_blocklists(tmpdir)
|
||||||
'content': {
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-block-lists': generic_blocklists(tmpdir),
|
config_stub.val.content.host_blocking.whitelist = None
|
||||||
'host-blocking-enabled': True,
|
|
||||||
'host-blocking-whitelist': None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.adblock_update()
|
host_blocker.adblock_update()
|
||||||
# Simulate download is finished
|
# Simulate download is finished
|
||||||
@ -287,18 +281,16 @@ def test_failed_dl_update(config_stub, basedir, download_stub,
|
|||||||
|
|
||||||
Ensure hosts from this list are not blocked.
|
Ensure hosts from this list are not blocked.
|
||||||
"""
|
"""
|
||||||
dl_fail_blocklist = QUrl(create_blocklist(tmpdir,
|
dl_fail_blocklist = QUrl('http://example.com/')
|
||||||
blocked_hosts=CLEAN_HOSTS,
|
dl_fail_blocklist.setPath(create_blocklist(
|
||||||
name='download_will_fail',
|
tmpdir, blocked_hosts=CLEAN_HOSTS, name='download_will_fail',
|
||||||
line_format='one_per_line'))
|
line_format='one_per_line'))
|
||||||
hosts_to_block = generic_blocklists(tmpdir) + [dl_fail_blocklist]
|
hosts_to_block = (generic_blocklists(tmpdir) +
|
||||||
config_stub.data = {
|
[dl_fail_blocklist.toString()])
|
||||||
'content': {
|
config_stub.val.content.host_blocking.lists = hosts_to_block
|
||||||
'host-block-lists': hosts_to_block,
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-blocking-enabled': True,
|
config_stub.val.content.host_blocking.whitelist = None
|
||||||
'host-blocking-whitelist': None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.adblock_update()
|
host_blocker.adblock_update()
|
||||||
while host_blocker._in_progress:
|
while host_blocker._in_progress:
|
||||||
@ -327,13 +319,12 @@ def test_invalid_utf8(config_stub, download_stub, tmpdir, caplog, location):
|
|||||||
for url in BLOCKLIST_HOSTS:
|
for url in BLOCKLIST_HOSTS:
|
||||||
blocklist.write(url + '\n', mode='a')
|
blocklist.write(url + '\n', mode='a')
|
||||||
|
|
||||||
config_stub.data = {
|
url = QUrl('https://www.example.com/')
|
||||||
'content': {
|
url.setPath(str(blocklist))
|
||||||
'host-block-lists': [QUrl(str(blocklist))],
|
config_stub.val.content.host_blocking.lists = [url.toString()]
|
||||||
'host-blocking-enabled': True,
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-blocking-whitelist': None,
|
config_stub.val.content.host_blocking.whitelist = None
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.adblock_update()
|
host_blocker.adblock_update()
|
||||||
finished_signal = host_blocker._in_progress[0].finished
|
finished_signal = host_blocker._in_progress[0].finished
|
||||||
@ -351,22 +342,18 @@ def test_invalid_utf8(config_stub, download_stub, tmpdir, caplog, location):
|
|||||||
assert_urls(host_blocker, whitelisted=[])
|
assert_urls(host_blocker, whitelisted=[])
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_utf8_compiled(config_stub, tmpdir, monkeypatch, caplog):
|
def test_invalid_utf8_compiled(config_stub, config_tmpdir, data_tmpdir,
|
||||||
|
monkeypatch, caplog):
|
||||||
"""Make sure invalid UTF-8 in the compiled file is handled."""
|
"""Make sure invalid UTF-8 in the compiled file is handled."""
|
||||||
data_dir = tmpdir / 'data'
|
config_stub.val.content.host_blocking.lists = []
|
||||||
config_dir = tmpdir / 'config'
|
|
||||||
monkeypatch.setattr(adblock.standarddir, 'data', lambda: str(data_dir))
|
|
||||||
monkeypatch.setattr(adblock.standarddir, 'config', lambda: str(config_dir))
|
|
||||||
|
|
||||||
config_stub.data = {
|
# Make sure the HostBlocker doesn't delete blocked-hosts in __init__
|
||||||
'content': {
|
monkeypatch.setattr(adblock.HostBlocker, '_update_files',
|
||||||
'host-block-lists': [],
|
lambda _self: None)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(config_dir / 'blocked-hosts').write_binary(
|
(config_tmpdir / 'blocked-hosts').write_binary(
|
||||||
b'https://www.example.org/\xa0')
|
b'https://www.example.org/\xa0')
|
||||||
(data_dir / 'blocked-hosts').ensure()
|
(data_tmpdir / 'blocked-hosts').ensure()
|
||||||
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
with caplog.at_level(logging.ERROR):
|
with caplog.at_level(logging.ERROR):
|
||||||
@ -385,13 +372,10 @@ def test_blocking_with_whitelist(config_stub, basedir, download_stub,
|
|||||||
blocked_hosts=filtered_blocked_hosts,
|
blocked_hosts=filtered_blocked_hosts,
|
||||||
name='blocked-hosts',
|
name='blocked-hosts',
|
||||||
line_format='one_per_line')
|
line_format='one_per_line')
|
||||||
config_stub.data = {
|
config_stub.val.content.host_blocking.lists = [blocklist]
|
||||||
'content': {
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-block-lists': [blocklist],
|
config_stub.val.content.host_blocking.whitelist = WHITELISTED_HOSTS
|
||||||
'host-blocking-enabled': True,
|
|
||||||
'host-blocking-whitelist': WHITELISTED_HOSTS,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.read_hosts()
|
host_blocker.read_hosts()
|
||||||
assert_urls(host_blocker)
|
assert_urls(host_blocker)
|
||||||
@ -407,13 +391,10 @@ def test_config_change_initial(config_stub, basedir, download_stub,
|
|||||||
"""
|
"""
|
||||||
create_blocklist(tmpdir, blocked_hosts=BLOCKLIST_HOSTS,
|
create_blocklist(tmpdir, blocked_hosts=BLOCKLIST_HOSTS,
|
||||||
name='blocked-hosts', line_format='one_per_line')
|
name='blocked-hosts', line_format='one_per_line')
|
||||||
config_stub.data = {
|
config_stub.val.content.host_blocking.lists = None
|
||||||
'content': {
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-block-lists': None,
|
config_stub.val.content.host_blocking.whitelist = None
|
||||||
'host-blocking-enabled': True,
|
|
||||||
'host-blocking-whitelist': None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.read_hosts()
|
host_blocker.read_hosts()
|
||||||
for str_url in URLS_TO_CHECK:
|
for str_url in URLS_TO_CHECK:
|
||||||
@ -424,20 +405,17 @@ def test_config_change(config_stub, basedir, download_stub,
|
|||||||
data_tmpdir, tmpdir):
|
data_tmpdir, tmpdir):
|
||||||
"""Ensure blocked-hosts resets if host-block-list is changed to None."""
|
"""Ensure blocked-hosts resets if host-block-list is changed to None."""
|
||||||
filtered_blocked_hosts = BLOCKLIST_HOSTS[1:] # Exclude localhost
|
filtered_blocked_hosts = BLOCKLIST_HOSTS[1:] # Exclude localhost
|
||||||
blocklist = QUrl(create_blocklist(tmpdir,
|
blocklist = QUrl('http://www.example.com/')
|
||||||
blocked_hosts=filtered_blocked_hosts,
|
blocklist.setPath(create_blocklist(
|
||||||
name='blocked-hosts',
|
tmpdir, blocked_hosts=filtered_blocked_hosts, name='blocked-hosts',
|
||||||
line_format='one_per_line'))
|
line_format='one_per_line'))
|
||||||
config_stub.data = {
|
config_stub.val.content.host_blocking.lists = [blocklist.toString()]
|
||||||
'content': {
|
config_stub.val.content.host_blocking.enabled = True
|
||||||
'host-block-lists': [blocklist],
|
config_stub.val.content.host_blocking.whitelist = None
|
||||||
'host-blocking-enabled': True,
|
|
||||||
'host-blocking-whitelist': None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
host_blocker = adblock.HostBlocker()
|
host_blocker = adblock.HostBlocker()
|
||||||
host_blocker.read_hosts()
|
host_blocker.read_hosts()
|
||||||
config_stub.set('content', 'host-block-lists', None)
|
config_stub.set_obj('content.host_blocking.lists', None)
|
||||||
host_blocker.read_hosts()
|
host_blocker.read_hosts()
|
||||||
for str_url in URLS_TO_CHECK:
|
for str_url in URLS_TO_CHECK:
|
||||||
assert not host_blocker.is_blocked(QUrl(str_url))
|
assert not host_blocker.is_blocked(QUrl(str_url))
|
||||||
|
Loading…
Reference in New Issue
Block a user