devices.py 9.2 KB

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