qutebrowser/tests/unit/utils/test_urlmatch.py

314 lines
9.7 KiB
Python
Raw Normal View History

2018-02-14 20:03:01 +01:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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.utils.urlmatch.
2018-02-15 16:19:22 +01:00
The tests are mostly inspired by Chromium's:
2018-02-14 20:03:01 +01:00
https://cs.chromium.org/chromium/src/extensions/common/url_pattern_unittest.cc
2018-02-15 16:19:22 +01:00
Currently not tested:
- The match_effective_tld attribute as it doesn't exist yet.
- Nested filesystem:// URLs as we don't have those.
2018-02-15 17:03:47 +01:00
- Unicode matching because QUrl doesn't like those URLs.
2018-02-14 20:03:01 +01:00
"""
import pytest
2018-02-15 15:39:03 +01:00
from PyQt5.QtCore import QUrl
2018-02-14 20:03:01 +01:00
from qutebrowser.utils import urlmatch
@pytest.mark.parametrize('pattern, error', [
# Chromium: PARSE_ERROR_MISSING_SCHEME_SEPARATOR
("http", "No scheme given"),
("http:", "Pattern without host"),
("http:/", "Pattern without host"),
("about://", "Pattern without path"),
("http:/bar", "Pattern without host"),
# Chromium: PARSE_ERROR_EMPTY_HOST
("http://", "Pattern without host"),
("http:///", "Pattern without host"),
("http:// /", "Pattern without host"),
# Chromium: PARSE_ERROR_EMPTY_PATH
# FIXME: should we allow this or not?
# ("http://bar", "URLPattern::"),
# Chromium: PARSE_ERROR_INVALID_HOST
("http://\0www/", "May not contain NUL byte"),
2018-02-14 20:03:01 +01:00
# Chromium: PARSE_ERROR_INVALID_HOST_WILDCARD
("http://*foo/bar", "Invalid host wildcard"),
("http://foo.*.bar/baz", "Invalid host wildcard"),
("http://fo.*.ba:123/baz", "Invalid host wildcard"),
("http://foo.*/bar", "TLD wildcards are not implemented yet"),
2018-02-14 23:32:40 +01:00
# Chromium: PARSE_ERROR_INVALID_PORT
("http://foo:/", "Empty port"),
("http://*.foo:/", "Empty port"),
("http://foo:com/", "Invalid port"),
("http://foo:123456/", "Invalid port"),
("http://foo:80:80/monkey", "Invalid port"),
("chrome://foo:1234/bar", "Ports are unsupported with chrome scheme"),
2018-02-14 20:03:01 +01:00
])
def test_invalid_patterns(pattern, error):
with pytest.raises(urlmatch.ParseError, match=error):
2018-02-14 20:03:01 +01:00
urlmatch.UrlPattern(pattern)
2018-02-14 22:30:35 +01:00
@pytest.mark.parametrize('pattern, port', [
("http://foo:1234/", 1234),
("http://foo:1234/bar", 1234),
("http://*.foo:1234/", 1234),
("http://*.foo:1234/bar", 1234),
2018-02-14 23:29:02 +01:00
# FIXME Why is this valid in Chromium?
# ("http://:1234/", 1234),
2018-02-15 14:29:39 +01:00
("http://foo:*/", None),
("file://foo:1234/bar", None),
# Port-like strings in the path should not trigger a warning.
("http://*/:1234", None),
("http://*.foo/bar:1234", None),
("http://foo/bar:1234/path", None),
# We don't implement ALLOW_WILDCARD_FOR_EFFECTIVE_TLD yet.
# ("http://*.foo.*/:1234", None),
2018-02-14 22:30:35 +01:00
])
2018-02-14 23:32:40 +01:00
def test_port(pattern, port):
2018-02-14 22:30:35 +01:00
up = urlmatch.UrlPattern(pattern)
assert up._port == port
2018-02-15 15:39:03 +01:00
2018-02-15 15:51:26 +01:00
class TestMatchAllPagesForGivenScheme:
@pytest.fixture
def up(self):
return urlmatch.UrlPattern("http://*/*")
def test_attrs(self, up):
assert up._scheme == 'http'
2018-02-15 16:42:41 +01:00
assert up._host is None
2018-02-15 15:51:26 +01:00
assert up._match_subdomains
assert not up._match_all
2018-02-15 17:10:53 +01:00
assert up._path is None
2018-02-15 15:51:26 +01:00
@pytest.mark.parametrize('url, expected', [
("http://google.com", True),
("http://yahoo.com", True),
("http://google.com/foo", True),
("https://google.com", False),
("http://74.125.127.100/search", True),
])
def test_urls(self, up, url, expected):
assert up.matches(QUrl(url)) == expected
2018-02-15 16:19:22 +01:00
class TestMatchAllDomains:
@pytest.fixture
def up(self):
return urlmatch.UrlPattern("https://*/foo*")
def test_attrs(self, up):
assert up._scheme == 'https'
2018-02-15 16:42:41 +01:00
assert up._host is None
2018-02-15 16:19:22 +01:00
assert up._match_subdomains
assert not up._match_all
assert up._path == '/foo*'
@pytest.mark.parametrize('url, expected', [
("https://google.com/foo", True),
("https://google.com/foobar", True),
("http://google.com/foo", False),
("https://google.com/", False),
])
def test_urls(self, up, url, expected):
assert up.matches(QUrl(url)) == expected
class TestMatchSubdomains:
@pytest.fixture
def up(self):
return urlmatch.UrlPattern("http://*.google.com/foo*bar")
def test_attrs(self, up):
assert up._scheme == 'http'
assert up._host == 'google.com'
assert up._match_subdomains
assert not up._match_all
assert up._path == '/foo*bar'
@pytest.mark.parametrize('url, expected', [
("http://google.com/foobar", True),
# FIXME The ?bar seems to be treated as path by GURL but as query by
# QUrl.
# ("http://www.google.com/foo?bar", True),
("http://monkey.images.google.com/foooobar", True),
("http://yahoo.com/foobar", False),
])
def test_urls(self, up, url, expected):
assert up.matches(QUrl(url)) == expected
2018-02-15 16:39:07 +01:00
class TestMatchGlobEscaping:
@pytest.fixture
def up(self):
return urlmatch.UrlPattern(r"file:///foo-bar\*baz")
def test_attrs(self, up):
assert up._scheme == 'file'
2018-02-15 16:42:41 +01:00
assert up._host is None
2018-02-15 16:39:07 +01:00
assert not up._match_subdomains
assert not up._match_all
assert up._path == r'/foo-bar\*baz'
@pytest.mark.parametrize('url, expected', [
# We use - instead of ? so it doesn't get treated as query
(r"file:///foo-bar\hellobaz", True),
(r"file:///fooXbar\hellobaz", False),
])
def test_urls(self, up, url, expected):
assert up.matches(QUrl(url)) == expected
class TestMatchIpAddresses:
@pytest.mark.parametrize('pattern, host, match_subdomains', [
("http://127.0.0.1/*", "127.0.0.1", False),
("http://*.0.0.1/*", "0.0.1", True),
])
def test_attrs(self, pattern, host, match_subdomains):
up = urlmatch.UrlPattern(pattern)
assert up._scheme == 'http'
assert up._host == host
assert up._match_subdomains == match_subdomains
assert not up._match_all
2018-02-15 17:10:53 +01:00
assert up._path is None
@pytest.mark.parametrize('pattern, expected', [
("http://127.0.0.1/*", True),
# No subdomain matching is done with IPs
("http://*.0.0.1/*", False),
])
def test_urls(self, pattern, expected):
up = urlmatch.UrlPattern(pattern)
assert up.matches(QUrl("http://127.0.0.1")) == expected
2018-02-15 17:03:47 +01:00
class TestMatchChromeUrls:
@pytest.fixture
def up(self):
return urlmatch.UrlPattern("chrome://favicon/*")
def test_attrs(self, up):
assert up._scheme == 'chrome'
assert up._host == 'favicon'
assert not up._match_subdomains
assert not up._match_all
2018-02-15 17:10:53 +01:00
assert up._path is None
2018-02-15 17:03:47 +01:00
@pytest.mark.parametrize('url, expected', [
("chrome://favicon/http://google.com", True),
("chrome://favicon/https://google.com", True),
("chrome://history", False),
])
def test_urls(self, up, url, expected):
assert up.matches(QUrl(url)) == expected
class TestMatchAnything:
2018-02-15 17:11:24 +01:00
@pytest.fixture(params=['*://*/*', '<all_urls>'])
def up(self, request):
return urlmatch.UrlPattern(request.param)
2018-02-15 17:03:47 +01:00
2018-02-15 17:11:24 +01:00
def test_attrs_common(self, up):
2018-02-15 17:03:47 +01:00
assert up._scheme is None
assert up._host is None
2018-02-15 17:11:24 +01:00
assert up._path is None
def test_attrs_wildcard(self):
up = urlmatch.UrlPattern('*://*/*')
2018-02-15 17:03:47 +01:00
assert up._match_subdomains
assert not up._match_all
2018-02-15 17:11:24 +01:00
def test_attrs_all(self):
up = urlmatch.UrlPattern('<all_urls>')
assert not up._match_subdomains
assert up._match_all
2018-02-15 17:03:47 +01:00
@pytest.mark.parametrize('url', [
"http://127.0.0.1",
2018-02-15 17:03:47 +01:00
# We deviate from Chromium as we allow other schemes as well
"chrome://favicon/http://google.com",
"file:///foo/bar",
"file://localhost/foo/bar",
"qute://version",
"about:blank",
"data:text/html;charset=utf-8,<html>asdf</html>",
2018-02-15 17:03:47 +01:00
])
def test_urls(self, up, url):
assert up.matches(QUrl(url))
@pytest.mark.parametrize('pattern, url, expected', [
("about:*", "about:blank", True),
("about:blank", "about:blank", True),
("about:*", "about:version", True),
("data:*", "data:monkey", True),
("javascript:*", "javascript:atemyhomework", True),
("data:*", "about:blank", False),
])
def test_special_schemes(pattern, url, expected):
assert urlmatch.UrlPattern(pattern).matches(QUrl(url)) == expected
2018-02-15 17:42:24 +01:00
class TestFileScheme:
@pytest.fixture(params=[
'file:///foo*',
'file://foo*',
# FIXME This doesn't pass all tests
pytest.param('file://localhost/foo*', marks=pytest.mark.skip(
reason="We're not handling this correctly in all cases"))
])
def up(self, request):
return urlmatch.UrlPattern(request.param)
def test_attrs(self, up):
assert up._scheme == 'file'
assert up._host is None
assert not up._match_subdomains
assert not up._match_all
assert up._path == '/foo*'
@pytest.mark.parametrize('url, expected', [
("file://foo", False),
("file://foobar", False),
("file:///foo", True),
("file:///foobar", True),
("file://localhost/foo", True),
])
def test_urls(self, up, url, expected):
assert up.matches(QUrl(url)) == expected