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>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2016-09-03 02:21:50 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2016-09-03 02:21:50 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2016-09-03 02:21:50 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2016-09-03 02:21:50 +02:00
|
|
|
#
|
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
|
|
|
|
|
|
|
|
|
2017-08-14 23:07:25 +02:00
|
|
|
@qubes.tests.skipUnlessEnv('QUBES_TEST_PCIDEV')
|
2017-07-12 19:01:15 +02:00
|
|
|
class TC_00_Devices_PCI(qubes.tests.SystemTestCase):
|
2016-09-03 02:21:50 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(TC_00_Devices_PCI, self).setUp()
|
|
|
|
if self._testMethodName not in ['test_000_list']:
|
2017-08-14 23:07:25 +02:00
|
|
|
pcidev = os.environ['QUBES_TEST_PCIDEV']
|
2016-09-03 02:21:50 +02:00
|
|
|
self.dev = self.app.domains[0].devices['pci'][pcidev]
|
2018-01-18 16:52:01 +01: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',
|
|
|
|
)
|
2018-01-26 23:49:11 +01:00
|
|
|
self.vm.virt_mode = 'hvm'
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
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']:
|
2018-01-18 16:52:01 +01:00
|
|
|
lspci_ident = dev.ident.replace('_', ':')
|
2016-09-03 02:21:50 +02:00
|
|
|
self.assertIsInstance(dev, qubes.ext.pci.PCIDevice)
|
|
|
|
self.assertEqual(dev.backend_domain, self.app.domains[0])
|
2018-01-18 16:52:01 +01:00
|
|
|
self.assertIn(lspci_ident, actual_devices)
|
|
|
|
self.assertEqual(dev.description, actual_devices[lspci_ident])
|
|
|
|
actual_devices.pop(lspci_ident)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
|
|
|
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)
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
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
|
|
|
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(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())
|
2018-01-18 16:52:01 +01:00
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
|
|
|
self.vm.run_for_stdio('lspci'))
|
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):
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
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):
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.vm.start())
|
2017-04-07 22:17:09 +02:00
|
|
|
dev_col = self.vm.devices['pci']
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
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)
|
2018-01-18 16:52:01 +01:00
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
|
|
|
self.vm.run_for_stdio('lspci'))
|
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)
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
dev_col.attach(self.assignment))
|
2016-09-03 02:21:50 +02:00
|
|
|
self.app.save()
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.vm.start())
|
2017-04-15 16:44:42 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
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)
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
dev_col.attach(self.assignment))
|
2017-04-07 22:17:09 +02:00
|
|
|
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))
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
dev_col.detach(self.assignment))
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
|
|
|
|
|
|
|
def test_031_attach_detach_online_temp(self):
|
|
|
|
dev_col = self.vm.devices['pci']
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.vm.start())
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assignment.persistent = False
|
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
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)
|
2018-01-18 16:52:01 +01:00
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
|
|
|
self.vm.run_for_stdio('lspci'))
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertIn(self.dev.description, stdout.decode())
|
2018-01-18 16:52:01 +01:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
dev_col.detach(self.assignment))
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertDeviceNotInCollection(self.dev, dev_col)
|
2016-09-03 02:21:50 +02:00
|
|
|
|
2018-01-18 16:52:01 +01:00
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
|
|
|
self.vm.run_for_stdio('lspci'))
|
2017-04-07 22:17:09 +02:00
|
|
|
self.assertNotIn(self.dev.description, stdout.decode())
|