init.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # pylint: disable=protected-access
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import lxml.etree
  23. import qubes
  24. import qubes.events
  25. import qubes.vm
  26. import qubes.tests
  27. class TestVMM(object):
  28. def __init__(self):
  29. super(TestVMM, self).__init__()
  30. self.offline_mode = True
  31. class TestApp(object):
  32. def __init__(self):
  33. super(TestApp, self).__init__()
  34. self.domains = {}
  35. self.vmm = TestVMM()
  36. class TestVM(qubes.vm.BaseVM):
  37. qid = qubes.property('qid', type=int)
  38. name = qubes.property('name')
  39. testprop = qubes.property('testprop')
  40. testlabel = qubes.property('testlabel')
  41. defaultprop = qubes.property('defaultprop', default='defaultvalue')
  42. class TC_10_BaseVM(qubes.tests.QubesTestCase):
  43. def setUp(self):
  44. super().setUp()
  45. self.xml = lxml.etree.XML('''
  46. <qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
  47. <labels>
  48. <label id="label-1" color="#cc0000">red</label>
  49. </labels>
  50. <domains>
  51. <domain id="domain-1" class="TestVM">
  52. <properties>
  53. <property name="qid">1</property>
  54. <property name="name">domain1</property>
  55. <property name="testprop">testvalue</property>
  56. <property name="testlabel" ref="label-1" />
  57. </properties>
  58. <tags>
  59. <tag name="testtag"/>
  60. </tags>
  61. <features>
  62. <feature name="testfeature_empty"></feature>
  63. <feature name="testfeature_aqq">aqq</feature>
  64. </features>
  65. <devices class="pci">
  66. <device backend-domain="domain1" id="00_11.22">
  67. <option name="no-strict-reset">True</option>
  68. </device>
  69. </devices>
  70. <devices class="usb" />
  71. <devices class="audio-in" />
  72. <devices class="firewire" />
  73. <devices class="i2c" />
  74. <devices class="isa" />
  75. </domain>
  76. </domains>
  77. </qubes>
  78. ''')
  79. def test_000_load(self):
  80. node = self.xml.xpath('//domain')[0]
  81. vm = TestVM(TestApp(), node)
  82. vm.app.domains['domain1'] = vm
  83. vm.load_properties(load_stage=None)
  84. vm.load_extras()
  85. self.assertEqual(vm.qid, 1)
  86. self.assertEqual(vm.testprop, 'testvalue')
  87. self.assertEqual(vm.testprop, 'testvalue')
  88. self.assertEqual(vm.testlabel, 'label-1')
  89. self.assertEqual(vm.defaultprop, 'defaultvalue')
  90. self.assertEqual(vm.tags, {'testtag'})
  91. self.assertEqual(vm.features, {
  92. 'testfeature_empty': '',
  93. 'testfeature_aqq': 'aqq',
  94. })
  95. self.assertCountEqual(vm.devices.keys(), ('pci',))
  96. self.assertCountEqual(list(vm.devices['pci'].persistent()),
  97. [qubes.ext.pci.PCIDevice(vm, '00_11.22')])
  98. assignments = list(vm.devices['pci'].assignments())
  99. self.assertEqual(len(assignments), 1)
  100. self.assertEqual(assignments[0].options, {'no-strict-reset': 'True'})
  101. self.assertEqual(assignments[0].persistent, True)
  102. self.assertXMLIsValid(vm.__xml__(), 'domain.rng')
  103. def test_001_nxproperty(self):
  104. xml = lxml.etree.XML('''
  105. <qubes version="3">
  106. <domains>
  107. <domain id="domain-1" class="TestVM">
  108. <properties>
  109. <property name="qid">1</property>
  110. <property name="name">domain1</property>
  111. <property name="nxproperty">nxvalue</property>
  112. </properties>
  113. </domain>
  114. </domains>
  115. </qubes>
  116. ''')
  117. node = xml.xpath('//domain')[0]
  118. with self.assertRaises(TypeError):
  119. TestVM(None, node)
  120. def test_002_save_nxproperty(self):
  121. vm = TestVM(None, None, qid=1, name='testvm')
  122. vm.nxproperty = 'value'
  123. xml = vm.__xml__()
  124. self.assertNotIn('nxproperty', xml)
  125. class TC_20_Tags(qubes.tests.QubesTestCase):
  126. def setUp(self):
  127. super(TC_20_Tags, self).setUp()
  128. self.vm = qubes.tests.TestEmitter()
  129. self.tags = qubes.vm.Tags(self.vm)
  130. def test_000_add(self):
  131. self.tags.add('testtag')
  132. self.assertEventFired(self.vm, 'domain-tag-add',
  133. kwargs={'tag': 'testtag'})
  134. def test_001_add_existing(self):
  135. self.tags.add('testtag')
  136. self.vm.fired_events.clear()
  137. self.tags.add('testtag')
  138. self.assertEventNotFired(self.vm, 'domain-tag-add')
  139. def test_002_remove(self):
  140. self.tags.add('testtag')
  141. self.vm.fired_events.clear()
  142. self.tags.remove('testtag')
  143. self.assertEventFired(self.vm, 'domain-tag-delete',
  144. kwargs={'tag': 'testtag'})
  145. def test_003_remove_not_present(self):
  146. with self.assertRaises(KeyError):
  147. self.tags.remove('testtag')
  148. self.assertEventNotFired(self.vm, 'domain-tag-delete')
  149. def test_004_discard_not_present(self):
  150. with self.assertNotRaises(KeyError):
  151. self.tags.discard('testtag')
  152. self.assertEventNotFired(self.vm, 'domain-tag-delete')
  153. def test_005_discard_present(self):
  154. self.tags.add('testtag')
  155. with self.assertNotRaises(KeyError):
  156. self.tags.discard('testtag')
  157. self.assertEventFired(self.vm, 'domain-tag-delete',
  158. kwargs={'tag': 'testtag'})
  159. def test_006_clear(self):
  160. self.tags.add('testtag')
  161. self.tags.add('testtag2')
  162. self.vm.fired_events.clear()
  163. self.tags.clear()
  164. self.assertEventFired(self.vm, 'domain-tag-delete',
  165. kwargs={'tag': 'testtag'})
  166. self.assertEventFired(self.vm, 'domain-tag-delete',
  167. kwargs={'tag': 'testtag2'})
  168. def test_007_update(self):
  169. self.tags.add('testtag')
  170. self.tags.add('testtag2')
  171. self.vm.fired_events.clear()
  172. self.tags.update(('testtag2', 'testtag3'))
  173. self.assertEventFired(self.vm, 'domain-tag-add',
  174. kwargs={'tag': 'testtag3'})
  175. self.assertEventNotFired(self.vm, 'domain-tag-add',
  176. kwargs={'tag': 'testtag2'})
  177. class TC_21_Features(qubes.tests.QubesTestCase):
  178. def setUp(self):
  179. super(TC_21_Features, self).setUp()
  180. self.vm = qubes.tests.TestEmitter()
  181. self.features = qubes.vm.Features(self.vm)
  182. def test_000_set(self):
  183. self.features['testfeature'] = 'value'
  184. self.assertEventFired(self.vm, 'domain-feature-set',
  185. kwargs={'feature': 'testfeature', 'value': 'value'})
  186. def test_001_set_existing(self):
  187. self.features['test'] = 'oldvalue'
  188. self.vm.fired_events.clear()
  189. self.features['test'] = 'value'
  190. self.assertEventFired(self.vm, 'domain-feature-set',
  191. kwargs={'feature': 'test', 'value': 'value', 'oldvalue':
  192. 'oldvalue'})
  193. def test_002_unset(self):
  194. self.features['test'] = 'value'
  195. self.vm.fired_events.clear()
  196. del self.features['test']
  197. self.assertEventFired(self.vm, 'domain-feature-delete',
  198. kwargs={'feature': 'test'})
  199. def test_003_unset_not_present(self):
  200. with self.assertRaises(KeyError):
  201. del self.features['test']
  202. self.assertEventNotFired(self.vm, 'domain-feature-delete')
  203. def test_004_set_bool_true(self):
  204. self.features['test'] = True
  205. self.assertTrue(self.features['test'])
  206. self.assertEventFired(self.vm, 'domain-feature-set',
  207. kwargs={'feature': 'test', 'value': '1'})
  208. def test_005_set_bool_false(self):
  209. self.features['test'] = False
  210. self.assertFalse(self.features['test'])
  211. self.assertEventFired(self.vm, 'domain-feature-set',
  212. kwargs={'feature': 'test', 'value': ''})
  213. def test_006_set_int(self):
  214. self.features['test'] = 123
  215. self.assertEventFired(self.vm, 'domain-feature-set',
  216. kwargs={'feature': 'test', 'value': '123'})
  217. def test_007_clear(self):
  218. self.features['test'] = 'value1'
  219. self.features['test2'] = 'value2'
  220. self.vm.fired_events.clear()
  221. self.features.clear()
  222. self.assertEventFired(self.vm, 'domain-feature-delete',
  223. kwargs={'feature': 'test'})
  224. self.assertEventFired(self.vm, 'domain-feature-delete',
  225. kwargs={'feature': 'test2'})
  226. def test_008_update(self):
  227. self.features['test'] = 'value'
  228. self.features['test2'] = 'value2'
  229. self.vm.fired_events.clear()
  230. self.features.update({'test2': 'value3', 'test3': 'value4'})
  231. self.assertEqual(self.features['test2'], 'value3')
  232. self.assertEqual(self.features['test3'], 'value4')
  233. self.assertEqual(self.features['test'], 'value')
  234. self.assertEventFired(self.vm, 'domain-feature-set',
  235. kwargs={'feature': 'test2', 'value': 'value3',
  236. 'oldvalue': 'value2'})
  237. self.assertEventFired(self.vm, 'domain-feature-set',
  238. kwargs={'feature': 'test3', 'value': 'value4'})