2015-10-21 13:37:47 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
|
|
|
# Copyright 2015 lamarpavel
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Tests for qutebrowser.browser.cache"""
|
|
|
|
|
2015-10-30 02:03:34 +01:00
|
|
|
from PyQt5.QtCore import QUrl, QDateTime
|
2015-10-29 02:28:57 +01:00
|
|
|
from PyQt5.QtNetwork import QNetworkDiskCache, QNetworkCacheMetaData
|
|
|
|
|
2015-10-21 13:37:47 +02:00
|
|
|
from qutebrowser.browser import cache
|
|
|
|
|
|
|
|
|
2015-10-29 02:28:57 +01:00
|
|
|
def preload_cache(cache, url='http://www.example.com/', content=b'foobar'):
|
|
|
|
metadata = QNetworkCacheMetaData()
|
|
|
|
metadata.setUrl(QUrl(url))
|
|
|
|
assert metadata.isValid()
|
|
|
|
device = cache.prepare(metadata)
|
|
|
|
assert device is not None
|
|
|
|
device.write(content)
|
|
|
|
cache.insert(device)
|
|
|
|
|
|
|
|
|
2015-10-21 14:20:01 +02:00
|
|
|
def test_cache_size_leq_max_cache_size(config_stub, tmpdir):
|
2015-10-21 13:37:47 +02:00
|
|
|
"""Test cacheSize <= MaximumCacheSize when cache is activated."""
|
2015-10-29 03:13:25 +01:00
|
|
|
limit = 100
|
2015-10-21 13:37:47 +02:00
|
|
|
config_stub.data = {
|
2015-10-29 03:13:25 +01:00
|
|
|
'storage': {'cache-size': limit},
|
2015-10-21 14:38:08 +02:00
|
|
|
'general': {'private-browsing': False}
|
|
|
|
}
|
2015-10-21 14:20:01 +02:00
|
|
|
disk_cache = cache.DiskCache(str(tmpdir))
|
2015-10-29 03:13:25 +01:00
|
|
|
assert disk_cache.maximumCacheSize() == limit
|
2015-10-29 02:28:57 +01:00
|
|
|
|
2015-10-29 02:56:40 +01:00
|
|
|
preload_cache(disk_cache, 'http://www.example.com/')
|
2015-10-29 02:28:57 +01:00
|
|
|
preload_cache(disk_cache, 'http://qutebrowser.org')
|
|
|
|
preload_cache(disk_cache, 'http://foo.xxx')
|
|
|
|
preload_cache(disk_cache, 'http://bar.net')
|
2015-10-29 03:13:25 +01:00
|
|
|
assert disk_cache.expire() < limit
|
2015-10-30 01:22:16 +01:00
|
|
|
# Add a threshold to the limit due to unforseeable Qt internals
|
|
|
|
assert disk_cache.cacheSize() < limit+100
|
2015-10-21 13:37:47 +02:00
|
|
|
|
|
|
|
|
2015-10-21 14:20:01 +02:00
|
|
|
def test_cache_deactivated_private_browsing(config_stub, tmpdir):
|
2015-10-21 13:37:47 +02:00
|
|
|
"""Test if cache is deactivated in private-browsing mode."""
|
|
|
|
config_stub.data = {
|
2015-10-21 14:38:08 +02:00
|
|
|
'storage': {'cache-size': 1024},
|
|
|
|
'general': {'private-browsing': True}
|
|
|
|
}
|
2015-10-21 14:20:01 +02:00
|
|
|
disk_cache = cache.DiskCache(str(tmpdir))
|
2015-10-21 13:37:47 +02:00
|
|
|
|
2015-10-29 02:28:57 +01:00
|
|
|
metadata = QNetworkCacheMetaData()
|
|
|
|
metadata.setUrl(QUrl('http://www.example.com/'))
|
|
|
|
assert metadata.isValid()
|
|
|
|
assert disk_cache.prepare(metadata) is None
|
2015-10-21 14:33:45 +02:00
|
|
|
|
|
|
|
|
2015-10-29 02:37:32 +01:00
|
|
|
def test_cache_insert_data(tmpdir):
|
|
|
|
"""Test if entries inserted into the cache are actually there."""
|
2015-10-29 03:13:25 +01:00
|
|
|
url = 'http://qutebrowser.org'
|
|
|
|
content = b'foobar'
|
2015-10-29 02:37:32 +01:00
|
|
|
disk_cache = QNetworkDiskCache()
|
|
|
|
disk_cache.setCacheDirectory(str(tmpdir))
|
|
|
|
assert disk_cache.cacheSize() == 0
|
|
|
|
|
2015-10-29 03:13:25 +01:00
|
|
|
preload_cache(disk_cache, url, content)
|
2015-10-29 02:37:32 +01:00
|
|
|
|
|
|
|
assert disk_cache.cacheSize() != 0
|
2015-10-29 03:13:25 +01:00
|
|
|
assert disk_cache.data(QUrl(url)).readAll() == content
|
2015-10-29 02:37:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_cache_remove_data(tmpdir):
|
|
|
|
"""Test if a previously inserted entry can be removed from the cache."""
|
2015-10-29 03:13:25 +01:00
|
|
|
url = 'http://qutebrowser.org'
|
2015-10-29 02:37:32 +01:00
|
|
|
disk_cache = QNetworkDiskCache()
|
|
|
|
disk_cache.setCacheDirectory(str(tmpdir))
|
2015-10-29 03:13:25 +01:00
|
|
|
preload_cache(disk_cache, url)
|
2015-10-29 02:37:32 +01:00
|
|
|
assert disk_cache.cacheSize() > 0
|
|
|
|
|
2015-10-29 03:13:25 +01:00
|
|
|
assert disk_cache.remove(QUrl(url))
|
2015-10-29 02:37:32 +01:00
|
|
|
assert disk_cache.cacheSize() == 0
|
|
|
|
|
|
|
|
|
2015-10-29 02:56:40 +01:00
|
|
|
def test_cache_clear_activated(config_stub, tmpdir):
|
2015-10-21 14:38:08 +02:00
|
|
|
"""Test if cache is empty after clearing it."""
|
2015-10-21 14:33:45 +02:00
|
|
|
config_stub.data = {
|
2015-10-21 14:38:08 +02:00
|
|
|
'storage': {'cache-size': 1024},
|
|
|
|
'general': {'private-browsing': False}
|
|
|
|
}
|
2015-10-21 14:33:45 +02:00
|
|
|
disk_cache = cache.DiskCache(str(tmpdir))
|
2015-10-29 02:28:57 +01:00
|
|
|
assert disk_cache.cacheSize() == 0
|
|
|
|
|
|
|
|
preload_cache(disk_cache)
|
|
|
|
assert disk_cache.cacheSize() != 0
|
|
|
|
|
2015-10-21 14:33:45 +02:00
|
|
|
disk_cache.clear()
|
2015-10-23 01:49:00 +02:00
|
|
|
assert disk_cache.cacheSize() == 0
|
2015-10-29 02:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_cache_metadata(tmpdir):
|
|
|
|
"""Ensure that DiskCache.metaData() returns exactly what was inserted."""
|
2015-10-29 03:13:25 +01:00
|
|
|
url = 'http://qutebrowser.org'
|
2015-10-29 02:56:40 +01:00
|
|
|
metadata = QNetworkCacheMetaData()
|
2015-10-29 03:13:25 +01:00
|
|
|
metadata.setUrl(QUrl(url))
|
2015-10-29 02:56:40 +01:00
|
|
|
assert metadata.isValid()
|
|
|
|
disk_cache = QNetworkDiskCache()
|
|
|
|
disk_cache.setCacheDirectory(str(tmpdir))
|
|
|
|
device = disk_cache.prepare(metadata)
|
|
|
|
device.write(b'foobar')
|
|
|
|
disk_cache.insert(device)
|
|
|
|
|
2015-10-29 03:13:25 +01:00
|
|
|
assert disk_cache.metaData(QUrl(url)) == metadata
|
2015-10-29 02:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_cache_update_metadata(tmpdir):
|
|
|
|
"""Test updating the meta data for an existing cache entry."""
|
2015-10-29 03:13:25 +01:00
|
|
|
url = 'http://qutebrowser.org'
|
2015-10-29 02:56:40 +01:00
|
|
|
disk_cache = QNetworkDiskCache()
|
|
|
|
disk_cache.setCacheDirectory(str(tmpdir))
|
2015-10-29 03:13:25 +01:00
|
|
|
preload_cache(disk_cache, url, b'foo')
|
2015-10-29 02:56:40 +01:00
|
|
|
assert disk_cache.cacheSize() > 0
|
|
|
|
|
|
|
|
metadata = QNetworkCacheMetaData()
|
2015-10-29 03:13:25 +01:00
|
|
|
metadata.setUrl(QUrl(url))
|
2015-10-29 02:56:40 +01:00
|
|
|
assert metadata.isValid()
|
|
|
|
disk_cache.updateMetaData(metadata)
|
2015-10-29 03:13:25 +01:00
|
|
|
assert disk_cache.metaData(QUrl(url)) == metadata
|
2015-10-30 02:03:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_cache_full(config_stub, tmpdir):
|
|
|
|
"""Do a sanity test involving everything."""
|
|
|
|
config_stub.data = {
|
|
|
|
'storage': {'cache-size': 1024},
|
|
|
|
'general': {'private-browsing': False}
|
|
|
|
}
|
|
|
|
disk_cache = QNetworkDiskCache()
|
|
|
|
disk_cache.setCacheDirectory(str(tmpdir))
|
|
|
|
|
|
|
|
url = 'http://qutebrowser.org'
|
|
|
|
content = b'cutebowser'
|
|
|
|
preload_cache(disk_cache, url, content)
|
|
|
|
url2 = 'https://qutebrowser.org'
|
|
|
|
content2 = b'ohmycert'
|
|
|
|
preload_cache(disk_cache, url2, content2)
|
|
|
|
|
|
|
|
metadata = QNetworkCacheMetaData()
|
|
|
|
metadata.setUrl(QUrl(url))
|
|
|
|
soon = QDateTime.currentDateTime().addMonths(4)
|
|
|
|
assert soon.isValid()
|
|
|
|
metadata.setLastModified(soon)
|
|
|
|
assert metadata.isValid()
|
|
|
|
disk_cache.updateMetaData(metadata)
|
|
|
|
disk_cache.remove(QUrl(url2))
|
|
|
|
|
|
|
|
assert disk_cache.metaData(QUrl(url)).lastModified() == soon
|
|
|
|
assert disk_cache.data(QUrl(url)).readAll() == content
|