| 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 errno |
| 9 |
import os |
| 10 |
import random |
| 11 |
import stat |
| 12 |
import sys |
| 13 |
import multiprocessing as mp |
| 14 |
import traceback |
| 15 |
from collections import defaultdict |
| 16 |
from gi.repository import Gio, GLib |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture |
| 20 |
def app_id(): |
| 21 |
return None |
| 22 |
|
| 23 |
|
| 24 |
def filename_to_ay(filename): |
| 25 |
return list(filename.encode("utf-8")) + [0] |
| 26 |
|
| 27 |
|
| 28 |
app_prefix = "org.test." |
| 29 |
dir_prefix = "dir" |
| 30 |
ensure_no_remaining = True |
| 31 |
|
| 32 |
running_count: defaultdict[str, int] = defaultdict(int) |
| 33 |
|
| 34 |
|
| 35 |
def log(str): |
| 36 |
print(str, file=sys.stderr) |
| 37 |
|
| 38 |
|
| 39 |
def logv(str): |
| 40 |
log(str) |
| 41 |
|
| 42 |
|
| 43 |
def get_a_count(counter: str): |
| 44 |
global running_count |
| 45 |
running_count[counter] += 1 |
| 46 |
return running_count[counter] |
| 47 |
|
| 48 |
|
| 49 |
def setFileContent(path, content): |
| 50 |
with open(path, "w") as f: |
| 51 |
f.write(content) |
| 52 |
|
| 53 |
|
| 54 |
def appendFileContent(path, content): |
| 55 |
with open(path, "a") as f: |
| 56 |
f.write(content) |
| 57 |
|
| 58 |
|
| 59 |
def readFdContent(fd): |
| 60 |
os.lseek(fd, 0, os.SEEK_SET) |
| 61 |
return str(os.read(fd, 64 * 1024), "utf-8") |
| 62 |
|
| 63 |
|
| 64 |
def replaceFdContent(fd, content): |
| 65 |
os.lseek(fd, 0, os.SEEK_SET) |
| 66 |
os.ftruncate(fd, 0) |
| 67 |
os.write(fd, bytes(content, "utf-8")) |
| 68 |
|
| 69 |
|
| 70 |
def appendFdContent(fd, content): |
| 71 |
os.lseek(fd, 0, os.SEEK_END) |
| 72 |
os.write(fd, bytes(content, "utf-8")) |
| 73 |
|
| 74 |
|
| 75 |
DOCUMENT_ADD_FLAGS_REUSE_EXISTING = 1 << 0 |
| 76 |
DOCUMENT_ADD_FLAGS_PERSISTENT = 1 << 1 |
| 77 |
DOCUMENT_ADD_FLAGS_AS_NEEDED_BY_APP = 1 << 2 |
| 78 |
DOCUMENT_ADD_FLAGS_DIRECTORY = 1 << 3 |
| 79 |
|
| 80 |
|
| 81 |
def assertRaises(exc_type, func, *args, **kwargs): |
| 82 |
with pytest.raises(exc_type): |
| 83 |
func(*args, **kwargs) |
| 84 |
|
| 85 |
|
| 86 |
def assertRaisesErrno(error_nr, func, *args, **kwargs): |
| 87 |
with pytest.raises(OSError) as excinfo: |
| 88 |
func(*args, **kwargs) |
| 89 |
|
| 90 |
if excinfo.value.errno != error_nr: |
| 91 |
raise AssertionError( |
| 92 |
"Wrong errno {0} was raised instead of {1}".format( |
| 93 |
excinfo.value.errno, error_nr |
| 94 |
) |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
def assertRaisesGError(message, code, func, *args, **kwargs): |
| 99 |
with pytest.raises(GLib.GError) as excinfo: |
| 100 |
func(*args, **kwargs) |
| 101 |
|
| 102 |
if not excinfo.value.message.startswith(message): |
| 103 |
raise AssertionError( |
| 104 |
"Wrong message {0} doesn't start with {1}".format( |
| 105 |
excinfo.value.message, message |
| 106 |
) |
| 107 |
) |
| 108 |
if excinfo.value.code != code: |
| 109 |
raise AssertionError( |
| 110 |
"Wrong code {0} was raised instead of {1}".format(excinfo.value.code, code) |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
def assertFileHasContent(path, expected_content): |
| 115 |
with open(path) as f: |
| 116 |
file_content = f.read() |
| 117 |
assert file_content == expected_content |
| 118 |
|
| 119 |
|
| 120 |
def assertFdHasContent(fd, expected_content): |
| 121 |
content = readFdContent(fd) |
| 122 |
assert content == expected_content |
| 123 |
|
| 124 |
|
| 125 |
def assertSameStat(a, b, b_mode_mask): |
| 126 |
if not ( |
| 127 |
a.st_mode == (b.st_mode & b_mode_mask) |
| 128 |
and a.st_nlink == b.st_nlink |
| 129 |
and a.st_size == b.st_size |
| 130 |
and a.st_uid == b.st_uid |
| 131 |
and a.st_gid == b.st_gid |
| 132 |
and a.st_atime == b.st_atime |
| 133 |
and a.st_mtime == b.st_mtime |
| 134 |
and a.st_ctime == b.st_ctime |
| 135 |
): |
| 136 |
raise AssertionError("Stat value {} was not the expected {})".format(a, b)) |
| 137 |
|
| 138 |
|
| 139 |
def assertFileExist(path): |
| 140 |
try: |
| 141 |
info = os.lstat(path) |
| 142 |
if info.st_mode & stat.S_IFREG != stat.S_IFREG: |
| 143 |
raise AssertionError("File {} is not a regular file".format(path)) |
| 144 |
except Exception: |
| 145 |
raise AssertionError("File {} doesn't exist".format(path)) |
| 146 |
|
| 147 |
|
| 148 |
def assertDirExist(path): |
| 149 |
try: |
| 150 |
info = os.lstat(path) |
| 151 |
if info.st_mode & stat.S_IFDIR != stat.S_IFDIR: |
| 152 |
raise AssertionError("File {} is not a directory file".format(path)) |
| 153 |
except Exception: |
| 154 |
raise AssertionError("File {} doesn't exist".format(path)) |
| 155 |
|
| 156 |
|
| 157 |
def assertSymlink(path, expected_target): |
| 158 |
try: |
| 159 |
info = os.lstat(path) |
| 160 |
if info.st_mode & stat.S_IFLNK != stat.S_IFLNK: |
| 161 |
raise AssertionError("File {} is not a symlink".format(path)) |
| 162 |
target = os.readlink(path) |
| 163 |
if target != expected_target: |
| 164 |
raise AssertionError( |
| 165 |
"File {} has wrong target {}, expected {}".format( |
| 166 |
path, target, expected_target |
| 167 |
) |
| 168 |
) |
| 169 |
except Exception: |
| 170 |
raise AssertionError("Symlink {} doesn't exist".format(path)) |
| 171 |
|
| 172 |
|
| 173 |
def assertFileNotExist(path): |
| 174 |
try: |
| 175 |
os.lstat(path) |
| 176 |
except FileNotFoundError: |
| 177 |
return |
| 178 |
except Exception: |
| 179 |
raise AssertionError( |
| 180 |
"Got wrong execption {} for {}, expected FileNotFoundError".format( |
| 181 |
sys.exc_info()[0], path |
| 182 |
) |
| 183 |
) |
| 184 |
raise AssertionError("Path {} unexpectedly exists".format(path)) |
| 185 |
|
| 186 |
|
| 187 |
def assertDirFiles(path, expected_files, exhaustive=True, volatile_files=None): |
| 188 |
found_files = os.listdir(path) |
| 189 |
remaining = set(found_files) |
| 190 |
for file in expected_files: |
| 191 |
if file in remaining: |
| 192 |
remaining.remove(file) |
| 193 |
elif file not in volatile_files: |
| 194 |
raise AssertionError( |
| 195 |
"Expected file {} not found in dir {} (all: {})".format( |
| 196 |
file, path, found_files |
| 197 |
) |
| 198 |
) |
| 199 |
if exhaustive: |
| 200 |
if len(remaining) != 0: |
| 201 |
raise AssertionError( |
| 202 |
"Unexpected files {} in dir {} (all: {})".format( |
| 203 |
remaining, path, found_files |
| 204 |
) |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
class Doc: |
| 209 |
def __init__(self, portal, id, path, content, is_dir=False): |
| 210 |
self.portal = portal |
| 211 |
self.id = id |
| 212 |
self.content = content |
| 213 |
self.real_path = path |
| 214 |
self.is_dir = is_dir |
| 215 |
self.apps = [] |
| 216 |
self.files = [] |
| 217 |
|
| 218 |
if is_dir: |
| 219 |
self.real_dirname = path |
| 220 |
self.filename = None |
| 221 |
self.dirname = os.path.basename(path) |
| 222 |
else: |
| 223 |
(self.real_dirname, self.filename) = os.path.split(path) |
| 224 |
self.dirname = None |
| 225 |
if content: |
| 226 |
self.files.append(self.filename) |
| 227 |
|
| 228 |
def is_readable_by(self, app_id): |
| 229 |
if app_id: |
| 230 |
return app_id in self.apps |
| 231 |
return True |
| 232 |
|
| 233 |
def is_writable_by(self, app_id): |
| 234 |
if app_id: |
| 235 |
return app_id in self.apps and ".write." in app_id |
| 236 |
else: |
| 237 |
return True |
| 238 |
|
| 239 |
def get_doc_path(self, app_id): |
| 240 |
if app_id: |
| 241 |
base = self.portal.app_path(app_id) + "/" + self.id |
| 242 |
else: |
| 243 |
base = self.portal.mountpoint + "/" + self.id |
| 244 |
if self.is_dir: |
| 245 |
return base + "/" + self.dirname |
| 246 |
else: |
| 247 |
return base |
| 248 |
|
| 249 |
def __str__(self): |
| 250 |
name = self.id |
| 251 |
if self.is_dir: |
| 252 |
return "%s(dir)" % (name) |
| 253 |
elif self.content is None: |
| 254 |
return "%s(missing)" % (name) |
| 255 |
else: |
| 256 |
return "%s" % (name) |
| 257 |
|
| 258 |
|
| 259 |
class DocPortal: |
| 260 |
def __init__(self): |
| 261 |
self.apps = [] |
| 262 |
self.volatile_apps = set() |
| 263 |
self.docs = {} |
| 264 |
self.bus = Gio.bus_get_sync(Gio.BusType.SESSION, None) |
| 265 |
self.proxy = Gio.DBusProxy.new_sync( |
| 266 |
self.bus, |
| 267 |
Gio.DBusProxyFlags.NONE, |
| 268 |
None, |
| 269 |
"org.freedesktop.portal.Documents", |
| 270 |
"/org/freedesktop/portal/documents", |
| 271 |
"org.freedesktop.portal.Documents", |
| 272 |
None, |
| 273 |
) |
| 274 |
self.mountpoint = self.get_mount_path() |
| 275 |
|
| 276 |
def get_mount_path(self): |
| 277 |
res = self.proxy.call_sync("GetMountPoint", GLib.Variant("()", ()), 0, -1, None) |
| 278 |
return bytearray(res[0][:-1]).decode("utf-8") |
| 279 |
|
| 280 |
def grant_permissions(self, doc_id, app_id, permissions): |
| 281 |
self.proxy.call_sync( |
| 282 |
"GrantPermissions", |
| 283 |
GLib.Variant("(ssas)", (doc_id, app_id, permissions)), |
| 284 |
0, |
| 285 |
-1, |
| 286 |
None, |
| 287 |
) |
| 288 |
|
| 289 |
def lookup(self, path): |
| 290 |
res = self.proxy.call_sync( |
| 291 |
"Lookup", GLib.Variant("(ay)", (filename_to_ay(path),)), 0, -1, None |
| 292 |
) |
| 293 |
return res[0] |
| 294 |
|
| 295 |
def delete(self, doc_id): |
| 296 |
self.proxy.call_sync("Delete", GLib.Variant("(s)", (doc_id,)), 0, -1, None) |
| 297 |
del self.docs[doc_id] |
| 298 |
|
| 299 |
def add(self, path, reuse_existing=True): |
| 300 |
fdlist = Gio.UnixFDList.new() |
| 301 |
fd = os.open(path, os.O_PATH) |
| 302 |
handle = fdlist.append(fd) |
| 303 |
os.close(fd) |
| 304 |
res = self.proxy.call_with_unix_fd_list_sync( |
| 305 |
"Add", |
| 306 |
GLib.Variant("(hbb)", (handle, reuse_existing, False)), |
| 307 |
0, |
| 308 |
-1, |
| 309 |
fdlist, |
| 310 |
None, |
| 311 |
) |
| 312 |
doc_id = res[0][0] |
| 313 |
if doc_id in self.docs: |
| 314 |
return self.docs[doc_id] |
| 315 |
|
| 316 |
with open(path) as f: |
| 317 |
content = f.read() |
| 318 |
doc = Doc(self, doc_id, path, content) |
| 319 |
self.docs[doc.id] = doc |
| 320 |
return doc |
| 321 |
|
| 322 |
def add_named(self, path, reuse_existing=True): |
| 323 |
(dirname, filename) = os.path.split(path) |
| 324 |
fdlist = Gio.UnixFDList.new() |
| 325 |
fd = os.open(dirname, os.O_PATH) |
| 326 |
handle = fdlist.append(fd) |
| 327 |
os.close(fd) |
| 328 |
res = self.proxy.call_with_unix_fd_list_sync( |
| 329 |
"AddNamed", |
| 330 |
GLib.Variant( |
| 331 |
"(haybb)", (handle, filename_to_ay(filename), reuse_existing, False) |
| 332 |
), |
| 333 |
0, |
| 334 |
-1, |
| 335 |
fdlist, |
| 336 |
None, |
| 337 |
) |
| 338 |
doc_id = res[0][0] |
| 339 |
if doc_id in self.docs: |
| 340 |
return self.docs[doc_id] |
| 341 |
|
| 342 |
try: |
| 343 |
with open(path) as f: |
| 344 |
content = f.read() |
| 345 |
except Exception: |
| 346 |
content = None |
| 347 |
doc = Doc(self, doc_id, path, content) |
| 348 |
self.docs[doc.id] = doc |
| 349 |
return doc |
| 350 |
|
| 351 |
def add_full(self, path, flags): |
| 352 |
fdlist = Gio.UnixFDList.new() |
| 353 |
fd = os.open(path, os.O_PATH) |
| 354 |
handle = fdlist.append(fd) |
| 355 |
os.close(fd) |
| 356 |
res = self.proxy.call_with_unix_fd_list_sync( |
| 357 |
"AddFull", |
| 358 |
GLib.Variant("(ahusas)", ([handle], flags, "", [])), |
| 359 |
0, |
| 360 |
-1, |
| 361 |
fdlist, |
| 362 |
None, |
| 363 |
) |
| 364 |
doc_id = res[0][0][0] |
| 365 |
if doc_id in self.docs: |
| 366 |
return self.docs[doc_id] |
| 367 |
doc = Doc(self, doc_id, path, True, (flags & DOCUMENT_ADD_FLAGS_DIRECTORY) != 0) |
| 368 |
self.docs[doc.id] = doc |
| 369 |
return doc |
| 370 |
|
| 371 |
def add_dir(self, path): |
| 372 |
return self.add_full( |
| 373 |
path, DOCUMENT_ADD_FLAGS_REUSE_EXISTING | DOCUMENT_ADD_FLAGS_DIRECTORY |
| 374 |
) |
| 375 |
|
| 376 |
def get_docs_for_app(self, app_id): |
| 377 |
docs = [] |
| 378 |
for doc in self.docs.values(): |
| 379 |
if doc.is_readable_by(app_id): |
| 380 |
docs.append(doc.id) |
| 381 |
return docs |
| 382 |
|
| 383 |
def ensure_app_id(self, app_id, volatile=False): |
| 384 |
if app_id not in self.apps: |
| 385 |
self.apps.append(app_id) |
| 386 |
if volatile: |
| 387 |
self.volatile_apps.add(app_id) |
| 388 |
|
| 389 |
def get_docs(self): |
| 390 |
return list(self.docs.values()) |
| 391 |
|
| 392 |
def get_docs_randomized(self): |
| 393 |
docs = list(self.docs.values()) |
| 394 |
random.shuffle(docs) |
| 395 |
return docs |
| 396 |
|
| 397 |
def get_doc(self, doc_id): |
| 398 |
return self.docs[doc_id] |
| 399 |
|
| 400 |
def get_app_ids(self): |
| 401 |
return self.apps |
| 402 |
|
| 403 |
def get_volatile_app_ids(self): |
| 404 |
return self.volatile_apps |
| 405 |
|
| 406 |
def get_app_ids_randomized(self): |
| 407 |
apps = self.apps.copy() |
| 408 |
random.shuffle(apps) |
| 409 |
return apps |
| 410 |
|
| 411 |
def by_app_path(self): |
| 412 |
return self.mountpoint + "/by-app" |
| 413 |
|
| 414 |
def app_path(self, app_id): |
| 415 |
return self.mountpoint + "/by-app/" + app_id |
| 416 |
|
| 417 |
|
| 418 |
class FileTransferPortal(DocPortal): |
| 419 |
def __init__(self): |
| 420 |
super().__init__() |
| 421 |
self.ft_proxy = Gio.DBusProxy.new_sync( |
| 422 |
self.bus, |
| 423 |
Gio.DBusProxyFlags.NONE, |
| 424 |
None, |
| 425 |
"org.freedesktop.portal.Documents", |
| 426 |
"/org/freedesktop/portal/documents", |
| 427 |
"org.freedesktop.portal.FileTransfer", |
| 428 |
None, |
| 429 |
) |
| 430 |
|
| 431 |
def start_transfer(self): |
| 432 |
res = self.ft_proxy.call_sync( |
| 433 |
"StartTransfer", GLib.Variant("(a{sv})", ([None])), 0, -1, None |
| 434 |
) |
| 435 |
return res[0] |
| 436 |
|
| 437 |
def add_files(self, key, files): |
| 438 |
fdlist = Gio.UnixFDList.new() |
| 439 |
handles = [] |
| 440 |
for filename in files: |
| 441 |
fd = os.open(filename, os.O_PATH) |
| 442 |
handle = fdlist.append(fd) |
| 443 |
handles.append(handle) |
| 444 |
os.close(fd) |
| 445 |
|
| 446 |
res = self.ft_proxy.call_with_unix_fd_list_sync( |
| 447 |
"AddFiles", |
| 448 |
GLib.Variant("(saha{sv})", (key, handles, [])), |
| 449 |
0, |
| 450 |
-1, |
| 451 |
fdlist, |
| 452 |
None, |
| 453 |
) |
| 454 |
return res |
| 455 |
|
| 456 |
def retrieve_files(self, key): |
| 457 |
res = self.ft_proxy.call_sync( |
| 458 |
"RetrieveFiles", |
| 459 |
GLib.Variant("(sa{sv})", (key, [])), |
| 460 |
0, |
| 461 |
-1, |
| 462 |
None, |
| 463 |
) |
| 464 |
return res |
| 465 |
|
| 466 |
def stop_transfer(self, key): |
| 467 |
res = self.ft_proxy.call_sync( |
| 468 |
"StopTransfer", |
| 469 |
GLib.Variant("(s)", (key,)), |
| 470 |
0, |
| 471 |
-1, |
| 472 |
None, |
| 473 |
) |
| 474 |
return res |
| 475 |
|
| 476 |
|
| 477 |
def check_virtual_stat(info, writable=False): |
| 478 |
assert info.st_uid == os.getuid() |
| 479 |
assert info.st_gid == os.getgid() |
| 480 |
if writable: |
| 481 |
assert info.st_mode == stat.S_IFDIR | 0o700 |
| 482 |
else: |
| 483 |
assert info.st_mode == stat.S_IFDIR | 0o500 |
| 484 |
|
| 485 |
|
| 486 |
def verify_virtual_dir(path, files, volatile_files=None): |
| 487 |
info = os.lstat(path) |
| 488 |
check_virtual_stat(info) |
| 489 |
assert os.access(path, os.R_OK) |
| 490 |
assert not os.access(path, os.W_OK) |
| 491 |
|
| 492 |
assertRaises(FileNotFoundError, os.lstat, path + "/not-existing-file") |
| 493 |
|
| 494 |
if files is not None: |
| 495 |
assertDirFiles(path, files, ensure_no_remaining, volatile_files) |
| 496 |
|
| 497 |
|
| 498 |
def verify_doc(doc, app_id=None): |
| 499 |
dir = doc.get_doc_path(app_id) |
| 500 |
|
| 501 |
if doc.is_dir: |
| 502 |
vdir = os.path.dirname(dir) |
| 503 |
info = os.lstat(vdir) |
| 504 |
check_virtual_stat(info) |
| 505 |
pass |
| 506 |
else: |
| 507 |
info = os.lstat(dir) |
| 508 |
check_virtual_stat(info, doc.is_writable_by(app_id)) |
| 509 |
assert os.access(dir, os.R_OK) |
| 510 |
if doc.is_writable_by(app_id): |
| 511 |
assert os.access(dir, os.W_OK) |
| 512 |
else: |
| 513 |
assert not os.access(dir, os.W_OK) |
| 514 |
|
| 515 |
assertRaises(FileNotFoundError, os.lstat, dir + "/not-existing-file") |
| 516 |
|
| 517 |
assertDirFiles(dir, doc.files) |
| 518 |
|
| 519 |
for file in doc.files: |
| 520 |
filepath = dir + "/" + file |
| 521 |
info = os.lstat(filepath) |
| 522 |
assert info.st_uid == os.getuid() |
| 523 |
assert info.st_gid == os.getgid() |
| 524 |
|
| 525 |
assert os.access(filepath, os.R_OK) |
| 526 |
if doc.is_writable_by(app_id): |
| 527 |
assert os.access(filepath, os.W_OK) |
| 528 |
else: |
| 529 |
assert not os.access(filepath, os.W_OK) |
| 530 |
|
| 531 |
if doc.filename: |
| 532 |
main_path = dir + "/" + doc.filename |
| 533 |
real_path = doc.real_path |
| 534 |
if doc.content: |
| 535 |
assertFileExist(main_path) |
| 536 |
assertFileHasContent(main_path, doc.content) |
| 537 |
assertFileHasContent(real_path, doc.content) |
| 538 |
|
| 539 |
info = os.lstat(main_path) |
| 540 |
real_info = os.lstat(real_path) |
| 541 |
mode_mask = ~(stat.S_ISUID | stat.S_ISGID | stat.S_ISVTX) |
| 542 |
if not doc.is_writable_by(app_id): |
| 543 |
mode_mask = mode_mask & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) |
| 544 |
assertSameStat(info, real_info, mode_mask) |
| 545 |
|
| 546 |
else: |
| 547 |
assertRaises(FileNotFoundError, os.lstat, main_path) |
| 548 |
assertRaises(FileNotFoundError, os.open, main_path, os.O_RDONLY) |
| 549 |
assertRaises(FileNotFoundError, os.lstat, doc.real_path) |
| 550 |
assertRaises(FileNotFoundError, os.open, doc.real_path, os.O_RDONLY) |
| 551 |
|
| 552 |
# Ensure no leftover temp files |
| 553 |
for real_file in os.listdir(os.path.dirname(doc.real_path)): |
| 554 |
assert not real_file.startswith(".xdp") |
| 555 |
|
| 556 |
|
| 557 |
def verify_fs_layout(portal): |
| 558 |
verify_virtual_dir(portal.mountpoint, ["by-app"] + list(portal.docs.keys())) |
| 559 |
verify_virtual_dir( |
| 560 |
portal.by_app_path(), portal.get_app_ids(), portal.get_volatile_app_ids() |
| 561 |
) |
| 562 |
|
| 563 |
for doc in portal.get_docs(): |
| 564 |
verify_doc(doc) |
| 565 |
|
| 566 |
# Verify the by-app subdirs (just the directory for now) |
| 567 |
for app_id in portal.get_app_ids(): |
| 568 |
docs_for_app = portal.get_docs_for_app(app_id) |
| 569 |
verify_virtual_dir(portal.app_path(app_id), docs_for_app) |
| 570 |
for doc_id in docs_for_app: |
| 571 |
doc = portal.get_doc(doc_id) |
| 572 |
verify_doc(doc, app_id) |
| 573 |
|
| 574 |
|
| 575 |
def check_virtdir_perms(path): |
| 576 |
assertRaises(PermissionError, os.mkdir, path + "/a_dir") |
| 577 |
assertRaises(PermissionError, os.open, path + "/a-file", os.O_RDWR | os.O_CREAT) |
| 578 |
|
| 579 |
|
| 580 |
def check_root_perms(path): |
| 581 |
check_virtdir_perms(path) |
| 582 |
assertRaises(PermissionError, os.rename, path + "/by-app", path + "/by-app2") |
| 583 |
assertRaises(PermissionError, os.rmdir, path + "/by-app") |
| 584 |
|
| 585 |
|
| 586 |
def check_byapp_perms(path): |
| 587 |
check_virtdir_perms(path) |
| 588 |
assertRaises(PermissionError, os.mkdir, path + "/a_dir") |
| 589 |
|
| 590 |
|
| 591 |
def check_regular_doc_perms(doc, app_id): |
| 592 |
path = doc.get_doc_path(app_id) |
| 593 |
writable = doc.is_writable_by(app_id) |
| 594 |
# regular documents, can't do most stuff |
| 595 |
assertRaises(PermissionError, os.mkdir, path + "/dir") |
| 596 |
assertRaises(PermissionError, os.symlink, "symlink-value", path + "/symlink") |
| 597 |
|
| 598 |
docpath = path + "/" + doc.filename |
| 599 |
tmppath = path + "/a-tmpfile" |
| 600 |
tmppath2 = path + "/another-tmpfile" |
| 601 |
if doc.content: # Main file exists |
| 602 |
assertFileExist(docpath) |
| 603 |
assertFileExist(doc.real_path) |
| 604 |
assertRaises(PermissionError, os.link, docpath, path + "/a-hardlink") |
| 605 |
assertRaises(NotADirectoryError, os.rmdir, docpath) |
| 606 |
assertRaises(PermissionError, os.setxattr, docpath, "user.attr", b"foo") |
| 607 |
assertRaises(PermissionError, os.removexattr, docpath, "user.attr") |
| 608 |
|
| 609 |
fd = os.open(docpath, os.O_RDONLY, 0o600) |
| 610 |
os.close(fd) |
| 611 |
|
| 612 |
if not writable: |
| 613 |
assertRaises( |
| 614 |
PermissionError, os.open, docpath, os.O_RDONLY | os.O_TRUNC, 0o600 |
| 615 |
) |
| 616 |
assertRaises(PermissionError, os.open, docpath, os.O_WRONLY, 0o600) |
| 617 |
assertRaises(PermissionError, os.open, docpath, os.O_RDWR, 0o600) |
| 618 |
assertRaises(PermissionError, os.rename, docpath, docpath + "renamed") |
| 619 |
assertRaises(PermissionError, os.truncate, docpath, 1) |
| 620 |
assertRaises(PermissionError, os.unlink, docpath) |
| 621 |
assertRaises(PermissionError, os.utime, docpath) |
| 622 |
else: |
| 623 |
# Can't move file out of docdir or into other version of same docdir |
| 624 |
assertRaisesErrno( |
| 625 |
errno.EXDEV, os.rename, docpath, path + "/../" + doc.filename |
| 626 |
) |
| 627 |
if app_id: |
| 628 |
assertRaisesErrno( |
| 629 |
errno.EXDEV, |
| 630 |
os.rename, |
| 631 |
docpath, |
| 632 |
doc.get_doc_path(None) + doc.filename, |
| 633 |
) |
| 634 |
if doc.apps and app_id != doc.apps[0]: |
| 635 |
assertRaisesErrno( |
| 636 |
errno.EXDEV, |
| 637 |
os.rename, |
| 638 |
docpath, |
| 639 |
doc.get_doc_path(doc.apps[0]) + doc.filename, |
| 640 |
) |
| 641 |
|
| 642 |
# Ensure we can read it (multiple times) |
| 643 |
fd = os.open(docpath, os.O_RDONLY, 0o600) |
| 644 |
assertFdHasContent(fd, doc.content) |
| 645 |
assertFdHasContent(fd, doc.content) |
| 646 |
|
| 647 |
# Ensure we can rename it |
| 648 |
os.rename(docpath, docpath + "_renamed") |
| 649 |
assertRaises(FileNotFoundError, os.open, docpath, os.O_RDONLY, 0o600) |
| 650 |
# ... and still read it |
| 651 |
assertFdHasContent(fd, doc.content) |
| 652 |
|
| 653 |
# Ensure we can delete it |
| 654 |
os.unlink(docpath + "_renamed") |
| 655 |
# ... and still read it |
| 656 |
assertFdHasContent(fd, doc.content) |
| 657 |
os.close(fd) |
| 658 |
|
| 659 |
# Replace main file with rename of tmpfile |
| 660 |
setFileContent(docpath, "orig-data") |
| 661 |
fd1 = os.open(docpath, os.O_RDONLY, 0o600) |
| 662 |
|
| 663 |
setFileContent(tmppath, "new-data") |
| 664 |
fd2 = os.open(tmppath, os.O_RDONLY, 0o600) |
| 665 |
|
| 666 |
os.rename(tmppath, tmppath2) |
| 667 |
assertRaises(FileNotFoundError, os.lstat, tmppath) |
| 668 |
assertFdHasContent(fd2, "new-data") |
| 669 |
assertFileHasContent(tmppath2, "new-data") |
| 670 |
|
| 671 |
os.rename(tmppath2, docpath) |
| 672 |
assertRaises(FileNotFoundError, os.lstat, tmppath2) |
| 673 |
assertFdHasContent(fd1, "orig-data") |
| 674 |
assertFdHasContent(fd2, "new-data") |
| 675 |
assertFileHasContent(docpath, "new-data") |
| 676 |
appendFileContent(docpath, "-more") |
| 677 |
assertFdHasContent(fd2, "new-data-more") |
| 678 |
|
| 679 |
setFileContent(tmppath, "replace-this-data") |
| 680 |
fd3 = os.open(tmppath, os.O_RDONLY, 0o600) |
| 681 |
os.rename(docpath, tmppath) |
| 682 |
assertFdHasContent(fd2, "new-data-more") |
| 683 |
assertFdHasContent(fd3, "replace-this-data") |
| 684 |
fd4 = os.open(tmppath, os.O_RDWR, 0o600) |
| 685 |
assertFdHasContent(fd4, "new-data-more") |
| 686 |
|
| 687 |
# Restore original version |
| 688 |
os.rename(tmppath, docpath) |
| 689 |
replaceFdContent(fd4, doc.content) |
| 690 |
assertFdHasContent(fd2, doc.content) |
| 691 |
assertFdHasContent(fd4, doc.content) |
| 692 |
assertFileHasContent(docpath, doc.content) |
| 693 |
assertFdHasContent(fd1, "orig-data") |
| 694 |
assertFdHasContent(fd3, "replace-this-data") |
| 695 |
|
| 696 |
os.close(fd1) |
| 697 |
os.close(fd2) |
| 698 |
os.close(fd3) |
| 699 |
os.close(fd4) |
| 700 |
|
| 701 |
assertRaises(NotADirectoryError, os.rmdir, docpath) |
| 702 |
assertRaises(PermissionError, os.link, docpath, path + "/a-hardlink") |
| 703 |
assertRaises(PermissionError, os.setxattr, docpath, "user.attr", b"foo") |
| 704 |
assertRaises(PermissionError, os.removexattr, docpath, "user.attr") |
| 705 |
|
| 706 |
else: # Main file doesn't exist |
| 707 |
assertFileNotExist(docpath) |
| 708 |
assertFileNotExist(doc.real_path) |
| 709 |
if writable: # But we can create it |
| 710 |
setFileContent(docpath, "some-data") |
| 711 |
assertFileHasContent(docpath, "some-data") |
| 712 |
os.unlink(docpath) |
| 713 |
else: # And we can't create it |
| 714 |
assertRaises( |
| 715 |
PermissionError, |
| 716 |
os.open, |
| 717 |
docpath, |
| 718 |
os.O_CREAT | os.O_RDONLY | os.O_TRUNC, |
| 719 |
0o600, |
| 720 |
) |
| 721 |
assertRaises( |
| 722 |
PermissionError, os.open, docpath, os.O_CREAT | os.O_WRONLY, 0o600 |
| 723 |
) |
| 724 |
assertRaises( |
| 725 |
PermissionError, os.open, docpath, os.O_CREAT | os.O_RDWR, 0o600 |
| 726 |
) |
| 727 |
|
| 728 |
# Ensure it show up if created outside |
| 729 |
setFileContent(doc.real_path, "from-outside") |
| 730 |
assertFileExist(docpath) |
| 731 |
assertFileHasContent(docpath, "from-outside") |
| 732 |
if writable: |
| 733 |
os.unlink(docpath) |
| 734 |
else: |
| 735 |
assertRaises(PermissionError, os.unlink, docpath) |
| 736 |
os.unlink(doc.real_path) |
| 737 |
assertFileNotExist(docpath) |
| 738 |
|
| 739 |
if writable: # We can create tempfiles, do some simple checks |
| 740 |
setFileContent(tmppath, "tempdata") |
| 741 |
assertFileHasContent(tmppath, "tempdata") |
| 742 |
assertRaises(NotADirectoryError, os.rmdir, tmppath) |
| 743 |
assertRaises(PermissionError, os.link, tmppath, path + "/a-hardlink") |
| 744 |
assertRaises(PermissionError, os.setxattr, tmppath, "user.attr", b"foo") |
| 745 |
assertRaises(PermissionError, os.removexattr, tmppath, "user.attr") |
| 746 |
|
| 747 |
os.rename(tmppath, tmppath2) |
| 748 |
assertFileHasContent(tmppath2, "tempdata") |
| 749 |
os.unlink(tmppath2) |
| 750 |
else: |
| 751 |
# We should be unable to create tempfiles |
| 752 |
assertRaises( |
| 753 |
PermissionError, |
| 754 |
os.open, |
| 755 |
tmppath, |
| 756 |
os.O_CREAT | os.O_RDONLY | os.O_TRUNC, |
| 757 |
0o600, |
| 758 |
) |
| 759 |
assertRaises(PermissionError, os.open, tmppath, os.O_CREAT | os.O_WRONLY, 0o600) |
| 760 |
assertRaises(PermissionError, os.open, tmppath, os.O_CREAT | os.O_RDWR, 0o600) |
| 761 |
|
| 762 |
|
| 763 |
def check_directory_doc_perms(doc, app_id): |
| 764 |
writable = doc.is_writable_by(app_id) |
| 765 |
|
| 766 |
docpath = doc.get_doc_path(app_id) |
| 767 |
realpath = doc.real_path |
| 768 |
|
| 769 |
# We should not be able to do anything with the toplevel document dir (other than reading the real dir) |
| 770 |
vpath = os.path.dirname(docpath) |
| 771 |
assertDirExist(vpath) |
| 772 |
assertDirFiles(vpath, [doc.dirname]) |
| 773 |
assertRaises(PermissionError, os.mkdir, vpath + "/a_dir") |
| 774 |
assertRaises(PermissionError, os.rename, docpath, vpath + "/foo") |
| 775 |
assertRaises(PermissionError, os.rmdir, docpath) |
| 776 |
assertRaises( |
| 777 |
PermissionError, os.open, vpath + "/a_file", os.O_CREAT | os.O_RDWR, 0o600 |
| 778 |
) |
| 779 |
|
| 780 |
assertDirExist(docpath) |
| 781 |
|
| 782 |
# Create some pre-existing files: |
| 783 |
|
| 784 |
real_dir = realpath + "/dir" |
| 785 |
os.mkdir(real_dir) |
| 786 |
setFileContent(real_dir + "/realfile", "real1") |
| 787 |
setFileContent(real_dir + "/readonly", "readonly") |
| 788 |
os.chmod(real_dir + "/readonly", 0o500) |
| 789 |
os.mkdir(real_dir + "/subdir") |
| 790 |
os.link(real_dir + "/realfile", real_dir + "/subdir/hardlink") |
| 791 |
os.symlink("realfile", real_dir + "/symlink") |
| 792 |
os.symlink("the-void", real_dir + "/broken-symlink") |
| 793 |
|
| 794 |
# Ensure they are visible via portal |
| 795 |
|
| 796 |
dir = docpath + "/dir" |
| 797 |
assertDirFiles(docpath, ["dir"]) |
| 798 |
assertDirExist(dir) |
| 799 |
assertDirFiles(dir, ["realfile", "readonly", "subdir", "symlink", "broken-symlink"]) |
| 800 |
assertDirExist(dir + "/subdir") |
| 801 |
assertDirFiles(dir + "/subdir", ["hardlink"]) |
| 802 |
assertFileHasContent(dir + "/realfile", "real1") |
| 803 |
assertFileHasContent(dir + "/readonly", "readonly") |
| 804 |
assertFileHasContent(dir + "/subdir/hardlink", "real1") |
| 805 |
assert ( |
| 806 |
os.lstat(dir + "/realfile").st_ino == os.lstat(dir + "/subdir/hardlink").st_ino |
| 807 |
) |
| 808 |
assertSymlink(dir + "/symlink", "realfile") |
| 809 |
assertSymlink(dir + "/broken-symlink", "the-void") |
| 810 |
|
| 811 |
filepath = docpath + "/a-file" |
| 812 |
real_filepath = doc.real_path + "/a-file" |
| 813 |
filepath2 = docpath + "/dir/a-file2" |
| 814 |
real_filepath2 = doc.real_path + "/dir/a-file2" |
| 815 |
|
| 816 |
if writable: # We can create files |
| 817 |
if os.environ.get("TEST_IN_ROOTED_CI"): |
| 818 |
assertRaises(PermissionError, os.open, dir + "/readonly", os.O_RDWR) |
| 819 |
os.chmod(dir + "/readonly", 0o700) |
| 820 |
fd = os.open(dir + "/readonly", os.O_RDWR) # Works now |
| 821 |
os.close(fd) |
| 822 |
|
| 823 |
setFileContent(filepath, "filedata") |
| 824 |
assertFileHasContent(filepath, "filedata") |
| 825 |
assertFileHasContent(real_filepath, "filedata") |
| 826 |
|
| 827 |
fd = os.open(filepath, os.O_RDONLY) |
| 828 |
fd2 = os.open(filepath, os.O_RDWR) |
| 829 |
assertFdHasContent(fd, "filedata") |
| 830 |
assertFdHasContent(fd2, "filedata") |
| 831 |
appendFdContent(fd2, "-more") |
| 832 |
assertFdHasContent(fd, "filedata-more") |
| 833 |
assertFdHasContent(fd2, "filedata-more") |
| 834 |
|
| 835 |
os.link(filepath, filepath2) |
| 836 |
assert os.lstat(filepath).st_ino == os.lstat(filepath2).st_ino |
| 837 |
assert os.lstat(filepath).st_ino == os.fstat(fd).st_ino |
| 838 |
assertFileHasContent(filepath2, "filedata-more") |
| 839 |
assertFileHasContent(real_filepath2, "filedata-more") |
| 840 |
|
| 841 |
os.unlink(filepath) |
| 842 |
assertFileNotExist(filepath) |
| 843 |
assertFileNotExist(real_filepath) |
| 844 |
assertFdHasContent(fd, "filedata-more") |
| 845 |
assertFdHasContent(fd2, "filedata-more") |
| 846 |
|
| 847 |
replaceFdContent(fd2, "replaced") |
| 848 |
assertFileHasContent(filepath2, "replaced") |
| 849 |
assertFileHasContent(real_filepath2, "replaced") |
| 850 |
assertFileNotExist(filepath) |
| 851 |
assertFileNotExist(real_filepath) |
| 852 |
assertFdHasContent(fd, "replaced") |
| 853 |
assertFdHasContent(fd2, "replaced") |
| 854 |
|
| 855 |
# Move between dirs |
| 856 |
os.rename(filepath2, docpath + "/moved") |
| 857 |
assertFileHasContent(docpath + "/moved", "replaced") |
| 858 |
|
| 859 |
assertRaisesErrno(errno.EXDEV, os.rename, docpath, doc.portal.mountpoint) |
| 860 |
|
| 861 |
os.unlink(docpath + "/moved") |
| 862 |
|
| 863 |
os.close(fd) |
| 864 |
os.close(fd2) |
| 865 |
|
| 866 |
os.symlink("realfile", dir + "/symlink2") |
| 867 |
os.symlink("the-void", dir + "/broken-symlink2") |
| 868 |
assertSymlink(dir + "/symlink2", "realfile") |
| 869 |
assertSymlink(dir + "/broken-symlink2", "the-void") |
| 870 |
os.unlink(dir + "/symlink2") |
| 871 |
os.unlink(dir + "/broken-symlink2") |
| 872 |
|
| 873 |
else: |
| 874 |
# We should be unable to create files |
| 875 |
assertRaises( |
| 876 |
PermissionError, |
| 877 |
os.open, |
| 878 |
filepath, |
| 879 |
os.O_CREAT | os.O_RDONLY | os.O_TRUNC, |
| 880 |
0o600, |
| 881 |
) |
| 882 |
assertRaises( |
| 883 |
PermissionError, os.open, filepath, os.O_CREAT | os.O_WRONLY, 0o600 |
| 884 |
) |
| 885 |
assertRaises(PermissionError, os.open, filepath, os.O_CREAT | os.O_RDWR, 0o600) |
| 886 |
|
| 887 |
assertRaises(PermissionError, os.open, dir + "/realfile", os.O_RDWR) |
| 888 |
assertRaises(PermissionError, os.open, dir + "/readonly", os.O_RDWR) |
| 889 |
assertRaises(PermissionError, os.truncate, dir + "/realfile", 0) |
| 890 |
assertRaises(PermissionError, os.link, dir + "/realfile", dir + "/foo") |
| 891 |
assertRaises(PermissionError, os.symlink, "foo", dir + "/new-symlink") |
| 892 |
assertRaises(PermissionError, os.rename, dir + "/realfile", dir + "/foo") |
| 893 |
assertRaises(PermissionError, os.unlink, dir + "/realfile") |
| 894 |
assertRaises(PermissionError, os.chmod, dir + "/realfile", 0o700) |
| 895 |
assertRaises(PermissionError, os.rmdir, dir + "/subdir") |
| 896 |
|
| 897 |
os.unlink(real_dir + "/realfile") |
| 898 |
os.unlink(real_dir + "/readonly") |
| 899 |
os.unlink(real_dir + "/subdir/hardlink") |
| 900 |
os.unlink(real_dir + "/symlink") |
| 901 |
os.unlink(real_dir + "/broken-symlink") |
| 902 |
os.rmdir(real_dir + "/subdir") |
| 903 |
os.rmdir(real_dir) |
| 904 |
|
| 905 |
|
| 906 |
def check_doc_perms(doc, app_id): |
| 907 |
path = doc.get_doc_path(app_id) |
| 908 |
readable = doc.is_readable_by(app_id) |
| 909 |
if not readable: |
| 910 |
assertRaises(FileNotFoundError, os.lstat, path) |
| 911 |
if doc.is_dir: # Non readable dir means we can't even see the toplevel dir |
| 912 |
assertRaises(FileNotFoundError, os.mkdir, path) |
| 913 |
else: |
| 914 |
assertRaises(PermissionError, os.mkdir, path) |
| 915 |
return |
| 916 |
|
| 917 |
assertRaises(PermissionError, os.rmdir, path) |
| 918 |
assertRaises(PermissionError, os.rename, path, path + "_renamed") |
| 919 |
assertRaises(IsADirectoryError, os.unlink, path) |
| 920 |
if doc.is_dir: |
| 921 |
check_directory_doc_perms(doc, app_id) |
| 922 |
else: |
| 923 |
check_regular_doc_perms(doc, app_id) |
| 924 |
|
| 925 |
|
| 926 |
def check_perms(portal): |
| 927 |
check_root_perms(portal.mountpoint) |
| 928 |
check_byapp_perms(portal.by_app_path()) |
| 929 |
|
| 930 |
for doc in portal.get_docs_randomized(): |
| 931 |
check_doc_perms(doc, None) |
| 932 |
for app_id in portal.get_app_ids_randomized(): |
| 933 |
check_doc_perms(doc, app_id) |
| 934 |
|
| 935 |
|
| 936 |
# Ensure that a single lookup by app-id creates that app id (we need this for when mounting the subdir for an app) |
| 937 |
def create_app_by_lookup(portal): |
| 938 |
# Should only work for valid app ids |
| 939 |
assertRaises(FileNotFoundError, os.lstat, portal.app_path("not-an-app-id")) |
| 940 |
|
| 941 |
app_id = app_prefix + "Lookup" |
| 942 |
info = os.lstat(portal.app_path(app_id)) |
| 943 |
check_virtual_stat(info) |
| 944 |
portal.ensure_app_id(app_id, volatile=True) |
| 945 |
|
| 946 |
|
| 947 |
def ensure_real_dir(create_hidden_file=True): |
| 948 |
count = get_a_count("doc") |
| 949 |
dir = os.environ["TMPDIR"] + "/" + dir_prefix + str(count) |
| 950 |
os.makedirs(dir) |
| 951 |
if create_hidden_file: |
| 952 |
setFileContent(dir + "/cant-see-this-file", "s3krit") |
| 953 |
return (dir, count) |
| 954 |
|
| 955 |
|
| 956 |
def ensure_real_dir_file(create_file): |
| 957 |
(dir, count) = ensure_real_dir() |
| 958 |
path = dir + "/the-file" |
| 959 |
if create_file: |
| 960 |
setFileContent(path, "data" + str(count)) |
| 961 |
return path |
| 962 |
|
| 963 |
|
| 964 |
def export_a_doc(portal): |
| 965 |
path = ensure_real_dir_file(True) |
| 966 |
doc = portal.add(path) |
| 967 |
logv("exported %s as %s" % (path, doc)) |
| 968 |
|
| 969 |
lookup = portal.lookup(path) |
| 970 |
assert lookup == doc.id |
| 971 |
|
| 972 |
lookup_on_fuse = portal.lookup(doc.get_doc_path(None) + "/" + doc.filename) |
| 973 |
assert lookup_on_fuse == doc.id |
| 974 |
|
| 975 |
reused_doc = portal.add(path) |
| 976 |
assert doc is reused_doc |
| 977 |
|
| 978 |
not_reused_doc = portal.add(path, False) |
| 979 |
assert doc is not not_reused_doc |
| 980 |
|
| 981 |
# We should not be able to re-export a tmpfile |
| 982 |
tmppath = doc.get_doc_path(None) + "/tmpfile" |
| 983 |
setFileContent(tmppath, "tempdata") |
| 984 |
|
| 985 |
# Should not be able to add a tempfile on the fuse mount, or look it up |
| 986 |
assertRaises(GLib.Error, portal.add, tmppath) |
| 987 |
lookup = portal.lookup(tmppath) |
| 988 |
assert lookup == "" |
| 989 |
|
| 990 |
os.unlink(tmppath) |
| 991 |
|
| 992 |
|
| 993 |
def export_a_named_doc(portal, create_file): |
| 994 |
path = ensure_real_dir_file(create_file) |
| 995 |
doc = portal.add_named(path) |
| 996 |
logv("exported (named) %s as %s" % (path, doc)) |
| 997 |
|
| 998 |
if create_file: |
| 999 |
lookup = portal.lookup(path) |
| 1000 |
assert lookup == doc.id |
| 1001 |
|
| 1002 |
reused_doc = portal.add_named(path) |
| 1003 |
assert doc is reused_doc |
| 1004 |
|
| 1005 |
not_reused_doc = portal.add_named(path, False) |
| 1006 |
assert doc is not not_reused_doc |
| 1007 |
|
| 1008 |
|
| 1009 |
def export_a_dir_doc(portal): |
| 1010 |
(dir, count) = ensure_real_dir(False) |
| 1011 |
doc = portal.add_dir(dir) |
| 1012 |
logv("exported (dir) %s as %s" % (dir, doc)) |
| 1013 |
|
| 1014 |
lookup = portal.lookup(dir) |
| 1015 |
assert lookup == doc.id |
| 1016 |
|
| 1017 |
lookup_on_fuse = portal.lookup(doc.get_doc_path(None)) |
| 1018 |
assert lookup_on_fuse == doc.id |
| 1019 |
|
| 1020 |
# We should not be able to portal lookup a file in the dir doc |
| 1021 |
subpath = doc.get_doc_path(None) + "/sub" |
| 1022 |
setFileContent(subpath, "sub") |
| 1023 |
doc = portal.lookup(subpath) |
| 1024 |
assert doc == "" |
| 1025 |
doc2 = portal.lookup(dir + "/sub") |
| 1026 |
assert doc2 == "" |
| 1027 |
|
| 1028 |
# But we should be able to re-export the file |
| 1029 |
reexported_doc = portal.add(subpath) |
| 1030 |
reexported_docdir = reexported_doc.get_doc_path(None) |
| 1031 |
assertFileHasContent(reexported_docdir + "/sub", "sub") |
| 1032 |
portal.delete(reexported_doc.id) |
| 1033 |
|
| 1034 |
os.unlink(subpath) |
| 1035 |
|
| 1036 |
# And also re-export a directory |
| 1037 |
os.mkdir(subpath) |
| 1038 |
setFileContent(subpath + "/subfile", "subfile") |
| 1039 |
reexported_doc = portal.add_dir(subpath) |
| 1040 |
reexported_docdir = reexported_doc.get_doc_path(None) |
| 1041 |
assertFileHasContent(reexported_docdir + "/subfile", "subfile") |
| 1042 |
portal.delete(reexported_doc.id) |
| 1043 |
|
| 1044 |
os.unlink(subpath + "/subfile") |
| 1045 |
os.rmdir(subpath) |
| 1046 |
|
| 1047 |
|
| 1048 |
def add_an_app(portal, num_docs): |
| 1049 |
if num_docs == 0: |
| 1050 |
return |
| 1051 |
count = get_a_count("app") |
| 1052 |
read_app = app_prefix + "read.App" + str(count) |
| 1053 |
write_app = app_prefix + "write.App" + str(count) |
| 1054 |
portal.ensure_app_id(read_app) |
| 1055 |
portal.ensure_app_id(write_app) |
| 1056 |
|
| 1057 |
docs = portal.get_docs() |
| 1058 |
ids = [] |
| 1059 |
for i in range(num_docs): |
| 1060 |
if len(docs) == 0: |
| 1061 |
continue |
| 1062 |
indx = random.randint(0, len(docs) - 1) |
| 1063 |
doc = docs[indx] |
| 1064 |
del docs[indx] |
| 1065 |
ids.append(doc.id) |
| 1066 |
portal.grant_permissions(doc.id, read_app, ["read"]) |
| 1067 |
doc.apps.append(read_app) |
| 1068 |
portal.grant_permissions(doc.id, write_app, ["read", "write"]) |
| 1069 |
doc.apps.append(write_app) |
| 1070 |
logv("granted acces to %s and %s for %s" % (read_app, write_app, ids)) |
| 1071 |
|
| 1072 |
|
| 1073 |
def file_transfer_portal_test(): |
| 1074 |
log("File transfer tests") |
| 1075 |
ft_portal = FileTransferPortal() |
| 1076 |
|
| 1077 |
key = ft_portal.start_transfer() |
| 1078 |
|
| 1079 |
file1 = ensure_real_dir_file(True) |
| 1080 |
file2 = ensure_real_dir_file(True) |
| 1081 |
res = ft_portal.add_files(key, [file1, file2]) |
| 1082 |
|
| 1083 |
res = ft_portal.retrieve_files(key) |
| 1084 |
files = res[0] |
| 1085 |
assert len(files) == 2 |
| 1086 |
# This is the same app, it's not sandboxed |
| 1087 |
assert files[0] == file1 |
| 1088 |
assert files[1] == file2 |
| 1089 |
log("filetransfer tests ok") |
| 1090 |
|
| 1091 |
log("filetransfer dir") |
| 1092 |
key = ft_portal.start_transfer() |
| 1093 |
dir1 = ensure_real_dir(True) |
| 1094 |
ft_portal.add_files(key, [file1, dir1[0], file2]) |
| 1095 |
res = ft_portal.retrieve_files(key) |
| 1096 |
assert len(res[0]) == 3 |
| 1097 |
assert res[0][0] == file1 |
| 1098 |
assert res[0][1] == dir1[0] |
| 1099 |
assert res[0][2] == file2 |
| 1100 |
log("filetransfer dir ok") |
| 1101 |
|
| 1102 |
log("filetransfer key") |
| 1103 |
# Test that an invalid key is rejected |
| 1104 |
key = ft_portal.start_transfer() |
| 1105 |
assert key != "1234" |
| 1106 |
assertRaisesGError( |
| 1107 |
"GDBus.Error:org.freedesktop.DBus.Error.AccessDenied", |
| 1108 |
9, |
| 1109 |
ft_portal.add_files, |
| 1110 |
"1234", |
| 1111 |
[file1, file2], |
| 1112 |
) |
| 1113 |
|
| 1114 |
# Test stop transfer |
| 1115 |
key = ft_portal.start_transfer() |
| 1116 |
ft_portal.add_files(key, [file1, file2]) |
| 1117 |
ft_portal.stop_transfer(key) |
| 1118 |
assertRaisesGError( |
| 1119 |
"GDBus.Error:org.freedesktop.DBus.Error.AccessDenied", |
| 1120 |
9, |
| 1121 |
ft_portal.retrieve_files, |
| 1122 |
key, |
| 1123 |
) |
| 1124 |
assertRaisesGError( |
| 1125 |
"GDBus.Error:org.freedesktop.DBus.Error.AccessDenied", |
| 1126 |
9, |
| 1127 |
ft_portal.add_files, |
| 1128 |
key, |
| 1129 |
[file1, file2], |
| 1130 |
) |
| 1131 |
|
| 1132 |
# Test that we can't reuse an old key |
| 1133 |
new_key = ft_portal.start_transfer() |
| 1134 |
assertRaisesGError( |
| 1135 |
"GDBus.Error:org.freedesktop.DBus.Error.AccessDenied", |
| 1136 |
9, |
| 1137 |
ft_portal.add_files, |
| 1138 |
key, |
| 1139 |
[file1, file2], |
| 1140 |
) |
| 1141 |
res = ft_portal.add_files(new_key, [file1, file2]) |
| 1142 |
log("filetransfer key ok") |
| 1143 |
|
| 1144 |
log("File transfer tests ok") |
| 1145 |
|
| 1146 |
|
| 1147 |
def run_test(iterations, prefix=None, do_ensure_no_remaining=True): |
| 1148 |
global app_prefix |
| 1149 |
global dir_prefix |
| 1150 |
global ensure_no_remaining |
| 1151 |
|
| 1152 |
if prefix: |
| 1153 |
app_prefix = app_prefix + prefix + "." |
| 1154 |
dir_prefix = dir_prefix + "-" + prefix + "-" |
| 1155 |
|
| 1156 |
ensure_no_remaining = do_ensure_no_remaining |
| 1157 |
|
| 1158 |
log("Connecting to portal") |
| 1159 |
doc_portal = DocPortal() |
| 1160 |
|
| 1161 |
log("Running fuse tests...") |
| 1162 |
create_app_by_lookup(doc_portal) |
| 1163 |
verify_fs_layout(doc_portal) |
| 1164 |
|
| 1165 |
log("Creating some docs") |
| 1166 |
for i in range(10): |
| 1167 |
export_a_doc(doc_portal) |
| 1168 |
verify_fs_layout(doc_portal) |
| 1169 |
|
| 1170 |
log("Creating some named docs (existing)") |
| 1171 |
for i in range(10): |
| 1172 |
export_a_named_doc(doc_portal, True) |
| 1173 |
verify_fs_layout(doc_portal) |
| 1174 |
|
| 1175 |
log("Creating some named docs (non-existing)") |
| 1176 |
for i in range(10): |
| 1177 |
export_a_named_doc(doc_portal, False) |
| 1178 |
verify_fs_layout(doc_portal) |
| 1179 |
|
| 1180 |
log("Creating some dir docs") |
| 1181 |
for i in range(10): |
| 1182 |
export_a_dir_doc(doc_portal) |
| 1183 |
verify_fs_layout(doc_portal) |
| 1184 |
|
| 1185 |
log("Creating some apps") |
| 1186 |
for i in range(10): |
| 1187 |
add_an_app(doc_portal, 6) |
| 1188 |
verify_fs_layout(doc_portal) |
| 1189 |
|
| 1190 |
for i in range(iterations): |
| 1191 |
log("Checking permissions, pass %d" % (i + 1)) |
| 1192 |
check_perms(doc_portal) |
| 1193 |
verify_fs_layout(doc_portal) |
| 1194 |
|
| 1195 |
log("fuse tests ok") |
| 1196 |
file_transfer_portal_test() |
| 1197 |
|
| 1198 |
|
| 1199 |
class Process(mp.Process): |
| 1200 |
def __init__(self, *args, **kwargs): |
| 1201 |
mp.Process.__init__(self, *args, **kwargs) |
| 1202 |
self._pconn, self._cconn = mp.Pipe() |
| 1203 |
self._exception = None |
| 1204 |
|
| 1205 |
def run(self): |
| 1206 |
try: |
| 1207 |
mp.Process.run(self) |
| 1208 |
self._cconn.send(None) |
| 1209 |
except Exception as e: |
| 1210 |
tb = traceback.format_exc() |
| 1211 |
self._cconn.send((e, tb)) |
| 1212 |
|
| 1213 |
@property |
| 1214 |
def exception(self): |
| 1215 |
if self._pconn.poll(): |
| 1216 |
self._exception = self._pconn.recv() |
| 1217 |
return self._exception |
| 1218 |
|
| 1219 |
|
| 1220 |
class TestDocumentFuse: |
| 1221 |
def parallel(self, test_function, parallel_tests, parallel_iterations): |
| 1222 |
procs = [] |
| 1223 |
for i in range(parallel_tests): |
| 1224 |
p = Process( |
| 1225 |
target=test_function, args=(parallel_iterations, f"c{i}", False) |
| 1226 |
) |
| 1227 |
p.start() |
| 1228 |
procs.append(p) |
| 1229 |
|
| 1230 |
for p in procs: |
| 1231 |
p.join() |
| 1232 |
|
| 1233 |
if p.exception: |
| 1234 |
error, traceback = p.exception |
| 1235 |
raise error |
| 1236 |
|
| 1237 |
def test_single_thread(self, portals, xdg_document_portal, dbus_con): |
| 1238 |
run_test(3) |
| 1239 |
|
| 1240 |
def test_multi_thread(self, portals, xdg_document_portal, dbus_con): |
| 1241 |
if xdp.run_long_tests(): |
| 1242 |
return self.parallel(run_test, 20, 10) |
| 1243 |
if xdp.is_in_ci(): |
| 1244 |
return self.parallel(run_test, 5, 3) |
| 1245 |
self.parallel(run_test, 10, 5) |
| 1246 |
|
| 1247 |
|
| 1248 |
# Running |
| 1249 |
# ./tests/run-test.sh -n 0 tests/test_document_fuse.py::TestDocumentFuse::test_multi_thread |
| 1250 |
# works fine, but with |
| 1251 |
# ./tests/run-test.sh -n 0 tests/test_document_fuse.py::TestDocumentFuse |
| 1252 |
# the `test_multi_thread` test is failing. |
| 1253 |
# For now, let's skip the test and turn it on again when we have fixed it. |
| 1254 |
pytest.skip("Test has a race condition which can make it fail", allow_module_level=True) |
| 1255 |
|
| 1256 |
try: |
| 1257 |
xdp.ensure_fuse_supported() |
| 1258 |
except xdp.FuseNotSupportedException as e: |
| 1259 |
pytest.skip(f"No fuse support: {e}", allow_module_level=True) |