| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# udisks2 integration test suite |
| 4 |
# |
| 5 |
# Run in udisks built tree to test local built binaries (needs |
| 6 |
# --localstatedir=/var), or from anywhere else to test system installed |
| 7 |
# binaries. |
| 8 |
# |
| 9 |
# Usage: |
| 10 |
# - Run all tests: |
| 11 |
# src/tests/integration-test |
| 12 |
# - Run only a particular class of tests: |
| 13 |
# src/tests/integration-test Drive |
| 14 |
# - Run only a single test: |
| 15 |
# src/tests/integration-test FS.test_ext3 |
| 16 |
# |
| 17 |
# Copyright: (C) 2011 Martin Pitt <[email protected]> |
| 18 |
# |
| 19 |
# This program is free software; you can redistribute it and/or modify |
| 20 |
# it under the terms of the GNU General Public License as published by |
| 21 |
# the Free Software Foundation; either version 2 of the License, or |
| 22 |
# (at your option) any later version. |
| 23 |
# |
| 24 |
# This program is distributed in the hope that it will be useful, |
| 25 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
# GNU General Public License for more details. |
| 28 |
|
| 29 |
# TODO: |
| 30 |
# - add and test method for changing LUKS passphrase |
| 31 |
# - test Format with take-ownership |
| 32 |
|
| 33 |
from __future__ import print_function |
| 34 |
|
| 35 |
import sys |
| 36 |
import os |
| 37 |
import contextlib |
| 38 |
import signal |
| 39 |
import shutil |
| 40 |
from os.path import dirname |
| 41 |
|
| 42 |
srcdir = dirname(dirname(dirname(os.path.realpath(__file__)))) |
| 43 |
libdir = os.path.join(srcdir, 'udisks', '.libs') |
| 44 |
|
| 45 |
# as we can't change LD_LIBRARY_PATH within a running program, and doing |
| 46 |
# #!/usr/bin/env LD_LIBRARY_PATH=... python3 does not work either, do this |
| 47 |
# nasty hack |
| 48 |
if 'LD_LIBRARY_PATH' not in os.environ and os.path.isdir(libdir): |
| 49 |
os.environ['LD_LIBRARY_PATH'] = libdir |
| 50 |
os.environ['GI_TYPELIB_PATH'] = '%s/udisks:%s' % ( |
| 51 |
srcdir, |
| 52 |
os.environ.get('GI_TYPELIB_PATH', '')) |
| 53 |
os.execv(sys.executable, [sys.executable] + sys.argv) |
| 54 |
assert False, 'not expecting to land here' |
| 55 |
|
| 56 |
import subprocess |
| 57 |
import unittest |
| 58 |
import tempfile |
| 59 |
import atexit |
| 60 |
import time |
| 61 |
import shutil |
| 62 |
import signal |
| 63 |
import argparse |
| 64 |
import re |
| 65 |
from glob import glob |
| 66 |
import gi |
| 67 |
|
| 68 |
gi.require_version('UDisks', '2.0') |
| 69 |
|
| 70 |
from gi.repository import GLib, Gio, UDisks |
| 71 |
|
| 72 |
# find local test_polkit.py |
| 73 |
sys.path.insert(0, dirname(__file__)) |
| 74 |
import test_polkitd |
| 75 |
|
| 76 |
# GI_TYPELIB_PATH=udisks LD_LIBRARY_PATH=udisks/.libs |
| 77 |
|
| 78 |
# size of virtual test device |
| 79 |
# XFS needs at least 300 MB |
| 80 |
VDEV_SIZE = 317718528 |
| 81 |
|
| 82 |
|
| 83 |
# Those file systems are known to have a broken handling of permissions, in |
| 84 |
# particular the executable bit |
| 85 |
BROKEN_PERMISSIONS_FS = ['ntfs', 'exfat'] |
| 86 |
|
| 87 |
no_options = GLib.Variant('a{sv}', {}) |
| 88 |
|
| 89 |
def get_distro_version(): |
| 90 |
try: |
| 91 |
out = subprocess.check_output('hostnamectl status | grep "CPE OS Name"', shell=True).decode().strip() |
| 92 |
try: |
| 93 |
# 3rd to 6th fields from e.g. "CPE OS Name: cpe:/o:fedoraproject:fedora:27" or "CPE OS Name: cpe:/o:centos:centos:7" |
| 94 |
_project, distro, version = tuple(out.split(":")[3:6]) |
| 95 |
except ValueError: |
| 96 |
# Returns a default value that is not likely something used to filter real systems |
| 97 |
return ("Linux", "0") |
| 98 |
except subprocess.CalledProcessError: |
| 99 |
# Returns a default value that is not likely something used to filter real systems |
| 100 |
return ("Linux", "0") |
| 101 |
|
| 102 |
return (distro, version) |
| 103 |
|
| 104 |
DISTRO_VER = get_distro_version() |
| 105 |
|
| 106 |
|
| 107 |
# ---------------------------------------------------------------------------- |
| 108 |
|
| 109 |
class UDisksTestCase(unittest.TestCase): |
| 110 |
"""Base class for udisks test cases. |
| 111 |
|
| 112 |
This provides static functions which are useful for all test cases. |
| 113 |
""" |
| 114 |
daemon = None |
| 115 |
daemon_log = None |
| 116 |
daemon_path = None |
| 117 |
dbus = None |
| 118 |
device = None |
| 119 |
cd_device = "" |
| 120 |
client = None |
| 121 |
manager = None |
| 122 |
system = False |
| 123 |
|
| 124 |
@classmethod |
| 125 |
def init(cls, logfile=None, system=False): |
| 126 |
"""start daemon and set up test environment""" |
| 127 |
|
| 128 |
# test against running system instance |
| 129 |
cls.system = system |
| 130 |
|
| 131 |
if os.geteuid() != 0: |
| 132 |
print('this test suite needs to run as root', file=sys.stderr) |
| 133 |
sys.exit(1) |
| 134 |
|
| 135 |
# run from local build tree if we are in one, otherwise use system instance |
| 136 |
cls.daemon_path = os.path.join(srcdir, 'src', '.libs', 'udisksd') |
| 137 |
if not system and (os.access(cls.daemon_path, os.X_OK)): |
| 138 |
print('Testing binaries from local build tree') |
| 139 |
cls.check_build_tree_config() |
| 140 |
# copy required system files |
| 141 |
testdir = os.path.abspath(os.path.dirname(__file__)) |
| 142 |
projdir = os.path.abspath(os.path.normpath(os.path.join(testdir, '..', '..'))) |
| 143 |
tmpdir = tempfile.mkdtemp(prefix='udisks-tst-') |
| 144 |
atexit.register(shutil.rmtree, tmpdir) |
| 145 |
files_to_restore = install_config_files(projdir, tmpdir) |
| 146 |
atexit.register(restore_files, files_to_restore, tmpdir) |
| 147 |
else: |
| 148 |
print('Testing installed system binaries') |
| 149 |
cls.daemon_path = None |
| 150 |
for l in open('/usr/share/dbus-1/system-services/' |
| 151 |
'org.freedesktop.UDisks2.service'): |
| 152 |
if l.startswith('Exec='): |
| 153 |
cls.daemon_path = l.split('=', 1)[1].split()[0] |
| 154 |
break |
| 155 |
assert cls.daemon_path, 'could not determine daemon path from D-BUS .service file' |
| 156 |
|
| 157 |
print('daemon path: ' + cls.daemon_path) |
| 158 |
|
| 159 |
(cls.device, cls.cd_device) = cls.setup_vdev() |
| 160 |
|
| 161 |
if not system: |
| 162 |
# start polkit and udisks on a private DBus |
| 163 |
cls.dbus = Gio.TestDBus() |
| 164 |
cls.dbus.up() |
| 165 |
os.environ['DBUS_SYSTEM_BUS_ADDRESS'] = cls.dbus.get_bus_address() |
| 166 |
|
| 167 |
# do not try to communicate with the current desktop session; this will |
| 168 |
# confuse it, as it cannot see this D-BUS instance |
| 169 |
try: |
| 170 |
del os.environ['DISPLAY'] |
| 171 |
except KeyError: |
| 172 |
pass |
| 173 |
if logfile: |
| 174 |
cls.daemon_log = open(logfile, 'w') |
| 175 |
else: |
| 176 |
cls.daemon_log = tempfile.TemporaryFile() |
| 177 |
atexit.register(cls.cleanup) |
| 178 |
|
| 179 |
cls.start_daemon(system) |
| 180 |
|
| 181 |
@classmethod |
| 182 |
def cleanup(cls): |
| 183 |
"""stop daemon again and clean up test environment""" |
| 184 |
|
| 185 |
# if a test failed |
| 186 |
subprocess.call(['umount', cls.device], stderr=subprocess.PIPE) |
| 187 |
|
| 188 |
cls.stop_daemon() |
| 189 |
|
| 190 |
cls.teardown_cd_vdev(cls.cd_device) |
| 191 |
cls.teardown_vdev(cls.device) |
| 192 |
cls.device = None |
| 193 |
|
| 194 |
os.environ.pop('DBUS_SYSTEM_BUS_ADDRESS', None) |
| 195 |
if cls.dbus: |
| 196 |
cls.dbus.down() |
| 197 |
|
| 198 |
@classmethod |
| 199 |
def start_daemon(cls, system): |
| 200 |
if not system: |
| 201 |
assert cls.daemon is None |
| 202 |
cls.daemon = subprocess.Popen([cls.daemon_path, '--replace'], |
| 203 |
stdout=cls.daemon_log, |
| 204 |
stderr=subprocess.STDOUT) |
| 205 |
assert cls.daemon.pid, 'daemon failed to start' |
| 206 |
|
| 207 |
# wait until the daemon has started up |
| 208 |
timeout = 10 |
| 209 |
cls.manager = None |
| 210 |
while cls.manager is None and timeout > 0: |
| 211 |
time.sleep(0.2) |
| 212 |
cls.client = UDisks.Client.new_sync(None) |
| 213 |
assert cls.client is not None |
| 214 |
cls.manager = cls.client.get_manager() |
| 215 |
timeout -= 1 |
| 216 |
assert cls.manager, 'daemon failed to start' |
| 217 |
|
| 218 |
if not system: |
| 219 |
assert cls.daemon.pid, 'daemon failed to start' |
| 220 |
|
| 221 |
cls.sync() |
| 222 |
|
| 223 |
@classmethod |
| 224 |
def stop_daemon(cls): |
| 225 |
if cls.daemon: |
| 226 |
os.kill(cls.daemon.pid, signal.SIGTERM) |
| 227 |
os.wait() |
| 228 |
cls.daemon = None |
| 229 |
|
| 230 |
@classmethod |
| 231 |
def sync(cls): |
| 232 |
"""Wait until pending events finished processing. |
| 233 |
|
| 234 |
This should only be called for situations where we genuinely have an |
| 235 |
asynchronous response, like invoking a CLI program and waiting for |
| 236 |
udev/udisks to catch up on the change events. |
| 237 |
""" |
| 238 |
subprocess.call(['udevadm', 'settle']) |
| 239 |
context = GLib.main_context_default() |
| 240 |
timeout = 100 |
| 241 |
# wait until all GDBus events have been processed |
| 242 |
while context.pending() and timeout > 0: |
| 243 |
cls.client.settle() |
| 244 |
time.sleep(0.1) |
| 245 |
timeout -= 1 |
| 246 |
if timeout <= 0: |
| 247 |
cls.write_stderr('[wait timeout!] ') |
| 248 |
|
| 249 |
@classmethod |
| 250 |
def zero_device(cls): |
| 251 |
subprocess.call(['dd', 'if=/dev/zero', 'of=' + cls.device, 'bs=10M'], |
| 252 |
stderr=subprocess.PIPE) |
| 253 |
time.sleep(0.5) |
| 254 |
cls.sync() |
| 255 |
|
| 256 |
@classmethod |
| 257 |
def devname(cls, partition=None, cd=False): |
| 258 |
"""Get name of test device or one of its partitions |
| 259 |
|
| 260 |
If cd is True, return the CD device, otherwise the hard disk device. |
| 261 |
""" |
| 262 |
if cd: |
| 263 |
dev = cls.cd_device |
| 264 |
else: |
| 265 |
dev = cls.device |
| 266 |
if partition: |
| 267 |
if dev[-1].isdigit(): |
| 268 |
return dev + 'p' + str(partition) |
| 269 |
else: |
| 270 |
return dev + str(partition) |
| 271 |
else: |
| 272 |
return dev |
| 273 |
|
| 274 |
@classmethod |
| 275 |
def udisks_block(cls, partition=None, cd=False): |
| 276 |
"""Get UDisksBlock object for test device or partition |
| 277 |
|
| 278 |
If cd is True, return the CD device, otherwise the hard disk device. |
| 279 |
""" |
| 280 |
assert cls.client |
| 281 |
devname = cls.devname(partition, cd) |
| 282 |
dev_t = os.stat(devname).st_rdev |
| 283 |
block = cls.client.get_block_for_dev(dev_t) |
| 284 |
assert block, 'did not find an UDisksBlock object for %s' % devname |
| 285 |
return block |
| 286 |
|
| 287 |
@classmethod |
| 288 |
def udisks_filesystem(cls, partition=None, cd=False): |
| 289 |
"""Get UDisksFilesystem object for test device or partition |
| 290 |
|
| 291 |
Return None if there is no file system on that device. |
| 292 |
|
| 293 |
If cd is True, return the CD device, otherwise the hard disk device. |
| 294 |
""" |
| 295 |
block = cls.udisks_block(partition, cd) |
| 296 |
return cls.client.get_object(block.get_object_path()).get_filesystem() |
| 297 |
|
| 298 |
@classmethod |
| 299 |
def blkid(cls, partition=None, device=None): |
| 300 |
"""Call blkid and return dictionary of results.""" |
| 301 |
|
| 302 |
if not device: |
| 303 |
device = cls.devname(partition) |
| 304 |
result = {} |
| 305 |
cmd = subprocess.Popen(['blkid', '-p', '-o', 'udev', device], stdout=subprocess.PIPE) |
| 306 |
for l in cmd.stdout: |
| 307 |
(key, value) = l.decode('UTF-8').split('=', 1) |
| 308 |
result[key] = value.strip() |
| 309 |
assert cmd.wait() == 0 |
| 310 |
return result |
| 311 |
|
| 312 |
@classmethod |
| 313 |
def is_mountpoint(cls, path): |
| 314 |
"""Check if given path is a mount point.""" |
| 315 |
|
| 316 |
return subprocess.call(['mountpoint', path], stdout=subprocess.PIPE) == 0 |
| 317 |
|
| 318 |
@classmethod |
| 319 |
def mkfs(cls, fs_type, label=None, partition=None): |
| 320 |
"""Create file system using mkfs.""" |
| 321 |
|
| 322 |
if fs_type == 'minix': |
| 323 |
assert label is None, 'minix does not support labels' |
| 324 |
|
| 325 |
# work around mkswap not properly cleaning up an existing reiserfs |
| 326 |
# signature (mailed kzak about it) |
| 327 |
if fs_type == 'swap': |
| 328 |
subprocess.check_call(['wipefs', '-a', cls.devname(partition)], |
| 329 |
stdout=subprocess.PIPE) |
| 330 |
|
| 331 |
mkcmd = {'swap': 'mkswap', |
| 332 |
'ntfs': 'mkntfs'} |
| 333 |
label_opt = {'vfat': '-n', |
| 334 |
'exfat': '-n', |
| 335 |
'f2fs': '-l', |
| 336 |
'reiserfs': '-l'} |
| 337 |
extra_opt = {'vfat': ['-I', '-F', '32'], |
| 338 |
'swap': ['-f'], |
| 339 |
'xfs': ['-f'], # XFS complains if there's an existing FS, so force |
| 340 |
'ext2': ['-F'], # ext* complains about using entire device, so force |
| 341 |
'ext3': ['-F'], |
| 342 |
'ext4': ['-F'], |
| 343 |
'ntfs': ['-F'], |
| 344 |
'btrfs': ['-f'], |
| 345 |
'reiserfs': ['-ff']} |
| 346 |
|
| 347 |
cmd = [mkcmd.get(fs_type, 'mkfs.' + fs_type)] |
| 348 |
cmd += extra_opt.get(fs_type, []) |
| 349 |
if label: |
| 350 |
cmd += [label_opt.get(fs_type, '-L'), label] |
| 351 |
cmd.append(cls.devname(partition)) |
| 352 |
|
| 353 |
subprocess.check_call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 354 |
|
| 355 |
# kernel/udev generally detect those changes itself, but do not quite |
| 356 |
# tell us when they are done; so do a little kludge here to know how |
| 357 |
# long we need to wait |
| 358 |
subprocess.call(['udevadm', 'trigger', '--action=change', |
| 359 |
'--sysname-match=' + os.path.basename(cls.devname(partition))]) |
| 360 |
cls.sync() |
| 361 |
|
| 362 |
@classmethod |
| 363 |
def fs_create(cls, partition, fs_type, options): |
| 364 |
"""Create file system using udisks.""" |
| 365 |
|
| 366 |
block = cls.udisks_block(partition) |
| 367 |
block.call_format_sync(fs_type, options, None) |
| 368 |
|
| 369 |
@classmethod |
| 370 |
def retry_busy(cls, fn, *args): |
| 371 |
"""Call a function until it does not fail with "Busy".""" |
| 372 |
|
| 373 |
timeout = 10 |
| 374 |
while timeout >= 0: |
| 375 |
try: |
| 376 |
return fn(*args) |
| 377 |
except GLib.GError as e: |
| 378 |
if 'UDisks2.Error.DeviceBusy' not in e.message: |
| 379 |
raise |
| 380 |
cls.write_stderr('[busy] ') |
| 381 |
time.sleep(0.3) |
| 382 |
timeout -= 1 |
| 383 |
|
| 384 |
@classmethod |
| 385 |
def check_build_tree_config(cls): |
| 386 |
"""Check configuration of build tree""" |
| 387 |
|
| 388 |
# read make variables |
| 389 |
make_vars = {} |
| 390 |
var_re = re.compile(r'^([a-zA-Z_]+) = (.*)$') |
| 391 |
make = subprocess.Popen(['make', '-p', '/dev/null'], |
| 392 |
stdout=subprocess.PIPE) |
| 393 |
for l in make.stdout: |
| 394 |
l = l.decode('UTF-8') |
| 395 |
m = var_re.match(l) |
| 396 |
if m: |
| 397 |
make_vars[m.group(1)] = m.group(2) |
| 398 |
make.wait() |
| 399 |
|
| 400 |
# expand make variables |
| 401 |
subst_re = re.compile(r'${([a-zA-Z_]+)}') |
| 402 |
for (k, v) in make_vars.items(): |
| 403 |
while True: |
| 404 |
m = subst_re.search(v) |
| 405 |
if m: |
| 406 |
v = subst_re.sub(make_vars.get(m.group(1), ''), v) |
| 407 |
make_vars[k] = v |
| 408 |
else: |
| 409 |
break |
| 410 |
|
| 411 |
# create localstatedirs |
| 412 |
for d in (os.path.join(make_vars['localstatedir'], 'run', 'udisks2'), |
| 413 |
os.path.join(make_vars['localstatedir'], 'lib', 'udisks2')): |
| 414 |
os.makedirs(d, exist_ok=True) |
| 415 |
|
| 416 |
@classmethod |
| 417 |
def setup_vdev(cls): |
| 418 |
"""create virtual test devices |
| 419 |
|
| 420 |
It is zeroed out initially. |
| 421 |
|
| 422 |
Return a pair (writable HD device path, readonly CD device path). |
| 423 |
""" |
| 424 |
# ensure that the scsi_debug module is loaded |
| 425 |
if os.path.isdir('/sys/module/scsi_debug'): |
| 426 |
sys.stderr.write('The scsi_debug module is already loaded; please ' |
| 427 |
'remove before running this test.\n') |
| 428 |
sys.exit(1) |
| 429 |
|
| 430 |
# work around scsi_debug not implementing CD-ROM SCSI commands, so that |
| 431 |
# udev's cdrom_id does not recognize tracks |
| 432 |
scsi_debug_rules = '/run/udev/rules.d/60-persistent-storage-scsi_debug.rules' |
| 433 |
if os.path.isdir('/run/udev') and not os.path.exists(scsi_debug_rules): |
| 434 |
if not os.path.exists('/run/udev/rules.d'): |
| 435 |
os.makedirs('/run/udev/rules.d') |
| 436 |
with open(scsi_debug_rules, 'w') as f: |
| 437 |
f.write('KERNEL=="sr*", ENV{DISK_EJECT_REQUEST}!="?*", ' |
| 438 |
'ATTRS{model}=="scsi_debug*", ' |
| 439 |
'ENV{ID_CDROM_MEDIA}=="?*", ' |
| 440 |
'IMPORT{program}="/sbin/blkid -o udev -p -u noraid $tempnode"\n') |
| 441 |
# reload udev |
| 442 |
subprocess.call('sync; pkill --signal HUP udevd || ' |
| 443 |
'pkill --signal HUP systemd-udevd', |
| 444 |
shell=True) |
| 445 |
|
| 446 |
# create a fake SCSI hard drive |
| 447 |
assert subprocess.call(['modprobe', 'scsi_debug', 'dev_size_mb=%i' % ( |
| 448 |
VDEV_SIZE / 1048576)]) == 0, 'Failure to modprobe scsi_debug' |
| 449 |
|
| 450 |
# wait until drive got created |
| 451 |
rw_dirs = [] |
| 452 |
while len(rw_dirs) < 1: |
| 453 |
rw_dirs = glob('/sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block') |
| 454 |
time.sleep(0.1) |
| 455 |
assert len(rw_dirs) == 1 |
| 456 |
|
| 457 |
# create a fake CD-ROM, too |
| 458 |
with open('/sys/bus/pseudo/drivers/scsi_debug/ptype', 'w') as f: |
| 459 |
f.write('5') # henceforth, created devices will be CD drives |
| 460 |
with open('/sys/bus/pseudo/drivers/scsi_debug/add_host', 'w') as f: |
| 461 |
f.write('1') # generate a new drive |
| 462 |
subprocess.call(['udevadm', 'settle']) |
| 463 |
|
| 464 |
ro_dirs = [] |
| 465 |
while len(ro_dirs) < 2: |
| 466 |
ro_dirs = glob('/sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block') |
| 467 |
time.sleep(0.1) |
| 468 |
ro_dirs.remove(rw_dirs[0]) |
| 469 |
assert len(ro_dirs) == 1 |
| 470 |
|
| 471 |
# determine the debug block devices |
| 472 |
devs = os.listdir(ro_dirs[0]) |
| 473 |
assert len(devs) == 1 |
| 474 |
ro_dev = '/dev/' + devs[0] |
| 475 |
devs = os.listdir(rw_dirs[0]) |
| 476 |
assert len(devs) == 1 |
| 477 |
rw_dev = '/dev/' + devs[0] |
| 478 |
assert os.path.exists(ro_dev) |
| 479 |
assert os.path.exists(rw_dev) |
| 480 |
|
| 481 |
# let's be 100% sure that we pick a virtual one |
| 482 |
assert open('/sys/block/%s/device/model' % |
| 483 |
os.path.basename(rw_dev)).read().strip() == 'scsi_debug' |
| 484 |
|
| 485 |
with open('/sys/bus/pseudo/drivers/scsi_debug/ptype', 'w') as f: |
| 486 |
f.write('0') |
| 487 |
|
| 488 |
print('Set up test device: r/w: %s, r/o: %s' % (rw_dev, ro_dev)) |
| 489 |
return (rw_dev, ro_dev) |
| 490 |
|
| 491 |
@classmethod |
| 492 |
def teardown_cd_vdev(cls, cd_device): |
| 493 |
"""Remove pktcdvd device attached to the specified scsi_debug cdrom device. |
| 494 |
|
| 495 |
Newer udftools ship an udev rule that calls pktsetup to create a /dev/pktcdvd/ |
| 496 |
device on every cd-rom device. So if there's no pktsetup tool in the system, |
| 497 |
the attached device wouldn't most likely exist either. """ |
| 498 |
if os.path.exists(cd_device) and shutil.which("pktsetup"): |
| 499 |
try: |
| 500 |
rdev = os.stat(cd_device).st_rdev |
| 501 |
subprocess.call(['pktsetup', '-d', '%d:%d' % (os.major(rdev), os.minor(rdev))]) |
| 502 |
subprocess.call(['rmmod', 'pktcdvd']) |
| 503 |
except Exception as e: |
| 504 |
print("Ignoring exception raised during pktcdvd device removal: %s" % e) |
| 505 |
|
| 506 |
@classmethod |
| 507 |
def teardown_vdev(cls, device): |
| 508 |
"""release and remove virtual test device""" |
| 509 |
|
| 510 |
cls.remove_device(device) |
| 511 |
assert subprocess.call(['rmmod', 'scsi_debug']) == 0, 'Failure to rmmod scsi_debug' |
| 512 |
|
| 513 |
@classmethod |
| 514 |
def remove_device(cls, device): |
| 515 |
"""remove virtual test device""" |
| 516 |
|
| 517 |
device = device.split('/')[-1] |
| 518 |
if os.path.exists('/sys/block/' + device): |
| 519 |
f = open('/sys/block/%s/device/delete' % device, 'w') |
| 520 |
f.write('1') |
| 521 |
f.close() |
| 522 |
while os.path.exists(device): |
| 523 |
time.sleep(0.1) |
| 524 |
cls.sync() |
| 525 |
time.sleep(0.5) # TODO |
| 526 |
|
| 527 |
@classmethod |
| 528 |
def readd_devices(cls): |
| 529 |
"""re-add virtual test devices after removal""" |
| 530 |
|
| 531 |
scan_files = glob('/sys/bus/pseudo/devices/adapter*/host*/scsi_host/host*/scan') |
| 532 |
assert len(scan_files) > 0 |
| 533 |
for f in scan_files: |
| 534 |
open(f, 'w').write('- - -\n') |
| 535 |
timeout = 100 |
| 536 |
while not os.path.exists(cls.device) and timeout > 0: |
| 537 |
time.sleep(0.1) |
| 538 |
timeout -= 1 |
| 539 |
assert os.path.exists(cls.device), 'timed out waiting for %s to exist' % cls.device |
| 540 |
time.sleep(0.5) |
| 541 |
cls.sync() |
| 542 |
|
| 543 |
def _check_with_retry(self, fn, value, cmp_fn): |
| 544 |
retries = 10 |
| 545 |
while retries > 0: |
| 546 |
if cmp_fn(fn(), value): |
| 547 |
return True |
| 548 |
retries -= 1 |
| 549 |
time.sleep(0.5) |
| 550 |
self.sync() |
| 551 |
return False |
| 552 |
|
| 553 |
def assertEventually(self, fn, value): |
| 554 |
"""Check that an function is eventually equal to value. |
| 555 |
|
| 556 |
This is mostly meant for checking object properties, as these are |
| 557 |
updated asynchronously. This retries up to 10 times. |
| 558 |
""" |
| 559 |
self._check_with_retry(fn, value, lambda x, y: x == y) |
| 560 |
|
| 561 |
if isinstance(value, set): |
| 562 |
self.assertEqual(set(fn()), value) |
| 563 |
else: |
| 564 |
self.assertEqual(fn(), value) |
| 565 |
|
| 566 |
def assertProperty(self, obj, name, value): |
| 567 |
"""Check that an object's property is eventually equal to value""" |
| 568 |
|
| 569 |
self.assertEventually(lambda: obj.get_property(name), value) |
| 570 |
|
| 571 |
def assertNotProperty(self, obj, name, value): |
| 572 |
"""Check that an object's property is eventually not equal to value""" |
| 573 |
|
| 574 |
if not self._check_with_retry(lambda: obj.get_property(name), value, lambda x, y: x != y): |
| 575 |
# we know it is equal, the assert here is just to raise the AssertionError |
| 576 |
self.assertNotEqual(obj.get_property(name), value) |
| 577 |
|
| 578 |
@classmethod |
| 579 |
def write_stderr(cls, msg): |
| 580 |
"""Write to stderr without buffering""" |
| 581 |
sys.stderr.write(msg) |
| 582 |
sys.stderr.flush() |
| 583 |
|
| 584 |
|
| 585 |
# ---------------------------------------------------------------------------- |
| 586 |
|
| 587 |
class Manager(UDisksTestCase): |
| 588 |
"""UDisksManager operations""" |
| 589 |
|
| 590 |
def test_version(self): |
| 591 |
"""daemon version""" |
| 592 |
|
| 593 |
self.assertTrue(self.manager.get_property('version')[0].isdigit()) |
| 594 |
|
| 595 |
def test_loop_rw(self): |
| 596 |
"""loop device R/W""" |
| 597 |
|
| 598 |
with tempfile.NamedTemporaryFile() as f: |
| 599 |
f.truncate(100000000) |
| 600 |
fd_list = Gio.UnixFDList.new_from_array([f.fileno()]) |
| 601 |
|
| 602 |
(path, out_fd_list) = self.manager.call_loop_setup_sync( |
| 603 |
GLib.Variant('h', 0), # fd index |
| 604 |
no_options, |
| 605 |
fd_list, |
| 606 |
None) |
| 607 |
self.client.settle() |
| 608 |
|
| 609 |
obj = self.client.get_object(path) |
| 610 |
loop = obj.get_property('loop') |
| 611 |
block = obj.get_property('block') |
| 612 |
self.assertNotEqual(block, None) |
| 613 |
self.assertNotEqual(loop, None) |
| 614 |
self.assertEqual(obj.get_property('filesystem'), None) |
| 615 |
|
| 616 |
try: |
| 617 |
self.assertEqual(loop.get_property('backing-file'), f.name) |
| 618 |
|
| 619 |
options = GLib.Variant('a{sv}', {'label': GLib.Variant('s', 'foo')}) |
| 620 |
block.call_format_sync('ext2', options, None) |
| 621 |
self.client.settle() |
| 622 |
self.assertNotEqual(obj.get_property('filesystem'), None) |
| 623 |
|
| 624 |
self.assertEqual(block.get_property('id-label'), 'foo') |
| 625 |
self.assertEqual(block.get_property('id-usage'), 'filesystem') |
| 626 |
self.assertEqual(block.get_property('id-type'), 'ext2') |
| 627 |
finally: |
| 628 |
loop.call_delete_sync(no_options, None) |
| 629 |
|
| 630 |
def test_loop_ro(self): |
| 631 |
"""loop device R/O""" |
| 632 |
|
| 633 |
with tempfile.NamedTemporaryFile() as f: |
| 634 |
f.truncate(100000000) |
| 635 |
fd_list = Gio.UnixFDList.new_from_array([f.fileno()]) |
| 636 |
|
| 637 |
(path, out_fd_list) = self.manager.call_loop_setup_sync( |
| 638 |
GLib.Variant('h', 0), # fd index |
| 639 |
GLib.Variant('a{sv}', {'read-only': GLib.Variant('b', True)}), |
| 640 |
fd_list, |
| 641 |
None) |
| 642 |
self.client.settle() |
| 643 |
|
| 644 |
obj = self.client.get_object(path) |
| 645 |
loop = obj.get_property('loop') |
| 646 |
block = obj.get_property('block') |
| 647 |
self.assertNotEqual(block, None) |
| 648 |
self.assertNotEqual(loop, None) |
| 649 |
self.assertEqual(obj.get_property('filesystem'), None) |
| 650 |
|
| 651 |
try: |
| 652 |
self.assertEqual(loop.get_property('backing-file'), f.name) |
| 653 |
|
| 654 |
# can't format due to permission error |
| 655 |
self.assertRaises(GLib.GError, block.call_format_sync, 'ext2', no_options, None) |
| 656 |
|
| 657 |
self.assertProperty(block, 'id-label', '') |
| 658 |
self.assertProperty(block, 'id-usage', '') |
| 659 |
self.assertProperty(block, 'id-type', '') |
| 660 |
finally: |
| 661 |
self.client.settle() |
| 662 |
loop.call_delete_sync(no_options, None) |
| 663 |
|
| 664 |
|
| 665 |
# ---------------------------------------------------------------------------- |
| 666 |
|
| 667 |
class Drive(UDisksTestCase): |
| 668 |
"""UDisksDrive""" |
| 669 |
|
| 670 |
def setUp(self): |
| 671 |
self.drive = self.client.get_drive_for_block(self.udisks_block()) |
| 672 |
self.assertNotEqual(self.drive, None) |
| 673 |
|
| 674 |
def test_properties(self): |
| 675 |
"""properties of UDisksDrive object""" |
| 676 |
|
| 677 |
self.assertEqual(self.drive.get_property('model'), 'scsi_debug') |
| 678 |
self.assertEqual(self.drive.get_property('vendor'), 'Linux') |
| 679 |
self.assertAlmostEqual(self.drive.get_property('size') / 1.e6, VDEV_SIZE / 1.e6, 0) |
| 680 |
self.assertEqual(self.drive.get_property('media-available'), True) |
| 681 |
self.assertEqual(self.drive.get_property('optical'), False) |
| 682 |
|
| 683 |
self.assertNotEqual(len(self.drive.get_property('serial')), 0) |
| 684 |
self.assertNotEqual(len(self.drive.get_property('revision')), 0) |
| 685 |
|
| 686 |
|
| 687 |
# ---------------------------------------------------------------------------- |
| 688 |
|
| 689 |
class FS(UDisksTestCase): |
| 690 |
"""Test detection of all supported file systems""" |
| 691 |
|
| 692 |
def setUp(self): |
| 693 |
self.workdir = tempfile.mkdtemp() |
| 694 |
self.block = self.udisks_block() |
| 695 |
self.assertNotEqual(self.block, None) |
| 696 |
|
| 697 |
def tearDown(self): |
| 698 |
if subprocess.call(['umount', self.device], stderr=subprocess.PIPE) == 0: |
| 699 |
self.write_stderr('[cleanup unmount] ') |
| 700 |
shutil.rmtree(self.workdir) |
| 701 |
|
| 702 |
def test_zero(self): |
| 703 |
"""properties of zeroed out device""" |
| 704 |
|
| 705 |
self.zero_device() |
| 706 |
self.assertProperty(self.block, 'device', self.device) |
| 707 |
self.assertIn('Linux_scsi_debug', self.block.get_property('drive')) |
| 708 |
self.assertProperty(self.block, 'id-label', '') |
| 709 |
self.assertEqual(self.block.get_property('hint-system'), True) |
| 710 |
self.assertEqual(self.block.get_property('id-usage'), '') |
| 711 |
self.assertEqual(self.block.get_property('id-type'), '') |
| 712 |
self.assertEqual(self.block.get_property('id-uuid'), '') |
| 713 |
self.assertAlmostEqual(self.block.get_property('size') / 1.e6, VDEV_SIZE / 1.e6, 0) |
| 714 |
obj = self.client.get_object(self.block.get_object_path()) |
| 715 |
self.assertEqual(obj.get_property('filesystem'), None) |
| 716 |
self.assertEqual(obj.get_property('partition'), None) |
| 717 |
self.assertEqual(obj.get_property('partition-table'), None) |
| 718 |
|
| 719 |
def test_exfat(self): |
| 720 |
"""fs: exFAT""" |
| 721 |
self._do_fs_check('exfat') |
| 722 |
|
| 723 |
def test_ext4(self): |
| 724 |
"""fs: ext4""" |
| 725 |
self._do_fs_check('ext4') |
| 726 |
|
| 727 |
def test_empty(self): |
| 728 |
"""fs: empty""" |
| 729 |
|
| 730 |
self.mkfs('ext4', 'foo') |
| 731 |
block = self.udisks_block() |
| 732 |
self.assertProperty(block, 'id-usage', 'filesystem') |
| 733 |
self.assertProperty(block, 'id-type', 'ext4') |
| 734 |
self.assertProperty(block, 'id-label', 'foo') |
| 735 |
self.assertNotEqual(self.udisks_filesystem(), None) |
| 736 |
|
| 737 |
self.fs_create(None, 'empty', no_options) |
| 738 |
|
| 739 |
self.assertProperty(block, 'id-usage', '') |
| 740 |
self.assertProperty(block, 'id-type', '') |
| 741 |
self.assertProperty(block, 'id-label', '') |
| 742 |
self.assertEqual(self.udisks_filesystem(), None) |
| 743 |
|
| 744 |
def test_create_fs_unknown_type(self): |
| 745 |
"""Format() with unknown type""" |
| 746 |
|
| 747 |
try: |
| 748 |
self.fs_create(None, 'bogus', no_options) |
| 749 |
self.fail('Expected failure for bogus file system') |
| 750 |
except GLib.GError as e: |
| 751 |
self.assertIn('UDisks2.Error.NotSupported', e.message) |
| 752 |
self.assertIn('\'bogus\' is not supported', e.message) |
| 753 |
|
| 754 |
def test_create_fs_unsupported_label(self): |
| 755 |
"""Format() with unsupported label""" |
| 756 |
|
| 757 |
options = GLib.Variant('a{sv}', {'label': GLib.Variant('s', 'foo')}) |
| 758 |
try: |
| 759 |
self.fs_create(None, 'minix', options) |
| 760 |
self.fail('Expected failure for unsupported label') |
| 761 |
except GLib.GError as e: |
| 762 |
self.assertIn('UDisks2.Error.NotSupported', e.message) |
| 763 |
|
| 764 |
def test_force_removal(self): |
| 765 |
"""fs: forced removal""" |
| 766 |
|
| 767 |
# create a fs and mount it |
| 768 |
self.mkfs('ext4', 'udiskstest') |
| 769 |
fs = self.udisks_filesystem() |
| 770 |
mount_path = fs.call_mount_sync(no_options, None) |
| 771 |
self.assertTrue(mount_path.endswith('udiskstest')) |
| 772 |
self.assertIn('/media/', mount_path) |
| 773 |
self.assertTrue(self.is_mountpoint(mount_path)) |
| 774 |
|
| 775 |
dev_t = os.stat(self.devname()).st_rdev |
| 776 |
|
| 777 |
# removal should clean up mounts |
| 778 |
self.remove_device(self.device) |
| 779 |
self.assertFalse(os.path.exists(mount_path)) |
| 780 |
self.assertEqual(self.client.get_block_for_dev(dev_t), None) |
| 781 |
|
| 782 |
# after putting it back, it should be mountable again |
| 783 |
self.readd_devices() |
| 784 |
fs = self.udisks_filesystem() |
| 785 |
self.assertProperty(fs, 'mount-points', []) |
| 786 |
|
| 787 |
mount_path = fs.call_mount_sync(no_options, None) |
| 788 |
self.assertTrue(mount_path.endswith('udiskstest')) |
| 789 |
self.assertIn('/media/', mount_path) |
| 790 |
self.assertTrue(self.is_mountpoint(mount_path)) |
| 791 |
self.assertProperty(fs, 'mount-points', [mount_path]) |
| 792 |
|
| 793 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 794 |
self.assertProperty(fs, 'mount-points', []) |
| 795 |
|
| 796 |
def test_existing_manual_mount_point(self): |
| 797 |
"""fs: does not reuse existing manual mount point""" |
| 798 |
|
| 799 |
self.mkfs('ext4', 'udiskstest') |
| 800 |
fs = self.udisks_filesystem() |
| 801 |
|
| 802 |
# mount it, determine mount path, and unmount again |
| 803 |
mount_path = fs.call_mount_sync(no_options, None) |
| 804 |
self.assertTrue(mount_path.endswith('udiskstest')) |
| 805 |
|
| 806 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 807 |
self.assertProperty(fs, 'mount-points', []) |
| 808 |
|
| 809 |
# the mountpoint should be gone once the unmount call returns |
| 810 |
self.assertFalse(os.path.exists(mount_path)) |
| 811 |
|
| 812 |
# now manually create the mount point again |
| 813 |
os.mkdir(mount_path) |
| 814 |
|
| 815 |
# settle down and check the mountpoint still exists |
| 816 |
time.sleep(5) |
| 817 |
self.assertTrue(os.path.exists(mount_path)) |
| 818 |
|
| 819 |
# now this should use mount_path + '1' |
| 820 |
try: |
| 821 |
new_mount_path = fs.call_mount_sync(no_options, None) |
| 822 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 823 |
self.assertProperty(fs, 'mount-points', []) |
| 824 |
self.assertEqual(new_mount_path, mount_path + '1') |
| 825 |
finally: |
| 826 |
os.rmdir(mount_path) |
| 827 |
|
| 828 |
def test_existing_udisks_mount_point(self): |
| 829 |
"""fs: reuses existing udisks mount point""" |
| 830 |
|
| 831 |
if self.system: |
| 832 |
self.skipTest("Skipping when testing a system instance") |
| 833 |
|
| 834 |
self.mkfs('ext4', 'udiskstest') |
| 835 |
fs = self.udisks_filesystem() |
| 836 |
|
| 837 |
# mount it, determine mount path |
| 838 |
mount_path = fs.call_mount_sync(no_options, None) |
| 839 |
self.assertTrue(mount_path.endswith('udiskstest')) |
| 840 |
|
| 841 |
# stop the daemon (happens during a package upgrade) |
| 842 |
UDisksTestCase.stop_daemon() |
| 843 |
|
| 844 |
# mount should still be there; unmount it manually |
| 845 |
self.assertTrue(self.is_mountpoint(mount_path)) |
| 846 |
subprocess.check_call(['umount', mount_path]) |
| 847 |
|
| 848 |
# restart daemon, mount again; this should use the same mount point as |
| 849 |
# before |
| 850 |
UDisksTestCase.start_daemon(self.system) |
| 851 |
fs = self.udisks_filesystem() |
| 852 |
new_mount_path = fs.call_mount_sync(no_options, None) |
| 853 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 854 |
self.assertProperty(fs, 'mount-points', []) |
| 855 |
self.assertEqual(new_mount_path, mount_path) |
| 856 |
|
| 857 |
def _do_fs_check(self, fs_type): |
| 858 |
"""Run checks for a particular file system.""" |
| 859 |
if fs_type == 'ntfs': |
| 860 |
mkfs = 'mkntfs' |
| 861 |
else: |
| 862 |
mkfs = 'mkfs.' + fs_type |
| 863 |
|
| 864 |
if fs_type != 'swap' and subprocess.call(['which', mkfs], |
| 865 |
stdout=subprocess.PIPE) != 0: |
| 866 |
self.write_stderr('[no %s, skip] ' % mkfs) |
| 867 |
|
| 868 |
# check correct D-Bus exception |
| 869 |
try: |
| 870 |
self.fs_create(None, fs_type, no_options) |
| 871 |
self.fail('Expected failure for missing mkfs.' + fs_type) |
| 872 |
except GLib.GError as e: |
| 873 |
self.assertIn('UDisks2.Error.NotSupported', e.message) |
| 874 |
self.assertIn('executable %s not found' % mkfs, e.message) |
| 875 |
return |
| 876 |
|
| 877 |
# do checks with command line tools (mkfs/mount/umount) |
| 878 |
self.write_stderr('[cli] ') |
| 879 |
|
| 880 |
self._do_cli_check(fs_type) |
| 881 |
if fs_type != 'minix': |
| 882 |
self._do_cli_check(fs_type, 't%stst' % fs_type) |
| 883 |
|
| 884 |
# put a different fs here instead of zeroing, so that we verify that |
| 885 |
# udisks overrides existing FS (e. g. XFS complains then), and does not |
| 886 |
# leave traces of other FS around |
| 887 |
if fs_type == 'ext3': |
| 888 |
self.mkfs('swap') |
| 889 |
else: |
| 890 |
self.mkfs('ext3') |
| 891 |
|
| 892 |
# do checks with udisks operations |
| 893 |
self.write_stderr('[ud] ') |
| 894 |
self._do_udisks_check(fs_type) |
| 895 |
if fs_type != 'minix': |
| 896 |
self._do_udisks_check(fs_type, 't%stst' % fs_type) |
| 897 |
# also test fs_create with an empty label |
| 898 |
self._do_udisks_check(fs_type, '') |
| 899 |
# also test fs_create with the no-discard option |
| 900 |
self._do_udisks_check(fs_type, '', True) |
| 901 |
|
| 902 |
def _do_cli_check(self, fs_type, label=None): |
| 903 |
"""udisks correctly picks up file system changes from command line tools""" |
| 904 |
|
| 905 |
self.mkfs(fs_type, label) |
| 906 |
|
| 907 |
block = self.udisks_block() |
| 908 |
|
| 909 |
self.assertProperty(block, 'id-usage', (fs_type == 'swap') and 'other' or 'filesystem') |
| 910 |
self.assertProperty(block, 'id-type', fs_type) |
| 911 |
l = block.get_property('id-label') |
| 912 |
if fs_type == 'vfat': |
| 913 |
l = l.lower() # VFAT is case insensitive |
| 914 |
self.assertEqual(l, label or '') |
| 915 |
self.assertEqual(block.get_property('hint-name'), '') |
| 916 |
if fs_type != 'minix': |
| 917 |
self.assertEqual(block.get_property('id-uuid'), self.blkid()['ID_FS_UUID']) |
| 918 |
|
| 919 |
obj = self.client.get_object(self.block.get_object_path()) |
| 920 |
self.assertEqual(obj.get_property('partition'), None) |
| 921 |
self.assertEqual(obj.get_property('partition-table'), None) |
| 922 |
|
| 923 |
fs = obj.get_property('filesystem') |
| 924 |
if fs_type == 'swap': |
| 925 |
self.assertEqual(fs, None) |
| 926 |
else: |
| 927 |
self.assertNotEqual(fs, None) |
| 928 |
|
| 929 |
if fs_type == 'swap': |
| 930 |
return |
| 931 |
|
| 932 |
# mount it on two points |
| 933 |
if fs_type == 'ntfs' and subprocess.call(['which', 'mount.ntfs-3g'], |
| 934 |
stdout=subprocess.PIPE) == 0: |
| 935 |
# prefer mount.ntfs-3g if we have it (on Debian; Ubuntu |
| 936 |
# defaults to ntfs-3g if installed); TODO: check other distros |
| 937 |
mount_prog = 'mount.ntfs-3g' |
| 938 |
else: |
| 939 |
mount_prog = 'mount' |
| 940 |
mount_a = os.path.join(self.workdir, 'mp_a') |
| 941 |
mount_b = os.path.join(self.workdir, 'mp_b') |
| 942 |
if not os.path.exists(mount_a): |
| 943 |
os.mkdir(mount_a) |
| 944 |
if not os.path.exists(mount_b): |
| 945 |
os.mkdir(mount_b) |
| 946 |
|
| 947 |
ret = subprocess.call([mount_prog, self.device, mount_a]) |
| 948 |
if ret == 32: |
| 949 |
# missing fs driver |
| 950 |
self.write_stderr('[missing kernel driver, skip] ') |
| 951 |
return |
| 952 |
self.assertEqual(ret, 0) |
| 953 |
|
| 954 |
self.assertProperty(fs, 'mount-points', [mount_a]) |
| 955 |
|
| 956 |
if fs_type not in ('ntfs', 'exfat'): |
| 957 |
# ntfs-3g and exfat don't support multiple mounts |
| 958 |
subprocess.check_call([mount_prog, self.device, mount_b]) |
| 959 |
self.assertProperty(fs, 'mount-points', {mount_a, mount_b}) |
| 960 |
subprocess.call(['umount', mount_b]) |
| 961 |
|
| 962 |
# unmount it |
| 963 |
subprocess.call(['umount', mount_a]) |
| 964 |
self.assertProperty(fs, 'mount-points', []) |
| 965 |
|
| 966 |
def _do_udisks_check(self, fs_type, label=None, no_discard=None): |
| 967 |
"""udisks API correctly changes file system""" |
| 968 |
|
| 969 |
|
| 970 |
# create fs |
| 971 |
options = { } |
| 972 |
if label is not None: |
| 973 |
options['label'] = GLib.Variant('s', label); |
| 974 |
if no_discard is not None: |
| 975 |
options['no-discard'] = GLib.Variant('b', no_discard); |
| 976 |
self.fs_create(None, fs_type, GLib.Variant('a{sv}', options)) |
| 977 |
|
| 978 |
# properties |
| 979 |
b_id = self.blkid() |
| 980 |
self.assertEqual(b_id['ID_FS_USAGE'], fs_type == 'swap' and 'other' or 'filesystem') |
| 981 |
self.assertEqual(b_id['ID_FS_TYPE'], fs_type) |
| 982 |
l = b_id.get('ID_FS_LABEL', '') |
| 983 |
if fs_type == 'vfat': |
| 984 |
l = l.lower() # VFAT is case insensitive |
| 985 |
self.assertEqual(l, label or '') |
| 986 |
|
| 987 |
block = self.udisks_block() |
| 988 |
self.assertProperty(block, 'id-usage', (fs_type == 'swap') and 'other' or 'filesystem') |
| 989 |
self.assertProperty(block, 'id-type', fs_type) |
| 990 |
if fs_type == 'vfat' and label: |
| 991 |
# VFAT is case insensitive |
| 992 |
self.assertEventually(lambda: block.get_property('id-label').lower(), label.lower()) |
| 993 |
else: |
| 994 |
self.assertProperty(block, 'id-label', label or '') |
| 995 |
|
| 996 |
if fs_type == 'swap': |
| 997 |
return |
| 998 |
|
| 999 |
obj = self.client.get_object(self.block.get_object_path()) |
| 1000 |
self.assertEqual(obj.get_property('partition'), None) |
| 1001 |
self.assertEqual(obj.get_property('partition-table'), None) |
| 1002 |
|
| 1003 |
fs = self.udisks_filesystem() |
| 1004 |
self.assertNotEqual(fs, None, 'no Filesystem interface for test device') |
| 1005 |
self.assertEqual(fs.get_property('mount-points'), []) |
| 1006 |
|
| 1007 |
# mount |
| 1008 |
mount_path = fs.call_mount_sync(no_options, None) |
| 1009 |
|
| 1010 |
self.assertIn('/media/', mount_path) |
| 1011 |
if label: |
| 1012 |
if fs_type == 'vfat': |
| 1013 |
self.assertTrue(mount_path.lower().endswith(label)) |
| 1014 |
else: |
| 1015 |
self.assertTrue(mount_path.endswith(label)) |
| 1016 |
|
| 1017 |
self.assertTrue(self.is_mountpoint(mount_path)) |
| 1018 |
# FIXME: this should work on the existing fs object, but doesn't! |
| 1019 |
self.sync() |
| 1020 |
fs = self.udisks_filesystem() |
| 1021 |
self.assertProperty(fs, 'mount-points', [mount_path]) |
| 1022 |
|
| 1023 |
# no ownership taken, should be root owned |
| 1024 |
st = os.stat(mount_path) |
| 1025 |
self.assertEqual((st.st_uid, st.st_gid), (0, 0)) |
| 1026 |
|
| 1027 |
self._do_file_perms_checks(fs_type, mount_path) |
| 1028 |
|
| 1029 |
# unmount |
| 1030 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 1031 |
self.assertFalse(os.path.exists(mount_path), 'mount point was not removed') |
| 1032 |
self.assertProperty(fs, 'mount-points', []) |
| 1033 |
|
| 1034 |
# create fs with taking ownership (daemon:mail == 1:8) |
| 1035 |
# if supports_unix_owners: |
| 1036 |
# options.append('take_ownership_uid=1') |
| 1037 |
# options.append('take_ownership_gid=8') |
| 1038 |
# self.fs_create(None, type, options) |
| 1039 |
# mount_path = iface.FilesystemMount('', []) |
| 1040 |
# st = os.stat(mount_path) |
| 1041 |
# self.assertEqual((st.st_uid, st.st_gid), (1, 8)) |
| 1042 |
# self.retry_busy(self.partition_iface().FilesystemUnmount, []) |
| 1043 |
# self.assertFalse(os.path.exists(mount_path), 'mount point was not removed') |
| 1044 |
|
| 1045 |
# change label |
| 1046 |
supported = True |
| 1047 |
l = r'n"a$m\"e' + fs_type |
| 1048 |
if fs_type == 'vfat': |
| 1049 |
# VFAT does not support some characters |
| 1050 |
# Also, the maximum length of the label is 11 characters (22 bytes) |
| 1051 |
self.assertRaises(GLib.GError, fs.call_set_label_sync, l, no_options, None) |
| 1052 |
l = r'n@a$me' |
| 1053 |
elif fs_type == 'exfat': |
| 1054 |
# EXFAT does not support some characters, but exfatprogs < 1.2.5 will still allow them. |
| 1055 |
# We cannot assert that the call would throw without knowing the exfatprogs version. |
| 1056 |
# Also, the maximum length of the label is 11 characters (22 bytes) |
| 1057 |
l = r'n@a$me' |
| 1058 |
try: |
| 1059 |
fs.call_set_label_sync(l, no_options, None) |
| 1060 |
except GLib.GError as e: |
| 1061 |
if 'UDisks2.Error.NotSupported' in e.message: |
| 1062 |
# these fses are known to not support relabeling |
| 1063 |
self.assertIn(fs_type, ['minix', 'f2fs']) |
| 1064 |
supported = False |
| 1065 |
else: |
| 1066 |
raise |
| 1067 |
|
| 1068 |
if supported: |
| 1069 |
block = self.udisks_block() |
| 1070 |
blkid_label = self.blkid().get('ID_FS_LABEL_ENC', '').replace('\\x22', '"').replace( |
| 1071 |
'\\x5c', '\\').replace('\\x24', '$') |
| 1072 |
if fs_type == 'vfat': |
| 1073 |
# EXFAIL: often (but not always) the label appears in all upper case |
| 1074 |
self.assertEqual(blkid_label.upper(), l.upper()) |
| 1075 |
self.assertEventually(lambda: block.get_property('id-label').upper(), l.upper()) |
| 1076 |
else: |
| 1077 |
self.assertEqual(blkid_label, l) |
| 1078 |
self.assertProperty(block, 'id-label', l) |
| 1079 |
|
| 1080 |
# test setting empty label |
| 1081 |
fs.call_set_label_sync('', no_options, None) |
| 1082 |
self.assertEqual(self.blkid().get('ID_FS_LABEL_ENC', ''), '') |
| 1083 |
self.assertProperty(block, 'id-label', '') |
| 1084 |
|
| 1085 |
# check fs - Not implemented in udisks yet |
| 1086 |
# self.assertEqual(iface.FilesystemCheck([]), True) |
| 1087 |
|
| 1088 |
# check mounting of a read-only device |
| 1089 |
# this is known-broken for reiserfs and xfs right now: |
| 1090 |
# https://github.com/karelzak/util-linux/issues/17 |
| 1091 |
# https://github.com/karelzak/util-linux/issues/18 |
| 1092 |
# exfat can be mounted with '-o rw' even if read-only |
| 1093 |
if fs_type not in ['reiserfs', 'exfat']: |
| 1094 |
# the scsi_debug CD drive content is the same as for the HD drive, but |
| 1095 |
# udev does not know about this; so give it a nudge to re-probe |
| 1096 |
subprocess.call(['partprobe', self.cd_device]) |
| 1097 |
self.sync() |
| 1098 |
time.sleep(2) |
| 1099 |
# for some reason udev still wouldn't notice the filesystem, poke it manually |
| 1100 |
subprocess.call(['udevadm', 'trigger', '--action=change', |
| 1101 |
'--sysname-match=' + os.path.basename(self.cd_device)]) |
| 1102 |
self.sync() |
| 1103 |
time.sleep(5) |
| 1104 |
self.sync() |
| 1105 |
cd_fs = self.udisks_filesystem(cd=True) |
| 1106 |
subprocess.call(['blockdev', '--setro', self.cd_device]) |
| 1107 |
|
| 1108 |
# forcing mount CD drive as 'rw' should fail |
| 1109 |
try: |
| 1110 |
ro_options = GLib.Variant('a{sv}', {'options': GLib.Variant('s', 'rw')}) |
| 1111 |
cd_fs.call_mount_sync(ro_options, None) |
| 1112 |
except GLib.GError as e: |
| 1113 |
msg = r"(is write-protected but `rw' option given|" \ |
| 1114 |
"is write-protected but explicit read-write mode requested|" \ |
| 1115 |
"fsconfig.*failed: /dev/sr.*: Can't open blockdev)" |
| 1116 |
if not re.search(msg, str(e)): |
| 1117 |
self.fail('Mounting read-only device with \'rw\' option failed ' |
| 1118 |
'with an unexpected error.\nGot: %s\nExpected: \'%s\'' |
| 1119 |
% (str(e), msg)) |
| 1120 |
else: |
| 1121 |
self.retry_busy(cd_fs.call_unmount_sync, no_options, None) |
| 1122 |
self.fail('Mounting read-only device didn\'t fail with \'rw\' option.') |
| 1123 |
|
| 1124 |
mount_path = cd_fs.call_mount_sync(no_options, None) |
| 1125 |
try: |
| 1126 |
self.assertIn('/media/', mount_path) |
| 1127 |
self.assertProperty(cd_fs, 'mount-points', [mount_path]) |
| 1128 |
self.assertTrue(self.is_mountpoint(mount_path)) |
| 1129 |
|
| 1130 |
self.assertProperty(self.udisks_block(cd=True), 'read-only', True) |
| 1131 |
finally: |
| 1132 |
self.retry_busy(cd_fs.call_unmount_sync, no_options, None) |
| 1133 |
self.assertFalse(os.path.exists(mount_path), 'mount point was not removed') |
| 1134 |
self.assertProperty(cd_fs, 'mount-points', []) |
| 1135 |
|
| 1136 |
def _do_file_perms_checks(self, fs_type, mount_point): |
| 1137 |
"""Check for permissions for data files and executables. |
| 1138 |
|
| 1139 |
This particularly checks sane and useful permissions on non-Unix file |
| 1140 |
systems like vfat. |
| 1141 |
""" |
| 1142 |
if fs_type in BROKEN_PERMISSIONS_FS: |
| 1143 |
return |
| 1144 |
|
| 1145 |
f = os.path.join(mount_point, 'simpledata.txt') |
| 1146 |
open(f, 'w').close() |
| 1147 |
self.assertTrue(os.access(f, os.R_OK)) |
| 1148 |
self.assertTrue(os.access(f, os.W_OK)) |
| 1149 |
self.assertFalse(os.access(f, os.X_OK)) |
| 1150 |
|
| 1151 |
f = os.path.join(mount_point, 'simple.exe') |
| 1152 |
shutil.copy('/bin/bash', f) |
| 1153 |
self.assertTrue(os.access(f, os.R_OK)) |
| 1154 |
self.assertTrue(os.access(f, os.W_OK)) |
| 1155 |
self.assertTrue(os.access(f, os.X_OK)) |
| 1156 |
|
| 1157 |
os.mkdir(os.path.join(mount_point, 'subdir')) |
| 1158 |
f = os.path.join(mount_point, 'subdir', 'subdirdata.txt') |
| 1159 |
open(f, 'w').close() |
| 1160 |
self.assertTrue(os.access(f, os.R_OK)) |
| 1161 |
self.assertTrue(os.access(f, os.W_OK)) |
| 1162 |
self.assertFalse(os.access(f, os.X_OK)) |
| 1163 |
|
| 1164 |
f = os.path.join(mount_point, 'subdir', 'subdir.exe') |
| 1165 |
shutil.copy('/bin/bash', f) |
| 1166 |
self.assertTrue(os.access(f, os.R_OK)) |
| 1167 |
self.assertTrue(os.access(f, os.W_OK)) |
| 1168 |
self.assertTrue(os.access(f, os.X_OK)) |
| 1169 |
|
| 1170 |
|
| 1171 |
# ---------------------------------------------------------------------------- |
| 1172 |
|
| 1173 |
class Fstab(UDisksTestCase): |
| 1174 |
"""Test /etc/fstab custom options""" |
| 1175 |
block = None |
| 1176 |
fs = None |
| 1177 |
mount_point = "" |
| 1178 |
orig_fstab = "" |
| 1179 |
p1label = "" |
| 1180 |
p1uuid = "" |
| 1181 |
|
| 1182 |
@classmethod |
| 1183 |
def setUpClass(cls): |
| 1184 |
if os.path.isfile('/etc/fstab'): |
| 1185 |
cls.orig_fstab = '/etc/fstab.udiskstest' |
| 1186 |
shutil.copy2('/etc/fstab', cls.orig_fstab) |
| 1187 |
|
| 1188 |
cls.p1label = 'udtestp1' |
| 1189 |
|
| 1190 |
# create one partition |
| 1191 |
subprocess.check_call("echo 'label:gpt' | sfdisk %s" % cls.device, |
| 1192 |
stdout=subprocess.PIPE, shell=True) |
| 1193 |
subprocess.check_call("echo 'size=60M, type=L, name=%s' | sfdisk %s" % (cls.p1label, cls.device), |
| 1194 |
stdout=subprocess.PIPE, shell=True) |
| 1195 |
cls.sync() |
| 1196 |
blkid = subprocess.check_output( |
| 1197 |
['blkid', '-oudev', '-p', cls.devname(1)], universal_newlines=True).splitlines() |
| 1198 |
for line in blkid: |
| 1199 |
if line.startswith('ID_PART_ENTRY_UUID='): |
| 1200 |
cls.p1uuid = line.split('=', 1)[1] |
| 1201 |
break |
| 1202 |
else: |
| 1203 |
raise SystemError('blkid does not contain partition UUID') |
| 1204 |
|
| 1205 |
cls.mkfs('ext2', partition=1, label='udtestfst') |
| 1206 |
cls.mount_point = tempfile.mkdtemp() |
| 1207 |
cls.block = cls.udisks_block(partition=1) |
| 1208 |
cls.fs = cls.udisks_filesystem(partition=1) |
| 1209 |
|
| 1210 |
@classmethod |
| 1211 |
def tearDownClass(cls): |
| 1212 |
os.rmdir(cls.mount_point) |
| 1213 |
if cls.orig_fstab: |
| 1214 |
os.unlink(cls.orig_fstab) |
| 1215 |
|
| 1216 |
def tearDown(self): |
| 1217 |
if self.orig_fstab: |
| 1218 |
shutil.copy2(self.orig_fstab, '/etc/fstab') |
| 1219 |
else: |
| 1220 |
os.unlink('/etc/fstab') |
| 1221 |
self.retry_busy(self.fs.call_unmount_sync, no_options, None) |
| 1222 |
|
| 1223 |
def test_devname(self): |
| 1224 |
"""by device name""" |
| 1225 |
|
| 1226 |
with open('/etc/fstab', 'a') as f: |
| 1227 |
f.write('%s %s ext4 defaults,nosuid,noexec 0 0\n' % |
| 1228 |
(self.devname(partition=1), self.mount_point)) |
| 1229 |
os.system('sync') |
| 1230 |
self.do_test() |
| 1231 |
|
| 1232 |
def test_label(self): |
| 1233 |
"""by label""" |
| 1234 |
|
| 1235 |
with open('/etc/fstab', 'a') as f: |
| 1236 |
f.write('LABEL=udtestfst %s ext4 defaults,nosuid,noexec 0 0\n' % |
| 1237 |
self.mount_point) |
| 1238 |
os.system('sync') |
| 1239 |
self.do_test() |
| 1240 |
|
| 1241 |
def test_partuuid(self): |
| 1242 |
"""by PARTUUID""" |
| 1243 |
|
| 1244 |
with open('/etc/fstab', 'a') as f: |
| 1245 |
f.write('PARTUUID=%s %s ext4 defaults,nosuid,noexec 0 0\n' % |
| 1246 |
(self.p1uuid, self.mount_point)) |
| 1247 |
|
| 1248 |
os.system('sync') |
| 1249 |
self.do_test() |
| 1250 |
|
| 1251 |
def test_partlabel(self): |
| 1252 |
"""by PARTLABEL""" |
| 1253 |
|
| 1254 |
with open('/etc/fstab', 'a') as f: |
| 1255 |
f.write('PARTLABEL=%s %s ext4 defaults,nosuid,noexec 0 0\n' % |
| 1256 |
(self.p1label, self.mount_point)) |
| 1257 |
|
| 1258 |
os.system('sync') |
| 1259 |
self.do_test() |
| 1260 |
|
| 1261 |
def test_uuid(self): |
| 1262 |
"""by UUID""" |
| 1263 |
|
| 1264 |
with open('/etc/fstab', 'a') as f: |
| 1265 |
f.write('UUID=%s %s ext4 defaults,nosuid,noexec 0 0\n' % |
| 1266 |
(self.block.get_property('id-uuid'), self.mount_point)) |
| 1267 |
os.system('sync') |
| 1268 |
self.do_test() |
| 1269 |
|
| 1270 |
def do_test(self): |
| 1271 |
self.assertEqual(self.fs.get_property('mount-points'), []) |
| 1272 |
mount_path = self.fs.call_mount_sync(no_options, None) |
| 1273 |
self.assertEqual(mount_path, self.mount_point) |
| 1274 |
with open('/proc/self/mounts') as f: |
| 1275 |
for line in f: |
| 1276 |
if line.startswith(self.devname()): |
| 1277 |
options = line.split()[3].split(',') |
| 1278 |
break |
| 1279 |
else: |
| 1280 |
self.fail('%s not mounted' % self.devname()) |
| 1281 |
self.assertIn('noexec', options) |
| 1282 |
self.assertIn('nosuid', options) |
| 1283 |
|
| 1284 |
|
| 1285 |
# ---------------------------------------------------------------------------- |
| 1286 |
|
| 1287 |
class Smart(UDisksTestCase): |
| 1288 |
"""Check SMART operation.""" |
| 1289 |
|
| 1290 |
def test_sda(self): |
| 1291 |
"""SMART status of first internal hard disk |
| 1292 |
|
| 1293 |
This is a best-effort readonly test. |
| 1294 |
""" |
| 1295 |
hd = '/dev/sda' |
| 1296 |
has_smart = False |
| 1297 |
|
| 1298 |
if not os.path.exists(hd): |
| 1299 |
self.write_stderr('[skip] ') |
| 1300 |
return |
| 1301 |
|
| 1302 |
process = subprocess.Popen(['skdump', '--can-smart', hd], |
| 1303 |
stdout=subprocess.PIPE) |
| 1304 |
|
| 1305 |
(output, err) = process.communicate() |
| 1306 |
exit_code = process.wait() |
| 1307 |
|
| 1308 |
if exit_code == 0 and output.decode().strip() == 'YES': |
| 1309 |
has_smart = True |
| 1310 |
|
| 1311 |
block = self.client.get_block_for_dev(os.stat(hd).st_rdev) |
| 1312 |
self.assertNotEqual(block, None) |
| 1313 |
drive = self.client.get_drive_for_block(block) |
| 1314 |
ata = self.client.get_object(drive.get_object_path()).get_property('drive-ata') |
| 1315 |
|
| 1316 |
if has_smart: |
| 1317 |
self.assertEqual(ata is not None, has_smart) |
| 1318 |
self.write_stderr('[avail] ') |
| 1319 |
self.assertEqual(ata.get_property('smart-supported'), True) |
| 1320 |
self.assertEqual(ata.get_property('smart-enabled'), True) |
| 1321 |
|
| 1322 |
# wait for SMART data to be read |
| 1323 |
while ata.get_property('smart-updated') == 0: |
| 1324 |
self.write_stderr('[wait for data] ') |
| 1325 |
self.client.settle() |
| 1326 |
time.sleep(0.5) |
| 1327 |
|
| 1328 |
# this is of course not truly correct for a test suite, but let's |
| 1329 |
# consider it a courtesy for developers :-) |
| 1330 |
self.assertEqual(ata.get_property('smart-failing'), False) |
| 1331 |
self.assertIn(ata.get_property('smart-selftest-status'), |
| 1332 |
['success', 'inprogress', 'aborted', 'interrupted']) |
| 1333 |
else: |
| 1334 |
self.write_stderr('[N/A] ') |
| 1335 |
|
| 1336 |
|
| 1337 |
# ---------------------------------------------------------------------------- |
| 1338 |
|
| 1339 |
def _bytes_to_ay(bytes): |
| 1340 |
return [i for i in bytes] |
| 1341 |
|
| 1342 |
|
| 1343 |
class Luks(UDisksTestCase): |
| 1344 |
"""Check LUKS.""" |
| 1345 |
|
| 1346 |
def setup_crypto_device(self): |
| 1347 |
self.fs_create(None, 'ext4', GLib.Variant('a{sv}', { |
| 1348 |
'encrypt.passphrase': GLib.Variant('s', 's3kr1ts3kr1t'), |
| 1349 |
'label': GLib.Variant('s', 'treasure')})) |
| 1350 |
self.client.settle() |
| 1351 |
crypt_obj = self.client.get_object(self.udisks_block().get_object_path()) |
| 1352 |
encrypted = crypt_obj.get_property('encrypted') |
| 1353 |
encrypted.call_lock_sync(no_options, None) |
| 1354 |
return encrypted |
| 1355 |
|
| 1356 |
@staticmethod |
| 1357 |
def unlock_crypto_device(encrypted): |
| 1358 |
return encrypted.call_unlock_sync( |
| 1359 |
's3kr1ts3kr1t', no_options, None) |
| 1360 |
|
| 1361 |
def tearDown(self): |
| 1362 |
"""clean up behind failed test cases""" |
| 1363 |
|
| 1364 |
crypt_obj = self.client.get_object(self.udisks_block().get_object_path()) |
| 1365 |
if crypt_obj: |
| 1366 |
encrypted = crypt_obj.get_property('encrypted') |
| 1367 |
if encrypted: |
| 1368 |
try: |
| 1369 |
encrypted.call_lock_sync(no_options, None) |
| 1370 |
self.write_stderr('[cleanup lock] ') |
| 1371 |
except GLib.GError: |
| 1372 |
pass |
| 1373 |
|
| 1374 |
# needs to run before the other tests |
| 1375 |
def test_0_create_teardown(self): |
| 1376 |
"""LUKS create/teardown""" |
| 1377 |
|
| 1378 |
self.setup_crypto_device() |
| 1379 |
|
| 1380 |
block = self.udisks_block() |
| 1381 |
obj = self.client.get_object(block.get_object_path()) |
| 1382 |
self.assertEqual(obj.get_property('filesystem'), None) |
| 1383 |
encrypted = obj.get_property('encrypted') |
| 1384 |
self.assertNotEqual(encrypted, None) |
| 1385 |
clear_dev = None |
| 1386 |
|
| 1387 |
try: |
| 1388 |
# check encrypted device info |
| 1389 |
self.assertEqual(block.get_property('id-type'), 'crypto_LUKS') |
| 1390 |
self.assertEqual(block.get_property('id-usage'), 'crypto') |
| 1391 |
self.assertEqual(block.get_property('id-label'), '') |
| 1392 |
self.assertEqual(block.get_property('id-uuid'), |
| 1393 |
self.blkid()['ID_FS_UUID']) |
| 1394 |
self.assertEqual(block.get_property('device'), self.devname()) |
| 1395 |
|
| 1396 |
# check whether we can lock/unlock; we also need this to get the |
| 1397 |
# cleartext device |
| 1398 |
self.assertRaises(GLib.GError, encrypted.call_lock_sync, |
| 1399 |
no_options, None) |
| 1400 |
|
| 1401 |
# wrong password |
| 1402 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1403 |
'h4ckpassword', no_options, None) |
| 1404 |
# right password |
| 1405 |
clear_path = Luks.unlock_crypto_device(encrypted) |
| 1406 |
|
| 1407 |
# check cleartext device info |
| 1408 |
clear_obj = self.client.get_object(clear_path) |
| 1409 |
self.assertEqual(clear_obj.get_property('encrypted'), None) |
| 1410 |
clear_block = clear_obj.get_property('block') |
| 1411 |
self.assertEqual(clear_block.get_property('id-type'), 'ext4') |
| 1412 |
self.assertEqual(clear_block.get_property('id-usage'), 'filesystem') |
| 1413 |
self.assertEqual(clear_block.get_property('id-label'), 'treasure') |
| 1414 |
self.assertNotEqual(clear_block.get_property('crypto-backing-device'), None) |
| 1415 |
clear_dev = clear_block.get_property('device') |
| 1416 |
self.assertNotEqual(clear_dev, None) |
| 1417 |
self.assertEqual(clear_block.get_property('id-uuid'), |
| 1418 |
self.blkid(device=clear_dev)['ID_FS_UUID']) |
| 1419 |
|
| 1420 |
clear_fs = clear_obj.get_property('filesystem') |
| 1421 |
self.assertEqual(clear_fs.get_property('mount-points'), []) |
| 1422 |
|
| 1423 |
# check that we do not leak key information |
| 1424 |
udev_dump = subprocess.Popen(['udevadm', 'info', '--export-db'], |
| 1425 |
stdout=subprocess.PIPE) |
| 1426 |
out = udev_dump.communicate()[0] |
| 1427 |
self.assertFalse(b's3kr1ts3kr1t' in out, 'password in udev properties') |
| 1428 |
self.assertFalse(b'essiv:sha' in out, 'key information in udev properties') |
| 1429 |
|
| 1430 |
finally: |
| 1431 |
# tear down cleartext device |
| 1432 |
encrypted.call_lock_sync(no_options, None) |
| 1433 |
if clear_dev is not None: |
| 1434 |
self.assertFalse(os.path.exists(clear_dev)) |
| 1435 |
|
| 1436 |
def test_luks_mount(self): |
| 1437 |
"""LUKS mount/unmount""" |
| 1438 |
|
| 1439 |
encrypted = self.setup_crypto_device() |
| 1440 |
|
| 1441 |
path = Luks.unlock_crypto_device(encrypted) |
| 1442 |
self.client.settle() |
| 1443 |
obj = self.client.get_object(path) |
| 1444 |
fs = obj.get_property('filesystem') |
| 1445 |
self.assertNotEqual(fs, None) |
| 1446 |
|
| 1447 |
# mount |
| 1448 |
mount_path = fs.call_mount_sync(no_options, None) |
| 1449 |
|
| 1450 |
try: |
| 1451 |
self.assertIn('/media/', mount_path) |
| 1452 |
self.assertTrue(mount_path.endswith('treasure')) |
| 1453 |
self.assertTrue(self.is_mountpoint(mount_path)) |
| 1454 |
self.assertProperty(fs, 'mount-points', [mount_path]) |
| 1455 |
|
| 1456 |
# can't lock, busy |
| 1457 |
try: |
| 1458 |
encrypted.call_lock_sync(no_options, None) |
| 1459 |
self.fail('Lock() unexpectedly succeeded on mounted file system') |
| 1460 |
except GLib.GError as e: |
| 1461 |
self.assertIn('UDisks2.Error.Failed', e.message) |
| 1462 |
finally: |
| 1463 |
# umount |
| 1464 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 1465 |
self.client.settle() |
| 1466 |
self.assertFalse(os.path.exists(mount_path), 'mount point was not removed') |
| 1467 |
self.assertProperty(fs, 'mount-points', []) |
| 1468 |
|
| 1469 |
# lock |
| 1470 |
encrypted.call_lock_sync(no_options, None) |
| 1471 |
self.client.settle() |
| 1472 |
self.assertEqual(self.client.get_object(path), None) |
| 1473 |
|
| 1474 |
def test_luks_forced_removal(self): |
| 1475 |
"""LUKS forced removal""" |
| 1476 |
|
| 1477 |
# unlock and mount it |
| 1478 |
encrypted = self.setup_crypto_device() |
| 1479 |
crypt_obj = self.client.get_object(self.udisks_block().get_object_path()) |
| 1480 |
path = Luks.unlock_crypto_device(encrypted) |
| 1481 |
try: |
| 1482 |
fs = self.client.get_object(path).get_property('filesystem') |
| 1483 |
mount_path = fs.call_mount_sync(no_options, None) |
| 1484 |
self.assertIn('/media/', mount_path) |
| 1485 |
self.assertTrue(mount_path.endswith('treasure')) |
| 1486 |
|
| 1487 |
# removal should clean up mounts |
| 1488 |
try: |
| 1489 |
self.remove_device(self.device) |
| 1490 |
self.assertFalse(os.path.exists(mount_path)) |
| 1491 |
timeout = 50 |
| 1492 |
while timeout > 0: |
| 1493 |
if self.client.get_object(path) is None: |
| 1494 |
break |
| 1495 |
timeout -= 1 |
| 1496 |
# we do not have a main loop, and cannot currently use |
| 1497 |
# g_main_context_get_default() from introspection, so |
| 1498 |
# instead of refreshing self.client, get a new one |
| 1499 |
self.client = UDisks.Client.new_sync(None) |
| 1500 |
time.sleep(0.1) |
| 1501 |
self.assertGreater(timeout, 0, |
| 1502 |
'timeout waiting for object path %s to disappear' % path) |
| 1503 |
finally: |
| 1504 |
self.readd_devices() |
| 1505 |
|
| 1506 |
# after putting it back, it should be mountable again |
| 1507 |
path = Luks.unlock_crypto_device(encrypted) |
| 1508 |
self.client.settle() |
| 1509 |
fs = self.client.get_object(path).get_property('filesystem') |
| 1510 |
mount_path = fs.call_mount_sync(no_options, None) |
| 1511 |
self.assertIn('/media/', mount_path) |
| 1512 |
self.assertTrue(mount_path.endswith('treasure')) |
| 1513 |
|
| 1514 |
# umount |
| 1515 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 1516 |
self.client.settle() |
| 1517 |
self.assertFalse(os.path.exists(mount_path), 'mount point was not removed') |
| 1518 |
self.assertProperty(fs, 'mount-points', []) |
| 1519 |
finally: |
| 1520 |
# lock |
| 1521 |
crypt_obj.get_property('encrypted').call_lock_sync( |
| 1522 |
no_options, None) |
| 1523 |
self.client.settle() |
| 1524 |
self.assertEqual(self.client.get_object(path), None) |
| 1525 |
|
| 1526 |
@staticmethod |
| 1527 |
def keyfile_options(keyfile_contents): |
| 1528 |
return GLib.Variant('a{sv}', { |
| 1529 |
'keyfile_contents': GLib.Variant('ay', _bytes_to_ay(keyfile_contents)) |
| 1530 |
}) |
| 1531 |
|
| 1532 |
def test_keyfile_equivalent(self): |
| 1533 |
"""Setup device using passphrase, unlock using keyfile.""" |
| 1534 |
encrypted = self.setup_crypto_device() |
| 1535 |
# wrong password, has bytes after a trailing '\0' |
| 1536 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1537 |
'', Luks.keyfile_options(b's3kr1ts3kr1t\0X'), None) |
| 1538 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1539 |
'', Luks.keyfile_options(b's3kr1ts3kr1t\n'), None) |
| 1540 |
# correct password, specified as keyfile |
| 1541 |
encrypted.call_unlock_sync('', Luks.keyfile_options(b's3kr1ts3kr1t'), None) |
| 1542 |
encrypted.call_lock_sync(no_options, None) |
| 1543 |
|
| 1544 |
def test_plaintext_keyfile(self): |
| 1545 |
"""Setup a device using a plaintext keyfile.""" |
| 1546 |
# Using a plaintext keyfile should be equivalent to passphrase |
| 1547 |
self.fs_create(None, 'ext4', GLib.Variant('a{sv}', { |
| 1548 |
'encrypt.passphrase': GLib.Variant('ay', _bytes_to_ay(b's3kr1ts3kr1t')), |
| 1549 |
'label': GLib.Variant('s', 'treasure')})) |
| 1550 |
|
| 1551 |
crypt_obj = self.client.get_object(self.udisks_block().get_object_path()) |
| 1552 |
encrypted = crypt_obj.get_property('encrypted') |
| 1553 |
encrypted.call_lock_sync(no_options, None) |
| 1554 |
|
| 1555 |
# wrong password |
| 1556 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1557 |
'h4ckpassword', no_options, None) |
| 1558 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1559 |
'', Luks.keyfile_options(b's3kr1ts3kr1t\0X'), None) |
| 1560 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1561 |
'', Luks.keyfile_options(b's3kr1ts3kr1t\n'), None) |
| 1562 |
|
| 1563 |
# correct password |
| 1564 |
encrypted.call_unlock_sync('', Luks.keyfile_options(b's3kr1ts3kr1t'), None) |
| 1565 |
encrypted.call_lock_sync(no_options, None) |
| 1566 |
|
| 1567 |
# correct password, specified as passphrase |
| 1568 |
encrypted.call_unlock_sync('s3kr1ts3kr1t', no_options, None) |
| 1569 |
encrypted.call_lock_sync(no_options, None) |
| 1570 |
|
| 1571 |
def test_binary_keyfile(self): |
| 1572 |
|
| 1573 |
key_file = b's\0me \bina\ry \xda\ta\0\0\1\1\xff\xff' |
| 1574 |
|
| 1575 |
self.fs_create(None, 'ext4', GLib.Variant('a{sv}', { |
| 1576 |
'encrypt.passphrase': GLib.Variant('ay', _bytes_to_ay(key_file)), |
| 1577 |
'label': GLib.Variant('s', 'treasure')})) |
| 1578 |
crypt_obj = self.client.get_object(self.udisks_block().get_object_path()) |
| 1579 |
encrypted = crypt_obj.get_property('encrypted') |
| 1580 |
encrypted.call_lock_sync(no_options, None) |
| 1581 |
|
| 1582 |
# wrong password |
| 1583 |
missing_byte = Luks.keyfile_options(key_file[:-1]) |
| 1584 |
extra_bytes = Luks.keyfile_options(key_file+b'\0X') |
| 1585 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1586 |
'h4ckpassword', no_options, None) |
| 1587 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1588 |
'', missing_byte, None) |
| 1589 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1590 |
'', extra_bytes, None) |
| 1591 |
|
| 1592 |
# correct password |
| 1593 |
encrypted.call_unlock_sync('', Luks.keyfile_options(key_file), None) |
| 1594 |
encrypted.call_lock_sync(no_options, None) |
| 1595 |
|
| 1596 |
def test_ascii_keyfile_newline(self): |
| 1597 |
|
| 1598 |
key_file = b'ascii string\nwith newline\n' |
| 1599 |
|
| 1600 |
# setup crypto device |
| 1601 |
self.fs_create(None, 'ext4', GLib.Variant('a{sv}', { |
| 1602 |
'encrypt.passphrase': GLib.Variant('ay', _bytes_to_ay(key_file)), |
| 1603 |
'label': GLib.Variant('s', 'treasure')})) |
| 1604 |
crypt_obj = self.client.get_object(self.udisks_block().get_object_path()) |
| 1605 |
encrypted = crypt_obj.get_property('encrypted') |
| 1606 |
encrypted.call_lock_sync(no_options, None) |
| 1607 |
|
| 1608 |
# wrong password |
| 1609 |
missing_byte = Luks.keyfile_options(key_file[:-1]) |
| 1610 |
extra_bytes = Luks.keyfile_options(key_file+b'\0X') |
| 1611 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1612 |
'h4ckpassword', no_options, None) |
| 1613 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1614 |
'', missing_byte, None) |
| 1615 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1616 |
'', extra_bytes, None) |
| 1617 |
|
| 1618 |
# correct password |
| 1619 |
encrypted.call_unlock_sync('', Luks.keyfile_options(key_file), None) |
| 1620 |
encrypted.call_lock_sync(no_options, None) |
| 1621 |
|
| 1622 |
# We no longer mimic cryptsetup which in passphrase-mode stops input |
| 1623 |
# at the first newline: |
| 1624 |
encrypted.call_unlock_sync(key_file.decode('utf-8'), no_options, None) |
| 1625 |
|
| 1626 |
def test_change_passphrase(self): |
| 1627 |
|
| 1628 |
key_file_0 = b's\0me \bina\ry \xda\ta\0\0\1\1\xff\xff\0' |
| 1629 |
key_file_1 = b's\0me \bina\ry \xda\ta\0\0\1\1\xff\xff\1' |
| 1630 |
|
| 1631 |
encrypted = self.setup_crypto_device() |
| 1632 |
|
| 1633 |
# change: passphrase -> passphrase |
| 1634 |
encrypted.call_change_passphrase_sync('s3kr1ts3kr1t', 'passphrase', no_options, None) |
| 1635 |
|
| 1636 |
# verify new password: |
| 1637 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1638 |
's3kr1ts3kr1t', no_options, None) |
| 1639 |
encrypted.call_unlock_sync('passphrase', no_options, None) |
| 1640 |
encrypted.call_lock_sync(no_options, None) |
| 1641 |
|
| 1642 |
# change: passphrase -> keyfile |
| 1643 |
encrypted.call_change_passphrase_sync( |
| 1644 |
'passphrase', '', GLib.Variant('a{sv}', { |
| 1645 |
'new_keyfile_contents': GLib.Variant('ay', _bytes_to_ay(key_file_0)), |
| 1646 |
}), |
| 1647 |
None) |
| 1648 |
|
| 1649 |
# verify new password: |
| 1650 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1651 |
'passphrase', no_options, None) |
| 1652 |
encrypted.call_unlock_sync('', Luks.keyfile_options(key_file_0), None) |
| 1653 |
encrypted.call_lock_sync(no_options, None) |
| 1654 |
|
| 1655 |
# change: keyfile -> keyfile |
| 1656 |
encrypted.call_change_passphrase_sync( |
| 1657 |
'', '', GLib.Variant('a{sv}', { |
| 1658 |
'old_keyfile_contents': GLib.Variant('ay', _bytes_to_ay(key_file_0)), |
| 1659 |
'new_keyfile_contents': GLib.Variant('ay', _bytes_to_ay(key_file_1)), |
| 1660 |
}), |
| 1661 |
None) |
| 1662 |
|
| 1663 |
# verify new password: |
| 1664 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1665 |
'', Luks.keyfile_options(key_file_0), None) |
| 1666 |
encrypted.call_unlock_sync('', Luks.keyfile_options(key_file_1), None) |
| 1667 |
encrypted.call_lock_sync(no_options, None) |
| 1668 |
|
| 1669 |
# change: keyfile -> passphrase |
| 1670 |
encrypted.call_change_passphrase_sync( |
| 1671 |
'', 's3kr1ts3kr1t', GLib.Variant('a{sv}', { |
| 1672 |
'old_keyfile_contents': GLib.Variant('ay', _bytes_to_ay(key_file_1)), |
| 1673 |
}), |
| 1674 |
None) |
| 1675 |
|
| 1676 |
# verify new password: |
| 1677 |
self.assertRaises(GLib.GError, encrypted.call_unlock_sync, |
| 1678 |
'', Luks.keyfile_options(key_file_1), None) |
| 1679 |
encrypted.call_unlock_sync('s3kr1ts3kr1t', no_options, None) |
| 1680 |
encrypted.call_lock_sync(no_options, None) |
| 1681 |
|
| 1682 |
|
| 1683 |
# ---------------------------------------------------------------------------- |
| 1684 |
|
| 1685 |
class TimeoutException(Exception): |
| 1686 |
pass |
| 1687 |
|
| 1688 |
|
| 1689 |
def handler(signum, frame): |
| 1690 |
raise TimeoutException |
| 1691 |
|
| 1692 |
|
| 1693 |
signal.signal(signal.SIGALRM, handler) |
| 1694 |
|
| 1695 |
|
| 1696 |
class Polkit(UDisksTestCase, test_polkitd.PolkitTestCase): |
| 1697 |
"""Check operation with polkit.""" |
| 1698 |
|
| 1699 |
def setUp(self): |
| 1700 |
if self.system: |
| 1701 |
self.skipTest("Skipping Polkit tests when testing a system instance") |
| 1702 |
if DISTRO_VER == ("fedora", "26"): |
| 1703 |
self.skipTest("Skipping hanging PolicyKit tests on Fedora 26") |
| 1704 |
|
| 1705 |
UDisksTestCase.setUp(self) |
| 1706 |
|
| 1707 |
def test_internal_fs_forbidden(self): |
| 1708 |
"""Create FS on internal drive (forbidden)""" |
| 1709 |
|
| 1710 |
self.start_polkitd(['org.freedesktop.udisks2.modify-device']) |
| 1711 |
|
| 1712 |
options = GLib.Variant('a{sv}', {'label': GLib.Variant('s', 'polkitno')}) |
| 1713 |
with self.assertRaises(GLib.GError) as cm: |
| 1714 |
self.fs_create(None, 'ext4', options) |
| 1715 |
self.assertIn('UDisks2.Error.NotAuthorized', cm.exception.message) |
| 1716 |
|
| 1717 |
# did not actually do anything |
| 1718 |
block = self.udisks_block() |
| 1719 |
self.assertNotEqual(block.get_property('id-label'), 'polkitno') |
| 1720 |
|
| 1721 |
def test_internal_fs_allowed(self): |
| 1722 |
"""Create FS on internal drive (allowed)""" |
| 1723 |
self.start_polkitd(['org.freedesktop.udisks2.modify-device-system', |
| 1724 |
'org.freedesktop.udisks2.modify-device']) |
| 1725 |
|
| 1726 |
options = GLib.Variant('a{sv}', {'label': GLib.Variant('s', 'polkityes')}) |
| 1727 |
|
| 1728 |
# it definitely shouldn't take more than 5 minutes to make an ext4 |
| 1729 |
# filesystem -- if this happens than something is really wrong -- polkit |
| 1730 |
# is probably waiting for password (and no one will ever type it) |
| 1731 |
signal.alarm(300) |
| 1732 |
try: |
| 1733 |
self.fs_create(None, 'ext4', options) |
| 1734 |
except TimeoutException: |
| 1735 |
self.fail('Timeout while trying to create filesystem with Polkit.') |
| 1736 |
else: |
| 1737 |
signal.alarm(0) |
| 1738 |
|
| 1739 |
block = self.udisks_block() |
| 1740 |
self.assertProperty(block, 'id-usage', 'filesystem') |
| 1741 |
self.assertEqual(block.get_property('id-type'), 'ext4') |
| 1742 |
self.assertEqual(block.get_property('id-label'), 'polkityes') |
| 1743 |
|
| 1744 |
def test_removable_fs(self): |
| 1745 |
"""Mount FS on removable drive (allowed)""" |
| 1746 |
|
| 1747 |
self.mkfs('ext4', 'polkityes') |
| 1748 |
self.start_polkitd(['org.freedesktop.udisks2.filesystem-mount', |
| 1749 |
'org.freedesktop.udisks2.filesystem-mount-other-seat']) |
| 1750 |
|
| 1751 |
# the scsi_debug CD drive content is the same as for the HD drive, but |
| 1752 |
# udev does not know about this; so give it a nudge to re-probe |
| 1753 |
subprocess.call(['udevadm', 'trigger', '--action=change', |
| 1754 |
'--sysname-match=' + os.path.basename(self.cd_device)]) |
| 1755 |
self.sync() |
| 1756 |
self.sync() |
| 1757 |
|
| 1758 |
signal.alarm(300) |
| 1759 |
try: |
| 1760 |
fs = self.udisks_filesystem(cd=True) |
| 1761 |
self.assertNotEqual(fs, None) |
| 1762 |
subprocess.call(['blockdev', '--setro', self.cd_device]) |
| 1763 |
mount_path = fs.call_mount_sync(no_options, None) |
| 1764 |
self.assertIn('/media/', mount_path) |
| 1765 |
|
| 1766 |
self.retry_busy(fs.call_unmount_sync, no_options, None) |
| 1767 |
except TimeoutException: |
| 1768 |
self.fail('Timeout while trying to mount and unmount filesystem with Polkit.') |
| 1769 |
else: |
| 1770 |
signal.alarm(0) |
| 1771 |
|
| 1772 |
self.client.settle() |
| 1773 |
|
| 1774 |
# ---------------------------------------------------------------------------- |
| 1775 |
|
| 1776 |
|
| 1777 |
def setup_lio(): |
| 1778 |
"""create virtual test devices""" |
| 1779 |
|
| 1780 |
orig_devs = {dev for dev in os.listdir("/dev") if |
| 1781 |
re.match(r'sd[a-z]+$', dev)} |
| 1782 |
|
| 1783 |
# create fake SCSI hard drives |
| 1784 |
our_path = os.path.abspath(__file__) |
| 1785 |
our_dir = os.path.dirname(our_path) |
| 1786 |
json_path = os.path.join(our_dir, 'dbus-tests', 'targetcli_config.json') |
| 1787 |
assert subprocess.call( |
| 1788 |
["targetcli", |
| 1789 |
"restoreconfig %s" % json_path]) == 0 |
| 1790 |
time.sleep(0.1) |
| 1791 |
|
| 1792 |
devs = {dev for dev in os.listdir("/dev") if |
| 1793 |
re.match(r'sd[a-z]+$', dev)} |
| 1794 |
|
| 1795 |
vdevs = ["/dev/%s" % dev for dev in (devs - orig_devs)] |
| 1796 |
|
| 1797 |
# let's be 100% sure that we pick a virtual one |
| 1798 |
for d in vdevs: |
| 1799 |
with open('/sys/block/%s/device/model' % |
| 1800 |
os.path.basename(d)) as model_file: |
| 1801 |
assert model_file.read().strip() == 'udisks_test_dis' |
| 1802 |
|
| 1803 |
assert len(vdevs) >= 4 |
| 1804 |
return vdevs |
| 1805 |
|
| 1806 |
|
| 1807 |
def remove_lio(): |
| 1808 |
# remove the fake SCSI devices and their backing files |
| 1809 |
assert subprocess.call(['targetcli', 'clearconfig confirm=True']) == 0 |
| 1810 |
for disk_file in glob("/var/tmp/udisks_test_disk*"): |
| 1811 |
os.unlink(disk_file) |
| 1812 |
|
| 1813 |
def is_s390(): |
| 1814 |
return os.uname()[4].startswith('s390') |
| 1815 |
|
| 1816 |
|
| 1817 |
@unittest.skipIf(is_s390(), "mdadm is broken on s390x") |
| 1818 |
class MDRaid(UDisksTestCase): |
| 1819 |
""" |
| 1820 |
Test some of the code we are refactoring. |
| 1821 |
""" |
| 1822 |
devices = None |
| 1823 |
|
| 1824 |
def setUp(self): |
| 1825 |
self.assertEqual(len(glob('/dev/md/int_test*')), 0) |
| 1826 |
self.md_device = '/dev/md/int_test0' |
| 1827 |
|
| 1828 |
self.md_legs = self.devices[:2] |
| 1829 |
create = ['mdadm', '--create', self.md_device, '--level=1', |
| 1830 |
'--metadata=0.90', '--raid-devices=2', '--bitmap=none'] |
| 1831 |
create.extend(self.md_legs) |
| 1832 |
self.assertEqual(subprocess.call(create), 0) |
| 1833 |
self.sync() |
| 1834 |
|
| 1835 |
def tearDown(self): |
| 1836 |
self.assertEqual( |
| 1837 |
subprocess.call(['mdadm', '--stop', self.md_device]), 0) |
| 1838 |
zero = ['mdadm', '--zero-superblock', '--force'] |
| 1839 |
zero.extend(self.md_legs) |
| 1840 |
self.assertEqual(subprocess.call(zero), 0) |
| 1841 |
|
| 1842 |
def test_md_raid_methods(self): |
| 1843 |
self.assertIsNotNone(self.devices) |
| 1844 |
|
| 1845 |
block_path = "/org/freedesktop/UDisks2/block_devices/%s" % os.path.basename(self.md_legs[0]) |
| 1846 |
bo = self.client.get_object(block_path) |
| 1847 |
block_interface = bo.get_interface('org.freedesktop.UDisks2.Block') |
| 1848 |
self.assertNotProperty(block_interface, 'mdraid-member', '/') |
| 1849 |
mdraid_path = block_interface.get_property('mdraid-member') |
| 1850 |
mdraid_object = self.client.get_object(mdraid_path) |
| 1851 |
|
| 1852 |
i = mdraid_object.get_interface('org.freedesktop.UDisks2.MDRaid') |
| 1853 |
|
| 1854 |
block = UDisks.Client.get_block_for_mdraid(self.client, i) |
| 1855 |
self.assertIsNotNone(block) |
| 1856 |
self.assertEqual( |
| 1857 |
block.get_property('mdraid'), i.get_object_path()) |
| 1858 |
|
| 1859 |
block_list = UDisks.Client.get_all_blocks_for_mdraid( |
| 1860 |
self.client, i) |
| 1861 |
self.assertIsNotNone(block_list) |
| 1862 |
self.assertIsInstance(block_list, list) |
| 1863 |
for b in block_list: |
| 1864 |
self.assertEqual( |
| 1865 |
b.get_property('mdraid'), i.get_object_path()) |
| 1866 |
|
| 1867 |
block_list = UDisks.Client.get_members_for_mdraid( |
| 1868 |
self.client, i) |
| 1869 |
self.assertIsNotNone(block_list) |
| 1870 |
self.assertIsInstance(block_list, list) |
| 1871 |
for b in block_list: |
| 1872 |
self.assertEqual( |
| 1873 |
b.get_property('mdraid-member'), |
| 1874 |
i.get_object_path()) |
| 1875 |
|
| 1876 |
@classmethod |
| 1877 |
def setUpClass(cls): |
| 1878 |
cls.devices = setup_lio() |
| 1879 |
cls.sync() |
| 1880 |
|
| 1881 |
@classmethod |
| 1882 |
def tearDownClass(cls): |
| 1883 |
remove_lio() |
| 1884 |
|
| 1885 |
# ---------------------------------------------------------------------------- |
| 1886 |
|
| 1887 |
def _copy_files(source_files, target_dir, tmpdir): |
| 1888 |
""" |
| 1889 |
Copies the source files to the target directory. If the file exists in the |
| 1890 |
target dir it's backed up to tmpdir and placed on a list of files to |
| 1891 |
restore. If the file doesn't exist it's flagged to be deleted. |
| 1892 |
Use restore_files for processing. |
| 1893 |
|
| 1894 |
Returns a list of files that need to be restored or deleted. |
| 1895 |
""" |
| 1896 |
restore_list = [] |
| 1897 |
for f in source_files: |
| 1898 |
tgt = os.path.join(target_dir, os.path.basename(f)) |
| 1899 |
if os.path.exists(tgt): |
| 1900 |
shutil.move(tgt, tmpdir) |
| 1901 |
restore_list.append((tgt, False)) |
| 1902 |
else: |
| 1903 |
restore_list.append((tgt, True)) |
| 1904 |
|
| 1905 |
print("Copying file: %s to %s directory!" % (f, target_dir)) |
| 1906 |
shutil.copy(f, target_dir) |
| 1907 |
|
| 1908 |
return restore_list |
| 1909 |
|
| 1910 |
|
| 1911 |
def install_config_files(projdir, tmpdir): |
| 1912 |
""" |
| 1913 |
Copies DBus, PolicyKit and UDev config file(s) |
| 1914 |
|
| 1915 |
Returns a list of files that need to be restored or deleted. |
| 1916 |
""" |
| 1917 |
copied = [] |
| 1918 |
|
| 1919 |
# udev rules |
| 1920 |
tgtdir = next((d for d in ['/usr/lib/udev/rules.d/', '/lib/udev/rules.d'] if os.path.exists(d)), None) |
| 1921 |
if tgtdir is None: |
| 1922 |
raise RuntimeError('Cannot find udev rules directory') |
| 1923 |
|
| 1924 |
copied.extend(_copy_files((os.path.join(projdir, 'data/80-udisks2.rules'),), |
| 1925 |
tgtdir, tmpdir)) |
| 1926 |
|
| 1927 |
# dbus config files |
| 1928 |
copied.extend(_copy_files((os.path.join(projdir, 'data/org.freedesktop.UDisks2.conf'),), |
| 1929 |
'/usr/share/dbus-1/system.d/', tmpdir)) |
| 1930 |
|
| 1931 |
# polkit policies |
| 1932 |
policies = glob(projdir + '/data/*.policy') + glob(projdir + '/modules/*/data/*.policy') |
| 1933 |
copied.extend(_copy_files(policies, '/usr/share/polkit-1/actions/', tmpdir)) |
| 1934 |
|
| 1935 |
# udisks2.conf |
| 1936 |
if not os.path.exists('/etc/udisks2'): |
| 1937 |
os.mkdir('/etc/udisks2', 0o755) |
| 1938 |
copied.extend(_copy_files((os.path.join(projdir, 'udisks/udisks2.conf'),), |
| 1939 |
'/etc/udisks2/', tmpdir)) |
| 1940 |
|
| 1941 |
return copied |
| 1942 |
|
| 1943 |
def restore_files(restore_list, tmpdir): |
| 1944 |
for f, delete in restore_list: |
| 1945 |
if delete: |
| 1946 |
print(f) |
| 1947 |
os.unlink(f) |
| 1948 |
else: |
| 1949 |
shutil.move(os.path.join(tmpdir, os.path.basename(f)), f) |
| 1950 |
|
| 1951 |
# ---------------------------------------------------------------------------- |
| 1952 |
|
| 1953 |
|
| 1954 |
if __name__ == '__main__': |
| 1955 |
argparser = argparse.ArgumentParser(description='udisks2 integration test suite') |
| 1956 |
argparser.add_argument('-s', '--system', dest='system', |
| 1957 |
help='run the test against the system installed instance', |
| 1958 |
action='store_true') |
| 1959 |
argparser.add_argument('-l', '--log-file', dest='logfile', |
| 1960 |
help='write daemon log to a file') |
| 1961 |
argparser.add_argument('testname', nargs='*', |
| 1962 |
help='name of test class or method (e. g. "Drive", "FS.test_ext2")') |
| 1963 |
cli_args = argparser.parse_args() |
| 1964 |
|
| 1965 |
UDisksTestCase.init(logfile=cli_args.logfile, system=cli_args.system) |
| 1966 |
if cli_args.testname: |
| 1967 |
tests = unittest.TestLoader().loadTestsFromNames( |
| 1968 |
cli_args.testname, __import__('__main__')) |
| 1969 |
else: |
| 1970 |
tests = unittest.TestLoader().loadTestsFromName('__main__') |
| 1971 |
if unittest.TextTestRunner(verbosity=2).run(tests).wasSuccessful(): |
| 1972 |
sys.exit(0) |
| 1973 |
else: |
| 1974 |
sys.exit(1) |