init.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # pylint: disable=protected-access,pointless-statement
  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 library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library 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 GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import unittest
  22. import uuid
  23. import lxml.etree
  24. import qubes
  25. import qubes.app
  26. import qubes.events
  27. import qubes.vm
  28. import qubes.tests
  29. class TC_00_Label(qubes.tests.QubesTestCase):
  30. def test_000_constructor(self):
  31. label = qubes.Label(1, '#cc0000', 'red')
  32. self.assertEqual(label.index, 1)
  33. self.assertEqual(label.color, '#cc0000')
  34. self.assertEqual(label.name, 'red')
  35. self.assertEqual(label.icon, 'appvm-red')
  36. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  37. def test_001_fromxml(self):
  38. xml = lxml.etree.XML('''
  39. <qubes version="3">
  40. <labels>
  41. <label id="label-1" color="#cc0000">red</label>
  42. </labels>
  43. </qubes>
  44. ''')
  45. node = xml.xpath('//label')[0]
  46. label = qubes.Label.fromxml(node)
  47. self.assertEqual(label.index, 1)
  48. self.assertEqual(label.color, '#cc0000')
  49. self.assertEqual(label.name, 'red')
  50. self.assertEqual(label.icon, 'appvm-red')
  51. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  52. class TC_10_property(qubes.tests.QubesTestCase):
  53. def setUp(self):
  54. super().setUp()
  55. try:
  56. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  57. testprop1 = qubes.property('testprop1')
  58. except: # pylint: disable=bare-except
  59. self.skipTest('MyTestHolder class definition failed')
  60. self.holder = MyTestHolder(None)
  61. def test_000_init(self):
  62. pass
  63. def test_001_hash(self):
  64. hash(self.holder.__class__.testprop1)
  65. def test_002_eq(self):
  66. self.assertEqual(qubes.property('testprop2'),
  67. qubes.property('testprop2'))
  68. def test_010_set(self):
  69. self.holder.testprop1 = 'testvalue'
  70. self.assertEventFired(self.holder, 'property-pre-set:testprop1',
  71. kwargs={'name': 'testprop1', 'newvalue': 'testvalue'})
  72. self.assertEventFired(self.holder, 'property-set:testprop1',
  73. kwargs={'name': 'testprop1', 'newvalue': 'testvalue'})
  74. def test_020_get(self):
  75. self.holder.testprop1 = 'testvalue'
  76. self.assertEqual(self.holder.testprop1, 'testvalue')
  77. def test_021_get_unset(self):
  78. with self.assertRaises(AttributeError):
  79. self.holder.testprop1
  80. def test_022_get_default(self):
  81. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  82. testprop1 = qubes.property('testprop1', default='defaultvalue')
  83. holder = MyTestHolder(None)
  84. self.assertEqual(holder.testprop1, 'defaultvalue')
  85. self.assertEqual(
  86. type(holder).testprop1.get_default(holder),
  87. 'defaultvalue')
  88. def test_023_get_default_func(self):
  89. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  90. testprop1 = qubes.property('testprop1',
  91. default=(lambda self: 'defaultvalue'))
  92. holder = MyTestHolder(None)
  93. self.assertEqual(holder.testprop1, 'defaultvalue')
  94. holder.testprop1 = 'testvalue'
  95. self.assertEqual(holder.testprop1, 'testvalue')
  96. self.assertEventFired(holder, 'property-pre-set:testprop1',
  97. kwargs={'name': 'testprop1',
  98. 'newvalue': 'testvalue',
  99. 'oldvalue': 'defaultvalue'})
  100. self.assertEventFired(holder, 'property-set:testprop1',
  101. kwargs={'name': 'testprop1',
  102. 'newvalue': 'testvalue',
  103. 'oldvalue': 'defaultvalue'})
  104. def test_030_set_setter(self):
  105. def setter(self2, prop, value):
  106. self.assertIs(self2, holder)
  107. self.assertIs(prop, MyTestHolder.testprop1)
  108. self.assertEqual(value, 'testvalue')
  109. return 'settervalue'
  110. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  111. testprop1 = qubes.property('testprop1', setter=setter)
  112. holder = MyTestHolder(None)
  113. holder.testprop1 = 'testvalue'
  114. self.assertEqual(holder.testprop1, 'settervalue')
  115. self.assertEventFired(holder, 'property-pre-set:testprop1',
  116. kwargs={'name': 'testprop1', 'newvalue': 'settervalue'})
  117. self.assertEventFired(holder, 'property-set:testprop1',
  118. kwargs={'name': 'testprop1', 'newvalue': 'settervalue'})
  119. def test_031_set_type(self):
  120. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  121. testprop1 = qubes.property('testprop1', type=int)
  122. holder = MyTestHolder(None)
  123. holder.testprop1 = '5'
  124. self.assertEqual(holder.testprop1, 5)
  125. self.assertNotEqual(holder.testprop1, '5')
  126. self.assertEventFired(holder, 'property-pre-set:testprop1',
  127. kwargs={'name': 'testprop1', 'newvalue': 5})
  128. self.assertEventFired(holder, 'property-set:testprop1',
  129. kwargs={'name': 'testprop1', 'newvalue': 5})
  130. def test_080_delete(self):
  131. self.holder.testprop1 = 'testvalue'
  132. try:
  133. if self.holder.testprop1 != 'testvalue':
  134. self.skipTest('testprop1 value is wrong')
  135. except AttributeError:
  136. self.skipTest('testprop1 value is wrong')
  137. del self.holder.testprop1
  138. with self.assertRaises(AttributeError):
  139. self.holder.testprop1
  140. self.assertEventFired(self.holder, 'property-pre-del:testprop1',
  141. kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
  142. self.assertEventFired(self.holder, 'property-del:testprop1',
  143. kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
  144. def test_081_delete_by_assign(self):
  145. self.holder.testprop1 = 'testvalue'
  146. try:
  147. if self.holder.testprop1 != 'testvalue':
  148. self.skipTest('testprop1 value is wrong')
  149. except AttributeError:
  150. self.skipTest('testprop1 value is wrong')
  151. self.holder.testprop1 = qubes.property.DEFAULT
  152. with self.assertRaises(AttributeError):
  153. self.holder.testprop1
  154. def test_082_delete_default(self):
  155. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  156. testprop1 = qubes.property('testprop1', default='defaultvalue')
  157. holder = MyTestHolder(None)
  158. holder.testprop1 = 'testvalue'
  159. try:
  160. if holder.testprop1 != 'testvalue':
  161. self.skipTest('testprop1 value is wrong')
  162. except AttributeError:
  163. self.skipTest('testprop1 value is wrong')
  164. del holder.testprop1
  165. self.assertEqual(holder.testprop1, 'defaultvalue')
  166. self.assertEventFired(holder, 'property-pre-del:testprop1', kwargs={
  167. 'name': 'testprop1', 'oldvalue': 'testvalue'})
  168. self.assertEventFired(holder, 'property-del:testprop1', kwargs={
  169. 'name': 'testprop1', 'oldvalue': 'testvalue'})
  170. def test_090_write_once_set(self):
  171. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  172. testprop1 = qubes.property('testprop1', write_once=True)
  173. holder = MyTestHolder(None)
  174. holder.testprop1 = 'testvalue'
  175. with self.assertRaises(AttributeError):
  176. holder.testprop1 = 'testvalue2'
  177. def test_091_write_once_delete(self):
  178. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  179. testprop1 = qubes.property('testprop1', write_once=True)
  180. holder = MyTestHolder(None)
  181. holder.testprop1 = 'testvalue'
  182. with self.assertRaises(AttributeError):
  183. del holder.testprop1
  184. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  185. testprop1 = qubes.property('testprop1', order=0)
  186. testprop2 = qubes.property('testprop2', order=1, save_via_ref=True)
  187. testprop3 = qubes.property('testprop3', order=2, default='testdefault')
  188. testprop4 = qubes.property('testprop4', order=3)
  189. class TC_20_PropertyHolder(qubes.tests.QubesTestCase):
  190. def setUp(self):
  191. super().setUp()
  192. xml = lxml.etree.XML('''
  193. <qubes version="3">
  194. <properties>
  195. <property name="testprop1">testvalue1</property>
  196. <property name="testprop2" ref="testref2" />
  197. </properties>
  198. </qubes>
  199. ''')
  200. self.holder = TestHolder(xml)
  201. def test_000_property_list(self):
  202. self.assertListEqual([p.__name__ for p in self.holder.property_list()],
  203. ['testprop1', 'testprop2', 'testprop3', 'testprop4'])
  204. def test_001_property_get_def(self):
  205. self.assertIs(
  206. self.holder.property_get_def('testprop1'), TestHolder.testprop1)
  207. self.assertIs(self.holder.property_get_def(TestHolder.testprop1),
  208. TestHolder.testprop1)
  209. def test_002_load_properties(self):
  210. self.holder.load_properties()
  211. self.assertEventFired(self.holder, 'property-set:testprop1')
  212. self.assertEqual(self.holder.testprop1, 'testvalue1')
  213. self.assertEqual(self.holder.testprop2, 'testref2')
  214. self.assertEqual(self.holder.testprop3, 'testdefault')
  215. with self.assertRaises(AttributeError):
  216. self.holder.testprop4
  217. def test_003_property_is_default(self):
  218. self.holder.load_properties()
  219. self.assertFalse(self.holder.property_is_default('testprop1'))
  220. self.assertFalse(self.holder.property_is_default('testprop2'))
  221. self.assertTrue(self.holder.property_is_default('testprop3'))
  222. self.assertTrue(self.holder.property_is_default('testprop4'))
  223. with self.assertRaises(AttributeError):
  224. self.holder.property_is_default('testprop5')
  225. @unittest.skip('test not implemented')
  226. def test_004_property_init(self):
  227. pass
  228. @unittest.skip('test not implemented')
  229. def test_005_clone_properties(self):
  230. pass
  231. def test_006_xml_properties(self):
  232. self.holder.load_properties()
  233. elements = self.holder.xml_properties()
  234. elements_with_defaults = self.holder.xml_properties(with_defaults=True)
  235. self.assertEqual(len(elements), 2)
  236. self.assertEqual(len(elements_with_defaults), 3)
  237. expected_prop1 = lxml.etree.Element('property', name='testprop1')
  238. expected_prop1.text = 'testvalue1'
  239. self.assertXMLEqual(elements_with_defaults[0], expected_prop1)
  240. expected_prop2 = lxml.etree.Element('property',
  241. name='testprop2', ref='testref2')
  242. self.assertXMLEqual(elements_with_defaults[1], expected_prop2)
  243. expected_prop3 = lxml.etree.Element('property', name='testprop3')
  244. expected_prop3.text = 'testdefault'
  245. self.assertXMLEqual(elements_with_defaults[2], expected_prop3)
  246. def test_007_property_get_default(self):
  247. self.assertEqual(
  248. self.holder.property_get_default('testprop3'),
  249. 'testdefault')
  250. with self.assertRaises(AttributeError):
  251. self.holder.property_get_default('testprop1'),
  252. @unittest.skip('test not implemented')
  253. def test_010_property_require(self):
  254. pass
  255. class TestVM(qubes.vm.BaseVM):
  256. qid = qubes.property('qid', type=int)
  257. name = qubes.property('name')
  258. uuid = uuid.uuid5(uuid.NAMESPACE_DNS, 'testvm')
  259. def __lt__(self, other):
  260. try:
  261. return self.name < other.name
  262. except AttributeError:
  263. return NotImplemented
  264. class MockLibvirt(object):
  265. def undefine(self):
  266. pass
  267. libvirt_domain = MockLibvirt()
  268. def is_halted(self):
  269. return True
  270. def get_power_state(self):
  271. return "Halted"
  272. class TestApp(qubes.tests.TestEmitter):
  273. pass
  274. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  275. def setUp(self):
  276. super().setUp()
  277. self.app = TestApp()
  278. self.vms = qubes.app.VMCollection(self.app)
  279. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  280. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  281. self.addCleanup(self.cleanup_testvm)
  282. def cleanup_testvm(self):
  283. self.vms.close()
  284. self.testvm1.close()
  285. self.testvm2.close()
  286. del self.testvm1
  287. del self.testvm2
  288. del self.vms
  289. del self.app
  290. def test_000_contains(self):
  291. self.vms._dict = {1: self.testvm1}
  292. self.assertIn(1, self.vms)
  293. self.assertIn('testvm1', self.vms)
  294. self.assertIn(self.testvm1, self.vms)
  295. self.assertNotIn(2, self.vms)
  296. self.assertNotIn('testvm2', self.vms)
  297. self.assertNotIn(self.testvm2, self.vms)
  298. def test_001_getitem(self):
  299. self.vms._dict = {1: self.testvm1}
  300. self.assertIs(self.vms[1], self.testvm1)
  301. self.assertIs(self.vms['testvm1'], self.testvm1)
  302. self.assertIs(self.vms[self.testvm1], self.testvm1)
  303. def test_002_add(self):
  304. self.vms.add(self.testvm1)
  305. self.assertIn(1, self.vms)
  306. self.assertEventFired(self.app, 'domain-add',
  307. kwargs={'vm': self.testvm1})
  308. with self.assertRaises(TypeError):
  309. self.vms.add(object())
  310. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  311. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  312. with self.assertRaises(ValueError):
  313. self.vms.add(testvm_qid_collision)
  314. with self.assertRaises(ValueError):
  315. self.vms.add(testvm_name_collision)
  316. def test_003_qids(self):
  317. self.vms.add(self.testvm1)
  318. self.vms.add(self.testvm2)
  319. self.assertCountEqual(self.vms.qids(), [1, 2])
  320. self.assertCountEqual(self.vms.keys(), [1, 2])
  321. def test_004_names(self):
  322. self.vms.add(self.testvm1)
  323. self.vms.add(self.testvm2)
  324. self.assertCountEqual(self.vms.names(), ['testvm1', 'testvm2'])
  325. def test_005_vms(self):
  326. self.vms.add(self.testvm1)
  327. self.vms.add(self.testvm2)
  328. self.assertCountEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  329. self.assertCountEqual(self.vms.values(), [self.testvm1, self.testvm2])
  330. def test_006_items(self):
  331. self.vms.add(self.testvm1)
  332. self.vms.add(self.testvm2)
  333. self.assertCountEqual(self.vms.items(),
  334. [(1, self.testvm1), (2, self.testvm2)])
  335. def test_007_len(self):
  336. self.vms.add(self.testvm1)
  337. self.vms.add(self.testvm2)
  338. self.assertEqual(len(self.vms), 2)
  339. def test_008_delitem(self):
  340. self.vms.add(self.testvm1)
  341. self.vms.add(self.testvm2)
  342. del self.vms['testvm2']
  343. self.assertCountEqual(self.vms.vms(), [self.testvm1])
  344. self.assertEventFired(self.app, 'domain-delete',
  345. kwargs={'vm': self.testvm2})
  346. def test_100_get_new_unused_qid(self):
  347. self.vms.add(self.testvm1)
  348. self.vms.add(self.testvm2)
  349. self.vms.get_new_unused_qid()
  350. # def test_200_get_vms_based_on(self):
  351. # pass
  352. # def test_201_get_vms_connected_to(self):
  353. # pass