devices.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import qubes.devices
  23. import qubes.tests
  24. class TestDevice(qubes.devices.DeviceInfo):
  25. # pylint: disable=too-few-public-methods
  26. pass
  27. class TestVMCollection(dict):
  28. def __iter__(self):
  29. return iter(set(self.values()))
  30. class TestApp(object):
  31. # pylint: disable=too-few-public-methods
  32. def __init__(self):
  33. self.domains = TestVMCollection()
  34. class TestVM(qubes.tests.TestEmitter):
  35. def __init__(self, app, name, *args, **kwargs):
  36. super(TestVM, self).__init__(*args, **kwargs)
  37. self.app = app
  38. self.name = name
  39. self.device = TestDevice(self, 'testdev', 'Description')
  40. self.events_enabled = True
  41. self.devices = {
  42. 'testclass': qubes.devices.DeviceCollection(self, 'testclass')
  43. }
  44. self.app.domains[name] = self
  45. self.app.domains[self] = self
  46. self.running = False
  47. def __str__(self):
  48. return self.name
  49. @qubes.events.handler('device-list-attached:testclass')
  50. def dev_testclass_list_attached(self, event, persistent = False):
  51. for vm in self.app.domains:
  52. if vm.device.frontend_domain == self:
  53. yield (vm.device, {})
  54. @qubes.events.handler('device-list:testclass')
  55. def dev_testclass_list(self, event):
  56. yield self.device
  57. def is_halted(self):
  58. return not self.running
  59. def is_running(self):
  60. return self.running
  61. class TC_00_DeviceCollection(qubes.tests.QubesTestCase):
  62. def setUp(self):
  63. super().setUp()
  64. self.app = TestApp()
  65. self.emitter = TestVM(self.app, 'vm')
  66. self.app.domains['vm'] = self.emitter
  67. self.device = self.emitter.device
  68. self.collection = self.emitter.devices['testclass']
  69. self.assignment = qubes.devices.DeviceAssignment(
  70. backend_domain=self.device.backend_domain,
  71. ident=self.device.ident,
  72. persistent=True
  73. )
  74. def test_000_init(self):
  75. self.assertFalse(self.collection._set)
  76. def test_001_attach(self):
  77. self.loop.run_until_complete(self.collection.attach(self.assignment))
  78. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  79. self.assertEventFired(self.emitter, 'device-attach:testclass')
  80. self.assertEventNotFired(self.emitter, 'device-pre-detach:testclass')
  81. self.assertEventNotFired(self.emitter, 'device-detach:testclass')
  82. def test_002_detach(self):
  83. self.loop.run_until_complete(self.collection.attach(self.assignment))
  84. self.loop.run_until_complete(self.collection.detach(self.assignment))
  85. self.assertEventFired(self.emitter, 'device-pre-attach:testclass')
  86. self.assertEventFired(self.emitter, 'device-attach:testclass')
  87. self.assertEventFired(self.emitter, 'device-pre-detach:testclass')
  88. self.assertEventFired(self.emitter, 'device-detach:testclass')
  89. def test_010_empty_detach(self):
  90. with self.assertRaises(LookupError):
  91. self.loop.run_until_complete(
  92. self.collection.detach(self.assignment))
  93. def test_011_double_attach(self):
  94. self.loop.run_until_complete(self.collection.attach(self.assignment))
  95. with self.assertRaises(qubes.devices.DeviceAlreadyAttached):
  96. self.loop.run_until_complete(
  97. self.collection.attach(self.assignment))
  98. def test_012_double_detach(self):
  99. self.loop.run_until_complete(self.collection.attach(self.assignment))
  100. self.loop.run_until_complete(self.collection.detach(self.assignment))
  101. with self.assertRaises(qubes.devices.DeviceNotAttached):
  102. self.loop.run_until_complete(
  103. self.collection.detach(self.assignment))
  104. def test_013_list_attached_persistent(self):
  105. self.assertEqual(set([]), set(self.collection.persistent()))
  106. self.loop.run_until_complete(self.collection.attach(self.assignment))
  107. self.assertEventFired(self.emitter, 'device-list-attached:testclass')
  108. self.assertEqual({self.device}, set(self.collection.persistent()))
  109. self.assertEqual(set([]),
  110. set(self.collection.attached()))
  111. def test_014_list_attached_non_persistent(self):
  112. self.assignment.persistent = False
  113. self.emitter.running = True
  114. self.loop.run_until_complete(self.collection.attach(self.assignment))
  115. # device-attach event not implemented, so manipulate object manually
  116. self.device.frontend_domain = self.emitter
  117. self.assertEqual({self.device},
  118. set(self.collection.attached()))
  119. self.assertEqual(set([]),
  120. set(self.collection.persistent()))
  121. self.assertEqual({self.device},
  122. set(self.collection.attached()))
  123. self.assertEventFired(self.emitter, 'device-list-attached:testclass')
  124. def test_015_list_available(self):
  125. self.assertEqual({self.device}, set(self.collection))
  126. self.assertEventFired(self.emitter, 'device-list:testclass')
  127. def test_020_update_persistent_to_false(self):
  128. self.emitter.running = True
  129. self.assertEqual(set([]), set(self.collection.persistent()))
  130. self.loop.run_until_complete(self.collection.attach(self.assignment))
  131. # device-attach event not implemented, so manipulate object manually
  132. self.device.frontend_domain = self.emitter
  133. self.assertEqual({self.device}, set(self.collection.persistent()))
  134. self.assertEqual({self.device}, set(self.collection.attached()))
  135. self.assertEqual({self.device}, set(self.collection.persistent()))
  136. self.assertEqual({self.device}, set(self.collection.attached()))
  137. self.collection.update_persistent(self.device, False)
  138. self.assertEqual(set(), set(self.collection.persistent()))
  139. self.assertEqual({self.device}, set(self.collection.attached()))
  140. def test_021_update_persistent_to_true(self):
  141. self.assignment.persistent = False
  142. self.emitter.running = True
  143. self.assertEqual(set([]), set(self.collection.persistent()))
  144. self.loop.run_until_complete(self.collection.attach(self.assignment))
  145. # device-attach event not implemented, so manipulate object manually
  146. self.device.frontend_domain = self.emitter
  147. self.assertEqual(set(), set(self.collection.persistent()))
  148. self.assertEqual({self.device}, set(self.collection.attached()))
  149. self.assertEqual(set(), set(self.collection.persistent()))
  150. self.assertEqual({self.device}, set(self.collection.attached()))
  151. self.collection.update_persistent(self.device, True)
  152. self.assertEqual({self.device}, set(self.collection.persistent()))
  153. self.assertEqual({self.device}, set(self.collection.attached()))
  154. def test_022_update_persistent_reject_not_running(self):
  155. self.assertEqual(set([]), set(self.collection.persistent()))
  156. self.loop.run_until_complete(self.collection.attach(self.assignment))
  157. self.assertEqual({self.device}, set(self.collection.persistent()))
  158. self.assertEqual(set(), set(self.collection.attached()))
  159. with self.assertRaises(qubes.exc.QubesVMNotStartedError):
  160. self.collection.update_persistent(self.device, False)
  161. def test_023_update_persistent_reject_not_attached(self):
  162. self.assertEqual(set(), set(self.collection.persistent()))
  163. self.assertEqual(set(), set(self.collection.attached()))
  164. self.emitter.running = True
  165. with self.assertRaises(qubes.exc.QubesValueError):
  166. self.collection.update_persistent(self.device, True)
  167. with self.assertRaises(qubes.exc.QubesValueError):
  168. self.collection.update_persistent(self.device, False)
  169. class TC_01_DeviceManager(qubes.tests.QubesTestCase):
  170. def setUp(self):
  171. super().setUp()
  172. self.app = TestApp()
  173. self.emitter = TestVM(self.app, 'vm')
  174. self.manager = qubes.devices.DeviceManager(self.emitter)
  175. def test_000_init(self):
  176. self.assertEqual(self.manager, {})
  177. def test_001_missing(self):
  178. device = TestDevice(self.emitter.app.domains['vm'], 'testdev')
  179. assignment = qubes.devices.DeviceAssignment(
  180. backend_domain=device.backend_domain,
  181. ident=device.ident,
  182. persistent=True)
  183. self.loop.run_until_complete(
  184. self.manager['testclass'].attach(assignment))
  185. self.assertEventFired(self.emitter, 'device-attach:testclass')