init.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/python2 -O
  2. import sys
  3. import unittest
  4. import lxml.etree
  5. import qubes
  6. import qubes.events
  7. import qubes.vm
  8. import qubes.tests
  9. class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
  10. def setUp(self):
  11. self.emitter = qubes.tests.TestEmitter()
  12. self.collection = qubes.vm.DeviceCollection(self.emitter, 'testclass')
  13. def test_000_init(self):
  14. self.assertFalse(self.collection._set)
  15. def test_001_attach(self):
  16. self.collection.attach('testdev')
  17. self.assertEventFired(self.emitter, 'device-pre-attached:testclass')
  18. self.assertEventFired(self.emitter, 'device-attached:testclass')
  19. self.assertEventNotFired(self.emitter, 'device-pre-detached:testclass')
  20. self.assertEventNotFired(self.emitter, 'device-detached:testclass')
  21. def test_002_detach(self):
  22. self.collection.attach('testdev')
  23. self.collection.detach('testdev')
  24. self.assertEventFired(self.emitter, 'device-pre-attached:testclass')
  25. self.assertEventFired(self.emitter, 'device-attached:testclass')
  26. self.assertEventFired(self.emitter, 'device-pre-detached:testclass')
  27. self.assertEventFired(self.emitter, 'device-detached:testclass')
  28. def test_010_empty_detach(self):
  29. with self.assertRaises(LookupError):
  30. self.collection.detach('testdev')
  31. def test_011_double_attach(self):
  32. self.collection.attach('testdev')
  33. with self.assertRaises(LookupError):
  34. self.collection.attach('testdev')
  35. def test_012_double_detach(self):
  36. self.collection.attach('testdev')
  37. self.collection.detach('testdev')
  38. with self.assertRaises(LookupError):
  39. self.collection.detach('testdev')
  40. class TC_01_DeviceManager(qubes.tests.QubesTestCase):
  41. def setUp(self):
  42. self.emitter = qubes.tests.TestEmitter()
  43. self.manager = qubes.vm.DeviceManager(self.emitter)
  44. def test_000_init(self):
  45. self.assertEqual(self.manager, {})
  46. def test_001_missing(self):
  47. self.manager['testclass'].attach('testdev')
  48. self.assertEventFired(self.emitter, 'device-attached:testclass')
  49. class TestVM(qubes.vm.BaseVM):
  50. qid = qubes.property('qid', type=int)
  51. name = qubes.property('name')
  52. testprop = qubes.property('testprop')
  53. testlabel = qubes.property('testlabel')
  54. defaultprop = qubes.property('defaultprop', default='defaultvalue')
  55. class TC_10_BaseVM(qubes.tests.QubesTestCase):
  56. def setUp(self):
  57. self.xml = lxml.etree.XML('''
  58. <qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
  59. <labels>
  60. <label id="label-1" color="#cc0000">red</label>
  61. </labels>
  62. <domains>
  63. <domain id="domain-1" class="TestVM">
  64. <properties>
  65. <property name="qid">1</property>
  66. <property name="name">domain1</property>
  67. <property name="testprop">testvalue</property>
  68. <property name="testlabel" ref="label-1" />
  69. </properties>
  70. <tags>
  71. <tag name="testtag">tagvalue</tag>
  72. </tags>
  73. <services>
  74. <service>testservice</service>
  75. <service enabled="True">enabledservice</service>
  76. <service enabled="False">disabledservice</service>
  77. </services>
  78. <devices class="pci">
  79. <device>00:11.22</device>
  80. </devices>
  81. <devices class="usb" />
  82. <devices class="audio-in" />
  83. <devices class="firewire" />
  84. <devices class="i2c" />
  85. <devices class="isa" />
  86. </domain>
  87. </domains>
  88. </qubes>
  89. ''')
  90. def test_000_BaseVM_load(self):
  91. node = self.xml.xpath('//domain')[0]
  92. vm = TestVM.fromxml(None, node)
  93. self.assertEqual(vm.qid, 1)
  94. self.assertEqual(vm.testprop, 'testvalue')
  95. self.assertEqual(vm.testprop, 'testvalue')
  96. self.assertEqual(vm.testlabel, 'label-1')
  97. self.assertEqual(vm.defaultprop, 'defaultvalue')
  98. self.assertEqual(vm.tags, {'testtag': 'tagvalue'})
  99. self.assertEqual(vm.devices, {'pci': ['00:11.22']})
  100. self.assertEqual(vm.services, {
  101. 'testservice': True,
  102. 'enabledservice': True,
  103. 'disabledservice': False,
  104. })
  105. self.assertXMLIsValid(vm.__xml__(), '../../relaxng/domain.rng')
  106. def test_001_BaseVM_nxproperty(self):
  107. xml = lxml.etree.XML('''
  108. <qubes version="3">
  109. <domains>
  110. <domain id="domain-1" class="TestVM">
  111. <properties>
  112. <property name="nxproperty">nxvalue</property>
  113. </properties>
  114. </domain>
  115. </domains>
  116. </qubes>
  117. ''')
  118. node = xml.xpath('//domain')[0]
  119. with self.assertRaises(AttributeError):
  120. TestVM.fromxml(None, node)