init.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #!/usr/bin/python2 -O
  2. import os
  3. import sys
  4. import unittest
  5. import lxml.etree
  6. import qubes
  7. import qubes.events
  8. import qubes.vm
  9. import qubes.tests
  10. class TC_00_Label(qubes.tests.QubesTestCase):
  11. def test_000_constructor(self):
  12. label = qubes.Label(1, '#cc0000', 'red')
  13. self.assertEqual(label.index, 1)
  14. self.assertEqual(label.color, '#cc0000')
  15. self.assertEqual(label.name, 'red')
  16. self.assertEqual(label.icon, 'appvm-red')
  17. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  18. def test_001_fromxml(self):
  19. xml = lxml.etree.XML('''
  20. <qubes version="3">
  21. <labels>
  22. <label id="label-1" color="#cc0000">red</label>
  23. </labels>
  24. </qubes>
  25. ''')
  26. node = xml.xpath('//label')[0]
  27. label = qubes.Label.fromxml(node)
  28. self.assertEqual(label.index, 1)
  29. self.assertEqual(label.color, '#cc0000')
  30. self.assertEqual(label.name, 'red')
  31. self.assertEqual(label.icon, 'appvm-red')
  32. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  33. class TC_10_property(qubes.tests.QubesTestCase):
  34. def setUp(self):
  35. try:
  36. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  37. testprop1 = qubes.property('testprop1')
  38. except:
  39. self.skipTest('TestHolder class definition failed')
  40. self.holder = TestHolder(None)
  41. def test_000_init(self):
  42. pass
  43. def test_001_hash(self):
  44. hash(self.holder.__class__.testprop1)
  45. def test_002_eq(self):
  46. self.assertEquals(qubes.property('testprop2'),
  47. qubes.property('testprop2'))
  48. def test_010_set(self):
  49. self.holder.testprop1 = 'testvalue'
  50. self.assertEventFired(self.holder, 'property-pre-set:testprop1')
  51. self.assertEventFired(self.holder, 'property-set:testprop1')
  52. def test_020_get(self):
  53. self.holder.testprop1 = 'testvalue'
  54. self.assertEqual(self.holder.testprop1, 'testvalue')
  55. def test_021_get_unset(self):
  56. with self.assertRaises(AttributeError):
  57. self.holder.testprop1
  58. def test_022_get_default(self):
  59. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  60. testprop1 = qubes.property('testprop1', default='defaultvalue')
  61. holder = TestHolder(None)
  62. self.assertEqual(holder.testprop1, 'defaultvalue')
  63. def test_023_get_default_func(self):
  64. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  65. testprop1 = qubes.property('testprop1', default=(lambda self: 'defaultvalue'))
  66. holder = TestHolder(None)
  67. self.assertEqual(holder.testprop1, 'defaultvalue')
  68. holder.testprop1 = 'testvalue'
  69. self.assertEqual(holder.testprop1, 'testvalue')
  70. def test_030_set_setter(self):
  71. def setter(self2, prop, value):
  72. self.assertIs(self2, holder)
  73. self.assertIs(prop, TestHolder.testprop1)
  74. self.assertEquals(value, 'testvalue')
  75. return 'settervalue'
  76. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  77. testprop1 = qubes.property('testprop1', setter=setter)
  78. holder = TestHolder(None)
  79. holder.testprop1 = 'testvalue'
  80. self.assertEqual(holder.testprop1, 'settervalue')
  81. def test_031_set_type(self):
  82. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  83. testprop1 = qubes.property('testprop1', type=int)
  84. holder = TestHolder(None)
  85. holder.testprop1 = '5'
  86. self.assertEqual(holder.testprop1, 5)
  87. self.assertNotEqual(holder.testprop1, '5')
  88. def test_090_delete(self):
  89. self.holder.testprop1 = 'testvalue'
  90. try:
  91. if self.holder.testprop1 != 'testvalue':
  92. self.skipTest('testprop1 value is wrong')
  93. except AttributeError:
  94. self.skipTest('testprop1 value is wrong')
  95. del self.holder.testprop1
  96. with self.assertRaises(AttributeError):
  97. self.holder.testprop
  98. def test_090_delete_by_assign(self):
  99. self.holder.testprop1 = 'testvalue'
  100. try:
  101. if self.holder.testprop1 != 'testvalue':
  102. self.skipTest('testprop1 value is wrong')
  103. except AttributeError:
  104. self.skipTest('testprop1 value is wrong')
  105. self.holder.testprop1 = qubes.property.DEFAULT
  106. with self.assertRaises(AttributeError):
  107. self.holder.testprop
  108. def test_092_delete_default(self):
  109. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  110. testprop1 = qubes.property('testprop1', default='defaultvalue')
  111. holder = TestHolder(None)
  112. holder.testprop1 = 'testvalue'
  113. try:
  114. if holder.testprop1 != 'testvalue':
  115. self.skipTest('testprop1 value is wrong')
  116. except AttributeError:
  117. self.skipTest('testprop1 value is wrong')
  118. del holder.testprop1
  119. self.assertEqual(holder.testprop1, 'defaultvalue')
  120. class TestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  121. testprop1 = qubes.property('testprop1', order=0)
  122. testprop2 = qubes.property('testprop2', order=1, save_via_ref=True)
  123. testprop3 = qubes.property('testprop3', order=2, default='testdefault')
  124. testprop4 = qubes.property('testprop4', order=3)
  125. class TC_20_PropertyHolder(qubes.tests.QubesTestCase):
  126. def setUp(self):
  127. xml = lxml.etree.XML('''
  128. <qubes version="3">
  129. <properties>
  130. <property name="testprop1">testvalue1</property>
  131. <property name="testprop2" ref="testref2" />
  132. </properties>
  133. </qubes>
  134. ''')
  135. self.holder = TestHolder(xml)
  136. @unittest.expectedFailure
  137. def test_000_load_properties(self):
  138. self.holder.load_properties()
  139. self.assertEventFired(self.holder, 'property-loaded')
  140. self.assertEventNotFired(self.holder, 'property-set:testprop1')
  141. self.assertEquals(self.holder.testprop1, 'testvalue1')
  142. self.assertEquals(self.holder.testprop2, 'testref2')
  143. self.assertEquals(self.holder.testprop3, 'testdefault')
  144. with self.assertRaises(AttributeError):
  145. self.holder.testprop4
  146. def test_001_save_properties(self):
  147. self.holder.load_properties()
  148. elements = self.holder.xml_properties()
  149. elements_with_defaults = self.holder.xml_properties(with_defaults=True)
  150. self.assertEqual(len(elements), 2)
  151. self.assertEqual(len(elements_with_defaults), 3)
  152. expected_prop1 = lxml.etree.Element('property', name='testprop1')
  153. expected_prop1.text = 'testvalue1'
  154. self.assertXMLEqual(elements_with_defaults[0], expected_prop1)
  155. expected_prop2 = lxml.etree.Element('property', name='testprop2', ref='testref2')
  156. self.assertXMLEqual(elements_with_defaults[1], expected_prop2)
  157. expected_prop3 = lxml.etree.Element('property', name='testprop3')
  158. expected_prop3.text = 'testdefault'
  159. self.assertXMLEqual(elements_with_defaults[2], expected_prop3)
  160. class TestVM(qubes.vm.BaseVM):
  161. qid = qubes.property('qid', type=int)
  162. name = qubes.property('name')
  163. netid = qid
  164. class TestApp(qubes.tests.TestEmitter):
  165. pass
  166. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  167. def setUp(self):
  168. self.app = TestApp()
  169. self.vms = qubes.VMCollection(self.app)
  170. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  171. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  172. def test_000_contains(self):
  173. self.vms._dict = {1: self.testvm1}
  174. self.assertIn(1, self.vms)
  175. self.assertIn('testvm1', self.vms)
  176. self.assertIn(self.testvm1, self.vms)
  177. self.assertNotIn(2, self.vms)
  178. self.assertNotIn('testvm2', self.vms)
  179. self.assertNotIn(self.testvm2, self.vms)
  180. def test_001_getitem(self):
  181. self.vms._dict = {1: self.testvm1}
  182. self.assertIs(self.vms[1], self.testvm1)
  183. self.assertIs(self.vms['testvm1'], self.testvm1)
  184. self.assertIs(self.vms[self.testvm1], self.testvm1)
  185. def test_002_add(self):
  186. self.vms.add(self.testvm1)
  187. self.assertIn(1, self.vms)
  188. self.assertEventFired(self.app, 'domain-added', args=[self.testvm1])
  189. with self.assertRaises(TypeError):
  190. self.vms.add(object())
  191. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  192. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  193. with self.assertRaises(ValueError):
  194. self.vms.add(testvm_qid_collision)
  195. with self.assertRaises(ValueError):
  196. self.vms.add(testvm_name_collision)
  197. def test_003_qids(self):
  198. self.vms.add(self.testvm1)
  199. self.vms.add(self.testvm2)
  200. self.assertItemsEqual(self.vms.qids(), [1, 2])
  201. self.assertItemsEqual(self.vms.keys(), [1, 2])
  202. def test_004_names(self):
  203. self.vms.add(self.testvm1)
  204. self.vms.add(self.testvm2)
  205. self.assertItemsEqual(self.vms.names(), ['testvm1', 'testvm2'])
  206. def test_005_vms(self):
  207. self.vms.add(self.testvm1)
  208. self.vms.add(self.testvm2)
  209. self.assertItemsEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  210. self.assertItemsEqual(self.vms.values(), [self.testvm1, self.testvm2])
  211. def test_006_items(self):
  212. self.vms.add(self.testvm1)
  213. self.vms.add(self.testvm2)
  214. self.assertItemsEqual(self.vms.items(), [(1, self.testvm1), (2, self.testvm2)])
  215. def test_007_len(self):
  216. self.vms.add(self.testvm1)
  217. self.vms.add(self.testvm2)
  218. self.assertEqual(len(self.vms), 2)
  219. def test_008_delitem(self):
  220. self.vms.add(self.testvm1)
  221. self.vms.add(self.testvm2)
  222. del self.vms['testvm2']
  223. self.assertItemsEqual(self.vms.vms(), [self.testvm1])
  224. self.assertEventFired(self.app, 'domain-deleted', args=[self.testvm2])
  225. def test_100_get_new_unused_qid(self):
  226. self.vms.add(self.testvm1)
  227. self.vms.add(self.testvm2)
  228. self.vms.get_new_unused_qid()
  229. def test_101_get_new_unused_netid(self):
  230. self.vms.add(self.testvm1)
  231. self.vms.add(self.testvm2)
  232. self.vms.get_new_unused_netid()
  233. # def test_200_get_vms_based_on(self):
  234. # pass
  235. # def test_201_get_vms_connected_to(self):
  236. # pass
  237. class TC_90_Qubes(qubes.tests.QubesTestCase):
  238. @qubes.tests.skipUnlessGit
  239. def test_900_example_xml_in_doc(self):
  240. self.assertXMLIsValid(
  241. lxml.etree.parse(open(
  242. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  243. 'qubes.rng')