init.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. self.xml = lxml.etree.XML('''
  45. <qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
  46. <labels>
  47. <label id="label-1" color="#cc0000">red</label>
  48. </labels>
  49. <domains>
  50. <domain id="domain-1" class="TestVM">
  51. <properties>
  52. <property name="qid">1</property>
  53. <property name="name">domain1</property>
  54. <property name="testprop">testvalue</property>
  55. <property name="testlabel" ref="label-1" />
  56. </properties>
  57. <tags>
  58. <tag name="testtag"/>
  59. </tags>
  60. <features>
  61. <feature name="testfeature_empty"></feature>
  62. <feature name="testfeature_aqq">aqq</feature>
  63. </features>
  64. <devices class="pci">
  65. <device backend-domain="domain1" id="00:11.22"/>
  66. </devices>
  67. <devices class="usb" />
  68. <devices class="audio-in" />
  69. <devices class="firewire" />
  70. <devices class="i2c" />
  71. <devices class="isa" />
  72. </domain>
  73. </domains>
  74. </qubes>
  75. ''')
  76. def test_000_load(self):
  77. node = self.xml.xpath('//domain')[0]
  78. vm = TestVM(TestApp(), node)
  79. vm.app.domains['domain1'] = vm
  80. vm.load_properties(load_stage=None)
  81. vm.load_extras()
  82. self.assertEqual(vm.qid, 1)
  83. self.assertEqual(vm.testprop, 'testvalue')
  84. self.assertEqual(vm.testprop, 'testvalue')
  85. self.assertEqual(vm.testlabel, 'label-1')
  86. self.assertEqual(vm.defaultprop, 'defaultvalue')
  87. self.assertEqual(vm.tags, {'testtag'})
  88. self.assertEqual(vm.features, {
  89. 'testfeature_empty': '',
  90. 'testfeature_aqq': 'aqq',
  91. })
  92. self.assertCountEqual(vm.devices.keys(), ('pci',))
  93. self.assertCountEqual(list(vm.devices['pci'].persistent()),
  94. [qubes.ext.pci.PCIDevice(vm, '00:11.22')])
  95. self.assertXMLIsValid(vm.__xml__(), 'domain.rng')
  96. def test_001_nxproperty(self):
  97. xml = lxml.etree.XML('''
  98. <qubes version="3">
  99. <domains>
  100. <domain id="domain-1" class="TestVM">
  101. <properties>
  102. <property name="qid">1</property>
  103. <property name="name">domain1</property>
  104. <property name="nxproperty">nxvalue</property>
  105. </properties>
  106. </domain>
  107. </domains>
  108. </qubes>
  109. ''')
  110. node = xml.xpath('//domain')[0]
  111. with self.assertRaises(TypeError):
  112. TestVM(None, node)
  113. def test_002_save_nxproperty(self):
  114. vm = TestVM(None, None, qid=1, name='testvm')
  115. vm.nxproperty = 'value'
  116. xml = vm.__xml__()
  117. self.assertNotIn('nxproperty', xml)
  118. class TC_20_Tags(qubes.tests.QubesTestCase):
  119. def setUp(self):
  120. super(TC_20_Tags, self).setUp()
  121. self.vm = qubes.tests.TestEmitter()
  122. self.tags = qubes.vm.Tags(self.vm)
  123. def test_000_add(self):
  124. self.tags.add('testtag')
  125. self.assertEventFired(self.vm, 'domain-tag-add',
  126. kwargs={'tag': 'testtag'})
  127. def test_001_add_existing(self):
  128. self.tags.add('testtag')
  129. self.vm.fired_events.clear()
  130. self.tags.add('testtag')
  131. self.assertEventNotFired(self.vm, 'domain-tag-add')
  132. def test_002_remove(self):
  133. self.tags.add('testtag')
  134. self.vm.fired_events.clear()
  135. self.tags.remove('testtag')
  136. self.assertEventFired(self.vm, 'domain-tag-delete',
  137. kwargs={'tag': 'testtag'})
  138. def test_003_remove_not_present(self):
  139. with self.assertRaises(KeyError):
  140. self.tags.remove('testtag')
  141. self.assertEventNotFired(self.vm, 'domain-tag-delete')
  142. def test_004_discard_not_present(self):
  143. with self.assertNotRaises(KeyError):
  144. self.tags.discard('testtag')
  145. self.assertEventNotFired(self.vm, 'domain-tag-delete')
  146. def test_005_discard_present(self):
  147. self.tags.add('testtag')
  148. with self.assertNotRaises(KeyError):
  149. self.tags.discard('testtag')
  150. self.assertEventFired(self.vm, 'domain-tag-delete',
  151. kwargs={'tag': 'testtag'})
  152. def test_006_clear(self):
  153. self.tags.add('testtag')
  154. self.tags.add('testtag2')
  155. self.vm.fired_events.clear()
  156. self.tags.clear()
  157. self.assertEventFired(self.vm, 'domain-tag-delete',
  158. kwargs={'tag': 'testtag'})
  159. self.assertEventFired(self.vm, 'domain-tag-delete',
  160. kwargs={'tag': 'testtag2'})
  161. def test_007_update(self):
  162. self.tags.add('testtag')
  163. self.tags.add('testtag2')
  164. self.vm.fired_events.clear()
  165. self.tags.update(('testtag2', 'testtag3'))
  166. self.assertEventFired(self.vm, 'domain-tag-add',
  167. kwargs={'tag': 'testtag3'})
  168. self.assertEventNotFired(self.vm, 'domain-tag-add',
  169. kwargs={'tag': 'testtag2'})
  170. class TC_21_Features(qubes.tests.QubesTestCase):
  171. def setUp(self):
  172. super(TC_21_Features, self).setUp()
  173. self.vm = qubes.tests.TestEmitter()
  174. self.features = qubes.vm.Features(self.vm)
  175. def test_000_set(self):
  176. self.features['testfeature'] = 'value'
  177. self.assertEventFired(self.vm, 'domain-feature-set',
  178. kwargs={'key': 'testfeature', 'value': 'value'})
  179. def test_001_set_existing(self):
  180. self.features['test'] = 'value'
  181. self.vm.fired_events.clear()
  182. self.features['test'] = 'value'
  183. self.assertEventFired(self.vm, 'domain-feature-set',
  184. kwargs={'key': 'test', 'value': 'value'})
  185. def test_002_unset(self):
  186. self.features['test'] = 'value'
  187. self.vm.fired_events.clear()
  188. del self.features['test']
  189. self.assertEventFired(self.vm, 'domain-feature-delete',
  190. kwargs={'key': 'test'})
  191. def test_003_unset_not_present(self):
  192. with self.assertRaises(KeyError):
  193. del self.features['test']
  194. self.assertEventNotFired(self.vm, 'domain-feature-delete')
  195. def test_004_set_bool_true(self):
  196. self.features['test'] = True
  197. self.assertTrue(self.features['test'])
  198. self.assertEventFired(self.vm, 'domain-feature-set',
  199. kwargs={'key': 'test', 'value': '1'})
  200. def test_005_set_bool_false(self):
  201. self.features['test'] = False
  202. self.assertFalse(self.features['test'])
  203. self.assertEventFired(self.vm, 'domain-feature-set',
  204. kwargs={'key': 'test', 'value': ''})
  205. def test_006_set_int(self):
  206. self.features['test'] = 123
  207. self.assertEventFired(self.vm, 'domain-feature-set',
  208. kwargs={'key': 'test', 'value': '123'})
  209. def test_007_clear(self):
  210. self.features['test'] = 'value1'
  211. self.features['test2'] = 'value2'
  212. self.vm.fired_events.clear()
  213. self.features.clear()
  214. self.assertEventFired(self.vm, 'domain-feature-delete',
  215. kwargs={'key': 'test'})
  216. self.assertEventFired(self.vm, 'domain-feature-delete',
  217. kwargs={'key': 'test2'})
  218. def test_008_update(self):
  219. self.features['test'] = 'value'
  220. self.features['test2'] = 'value2'
  221. self.vm.fired_events.clear()
  222. self.features.update({'test2': 'value3', 'test3': 'value4'})
  223. self.assertEqual(self.features['test2'], 'value3')
  224. self.assertEqual(self.features['test3'], 'value4')
  225. self.assertEqual(self.features['test'], 'value')
  226. self.assertEventFired(self.vm, 'domain-feature-set',
  227. kwargs={'key': 'test2', 'value': 'value3'})
  228. self.assertEventFired(self.vm, 'domain-feature-set',
  229. kwargs={'key': 'test3', 'value': 'value4'})