2014-11-18 17:35:05 +01:00
|
|
|
#!/usr/bin/python2 -O
|
2015-01-19 18:03:23 +01:00
|
|
|
# vim: fileencoding=utf-8
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-19 18:03:23 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 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.
|
|
|
|
#
|
2014-11-18 17:35:05 +01:00
|
|
|
|
|
|
|
import lxml.etree
|
|
|
|
|
2014-12-17 13:29:03 +01:00
|
|
|
import qubes
|
|
|
|
import qubes.events
|
2014-11-18 17:35:05 +01:00
|
|
|
import qubes.vm
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
import qubes.tests
|
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
|
2014-12-17 13:29:03 +01:00
|
|
|
def setUp(self):
|
2015-01-05 15:39:14 +01:00
|
|
|
self.emitter = qubes.tests.TestEmitter()
|
2014-12-17 13:29:03 +01:00
|
|
|
self.collection = qubes.vm.DeviceCollection(self.emitter, 'testclass')
|
|
|
|
|
|
|
|
def test_000_init(self):
|
|
|
|
self.assertFalse(self.collection._set)
|
|
|
|
|
|
|
|
def test_001_attach(self):
|
|
|
|
self.collection.attach('testdev')
|
2015-01-05 15:39:14 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-pre-attached:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-attached:testclass')
|
|
|
|
self.assertEventNotFired(self.emitter, 'device-pre-detached:testclass')
|
|
|
|
self.assertEventNotFired(self.emitter, 'device-detached:testclass')
|
2014-12-17 13:29:03 +01:00
|
|
|
|
|
|
|
def test_002_detach(self):
|
|
|
|
self.collection.attach('testdev')
|
|
|
|
self.collection.detach('testdev')
|
2015-01-05 15:39:14 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-pre-attached:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-attached:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-pre-detached:testclass')
|
|
|
|
self.assertEventFired(self.emitter, 'device-detached:testclass')
|
2014-12-17 13:29:03 +01:00
|
|
|
|
|
|
|
def test_010_empty_detach(self):
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
self.collection.detach('testdev')
|
|
|
|
|
|
|
|
def test_011_double_attach(self):
|
|
|
|
self.collection.attach('testdev')
|
|
|
|
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
self.collection.attach('testdev')
|
|
|
|
|
|
|
|
def test_012_double_detach(self):
|
|
|
|
self.collection.attach('testdev')
|
|
|
|
self.collection.detach('testdev')
|
|
|
|
|
|
|
|
with self.assertRaises(LookupError):
|
|
|
|
self.collection.detach('testdev')
|
|
|
|
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
class TC_01_DeviceManager(qubes.tests.QubesTestCase):
|
2014-12-17 13:29:03 +01:00
|
|
|
def setUp(self):
|
2015-01-05 15:39:14 +01:00
|
|
|
self.emitter = qubes.tests.TestEmitter()
|
2014-12-17 13:29:03 +01:00
|
|
|
self.manager = qubes.vm.DeviceManager(self.emitter)
|
|
|
|
|
|
|
|
def test_000_init(self):
|
|
|
|
self.assertEqual(self.manager, {})
|
|
|
|
|
|
|
|
def test_001_missing(self):
|
|
|
|
self.manager['testclass'].attach('testdev')
|
2015-01-05 15:39:14 +01:00
|
|
|
self.assertEventFired(self.emitter, 'device-attached:testclass')
|
2014-12-17 13:29:03 +01:00
|
|
|
|
|
|
|
|
2014-11-18 17:35:05 +01:00
|
|
|
class TestVM(qubes.vm.BaseVM):
|
2014-12-05 14:58:05 +01:00
|
|
|
qid = qubes.property('qid', type=int)
|
|
|
|
name = qubes.property('name')
|
|
|
|
testprop = qubes.property('testprop')
|
|
|
|
testlabel = qubes.property('testlabel')
|
|
|
|
defaultprop = qubes.property('defaultprop', default='defaultvalue')
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
class TC_10_BaseVM(qubes.tests.QubesTestCase):
|
2014-11-18 17:35:05 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.xml = lxml.etree.XML('''
|
|
|
|
<qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
|
|
|
|
<labels>
|
|
|
|
<label id="label-1" color="#cc0000">red</label>
|
|
|
|
</labels>
|
|
|
|
|
|
|
|
<domains>
|
|
|
|
<domain id="domain-1" class="TestVM">
|
|
|
|
<properties>
|
2014-12-05 14:58:05 +01:00
|
|
|
<property name="qid">1</property>
|
|
|
|
<property name="name">domain1</property>
|
2014-11-18 17:35:05 +01:00
|
|
|
<property name="testprop">testvalue</property>
|
|
|
|
<property name="testlabel" ref="label-1" />
|
|
|
|
</properties>
|
|
|
|
|
|
|
|
<tags>
|
|
|
|
<tag name="testtag">tagvalue</tag>
|
|
|
|
</tags>
|
|
|
|
|
2016-03-03 13:03:27 +01:00
|
|
|
<features>
|
|
|
|
<feature name="testfeature_none"/>
|
|
|
|
<feature name="testfeature_empty"></feature>
|
|
|
|
<feature name="testfeature_aqq">aqq</feature>
|
|
|
|
</features>
|
2014-11-18 17:35:05 +01:00
|
|
|
|
|
|
|
<devices class="pci">
|
|
|
|
<device>00:11.22</device>
|
|
|
|
</devices>
|
|
|
|
|
|
|
|
<devices class="usb" />
|
|
|
|
<devices class="audio-in" />
|
|
|
|
<devices class="firewire" />
|
|
|
|
<devices class="i2c" />
|
|
|
|
<devices class="isa" />
|
|
|
|
</domain>
|
|
|
|
</domains>
|
|
|
|
</qubes>
|
|
|
|
''')
|
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
def test_000_load(self):
|
2014-11-18 17:35:05 +01:00
|
|
|
node = self.xml.xpath('//domain')[0]
|
2015-01-21 15:24:29 +01:00
|
|
|
vm = TestVM(None, node)
|
|
|
|
vm.load_properties(load_stage=None)
|
2014-11-18 17:35:05 +01:00
|
|
|
|
2014-12-05 14:58:05 +01:00
|
|
|
self.assertEqual(vm.qid, 1)
|
|
|
|
self.assertEqual(vm.testprop, 'testvalue')
|
2014-11-18 17:35:05 +01:00
|
|
|
self.assertEqual(vm.testprop, 'testvalue')
|
|
|
|
self.assertEqual(vm.testlabel, 'label-1')
|
|
|
|
self.assertEqual(vm.defaultprop, 'defaultvalue')
|
|
|
|
self.assertEqual(vm.tags, {'testtag': 'tagvalue'})
|
2016-03-03 13:03:27 +01:00
|
|
|
self.assertEqual(vm.features, {
|
|
|
|
'testfeature_none': None,
|
|
|
|
'testfeature_empty': '',
|
|
|
|
'testfeature_aqq': 'aqq',
|
2014-11-18 17:35:05 +01:00
|
|
|
})
|
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
self.assertItemsEqual(vm.devices.keys(), ('pci',))
|
|
|
|
self.assertItemsEqual(vm.devices['pci'], ('00:11.22',))
|
|
|
|
|
2015-01-13 23:17:18 +01:00
|
|
|
self.assertXMLIsValid(vm.__xml__(), 'domain.rng')
|
2014-12-05 14:58:05 +01:00
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
def test_001_nxproperty(self):
|
2014-11-18 17:35:05 +01:00
|
|
|
xml = lxml.etree.XML('''
|
|
|
|
<qubes version="3">
|
|
|
|
<domains>
|
|
|
|
<domain id="domain-1" class="TestVM">
|
|
|
|
<properties>
|
2015-01-21 15:24:29 +01:00
|
|
|
<property name="qid">1</property>
|
|
|
|
<property name="name">domain1</property>
|
2014-11-18 17:35:05 +01:00
|
|
|
<property name="nxproperty">nxvalue</property>
|
|
|
|
</properties>
|
|
|
|
</domain>
|
|
|
|
</domains>
|
|
|
|
</qubes>
|
|
|
|
''')
|
|
|
|
|
|
|
|
node = xml.xpath('//domain')[0]
|
|
|
|
|
2015-01-21 15:24:29 +01:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
TestVM(None, node)
|