1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# 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"""
from PyQt5.QtCore import QUrl, QDateTime
from PyQt5.QtNetwork import QNetworkDiskCache, QNetworkCacheMetaData
from qutebrowser.browser import cache
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)
def test_cache_size_leq_max_cache_size(config_stub, tmpdir):
"""Test cacheSize <= MaximumCacheSize when cache is activated."""
limit = 100
config_stub.data = {
'storage': {'cache-size': limit},
'general': {'private-browsing': False}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.maximumCacheSize() == limit
preload_cache(disk_cache, 'http://www.example.com/')
preload_cache(disk_cache, 'http://qutebrowser.org')
preload_cache(disk_cache, 'http://foo.xxx')
preload_cache(disk_cache, 'http://bar.net')
assert disk_cache.expire() < limit
# Add a threshold to the limit due to unforseeable Qt internals
assert disk_cache.cacheSize() < limit+100
def test_cache_deactivated_private_browsing(config_stub, tmpdir):
"""Test if cache is deactivated in private-browsing mode."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
disk_cache = cache.DiskCache(str(tmpdir))
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl('http://www.example.com/'))
assert metadata.isValid()
assert disk_cache.prepare(metadata) is None
def test_cache_deactivated_get_data(config_stub, tmpdir):
"""Query some data from a deactivated cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
disk_cache = cache.DiskCache(str(tmpdir))
url = QUrl('http://www.example.com/')
assert disk_cache.data(url) is None
def test_cache_get_nonexistent_data(config_stub, tmpdir):
"""Test querying some data that was never inserted."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
disk_cache = cache.DiskCache(str(tmpdir))
preload_cache(disk_cache, 'https://qutebrowser.org')
assert disk_cache.data(QUrl('http://qutebrowser.org')) is None
def test_cache_deactivated_remove_data(config_stub, tmpdir):
"""Test removing some data from a deactivated cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
disk_cache = cache.DiskCache(str(tmpdir))
url = QUrl('http://www.example.com/')
assert disk_cache.remove(url) == False
def test_cache_insert_data(tmpdir):
"""Test if entries inserted into the cache are actually there."""
url = 'http://qutebrowser.org'
content = b'foobar'
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
assert disk_cache.cacheSize() == 0
preload_cache(disk_cache, url, content)
assert disk_cache.cacheSize() != 0
assert disk_cache.data(QUrl(url)).readAll() == content
def test_cache_remove_data(tmpdir):
"""Test if a previously inserted entry can be removed from the cache."""
url = 'http://qutebrowser.org'
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
preload_cache(disk_cache, url)
assert disk_cache.cacheSize() > 0
assert disk_cache.remove(QUrl(url))
assert disk_cache.cacheSize() == 0
def test_cache_clear_activated(config_stub, tmpdir):
"""Test if cache is empty after clearing it."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.cacheSize() == 0
preload_cache(disk_cache)
assert disk_cache.cacheSize() != 0
disk_cache.clear()
assert disk_cache.cacheSize() == 0
def test_cache_metadata(tmpdir):
"""Ensure that DiskCache.metaData() returns exactly what was inserted."""
url = 'http://qutebrowser.org'
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
assert metadata.isValid()
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
device = disk_cache.prepare(metadata)
device.write(b'foobar')
disk_cache.insert(device)
assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_update_metadata(tmpdir):
"""Test updating the meta data for an existing cache entry."""
url = 'http://qutebrowser.org'
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
preload_cache(disk_cache, url, b'foo')
assert disk_cache.cacheSize() > 0
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
assert metadata.isValid()
disk_cache.updateMetaData(metadata)
assert disk_cache.metaData(QUrl(url)) == metadata
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
|