2014-12-29 12:46:16 +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-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
import unittest
|
2015-12-30 01:48:37 +01:00
|
|
|
import uuid
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
import qubes
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.exc
|
2015-01-20 14:09:47 +01:00
|
|
|
import qubes.config
|
2014-12-29 12:46:16 +01:00
|
|
|
import qubes.vm.qubesvm
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
import qubes.tests
|
|
|
|
|
2015-12-30 01:48:37 +01:00
|
|
|
class TestApp(object):
|
|
|
|
labels = {1: qubes.Label(1, '0xcc0000', 'red')}
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
class TestProp(object):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2014-12-29 12:46:16 +01:00
|
|
|
__name__ = 'testprop'
|
|
|
|
|
|
|
|
class TestVM(object):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2015-12-30 01:48:37 +01:00
|
|
|
app = TestApp()
|
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.running = False
|
|
|
|
self.installed_by_rpm = False
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
return self.running
|
|
|
|
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
class TC_00_setters(qubes.tests.QubesTestCase):
|
2014-12-29 12:46:16 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.vm = TestVM()
|
|
|
|
self.prop = TestProp()
|
|
|
|
|
|
|
|
|
|
|
|
def test_000_setter_qid(self):
|
|
|
|
self.assertEqual(
|
|
|
|
qubes.vm.qubesvm._setter_qid(self.vm, self.prop, 5), 5)
|
|
|
|
|
|
|
|
def test_001_setter_qid_lt_0(self):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
qubes.vm.qubesvm._setter_qid(self.vm, self.prop, -1)
|
|
|
|
|
|
|
|
def test_002_setter_qid_gt_max(self):
|
|
|
|
with self.assertRaises(ValueError):
|
2015-01-20 14:09:47 +01:00
|
|
|
qubes.vm.qubesvm._setter_qid(self.vm,
|
|
|
|
self.prop, qubes.config.max_qid + 5)
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_010_setter_name(self):
|
|
|
|
self.assertEqual(
|
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'test_name-1'),
|
|
|
|
'test_name-1')
|
|
|
|
|
2015-12-29 20:35:04 +01:00
|
|
|
def test_011_setter_name_not_a_string(self):
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, False)
|
|
|
|
|
|
|
|
def test_012_setter_name_longer_than_31(self):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=invalid-name
|
2014-12-29 12:46:16 +01:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, 't' * 32)
|
|
|
|
|
2015-12-29 20:35:04 +01:00
|
|
|
def test_013_setter_name_illegal_character(self):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=invalid-name
|
2014-12-29 12:46:16 +01:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'test#')
|
|
|
|
|
2015-12-29 20:35:04 +01:00
|
|
|
def test_014_setter_name_first_not_letter(self):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=invalid-name
|
2014-12-29 12:46:16 +01:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, '1test')
|
|
|
|
|
2015-12-29 20:35:04 +01:00
|
|
|
def test_015_setter_name_running(self):
|
2014-12-29 12:46:16 +01:00
|
|
|
self.vm.running = True
|
2015-10-14 22:02:11 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
|
2014-12-29 12:46:16 +01:00
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'testname')
|
|
|
|
|
2015-12-29 20:35:04 +01:00
|
|
|
def test_016_setter_name_installed_by_rpm(self):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=invalid-name
|
2014-12-29 12:46:16 +01:00
|
|
|
self.vm.installed_by_rpm = True
|
2015-10-14 22:02:11 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesException):
|
2014-12-29 12:46:16 +01:00
|
|
|
qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'testname')
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skip('test not implemented')
|
|
|
|
def test_020_setter_kernel(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-12-30 01:48:37 +01:00
|
|
|
def test_030_setter_label_object(self):
|
|
|
|
label = TestApp.labels[1]
|
|
|
|
self.assertIs(label,
|
|
|
|
qubes.vm.qubesvm._setter_label(self.vm, self.prop, label))
|
|
|
|
|
|
|
|
def test_031_setter_label_getitem(self):
|
|
|
|
label = TestApp.labels[1]
|
|
|
|
self.assertIs(label,
|
|
|
|
qubes.vm.qubesvm._setter_label(self.vm, self.prop, 'label-1'))
|
|
|
|
|
|
|
|
# there is no check for self.app.get_label()
|
|
|
|
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
class TC_90_QubesVM(qubes.tests.QubesTestCase):
|
2015-12-29 20:35:04 +01:00
|
|
|
def setUp(self):
|
|
|
|
super(TC_90_QubesVM, self).setUp()
|
|
|
|
self.app = qubes.tests.vm.TestApp()
|
|
|
|
|
2015-12-30 01:48:37 +01:00
|
|
|
def get_vm(self, **kwargs):
|
|
|
|
return qubes.vm.qubesvm.QubesVM(self.app, None,
|
|
|
|
qid=1, name=qubes.tests.VMPREFIX + 'test',
|
|
|
|
**kwargs)
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
def test_000_init(self):
|
2015-12-30 01:48:37 +01:00
|
|
|
self.get_vm()
|
2015-12-29 20:35:04 +01:00
|
|
|
|
|
|
|
def test_001_init_no_qid_or_name(self):
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
qubes.vm.qubesvm.QubesVM(self.app, None,
|
|
|
|
name=qubes.tests.VMPREFIX + 'test')
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
qubes.vm.qubesvm.QubesVM(self.app, None,
|
|
|
|
qid=1)
|
|
|
|
|
|
|
|
def test_003_init_fire_domain_init(self):
|
|
|
|
class TestVM2(qubes.vm.qubesvm.QubesVM):
|
|
|
|
event_fired = False
|
|
|
|
@qubes.events.handler('domain-init')
|
2015-12-29 22:04:00 +01:00
|
|
|
def on_domain_init(self, event): # pylint: disable=unused-argument
|
2015-12-29 20:35:04 +01:00
|
|
|
self.__class__.event_fired = True
|
|
|
|
|
2015-12-29 22:04:00 +01:00
|
|
|
TestVM2(self.app, None, qid=1, name=qubes.tests.VMPREFIX + 'test')
|
2015-12-29 20:35:04 +01:00
|
|
|
self.assertTrue(TestVM2.event_fired)
|
2015-12-30 01:48:37 +01:00
|
|
|
|
|
|
|
def test_004_uuid_autogen(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
self.assertTrue(hasattr(vm, 'uuid'))
|
|
|
|
|
|
|
|
def test_100_qid(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
self.assertIsInstance(vm.qid, int)
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
vm.qid = 2
|
|
|
|
|
|
|
|
def test_110_name(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
self.assertIsInstance(vm.name, basestring)
|
|
|
|
|
|
|
|
def test_120_uuid(self):
|
|
|
|
my_uuid = uuid.uuid4()
|
|
|
|
vm = self.get_vm(uuid=my_uuid)
|
|
|
|
self.assertIsInstance(vm.uuid, uuid.UUID)
|
|
|
|
self.assertIs(vm.uuid, my_uuid)
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
vm.uuid = uuid.uuid4()
|
|
|
|
|
|
|
|
# label = qubes.property('label',
|
|
|
|
# netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
|
|
|
|
# conf_file = qubes.property('conf_file', type=str,
|
|
|
|
# firewall_conf = qubes.property('firewall_conf', type=str,
|
|
|
|
# installed_by_rpm = qubes.property('installed_by_rpm',
|
|
|
|
# memory = qubes.property('memory', type=int,
|
|
|
|
# maxmem = qubes.property('maxmem', type=int, default=None,
|
|
|
|
# internal = qubes.property('internal', default=False,
|
|
|
|
# vcpus = qubes.property('vcpus',
|
|
|
|
# kernel = qubes.property('kernel', type=str,
|
|
|
|
# kernelopts = qubes.property('kernelopts', type=str, load_stage=4,
|
|
|
|
# mac = qubes.property('mac', type=str,
|
|
|
|
# debug = qubes.property('debug', type=bool, default=False,
|
|
|
|
# default_user = qubes.property('default_user', type=str,
|
|
|
|
# qrexec_timeout = qubes.property('qrexec_timeout', type=int, default=60,
|
|
|
|
# autostart = qubes.property('autostart', default=False,
|
|
|
|
# include_in_backups = qubes.property('include_in_backups', default=True,
|
|
|
|
# backup_content = qubes.property('backup_content', default=False,
|
|
|
|
# backup_size = qubes.property('backup_size', type=int, default=0,
|
|
|
|
# backup_path = qubes.property('backup_path', type=str, default='',
|
|
|
|
# backup_timestamp = qubes.property('backup_timestamp', default=None,
|
|
|
|
|
|
|
|
@qubes.tests.skipUnlessDom0
|
|
|
|
def test_200_create_on_disk(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
vm.create_on_disk()
|
|
|
|
|
|
|
|
@unittest.skip('test not implemented')
|
|
|
|
def test_300_rename(self):
|
|
|
|
pass
|