init.py 5.6 KB

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