Beispiel #1
0
def test_cache_existing_metadata_file(config_stub, tmpdir):
    """Test querying existing meta data file from activated cache."""
    config_stub.data = {
        'storage': {
            'cache-size': 1024
        },
        'general': {
            'private-browsing': False
        }
    }
    url = 'http://qutebrowser.org'
    content = b'foobar'

    metadata = QNetworkCacheMetaData()
    metadata.setUrl(QUrl(url))
    assert metadata.isValid()

    disk_cache = cache.DiskCache(str(tmpdir))
    device = disk_cache.prepare(metadata)
    assert device is not None
    device.write(content)
    disk_cache.insert(device)
    disk_cache.updateMetaData(metadata)

    files = list(tmpdir.visit(fil=lambda path: path.isfile()))
    assert len(files) == 1
    assert disk_cache.fileMetaData(str(files[0])) == metadata
Beispiel #2
0
    def _init_modules(self):
        """Initialize all 'modules' which need to be initialized."""
        log.init.debug("Initializing readline-bridge...")
        readline_bridge = readline.ReadlineBridge()
        objreg.register('readline-bridge', readline_bridge)

        log.init.debug("Initializing directories...")
        standarddir.init()
        log.init.debug("Initializing config...")
        config.init(self._args)
        log.init.debug("Initializing crashlog...")
        self._handle_segfault()
        log.init.debug("Initializing websettings...")
        websettings.init()
        log.init.debug("Initializing quickmarks...")
        quickmark_manager = quickmarks.QuickmarkManager()
        objreg.register('quickmark-manager', quickmark_manager)
        log.init.debug("Initializing proxy...")
        proxy.init()
        log.init.debug("Initializing cookies...")
        cookie_jar = cookies.CookieJar(self)
        objreg.register('cookie-jar', cookie_jar)
        log.init.debug("Initializing cache...")
        diskcache = cache.DiskCache(self)
        objreg.register('cache', diskcache)
        log.init.debug("Initializing main window...")
        win_id = mainwindow.MainWindow.spawn(
            False if self._args.nowindow else True)
        main_window = objreg.get('main-window', scope='window', window=win_id)
        self.setActiveWindow(main_window)
Beispiel #3
0
def test_cache_deactivated_metadata_file(config_stub, tmpdir):
    """Test querying meta data file when cache is deactivated."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }
    disk_cache = cache.DiskCache(str(tmpdir))
    assert disk_cache.fileMetaData("foo") == QNetworkCacheMetaData()
Beispiel #4
0
def test_cache_size_deactivated(config_stub, tmpdir):
    """Confirm that the cache size returns 0 when deactivated."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }
    disk_cache = cache.DiskCache(str(tmpdir))
    assert disk_cache.cacheSize() == 0
Beispiel #5
0
def test_cache_clear_deactivated(config_stub, tmpdir):
    """Test method clear() on deactivated cache."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }
    disk_cache = cache.DiskCache(str(tmpdir))
    assert disk_cache.clear() is None
Beispiel #6
0
def _init_modules(args, crash_handler):
    """Initialize all 'modules' which need to be initialized.

    Args:
        args: The argparse namespace.
        crash_handler: The CrashHandler instance.
    """
    # pylint: disable=too-many-statements
    log.init.debug("Initializing save manager...")
    save_manager = savemanager.SaveManager(qApp)
    objreg.register('save-manager', save_manager)
    save_manager.add_saveable('version', _save_version)
    log.init.debug("Initializing network...")
    networkmanager.init()
    log.init.debug("Initializing readline-bridge...")
    readline_bridge = readline.ReadlineBridge()
    objreg.register('readline-bridge', readline_bridge)
    log.init.debug("Initializing directories...")
    standarddir.init(args)
    log.init.debug("Initializing config...")
    config.init(qApp)
    save_manager.init_autosave()
    log.init.debug("Initializing web history...")
    history.init(qApp)
    log.init.debug("Initializing crashlog...")
    if not args.no_err_windows:
        crash_handler.handle_segfault()
    log.init.debug("Initializing sessions...")
    sessions.init(qApp)
    log.init.debug("Initializing js-bridge...")
    js_bridge = qutescheme.JSBridge(qApp)
    objreg.register('js-bridge', js_bridge)
    log.init.debug("Initializing websettings...")
    websettings.init()
    log.init.debug("Initializing adblock...")
    host_blocker = adblock.HostBlocker()
    host_blocker.read_hosts()
    objreg.register('host-blocker', host_blocker)
    log.init.debug("Initializing quickmarks...")
    quickmark_manager = urlmarks.QuickmarkManager(qApp)
    objreg.register('quickmark-manager', quickmark_manager)
    log.init.debug("Initializing bookmarks...")
    bookmark_manager = urlmarks.BookmarkManager(qApp)
    objreg.register('bookmark-manager', bookmark_manager)
    log.init.debug("Initializing proxy...")
    proxy.init()
    log.init.debug("Initializing cookies...")
    cookie_jar = cookies.CookieJar(qApp)
    objreg.register('cookie-jar', cookie_jar)
    log.init.debug("Initializing cache...")
    diskcache = cache.DiskCache(qApp)
    objreg.register('cache', diskcache)
    log.init.debug("Initializing completions...")
    completionmodels.init()
    log.init.debug("Misc initialization...")
    _maybe_hide_mouse_cursor()
    objreg.get('config').changed.connect(_maybe_hide_mouse_cursor)
Beispiel #7
0
def test_cache_deactivated_metadata(config_stub, tmpdir):
    """Test querying metaData() on not activated cache."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }
    url = 'http://qutebrowser.org'

    disk_cache = cache.DiskCache(str(tmpdir))
    assert disk_cache.metaData(QUrl(url)) == QNetworkCacheMetaData()
Beispiel #8
0
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 not disk_cache.remove(url)
Beispiel #9
0
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
Beispiel #10
0
def test_cache_nonexistent_metadata_file(config_stub, tmpdir):
    """Test querying nonexistent meta data file from activated cache."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': False}
    }

    disk_cache = cache.DiskCache(str(tmpdir))
    cache_file = disk_cache.fileMetaData("nosuchfile")
    assert not cache_file.isValid()
Beispiel #11
0
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
Beispiel #12
0
def test_cache_config_change_cache_size(config_stub, tmpdir):
    """Change cache size and emit signal to trigger on_config_changed."""
    max_cache_size = 1024
    config_stub.data = {
        'storage': {'cache-size': max_cache_size},
        'general': {'private-browsing': False}
    }
    disk_cache = cache.DiskCache(str(tmpdir))
    assert disk_cache.maximumCacheSize() == max_cache_size

    config_stub.set('storage', 'cache-size', max_cache_size * 2)
    assert disk_cache.maximumCacheSize() == max_cache_size * 2
Beispiel #13
0
def test_cache_no_cache_dir(config_stub):
    """Confirm that the cache is deactivated when cache_dir is None."""
    config_stub.data = {
        'storage': {
            'cache-size': 1024
        },
        'general': {
            'private-browsing': False
        },
    }
    disk_cache = cache.DiskCache(None)
    assert disk_cache.cacheSize() == 0
Beispiel #14
0
def test_cache_deactivated_update_metadata(config_stub, tmpdir):
    """Test updating the meta data when cache is not activated."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }
    url = 'http://qutebrowser.org'
    disk_cache = cache.DiskCache(str(tmpdir))

    metadata = QNetworkCacheMetaData()
    metadata.setUrl(QUrl(url))
    assert metadata.isValid()
    assert disk_cache.updateMetaData(metadata) is None
Beispiel #15
0
def test_cache_config_enable_private_browsing(config_stub, tmpdir):
    """Change private-browsing config to True and emit signal."""
    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

    config_stub.set('general', 'private-browsing', True)
    assert disk_cache.cacheSize() == 0
Beispiel #16
0
def test_cache_remove_data(config_stub, tmpdir):
    """Test if a previously inserted entry can be removed from the cache."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': False}
    }
    url = 'http://qutebrowser.org'
    disk_cache = cache.DiskCache(str(tmpdir))
    preload_cache(disk_cache, url)
    assert disk_cache.cacheSize() > 0

    assert disk_cache.remove(QUrl(url))
    assert disk_cache.cacheSize() == 0
Beispiel #17
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
Beispiel #18
0
def test_cache_insert_data(config_stub, tmpdir):
    """Test if entries inserted into the cache are actually there."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': False}
    }
    url = 'http://qutebrowser.org'
    content = b'foobar'
    disk_cache = cache.DiskCache(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
Beispiel #19
0
def test_cache_update_metadata(config_stub, tmpdir):
    """Test updating the meta data for an existing cache entry."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': False}
    }
    url = 'http://qutebrowser.org'
    disk_cache = cache.DiskCache(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
Beispiel #20
0
def test_cache_metadata(config_stub, tmpdir):
    """Ensure that DiskCache.metaData() returns exactly what was inserted."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': False}
    }
    url = 'http://qutebrowser.org'
    metadata = QNetworkCacheMetaData()
    metadata.setUrl(QUrl(url))
    assert metadata.isValid()
    disk_cache = cache.DiskCache(str(tmpdir))
    device = disk_cache.prepare(metadata)
    device.write(b'foobar')
    disk_cache.insert(device)

    assert disk_cache.metaData(QUrl(url)) == metadata
Beispiel #21
0
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
Beispiel #22
0
def test_cache_config_disable_private_browsing(config_stub, tmpdir):
    """Change private-browsing config to False and emit signal."""
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }
    url = 'http://qutebrowser.org'
    metadata = QNetworkCacheMetaData()
    metadata.setUrl(QUrl(url))
    assert metadata.isValid()

    disk_cache = cache.DiskCache(str(tmpdir))
    assert disk_cache.prepare(metadata) is None

    config_stub.set('general', 'private-browsing', False)
    content = b'cute'
    preload_cache(disk_cache, url, content)
    assert disk_cache.data(QUrl(url)).readAll() == content
Beispiel #23
0
def test_cache_deactivated_insert_data(config_stub, tmpdir):
    """Insert data when cache is deactivated."""
    # First create QNetworkDiskCache just to get a valid QIODevice from it
    url = 'http://qutebrowser.org'
    disk_cache = QNetworkDiskCache()
    disk_cache.setCacheDirectory(str(tmpdir))
    metadata = QNetworkCacheMetaData()
    metadata.setUrl(QUrl(url))
    device = disk_cache.prepare(metadata)
    assert device is not None

    # Now create a deactivated DiskCache and insert the valid device created
    # above (there probably is a better way to get a valid QIODevice...)
    config_stub.data = {
        'storage': {'cache-size': 1024},
        'general': {'private-browsing': True}
    }

    deactivated_cache = cache.DiskCache(str(tmpdir))
    assert deactivated_cache.insert(device) is None