qubesvm.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access
  4. #
  5. # The Qubes OS Project, https://www.qubes-os.org/
  6. #
  7. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  8. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import unittest
  25. import uuid
  26. import qubes
  27. import qubes.exc
  28. import qubes.config
  29. import qubes.vm.qubesvm
  30. import qubes.tests
  31. class TestApp(object):
  32. labels = {1: qubes.Label(1, '0xcc0000', 'red')}
  33. class TestProp(object):
  34. # pylint: disable=too-few-public-methods
  35. __name__ = 'testprop'
  36. class TestVM(object):
  37. # pylint: disable=too-few-public-methods
  38. app = TestApp()
  39. def __init__(self):
  40. self.running = False
  41. self.installed_by_rpm = False
  42. def is_running(self):
  43. return self.running
  44. class TC_00_setters(qubes.tests.QubesTestCase):
  45. def setUp(self):
  46. self.vm = TestVM()
  47. self.prop = TestProp()
  48. def test_000_setter_qid(self):
  49. self.assertEqual(
  50. qubes.vm.qubesvm._setter_qid(self.vm, self.prop, 5), 5)
  51. def test_001_setter_qid_lt_0(self):
  52. with self.assertRaises(ValueError):
  53. qubes.vm.qubesvm._setter_qid(self.vm, self.prop, -1)
  54. def test_002_setter_qid_gt_max(self):
  55. with self.assertRaises(ValueError):
  56. qubes.vm.qubesvm._setter_qid(self.vm,
  57. self.prop, qubes.config.max_qid + 5)
  58. def test_010_setter_name(self):
  59. self.assertEqual(
  60. qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'test_name-1'),
  61. 'test_name-1')
  62. def test_011_setter_name_not_a_string(self):
  63. # pylint: disable=invalid-name
  64. with self.assertRaises(TypeError):
  65. qubes.vm.qubesvm._setter_name(self.vm, self.prop, False)
  66. def test_012_setter_name_longer_than_31(self):
  67. # pylint: disable=invalid-name
  68. with self.assertRaises(ValueError):
  69. qubes.vm.qubesvm._setter_name(self.vm, self.prop, 't' * 32)
  70. def test_013_setter_name_illegal_character(self):
  71. # pylint: disable=invalid-name
  72. with self.assertRaises(ValueError):
  73. qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'test#')
  74. def test_014_setter_name_first_not_letter(self):
  75. # pylint: disable=invalid-name
  76. with self.assertRaises(ValueError):
  77. qubes.vm.qubesvm._setter_name(self.vm, self.prop, '1test')
  78. def test_015_setter_name_running(self):
  79. self.vm.running = True
  80. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  81. qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'testname')
  82. def test_016_setter_name_installed_by_rpm(self):
  83. # pylint: disable=invalid-name
  84. self.vm.installed_by_rpm = True
  85. with self.assertRaises(qubes.exc.QubesException):
  86. qubes.vm.qubesvm._setter_name(self.vm, self.prop, 'testname')
  87. @unittest.skip('test not implemented')
  88. def test_020_setter_kernel(self):
  89. pass
  90. def test_030_setter_label_object(self):
  91. label = TestApp.labels[1]
  92. self.assertIs(label,
  93. qubes.vm.qubesvm._setter_label(self.vm, self.prop, label))
  94. def test_031_setter_label_getitem(self):
  95. label = TestApp.labels[1]
  96. self.assertIs(label,
  97. qubes.vm.qubesvm._setter_label(self.vm, self.prop, 'label-1'))
  98. # there is no check for self.app.get_label()
  99. class TC_90_QubesVM(qubes.tests.QubesTestCase):
  100. def setUp(self):
  101. super(TC_90_QubesVM, self).setUp()
  102. self.app = qubes.tests.vm.TestApp()
  103. def get_vm(self, **kwargs):
  104. return qubes.vm.qubesvm.QubesVM(self.app, None,
  105. qid=1, name=qubes.tests.VMPREFIX + 'test',
  106. **kwargs)
  107. def test_000_init(self):
  108. self.get_vm()
  109. def test_001_init_no_qid_or_name(self):
  110. with self.assertRaises(AssertionError):
  111. qubes.vm.qubesvm.QubesVM(self.app, None,
  112. name=qubes.tests.VMPREFIX + 'test')
  113. with self.assertRaises(AssertionError):
  114. qubes.vm.qubesvm.QubesVM(self.app, None,
  115. qid=1)
  116. def test_003_init_fire_domain_init(self):
  117. class TestVM2(qubes.vm.qubesvm.QubesVM):
  118. event_fired = False
  119. @qubes.events.handler('domain-init')
  120. def on_domain_init(self, event): # pylint: disable=unused-argument
  121. self.__class__.event_fired = True
  122. TestVM2(self.app, None, qid=1, name=qubes.tests.VMPREFIX + 'test')
  123. self.assertTrue(TestVM2.event_fired)
  124. def test_004_uuid_autogen(self):
  125. vm = self.get_vm()
  126. self.assertTrue(hasattr(vm, 'uuid'))
  127. def test_100_qid(self):
  128. vm = self.get_vm()
  129. self.assertIsInstance(vm.qid, int)
  130. with self.assertRaises(AttributeError):
  131. vm.qid = 2
  132. def test_110_name(self):
  133. vm = self.get_vm()
  134. self.assertIsInstance(vm.name, basestring)
  135. def test_120_uuid(self):
  136. my_uuid = uuid.uuid4()
  137. vm = self.get_vm(uuid=my_uuid)
  138. self.assertIsInstance(vm.uuid, uuid.UUID)
  139. self.assertIs(vm.uuid, my_uuid)
  140. with self.assertRaises(AttributeError):
  141. vm.uuid = uuid.uuid4()
  142. # label = qubes.property('label',
  143. # netvm = qubes.VMProperty('netvm', load_stage=4, allow_none=True,
  144. # conf_file = qubes.property('conf_file', type=str,
  145. # firewall_conf = qubes.property('firewall_conf', type=str,
  146. # installed_by_rpm = qubes.property('installed_by_rpm',
  147. # memory = qubes.property('memory', type=int,
  148. # maxmem = qubes.property('maxmem', type=int, default=None,
  149. # internal = qubes.property('internal', default=False,
  150. # vcpus = qubes.property('vcpus',
  151. # kernel = qubes.property('kernel', type=str,
  152. # kernelopts = qubes.property('kernelopts', type=str, load_stage=4,
  153. # mac = qubes.property('mac', type=str,
  154. # debug = qubes.property('debug', type=bool, default=False,
  155. # default_user = qubes.property('default_user', type=str,
  156. # qrexec_timeout = qubes.property('qrexec_timeout', type=int, default=60,
  157. # autostart = qubes.property('autostart', default=False,
  158. # include_in_backups = qubes.property('include_in_backups', default=True,
  159. # backup_content = qubes.property('backup_content', default=False,
  160. # backup_size = qubes.property('backup_size', type=int, default=0,
  161. # backup_path = qubes.property('backup_path', type=str, default='',
  162. # backup_timestamp = qubes.property('backup_timestamp', default=None,
  163. @qubes.tests.skipUnlessDom0
  164. def test_200_create_on_disk(self):
  165. vm = self.get_vm()
  166. vm.create_on_disk()
  167. @unittest.skip('test not implemented')
  168. def test_300_rename(self):
  169. pass