2016-03-09 14:20:32 +01: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.
|
|
|
|
#
|
|
|
|
|
|
|
|
import qubes.devices
|
|
|
|
|
|
|
|
import qubes.tests
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
class TestDevice(qubes.devices.DeviceInfo):
|
2017-04-15 23:48:02 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
2016-06-26 04:07:15 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TestVMCollection(dict):
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(set(self.values()))
|
|
|
|
|
|
|
|
|
|
|
|
class TestApp(object):
|
2017-04-15 23:48:02 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
2016-06-26 04:07:15 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.domains = TestVMCollection()
|
|
|
|
|
|
|
|
|
|
|
|
class TestVM(qubes.tests.TestEmitter):
|
|
|
|
def __init__(self, app, name, *args, **kwargs):
|
|
|
|
super(TestVM, self).__init__(*args, **kwargs)
|
|
|
|
self.app = app
|
|
|
|
self.name = name
|
|
|
|
self.device = TestDevice(self, 'testdev', 'Description')
|
|
|
|
self.events_enabled = True
|
|
|
|
self.devices = {
|
|
|
|
'testclass': qubes.devices.DeviceCollection(self, 'testclass')
|
|
|
|
}
|
|
|
|
self.app.domains[name] = self
|
|
|
|
self.app.domains[self] = self
|
2017-04-08 06:30:03 +02:00
|
|
|
self.running = False
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@qubes.events.handler('device-list-attached:testclass')
|
2017-04-08 06:30:03 +02:00
|
|
|
def dev_testclass_list_attached(self, event, persistent = False):
|
2016-06-26 04:07:15 +02:00
|
|
|
for vm in self.app.domains:
|
|
|
|
if vm.device.frontend_domain == self:
|
2017-04-15 19:12:39 +02:00
|
|
|
yield (vm.device, {})
|
2016-06-26 04:07:15 +02:00
|
|
|
|
|
|
|
@qubes.events.handler('device-list:testclass')
|
|
|
|
def dev_testclass_list(self, event):
|
|
|
|
yield self.device
|
|
|
|
|
2017-04-08 06:30:03 +02:00
|
|
|
def is_halted(self):
|
|
|
|
return not self.running
|
|
|
|
|
|
|
|
|
2016-06-26 04:07:15 +02:00
|
|
|
|
2016-03-09 14:20:32 +01:00
|
|
|
class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
|
|
|
|
def setUp(self):
|
2016-06-26 04:07:15 +02:00
|
|
|
self.app = TestApp()
|
|
|
|
self.emitter = TestVM(self.app, 'vm')
|
|
|
|
self.app.domains['vm'] = self.emitter
|
|
|
|
self.device = self.emitter.device
|
|
|
|
self.collection = self.emitter.devices['testclass']
|
2017-04-08 06:30:03 +02:00
|
|
|
self.assignment = qubes.devices.DeviceAssignment(
|
2017-05-22 01:09:29 +02:00
|
|
|
backend_domain=self.device.backend_domain,
|
|
|
|
ident=self.device.ident,
|
2017-04-08 06:30:03 +02:00
|
|
|
persistent=True
|
|
|
|
)
|
2016-03-09 14:20:32 +01:00
|
|
|
|
|
|
|
def test_000_init(self):
|
|
|
|
self.assertFalse(self.collection._set)
|
|
|
|
|
|
|
|
def test_001_attach(self):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.collection.attach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-attach:testclass')
|
|
|
|
self.assertEventNotFired(self.emitter, 'device-pre-detach:testclass')
|
|
|
|
self.assertEventNotFired(self.emitter, 'device-detach:testclass')
|
|
|
|
|
|
|
|
def test_002_detach(self):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.collection.attach(self.assignment)
|
|
|
|
self.collection.detach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-attach:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-pre-detach:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-detach:testclass')
|
|
|
|
|
|
|
|
def test_010_empty_detach(self):
|
|
|
|
with self.assertRaises(LookupError):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.collection.detach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
|
|
|
|
def test_011_double_attach(self):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.collection.attach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
|
2017-05-15 21:04:50 +02:00
|
|
|
with self.assertRaises(qubes.devices.DeviceAlreadyAttached):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.collection.attach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
|
|
|
|
def test_012_double_detach(self):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.collection.attach(self.assignment)
|
|
|
|
self.collection.detach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
|
2017-04-08 06:30:03 +02:00
|
|
|
with self.assertRaises(qubes.devices.DeviceNotAttached):
|
|
|
|
self.collection.detach(self.assignment)
|
2016-03-09 14:20:32 +01:00
|
|
|
|
2016-09-03 20:35:11 +02:00
|
|
|
def test_013_list_attached_persistent(self):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.assertEqual(set([]), set(self.collection.persistent()))
|
|
|
|
self.collection.attach(self.assignment)
|
2017-02-23 00:18:54 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-list-attached:testclass')
|
2017-04-08 06:30:03 +02:00
|
|
|
self.assertEqual({self.device}, set(self.collection.persistent()))
|
2016-09-03 20:35:11 +02:00
|
|
|
self.assertEqual({self.device},
|
2017-04-08 06:30:03 +02:00
|
|
|
set(self.collection.persistent()))
|
2016-09-03 20:35:11 +02:00
|
|
|
self.assertEqual(set([]),
|
2017-04-08 06:30:03 +02:00
|
|
|
set(self.collection.attached()))
|
2016-09-03 20:35:11 +02:00
|
|
|
|
|
|
|
def test_014_list_attached_non_persistent(self):
|
2017-04-08 06:30:03 +02:00
|
|
|
self.assignment.persistent = False
|
|
|
|
self.emitter.running = True
|
|
|
|
self.collection.attach(self.assignment)
|
2016-09-03 20:35:11 +02:00
|
|
|
# device-attach event not implemented, so manipulate object manually
|
|
|
|
self.device.frontend_domain = self.emitter
|
|
|
|
self.assertEqual({self.device},
|
|
|
|
set(self.collection.attached()))
|
|
|
|
self.assertEqual(set([]),
|
2017-04-08 06:30:03 +02:00
|
|
|
set(self.collection.persistent()))
|
2016-09-03 20:35:11 +02:00
|
|
|
self.assertEqual({self.device},
|
2017-04-08 06:30:03 +02:00
|
|
|
set(self.collection.attached()))
|
2017-02-23 00:18:54 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-list-attached:testclass')
|
2016-09-03 20:35:11 +02:00
|
|
|
|
|
|
|
def test_015_list_available(self):
|
|
|
|
self.assertEqual({self.device}, set(self.collection))
|
2017-02-23 00:18:54 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-list:testclass')
|
2016-09-03 20:35:11 +02:00
|
|
|
|
2016-03-09 14:20:32 +01:00
|
|
|
|
|
|
|
class TC_01_DeviceManager(qubes.tests.QubesTestCase):
|
|
|
|
def setUp(self):
|
2016-06-26 04:07:15 +02:00
|
|
|
self.app = TestApp()
|
|
|
|
self.emitter = TestVM(self.app, 'vm')
|
2016-03-09 14:20:32 +01:00
|
|
|
self.manager = qubes.devices.DeviceManager(self.emitter)
|
|
|
|
|
|
|
|
def test_000_init(self):
|
|
|
|
self.assertEqual(self.manager, {})
|
|
|
|
|
|
|
|
def test_001_missing(self):
|
2016-06-26 04:07:15 +02:00
|
|
|
device = TestDevice(self.emitter.app.domains['vm'], 'testdev')
|
2017-05-22 01:09:29 +02:00
|
|
|
assignment = qubes.devices.DeviceAssignment(
|
|
|
|
backend_domain=device.backend_domain,
|
|
|
|
ident=device.ident,
|
|
|
|
persistent=True)
|
2017-04-08 06:30:03 +02:00
|
|
|
self.manager['testclass'].attach(assignment)
|
2016-04-03 03:40:41 +02:00
|
|
|
self.assertEventFired(self.emitter, 'device-attach:testclass')
|
2016-03-09 14:20:32 +01:00
|
|
|
|