| 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 pytest |
| 9 |
import dbus |
| 10 |
from pathlib import Path |
| 11 |
import os |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def xdp_app_info() -> xdp.AppInfo: |
| 16 |
return xdp.AppInfoHost(app_id="") |
| 17 |
|
| 18 |
|
| 19 |
class TestDocuments: |
| 20 |
def test_version(self, xdg_document_portal, dbus_con): |
| 21 |
documents = dbus_con.get_object( |
| 22 |
"org.freedesktop.portal.Documents", |
| 23 |
"/org/freedesktop/portal/documents", |
| 24 |
) |
| 25 |
|
| 26 |
properties_intf = dbus.Interface( |
| 27 |
documents, |
| 28 |
"org.freedesktop.DBus.Properties", |
| 29 |
) |
| 30 |
portal_version = properties_intf.Get( |
| 31 |
"org.freedesktop.portal.Documents", |
| 32 |
"version", |
| 33 |
) |
| 34 |
assert int(portal_version) == 5 |
| 35 |
|
| 36 |
def test_mount_point(self, xdg_document_portal, dbus_con): |
| 37 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 38 |
xdp_doc.get_mountpoint(documents_intf) |
| 39 |
|
| 40 |
def test_create_doc(self, xdg_document_portal, dbus_con): |
| 41 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 42 |
mountpoint = xdp_doc.get_mountpoint(documents_intf) |
| 43 |
|
| 44 |
content = b"content" |
| 45 |
file_name = "a-file" |
| 46 |
|
| 47 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 48 |
xdp_doc.write_bytes_atomic(file_path, content) |
| 49 |
doc_id = xdp_doc.export_file(documents_intf, file_path) |
| 50 |
|
| 51 |
doc_path = mountpoint / doc_id |
| 52 |
doc_app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id |
| 53 |
doc_app2_path = mountpoint / "by-app" / "com.test.App2" / doc_id |
| 54 |
|
| 55 |
# Make sure it got exported |
| 56 |
assert (doc_path / file_name).read_bytes() == content |
| 57 |
|
| 58 |
assert not (doc_path / "another-file").exists() |
| 59 |
assert not (mountpoint / "anotherid" / file_name).exists() |
| 60 |
|
| 61 |
# Make sure it is not viewable by apps |
| 62 |
assert not doc_app1_path.exists() |
| 63 |
assert not doc_app2_path.exists() |
| 64 |
|
| 65 |
# Create a tmp file in same dir, ensure it works and can't be seen by other apps |
| 66 |
xdp_doc.write_bytes_atomic(doc_path / "tmp1", b"tmpdata1") |
| 67 |
assert (doc_path / "tmp1").read_bytes() == b"tmpdata1" |
| 68 |
assert not (doc_app1_path / "tmp1").exists() |
| 69 |
|
| 70 |
# Ensure App 1 and only it can see the document and tmpfile |
| 71 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["read"]) |
| 72 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 73 |
assert not (doc_app2_path / file_name).exists() |
| 74 |
|
| 75 |
# Make sure App 1 can't create a tmpfile |
| 76 |
assert not (doc_app1_path / "tmp2").exists() |
| 77 |
with pytest.raises(PermissionError): |
| 78 |
(doc_app1_path / "tmp2").write_bytes(b"tmpdata1") |
| 79 |
assert not (doc_app1_path / "tmp2").exists() |
| 80 |
assert not (doc_path / "tmp2").exists() |
| 81 |
|
| 82 |
# Update the document contents, ensure this is propagated |
| 83 |
content = b"content2" |
| 84 |
xdp_doc.write_bytes_atomic(doc_path / file_name, content) |
| 85 |
assert (doc_path / file_name).read_bytes() == content |
| 86 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 87 |
assert file_path.read_bytes() == content |
| 88 |
assert not (doc_app2_path / file_name).exists() |
| 89 |
assert not (doc_app2_path / "tmp1").exists() |
| 90 |
|
| 91 |
# Update the document contents outside fuse fd, ensure this is propagated |
| 92 |
content = b"content3" |
| 93 |
xdp_doc.write_bytes_atomic(file_path, content) |
| 94 |
assert (doc_path / file_name).read_bytes() == content |
| 95 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 96 |
assert file_path.read_bytes() == content |
| 97 |
assert not (doc_app2_path / file_name).exists() |
| 98 |
assert not (doc_app2_path / "tmp1").exists() |
| 99 |
|
| 100 |
# Try to update the doc from an app that can't write to it |
| 101 |
with pytest.raises(PermissionError): |
| 102 |
(doc_app1_path / file_name).write_bytes(b"content4") |
| 103 |
|
| 104 |
# Update the doc from an app with write access |
| 105 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["write"]) |
| 106 |
content = b"content5" |
| 107 |
xdp_doc.write_bytes_atomic(doc_app1_path / file_name, content) |
| 108 |
assert (doc_path / file_name).read_bytes() == content |
| 109 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 110 |
assert file_path.read_bytes() == content |
| 111 |
assert not (doc_app2_path / file_name).exists() |
| 112 |
|
| 113 |
# Try to create a tmp file for an app |
| 114 |
assert not (doc_app1_path / "tmp3").exists() |
| 115 |
xdp_doc.write_bytes_atomic(doc_app1_path / "tmp3", b"tmpdata2") |
| 116 |
(doc_app1_path / "tmp3").read_bytes() == b"tmpdata2" |
| 117 |
assert not (doc_path / "tmp3").exists() |
| 118 |
|
| 119 |
# Re-Create a file from a fuse document file, in various ways |
| 120 |
doc_id2 = xdp_doc.export_file(documents_intf, (doc_path / file_name)) |
| 121 |
assert doc_id2 == doc_id |
| 122 |
doc_id3 = xdp_doc.export_file(documents_intf, (doc_app1_path / file_name)) |
| 123 |
assert doc_id3 == doc_id |
| 124 |
doc_id4 = xdp_doc.export_file(documents_intf, file_path) |
| 125 |
assert doc_id4 == doc_id |
| 126 |
|
| 127 |
# Ensure we can make a unique document |
| 128 |
doc_id5 = xdp_doc.export_file(documents_intf, file_path, unique=True) |
| 129 |
assert doc_id5 != doc_id |
| 130 |
|
| 131 |
def test_recursive_doc(self, xdg_document_portal, dbus_con): |
| 132 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 133 |
mountpoint = xdp_doc.get_mountpoint(documents_intf) |
| 134 |
|
| 135 |
content = b"content" |
| 136 |
file_name = "recursive-file" |
| 137 |
|
| 138 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 139 |
xdp_doc.write_bytes_atomic(file_path, content) |
| 140 |
doc_id = xdp_doc.export_file(documents_intf, file_path) |
| 141 |
|
| 142 |
doc_path = mountpoint / doc_id |
| 143 |
doc_app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id |
| 144 |
|
| 145 |
assert (doc_path / file_name).read_bytes() == content |
| 146 |
|
| 147 |
doc_id2 = xdp_doc.export_file(documents_intf, doc_path / file_name) |
| 148 |
assert doc_id2 == doc_id |
| 149 |
|
| 150 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["read"]) |
| 151 |
|
| 152 |
doc_id3 = xdp_doc.export_file(documents_intf, doc_app1_path / file_name) |
| 153 |
assert doc_id3 == doc_id |
| 154 |
|
| 155 |
def test_create_docs(self, xdg_document_portal, dbus_con): |
| 156 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 157 |
mountpoint = xdp_doc.get_mountpoint(documents_intf) |
| 158 |
|
| 159 |
files = { |
| 160 |
"doc1": b"doc1-content", |
| 161 |
"doc2": b"doc2-content", |
| 162 |
} |
| 163 |
|
| 164 |
file_paths = [] |
| 165 |
for file_name, file_content in files.items(): |
| 166 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 167 |
xdp_doc.write_bytes_atomic(file_path, file_content) |
| 168 |
file_paths.append(file_path) |
| 169 |
|
| 170 |
doc_ids, extra = xdp_doc.export_files( |
| 171 |
documents_intf, file_paths, ["read"], app_id="org.other.App" |
| 172 |
) |
| 173 |
|
| 174 |
assert extra |
| 175 |
out_mountpoint = xdp_doc.path_from_null_term_bytes(extra["mountpoint"]) |
| 176 |
assert out_mountpoint == mountpoint |
| 177 |
|
| 178 |
assert doc_ids |
| 179 |
for doc_id, (file_name, file_content) in zip(doc_ids, files.items()): |
| 180 |
assert (mountpoint / doc_id / file_name).read_bytes() == file_content |
| 181 |
assert (Path(os.environ["TMPDIR"]) / file_name).read_bytes() == file_content |
| 182 |
app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id / file_name |
| 183 |
app2_path = mountpoint / "by-app" / "com.test.App2" / doc_id / file_name |
| 184 |
assert not app1_path.exists() |
| 185 |
assert not app2_path.exists() |
| 186 |
assert not (mountpoint / doc_id / "another-file").exists() |
| 187 |
assert not (mountpoint / "anotherid" / file_name).exists() |
| 188 |
|
| 189 |
other_app_path = ( |
| 190 |
mountpoint / "by-app" / "org.other.App" / doc_id / file_name |
| 191 |
) |
| 192 |
assert other_app_path.read_bytes() == file_content |
| 193 |
with pytest.raises(PermissionError): |
| 194 |
other_app_path.write_bytes(b"new-content") |
| 195 |
|
| 196 |
def test_add_named(self, xdg_document_portal, dbus_con): |
| 197 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 198 |
mountpoint = xdp_doc.get_mountpoint(documents_intf) |
| 199 |
|
| 200 |
content = b"content" |
| 201 |
file_name = "add-named-1" |
| 202 |
|
| 203 |
folder_path = Path(os.environ["TMPDIR"]) |
| 204 |
doc_id = xdp_doc.export_file_named(documents_intf, folder_path, file_name) |
| 205 |
assert doc_id |
| 206 |
|
| 207 |
doc_path = mountpoint / doc_id |
| 208 |
doc_app1_path = mountpoint / "by-app" / "com.test.App1" / doc_id |
| 209 |
doc_app2_path = mountpoint / "by-app" / "com.test.App2" / doc_id |
| 210 |
|
| 211 |
assert doc_path.exists() |
| 212 |
assert not doc_app1_path.exists() |
| 213 |
assert not (doc_path / file_name).exists() |
| 214 |
assert not (doc_app1_path / file_name).exists() |
| 215 |
|
| 216 |
documents_intf.GrantPermissions(doc_id, "com.test.App1", ["read", "write"]) |
| 217 |
|
| 218 |
assert doc_path.exists() |
| 219 |
assert doc_app1_path.exists() |
| 220 |
assert not (doc_path / file_name).exists() |
| 221 |
assert not (doc_app1_path / file_name).exists() |
| 222 |
|
| 223 |
# Update truncating with no previous file |
| 224 |
xdp_doc.write_bytes_trunc(doc_path / file_name, content) |
| 225 |
assert (doc_path / file_name).read_bytes() == content |
| 226 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 227 |
assert not (doc_app2_path / file_name).exists() |
| 228 |
|
| 229 |
# Update truncating with previous file |
| 230 |
content = b"content2" |
| 231 |
xdp_doc.write_bytes_trunc(doc_path / file_name, content) |
| 232 |
assert (doc_path / file_name).read_bytes() == content |
| 233 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 234 |
assert not (doc_app2_path / file_name).exists() |
| 235 |
|
| 236 |
# Update atomic with previous file |
| 237 |
content = b"content3" |
| 238 |
xdp_doc.write_bytes_atomic(doc_path / file_name, content) |
| 239 |
assert (doc_path / file_name).read_bytes() == content |
| 240 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 241 |
assert not (doc_app2_path / file_name).exists() |
| 242 |
|
| 243 |
# Update from host |
| 244 |
content = b"content4" |
| 245 |
xdp_doc.write_bytes_atomic(folder_path / file_name, content) |
| 246 |
assert (doc_path / file_name).read_bytes() == content |
| 247 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 248 |
assert not (doc_app2_path / file_name).exists() |
| 249 |
|
| 250 |
# Unlink doc |
| 251 |
(doc_path / file_name).unlink() |
| 252 |
assert doc_path.exists() |
| 253 |
assert doc_app1_path.exists() |
| 254 |
assert not (doc_path / file_name).exists() |
| 255 |
assert not (doc_app1_path / file_name).exists() |
| 256 |
|
| 257 |
# Update atomic with no previous file |
| 258 |
content = b"content5" |
| 259 |
xdp_doc.write_bytes_atomic(doc_path / file_name, content) |
| 260 |
assert (doc_path / file_name).read_bytes() == content |
| 261 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 262 |
assert not (doc_app2_path / file_name).exists() |
| 263 |
|
| 264 |
# Unlink doc on host |
| 265 |
(folder_path / file_name).unlink() |
| 266 |
assert doc_path.exists() |
| 267 |
assert doc_app1_path.exists() |
| 268 |
assert not (doc_path / file_name).exists() |
| 269 |
assert not (doc_app1_path / file_name).exists() |
| 270 |
|
| 271 |
# Update atomic with unexpected no previous file |
| 272 |
content = b"content6" |
| 273 |
xdp_doc.write_bytes_atomic(doc_path / file_name, content) |
| 274 |
assert (doc_path / file_name).read_bytes() == content |
| 275 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 276 |
assert not (doc_app2_path / file_name).exists() |
| 277 |
|
| 278 |
# Unlink doc on host again |
| 279 |
(folder_path / file_name).unlink() |
| 280 |
assert doc_path.exists() |
| 281 |
assert doc_app1_path.exists() |
| 282 |
assert not (doc_path / file_name).exists() |
| 283 |
assert not (doc_app1_path / file_name).exists() |
| 284 |
|
| 285 |
# Update truncating with unexpected no previous file |
| 286 |
content = b"content7" |
| 287 |
xdp_doc.write_bytes_trunc(doc_path / file_name, content) |
| 288 |
assert (doc_path / file_name).read_bytes() == content |
| 289 |
assert (doc_app1_path / file_name).read_bytes() == content |
| 290 |
assert not (doc_app2_path / file_name).exists() |
| 291 |
|
| 292 |
def test_get_host_paths(self, xdg_document_portal, dbus_con): |
| 293 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 294 |
|
| 295 |
content = b"content" |
| 296 |
file_name = "host-path" |
| 297 |
|
| 298 |
file_path = Path(os.environ["TMPDIR"]) / file_name |
| 299 |
xdp_doc.write_bytes_atomic(file_path, content) |
| 300 |
doc_id = xdp_doc.export_file(documents_intf, file_path) |
| 301 |
|
| 302 |
host_paths = documents_intf.GetHostPaths([doc_id], byte_arrays=True) |
| 303 |
assert doc_id in host_paths |
| 304 |
doc_host_path = xdp_doc.path_from_null_term_bytes(host_paths[doc_id]) |
| 305 |
assert doc_host_path == file_path |
| 306 |
|
| 307 |
def test_host_paths_xattr(self, xdg_document_portal, dbus_con): |
| 308 |
documents_intf = xdp.get_document_portal_iface(dbus_con) |
| 309 |
mountpoint = xdp_doc.get_mountpoint(documents_intf) |
| 310 |
|
| 311 |
base_path = Path(os.environ["TMPDIR"]) / "a" |
| 312 |
file_path = base_path / "b" / "c" |
| 313 |
file_path.parent.mkdir(parents=True, exist_ok=True) |
| 314 |
file_path.write_bytes(b"test") |
| 315 |
|
| 316 |
doc_ids, extra = xdp_doc.export_files( |
| 317 |
documents_intf, |
| 318 |
[base_path], |
| 319 |
["read"], |
| 320 |
flags=xdp_doc.EXPORT_FILES_FLAG_EXPORT_DIR, |
| 321 |
) |
| 322 |
doc_id = doc_ids[0] |
| 323 |
|
| 324 |
host_path = xdp_doc.get_host_path_attr(mountpoint / doc_id) |
| 325 |
assert not host_path |
| 326 |
|
| 327 |
host_path = xdp_doc.get_host_path_attr(mountpoint / doc_id / "a") |
| 328 |
assert host_path == base_path |
| 329 |
|
| 330 |
host_path = xdp_doc.get_host_path_attr(mountpoint / doc_id / "a" / "b") |
| 331 |
assert host_path == base_path / "b" |
| 332 |
|
| 333 |
host_path = xdp_doc.get_host_path_attr(mountpoint / doc_id / "a" / "b" / "c") |
| 334 |
assert host_path == base_path / "b" / "c" |
| 335 |
|
| 336 |
|
| 337 |
try: |
| 338 |
xdp.ensure_fuse_supported() |
| 339 |
except xdp.FuseNotSupportedException as e: |
| 340 |
pytest.skip(f"No fuse support: {e}", allow_module_level=True) |