init.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import os
  24. import unittest
  25. import lxml.etree
  26. import qubes
  27. import qubes.events
  28. import qubes.vm
  29. import qubes.tests
  30. class TC_00_Label(qubes.tests.QubesTestCase):
  31. def test_000_constructor(self):
  32. label = qubes.Label(1, '#cc0000', 'red')
  33. self.assertEqual(label.index, 1)
  34. self.assertEqual(label.color, '#cc0000')
  35. self.assertEqual(label.name, 'red')
  36. self.assertEqual(label.icon, 'appvm-red')
  37. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  38. def test_001_fromxml(self):
  39. xml = lxml.etree.XML('''
  40. <qubes version="3">
  41. <labels>
  42. <label id="label-1" color="#cc0000">red</label>
  43. </labels>
  44. </qubes>
  45. ''')
  46. node = xml.xpath('//label')[0]
  47. label = qubes.Label.fromxml(node)
  48. self.assertEqual(label.index, 1)
  49. self.assertEqual(label.color, '#cc0000')
  50. self.assertEqual(label.name, 'red')
  51. self.assertEqual(label.icon, 'appvm-red')
  52. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  53. class TC_10_property(qubes.tests.QubesTestCase):
  54. def setUp(self):
  55. try:
  56. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  57. testprop1 = qubes.property('testprop1')
  58. except:
  59. self.skipTest('TestHolder class definition failed')
  60. self.holder = TestHolder(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.assertEquals(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. self.assertEventFired(self.holder, 'property-set:testprop1')
  72. def test_020_get(self):
  73. self.holder.testprop1 = 'testvalue'
  74. self.assertEqual(self.holder.testprop1, 'testvalue')
  75. def test_021_get_unset(self):
  76. with self.assertRaises(AttributeError):
  77. self.holder.testprop1
  78. def test_022_get_default(self):
  79. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  80. testprop1 = qubes.property('testprop1', default='defaultvalue')
  81. holder = TestHolder(None)
  82. self.assertEqual(holder.testprop1, 'defaultvalue')
  83. def test_023_get_default_func(self):
  84. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  85. testprop1 = qubes.property('testprop1',
  86. default=(lambda self: 'defaultvalue'))
  87. holder = TestHolder(None)
  88. self.assertEqual(holder.testprop1, 'defaultvalue')
  89. holder.testprop1 = 'testvalue'
  90. self.assertEqual(holder.testprop1, 'testvalue')
  91. def test_030_set_setter(self):
  92. def setter(self2, prop, value):
  93. self.assertIs(self2, holder)
  94. self.assertIs(prop, TestHolder.testprop1)
  95. self.assertEquals(value, 'testvalue')
  96. return 'settervalue'
  97. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  98. testprop1 = qubes.property('testprop1', setter=setter)
  99. holder = TestHolder(None)
  100. holder.testprop1 = 'testvalue'
  101. self.assertEqual(holder.testprop1, 'settervalue')
  102. def test_031_set_type(self):
  103. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  104. testprop1 = qubes.property('testprop1', type=int)
  105. holder = TestHolder(None)
  106. holder.testprop1 = '5'
  107. self.assertEqual(holder.testprop1, 5)
  108. self.assertNotEqual(holder.testprop1, '5')
  109. def test_090_delete(self):
  110. self.holder.testprop1 = 'testvalue'
  111. try:
  112. if self.holder.testprop1 != 'testvalue':
  113. self.skipTest('testprop1 value is wrong')
  114. except AttributeError:
  115. self.skipTest('testprop1 value is wrong')
  116. del self.holder.testprop1
  117. with self.assertRaises(AttributeError):
  118. self.holder.testprop
  119. def test_090_delete_by_assign(self):
  120. self.holder.testprop1 = 'testvalue'
  121. try:
  122. if self.holder.testprop1 != 'testvalue':
  123. self.skipTest('testprop1 value is wrong')
  124. except AttributeError:
  125. self.skipTest('testprop1 value is wrong')
  126. self.holder.testprop1 = qubes.property.DEFAULT
  127. with self.assertRaises(AttributeError):
  128. self.holder.testprop
  129. def test_092_delete_default(self):
  130. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  131. testprop1 = qubes.property('testprop1', default='defaultvalue')
  132. holder = TestHolder(None)
  133. holder.testprop1 = 'testvalue'
  134. try:
  135. if holder.testprop1 != 'testvalue':
  136. self.skipTest('testprop1 value is wrong')
  137. except AttributeError:
  138. self.skipTest('testprop1 value is wrong')
  139. del holder.testprop1
  140. self.assertEqual(holder.testprop1, 'defaultvalue')
  141. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  142. testprop1 = qubes.property('testprop1', order=0)
  143. testprop2 = qubes.property('testprop2', order=1, save_via_ref=True)
  144. testprop3 = qubes.property('testprop3', order=2, default='testdefault')
  145. testprop4 = qubes.property('testprop4', order=3)
  146. class TC_20_PropertyHolder(qubes.tests.QubesTestCase):
  147. def setUp(self):
  148. xml = lxml.etree.XML('''
  149. <qubes version="3">
  150. <properties>
  151. <property name="testprop1">testvalue1</property>
  152. <property name="testprop2" ref="testref2" />
  153. </properties>
  154. </qubes>
  155. ''')
  156. self.holder = TestHolder(xml)
  157. @unittest.expectedFailure
  158. def test_000_load_properties(self):
  159. self.holder.load_properties()
  160. self.assertEventFired(self.holder, 'property-loaded')
  161. self.assertEventNotFired(self.holder, 'property-set:testprop1')
  162. self.assertEquals(self.holder.testprop1, 'testvalue1')
  163. self.assertEquals(self.holder.testprop2, 'testref2')
  164. self.assertEquals(self.holder.testprop3, 'testdefault')
  165. with self.assertRaises(AttributeError):
  166. self.holder.testprop4
  167. def test_001_save_properties(self):
  168. self.holder.load_properties()
  169. elements = self.holder.xml_properties()
  170. elements_with_defaults = self.holder.xml_properties(with_defaults=True)
  171. self.assertEqual(len(elements), 2)
  172. self.assertEqual(len(elements_with_defaults), 3)
  173. expected_prop1 = lxml.etree.Element('property', name='testprop1')
  174. expected_prop1.text = 'testvalue1'
  175. self.assertXMLEqual(elements_with_defaults[0], expected_prop1)
  176. expected_prop2 = lxml.etree.Element('property',
  177. name='testprop2', ref='testref2')
  178. self.assertXMLEqual(elements_with_defaults[1], expected_prop2)
  179. expected_prop3 = lxml.etree.Element('property', name='testprop3')
  180. expected_prop3.text = 'testdefault'
  181. self.assertXMLEqual(elements_with_defaults[2], expected_prop3)
  182. class TestVM(qubes.vm.BaseVM):
  183. qid = qubes.property('qid', type=int)
  184. name = qubes.property('name')
  185. netid = qid
  186. class TestApp(qubes.tests.TestEmitter):
  187. pass
  188. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  189. def setUp(self):
  190. self.app = TestApp()
  191. self.vms = qubes.VMCollection(self.app)
  192. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  193. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  194. def test_000_contains(self):
  195. self.vms._dict = {1: self.testvm1}
  196. self.assertIn(1, self.vms)
  197. self.assertIn('testvm1', self.vms)
  198. self.assertIn(self.testvm1, self.vms)
  199. self.assertNotIn(2, self.vms)
  200. self.assertNotIn('testvm2', self.vms)
  201. self.assertNotIn(self.testvm2, self.vms)
  202. def test_001_getitem(self):
  203. self.vms._dict = {1: self.testvm1}
  204. self.assertIs(self.vms[1], self.testvm1)
  205. self.assertIs(self.vms['testvm1'], self.testvm1)
  206. self.assertIs(self.vms[self.testvm1], self.testvm1)
  207. def test_002_add(self):
  208. self.vms.add(self.testvm1)
  209. self.assertIn(1, self.vms)
  210. self.assertEventFired(self.app, 'domain-added', args=[self.testvm1])
  211. with self.assertRaises(TypeError):
  212. self.vms.add(object())
  213. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  214. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  215. with self.assertRaises(ValueError):
  216. self.vms.add(testvm_qid_collision)
  217. with self.assertRaises(ValueError):
  218. self.vms.add(testvm_name_collision)
  219. def test_003_qids(self):
  220. self.vms.add(self.testvm1)
  221. self.vms.add(self.testvm2)
  222. self.assertItemsEqual(self.vms.qids(), [1, 2])
  223. self.assertItemsEqual(self.vms.keys(), [1, 2])
  224. def test_004_names(self):
  225. self.vms.add(self.testvm1)
  226. self.vms.add(self.testvm2)
  227. self.assertItemsEqual(self.vms.names(), ['testvm1', 'testvm2'])
  228. def test_005_vms(self):
  229. self.vms.add(self.testvm1)
  230. self.vms.add(self.testvm2)
  231. self.assertItemsEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  232. self.assertItemsEqual(self.vms.values(), [self.testvm1, self.testvm2])
  233. def test_006_items(self):
  234. self.vms.add(self.testvm1)
  235. self.vms.add(self.testvm2)
  236. self.assertItemsEqual(self.vms.items(),
  237. [(1, self.testvm1), (2, self.testvm2)])
  238. def test_007_len(self):
  239. self.vms.add(self.testvm1)
  240. self.vms.add(self.testvm2)
  241. self.assertEqual(len(self.vms), 2)
  242. def test_008_delitem(self):
  243. self.vms.add(self.testvm1)
  244. self.vms.add(self.testvm2)
  245. del self.vms['testvm2']
  246. self.assertItemsEqual(self.vms.vms(), [self.testvm1])
  247. self.assertEventFired(self.app, 'domain-deleted', args=[self.testvm2])
  248. def test_100_get_new_unused_qid(self):
  249. self.vms.add(self.testvm1)
  250. self.vms.add(self.testvm2)
  251. self.vms.get_new_unused_qid()
  252. def test_101_get_new_unused_netid(self):
  253. self.vms.add(self.testvm1)
  254. self.vms.add(self.testvm2)
  255. self.vms.get_new_unused_netid()
  256. # def test_200_get_vms_based_on(self):
  257. # pass
  258. # def test_201_get_vms_connected_to(self):
  259. # pass
  260. class TC_90_Qubes(qubes.tests.QubesTestCase):
  261. @qubes.tests.skipUnlessDom0
  262. def test_000_init_empty(self):
  263. try:
  264. os.unlink('/tmp/qubestest.xml')
  265. except:
  266. pass
  267. app = qubes.Qubes('/tmp/qubestest.xml')
  268. @qubes.tests.skipUnlessGit
  269. def test_900_example_xml_in_doc(self):
  270. self.assertXMLIsValid(
  271. lxml.etree.parse(open(
  272. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  273. 'qubes.rng')