init.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-pre-reset:testprop1',
  143. kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
  144. self.assertEventFired(self.holder, 'property-del:testprop1',
  145. kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
  146. self.assertEventFired(self.holder, 'property-reset:testprop1',
  147. kwargs={'name': 'testprop1', 'oldvalue': 'testvalue'})
  148. def test_081_delete_by_assign(self):
  149. self.holder.testprop1 = 'testvalue'
  150. try:
  151. if self.holder.testprop1 != 'testvalue':
  152. self.skipTest('testprop1 value is wrong')
  153. except AttributeError:
  154. self.skipTest('testprop1 value is wrong')
  155. self.holder.testprop1 = qubes.property.DEFAULT
  156. with self.assertRaises(AttributeError):
  157. self.holder.testprop1
  158. def test_082_delete_default(self):
  159. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  160. testprop1 = qubes.property('testprop1', default='defaultvalue')
  161. holder = MyTestHolder(None)
  162. holder.testprop1 = 'testvalue'
  163. try:
  164. if holder.testprop1 != 'testvalue':
  165. self.skipTest('testprop1 value is wrong')
  166. except AttributeError:
  167. self.skipTest('testprop1 value is wrong')
  168. del holder.testprop1
  169. self.assertEqual(holder.testprop1, 'defaultvalue')
  170. self.assertEventFired(holder, 'property-pre-del:testprop1', kwargs={
  171. 'name': 'testprop1', 'oldvalue': 'testvalue'})
  172. self.assertEventFired(holder, 'property-pre-reset:testprop1', kwargs={
  173. 'name': 'testprop1', 'oldvalue': 'testvalue'})
  174. self.assertEventFired(holder, 'property-del:testprop1', kwargs={
  175. 'name': 'testprop1', 'oldvalue': 'testvalue'})
  176. self.assertEventFired(holder, 'property-reset:testprop1', kwargs={
  177. 'name': 'testprop1', 'oldvalue': 'testvalue'})
  178. def test_090_write_once_set(self):
  179. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  180. testprop1 = qubes.property('testprop1', write_once=True)
  181. holder = MyTestHolder(None)
  182. holder.testprop1 = 'testvalue'
  183. with self.assertRaises(AttributeError):
  184. holder.testprop1 = 'testvalue2'
  185. def test_091_write_once_delete(self):
  186. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  187. testprop1 = qubes.property('testprop1', write_once=True)
  188. holder = MyTestHolder(None)
  189. holder.testprop1 = 'testvalue'
  190. with self.assertRaises(AttributeError):
  191. del holder.testprop1
  192. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  193. testprop1 = qubes.property('testprop1', order=0)
  194. testprop2 = qubes.property('testprop2', order=1, save_via_ref=True)
  195. testprop3 = qubes.property('testprop3', order=2, default='testdefault')
  196. testprop4 = qubes.property('testprop4', order=3)
  197. class TC_20_PropertyHolder(qubes.tests.QubesTestCase):
  198. def setUp(self):
  199. super().setUp()
  200. xml = lxml.etree.XML('''
  201. <qubes version="3">
  202. <properties>
  203. <property name="testprop1">testvalue1</property>
  204. <property name="testprop2" ref="testref2" />
  205. </properties>
  206. </qubes>
  207. ''')
  208. self.holder = TestHolder(xml)
  209. def test_000_property_list(self):
  210. self.assertListEqual([p.__name__ for p in self.holder.property_list()],
  211. ['testprop1', 'testprop2', 'testprop3', 'testprop4'])
  212. def test_001_property_get_def(self):
  213. self.assertIs(
  214. self.holder.property_get_def('testprop1'), TestHolder.testprop1)
  215. self.assertIs(self.holder.property_get_def(TestHolder.testprop1),
  216. TestHolder.testprop1)
  217. def test_002_load_properties(self):
  218. self.holder.load_properties()
  219. self.assertEventFired(self.holder, 'property-set:testprop1')
  220. self.assertEqual(self.holder.testprop1, 'testvalue1')
  221. self.assertEqual(self.holder.testprop2, 'testref2')
  222. self.assertEqual(self.holder.testprop3, 'testdefault')
  223. with self.assertRaises(AttributeError):
  224. self.holder.testprop4
  225. def test_003_property_is_default(self):
  226. self.holder.load_properties()
  227. self.assertFalse(self.holder.property_is_default('testprop1'))
  228. self.assertFalse(self.holder.property_is_default('testprop2'))
  229. self.assertTrue(self.holder.property_is_default('testprop3'))
  230. self.assertTrue(self.holder.property_is_default('testprop4'))
  231. with self.assertRaises(AttributeError):
  232. self.holder.property_is_default('testprop5')
  233. @unittest.skip('test not implemented')
  234. def test_004_property_init(self):
  235. pass
  236. @unittest.skip('test not implemented')
  237. def test_005_clone_properties(self):
  238. pass
  239. def test_006_xml_properties(self):
  240. self.holder.load_properties()
  241. elements = self.holder.xml_properties()
  242. elements_with_defaults = self.holder.xml_properties(with_defaults=True)
  243. self.assertEqual(len(elements), 2)
  244. self.assertEqual(len(elements_with_defaults), 3)
  245. expected_prop1 = lxml.etree.Element('property', name='testprop1')
  246. expected_prop1.text = 'testvalue1'
  247. self.assertXMLEqual(elements_with_defaults[0], expected_prop1)
  248. expected_prop2 = lxml.etree.Element('property',
  249. name='testprop2', ref='testref2')
  250. self.assertXMLEqual(elements_with_defaults[1], expected_prop2)
  251. expected_prop3 = lxml.etree.Element('property', name='testprop3')
  252. expected_prop3.text = 'testdefault'
  253. self.assertXMLEqual(elements_with_defaults[2], expected_prop3)
  254. def test_007_property_get_default(self):
  255. self.assertEqual(
  256. self.holder.property_get_default('testprop3'),
  257. 'testdefault')
  258. with self.assertRaises(AttributeError):
  259. self.holder.property_get_default('testprop1'),
  260. @unittest.skip('test not implemented')
  261. def test_010_property_require(self):
  262. pass
  263. class TestVM(qubes.vm.BaseVM):
  264. qid = qubes.property('qid', type=int)
  265. name = qubes.property('name')
  266. uuid = uuid.uuid5(uuid.NAMESPACE_DNS, 'testvm')
  267. def __lt__(self, other):
  268. try:
  269. return self.name < other.name
  270. except AttributeError:
  271. return NotImplemented
  272. class MockLibvirt(object):
  273. def undefine(self):
  274. pass
  275. libvirt_domain = MockLibvirt()
  276. def is_halted(self):
  277. return True
  278. def get_power_state(self):
  279. return "Halted"
  280. class TestApp(qubes.tests.TestEmitter):
  281. pass
  282. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  283. def setUp(self):
  284. super().setUp()
  285. self.app = TestApp()
  286. self.vms = qubes.app.VMCollection(self.app)
  287. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  288. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  289. self.addCleanup(self.cleanup_testvm)
  290. def cleanup_testvm(self):
  291. self.vms.close()
  292. self.testvm1.close()
  293. self.testvm2.close()
  294. del self.testvm1
  295. del self.testvm2
  296. del self.vms
  297. del self.app
  298. def test_000_contains(self):
  299. self.vms._dict = {1: self.testvm1}
  300. self.assertIn(1, self.vms)
  301. self.assertIn('testvm1', self.vms)
  302. self.assertIn(self.testvm1, self.vms)
  303. self.assertNotIn(2, self.vms)
  304. self.assertNotIn('testvm2', self.vms)
  305. self.assertNotIn(self.testvm2, self.vms)
  306. def test_001_getitem(self):
  307. self.vms._dict = {1: self.testvm1}
  308. self.assertIs(self.vms[1], self.testvm1)
  309. self.assertIs(self.vms['testvm1'], self.testvm1)
  310. self.assertIs(self.vms[self.testvm1], self.testvm1)
  311. def test_002_add(self):
  312. self.vms.add(self.testvm1)
  313. self.assertIn(1, self.vms)
  314. self.assertEventFired(self.app, 'domain-add',
  315. kwargs={'vm': self.testvm1})
  316. with self.assertRaises(TypeError):
  317. self.vms.add(object())
  318. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  319. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  320. with self.assertRaises(ValueError):
  321. self.vms.add(testvm_qid_collision)
  322. with self.assertRaises(ValueError):
  323. self.vms.add(testvm_name_collision)
  324. def test_003_qids(self):
  325. self.vms.add(self.testvm1)
  326. self.vms.add(self.testvm2)
  327. self.assertCountEqual(self.vms.qids(), [1, 2])
  328. self.assertCountEqual(self.vms.keys(), [1, 2])
  329. def test_004_names(self):
  330. self.vms.add(self.testvm1)
  331. self.vms.add(self.testvm2)
  332. self.assertCountEqual(self.vms.names(), ['testvm1', 'testvm2'])
  333. def test_005_vms(self):
  334. self.vms.add(self.testvm1)
  335. self.vms.add(self.testvm2)
  336. self.assertCountEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  337. self.assertCountEqual(self.vms.values(), [self.testvm1, self.testvm2])
  338. def test_006_items(self):
  339. self.vms.add(self.testvm1)
  340. self.vms.add(self.testvm2)
  341. self.assertCountEqual(self.vms.items(),
  342. [(1, self.testvm1), (2, self.testvm2)])
  343. def test_007_len(self):
  344. self.vms.add(self.testvm1)
  345. self.vms.add(self.testvm2)
  346. self.assertEqual(len(self.vms), 2)
  347. def test_008_delitem(self):
  348. self.vms.add(self.testvm1)
  349. self.vms.add(self.testvm2)
  350. del self.vms['testvm2']
  351. self.assertCountEqual(self.vms.vms(), [self.testvm1])
  352. self.assertEventFired(self.app, 'domain-delete',
  353. kwargs={'vm': self.testvm2})
  354. def test_100_get_new_unused_qid(self):
  355. self.vms.add(self.testvm1)
  356. self.vms.add(self.testvm2)
  357. self.vms.get_new_unused_qid()
  358. # def test_200_get_vms_based_on(self):
  359. # pass
  360. # def test_201_get_vms_connected_to(self):
  361. # pass