2016-09-03 02:21:50 +02:00
|
|
|
# pylint: disable=protected-access,pointless-statement
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
2017-01-18 22:16:46 +01:00
|
|
|
|
2016-09-03 02:21:50 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import time
|
2016-10-18 13:10:06 +02:00
|
|
|
import unittest
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
import qubes.devices
|
|
|
|
import qubes.ext.pci
|
|
|
|
import qubes.tests
|
|
|
|
|
|
|
|
|
|
|
|
class TC_00_Devices_PCI(qubes.tests.SystemTestsMixin,
|
|
|
|
qubes.tests.QubesTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(TC_00_Devices_PCI, self).setUp()
|
|
|
|
if self._testMethodName not in ['test_000_list']:
|
|
|
|
pcidev = os.environ.get('QUBES_TEST_PCIDEV', None)
|
|
|
|
if pcidev is None:
|
|
|
|
self.skipTest('Specify PCI device with QUBES_TEST_PCIDEV '
|
|
|
|
'environment variable')
|
|
|
|
self.dev = self.app.domains[0].devices['pci'][pcidev]
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assignment = qubes.devices.DeviceAssignment(backend_domain=self.dev.backend_domain, ident=self.dev.ident, persistent=True)
|
2016-09-03 02:21:50 +02:00
|
|
|
if isinstance(self.dev, qubes.devices.UnknownDevice):
|
|
|
|
self.skipTest('Specified device {} does not exists'.format(pcidev))
|
|
|
|
self.init_default_template()
|
|
|
|
self.vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('vm'),
|
|
|
|
label='red',
|
|
|
|
)
|
|
|
|
self.vm.create_on_disk()
|
2016-09-03 20:29:14 +02:00
|
|
|
self.vm.features['pci-no-strict-reset/' + pcidev] = True
|
2016-09-03 02:21:50 +02:00
|
|
|
self.app.save()
|
|
|
|
|
2016-10-18 13:10:06 +02:00
|
|
|
@unittest.expectedFailure
|
2016-09-03 02:21:50 +02:00
|
|
|
def test_000_list(self):
|
|
|
|
p = subprocess.Popen(['lspci'], stdout=subprocess.PIPE)
|
|
|
|
# get a dict: BDF -> description
|
|
|
|
actual_devices = dict(
|
|
|
|
l.split(' (')[0].split(' ', 1)
|
2017-02-19 00:07:16 +01:00
|
|
|
for l in p.communicate()[0].decode().splitlines())
|
2016-09-03 02:21:50 +02:00
|
|
|
for dev in self.app.domains[0].devices['pci']:
|
|
|
|
self.assertIsInstance(dev, qubes.ext.pci.PCIDevice)
|
|
|
|
self.assertEqual(dev.backend_domain, self.app.domains[0])
|
|
|
|
self.assertIn(dev.ident, actual_devices)
|
|
|
|
self.assertEqual(dev.description, actual_devices[dev.ident])
|
|
|
|
actual_devices.pop(dev.ident)
|
|
|
|
|
|
|
|
if actual_devices:
|
|
|
|
self.fail('Not all devices listed, missing: {}'.format(
|
|
|
|
actual_devices))
|
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
def assertDeviceNotInCollection(self, dev, dev_col):
|
|
|
|
self.assertNotIn(dev, dev_col.attached())
|
|
|
|
self.assertNotIn(dev, dev_col.persistent())
|
|
|
|
self.assertNotIn(dev, dev_col.assignments())
|
|
|
|
self.assertNotIn(dev, dev_col.assignments(persistent=True))
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
def test_010_attach_offline_persistent(self):
|
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
dev_col.attach(self.assignment)
|
2016-09-03 02:21:50 +02:00
|
|
|
self.app.save()
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertNotIn(self.dev, dev_col.attached())
|
|
|
|
self.assertIn(self.dev, dev_col.persistent())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments(persistent=True))
|
|
|
|
self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
self.vm.start()
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev, dev_col.attached())
|
2016-09-03 02:21:50 +02:00
|
|
|
p = self.vm.run('lspci', passio_popen=True)
|
|
|
|
(stdout, _) = p.communicate()
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev.description, stdout.decode())
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
def test_011_attach_offline_temp_fail(self):
|
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
self.assignment.persistent = False
|
2017-04-15 16:44:42 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesVMNotRunningError):
|
2017-04-07 22:17:09 +02:00
|
|
|
dev_col.attach(self.assignment)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
def test_020_attach_online_persistent(self):
|
2016-09-03 02:21:50 +02:00
|
|
|
self.vm.start()
|
2017-04-07 22:17:09 +02:00
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
dev_col.attach(self.assignment)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev, dev_col.attached())
|
|
|
|
self.assertIn(self.dev, dev_col.persistent())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments(persistent=True))
|
|
|
|
self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
# give VM kernel some time to discover new device
|
|
|
|
time.sleep(1)
|
|
|
|
p = self.vm.run('lspci', passio_popen=True)
|
|
|
|
(stdout, _) = p.communicate()
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev.description, stdout.decode())
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
|
|
|
|
def test_021_persist_detach_online_fail(self):
|
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
dev_col.attach(self.assignment)
|
2016-09-03 02:21:50 +02:00
|
|
|
self.app.save()
|
|
|
|
self.vm.start()
|
2017-04-15 16:44:42 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
|
2017-04-07 22:17:09 +02:00
|
|
|
self.vm.devices['pci'].detach(self.assignment)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
def test_030_persist_attach_detach_offline(self):
|
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
dev_col.attach(self.assignment)
|
|
|
|
self.app.save()
|
|
|
|
self.assertNotIn(self.dev, dev_col.attached())
|
|
|
|
self.assertIn(self.dev, dev_col.persistent())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments(persistent=True))
|
|
|
|
self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
|
|
|
|
dev_col.detach(self.assignment)
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
|
|
|
|
def test_031_attach_detach_online_temp(self):
|
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.vm.start()
|
|
|
|
self.assignment.persistent = False
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
dev_col.attach(self.assignment)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev, dev_col.attached())
|
|
|
|
self.assertNotIn(self.dev, dev_col.persistent())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments())
|
|
|
|
self.assertIn(self.dev, dev_col.assignments(persistent=False))
|
|
|
|
self.assertNotIn(self.dev, dev_col.assignments(persistent=True))
|
|
|
|
self.assertIn(self.dev, dev_col.assignments(persistent=False))
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
# give VM kernel some time to discover new device
|
|
|
|
time.sleep(1)
|
2016-09-03 02:21:50 +02:00
|
|
|
p = self.vm.run('lspci', passio_popen=True)
|
|
|
|
(stdout, _) = p.communicate()
|
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev.description, stdout.decode())
|
|
|
|
dev_col.detach(self.assignment)
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
p = self.vm.run('lspci', passio_popen=True)
|
|
|
|
(stdout, _) = p.communicate()
|
|
|
|
self.assertNotIn(self.dev.description, stdout.decode())
|