init.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access
  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 lxml.etree
  25. import qubes
  26. import qubes.events
  27. import qubes.vm
  28. import qubes.tests
  29. class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
  30. def setUp(self):
  31. self.emitter = qubes.tests.TestEmitter()
  32. self.collection = qubes.vm.DeviceCollection(self.emitter, 'testclass')
  33. def test_000_init(self):
  34. self.assertFalse(self.collection._set)
  35. def test_001_attach(self):
  36. self.collection.attach('testdev')
  37. self.assertEventFired(self.emitter, 'device-pre-attached:testclass')
  38. self.assertEventFired(self.emitter, 'device-attached:testclass')
  39. self.assertEventNotFired(self.emitter, 'device-pre-detached:testclass')
  40. self.assertEventNotFired(self.emitter, 'device-detached:testclass')
  41. def test_002_detach(self):
  42. self.collection.attach('testdev')
  43. self.collection.detach('testdev')
  44. self.assertEventFired(self.emitter, 'device-pre-attached:testclass')
  45. self.assertEventFired(self.emitter, 'device-attached:testclass')
  46. self.assertEventFired(self.emitter, 'device-pre-detached:testclass')
  47. self.assertEventFired(self.emitter, 'device-detached:testclass')
  48. def test_010_empty_detach(self):
  49. with self.assertRaises(LookupError):
  50. self.collection.detach('testdev')
  51. def test_011_double_attach(self):
  52. self.collection.attach('testdev')
  53. with self.assertRaises(LookupError):
  54. self.collection.attach('testdev')
  55. def test_012_double_detach(self):
  56. self.collection.attach('testdev')
  57. self.collection.detach('testdev')
  58. with self.assertRaises(LookupError):
  59. self.collection.detach('testdev')
  60. class TC_01_DeviceManager(qubes.tests.QubesTestCase):
  61. def setUp(self):
  62. self.emitter = qubes.tests.TestEmitter()
  63. self.manager = qubes.vm.DeviceManager(self.emitter)
  64. def test_000_init(self):
  65. self.assertEqual(self.manager, {})
  66. def test_001_missing(self):
  67. self.manager['testclass'].attach('testdev')
  68. self.assertEventFired(self.emitter, 'device-attached:testclass')
  69. class TestVM(qubes.vm.BaseVM):
  70. qid = qubes.property('qid', type=int)
  71. name = qubes.property('name')
  72. testprop = qubes.property('testprop')
  73. testlabel = qubes.property('testlabel')
  74. defaultprop = qubes.property('defaultprop', default='defaultvalue')
  75. class TC_10_BaseVM(qubes.tests.QubesTestCase):
  76. def setUp(self):
  77. self.xml = lxml.etree.XML('''
  78. <qubes version="3"> <!-- xmlns="https://qubes-os.org/QubesXML/1" -->
  79. <labels>
  80. <label id="label-1" color="#cc0000">red</label>
  81. </labels>
  82. <domains>
  83. <domain id="domain-1" class="TestVM">
  84. <properties>
  85. <property name="qid">1</property>
  86. <property name="name">domain1</property>
  87. <property name="testprop">testvalue</property>
  88. <property name="testlabel" ref="label-1" />
  89. </properties>
  90. <tags>
  91. <tag name="testtag">tagvalue</tag>
  92. </tags>
  93. <services>
  94. <service>testservice</service>
  95. <service enabled="True">enabledservice</service>
  96. <service enabled="False">disabledservice</service>
  97. </services>
  98. <devices class="pci">
  99. <device>00:11.22</device>
  100. </devices>
  101. <devices class="usb" />
  102. <devices class="audio-in" />
  103. <devices class="firewire" />
  104. <devices class="i2c" />
  105. <devices class="isa" />
  106. </domain>
  107. </domains>
  108. </qubes>
  109. ''')
  110. def test_000_load(self):
  111. node = self.xml.xpath('//domain')[0]
  112. vm = TestVM(None, node)
  113. vm.load_properties(load_stage=None)
  114. self.assertEqual(vm.qid, 1)
  115. self.assertEqual(vm.testprop, 'testvalue')
  116. self.assertEqual(vm.testprop, 'testvalue')
  117. self.assertEqual(vm.testlabel, 'label-1')
  118. self.assertEqual(vm.defaultprop, 'defaultvalue')
  119. self.assertEqual(vm.tags, {'testtag': 'tagvalue'})
  120. self.assertEqual(vm.services, {
  121. 'testservice': True,
  122. 'enabledservice': True,
  123. 'disabledservice': False,
  124. })
  125. self.assertItemsEqual(vm.devices.keys(), ('pci',))
  126. self.assertItemsEqual(vm.devices['pci'], ('00:11.22',))
  127. self.assertXMLIsValid(vm.__xml__(), 'domain.rng')
  128. def test_001_nxproperty(self):
  129. xml = lxml.etree.XML('''
  130. <qubes version="3">
  131. <domains>
  132. <domain id="domain-1" class="TestVM">
  133. <properties>
  134. <property name="qid">1</property>
  135. <property name="name">domain1</property>
  136. <property name="nxproperty">nxvalue</property>
  137. </properties>
  138. </domain>
  139. </domains>
  140. </qubes>
  141. ''')
  142. node = xml.xpath('//domain')[0]
  143. with self.assertRaises(TypeError):
  144. TestVM(None, node)