init.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access,pointless-statement
  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 os
  25. import unittest
  26. import lxml.etree
  27. import qubes
  28. import qubes.events
  29. import qubes.vm
  30. import qubes.tests
  31. class TC_00_Label(qubes.tests.QubesTestCase):
  32. def test_000_constructor(self):
  33. label = qubes.Label(1, '#cc0000', 'red')
  34. self.assertEqual(label.index, 1)
  35. self.assertEqual(label.color, '#cc0000')
  36. self.assertEqual(label.name, 'red')
  37. self.assertEqual(label.icon, 'appvm-red')
  38. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  39. def test_001_fromxml(self):
  40. xml = lxml.etree.XML('''
  41. <qubes version="3">
  42. <labels>
  43. <label id="label-1" color="#cc0000">red</label>
  44. </labels>
  45. </qubes>
  46. ''')
  47. node = xml.xpath('//label')[0]
  48. label = qubes.Label.fromxml(node)
  49. self.assertEqual(label.index, 1)
  50. self.assertEqual(label.color, '#cc0000')
  51. self.assertEqual(label.name, 'red')
  52. self.assertEqual(label.icon, 'appvm-red')
  53. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  54. class TC_10_property(qubes.tests.QubesTestCase):
  55. def setUp(self):
  56. try:
  57. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  58. testprop1 = qubes.property('testprop1')
  59. except: # pylint: disable=bare-except
  60. self.skipTest('MyTestHolder class definition failed')
  61. self.holder = MyTestHolder(None)
  62. def test_000_init(self):
  63. pass
  64. def test_001_hash(self):
  65. hash(self.holder.__class__.testprop1)
  66. def test_002_eq(self):
  67. self.assertEquals(qubes.property('testprop2'),
  68. qubes.property('testprop2'))
  69. def test_010_set(self):
  70. self.holder.testprop1 = 'testvalue'
  71. self.assertEventFired(self.holder, 'property-pre-set:testprop1')
  72. self.assertEventFired(self.holder, 'property-set:testprop1')
  73. def test_020_get(self):
  74. self.holder.testprop1 = 'testvalue'
  75. self.assertEqual(self.holder.testprop1, 'testvalue')
  76. def test_021_get_unset(self):
  77. with self.assertRaises(AttributeError):
  78. self.holder.testprop1
  79. def test_022_get_default(self):
  80. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  81. testprop1 = qubes.property('testprop1', default='defaultvalue')
  82. holder = MyTestHolder(None)
  83. self.assertEqual(holder.testprop1, 'defaultvalue')
  84. def test_023_get_default_func(self):
  85. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  86. testprop1 = qubes.property('testprop1',
  87. default=(lambda self: 'defaultvalue'))
  88. holder = MyTestHolder(None)
  89. self.assertEqual(holder.testprop1, 'defaultvalue')
  90. holder.testprop1 = 'testvalue'
  91. self.assertEqual(holder.testprop1, 'testvalue')
  92. def test_030_set_setter(self):
  93. def setter(self2, prop, value):
  94. self.assertIs(self2, holder)
  95. self.assertIs(prop, MyTestHolder.testprop1)
  96. self.assertEquals(value, 'testvalue')
  97. return 'settervalue'
  98. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  99. testprop1 = qubes.property('testprop1', setter=setter)
  100. holder = MyTestHolder(None)
  101. holder.testprop1 = 'testvalue'
  102. self.assertEqual(holder.testprop1, 'settervalue')
  103. def test_031_set_type(self):
  104. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  105. testprop1 = qubes.property('testprop1', type=int)
  106. holder = MyTestHolder(None)
  107. holder.testprop1 = '5'
  108. self.assertEqual(holder.testprop1, 5)
  109. self.assertNotEqual(holder.testprop1, '5')
  110. def test_090_delete(self):
  111. self.holder.testprop1 = 'testvalue'
  112. try:
  113. if self.holder.testprop1 != 'testvalue':
  114. self.skipTest('testprop1 value is wrong')
  115. except AttributeError:
  116. self.skipTest('testprop1 value is wrong')
  117. del self.holder.testprop1
  118. with self.assertRaises(AttributeError):
  119. self.holder.testprop1
  120. def test_090_delete_by_assign(self):
  121. self.holder.testprop1 = 'testvalue'
  122. try:
  123. if self.holder.testprop1 != 'testvalue':
  124. self.skipTest('testprop1 value is wrong')
  125. except AttributeError:
  126. self.skipTest('testprop1 value is wrong')
  127. self.holder.testprop1 = qubes.property.DEFAULT
  128. with self.assertRaises(AttributeError):
  129. self.holder.testprop1
  130. def test_092_delete_default(self):
  131. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  132. testprop1 = qubes.property('testprop1', default='defaultvalue')
  133. holder = MyTestHolder(None)
  134. holder.testprop1 = 'testvalue'
  135. try:
  136. if holder.testprop1 != 'testvalue':
  137. self.skipTest('testprop1 value is wrong')
  138. except AttributeError:
  139. self.skipTest('testprop1 value is wrong')
  140. del holder.testprop1
  141. self.assertEqual(holder.testprop1, 'defaultvalue')
  142. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  143. testprop1 = qubes.property('testprop1', order=0)
  144. testprop2 = qubes.property('testprop2', order=1, save_via_ref=True)
  145. testprop3 = qubes.property('testprop3', order=2, default='testdefault')
  146. testprop4 = qubes.property('testprop4', order=3)
  147. class TC_20_PropertyHolder(qubes.tests.QubesTestCase):
  148. def setUp(self):
  149. xml = lxml.etree.XML('''
  150. <qubes version="3">
  151. <properties>
  152. <property name="testprop1">testvalue1</property>
  153. <property name="testprop2" ref="testref2" />
  154. </properties>
  155. </qubes>
  156. ''')
  157. self.holder = TestHolder(xml)
  158. def test_000_property_list(self):
  159. self.assertListEqual([p.__name__ for p in self.holder.property_list()],
  160. ['testprop1', 'testprop2', 'testprop3', 'testprop4'])
  161. def test_001_property_get_def(self):
  162. self.assertIs(
  163. self.holder.property_get_def('testprop1'), TestHolder.testprop1)
  164. self.assertIs(self.holder.property_get_def(TestHolder.testprop1),
  165. TestHolder.testprop1)
  166. @unittest.expectedFailure
  167. def test_002_load_properties(self):
  168. self.holder.load_properties()
  169. self.assertEventFired(self.holder, 'property-loaded')
  170. self.assertEventNotFired(self.holder, 'property-set:testprop1')
  171. self.assertEquals(self.holder.testprop1, 'testvalue1')
  172. self.assertEquals(self.holder.testprop2, 'testref2')
  173. self.assertEquals(self.holder.testprop3, 'testdefault')
  174. with self.assertRaises(AttributeError):
  175. self.holder.testprop4
  176. def test_003_property_is_default(self):
  177. self.holder.load_properties()
  178. self.assertFalse(self.holder.property_is_default('testprop1'))
  179. self.assertFalse(self.holder.property_is_default('testprop2'))
  180. self.assertTrue(self.holder.property_is_default('testprop3'))
  181. self.assertTrue(self.holder.property_is_default('testprop4'))
  182. with self.assertRaises(AttributeError):
  183. self.holder.property_is_default('testprop5')
  184. @unittest.skip('test not implemented')
  185. def test_004_property_init(self):
  186. pass
  187. @unittest.skip('test not implemented')
  188. def test_005_clone_properties(self):
  189. pass
  190. def test_006_xml_properties(self):
  191. self.holder.load_properties()
  192. elements = self.holder.xml_properties()
  193. elements_with_defaults = self.holder.xml_properties(with_defaults=True)
  194. self.assertEqual(len(elements), 2)
  195. self.assertEqual(len(elements_with_defaults), 3)
  196. expected_prop1 = lxml.etree.Element('property', name='testprop1')
  197. expected_prop1.text = 'testvalue1'
  198. self.assertXMLEqual(elements_with_defaults[0], expected_prop1)
  199. expected_prop2 = lxml.etree.Element('property',
  200. name='testprop2', ref='testref2')
  201. self.assertXMLEqual(elements_with_defaults[1], expected_prop2)
  202. expected_prop3 = lxml.etree.Element('property', name='testprop3')
  203. expected_prop3.text = 'testdefault'
  204. self.assertXMLEqual(elements_with_defaults[2], expected_prop3)
  205. @unittest.skip('test not implemented')
  206. def test_010_property_require(self):
  207. pass
  208. class TestVM(qubes.vm.BaseVM):
  209. qid = qubes.property('qid', type=int)
  210. name = qubes.property('name')
  211. netid = qid
  212. class TestApp(qubes.tests.TestEmitter):
  213. pass
  214. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  215. def setUp(self):
  216. self.app = TestApp()
  217. self.vms = qubes.VMCollection(self.app)
  218. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  219. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  220. def test_000_contains(self):
  221. self.vms._dict = {1: self.testvm1}
  222. self.assertIn(1, self.vms)
  223. self.assertIn('testvm1', self.vms)
  224. self.assertIn(self.testvm1, self.vms)
  225. self.assertNotIn(2, self.vms)
  226. self.assertNotIn('testvm2', self.vms)
  227. self.assertNotIn(self.testvm2, self.vms)
  228. def test_001_getitem(self):
  229. self.vms._dict = {1: self.testvm1}
  230. self.assertIs(self.vms[1], self.testvm1)
  231. self.assertIs(self.vms['testvm1'], self.testvm1)
  232. self.assertIs(self.vms[self.testvm1], self.testvm1)
  233. def test_002_add(self):
  234. self.vms.add(self.testvm1)
  235. self.assertIn(1, self.vms)
  236. self.assertEventFired(self.app, 'domain-added', args=[self.testvm1])
  237. with self.assertRaises(TypeError):
  238. self.vms.add(object())
  239. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  240. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  241. with self.assertRaises(ValueError):
  242. self.vms.add(testvm_qid_collision)
  243. with self.assertRaises(ValueError):
  244. self.vms.add(testvm_name_collision)
  245. def test_003_qids(self):
  246. self.vms.add(self.testvm1)
  247. self.vms.add(self.testvm2)
  248. self.assertItemsEqual(self.vms.qids(), [1, 2])
  249. self.assertItemsEqual(self.vms.keys(), [1, 2])
  250. def test_004_names(self):
  251. self.vms.add(self.testvm1)
  252. self.vms.add(self.testvm2)
  253. self.assertItemsEqual(self.vms.names(), ['testvm1', 'testvm2'])
  254. def test_005_vms(self):
  255. self.vms.add(self.testvm1)
  256. self.vms.add(self.testvm2)
  257. self.assertItemsEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  258. self.assertItemsEqual(self.vms.values(), [self.testvm1, self.testvm2])
  259. def test_006_items(self):
  260. self.vms.add(self.testvm1)
  261. self.vms.add(self.testvm2)
  262. self.assertItemsEqual(self.vms.items(),
  263. [(1, self.testvm1), (2, self.testvm2)])
  264. def test_007_len(self):
  265. self.vms.add(self.testvm1)
  266. self.vms.add(self.testvm2)
  267. self.assertEqual(len(self.vms), 2)
  268. def test_008_delitem(self):
  269. self.vms.add(self.testvm1)
  270. self.vms.add(self.testvm2)
  271. del self.vms['testvm2']
  272. self.assertItemsEqual(self.vms.vms(), [self.testvm1])
  273. self.assertEventFired(self.app, 'domain-deleted', args=[self.testvm2])
  274. def test_100_get_new_unused_qid(self):
  275. self.vms.add(self.testvm1)
  276. self.vms.add(self.testvm2)
  277. self.vms.get_new_unused_qid()
  278. def test_101_get_new_unused_netid(self):
  279. self.vms.add(self.testvm1)
  280. self.vms.add(self.testvm2)
  281. self.vms.get_new_unused_netid()
  282. # def test_200_get_vms_based_on(self):
  283. # pass
  284. # def test_201_get_vms_connected_to(self):
  285. # pass
  286. class TC_90_Qubes(qubes.tests.QubesTestCase):
  287. @qubes.tests.skipUnlessDom0
  288. def test_000_init_empty(self):
  289. # pylint: disable=no-self-use,unused-variable,bare-except
  290. try:
  291. os.unlink('/tmp/qubestest.xml')
  292. except:
  293. pass
  294. app = qubes.Qubes.create_empty_store('/tmp/qubestest.xml')
  295. @qubes.tests.skipUnlessGit
  296. def test_900_example_xml_in_doc(self):
  297. self.assertXMLIsValid(
  298. lxml.etree.parse(open(
  299. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  300. 'qubes.rng')