| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
import tests.xdp_doc_utils as xdp_doc |
| 7 |
|
| 8 |
import dbus |
| 9 |
import pytest |
| 10 |
import os |
| 11 |
from pathlib import Path |
| 12 |
from typing import Any |
| 13 |
|
| 14 |
|
| 15 |
defaults_list = b"""[Default Applications] |
| 16 |
x-scheme-handler/http=furrfix.desktop; |
| 17 |
text/plain=furrfix.desktop |
| 18 |
""" |
| 19 |
|
| 20 |
furrfix_desktop = b"""[Desktop Entry] |
| 21 |
Version=1.0 |
| 22 |
Name=Furrfix |
| 23 |
GenericName=Not a Web Browser |
| 24 |
Comment=Don't Browse the Web |
| 25 |
Exec=true %u |
| 26 |
Icon=furrfix |
| 27 |
Terminal=false |
| 28 |
Type=Application |
| 29 |
MimeType=text/plain;text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/xdg-desktop-portal-test; |
| 30 |
StartupNotify=true |
| 31 |
Categories=Network;WebBrowser; |
| 32 |
Keywords=web;browser;internet; |
| 33 |
""" |
| 34 |
|
| 35 |
furrfix_desktop2 = b"""[Desktop Entry] |
| 36 |
Version=1.0 |
| 37 |
Name=Furrfix2 |
| 38 |
GenericName=Not a Web Browser 2 |
| 39 |
Comment=Don't Browse the Web |
| 40 |
Exec=true %u |
| 41 |
Icon=furrfix2 |
| 42 |
Terminal=false |
| 43 |
Type=Application |
| 44 |
MimeType=text/plain;text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/xdg-desktop-portal-test; |
| 45 |
StartupNotify=true |
| 46 |
Categories=Network;WebBrowser; |
| 47 |
Keywords=web;browser;internet; |
| 48 |
""" |
| 49 |
|
| 50 |
mimeinfo_cache = b"""[MIME Cache] |
| 51 |
application/vnd.mozilla.xul+xml=furrfix.desktop;furrfix2.desktop; |
| 52 |
application/xhtml+xml=furrfix.desktop;furrfix2.desktop; |
| 53 |
text/plain=furrfix.desktop;furrfix2.desktop; |
| 54 |
text/html=furrfix.desktop;furrfix2.desktop; |
| 55 |
text/mml=furrfix.desktop;furrfix2.desktop; |
| 56 |
text/xml=furrfix.desktop;furrfix2.desktop; |
| 57 |
x-scheme-handler/http=furrfix.desktop;furrfix2.desktop; |
| 58 |
x-scheme-handler/https=furrfix.desktop;furrfix2.desktop; |
| 59 |
x-scheme-handler/xdg-desktop-portal-test=furrfix.desktop;furrfix2.desktop; |
| 60 |
""" |
| 61 |
|
| 62 |
|
| 63 |
@pytest.fixture |
| 64 |
def xdg_data_home_files(): |
| 65 |
return { |
| 66 |
"applications/defaults.list": defaults_list, |
| 67 |
"applications/furrfix.desktop": furrfix_desktop, |
| 68 |
"applications/furrfix2.desktop": furrfix_desktop, |
| 69 |
"applications/mimeinfo.cache": mimeinfo_cache, |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture |
| 74 |
def required_templates(): |
| 75 |
return { |
| 76 |
"appchooser": {}, |
| 77 |
"lockdown": {}, |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
class TestOpenURI: |
| 82 |
def set_permissions(self, dbus_con, type, permissions): |
| 83 |
perm_store_intf = xdp.get_permission_store_iface(dbus_con) |
| 84 |
perm_store_intf.SetPermission( |
| 85 |
"desktop-used-apps", |
| 86 |
True, |
| 87 |
"inhibit", |
| 88 |
type, |
| 89 |
permissions, |
| 90 |
) |
| 91 |
|
| 92 |
def enable_paranoid_mode(self, dbus_con, type): |
| 93 |
# turn on paranoid mode to ensure we get a backend call |
| 94 |
perm_store_intf = xdp.get_permission_store_iface(dbus_con) |
| 95 |
perm_store_intf.SetValue( |
| 96 |
"desktop-used-apps", |
| 97 |
True, |
| 98 |
type, |
| 99 |
dbus.Dictionary( |
| 100 |
{ |
| 101 |
"always-ask": True, |
| 102 |
}, |
| 103 |
signature="sv", |
| 104 |
), |
| 105 |
) |
| 106 |
|
| 107 |
def test_version(self, portals, dbus_con): |
| 108 |
xdp.check_version(dbus_con, "OpenURI", 5) |
| 109 |
|
| 110 |
def test_http1(self, portals, dbus_con, xdp_app_info): |
| 111 |
app_id = xdp_app_info.app_id |
| 112 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 113 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 114 |
|
| 115 |
scheme_handler = "x-scheme-handler/http" |
| 116 |
self.enable_paranoid_mode(dbus_con, scheme_handler) |
| 117 |
|
| 118 |
uri = "http://www.flatpak.org" |
| 119 |
writable = False |
| 120 |
activation_token = "token" |
| 121 |
|
| 122 |
request = xdp.Request(dbus_con, openuri_intf) |
| 123 |
options = { |
| 124 |
"writable": writable, |
| 125 |
"activation_token": activation_token, |
| 126 |
} |
| 127 |
response = request.call( |
| 128 |
"OpenURI", |
| 129 |
parent_window="", |
| 130 |
uri=uri, |
| 131 |
options=options, |
| 132 |
) |
| 133 |
|
| 134 |
assert response |
| 135 |
assert response.response == 0 |
| 136 |
|
| 137 |
# Check the impl portal was called with the right args |
| 138 |
method_calls = mock_intf.GetMethodCalls("ChooseApplication") |
| 139 |
assert len(method_calls) > 0 |
| 140 |
_, args = method_calls[-1] |
| 141 |
assert args[1] == app_id |
| 142 |
assert args[2] == "" # parent window |
| 143 |
assert "furrfix" in args[3] |
| 144 |
assert args[4]["uri"] == uri |
| 145 |
assert args[4]["content_type"] == scheme_handler |
| 146 |
assert args[4]["activation_token"] == activation_token |
| 147 |
|
| 148 |
def test_http2(self, portals, dbus_con): |
| 149 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 150 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 151 |
|
| 152 |
scheme_handler = "x-scheme-handler/http" |
| 153 |
self.set_permissions(dbus_con, scheme_handler, ["furrfix", "3", "3"]) |
| 154 |
|
| 155 |
uri = "http://www.flatpak.org" |
| 156 |
writable = False |
| 157 |
activation_token = "token" |
| 158 |
|
| 159 |
request = xdp.Request(dbus_con, openuri_intf) |
| 160 |
options = { |
| 161 |
"writable": writable, |
| 162 |
"activation_token": activation_token, |
| 163 |
} |
| 164 |
response = request.call( |
| 165 |
"OpenURI", |
| 166 |
parent_window="", |
| 167 |
uri=uri, |
| 168 |
options=options, |
| 169 |
) |
| 170 |
|
| 171 |
assert response |
| 172 |
assert response.response == 0 |
| 173 |
|
| 174 |
# Check the impl portal was not called because the choice thresold |
| 175 |
# has been reached |
| 176 |
method_calls = mock_intf.GetMethodCalls("ChooseApplication") |
| 177 |
assert len(method_calls) == 0 |
| 178 |
|
| 179 |
def test_file(self, portals, dbus_con, xdp_app_info): |
| 180 |
app_id = xdp_app_info.app_id |
| 181 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 182 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 183 |
|
| 184 |
scheme_handler = "text/plain" |
| 185 |
self.enable_paranoid_mode(dbus_con, scheme_handler) |
| 186 |
|
| 187 |
file_path = Path.home() / "openuri_mock_file" |
| 188 |
file_path.write_text("openuri_mock_file") |
| 189 |
fd = os.open(file_path.absolute().as_posix(), os.O_RDONLY) |
| 190 |
|
| 191 |
writable = False |
| 192 |
activation_token = "token" |
| 193 |
|
| 194 |
request = xdp.Request(dbus_con, openuri_intf) |
| 195 |
options = { |
| 196 |
"writable": writable, |
| 197 |
"activation_token": activation_token, |
| 198 |
} |
| 199 |
response = request.call( |
| 200 |
"OpenFile", |
| 201 |
parent_window="", |
| 202 |
fd=fd, |
| 203 |
options=options, |
| 204 |
) |
| 205 |
|
| 206 |
os.close(fd) |
| 207 |
|
| 208 |
assert response |
| 209 |
assert response.response == 0 |
| 210 |
|
| 211 |
# Check the impl portal was called with the right args |
| 212 |
method_calls = mock_intf.GetMethodCalls("ChooseApplication") |
| 213 |
assert len(method_calls) > 0 |
| 214 |
_, args = method_calls[-1] |
| 215 |
assert args[1] == app_id |
| 216 |
assert args[2] == "" # parent window |
| 217 |
assert "furrfix" in args[3] |
| 218 |
assert args[4]["content_type"] == scheme_handler |
| 219 |
assert args[4]["activation_token"] == activation_token |
| 220 |
|
| 221 |
path = args[4]["uri"] |
| 222 |
assert path.startswith("file:///") |
| 223 |
|
| 224 |
with open(path[7:]) as file: |
| 225 |
openuri_file_contents = file.read() |
| 226 |
assert openuri_file_contents == "openuri_mock_file" |
| 227 |
|
| 228 |
@pytest.mark.parametrize("template_params", ({"appchooser": {"response": 1}},)) |
| 229 |
def test_cancel(self, portals, dbus_con): |
| 230 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 231 |
|
| 232 |
scheme_handler = "x-scheme-handler/http" |
| 233 |
self.enable_paranoid_mode(dbus_con, scheme_handler) |
| 234 |
|
| 235 |
uri = "http://www.flatpak.org" |
| 236 |
|
| 237 |
request = xdp.Request(dbus_con, openuri_intf) |
| 238 |
options: Any = {} |
| 239 |
response = request.call( |
| 240 |
"OpenURI", |
| 241 |
parent_window="", |
| 242 |
uri=uri, |
| 243 |
options=options, |
| 244 |
) |
| 245 |
|
| 246 |
assert response |
| 247 |
assert response.response == 1 |
| 248 |
|
| 249 |
@pytest.mark.parametrize( |
| 250 |
"template_params", ({"appchooser": {"expect-close": True}},) |
| 251 |
) |
| 252 |
def test_close(self, portals, dbus_con, xdp_app_info): |
| 253 |
app_id = xdp_app_info.app_id |
| 254 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 255 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 256 |
|
| 257 |
scheme_handler = "x-scheme-handler/http" |
| 258 |
self.enable_paranoid_mode(dbus_con, scheme_handler) |
| 259 |
|
| 260 |
uri = "http://www.flatpak.org" |
| 261 |
writable = False |
| 262 |
activation_token = "token" |
| 263 |
|
| 264 |
request = xdp.Request(dbus_con, openuri_intf) |
| 265 |
request.schedule_close(1000) |
| 266 |
options = { |
| 267 |
"writable": writable, |
| 268 |
"activation_token": activation_token, |
| 269 |
} |
| 270 |
request.call( |
| 271 |
"OpenURI", |
| 272 |
parent_window="", |
| 273 |
uri=uri, |
| 274 |
options=options, |
| 275 |
) |
| 276 |
|
| 277 |
# Only true if the impl.Request was closed too |
| 278 |
assert request.closed |
| 279 |
|
| 280 |
# Check the impl portal was called with the right args |
| 281 |
method_calls = mock_intf.GetMethodCalls("ChooseApplication") |
| 282 |
assert len(method_calls) > 0 |
| 283 |
_, args = method_calls[-1] |
| 284 |
assert args[1] == app_id |
| 285 |
assert args[2] == "" # parent window |
| 286 |
assert "furrfix" in args[3] |
| 287 |
assert args[4]["uri"] == uri |
| 288 |
assert args[4]["content_type"] == scheme_handler |
| 289 |
assert args[4]["activation_token"] == activation_token |
| 290 |
|
| 291 |
@pytest.mark.parametrize( |
| 292 |
"template_params", ({"lockdown": {"disable-application-handlers": True}},) |
| 293 |
) |
| 294 |
def test_lockdown(self, portals, dbus_con): |
| 295 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 296 |
|
| 297 |
scheme_handler = "x-scheme-handler/http" |
| 298 |
self.enable_paranoid_mode(dbus_con, scheme_handler) |
| 299 |
|
| 300 |
uri = "http://www.flatpak.org" |
| 301 |
writable = False |
| 302 |
activation_token = "token" |
| 303 |
|
| 304 |
request = xdp.Request(dbus_con, openuri_intf) |
| 305 |
options = { |
| 306 |
"writable": writable, |
| 307 |
"activation_token": activation_token, |
| 308 |
} |
| 309 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 310 |
request.call( |
| 311 |
"OpenURI", |
| 312 |
parent_window="", |
| 313 |
uri=uri, |
| 314 |
options=options, |
| 315 |
) |
| 316 |
assert ( |
| 317 |
excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed" |
| 318 |
) |
| 319 |
|
| 320 |
def test_dir(self, portals, dbus_con, xdp_app_info): |
| 321 |
app_id = xdp_app_info.app_id |
| 322 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 323 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 324 |
|
| 325 |
scheme_handler = "inode/directory" |
| 326 |
self.enable_paranoid_mode(dbus_con, scheme_handler) |
| 327 |
|
| 328 |
file_path = Path.home() / "openuri_mock_file" |
| 329 |
file_path.write_text("openuri_mock_file") |
| 330 |
fd = os.open(file_path.absolute().as_posix(), os.O_RDONLY) |
| 331 |
|
| 332 |
activation_token = "token" |
| 333 |
|
| 334 |
request = xdp.Request(dbus_con, openuri_intf) |
| 335 |
options = { |
| 336 |
"activation_token": activation_token, |
| 337 |
} |
| 338 |
response = request.call( |
| 339 |
"OpenDirectory", |
| 340 |
parent_window="", |
| 341 |
fd=fd, |
| 342 |
options=options, |
| 343 |
) |
| 344 |
|
| 345 |
os.close(fd) |
| 346 |
|
| 347 |
assert response |
| 348 |
assert response.response == 0 |
| 349 |
|
| 350 |
# Check the appchooser portal got called to open the containing dir |
| 351 |
method_calls = mock_intf.GetMethodCalls("ChooseApplication") |
| 352 |
assert len(method_calls) > 0 |
| 353 |
_, args = method_calls[-1] |
| 354 |
assert args[1] == app_id |
| 355 |
assert args[2] == "" # parent window |
| 356 |
assert args[4]["content_type"] == scheme_handler |
| 357 |
assert args[4]["activation_token"] == activation_token |
| 358 |
|
| 359 |
path = args[4]["uri"] |
| 360 |
assert path.startswith("file:///") |
| 361 |
|
| 362 |
assert Path(path[7:]) == Path(file_path).parent |
| 363 |
|
| 364 |
def test_scheme_supported(self, portals, dbus_con): |
| 365 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 366 |
|
| 367 |
supported = openuri_intf.SchemeSupported("https", {}) |
| 368 |
assert supported |
| 369 |
|
| 370 |
supported = openuri_intf.SchemeSupported("bogusnonexistanthandler", {}) |
| 371 |
assert not supported |
| 372 |
|
| 373 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 374 |
openuri_intf.SchemeSupported("", {}) |
| 375 |
assert ( |
| 376 |
excinfo.value.get_dbus_name() |
| 377 |
== "org.freedesktop.portal.Error.InvalidArgument" |
| 378 |
) |
| 379 |
|
| 380 |
# tests mapping from |
| 381 |
# /run/user/1000/doc/_id_/openuri-test -> /home/user/openuri-test |
| 382 |
@pytest.mark.parametrize( |
| 383 |
"path", ("file.html", "dir/file.html", "dir/subdir/file.html") |
| 384 |
) |
| 385 |
def test_openfile_opens_host_path( |
| 386 |
self, xdg_document_portal, portals, dbus_con, xdp_app_info, path |
| 387 |
): |
| 388 |
app_id = xdp_app_info.app_id |
| 389 |
openuri_intf = xdp.get_portal_iface(dbus_con, "OpenURI") |
| 390 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 391 |
mountpoint = xdp_doc.get_mountpoint(documents_intf) |
| 392 |
|
| 393 |
# create directory in host which will be added to document portal |
| 394 |
export_path = Path.home() / "openuri-test" |
| 395 |
export_path.mkdir(parents=True) |
| 396 |
doc_ids = xdp_doc.export_files( |
| 397 |
documents_intf, |
| 398 |
[export_path], |
| 399 |
["read", "write"], |
| 400 |
flags=xdp_doc.EXPORT_FILES_FLAG_EXPORT_DIR, |
| 401 |
) |
| 402 |
assert doc_ids |
| 403 |
doc_id = doc_ids[0][0] |
| 404 |
assert doc_id |
| 405 |
|
| 406 |
# create file in the directory which was added to document directory |
| 407 |
file_host_path = export_path / path |
| 408 |
file_doc_path = mountpoint / doc_id / "openuri-test" / path |
| 409 |
|
| 410 |
file_host_path.parent.mkdir(parents=True, exist_ok=True) |
| 411 |
file_host_path.write_bytes(b"openuri_mock_file_content") |
| 412 |
|
| 413 |
# Call OpenFile by using fd |
| 414 |
with open(file_doc_path) as f: |
| 415 |
fd = f.fileno() |
| 416 |
assert fd |
| 417 |
activation_token = "token" |
| 418 |
request = xdp.Request(dbus_con, openuri_intf) |
| 419 |
options = { |
| 420 |
"writable": False, |
| 421 |
"activation_token": activation_token, |
| 422 |
} |
| 423 |
|
| 424 |
response = request.call( |
| 425 |
"OpenFile", |
| 426 |
parent_window="", |
| 427 |
fd=fd, |
| 428 |
options=options, |
| 429 |
) |
| 430 |
assert response |
| 431 |
assert response.response == 0 |
| 432 |
|
| 433 |
# Check the impl portal was called with the right args |
| 434 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 435 |
method_calls = mock_intf.GetMethodCalls("ChooseApplication") |
| 436 |
assert len(method_calls) > 0 |
| 437 |
_, args = method_calls[-1] |
| 438 |
assert args[1] == app_id |
| 439 |
assert args[2] == "" # parent window |
| 440 |
assert "furrfix" in args[3] |
| 441 |
|
| 442 |
assert args[4]["activation_token"] == activation_token |
| 443 |
|
| 444 |
path = args[4]["uri"] |
| 445 |
assert path == "file://" + file_host_path.absolute().as_posix() |
| 446 |
assert file_doc_path != file_host_path.absolute().as_posix() |