| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests as xdp |
| 6 |
|
| 7 |
import pytest |
| 8 |
import dbus |
| 9 |
from pathlib import Path |
| 10 |
import os |
| 11 |
from gi.repository import GLib, Gio |
| 12 |
|
| 13 |
|
| 14 |
EXPORT_FILES_FLAG_EXPORT_DIR = 8 |
| 15 |
|
| 16 |
|
| 17 |
def path_from_null_term_bytes(bytes): |
| 18 |
path_bytes, rest = bytes.split(b"\x00") |
| 19 |
assert rest == b"" |
| 20 |
return Path(os.fsdecode(path_bytes)) |
| 21 |
|
| 22 |
|
| 23 |
def get_mountpoint(documents_intf): |
| 24 |
mountpoint = documents_intf.GetMountPoint(byte_arrays=True) |
| 25 |
mountpoint = path_from_null_term_bytes(mountpoint) |
| 26 |
assert mountpoint.exists() |
| 27 |
return mountpoint |
| 28 |
|
| 29 |
|
| 30 |
def write_bytes_atomic(file_path, bytes): |
| 31 |
GLib.file_set_contents(file_path.absolute().as_posix(), bytes) |
| 32 |
|
| 33 |
|
| 34 |
def write_bytes_trunc(file_path, bytes): |
| 35 |
try: |
| 36 |
fd = os.open( |
| 37 |
file_path.absolute().as_posix(), os.O_RDWR | os.O_TRUNC | os.O_CREAT |
| 38 |
) |
| 39 |
os.write(fd, bytes) |
| 40 |
finally: |
| 41 |
os.close(fd) |
| 42 |
|
| 43 |
|
| 44 |
def get_host_path_attr(path): |
| 45 |
xattr = "xattr::document-portal.host-path" |
| 46 |
file = Gio.file_new_for_path(path.absolute().as_posix()) |
| 47 |
info = file.query_info(xattr, Gio.FileQueryInfoFlags.NONE) |
| 48 |
host_path = info.get_attribute_as_string(xattr) |
| 49 |
if not host_path: |
| 50 |
return None |
| 51 |
return Path(os.fsdecode(host_path)) |
| 52 |
|
| 53 |
|
| 54 |
def export_file(documents_intf, file_path, unique=False): |
| 55 |
assert file_path.exists() |
| 56 |
|
| 57 |
with open(file_path.absolute().as_posix(), "r") as file: |
| 58 |
doc_id = documents_intf.Add(file.fileno(), not unique, False) |
| 59 |
assert doc_id |
| 60 |
|
| 61 |
return doc_id |
| 62 |
|
| 63 |
|
| 64 |
def export_file_named(documents_intf, folder_path, name, unique=False): |
| 65 |
assert folder_path.exists() |
| 66 |
|
| 67 |
# bytestring convention is zero terminated |
| 68 |
name_nt = os.fsencode(name) + b"\x00" |
| 69 |
|
| 70 |
try: |
| 71 |
fd = os.open(folder_path.absolute().as_posix(), os.O_PATH | os.O_CLOEXEC) |
| 72 |
doc_id = documents_intf.AddNamed(fd, name_nt, not unique, False) |
| 73 |
assert doc_id |
| 74 |
finally: |
| 75 |
os.close(fd) |
| 76 |
|
| 77 |
return doc_id |
| 78 |
|
| 79 |
|
| 80 |
def export_files(documents_intf, file_paths, perms, flags=0, app_id=""): |
| 81 |
fds = [] |
| 82 |
try: |
| 83 |
for file_path in file_paths: |
| 84 |
fds.append( |
| 85 |
os.open(file_path.absolute().as_posix(), os.O_PATH | os.O_CLOEXEC) |
| 86 |
) |
| 87 |
|
| 88 |
result = documents_intf.AddFull( |
| 89 |
fds, |
| 90 |
flags, |
| 91 |
app_id, |
| 92 |
perms, |
| 93 |
byte_arrays=True, |
| 94 |
) |
| 95 |
finally: |
| 96 |
for fd in fds: |
| 97 |
os.close(fd) |
| 98 |
|
| 99 |
assert result |
| 100 |
return result |
| 101 |
|
| 102 |
|
| 103 |
class TestDocuments: |
| 104 |
def test_version(self, xdg_document_portal, dbus_con): |
| 105 |
documents = dbus_con.get_object( |
| 106 |
"org.freedesktop.portal.Documents", |
| 107 |
"/org/freedesktop/portal/documents", |
| 108 |
) |
| 109 |
|
| 110 |
properties_intf = dbus.Interface( |
| 111 |
documents, |
| 112 |
"org.freedesktop.DBus.Properties", |
| 113 |
) |
| 114 |
portal_version = properties_intf.Get( |
| 115 |
"org.freedesktop.portal.Documents", |
| 116 |
"version", |
| 117 |
) |
| 118 |
assert int(portal_version) == 5 |
| 119 |
|
| 120 |
def test_mount_point(self, xdg_document_portal, dbus_con): |
| 121 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 122 |
get_mountpoint(documents_intf) |
| 123 |
|
| 124 |
def test_create_doc(self, xdg_document_portal, dbus_con): |
| 125 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 126 |
mountpoint = get_mountpoint(documents_intf) |
| 127 |
|
| 128 |
content = b"content" |
| 129 |
file_name = "a-file" |
| 130 |
|
| 131 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 132 |
write_bytes_atomic(file_path, content) |
| 133 |
doc_id = export_file(documents_intf, file_path) |
| 134 |
|
| 135 |
doc_path = mountpoint / doc_id |
| 136 |
doc_app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id |
| 137 |
doc_app2_path = mountpoint / "by-app" / "com.test.App2" / doc_id |
| 138 |
|
| 139 |
# Make sure it got exported |
| 140 |
assert (doc_path / file_name).read_bytes() == content |
| 141 |
|
| 142 |
assert not (doc_path / "another-file").exists() |
| 143 |
assert not (mountpoint / "anotherid" / file_name).exists() |
| 144 |
|
| 145 |
# Make sure it is not viewable by apps |
| 146 |
assert not doc_app1_path.exists() |
| 147 |
assert not doc_app2_path.exists() |
| 148 |
|
| 149 |
# Create a tmp file in same dir, ensure it works and can't be seen by other apps |
| 150 |
write_bytes_atomic(doc_path / "tmp1", b"tmpdata1") |
| 151 |
assert (doc_path / "tmp1").read_bytes() == b"tmpdata1" |
| 152 |
assert not (doc_app1_path / "tmp1").exists() |
| 153 |
|
| 154 |
# Ensure App 1 and only it can see the document and tmpfile |
| 155 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["read"]) |
| 156 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 157 |
assert not (doc_app2_path / file_name).exists() |
| 158 |
|
| 159 |
# Make sure App 1 can't create a tmpfile |
| 160 |
assert not (doc_app1_path / "tmp2").exists() |
| 161 |
with pytest.raises(PermissionError): |
| 162 |
(doc_app1_path / "tmp2").write_bytes(b"tmpdata1") |
| 163 |
assert not (doc_app1_path / "tmp2").exists() |
| 164 |
assert not (doc_path / "tmp2").exists() |
| 165 |
|
| 166 |
# Update the document contents, ensure this is propagated |
| 167 |
content = b"content2" |
| 168 |
write_bytes_atomic(doc_path / file_name, content) |
| 169 |
assert (doc_path / file_name).read_bytes() == content |
| 170 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 171 |
assert file_path.read_bytes() == content |
| 172 |
assert not (doc_app2_path / file_name).exists() |
| 173 |
assert not (doc_app2_path / "tmp1").exists() |
| 174 |
|
| 175 |
# Update the document contents outside fuse fd, ensure this is propagated |
| 176 |
content = b"content3" |
| 177 |
write_bytes_atomic(file_path, content) |
| 178 |
assert (doc_path / file_name).read_bytes() == content |
| 179 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 180 |
assert file_path.read_bytes() == content |
| 181 |
assert not (doc_app2_path / file_name).exists() |
| 182 |
assert not (doc_app2_path / "tmp1").exists() |
| 183 |
|
| 184 |
# Try to update the doc from an app that can't write to it |
| 185 |
with pytest.raises(PermissionError): |
| 186 |
(doc_app1_path / file_name).write_bytes(b"content4") |
| 187 |
|
| 188 |
# Update the doc from an app with write access |
| 189 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["write"]) |
| 190 |
content = b"content5" |
| 191 |
write_bytes_atomic(doc_app1_path / file_name, content) |
| 192 |
assert (doc_path / file_name).read_bytes() == content |
| 193 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 194 |
assert file_path.read_bytes() == content |
| 195 |
assert not (doc_app2_path / file_name).exists() |
| 196 |
|
| 197 |
# Try to create a tmp file for an app |
| 198 |
assert not (doc_app1_path / "tmp3").exists() |
| 199 |
write_bytes_atomic(doc_app1_path / "tmp3", b"tmpdata2") |
| 200 |
(doc_app1_path / "tmp3").read_bytes() == b"tmpdata2" |
| 201 |
assert not (doc_path / "tmp3").exists() |
| 202 |
|
| 203 |
# Re-Create a file from a fuse document file, in various ways |
| 204 |
doc_id2 = export_file(documents_intf, (doc_path / file_name)) |
| 205 |
assert doc_id2 == doc_id |
| 206 |
doc_id3 = export_file(documents_intf, (doc_app1_path / file_name)) |
| 207 |
assert doc_id3 == doc_id |
| 208 |
doc_id4 = export_file(documents_intf, file_path) |
| 209 |
assert doc_id4 == doc_id |
| 210 |
|
| 211 |
# Ensure we can make a unique document |
| 212 |
doc_id5 = export_file(documents_intf, file_path, unique=True) |
| 213 |
assert doc_id5 != doc_id |
| 214 |
|
| 215 |
def test_recursive_doc(self, xdg_document_portal, dbus_con): |
| 216 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 217 |
mountpoint = get_mountpoint(documents_intf) |
| 218 |
|
| 219 |
content = b"content" |
| 220 |
file_name = "recursive-file" |
| 221 |
|
| 222 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 223 |
write_bytes_atomic(file_path, content) |
| 224 |
doc_id = export_file(documents_intf, file_path) |
| 225 |
|
| 226 |
doc_path = mountpoint / doc_id |
| 227 |
doc_app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id |
| 228 |
|
| 229 |
assert (doc_path / file_name).read_bytes() == content |
| 230 |
|
| 231 |
doc_id2 = export_file(documents_intf, doc_path / file_name) |
| 232 |
assert doc_id2 == doc_id |
| 233 |
|
| 234 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["read"]) |
| 235 |
|
| 236 |
doc_id3 = export_file(documents_intf, doc_app1_path / file_name) |
| 237 |
assert doc_id3 == doc_id |
| 238 |
|
| 239 |
def test_create_docs(self, xdg_document_portal, dbus_con): |
| 240 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 241 |
mountpoint = get_mountpoint(documents_intf) |
| 242 |
|
| 243 |
files = { |
| 244 |
"doc1": b"doc1-content", |
| 245 |
"doc2": b"doc2-content", |
| 246 |
} |
| 247 |
|
| 248 |
file_paths = [] |
| 249 |
for file_name, file_content in files.items(): |
| 250 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 251 |
write_bytes_atomic(file_path, file_content) |
| 252 |
file_paths.append(file_path) |
| 253 |
|
| 254 |
doc_ids, extra = export_files( |
| 255 |
documents_intf, file_paths, ["read"], app_id="org.other.App" |
| 256 |
) |
| 257 |
|
| 258 |
assert extra |
| 259 |
out_mountpoint = path_from_null_term_bytes(extra["mountpoint"]) |
| 260 |
assert out_mountpoint == mountpoint |
| 261 |
|
| 262 |
assert doc_ids |
| 263 |
for doc_id, (file_name, file_content) in zip(doc_ids, files.items()): |
| 264 |
assert (mountpoint / doc_id / file_name).read_bytes() == file_content |
| 265 |
assert (Path(os.environ["TMPDIR"]) / file_name).read_bytes() == file_content |
| 266 |
app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id / file_name |
| 267 |
app2_path = mountpoint / "by-app" / "com.test.App2" / doc_id / file_name |
| 268 |
assert not app1_path.exists() |
| 269 |
assert not app2_path.exists() |
| 270 |
assert not (mountpoint / doc_id / "another-file").exists() |
| 271 |
assert not (mountpoint / "anotherid" / file_name).exists() |
| 272 |
|
| 273 |
other_app_path = ( |
| 274 |
mountpoint / "by-app" / "org.other.App" / doc_id / file_name |
| 275 |
) |
| 276 |
assert other_app_path.read_bytes() == file_content |
| 277 |
with pytest.raises(PermissionError): |
| 278 |
other_app_path.write_bytes(b"new-content") |
| 279 |
|
| 280 |
def test_add_named(self, xdg_document_portal, dbus_con): |
| 281 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 282 |
mountpoint = get_mountpoint(documents_intf) |
| 283 |
|
| 284 |
content = b"content" |
| 285 |
file_name = "add-named-1" |
| 286 |
|
| 287 |
folder_path = Path(os.environ["TMPDIR"]) |
| 288 |
doc_id = export_file_named(documents_intf, folder_path, file_name) |
| 289 |
assert doc_id |
| 290 |
|
| 291 |
doc_path = mountpoint / doc_id |
| 292 |
doc_app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id |
| 293 |
doc_app2_path = mountpoint / "by-app" / "com.test.App2" / doc_id |
| 294 |
|
| 295 |
assert doc_path.exists() |
| 296 |
assert not doc_app1_path.exists() |
| 297 |
assert not (doc_path / file_name).exists() |
| 298 |
assert not (doc_app1_path / file_name).exists() |
| 299 |
|
| 300 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["read", "write"]) |
| 301 |
|
| 302 |
assert doc_path.exists() |
| 303 |
assert doc_app1_path.exists() |
| 304 |
assert not (doc_path / file_name).exists() |
| 305 |
assert not (doc_app1_path / file_name).exists() |
| 306 |
|
| 307 |
# Update truncating with no previous file |
| 308 |
write_bytes_trunc(doc_path / file_name, content) |
| 309 |
assert (doc_path / file_name).read_bytes() == content |
| 310 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 311 |
assert not (doc_app2_path / file_name).exists() |
| 312 |
|
| 313 |
# Update truncating with previous file |
| 314 |
content = b"content2" |
| 315 |
write_bytes_trunc(doc_path / file_name, content) |
| 316 |
assert (doc_path / file_name).read_bytes() == content |
| 317 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 318 |
assert not (doc_app2_path / file_name).exists() |
| 319 |
|
| 320 |
# Update atomic with previous file |
| 321 |
content = b"content3" |
| 322 |
write_bytes_atomic(doc_path / file_name, content) |
| 323 |
assert (doc_path / file_name).read_bytes() == content |
| 324 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 325 |
assert not (doc_app2_path / file_name).exists() |
| 326 |
|
| 327 |
# Update from host |
| 328 |
content = b"content4" |
| 329 |
write_bytes_atomic(folder_path / file_name, content) |
| 330 |
assert (doc_path / file_name).read_bytes() == content |
| 331 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 332 |
assert not (doc_app2_path / file_name).exists() |
| 333 |
|
| 334 |
# Unlink doc |
| 335 |
(doc_path / file_name).unlink() |
| 336 |
assert doc_path.exists() |
| 337 |
assert doc_app1_path.exists() |
| 338 |
assert not (doc_path / file_name).exists() |
| 339 |
assert not (doc_app1_path / file_name).exists() |
| 340 |
|
| 341 |
# Update atomic with no previous file |
| 342 |
content = b"content5" |
| 343 |
write_bytes_atomic(doc_path / file_name, content) |
| 344 |
assert (doc_path / file_name).read_bytes() == content |
| 345 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 346 |
assert not (doc_app2_path / file_name).exists() |
| 347 |
|
| 348 |
# Unlink doc on host |
| 349 |
(folder_path / file_name).unlink() |
| 350 |
assert doc_path.exists() |
| 351 |
assert doc_app1_path.exists() |
| 352 |
assert not (doc_path / file_name).exists() |
| 353 |
assert not (doc_app1_path / file_name).exists() |
| 354 |
|
| 355 |
# Update atomic with unexpected no previous file |
| 356 |
content = b"content6" |
| 357 |
write_bytes_atomic(doc_path / file_name, content) |
| 358 |
assert (doc_path / file_name).read_bytes() == content |
| 359 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 360 |
assert not (doc_app2_path / file_name).exists() |
| 361 |
|
| 362 |
# Unlink doc on host again |
| 363 |
(folder_path / file_name).unlink() |
| 364 |
assert doc_path.exists() |
| 365 |
assert doc_app1_path.exists() |
| 366 |
assert not (doc_path / file_name).exists() |
| 367 |
assert not (doc_app1_path / file_name).exists() |
| 368 |
|
| 369 |
# Update truncating with unexpected no previous file |
| 370 |
content = b"content7" |
| 371 |
write_bytes_trunc(doc_path / file_name, content) |
| 372 |
assert (doc_path / file_name).read_bytes() == content |
| 373 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 374 |
assert not (doc_app2_path / file_name).exists() |
| 375 |
|
| 376 |
def test_get_host_paths(self, xdg_document_portal, dbus_con): |
| 377 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 378 |
|
| 379 |
content = b"content" |
| 380 |
file_name = "host-path" |
| 381 |
|
| 382 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 383 |
write_bytes_atomic(file_path, content) |
| 384 |
doc_id = export_file(documents_intf, file_path) |
| 385 |
|
| 386 |
host_paths = documents_intf.GetHostPaths([doc_id], byte_arrays=True) |
| 387 |
assert doc_id in host_paths |
| 388 |
doc_host_path = path_from_null_term_bytes(host_paths[doc_id]) |
| 389 |
assert doc_host_path == file_path |
| 390 |
|
| 391 |
def test_host_paths_xattr(self, xdg_document_portal, dbus_con): |
| 392 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 393 |
mountpoint = get_mountpoint(documents_intf) |
| 394 |
|
| 395 |
base_path = Path(os.environ["TMPDIR"]) / "a" |
| 396 |
file_path = base_path / "b" / "c" |
| 397 |
file_path.parent.mkdir(parents=True, exist_ok=True) |
| 398 |
file_path.write_bytes(b"test") |
| 399 |
|
| 400 |
doc_ids, extra = export_files( |
| 401 |
documents_intf, [base_path], ["read"], flags=EXPORT_FILES_FLAG_EXPORT_DIR |
| 402 |
) |
| 403 |
doc_id = doc_ids[0] |
| 404 |
|
| 405 |
host_path = get_host_path_attr(mountpoint / doc_id) |
| 406 |
assert not host_path |
| 407 |
|
| 408 |
host_path = get_host_path_attr(mountpoint / doc_id / "a") |
| 409 |
assert host_path == base_path |
| 410 |
|
| 411 |
host_path = get_host_path_attr(mountpoint / doc_id / "a" / "b") |
| 412 |
assert host_path == base_path / "b" |
| 413 |
|
| 414 |
host_path = get_host_path_attr(mountpoint / doc_id / "a" / "b" / "c") |
| 415 |
assert host_path == base_path / "b" / "c" |
| 416 |
|
| 417 |
|
| 418 |
try: |
| 419 |
xdp.ensure_fuse_supported() |
| 420 |
except xdp.FuseNotSupportedException as e: |
| 421 |
pytest.skip(f"No fuse support: {e}", allow_module_level=True) |